added actions and shortcut keys

This commit is contained in:
codeimp 2007-06-24 22:53:41 +00:00
parent b9a6bfb3ea
commit 0ddda9fe00
10 changed files with 418 additions and 5 deletions

View file

@ -2,9 +2,9 @@
mainwindow
{
positionx = 124;
sizewidth = 739;
sizeheight = 572;
windowstate = 2;
sizeheight = 388;
sizewidth = 530;
positiony = 35;
}

View file

@ -22,7 +22,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<UseVSHostingProcess>false</UseVSHostingProcess>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -33,7 +33,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<UseVSHostingProcess>false</UseVSHostingProcess>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@ -47,8 +47,12 @@
<Compile Include="Editing\EditMode.cs" />
<Compile Include="Editing\FrozenOverviewMode.cs" />
<Compile Include="Editing\ViewClassicMode.cs" />
<Compile Include="Controls\ActionDelegate.cs" />
<Compile Include="Controls\Actions.cs" />
<Compile Include="Controls\ActionManager.cs" />
<Compile Include="General\ConfigurationInfo.cs" />
<Compile Include="General\MapManager.cs" />
<Compile Include="Controls\SpecialKeys.cs" />
<Compile Include="Geometry\Angle2D.cs" />
<Compile Include="Geometry\Line2D.cs" />
<Compile Include="Geometry\Vector2D.cs" />
@ -149,6 +153,7 @@
<None Include="Resources\Splash2small.png" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\Actions.cfg" />
<None Include="Resources\Splash2.png" />
</ItemGroup>
<ItemGroup>

View file

@ -0,0 +1,30 @@
#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;
#endregion
namespace CodeImp.DoomBuilder.Controls
{
internal delegate void ActionDelegate();
}

View file

@ -0,0 +1,156 @@
#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;
#endregion
namespace CodeImp.DoomBuilder.Controls
{
internal class ActionManager : IDisposable
{
#region ================== Constants
#endregion
#region ================== Variables
// Actions
private Dictionary<string, Action> actions;
// Disposing
private bool isdisposed = false;
#endregion
#region ================== Properties
public Action this[string action] { get { return actions[action]; } }
public bool IsDisposed { get { return isdisposed; } }
#endregion
#region ================== Constructor / Disposer
// Constructor
public ActionManager()
{
// Initialize
actions = new Dictionary<string, Action>();
// Load all actions
LoadActions();
// We have no destructor
GC.SuppressFinalize(this);
}
// Diposer
public void Dispose()
{
// Not already disposed?
if(!isdisposed)
{
// Clean up
// Done
isdisposed = true;
}
}
#endregion
#region ================== Actions
// This loads all actions
private void LoadActions()
{
Stream actionsdata;
StreamReader actionsreader;
Configuration cfg;
string name, title, desc;
// Get a stream from the resource
actionsdata = General.ThisAssembly.GetManifestResourceStream("CodeImp.DoomBuilder.Resources.Actions.cfg");
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
name = a.Key.ToString();
title = cfg.ReadSetting(name + ".title", "[" + name + "]");
desc = cfg.ReadSetting(name + ".description", "");
// Create an action
actions.Add(name, new Action(title, desc));
}
}
// This checks if a given action exists
public bool Exists(string action)
{
return actions.ContainsKey(action);
}
#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);
}
// This will call the associated action for a keypress
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();
}
}
}
#endregion
}
}

112
Source/Controls/Actions.cs Normal file
View file

@ -0,0 +1,112 @@
#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;
#endregion
namespace CodeImp.DoomBuilder.Controls
{
internal class Action
{
#region ================== Constants
public const string NEWMAP = "newmap";
public const string OPENMAP = "openmap";
public const string SAVEMAP = "savemap";
public const string SAVEMAPAS = "savemapas";
public const string SHOWOVERVIEW = "showoverview";
#endregion
#region ================== Variables
// Description
private string title;
private string description;
// Shortcut key
private int key;
// Delegate
private List<ActionDelegate> delegates;
#endregion
#region ================== Properties
public string Title { get { return title; } }
public string Description { get { return description; } }
public int ShortcutKey { get { return key; } }
#endregion
#region ================== Constructor / Disposer
// Constructor
public Action(string title, string description)
{
// Initialize
this.title = title;
this.description = description;
this.delegates = new List<ActionDelegate>();
}
// Destructor
~Action()
{
// Moo.
}
#endregion
#region ================== Methods
// This sets a new key for the action
public void SetShortcutKey(int key)
{
// Make it so.
this.key = key;
}
// This binds a delegate to this action
public void Bind(ActionDelegate method)
{
delegates.Add(method);
}
// This removes a delegate from this action
public void Unbind(ActionDelegate method)
{
delegates.Remove(method);
}
// This raises events for this action
public void Invoke()
{
// Check if this action exists
foreach(ActionDelegate ad in delegates) ad.Invoke();
}
#endregion
}
}

View file

@ -0,0 +1,34 @@
#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;
#endregion
namespace CodeImp.DoomBuilder.Controls
{
public enum SpecialKeys : int
{
MScrollUp = 65530,
MScrollDown = 65531,
}
}

View file

@ -29,6 +29,7 @@ using CodeImp.DoomBuilder.IO;
using CodeImp.DoomBuilder.Map;
using CodeImp.DoomBuilder.Geometry;
using System.Runtime.InteropServices;
using CodeImp.DoomBuilder.Controls;
#endregion
@ -63,6 +64,7 @@ namespace CodeImp.DoomBuilder
private static MainForm mainwindow;
private static Configuration settings;
private static MapManager map;
private static ActionManager actions;
// Configurations
private static List<ConfigurationInfo> configs;
@ -79,6 +81,7 @@ namespace CodeImp.DoomBuilder
public static Configuration Settings { get { return settings; } }
public static List<ConfigurationInfo> Configs { get { return configs; } }
public static MapManager Map { get { return map; } }
public static ActionManager Actions { get { return actions; } }
#endregion
@ -192,6 +195,9 @@ namespace CodeImp.DoomBuilder
if(!File.Exists(Path.Combine(apppath, SETTINGS_CONFIG_FILE))) throw (new FileNotFoundException("Unable to find the program configuration \"" + SETTINGS_CONFIG_FILE + "\"."));
settings = new Configuration(Path.Combine(apppath, SETTINGS_CONFIG_FILE), false);
// Create action manager
actions = new ActionManager();
// Create main window
mainwindow = new MainForm();
mainwindow.UpdateMenus();
@ -217,6 +223,7 @@ namespace CodeImp.DoomBuilder
{
// Clean up
mainwindow.Dispose();
actions.Dispose();
// Save settings configuration
settings.SaveConfiguration(Path.Combine(apppath, SETTINGS_CONFIG_FILE));

View file

@ -83,7 +83,9 @@ namespace CodeImp.DoomBuilder.Interface
// itemnewmap
//
this.itemnewmap.Name = "itemnewmap";
this.itemnewmap.ShortcutKeyDisplayString = "";
this.itemnewmap.Size = new System.Drawing.Size(167, 22);
this.itemnewmap.Tag = "newmap";
this.itemnewmap.Text = "New Map";
this.itemnewmap.Click += new System.EventHandler(this.itemnewmap_Click);
//
@ -91,6 +93,7 @@ namespace CodeImp.DoomBuilder.Interface
//
this.itemopenmap.Name = "itemopenmap";
this.itemopenmap.Size = new System.Drawing.Size(167, 22);
this.itemopenmap.Tag = "openmap";
this.itemopenmap.Text = "Open Map...";
this.itemopenmap.Click += new System.EventHandler(this.itemopenmap_Click);
//

View file

@ -22,6 +22,7 @@ using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodeImp.DoomBuilder.Controls;
#endregion
@ -61,7 +62,10 @@ namespace CodeImp.DoomBuilder.Interface
{
// Setup controls
InitializeComponent();
// Apply shortcut keys
ApplyShortcutKeys();
// Keep last position and size
lastposition = this.Location;
lastsize = this.Size;
@ -274,6 +278,49 @@ namespace CodeImp.DoomBuilder.Interface
#region ================== Menus
// Public method to apply shortcut keys
public void ApplyShortcutKeys()
{
// Apply shortcut keys to menus
ApplyShortcutKeys(menumain.Items);
}
// This sets the shortcut keys on menu items
private void ApplyShortcutKeys(ToolStripItemCollection items)
{
ToolStripMenuItem menuitem;
string actionname;
// Go for all controls to find menu items
foreach(ToolStripItem item in items)
{
// This is a menu item?
if(item is ToolStripMenuItem)
{
// Get the item in proper type
menuitem = (item as ToolStripMenuItem);
// Tag set for this item?
if(menuitem.Tag != null)
{
// Get the action name
actionname = menuitem.Tag.ToString();
// Action with this name available?
if(General.Actions.Exists(actionname))
{
// Put the action shortcut key on the menu item
// TODO: Use a friendly shortcut key description
menuitem.ShortcutKeyDisplayString = General.Actions[actionname].ShortcutKey.ToString();
}
}
// Recursively apply shortcut keys to child menu items as well
ApplyShortcutKeys(menuitem.DropDownItems);
}
}
}
// This updates all menus for the current status
public void UpdateMenus()
{

View file

@ -0,0 +1,19 @@
/******************************************\
Doom Builder Actions Configuration
\******************************************/
// This just defines which actions there are and what description they have
// The source code will bind to these actions with delegates (function pointers)
newmap
{
title = "File: New Map";
description = "Starts with a new, empty workspace to begin drawing a map from scratch.";
}
openmap
{
title = "File: Open Map";
description = "Opens an existing map from WAD file for viewing or modifying.";
}