namespace Yw.WpfUI.Hydro
{
///
/// 绘制小管网辅助类
///
internal class DrawSmallHelper
{
///
///
///
public DrawSmallHelper
(
HelixViewport3D viewport,
DrawInitialHelper initialHelper,
DrawHighlightHelper highlightHelper,
DrawSelectionHelper selectionHelper,
DrawMoudingHelper moudingHelper
)
{
_viewport = viewport;
_initialHelper = initialHelper;
_highlightHelper = highlightHelper;
_selectionHelper = selectionHelper;
_moudingHelper = moudingHelper;
Attach();
}
#region 事件集合
///
/// 小管网改变事件
///
public event Action> SmallChangedEvent;
#endregion
private readonly HelixViewport3D _viewport = null;//三维组件
private readonly DrawInitialHelper _initialHelper = null;//初始化辅助类
private readonly DrawHighlightHelper _highlightHelper = null;//高亮辅助类
private readonly DrawSelectionHelper _selectionHelper = null;//选择辅助类
private readonly DrawMoudingHelper _moudingHelper = null;//牟定辅助类
private bool _isWorking = false;//是否工作中
private bool? _lastHighlightEnabled = false;//上次高亮是否启用
private bool? _lastSelectionEnabled = false;//上次选择是否启用
private bool? _lastMoudingEnabled = false;//上次牟定是否启用
private SmallInputL3d _input;//输入参数
private SmallDraw3D _small3d;//小管网绘制件
///
/// 是否初始化
///
public bool Initialized
{
get { return _initialized; }
private set { _initialized = value; }
}
private bool _initialized = false;
//初始化
private void Initialize()
{
if (!_initialHelper.Initialized)
{
return;
}
_initialized = true;
}
//处理初始化完成
private void OnInitialCompleted()
{
Initialize();
}
///
/// 开始
///
public bool Start(SmallInputL3d input)
{
if (!Initialized)
{
return false;
}
if (!input.Verify())
{
return false;
}
_initialHelper.RegistDrawStatus(eDrawStatusL3d.Small, End);
_input = input;
_lastHighlightEnabled = _highlightHelper.Enabled;
_lastSelectionEnabled = _selectionHelper.Enabled;
_lastMoudingEnabled = _moudingHelper.Enabled;
_highlightHelper.Enabled = false;
_selectionHelper.Enabled = false;
_moudingHelper.Enabled = false;
_isWorking = true;
return true;
}
///
/// 结束
///
public bool End()
{
if (!Initialized)
{
return false;
}
_isWorking = false;
_highlightHelper.Enabled = _lastHighlightEnabled ?? false;
_selectionHelper.Enabled = _lastSelectionEnabled ?? false;
_moudingHelper.Enabled = _lastMoudingEnabled ?? false;
if (_small3d != null)
{
_small3d.Close();
_viewport.Children.Remove(_small3d);
_small3d = null;
}
_initialHelper.ResetDrawStatus();
return true;
}
//处理鼠标按下
private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
if (!_isWorking)
{
return;
}
}
//处理鼠标移动
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (!_isWorking)
{
return;
}
var pt = e.GetPosition(_viewport);
pt = new Point(pt.X + 3, pt.Y - 3);
var sp = _viewport.Viewport.UnProject(pt);
if (!sp.HasValue)
{
return;
}
if (_small3d == null)
{
var input = _input.CloneC();
var drawMode = _initialHelper.GetDrawMode();
_small3d = new SmallDraw3D(input, drawMode);
_viewport.Children.Add(_small3d);
}
_small3d.UpdateTransform(sp.Value);
_moudingHelper.Mouding(pt, _small3d.Visual3ds);
}
//处理鼠标弹起
private void OnMouseUp(object sender, MouseButtonEventArgs e)
{
if (!_isWorking)
{
return;
}
if (e.ChangedButton != MouseButton.Left)
{
return;
}
if (_small3d == null)
{
return;
}
var pt = e.GetPosition(_viewport);
var snapNode = _moudingHelper.SnapNearestNode(pt, _small3d.Visual3ds);
if (snapNode != null)
{
var allVisual3ds = _small3d.Close(snapNode);
allVisual3ds.ForEach(x =>
{
_initialHelper.AddVisual3D(x);
});
_viewport.Children.Remove(_small3d);
_small3d = null;
OnSmallChanged(allVisual3ds);
}
}
//处理小管网改变
private void OnSmallChanged(List visual3ds)
{
if (visual3ds == null || visual3ds.Count < 1)
{
return;
}
this.SmallChangedEvent?.Invoke(visual3ds);
}
//附加
private void Attach()
{
_initialHelper.InitialCompletedEvent += OnInitialCompleted;
_viewport.MouseDown += OnMouseDown;
_viewport.MouseUp += OnMouseUp;
_viewport.MouseMove += OnMouseMove;
}
//分离
private void Detach()
{
_initialHelper.InitialCompletedEvent -= OnInitialCompleted;
_viewport.MouseDown -= OnMouseDown;
_viewport.MouseUp -= OnMouseUp;
_viewport.MouseMove -= OnMouseMove;
}
///
/// 释放
///
public void Dispose()
{
Detach();
}
}
}