using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Tango.Editors; using Tango.BL.Entities; using Tango.MachineStudio.Technician.TechItems; using Tango.Core; namespace Tango.MachineStudio.Technician.Editors { [ContentProperty("InnerContent")] public partial class DispenserElementEditor : ElementEditor { /// /// Initializes a new instance of the class. /// public DispenserElementEditor() : base() { InitializeComponent(); } /// /// Initializes a new instance of the class. /// /// The framework element. public DispenserElementEditor(DispenserItem dispenserItem) : this() { DispenserItem = dispenserItem; DataContext = DispenserItem; } /// /// Initializes a new instance of the class. /// /// The framework element. /// The bounds. public DispenserElementEditor(DispenserItem monitorItem, Rect bounds) : this(monitorItem) { Left = bounds.Left; Top = bounds.Top; Width = bounds.Width; Height = bounds.Height; } private DispenserItem _monitorItem; public DispenserItem DispenserItem { get { return _monitorItem; } set { _monitorItem = value; RaisePropertyChanged(nameof(DispenserItem)); if (_monitorItem != null) { _monitorItem.HomingCompleted -= _monitorItem_HomingCompleted; _monitorItem.HomingCompleted += _monitorItem_HomingCompleted; } } } private void _monitorItem_HomingCompleted(object sender, EventArgs e) { StopAnimation(); } /// /// Clones this instance. /// /// public override IElementEditor Clone() { try { var clonedItem = DispenserItem.Clone() as DispenserItem; DispenserElementEditor cloned = new DispenserElementEditor(clonedItem); cloned.Top = Top; cloned.Left = Left; cloned.Width = Width; cloned.Height = Height; cloned.Angle = Angle; return cloned; } catch (Exception ex) { throw new InvalidOperationException("Could not clone this editor. You may have to create a custom editor and implement a custom Clone method.", ex); } } /// /// Gets the hosted element. /// [ParameterIgnore] public override Object HostedElement { get { return DispenserItem; } } private void OnForwardPressed(object sender, MouseButtonEventArgs e) { (e.Source as FrameworkElement).CaptureMouse(); AnimateRight(); DispenserItem.RaiseAction(MotorActionType.ForwardPressed); } private void OnForwardReleased(object sender, MouseButtonEventArgs e) { (e.Source as FrameworkElement).ReleaseMouseCapture(); StopAnimation(); DispenserItem.RaiseAction(MotorActionType.ForwardReleased); } private void OnBackwardPressed(object sender, MouseButtonEventArgs e) { (e.Source as FrameworkElement).CaptureMouse(); AnimateLeft(); DispenserItem.RaiseAction(MotorActionType.BackwardPressed); } private void OnBackwardReleased(object sender, MouseButtonEventArgs e) { (e.Source as FrameworkElement).ReleaseMouseCapture(); StopAnimation(); DispenserItem.RaiseAction(MotorActionType.BackwardReleased); } private void OnHomingStarted(object sender, RoutedEventArgs e) { AnimateLeft(); DispenserItem.RaiseAction(MotorActionType.HomingForwardStarted); } private void OnHomingStopped(object sender, RoutedEventArgs e) { StopAnimation(); DispenserItem.RaiseAction(MotorActionType.HomingStopped); } private void AnimateRight() { DoubleAnimation ani = new DoubleAnimation(); ani.Duration = TimeSpan.FromSeconds(1); ani.RepeatBehavior = RepeatBehavior.Forever; ani.FillBehavior = FillBehavior.HoldEnd; ani.To = 360; propRotate.BeginAnimation(RotateTransform.AngleProperty, ani); } private void AnimateLeft() { DoubleAnimation ani = new DoubleAnimation(); ani.Duration = TimeSpan.FromSeconds(1); ani.RepeatBehavior = RepeatBehavior.Forever; ani.FillBehavior = FillBehavior.HoldEnd; ani.To = -360; propRotate.BeginAnimation(RotateTransform.AngleProperty, ani); } public void StopAnimation() { this.Dispatcher.Invoke(() => { propRotate.BeginAnimation(RotateTransform.AngleProperty, null); }); } private void OnHomingBackwardStarted(object sender, RoutedEventArgs e) { AnimateLeft(); DispenserItem.RaiseAction(MotorActionType.HomingBackwardStarted); } private void OnHomingForwardStarted(object sender, RoutedEventArgs e) { AnimateRight(); DispenserItem.RaiseAction(MotorActionType.HomingForwardStarted); } } }