namespace Yw.WpfUI.Hydro
|
{
|
/// <summary>
|
/// 选择管理器
|
/// </summary>
|
internal class SimpleSelectionManager
|
{
|
/// <summary>
|
///
|
/// </summary>
|
public SimpleSelectionManager(HelixViewport3D viewport)
|
{
|
_viewport = viewport;
|
}
|
|
/// <summary>
|
/// 选择改变事件
|
/// </summary>
|
public event Action<List<Visual3D>> SelectionChangedEvent;
|
/// <summary>
|
/// 状态改变事件
|
/// </summary>
|
public event Action<Visual3D, eSelectionType> StateChangedEvent;
|
|
private readonly HelixViewport3D _viewport;//控件
|
private readonly List<Visual3D> _selection = new();//选择集合
|
|
#region 内部实现
|
|
//添加选择
|
private void AddToSelection(Visual3D visual)
|
{
|
if (_selection.Contains(visual))
|
{
|
return;
|
}
|
_selection.Add(visual);
|
this.StateChangedEvent?.Invoke(visual, eSelectionType.Load);
|
}
|
|
//从选择中移除
|
private void RemoveFromSelection(Visual3D visual)
|
{
|
if (!_selection.Contains(visual))
|
{
|
return;
|
}
|
_selection.Remove(visual);
|
this.StateChangedEvent?.Invoke(visual, eSelectionType.Unload);
|
}
|
|
//清理选择
|
private void ClearSelection()
|
{
|
if (_selection.Count < 1)
|
{
|
return;
|
}
|
_selection.ForEach(x => this.StateChangedEvent?.Invoke(x, eSelectionType.Unload));
|
_selection.Clear();
|
}
|
|
#endregion
|
|
/// <summary>
|
/// 处理单个选择
|
/// </summary>
|
public void HandleSingle(Point pt)
|
{
|
var visual = _viewport.FindNearestVisual(pt);
|
if (visual == null)
|
{
|
if (_selection.Count > 0)
|
{
|
ClearSelection();
|
this.SelectionChangedEvent?.Invoke(null);
|
}
|
return;
|
}
|
if (_selection.Count == 1 && _selection[0] == visual)
|
{
|
return;
|
}
|
ClearSelection();
|
AddToSelection(visual);
|
this.SelectionChangedEvent?.Invoke(_selection);
|
}
|
|
/// <summary>
|
/// 处理多个选择
|
/// </summary>
|
public void HandleMulti(Point pt)
|
{
|
var visual = _viewport.FindNearestVisual(pt);
|
if (visual == null)
|
{
|
return;
|
}
|
if (_selection.Contains(visual))
|
{
|
return;
|
}
|
AddToSelection(visual);
|
this.SelectionChangedEvent?.Invoke(_selection);
|
}
|
|
/// <summary>
|
/// 选择Visual
|
/// 不触发选择改变事件
|
/// </summary>
|
public void SelectVisual(Visual3D visual)
|
{
|
ClearSelection();
|
if (visual == null)
|
{
|
return;
|
}
|
AddToSelection(visual);
|
}
|
|
/// <summary>
|
/// 选择Visuals
|
/// </summary>
|
public void SelectVisuals(List<Visual3D> visuals)
|
{
|
ClearSelection();
|
if (visuals == null || visuals.Count < 1)
|
{
|
return;
|
}
|
visuals.ForEach(AddToSelection);
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
}
|