using NEIntelligentControl2.Models.Windturbine;
using NEIntelligentControl2.Views.Basic;
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 NEIntelligentControl2.Views.Infos
{
///
/// 状态信息
///
public partial class StateInfo : UserControl
{
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(List), typeof(StateInfo), new PropertyMetadata(ItemsSourceChanged));
public static readonly DependencyProperty ItemsTitleProperty = DependencyProperty.Register("ItemsTitle", typeof(string), typeof(StateInfo), new PropertyMetadata(ItemsTitleChanged));
///
/// 内容集合
///
public List ItemsSource { get => GetValue(ItemsSourceProperty) as List; set => SetValue(ItemsSourceProperty, value); }
public string ItemsTitle { get => GetValue(ItemsTitleProperty) as string; set => SetValue(ItemsTitleProperty, value); }
///
/// 框选开始坐标
///
private Point _StartPoint;
///
/// 选择的时间
///
private double _SelectedTime = 0;
private bool _IsWidth;
public StateInfo()
{
InitializeComponent();
}
private static void ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var si = d as StateInfo;
if (si == null) return;
si.SetStateValue();
}
private static void ItemsTitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var si = d as StateInfo;
if (si == null) return;
if (!string.IsNullOrWhiteSpace(e.NewValue as string))
{
si._tbtitle.Visibility = Visibility.Visible;
}
si._tbtitle.Text = e.NewValue as string;
}
///
/// 设置数值
///
private void SetStateValue()
{
_SelectedTime = 0;
_tb.Text = Math.Round(_SelectedTime, 2).ToString();
_sp.Children.Clear();
_IsWidth = this.ActualWidth == 0;
double width = _IsWidth ? this.Width - 70 : _sp.ActualWidth;
Console.WriteLine(width);
var itemsSource = this.ItemsSource;
if (itemsSource == null) return;
double totalTime = itemsSource.Sum(i => i.Duration);
foreach (var v in itemsSource)
{
StateInfoPoint sip = new StateInfoPoint(v) { TotalWidth = width, TotalTime = totalTime, SelectionChanged = ItemSelectionChanged };
_sp.Children.Add(sip);
}
}
private void ItemSelectionChanged(bool arg1, double arg2)
{
_SelectedTime = arg1 ? _SelectedTime + arg2 : _SelectedTime - arg2;
_tb.Text = Math.Round(_SelectedTime, 2).ToString();
}
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_StartPoint = e.GetPosition(sender as Grid);
}
private void Grid_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton != MouseButtonState.Pressed) return;
var ct2 = sender as Grid;
var ponit = e.GetPosition(ct2);
if (_RTMain.Visibility != Visibility.Visible)
{
_RTMain.Visibility = Visibility.Visible;
}
Point p1 = new Point();
p1.X = Math.Min(_StartPoint.X, ponit.X);
p1.Y = Math.Min(_StartPoint.Y, ponit.Y);
Point p2 = new Point();
p2.X = Math.Max(_StartPoint.X, ponit.X);
p2.Y = Math.Max(_StartPoint.Y, ponit.Y);
_RTMain.Margin = new Thickness(p1.X, p1.Y, ct2.ActualWidth - p2.X, ct2.ActualHeight - p2.Y);
if (_IsWidth)
{
p1.X -= 70;
p2.X -= 70;
}
double d = 0;
foreach (var v in _sp.Children)
{
var ct = v as StateInfoPoint;
var left = ct.TranslatePoint(default(Point), _sp).X;
var right = left + ct.ActualWidth;
var b = right >= p1.X && left <= p2.X;
if (Keyboard.Modifiers == ModifierKeys.Control)
{
if (b) ct.IsSelected = true;
}
else
{
ct.IsSelected = b;
}
if (ct.IsSelected)
{
d += ct.Time;
}
}
_SelectedTime = d;
_tb.Text = Math.Round(d, 2).ToString();
}
private void Grid_MouseLeave(object sender, MouseEventArgs e)
{
_RTMain.Visibility = Visibility.Hidden;
}
private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_RTMain.Visibility = Visibility.Hidden;
}
}
}