using System.Collections.Generic; namespace Yw.WpfUI.Hydro { internal class SimpleViewManager { public SimpleViewManager(HelixViewport3D viewport) { _viewport = viewport; _materialManager = new SimpleMaterialManager(); _geometryManager = new SimpleGeometryManager(); _highlightManager = new SimpleHighlightManager(viewport); _highlightManager.StateChangedEvent += _highlightManager_StateChangedEvent; _selectionManager = new SimpleSelectionManager(viewport); _selectionManager.StateChangedEvent += _selectionManager_StateChangedEvent; _selectionManager.SelectionChangedEvent += _selectionManager_SelectionChangedEvent; _zoomManger = new ZoomManager(viewport); _stateManager = new VisualStateManager(); _highlightColor = ConstParas.SimpleHighlightColor.ToMediaColor(); _highlightFactor = ConstParas.SimpleHighlightFactor; _selectionColor = ConstParas.SimpleSelectionColor.ToMediaColor(); _selectionFactor = ConstParas.SimpleSelectionFactor; } #region 固定资源 private readonly HelixViewport3D _viewport = null; private readonly SimpleMaterialManager _materialManager = null; private readonly SimpleGeometryManager _geometryManager = null; private readonly ColorOverrideManager _colorOverrideManager = null; private readonly VisualStateManager _stateManager = null; private readonly ZoomManager _zoomManger = null; private readonly Color _highlightColor; private readonly double _highlightFactor; private readonly Color _selectionColor; private readonly double _selectionFactor; #endregion #region 私有字段 private NetworkL3d _nw = null; private Dictionary _allVisualL3dDict; private Dictionary _allVisualModelDict; #endregion #region 初始化 /// /// 是否初始化 /// public bool Initialized { get { if (_nw == null) { return false; } if (_allVisualL3dDict == null) { return false; } if (_allVisualModelDict == null) { return false; } return true; } } /// /// 初始化 /// public void Initial(NetworkL3d nw) { InitialNetwork(nw); InitialViewport(); InitialManager(); } //加载管网 private void InitialNetwork(NetworkL3d nw) { _nw = nw; if (_nw == null) { return; } _allVisualL3dDict = _nw.Visuals.ToDictionary(x => x.Id); _allVisualModelDict = new Dictionary(); _nw.Links.ForEach(x => { var visual3d = CreateVisual3D(x); _allVisualModelDict.Add(x, visual3d); }); _nw.Nodes.ForEach(x => { var visual3d = CreateVisual3D(x); _allVisualModelDict.Add(x, visual3d); }); } //初始化Vieport private void InitialViewport() { _viewport.Children.Clear(); _viewport.Children.Add(new DefaultLights()); _viewport.ViewCubeBackText = "后"; _viewport.ViewCubeBottomText = "下"; _viewport.ViewCubeFrontText = "前"; _viewport.ViewCubeLeftText = "左"; _viewport.ViewCubeRightText = "右"; _viewport.ViewCubeTopText = "上"; //_viewport.CameraController.ZoomAroundMouseDownPoint = true; //_viewport.CameraController.ZoomSensitivity = 1.1; // 设置缩放灵敏度 _allVisualModelDict?.Values.ToList().ForEach(x => _viewport.Children.Add(x)); _viewport.ZoomExtents(); } //初始化管理器 private void InitialManager() { _stateManager.Initial(_allVisualL3dDict?.Values.ToList()); } //选择改变事件 private void _selectionManager_SelectionChangedEvent(List obj) { } #endregion #region 高亮 //高亮管理器 private readonly SimpleHighlightManager _highlightManager = null; /// /// 高亮显示 /// public void Highlight(Point pt) { if (!Initialized) { return; } _highlightManager.Highlight(pt); } //高亮状态改变事件 private void _highlightManager_StateChangedEvent(Visual3D visual3d, eHighlightType highlightType) { if (visual3d == null) { return; } var visual = Visual3DProperties.GetTag(visual3d); if (visual == null) { return; } switch (highlightType) { case eHighlightType.Load: { _stateManager.LoadState(visual, eVisualState.Highlight); } break; case eHighlightType.Unload: { _stateManager.UnloadState(visual, eVisualState.Highlight); } break; default: break; } UpdateVisual3D(visual, visual3d); } #endregion #region 选择 //选择管理器 private readonly SimpleSelectionManager _selectionManager = null; /// /// 处理单个选择 /// public void HandleSingleSelection(Point pt) { if (!Initialized) { return; } _selectionManager.HandleSingle(pt); } //选择状态改变事件 private void _selectionManager_StateChangedEvent(Visual3D visual3d, eSelectionType selectionType) { if (visual3d == null) { return; } var visual = Visual3DProperties.GetTag(visual3d); if (visual == null) { return; } switch (selectionType) { case eSelectionType.Load: { _stateManager.LoadState(visual, eVisualState.Selection); } break; case eSelectionType.Unload: { _stateManager.UnloadState(visual, eVisualState.Selection); } break; default: break; } UpdateVisual3D(visual, visual3d); } #endregion #region 绘制逻辑 //更新Visual3D private void UpdateVisual3D(VisualL3d visual, Visual3D visual3d) { var state = _stateManager.GetEffectState(visual); var model3d = (visual3d as ModelVisual3D).Content as GeometryModel3D; model3d.Material = GetMaterial(visual, state); model3d.Geometry = GetGeometry(visual, state); } //创建Visual3D private Visual3D CreateVisual3D(VisualL3d visual) { var visual3d = new ModelVisual3D() { Content = CreateModel3D(visual) }; Visual3DProperties.SetTag(visual3d, visual); return visual3d; } //创建Model3D private Model3D CreateModel3D(VisualL3d visual) { var state = _stateManager.GetEffectState(visual); return new GeometryModel3D() { Geometry = GetGeometry(visual, state), Material = GetMaterial(visual, state) }; } //获取材质 private Material GetMaterial(VisualL3d visual, eVisualState state) { var color = GetVisual3DColor(visual, state); return _materialManager.GetMaterial(color); } //获取几何体 private Geometry3D GetGeometry(VisualL3d visual, eVisualState state) { var size = GetVisual3DSize(visual, state); var factor = GetVisual3DFactor(visual, state); return _geometryManager.GetGeometry(visual, size, factor); } //获取颜色 private Color GetVisual3DColor(VisualL3d visual, eVisualState state) { Color color; switch (state) { case eVisualState.Highlight: { color = _highlightColor; } break; case eVisualState.Selection: { color = _selectionColor; } break; case eVisualState.Override: { var cv = _colorOverrideManager.GetColor(visual); if (cv.HasValue) { color = cv.Value; } } break; default: { color = visual.SimpleStyle.HtmlColor.ToMediaColor(); } break; } return color; } //获取尺寸 private double GetVisual3DSize(VisualL3d visual, eVisualState state) { if (visual is NodeL3d node) { return node.SimpleStyle.Radiu; } if (visual is LinkL3d link) { return link.SimpleStyle.Diameter; } return default; } //获取因子 private double GetVisual3DFactor(VisualL3d visual, eVisualState state) { double factor = 1.0d; switch (state) { case eVisualState.Highlight: { factor = _highlightFactor; } break; case eVisualState.Selection: { factor = _selectionFactor; } break; case eVisualState.Override: { } break; default: { factor = 1.0d; } break; } return factor; } #endregion } }