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
{
///
/// 运行控件
/// RunCanvas.xaml 的交互逻辑
///
public partial class RunCanvas : UserControl
{
public RunCanvas()
{
InitializeComponent();
Unloaded += (s, e) => Destory();
}
///
/// Dispose子集
///
public void Destory()
{
foreach (var item in RootCanvas.Children)
{
if (item is IDisposable disposable)
{
disposable.Dispose();
}
}
}
///
/// 加载数据
///
public void LoadingData(string path)
{
//加载控件
List Children = new List();
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);
}
}
///
/// 初始化
///
///
public void Init(List 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 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
}
}