using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using TProduct.ProcessDrawing.Components;
using TProduct.ProcessDrawing.Models;
namespace TProduct.ProcessDrawing.ViewModels
{
///
/// 对应窗口的业务逻辑
///
public class ConfigViewModel : ViewModelBase
{
public ObservableCollection DeviceList { get; set; } = new ObservableCollection();
//private RefObjectModel _refObjects = new RefObjectModel();
//public RefObjectModel RefObjects
//{
// get => _refObjects;
// set => Set(ref _refObjects, value);
//}
private DeviceItem4Config _currentDevice;
public DeviceItem4Config CurrentDevice
{
get => _currentDevice;
set => Set(ref _currentDevice, value);
}
private string _saveFailedMessage;
public string SaveFailedMessage
{
get { return _saveFailedMessage; }
set { Set(ref _saveFailedMessage, value); }
}
private bool _isSelectDevice = false;
public bool IsSelectDevice
{
get { return _isSelectDevice; }
set { Set(ref _isSelectDevice, value); }
}
public List ThumbList { get; set; }
public RelayCommand ToggleFlowDispCommand { get; set; }//切换水流效果是否显示
public RelayCommand ToggleAnchorDispCommand { get; set; }//切换锚点是否显示
public RelayCommand ThumbDropCommand { get; set; }
public RelayCommand ThumbDragEnterCommand { get; set; }
public RelayCommand ThumbDragLeaveCommand { get; set; }
public RelayCommand KeyDownCommand { get; set; }
public RelayCommand SaveCommand { get; set; }
public RelayCommand CloseSaveFailedCommand { get; set; }
public RelayCommand DeviceSelectedCommand { get; set; }
//private bool _windowState = false;
public RelayCommand CloseCommand { get; set; }
///
///
///
public ConfigViewModel()
{
if (!IsInDesignMode)
{
//初始化图标
IntialDeviceThumb();
// 初始化组态组件
InititialDevices();
//拖拽图标后
ThumbDropCommand = new RelayCommand(DoThumbItemDropCommand);
ThumbDragEnterCommand = new RelayCommand((obj) => {
this.StartDropThumb();
});
ThumbDragLeaveCommand = new RelayCommand((obj) => {
this.StopDropThumb();
});
//
SaveCommand = new RelayCommand(DoSaveCommand);
//
CloseSaveFailedCommand = new RelayCommand(obj =>
{
VisualStateManager.GoToElementState(obj as Window, "SaveFailedClose", true);
});
//点击组件
DeviceSelectedCommand = new RelayCommand(current_sel_model =>
{
// 记录一个当前选中组件
if (current_sel_model != null)
{
IsSelectDevice = true;
}
else
{
IsSelectDevice = false;
}
if (CurrentDevice != null)
{
if (current_sel_model != null)
{
if (CurrentDevice.NO == current_sel_model.NO)
{
CurrentDevice.IsAbleMove = true;
return;
}
}
CurrentDevice.IsSelected = false;
CurrentDevice.UiComponent.IsShowResizeCtrlPt = false;
}
if (current_sel_model != null)
{
current_sel_model.IsSelected = true;
current_sel_model.UiComponent.IsShowResizeCtrlPt = true ;
}
CurrentDevice = current_sel_model;
});
//
KeyDownCommand = new RelayCommand(obj =>
{
if (CurrentDevice != null)
{
var ea = obj as KeyEventArgs;
if (ea.Key == Key.Up)
CurrentDevice.Y--;
else if (ea.Key == Key.Down)
CurrentDevice.Y++;
else if (ea.Key == Key.Left)
CurrentDevice.X--;
else if (ea.Key == Key.Right)
CurrentDevice.X++;
else if (ea.Key == Key.Escape)
{
CurrentDevice.IsSelected = false;
CurrentDevice.UiComponent.IsShowResizeCtrlPt = false ;
}
}
});
//
CloseCommand = new RelayCommand(obj =>
{
//(obj as Window).DialogResult = _windowState;
});
//
ToggleFlowDispCommand = new RelayCommand(obj =>
{
var btn = obj as ToggleButton;
if (btn.IsChecked == true)
{
foreach (var p in this.DeviceList)
{
if (p.UiComponent != null)
{
(p.UiComponent).StartAnimation();
}
}
}
else
{
foreach (var p in this.DeviceList)
{
if (p.UiComponent != null)
{
(p.UiComponent).StopAnimation();
}
}
}
//(obj as Window).DialogResult = _windowState;
});
ToggleAnchorDispCommand = new RelayCommand(obj =>
{
var btn = obj as ToggleButton;
var is_disp = btn.IsChecked.Value;
foreach (var p in this.DeviceList)
{
if (p.UiComponent != null)
{
(p.UiComponent).IsShowAnchorPoint = is_disp;
}
}
});
}
}
//
private void IntialDeviceThumb()
{
ThumbList = DeviceItemHelper.GetAllThumb();
//foreach(var thb_cat in ThumbList)
//{//
// foreach(var thb in thb_cat.Children)
// {
// thb.ActionClick += (obj) =>
// {
// };
// }
//}
}
private bool _isDropDeviceThumb = false;
private void StartDropThumb()
{
if (_isDropDeviceThumb == true)
return;
_isDropDeviceThumb = true;
foreach(var item in this.DeviceList)
{
if (item.UiComponent != null)
{
item.UiComponent.IsShowAnchorPoint = true;
}
}
}
private void StopDropThumb()
{
if (_isDropDeviceThumb == false)
return;
_isDropDeviceThumb = false;
foreach (var item in this.DeviceList)
{
if (item.UiComponent != null)
{
item.UiComponent.IsShowAnchorPoint = false;
}
}
}
///
/// 初始化组态组件
///
private void InititialDevices()
{
if (DeviceList == null)
{
DeviceList = new ObservableCollection();
}
//wr = new DeviceItem4Config { X = 0, Y = 0, Width = 0, Height = 15, Z = 9999, ComponentType = "WidthRule", IsVisible = true };
//wr.BuildDeviceComponet();
//RefDeviceList.Add(wr);
//hr = new DeviceItem4Config { X = 0, Y = 0, Width = 15, Height = 0, Z = 9999, ComponentType = "HeightRule", IsVisible = true };
//hr.BuildDeviceComponet();
//RefDeviceList.Add(hr);
List all_DeviceItemModels = null;
if (!ProcessDrawingConfigFileHelper.ReadFile(out all_DeviceItemModels))
{
return;
}
foreach (var dim in all_DeviceItemModels)
{
AddDeviceItem(dim);
}
}
//
private void AddDeviceItem(TProduct.ProcessDrawing.Models.DeviceItem4Config dim)
{
dim.OnDeleteSelf += (model =>
{
DeviceList.Remove(model);
});
dim.OnStartMoveComponent = (comp) =>
{
foreach (var item in this.DeviceList)
{
if (item.UiComponent == null)
{
continue;
}
if (item == comp)
{
item.UiComponent.IsShowAnchorPoint = true;
}
else
{
item.UiComponent.IsShowAnchorPoint = true;
}
item.UiComponent.IsShowResizeCtrlPt = false;
}
};
dim.OnEndMoveComponent = (comp) =>
{
foreach (var item in this.DeviceList)
{
if (item.UiComponent == null)
{
continue;
}
if (item == comp)
{
item.UiComponent.IsShowResizeCtrlPt = true ;
}
item.UiComponent.IsShowAnchorPoint = false;
}
};
dim.OnSnapComponent = (source_comp,loc_x,loc_y) =>
{
var cur_comp_anchor_pts = source_comp.UiComponent.GetRefAnchorPoints();
if (cur_comp_anchor_pts == null)
return null;
foreach(var cur_comp_anchor_pt in cur_comp_anchor_pts)
{
foreach (var compare_item in this.DeviceList)
{
if (compare_item.UiComponent == null)
{
continue;
}
if (compare_item.NO == source_comp.NO)
continue;
var snap_posi = compare_item.SnapPosition(
loc_x + cur_comp_anchor_pt.X,
loc_y + cur_comp_anchor_pt.Y);
if (snap_posi != null)
{
return new System.Windows.Point(
snap_posi.Value.X - cur_comp_anchor_pt.X,
snap_posi.Value.Y - cur_comp_anchor_pt.Y);
}
}
}
return null;
};
if (dim.PropList != null)
{
foreach (var prp in dim.PropList)
{
prp.PropOptions = DeviceItemHelper.GetPropOptions(prp.PropTag);
}
}
if (DeviceList == null)
{
DeviceList = new ObservableCollection();
}
DeviceList.Add(dim);
}
//拖拽小图标后
//bool isFirst = true;
private void DoThumbItemDropCommand(object obj)
{
StopDropThumb();
DragEventArgs e = obj as DragEventArgs;
var thumb_model = (DeviceThumbItemModel)e.Data.GetData(typeof(DeviceThumbItemModel));
var mouse_point = e.GetPosition((IInputElement)e.Source);
//if (isFirst)
//{
// mouse_point.X = 100;
// mouse_point.Y = 100;
//}
//else
//{
// mouse_point.X = 100+800;
// mouse_point.Y = 100;
//}
var dim = new DeviceItem4Config(thumb_model);
var point = CalcSnapPosi4Drop(mouse_point.X, mouse_point.Y);
dim.SetPosiAnchorPoint(point);
dim.PropList = DeviceItemHelper.InitialDefaultProp(thumb_model.ComponentName,
dim.DoPropValueChanged);
AddDeviceItem(dim);
}
//抓取点位置
private System.Windows.Point CalcSnapPosi4Drop(double mouse_x, double mouse_y)
{
var _all_devices = DeviceList;
if (_all_devices == null || _all_devices.Count() == 0)
return new System.Windows.Point(mouse_x, mouse_y);
foreach (var dim in _all_devices)
{
var snap_pt = dim.SnapPosition(mouse_x, mouse_y);
if (snap_pt != null)
{
return snap_pt.Value;
}
}
return new System.Windows.Point(mouse_x, mouse_y);
}
//点击保存
private void DoSaveCommand(object obj)
{
VisualStateManager.GoToElementState(obj as Window, "NormalSuccess", true);
VisualStateManager.GoToElementState(obj as Window, "SaveFailedNormal", true);
try
{
//throw new Exception("保存异常测试,没有执行实际保存逻辑,只用作查看异常提示效果!");
ProcessDrawingConfigFileHelper.Save(DeviceList);
//_windowState = true;
// 提示保存成功
VisualStateManager.GoToElementState(obj as Window, "SaveSuccess", true);
}
catch (Exception ex)
{
SaveFailedMessage = ex.Message;
// 提示保存失败,包括异常信息
VisualStateManager.GoToElementState(obj as Window, "SaveFailedShow", true);
}
}
}
}