205 lines
4.7 KiB
C#
205 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Windows.Forms;
|
|
using JD.Kernel.StaticHelpers;
|
|
using JD.Sequencers.Controls;
|
|
|
|
namespace JD.Sequencers.Tools
|
|
{
|
|
/// <summary>
|
|
/// Manages the tools for a <see cref="WindowlessContainer"/>
|
|
/// </summary>
|
|
public class ToolManager
|
|
{
|
|
#region Variables
|
|
|
|
private readonly WindowlessContainer m_container;
|
|
private Tool m_currentTool;
|
|
|
|
private readonly List<Tool> m_mouseDownTools = new List<Tool>();
|
|
private readonly List<Tool> m_mouseMoveTools = new List<Tool>();
|
|
private readonly List<Tool> m_mouseUpTools = new List<Tool>();
|
|
private readonly List<Tool> m_mouseWheelTools = new List<Tool>();
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
/// <summary>
|
|
/// Constructor.
|
|
/// </summary>
|
|
public ToolManager(WindowlessContainer container)
|
|
{
|
|
Throw.IfNull(container, "container");
|
|
m_container = container;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
public WindowlessContainer Container
|
|
{
|
|
get { return m_container; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// The current tool.
|
|
/// </summary>
|
|
public Tool CurrentTool
|
|
{
|
|
get { return m_currentTool; }
|
|
set
|
|
{
|
|
if (m_currentTool != value)
|
|
{
|
|
if (m_currentTool != null)
|
|
{
|
|
m_currentTool.End();
|
|
}
|
|
|
|
m_currentTool = value;
|
|
|
|
if (m_currentTool != null)
|
|
{
|
|
m_currentTool.Start();
|
|
}
|
|
|
|
OnCurrentToolChanged(EventArgs.Empty);
|
|
}
|
|
}
|
|
}
|
|
|
|
#region Mouse Tools
|
|
|
|
/// <summary>
|
|
/// Mouse Down Tools
|
|
/// </summary>
|
|
public IList<Tool> MouseDownTools
|
|
{
|
|
get
|
|
{
|
|
return m_mouseDownTools;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Mouse Move Tools
|
|
/// </summary>
|
|
public IList<Tool> MouseMoveTools
|
|
{
|
|
get
|
|
{
|
|
return m_mouseMoveTools;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Mouse Up Tools
|
|
/// </summary>
|
|
public IList<Tool> MouseUpTools
|
|
{
|
|
get
|
|
{
|
|
return m_mouseUpTools;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Mouse Wheel Tools
|
|
/// </summary>
|
|
public IList<Tool> MouseWheelTools
|
|
{
|
|
get
|
|
{
|
|
return m_mouseWheelTools;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
public void OnMouseDown(MouseEventArgs e)
|
|
{
|
|
StartToolIfPossible(MouseDownTools, e);
|
|
if (CurrentTool != null)
|
|
{
|
|
CurrentTool.OnMouseDown(e);
|
|
}
|
|
}
|
|
|
|
public void OnMouseUp(MouseEventArgs e)
|
|
{
|
|
StartToolIfPossible(MouseUpTools, e);
|
|
if (CurrentTool != null)
|
|
{
|
|
CurrentTool.OnMouseUp(e);
|
|
}
|
|
}
|
|
|
|
public void OnMouseMove(MouseEventArgs e)
|
|
{
|
|
if (CurrentTool != null)
|
|
{
|
|
CurrentTool.OnMouseMove(e);
|
|
}
|
|
else
|
|
{
|
|
StartToolIfPossible(MouseMoveTools, e);
|
|
}
|
|
}
|
|
|
|
public void OnMouseWheel(MouseEventArgs e)
|
|
{
|
|
StartToolIfPossible(MouseWheelTools, e);
|
|
if (CurrentTool != null)
|
|
{
|
|
CurrentTool.OnMouseWheel(e);
|
|
}
|
|
}
|
|
|
|
private void StartToolIfPossible(IEnumerable<Tool> tools, MouseEventArgs e)
|
|
{
|
|
foreach (Tool tool in tools)
|
|
{
|
|
var eventArgs = new ToolStartingEventArgs(e);
|
|
|
|
bool canStart = tool.CanStart(eventArgs);
|
|
|
|
if (eventArgs.CancelEntireEvent)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (canStart)
|
|
{
|
|
CurrentTool = tool;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Events
|
|
|
|
/// <summary>
|
|
/// Triggered when the <see cref="CurrentTool"/> changes.
|
|
/// </summary>
|
|
public event EventHandler CurrentToolChanged;
|
|
|
|
/// <summary>
|
|
/// Triggers the <see cref="CurrentToolChanged"/> event.
|
|
/// </summary>
|
|
/// <param name="e">Event Args.</param>
|
|
protected void OnCurrentToolChanged(EventArgs e)
|
|
{
|
|
CurrentToolChanged?.Invoke(Container, e);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|