UltimateZoneBuilder/Source/Editing/EditingManager.cs

405 lines
10 KiB
C#
Raw Normal View History

#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;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using CodeImp.DoomBuilder.Windows;
using CodeImp.DoomBuilder.IO;
using CodeImp.DoomBuilder.Map;
using CodeImp.DoomBuilder.Rendering;
using System.Diagnostics;
using CodeImp.DoomBuilder.Actions;
using CodeImp.DoomBuilder.Geometry;
using CodeImp.DoomBuilder.Plugins;
using CodeImp.DoomBuilder.VisualModes;
#endregion
namespace CodeImp.DoomBuilder.Editing
{
public sealed class EditingManager
{
#region ================== Constants
#endregion
#region ================== Variables
// All editing mode groups, sorted alphabetically
private List<string> groups;
// All editing modes available
private List<EditModeInfo> allmodes;
// Editing modes selected through configuration
private List<EditModeInfo> usedmodes;
// Status
private EditMode mode;
private EditMode newmode;
private Type prevmode;
private Type prevstablemode;
private Type prevclassicmode;
// Disposing
private bool isdisposed = false;
#endregion
#region ================== Properties
internal List<EditModeInfo> ModesInfo { get { return allmodes; } }
public EditMode Mode { get { return mode; } }
public EditMode NewMode { get { return newmode; } }
public Type PreviousMode { get { return prevmode; } }
public Type PreviousStableMode { get { return prevstablemode; } }
public Type PreviousClassicMode { get { return prevclassicmode; } }
public bool IsDisposed { get { return isdisposed; } }
#endregion
#region ================== Constructor / Disposer
// Constructor
internal EditingManager()
{
// Initialize
allmodes = new List<EditModeInfo>();
usedmodes = new List<EditModeInfo>();
groups = new List<string>();
2009-01-03 22:18:59 +00:00
// Bind any methods
General.Actions.BindMethods(this);
// Make list of all editing modes we can find
foreach(Plugin p in General.Plugins.Plugins)
{
// For all classes that inherit from EditMode
Type[] editclasses = p.FindClasses(typeof(EditMode));
foreach(Type t in editclasses)
{
// For all defined EditMode attributes
EditModeAttribute[] emattrs = (EditModeAttribute[])t.GetCustomAttributes(typeof(EditModeAttribute), false);
foreach(EditModeAttribute a in emattrs)
{
// Make edit mode information
EditModeInfo modeinfo = new EditModeInfo(p, t, a);
allmodes.Add(modeinfo);
// Add group if not added yet
if(!groups.Contains(modeinfo.Attributes.ButtonGroup))
groups.Add(modeinfo.Attributes.ButtonGroup);
}
}
}
// Sort the lists
allmodes.Sort();
groups.Sort();
// Update modes
UpdateCurrentEditModes();
// We have no destructor
GC.SuppressFinalize(this);
}
// Disposer
internal void Dispose()
{
// Not already disposed?
if(!isdisposed)
{
2009-01-03 22:18:59 +00:00
// Unbind any methods
General.Actions.UnbindMethods(this);
// Clean up
// Done
isdisposed = true;
}
}
#endregion
#region ================== Switch Actions
// This unbinds all editing mode switch actions
private void UnbindSwitchActions()
{
foreach(EditModeInfo emi in allmodes)
{
emi.UnbindSwitchAction();
}
}
// This binds all editing mode switch actions for the available modes only
private void BindAvailableSwitchActions()
{
// In case of VisualMode, we only bind the switch action
// of the VisualMode to switch back to the previous mode
if(mode is VisualMode)
{
// Bind only the switch action for this mode
EditModeInfo info = GetEditModeInfo(mode.GetType());
info.BindSwitchAction();
}
else
{
// Bind all available mode swtich actions
foreach(EditModeInfo emi in usedmodes)
{
emi.BindSwitchAction();
}
}
}
#endregion
#region ================== Methods
// This returns specific editing mode info by name
internal EditModeInfo GetEditModeInfo(string editmodename)
{
// Find the edit mode
foreach(EditModeInfo emi in usedmodes)
{
// Mode matches class name?
if(emi.ToString() == editmodename) return emi;
}
// No such mode found
return null;
}
// This returns specific editing mode info by name
internal EditModeInfo GetEditModeInfo(Type modetype)
{
// Find the edit mode
foreach(EditModeInfo emi in usedmodes)
{
// Mode matches class name?
if(emi.Type == modetype) return emi;
}
// No such mode found
return null;
}
// This is called when the editing modes must update
internal void UpdateCurrentEditModes()
{
2008-11-27 13:42:18 +00:00
// Unbind editing mode switch actions
UnbindSwitchActions();
2008-11-27 13:42:18 +00:00
// Rebuild list of used modes
usedmodes.Clear();
2008-11-27 13:42:18 +00:00
if(General.Map != null)
{
foreach(EditModeInfo emi in allmodes)
{
2008-11-27 19:25:13 +00:00
// Include the mode when it is listed and enabled
// Also include the mode when it is not optional
if( (General.Map.ConfigSettings.EditModes.ContainsKey(emi.Type.FullName) &&
General.Map.ConfigSettings.EditModes[emi.Type.FullName]) || !emi.IsOptional )
2008-11-27 13:42:18 +00:00
{
// Add the mode to be used and bind switch action
usedmodes.Add(emi);
}
}
}
// Bind switch action for used modes
BindAvailableSwitchActions();
// Remove editing mode buttons from interface
General.MainWindow.RemoveEditModeButtons();
// Go for all the editing mode groups
foreach(string grp in groups)
{
General.MainWindow.AddEditModeSeperator();
// Go for all used edit modes to add buttons
foreach(EditModeInfo emi in usedmodes)
{
if((emi.ButtonImage != null) && (emi.ButtonDesc != null) &&
(emi.Attributes.ButtonGroup == grp))
General.MainWindow.AddEditModeButton(emi);
}
}
}
//
// This changes the editing mode.
// Order in which events occur for the old and new modes:
//
// - Constructor of new mode is called
// - Disengage of old mode is called
// ----- Mode switches -----
// - Engage of new mode is called
// - Dispose of old mode is called
//
// Returns false when cancelled
public bool ChangeMode(EditMode nextmode)
{
EditMode oldmode = mode;
2008-11-27 19:25:13 +00:00
if(nextmode != null)
{
// Verify that this mode is usable
bool allowuse = false;
foreach(EditModeInfo emi in usedmodes)
{
if(emi.Type.FullName == nextmode.GetType().FullName)
{
allowuse = true;
break;
}
}
if(!allowuse)
{
General.Interface.MessageBeep(MessageBeepType.Error);
General.WriteLogLine("Attempt to switch to an invalid edit mode " + nextmode.GetType().Name + "!");
return false;
}
else
{
General.WriteLogLine("Preparing to change editing mode to " + nextmode.GetType().Name + "...");
2008-11-27 19:25:13 +00:00
}
}
else
2008-11-27 19:25:13 +00:00
{
General.WriteLogLine("Stopping editing mode...");
2008-11-27 19:25:13 +00:00
}
// Remember previous mode
newmode = nextmode;
if(mode != null)
{
prevmode = mode.GetType();
if(!mode.Attributes.Volatile)
{
prevstablemode = prevmode;
if(mode is ClassicMode) prevclassicmode = prevmode;
}
}
else
{
prevmode = null;
prevstablemode = null;
prevclassicmode = null;
}
// Let the plugins know beforehand and check if not cancelled
if(General.Plugins.ModeChanges(oldmode, newmode))
{
// Disenagage old mode
if(oldmode != null) oldmode.OnDisengage();
// Reset cursor
General.Interface.SetCursor(Cursors.Default);
// Apply new mode
General.WriteLogLine("Editing mode changes from " + TypeNameOrNull(oldmode) + " to " + TypeNameOrNull(nextmode));
General.WriteLogLine("Previous stable mode is " + TypeNameOrNull(prevstablemode) + ", previous classic mode is " + TypeNameOrNull(prevclassicmode));
mode = newmode;
// Engage new mode
if(newmode != null) newmode.OnEngage();
// Bind new switch actions
UnbindSwitchActions();
BindAvailableSwitchActions();
// Update the interface
General.MainWindow.EditModeChanged();
// Dispose old mode
if(oldmode != null) oldmode.Dispose();
// Done switching
General.WriteLogLine("Editing mode change complete.");
newmode = null;
// Redraw the display
General.MainWindow.RedrawDisplay();
return true;
}
else
{
// Cancelled
General.WriteLogLine("Editing mode change cancelled.");
return false;
}
}
// This changes mode by class name and optionally with arguments
public void ChangeMode(string classname, params object[] args)
{
EditModeInfo emi = GetEditModeInfo(classname);
if(emi != null) emi.SwitchToMode(args);
}
// This returns the type name as string
private string TypeNameOrNull(Type type)
{
return (type != null) ? type.Name : "NULL";
}
// This returns the type name as string
private string TypeNameOrNull(object obj)
{
return (obj != null) ? obj.GetType().Name : "NULL";
}
#endregion
#region ================== Actions
/// <summary>
/// This cancels the current mode.
/// </summary>
[BeginAction("cancelmode")]
public void CancelMode()
{
// Let the mode know
mode.OnCancel();
}
/// <summary>
/// This accepts the changes in the current mode.
/// </summary>
[BeginAction("acceptmode")]
public void AcceptMode()
{
// Let the mode know
mode.OnAccept();
}
#endregion
}
}