using Aitex.Core.Common.DeviceData;
using MECF.Framework.Common.OperationCenter;
using MECF.Framework.UI.Core.Control;
using System;
using System.Collections.Generic;
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.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Aitex.Core.UI.Control
{
///
/// ValveControl.xaml 的交互逻辑
///
public partial class ValveControl : UserControl
{
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
"Command", typeof(ICommand), typeof(ValveControl),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
public ICommand Command
{
get
{
return (ICommand)this.GetValue(CommandProperty);
}
set
{
this.SetValue(CommandProperty, value);
}
}
public Window AITGasOwner { get; set; }
private SwitchDialog dialog;
public bool IsVirtualSwitchOpen { get; set; }
public bool IsEnableMouseRight
{
get { return (bool)GetValue(IsEnableMouseRightProperty); }
set { SetValue(IsEnableMouseRightProperty, value); }
}
// Using a DependencyProperty as the backing store for IsEnableMouseRight. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsEnableMouseRightProperty =
DependencyProperty.Register("IsEnableMouseRight", typeof(bool), typeof(ValveControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
public bool IsOpen
{
get
{
return (bool)this.GetValue(IsOpenProperty);
}
set
{
this.SetValue(IsOpenProperty, value);
}
}
public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register(
"IsOpen", typeof(bool), typeof(ValveControl),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty EnableServiceControlProperty = DependencyProperty.Register(
"EnableServiceControl", typeof(bool), typeof(ValveControl),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
public bool EnableServiceControl
{
get
{
return (bool)this.GetValue(EnableServiceControlProperty);
}
set
{
this.SetValue(EnableServiceControlProperty, value);
}
}
public static readonly DependencyProperty DeviceDataProperty = DependencyProperty.Register(
"DeviceData", typeof(AITValveData), typeof(ValveControl),
new FrameworkPropertyMetadata(new AITValveData(), FrameworkPropertyMetadataOptions.AffectsRender));
public AITValveData DeviceData
{
get
{
var data = (AITValveData)this.GetValue(DeviceDataProperty);
return data;
}
set
{
this.SetValue(DeviceDataProperty, value);
}
}
private int _rotationAngle;
public int RotationAngle
{
get => _rotationAngle;
set
{
_rotationAngle = value;
canvasRotate.Angle = _rotationAngle;
}
}
public ValveControl()
{
InitializeComponent();
IsEnableMouseRight = false;
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
if (IsOpen)
{
imgClose.Visibility = Visibility.Hidden;
imgOpen.Visibility = Visibility.Visible;
}
else
{
imgClose.Visibility = Visibility.Visible;
imgOpen.Visibility = Visibility.Hidden;
}
}
///
/// open
///
///
///
private void TurnOnValve(object sender, RoutedEventArgs e)
{
if (EnableServiceControl)
{
IsOpen = true;
InvokeClient.Instance.Service.DoOperation($"{DeviceData.UniqueName}.{AITValveOperation.GVTurnValve}", true);
this.UpdateLayout();
}
}
///
/// close
///
///
///
private void TurnOffValve(object sender, RoutedEventArgs e)
{
IsOpen = false;
InvokeClient.Instance.Service.DoOperation($"{DeviceData.UniqueName}.{AITValveOperation.GVTurnValve}", false);
this.UpdateLayout();
}
private void nopass_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (DeviceData == null || string.IsNullOrEmpty(DeviceData.DeviceName))
return;
ContextMenu mouseClickMenu = new ContextMenu();
MenuItem item = new MenuItem();
item.Header = "_" + DeviceData.DisplayName;
item.Background = Brushes.Gray;
item.Foreground = Brushes.White;
item.IsEnabled = false;
mouseClickMenu.Items.Add(item);
addOpenMenu(mouseClickMenu, item, false);
addCloseMenu(mouseClickMenu, item, false);
mouseClickMenu.IsOpen = true;
}
private void passtrigle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (DeviceData == null || string.IsNullOrEmpty(DeviceData.DeviceName))
return;
ContextMenu mouseClickMenu = new ContextMenu();
MenuItem item = new MenuItem();
item.Header = "_" + DeviceData.DeviceName;
item.Background = Brushes.Gray;
item.Foreground = Brushes.White;
item.IsEnabled = false;
mouseClickMenu.Items.Add(item);
addCloseMenu(mouseClickMenu, item, true);
addOpenMenu(mouseClickMenu, item, true);
mouseClickMenu.IsOpen = true;
}
void addOpenMenu(ContextMenu mouseClickMenu, MenuItem item, bool isEnable)
{
item = new MenuItem();
item.Header = "打开 (_O)";
item.Click += TurnOnValve;
item.Tag = this.Tag;
item.IsEnabled = isEnable;
mouseClickMenu.Items.Add(item);
}
void addCloseMenu(ContextMenu mouseClickMenu, MenuItem item, bool isEnable)
{
item = new MenuItem();
item.Header = "关闭 (_C)";
item.Tag = this.Tag;
item.Click += TurnOffValve;
item.IsEnabled = isEnable;
mouseClickMenu.Items.Add(item);
}
private void imgValve_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
if (!IsEnableMouseRight)
{ return; }
if (EnableServiceControl)
{
passtrigle_MouseLeftButtonDown(null, null);
}
else
{
nopass_MouseLeftButtonDown(null, null);
}
}
private void imgValve_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (!IsEnableMouseRight)
{ return; }
dialog = new SwitchDialog { };
dialog.IsOpen = EnableServiceControl ? IsOpen : IsVirtualSwitchOpen;
dialog.Owner = AITGasOwner;
dialog.Topmost = true;
//dialog.WindowStartupLocation = WindowStartupLocation.CenterScreen;
dialog.DeviceName = "Valve Name: " + DeviceData.DeviceName.Replace("Valve", "");
//var point = this.PointFromScreen(new Point(0, 0));
var point = System.Windows.Forms.Control.MousePosition;
double x = SystemParameters.WorkArea.Width;
double y = SystemParameters.WorkArea.Height;
if (point.Y + this.ActualHeight + 15 + dialog.Height < y)
{
dialog.Top = point.Y + dialog.ActualHeight + 15;
}
else
{
dialog.Top = point.Y - dialog.Height - 15;
}
if (point.X + dialog.Width < x)
{
dialog.Left = point.X;
}
else
{
dialog.Left = point.X - (dialog.Width - this.ActualWidth);
}
//Point centerP = new Point(dialog.Left + 100, dialog.Top + 100);
//MoveMouseToPoint(centerP);
dialog.ShowDialog();
if ((bool)dialog.IsSave)
{
if (EnableServiceControl)
{
IsOpen = dialog.IsOpen;
InvokeClient.Instance.Service.DoOperation($"{DeviceData.UniqueName}.{AITValveOperation.GVTurnValve}", dialog.IsOpen);
return;
}
// IsVirtualSwitchOpen = dialog.IsOpen;
if (Command == null)
return;
//Command.Execute(new object[] { DeviceData.UniqueName, AITValveOperation.GVVirtualTurnValve, dialog.IsOpen });
//if (DeviceData2 != null && !string.IsNullOrEmpty(DeviceData2.DeviceName))
//{
// Command.Execute(new object[] { DeviceData2.DeviceName, AITValveOperation.GVTurnValve, !dialog.IsOpen });
//}
}
}
}
}