namespace Yw.WpfUI.Hydro
|
{
|
/// <summary>
|
/// 绘制选择辅助类
|
/// </summary>
|
internal class DrawSelectionHelper : IDisposable
|
{
|
/// <summary>
|
///
|
/// </summary>
|
public DrawSelectionHelper
|
(
|
HelixViewport3D viewport,
|
DrawInitialHelper initialHelper
|
)
|
{
|
_viewport = viewport;
|
_initialHelper = initialHelper;
|
Attach();
|
}
|
|
|
|
/// <summary>
|
/// 选择改变事件
|
/// </summary>
|
public event Action<List<VisualDraw3D>> SelectionChangedEvent;
|
|
|
private readonly HelixViewport3D _viewport = null;//三维组件
|
private readonly DrawInitialHelper _initialHelper = null;//初始化辅助类
|
private VisualStateObject _stateObject = null;//状态对象
|
private List<VisualDraw3D> _selection = new();//选择集合
|
|
/// <summary>
|
/// 选择集合
|
/// </summary>
|
public List<VisualDraw3D> Selection
|
{
|
get { return _selection; }
|
private set { _selection = value; }
|
}
|
|
/// <summary>
|
/// 是否启用
|
/// </summary>
|
public bool Enabled
|
{
|
get { return _enabled; }
|
set { _enabled = value; }
|
}
|
private bool _enabled = true;
|
|
/// <summary>
|
/// 是否初始化
|
/// </summary>
|
public bool Initialized
|
{
|
get { return _initialized; }
|
private set { _initialized = value; }
|
}
|
private bool _initialized = false;
|
|
//初始化
|
private void Initialize()
|
{
|
if (!_initialHelper.Initialized)
|
{
|
return;
|
}
|
if (_stateObject == null)
|
{
|
_stateObject = new VisualStateObject();
|
_stateObject.HtmlColor = Yw.Settings.HydroL3dParasHelper.HydroL3d.Selection.HtmlColor;
|
_stateObject.Opacity = Yw.Settings.HydroL3dParasHelper.HydroL3d.Selection.Opacity;
|
_stateObject.Scale = Yw.Settings.HydroL3dParasHelper.HydroL3d.Selection.Scale;
|
_stateObject.Visible = Yw.Settings.HydroL3dParasHelper.HydroL3d.Selection.Visible;
|
}
|
_initialized = true;
|
}
|
|
//处理初始化完成
|
private void OnInitialCompleted()
|
{
|
Initialize();
|
}
|
|
//处理鼠标弹起
|
private void OnMouseUp(object sender, MouseButtonEventArgs e)
|
{
|
if (e.ChangedButton == MouseButton.Left)
|
{
|
var pt = e.GetPosition(_viewport);
|
Handle(pt);
|
}
|
}
|
|
|
#region 内部实现
|
|
//添加选择
|
private void AddToSelection(VisualDraw3D visual3d)
|
{
|
if (_selection.Contains(visual3d))
|
{
|
return;
|
}
|
_selection.Add(visual3d);
|
visual3d.LoadState(eVisualState.Selection, _stateObject);
|
visual3d.UpdateVisual3D();
|
}
|
|
//从选择中移除
|
private void RemoveFromSelection(VisualDraw3D visual3d)
|
{
|
if (!_selection.Contains(visual3d))
|
{
|
return;
|
}
|
_selection.Remove(visual3d);
|
visual3d.UnloadState(eVisualState.Selection);
|
visual3d.UpdateVisual3D();
|
}
|
|
//清理选择
|
private void ClearSelection()
|
{
|
if (_selection.Count < 1)
|
{
|
return;
|
}
|
_selection.ForEach(x =>
|
{
|
x.UnloadState(eVisualState.Selection);
|
x.UpdateVisual3D();
|
});
|
_selection.Clear();
|
}
|
|
#endregion
|
|
#region 选择处理
|
|
//处理
|
private void Handle(Point pt)
|
{
|
if (!Initialized)
|
{
|
return;
|
}
|
if (!Enabled)
|
{
|
return;
|
}
|
var visual3d = _viewport.FindNearestVisual(pt) as VisualDraw3D;
|
if (visual3d == null)
|
{
|
if (_selection.Count > 0)
|
{
|
ClearSelection();
|
this.SelectionChangedEvent?.Invoke(null);
|
}
|
return;
|
}
|
|
if (_selection.Count == 1 && _selection[0] == visual3d)
|
{
|
return;
|
}
|
ClearSelection();
|
AddToSelection(visual3d);
|
this.SelectionChangedEvent?.Invoke(_selection);
|
}
|
|
//附加
|
private void Append(Point pt)
|
{
|
if (!Initialized)
|
{
|
return;
|
}
|
if (!Enabled)
|
{
|
return;
|
}
|
var visual3d = _viewport.FindNearestVisual(pt) as VisualDraw3D;
|
if (visual3d == null)
|
{
|
return;
|
}
|
if (_selection.Contains(visual3d))
|
{
|
return;
|
}
|
AddToSelection(visual3d);
|
this.SelectionChangedEvent?.Invoke(_selection);
|
}
|
|
#endregion
|
|
#region 外部选择
|
|
/// <summary>
|
/// 选择Visual
|
/// 不触发选择改变事件
|
/// </summary>
|
public void SelectVisual(VisualDraw3D visual)
|
{
|
ClearSelection();
|
if (visual == null)
|
{
|
return;
|
}
|
AddToSelection(visual);
|
}
|
|
/// <summary>
|
/// 选择Visuals
|
/// </summary>
|
public void SelectVisual(List<VisualDraw3D> visuals)
|
{
|
ClearSelection();
|
if (visuals == null || visuals.Count < 1)
|
{
|
return;
|
}
|
visuals.ForEach(AddToSelection);
|
}
|
|
#endregion
|
|
|
//附加
|
private void Attach()
|
{
|
_viewport.MouseUp += OnMouseUp;
|
_initialHelper.InitialCompletedEvent += OnInitialCompleted;
|
}
|
|
//分离
|
private void Detach()
|
{
|
_viewport.MouseUp -= OnMouseUp;
|
_initialHelper.InitialCompletedEvent -= OnInitialCompleted;
|
}
|
|
/// <summary>
|
/// 释放
|
/// </summary>
|
public void Dispose()
|
{
|
Detach();
|
}
|
|
}
|
}
|