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

149 lines
4 KiB
C#

using System;
using System.Drawing;
using System.Windows.Forms;
using JD.Kernel.Structs;
using JD.Sequencers.Controls;
using JD.Sequencers.Windowlesses;
namespace JD.Sequencers.Tools
{
/// <summary>
/// Basic tool that selects controls.
/// </summary>
public class SelectingTool : Tool
{
#region Variables
private bool m_allowMultiselection = true;
#endregion
#region Constructor
/// <summary>
/// Constructor.
/// </summary>
/// <param name="owner"></param>
public SelectingTool(WindowlessContainer owner)
: base(owner)
{
}
#endregion
#region Properties
/// <summary>
/// Whether to allow multi-selection.
/// </summary>
public bool AllowMultiselection
{
get
{
return m_allowMultiselection;
}
set
{
m_allowMultiselection = value;
}
}
#endregion
#region Methods
/// <summary>
/// Overriden.
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
protected internal override bool CanStart(ToolStartingEventArgs e)
{
return (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right) && base.CanStart(e);
}
private bool CanSelect(WindowlessControl control)
{
return Selection.Count == 0 || AllowMultiselection;
}
protected internal override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
DoSingleSelection(e.Button, e.Location, SelectionType.MouseDown);
}
protected internal override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
DoSingleSelection(e.Button, e.Location, SelectionType.MouseUp);
}
[Flags]
protected enum SelectionType
{
MouseDown = 1,
MouseUp = 2
}
protected void DoSingleSelection(MouseButtons button, Point location, Flags<SelectionType> selectionType)
{
CurrentControl = Container.FindSelectableControl<WindowlessControl>(location);
if (CurrentControl != null)
{
if (IsControlPressed && selectionType[SelectionType.MouseDown])
{
if (CurrentControl.Selected)
{
Selection.Remove(CurrentControl);
}
else if (CanSelect(CurrentControl))
{
AddToSelection(CurrentControl);
Selection.PerformMultiSelect();
}
}
else if (IsShiftPressed && CanSelect(CurrentControl) && selectionType[SelectionType.MouseDown])
{
AddToSelection(CurrentControl);
}
else if (!IsShiftPressed && !IsControlPressed)
{
if (!CurrentControl.Selected || (selectionType[SelectionType.MouseUp] && button == MouseButtons.Left))
{
Selection.Select(CurrentControl);
}
}
}
else if (!IsControlPressed && !IsShiftPressed && selectionType[SelectionType.MouseUp])
{
Selection.Clear();
}
}
private void AddToSelection(WindowlessControl control)
{
if (Selection.Count == 0 || Selection.Primary.CanSelectWith(control))
{
Selection.Add(control);
}
}
/// <summary>
/// Overriden.
/// </summary>
/// <param name="e"></param>
protected internal override void OnMouseMove(MouseEventArgs e)
{
if (e.Location != LastMouseDownEventArgs.Location)
{
EndTool();
}
}
#endregion
}
}