|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using BPASmartClient.Compiler;
- using BPASmartClient.SCADAControl;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Markup;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
-
- namespace BeDesignerSCADA.Controls
- {
- /// <summary>
- /// 运行控件
- /// RunCanvas.xaml 的交互逻辑
- /// </summary>
- public partial class RunCanvas : UserControl
- {
- public RunCanvas()
- {
- InitializeComponent();
- Unloaded += (s, e) => Destory();
- }
-
- /// <summary>
- /// Dispose子集
- /// </summary>
- public void Destory()
- {
- foreach (var item in RootCanvas.Children)
- {
- if (item is IDisposable disposable)
- {
- disposable.Dispose();
- }
- }
- }
-
- /// <summary>
- /// 加载数据
- /// </summary>
- public void LoadingData(string path)
- {
- //加载控件
- List<FrameworkElement> Children = new List<FrameworkElement>();
- FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
- using (StreamReader sr = new StreamReader(fs, System.Text.Encoding.Unicode))
- {
- while (sr.Peek() > -1)
- {
- string str = sr.ReadLine();
- var ele = XamlReader.Parse(str) as FrameworkElement;
- Children.Add(ele);
- }
- }
- if (Children.Count > 0)
- {
- Run(Children);
- }
- }
-
- /// <summary>
- /// 初始化
- /// </summary>
- /// <param name="canvas"></param>
- public void Init(List<FrameworkElement> canvas)
- {
- RootCanvas.Children.Clear();
- foreach (FrameworkElement element in canvas)
- {
- RootCanvas.Children.Add(element);
- }
- }
-
- public void RunSelect()
- {
- Config.GetInstance().Reset();
- foreach (FrameworkElement element in RootCanvas.Children)
- {
- RegisterJsName(element);//注册rejs
- }
- foreach (FrameworkElement element in RootCanvas.Children)
- {
- if (element is IExecutable executable)
- executable.IsExecuteState = true;
- }
- }
-
-
- public void Run(List<FrameworkElement> canvas)
- {
- RootCanvas.Children.Clear();
- foreach (FrameworkElement element in canvas)
- {
- if (element is IExecutable executable)
- executable.IsExecuteState = true;
-
- RootCanvas.Children.Add(element);
- RegisterJsName(element);
- }
- }
-
- // 注册名称到Js
- static void RegisterJsName(FrameworkElement element)
- {
- Config.GetInstance().SetVariable(element.Name, element);
- if (element is Panel panel)
- {
- foreach (var item in panel.Children)
- {
- RegisterJsName(item as FrameworkElement);
- }
- }
- }
-
- #region 拖动与缩放
- private void RootCanvas_MouseMove(object sender, MouseEventArgs e)
- {
- if (DragEnable.IsChecked == false)
- {
- return;
- }
-
- if (e.LeftButton == MouseButtonState.Pressed && isPressed)
- {
- Point point = e.GetPosition(this);
- var movex = (point.X - last.X);
- var movey = (point.Y - last.Y);
-
- Translate.X += movex;
- Translate.Y += movey;
- last = point;
-
- }
- }
-
- bool isPressed = false;
- Point last;//记录上次鼠标坐标位置
- private void RootCanvas_MouseLeftButtoDown(object sender, MouseButtonEventArgs e)
- {
- last = e.GetPosition(this);
- isPressed = true;
- }
-
- private void RootCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- isPressed = false;
- }
-
- // 缩放
- private void RootCanvas_MouseWheel(object sender, MouseWheelEventArgs e)
- {
- if (ZoomEnable.IsChecked == false)
- {
- return;
- }
- var zoomS = (e.Delta / 960d);
- var zoom = zoomS + Scale.ScaleX;
- if (zoom > 3 || zoom < 0.8)
- {
- return;
- }
- Scale.ScaleX = Scale.ScaleY = zoom;
-
- Point mouse = e.GetPosition(RootCanvas);
- Point newMouse = new Point(mouse.X * zoomS, mouse.Y * zoomS);
-
- Translate.X -= newMouse.X;
- Translate.Y -= newMouse.Y;
- }
- #endregion
- }
- }
|