JD2022-TU1/main/tools/framework/JD.Sequencers/Tools/Tool.cs

248 lines
5.8 KiB
C#

using System;
using System.Windows.Forms;
using JD.Kernel.StaticHelpers;
using JD.Sequencers.Controls;
using JD.Sequencers.Windowlesses;
namespace JD.Sequencers.Tools
{
/// <summary>
/// A tool is an object that allows the user to interact with a control.
/// </summary>
public abstract class Tool
{
#region Variables
private readonly WindowlessContainer m_container;
private WindowlessControl m_currentControl;
private MouseEventArgs m_lastMouseDownEventArgs;
private bool _isStarted = false;
#endregion
#region Constructor
/// <summary>
/// Constructor.
/// </summary>
/// <param name="owner"></param>
protected Tool(WindowlessContainer owner)
{
Throw.IfNull(owner, "owner");
m_container = owner;
}
#endregion
#region Properties
/// <summary>
/// Container owning this tool.
/// </summary>
public WindowlessContainer Container
{
get
{
return m_container;
}
}
/// <summary>
/// Selection.
/// </summary>
public Selection Selection
{
get
{
return m_container.Selection;
}
}
/// <summary>
/// Control on which this tool is currently affecting.
/// </summary>
public WindowlessControl CurrentControl
{
get { return m_currentControl; }
protected set
{
if (m_currentControl != null)
{
m_currentControl.IsCurrentControl = false;
}
m_currentControl = value;
if (m_currentControl != null)
{
m_currentControl.IsCurrentControl = true;
}
}
}
/// <summary>
/// Last Mouse Down EventArgs
/// </summary>
protected MouseEventArgs LastMouseDownEventArgs
{
get
{
return m_lastMouseDownEventArgs;
}
}
/// <summary>
/// IsStarted
/// </summary>
protected bool IsStarted
{
get { return _isStarted; }
set { _isStarted = value; }
}
#region Key States
/// <summary>
/// Returns true if Shift is currently pressed.
/// </summary>
protected bool IsShiftPressed
{
get { return (Container.RootControl.ModifierKeys & Keys.Shift) == Keys.Shift; }
}
/// <summary>
/// Returns true if Control is currently pressed.
/// </summary>
protected bool IsControlPressed
{
get { return (Container.RootControl.ModifierKeys & Keys.Control) == Keys.Control; }
}
/// <summary>
/// Returns true if Alt is currently pressed.
/// </summary>
protected bool IsAltPressed
{
get { return (Container.RootControl.ModifierKeys & Keys.Alt) == Keys.Alt; }
}
#endregion
#endregion
#region Methods
/// <summary>
/// Called when the tool is being set as the current tool for a container.
/// </summary>
protected internal virtual void Start()
{
IsStarted = true;
}
/// <summary>
/// Called when the tool is being unset as the current tool for a container.
/// </summary>
protected internal virtual void End()
{
IsStarted = false;
m_lastMouseDownEventArgs = null;
}
/// <summary>
/// Ends the current tool.
/// </summary>
public void EndTool()
{
if (Container.ToolManager.CurrentTool == this)
{
Container.ToolManager.CurrentTool = null;
}
}
/// <summary>
/// Whether this tool should start.
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
protected internal virtual bool CanStart(ToolStartingEventArgs e)
{
OnStarting(e);
return !e.Cancel;
}
#region Key events
/// <summary>
/// OnKeyDown.
/// </summary>
/// <param name="e"></param>
protected internal virtual void OnKeyDown(KeyEventArgs e)
{
}
/// <summary>
/// OnKeyUp.
/// </summary>
/// <param name="e"></param>
protected internal virtual void OnKeyUp(KeyEventArgs e)
{
}
#endregion
#region Mouse events
/// <summary>
/// OnMouseDown.
/// </summary>
/// <param name="e"></param>
protected internal virtual void OnMouseDown(MouseEventArgs e)
{
m_lastMouseDownEventArgs = e;
}
/// <summary>
/// OnMouseUp.
/// </summary>
/// <param name="e"></param>
protected internal virtual void OnMouseUp(MouseEventArgs e)
{
// By default, stop the tool
EndTool();
}
/// <summary>
/// OnMouseWheel.
/// </summary>
/// <param name="e"></param>
protected internal virtual void OnMouseWheel(MouseEventArgs e)
{
}
/// <summary>
/// OnMouseMove.
/// </summary>
/// <param name="e"></param>
protected internal virtual void OnMouseMove(MouseEventArgs e)
{
}
#endregion
#endregion
#region Events
public event EventHandler<ToolStartingEventArgs> Starting;
protected virtual bool OnStarting(ToolStartingEventArgs e)
{
Starting?.Invoke(this, e);
return !e.Cancel;
}
#endregion
}
}