UltimateZoneBuilder/Source/Controls/ActionManager.cs

240 lines
6.1 KiB
C#
Raw Normal View History

2007-06-24 22:53:41 +00:00
#region ================== Copyright (c) 2007 Pascal vd Heiden
/*
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#endregion
#region ================== Namespaces
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using CodeImp.DoomBuilder.Properties;
using System.IO;
using CodeImp.DoomBuilder.IO;
using System.Collections;
2008-01-02 21:49:43 +00:00
using System.Reflection;
2007-06-24 22:53:41 +00:00
#endregion
namespace CodeImp.DoomBuilder.Controls
{
2008-01-02 21:49:43 +00:00
internal class ActionManager
2007-06-24 22:53:41 +00:00
{
#region ================== Constants
2008-01-02 21:49:43 +00:00
private const string ACTIONS_RESOURCE = "Actions.cfg";
2007-06-24 22:53:41 +00:00
#endregion
#region ================== Variables
// Actions
private Dictionary<string, Action> actions;
// Disposing
private bool isdisposed = false;
#endregion
#region ================== Properties
public Action this[string action] { get { if(actions.ContainsKey(action)) return actions[action]; else throw new ArgumentException("There is no such action \"" + action + "\""); } }
2007-06-24 22:53:41 +00:00
public bool IsDisposed { get { return isdisposed; } }
#endregion
#region ================== Constructor / Disposer
// Constructor
public ActionManager()
{
// Initialize
General.WriteLogLine("Starting action manager...");
2007-06-24 22:53:41 +00:00
actions = new Dictionary<string, Action>();
2008-01-02 21:49:43 +00:00
// Load all actions in this assembly
LoadActions(General.ThisAssembly);
2007-06-24 22:53:41 +00:00
// We have no destructor
GC.SuppressFinalize(this);
}
2008-01-02 21:49:43 +00:00
// Disposer
2007-06-24 22:53:41 +00:00
public void Dispose()
{
// Not already disposed?
if(!isdisposed)
{
// Clean up
// Done
isdisposed = true;
}
}
#endregion
#region ================== Actions
2008-01-02 21:49:43 +00:00
// This loads all actions from an assembly
public void LoadActions(Assembly asm)
2007-06-24 22:53:41 +00:00
{
Stream actionsdata;
StreamReader actionsreader;
Configuration cfg;
2008-01-13 21:23:59 +00:00
string name, title, desc, shortname;
bool amouse, akeys, ascroll, debugonly;
2008-01-02 21:49:43 +00:00
string[] resnames;
AssemblyName asmname = asm.GetName();
2007-06-24 22:53:41 +00:00
2008-01-02 21:49:43 +00:00
// Find a resource named Actions.cfg
resnames = asm.GetManifestResourceNames();
foreach(string rn in resnames)
{
// Found one?
if(rn.EndsWith(ACTIONS_RESOURCE, StringComparison.InvariantCultureIgnoreCase))
{
// Get a stream from the resource
actionsdata = asm.GetManifestResourceStream(rn);
actionsreader = new StreamReader(actionsdata, Encoding.ASCII);
// Load configuration from stream
cfg = new Configuration();
cfg.InputConfiguration(actionsreader.ReadToEnd());
// Done with the resource
actionsreader.Dispose();
actionsdata.Dispose();
// Go for all objects in the configuration
foreach(DictionaryEntry a in cfg.Root)
{
// Get action properties
2008-01-13 21:23:59 +00:00
shortname = a.Key.ToString();
name = asmname.Name.ToLowerInvariant() + "_" + shortname;
2008-01-02 21:49:43 +00:00
title = cfg.ReadSetting(a.Key + ".title", "[" + name + "]");
desc = cfg.ReadSetting(a.Key + ".description", "");
akeys = cfg.ReadSetting(a.Key + ".allowkeys", false);
amouse = cfg.ReadSetting(a.Key + ".allowmouse", false);
ascroll = cfg.ReadSetting(a.Key + ".allowscroll", false);
debugonly = cfg.ReadSetting(a.Key + ".debugonly", false);
// Check if action should be included
if(General.DebugBuild || !debugonly)
{
// Create an action
2008-01-13 21:23:59 +00:00
CreateAction(name, shortname, title, desc, akeys, amouse, ascroll);
}
2008-01-02 21:49:43 +00:00
}
}
}
}
2007-06-24 22:53:41 +00:00
2008-01-02 21:49:43 +00:00
// This manually creates an action
2008-01-13 21:23:59 +00:00
private void CreateAction(string name, string shortname, string title, string desc, bool allowkeys, bool allowmouse, bool allowscroll)
2008-01-02 21:49:43 +00:00
{
// Action does not exist yet?
if(!actions.ContainsKey(name))
2007-06-24 22:53:41 +00:00
{
2008-01-02 21:49:43 +00:00
// Read the key from configuration
int key = General.Settings.ReadSetting("shortcuts." + name, 0);
2007-06-24 22:53:41 +00:00
// Create an action
2008-01-13 21:23:59 +00:00
actions.Add(name, new Action(name, shortname, title, desc, key, allowkeys, allowmouse, allowscroll));
2008-01-02 21:49:43 +00:00
}
else
{
// Action already exists!
General.WriteLogLine("WARNING: Action '" + name + "' already exists. Action names must be unique!");
2007-06-24 22:53:41 +00:00
}
}
2008-01-02 21:49:43 +00:00
2007-06-24 22:53:41 +00:00
// This checks if a given action exists
public bool Exists(string action)
{
return actions.ContainsKey(action);
}
2007-09-29 15:43:59 +00:00
// This returns a list of all actions
public Action[] GetAllActions()
{
Action[] list = new Action[actions.Count];
actions.Values.CopyTo(list, 0);
return list;
}
2007-09-30 19:37:57 +00:00
// This saves the control settings
public void SaveSettings()
{
// Go for all actions
foreach(KeyValuePair<string, Action> a in actions)
{
// Write to configuration
General.Settings.WriteSetting("shortcuts." + a.Key, a.Value.ShortcutKey);
}
}
2007-06-24 22:53:41 +00:00
#endregion
#region ================== Shortcut Keys
// Removes all shortcut keys
public void RemoveShortcutKeys()
{
// Clear all keys
foreach(KeyValuePair<string, Action> a in actions)
a.Value.SetShortcutKey(0);
}
2008-01-13 21:23:59 +00:00
// This will call the associated actions for a keypress
2007-06-24 22:53:41 +00:00
public void InvokeByKey(int key)
{
// Go for all actions
foreach(KeyValuePair<string, Action> a in actions)
{
// This action is associated with this key?
if(a.Value.ShortcutKey == key)
{
// Invoke action
a.Value.Invoke();
}
}
}
2008-01-13 21:23:59 +00:00
// This returns all action names for a given key
public string[] GetActionsByKey(int key)
{
List<string> actionnames = new List<string>();
// Go for all actions
foreach(KeyValuePair<string, Action> a in actions)
{
// This action is associated with this key?
if(a.Value.ShortcutKey == key)
{
// List short name
actionnames.Add(a.Value.ShortName);
}
}
// Return result;
return actionnames.ToArray();
}
2007-06-24 22:53:41 +00:00
#endregion
}
}