actions can now be defined with ActionAttribute

This commit is contained in:
codeimp 2007-06-26 06:01:52 +00:00
parent 1eedfae8bf
commit 3066b1bf68
8 changed files with 284 additions and 11 deletions

View file

@ -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;
}

View file

@ -44,6 +44,7 @@
</Target>
-->
<ItemGroup>
<Compile Include="Controls\ActionAttribute.cs" />
<Compile Include="Editing\EditMode.cs" />
<Compile Include="Editing\FrozenOverviewMode.cs" />
<Compile Include="Editing\ViewClassicMode.cs" />

View file

@ -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

View file

@ -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
}
}

View file

@ -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

View file

@ -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
}
}

View file

@ -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;

View file

@ -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;
}