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