using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace QuikDawEditor { public partial class AdjusterKnob : UserControl, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } private double ImageMargin = 10; private bool _ShowGrid = false; public bool ShowGrid { get { return _ShowGrid; } set { _ShowGrid = value; NotifyPropertyChanged(nameof(ShowGridVisibility)); } } public Visibility ShowGridVisibility { get { return ShowGrid ? Visibility.Visible : Visibility.Hidden; } } public event ValueChangedEventHandler ValueChanged; public delegate void ValueChangedEventHandler(double Value); public void ChangeValue(double newval) { if (ValueChanged != null) ValueChanged(newval); } public string Dispstring = ""; public double MaximumValue { get; set; } public double MinimumValue { get; set; } public ObservableCollection GridLines { get; set; } = new ObservableCollection(); public AdjusterKnob() { InitializeComponent(); //KnobRect = new Rect(0, 0, ActualWidth, ActualHeight); DataContext = this; } public double GridLineFreq { get; set; } = 10; private void aKnob_Loaded(object sender, RoutedEventArgs e) { Value = MinimumValue; for (double r = minRotVal; r <= RotatableRange + minRotVal; r += GridLineFreq) { RotateTransform rtrans = new RotateTransform(r); rtrans.CenterX = CenterXYKnob; rtrans.CenterY = CenterXYKnob; GridLines.Add(new Line() { Stroke = Brushes.Green, StrokeThickness = 0.7, X1 = CenterXYKnob, X2 = CenterXYKnob, Y1 = CenterXYKnob, Y2 = CenterXYKnob * 2, RenderTransform = rtrans }); } } private Point LastMouseLocation; public bool MouseIsDown; protected override void OnMouseDoubleClick(MouseButtonEventArgs e) { base.OnMouseDoubleClick(e); //Value = 0; } protected override void OnMouseUp(MouseButtonEventArgs e) { base.OnMouseUp(e); MouseIsDown = false; this.ReleaseMouseCapture(); } protected override void OnMouseDown(MouseButtonEventArgs e) { base.OnMouseDown(e); this.CaptureMouse(); MouseIsDown = true; LastMouseLocation = e.GetPosition(this); InvalidateVisual(); } private double _RotAngle = 0; public double RotAngle { get { return _RotAngle; } set { _RotAngle = value; NotifyPropertyChanged(nameof(RotAngle)); } } private double _CenterXYImg; public double CenterXYImg { get { return _CenterXYImg; } set { _CenterXYImg = value; NotifyPropertyChanged(nameof(CenterXYImg)); } } private double _CenterXYKnob; public double CenterXYKnob { get { return _CenterXYKnob; } set { _CenterXYKnob = value; NotifyPropertyChanged(nameof(CenterXYKnob)); } } private readonly double changeSpeed = 12; protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (MouseIsDown) { double ydiff = (LastMouseLocation.Y - e.GetPosition(this).Y) * changeSpeed; //Value = Math.Min(MaximumValue, Math.Max(MinimumValue, MinimumValue + ydiff / (MaximumValue - MinimumValue))); Value = Math.Min(MaximumValue, Math.Max(MinimumValue, Value + ydiff / (MaximumValue - MinimumValue))); } } double unitDegreeVal { get { return RotatableRange / (MaximumValue - MinimumValue); } } private double _Value = 0; private double RotatableRange = 270; private double minRotVal {get{return (360 - RotatableRange) / 2 ;} } public double Value { get { return _Value; } set { _Value = value; RotAngle = minRotVal + (value - MinimumValue) * unitDegreeVal; NotifyPropertyChanged(nameof(Value)); ChangeValue(value); } } private void aKnob_SizeChanged(object sender, SizeChangedEventArgs e) { CenterXYKnob = this.ActualWidth / 2; CenterXYImg = KnobImage.ActualWidth / 2; } } }