From 3066b1bf68d852ba16ec0294d27cc3c283f1e243 Mon Sep 17 00:00:00 2001 From: codeimp Date: Tue, 26 Jun 2007 06:01:52 +0000 Subject: [PATCH] actions can now be defined with ActionAttribute --- Build/Builder.cfg | 10 ++- Source/Builder.csproj | 1 + Source/Controls/Action.cs | 6 ++ Source/Controls/ActionAttribute.cs | 135 +++++++++++++++++++++++++++++ Source/Editing/EditMode.cs | 7 ++ Source/Editing/ViewClassicMode.cs | 67 +++++++++++++- Source/General/General.cs | 15 ++-- Source/Resources/Actions.cfg | 54 ++++++++++++ 8 files changed, 284 insertions(+), 11 deletions(-) create mode 100644 Source/Controls/ActionAttribute.cs diff --git a/Build/Builder.cfg b/Build/Builder.cfg index f1cc6f01..cf32d029 100644 --- a/Build/Builder.cfg +++ b/Build/Builder.cfg @@ -1,7 +1,13 @@ shortcuts { + scrolleast = 39; + scrollsouth = 40; + zoomout = 65531; + scrollnorth = 38; + scrollwest = 37; newmap = 131150; + zoomin = 65530; openmap = 131151; } @@ -9,9 +15,9 @@ shortcuts mainwindow { positionx = 124; - sizeheight = 572; - positiony = 35; windowstate = 2; + positiony = 35; + sizeheight = 572; sizewidth = 739; } diff --git a/Source/Builder.csproj b/Source/Builder.csproj index 7de45d5f..019ea5da 100644 --- a/Source/Builder.csproj +++ b/Source/Builder.csproj @@ -44,6 +44,7 @@ --> + diff --git a/Source/Controls/Action.cs b/Source/Controls/Action.cs index ac00420a..1f1dfc98 100644 --- a/Source/Controls/Action.cs +++ b/Source/Controls/Action.cs @@ -37,6 +37,12 @@ namespace CodeImp.DoomBuilder.Controls public const string SAVEMAP = "savemap"; public const string SAVEMAPAS = "savemapas"; public const string SHOWOVERVIEW = "showoverview"; + public const string SCROLLNORTH = "scrollnorth"; + public const string SCROLLSOUTH = "scrollsouth"; + public const string SCROLLWEST = "scrollwest"; + public const string SCROLLEAST = "scrolleast"; + public const string ZOOMIN = "zoomin"; + public const string ZOOMOUT = "zoomout"; #endregion diff --git a/Source/Controls/ActionAttribute.cs b/Source/Controls/ActionAttribute.cs new file mode 100644 index 00000000..89a30350 --- /dev/null +++ b/Source/Controls/ActionAttribute.cs @@ -0,0 +1,135 @@ + +#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 System.Reflection; + +#endregion + +namespace CodeImp.DoomBuilder.Controls +{ + [AttributeUsage(AttributeTargets.Method, Inherited=true, AllowMultiple=true)] + internal class ActionAttribute : Attribute + { + #region ================== Variables + + // The action to bind to + private string action; + + #endregion + + #region ================== Constructor / Disposer + + // Constructor + public ActionAttribute(string action) + { + // Initialize + this.action = action; + } + + #endregion + + #region ================== Static Methods + + // This binds all methods marked with this attribute + public static void BindMethods(Type type) + { + // Bind static methods + BindMethods(null, type); + } + + // This binds all methods marked with this attribute + public static void BindMethods(object obj) + { + // Bind instance methods + BindMethods(obj, obj.GetType()); + } + + // This binds all methods marked with this attribute + private static void BindMethods(object obj, Type type) + { + MethodInfo[] methods; + ActionAttribute[] attrs; + ActionDelegate del; + + // Go for all methods on obj + methods = type.GetMethods(); + foreach(MethodInfo m in methods) + { + // Check if the method has this attribute + attrs = (ActionAttribute[])m.GetCustomAttributes(typeof(ActionAttribute), true); + + // Go for all attributes + foreach(ActionAttribute a in attrs) + { + // Create a delegate for this method + del = (ActionDelegate)Delegate.CreateDelegate(typeof(ActionDelegate), obj, m); + + // Bind method to action + General.Actions[a.action].Bind(del); + } + } + } + + // This unbinds all methods marked with this attribute + public static void UnbindMethods(Type type) + { + // Unbind static methods + UnbindMethods(null, type); + } + + // This unbinds all methods marked with this attribute + public static void UnbindMethods(object obj) + { + // Unbind instance methods + UnbindMethods(obj, obj.GetType()); + } + + // This unbinds all methods marked with this attribute + private static void UnbindMethods(object obj, Type type) + { + MethodInfo[] methods; + ActionAttribute[] attrs; + ActionDelegate del; + + // Go for all methods on obj + methods = type.GetMethods(); + foreach(MethodInfo m in methods) + { + // Check if the method has this attribute + attrs = (ActionAttribute[])m.GetCustomAttributes(typeof(ActionAttribute), true); + + // Go for all attributes + foreach(ActionAttribute a in attrs) + { + // Create a delegate for this method + del = (ActionDelegate)Delegate.CreateDelegate(typeof(ActionDelegate), obj, m); + + // Unbind method from action + General.Actions[a.action].Unbind(del); + } + } + } + + #endregion + } +} diff --git a/Source/Editing/EditMode.cs b/Source/Editing/EditMode.cs index ebd94fcb..5dab960b 100644 --- a/Source/Editing/EditMode.cs +++ b/Source/Editing/EditMode.cs @@ -29,6 +29,7 @@ using CodeImp.DoomBuilder.IO; using CodeImp.DoomBuilder.Map; using CodeImp.DoomBuilder.Rendering; using System.Diagnostics; +using CodeImp.DoomBuilder.Controls; #endregion @@ -65,6 +66,9 @@ namespace CodeImp.DoomBuilder.Editing // Initialize this.graphics = General.Map.Graphics; + // Bind any methods + ActionAttribute.BindMethods(this); + // We have no destructor GC.SuppressFinalize(this); } @@ -75,6 +79,9 @@ namespace CodeImp.DoomBuilder.Editing // Not already disposed? if(!isdisposed) { + // Unbind any methods + ActionAttribute.UnbindMethods(this); + // Clean up // Done diff --git a/Source/Editing/ViewClassicMode.cs b/Source/Editing/ViewClassicMode.cs index a71e79fa..ff3ce4cc 100644 --- a/Source/Editing/ViewClassicMode.cs +++ b/Source/Editing/ViewClassicMode.cs @@ -28,6 +28,7 @@ using CodeImp.DoomBuilder.Interface; using CodeImp.DoomBuilder.IO; using CodeImp.DoomBuilder.Map; using CodeImp.DoomBuilder.Rendering; +using CodeImp.DoomBuilder.Controls; #endregion @@ -74,8 +75,72 @@ namespace CodeImp.DoomBuilder.Editing #endregion - #region ================== Methods + #region ================== Scroll / Zoom + // This scrolls the view north + [Action(Action.SCROLLNORTH)] + public void ScrollNorth() + { + // Scroll + ScrollBy(0f, 100f / renderer.Scale); + } + + // This scrolls the view south + [Action(Action.SCROLLSOUTH)] + public void ScrollSouth() + { + // Scroll + ScrollBy(0f, -100f / renderer.Scale); + } + + // This scrolls the view west + [Action(Action.SCROLLWEST)] + public void ScrollWest() + { + // Scroll + ScrollBy(-100f / renderer.Scale, 0f); + } + + // This scrolls the view east + [Action(Action.SCROLLEAST)] + public void ScrollEast() + { + // Scroll + ScrollBy(100f / renderer.Scale, 0f); + } + + // This zooms in + [Action(Action.ZOOMIN)] + public void ZoomIn() + { + // Zoom + ZoomBy(0.1f); + } + + // This zooms out + [Action(Action.ZOOMOUT)] + public void ZoomOut() + { + // Zoom + ZoomBy(-0.1f); + } + + // This scrolls anywhere + private void ScrollBy(float deltax, float deltay) + { + // Scroll now + renderer.PositionView(renderer.OffsetX + deltax, renderer.OffsetY + deltay); + RedrawDisplay(); + } + + // This zooms + private void ZoomBy(float deltaz) + { + // Zoom now + renderer.ScaleView(renderer.Scale + deltaz); + RedrawDisplay(); + } + #endregion } } diff --git a/Source/General/General.cs b/Source/General/General.cs index 72981af9..bce840ee 100644 --- a/Source/General/General.cs +++ b/Source/General/General.cs @@ -198,10 +198,8 @@ namespace CodeImp.DoomBuilder // Create action manager actions = new ActionManager(); - // Bind my methods to actions - actions[Action.NEWMAP].Bind(new ActionDelegate(NewMap)); - actions[Action.OPENMAP].Bind(new ActionDelegate(OpenMap)); - actions[Action.CLOSEMAP].Bind(new ActionDelegate(CloseMap)); + // Bind static methods to actions + ActionAttribute.BindMethods(typeof(General)); // Create main window mainwindow = new MainForm(); @@ -226,10 +224,8 @@ namespace CodeImp.DoomBuilder // This terminates the program public static void Terminate() { - // Unbind my methods to actions - actions[Action.NEWMAP].Unbind(new ActionDelegate(NewMap)); - actions[Action.OPENMAP].Unbind(new ActionDelegate(OpenMap)); - actions[Action.CLOSEMAP].Unbind(new ActionDelegate(CloseMap)); + // Unbind static methods from actions + ActionAttribute.UnbindMethods(typeof(General)); // Clean up mainwindow.Dispose(); @@ -247,6 +243,7 @@ namespace CodeImp.DoomBuilder #region ================== Management // This creates a new map + [Action(Action.NEWMAP)] public static void NewMap() { MapOptions newoptions = new MapOptions(); @@ -294,6 +291,7 @@ namespace CodeImp.DoomBuilder } // This closes the current map + [Action(Action.CLOSEMAP)] public static void CloseMap() { // Ask the user to save changes (if any) @@ -316,6 +314,7 @@ namespace CodeImp.DoomBuilder } // This loads a map from file + [Action(Action.OPENMAP)] public static void OpenMap() { OpenFileDialog openfile; diff --git a/Source/Resources/Actions.cfg b/Source/Resources/Actions.cfg index e06c8cc7..1d8d7ea1 100644 --- a/Source/Resources/Actions.cfg +++ b/Source/Resources/Actions.cfg @@ -31,3 +31,57 @@ closemap allowmouse = false; allowscroll = false; } + +scrollwest +{ + title = "2D: Scroll West"; + description = "Scrolls the 2D map view to the left."; + allowkeys = true; + allowmouse = true; + allowscroll = true; +} + +scrolleast +{ + title = "2D: Scroll East"; + description = "Scrolls the 2D map view to the right."; + allowkeys = true; + allowmouse = true; + allowscroll = true; +} + +scrollnorth +{ + title = "2D: Scroll North"; + description = "Scrolls the 2D map view to the north."; + allowkeys = true; + allowmouse = true; + allowscroll = true; +} + +scrollsouth +{ + title = "2D: Scroll South"; + description = "Scrolls the 2D map view to the south."; + allowkeys = true; + allowmouse = true; + allowscroll = true; +} + +zoomin +{ + title = "2D: Zoom In"; + description = "Zooms in on the map at the current mouse location."; + allowkeys = true; + allowmouse = true; + allowscroll = true; +} + +zoomout +{ + title = "2D: Zoom Out"; + description = "Zooms out on the map from the current mouse location."; + allowkeys = true; + allowmouse = true; + allowscroll = true; +}