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 RotatingKnobControl : 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 string Dispstring = ""; //public ObservableCollection GridLines { get; set; } = new ObservableCollection(); //public double GridLineFreq { get; set; } = 10; //public event ValueChangedEventHandler ValueChanged; //public delegate void ValueChangedEventHandler(float Value); //public RotatingKnobControl() { InitializeComponent(); KnobImage.DataContext = this; } public RotatingKnobControl() { InitializeComponent(); NotifyPropertyChanged(nameof(RotAngle)); NotifyPropertyChanged(nameof(KnobImageSource)); } private void aKnob_Loaded(object sender, RoutedEventArgs e) { //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 }); //} Value = Value; //Necessary to trigger initial image rotation } private Point MouseDownLocation; public bool MouseIsDown; protected override void OnMouseDoubleClick(MouseButtonEventArgs e) { base.OnMouseDoubleClick(e); //Value = 0; } private float DownValue; 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; MouseDownLocation = e.GetPosition(this); DownValue = Value; } private readonly double changeSpeed = 0.03; protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (MouseIsDown) { double ydiff = (MouseDownLocation.Y - e.GetPosition(this).Y) * changeSpeed; Value = Math.Min(MaxValue, Math.Max(MinValue, DownValue + (float)ydiff)); } } 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)); } } double unitDegreeVal { get { return RotatableRange / (MaxValue - MinValue); } } private double RotatableRange = 270; private double minRotVal {get{return (360 - RotatableRange) / 2 ;} } public float Value { get { return (float)GetValue(ValueProperty); } set { if (value < MinValue) value = MinValue; if (value > MaxValue) value = MaxValue; RotAngle = minRotVal + (double)(value - MinValue) * unitDegreeVal; //NotifyPropertyChanged(nameof(ValueString)); SetValue(ValueProperty, value); } } internal void ValuePropertyChanged(float Val) { Value = Val; } public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(float), typeof(RotatingKnobControl), new FrameworkPropertyMetadata(0f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (o, e) => ((RotatingKnobControl)o).ValuePropertyChanged((float)e.NewValue))); public float MaxValue { get { return (float)GetValue(MaxValueProperty); } set { SetValue(MaxValueProperty, value); } } private void MaxValuePropertyChanged(float Val) { MaxValue = Val; } public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(float), typeof(RotatingKnobControl), new FrameworkPropertyMetadata(1f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (o, e) => ((RotatingKnobControl)o).MaxValuePropertyChanged((float)e.NewValue))); public float MinValue { get { return (float)GetValue(MinValueProperty); } set { SetValue(MinValueProperty, value); } } private void MinValuePropertyChanged(float Val) { MinValue = Val; } public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register("MinValue", typeof(float), typeof(RotatingKnobControl), new FrameworkPropertyMetadata(-1f, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (o, e) => ((RotatingKnobControl)o).MinValuePropertyChanged((float)e.NewValue))); public string KnobImageSource { get { return (string)GetValue(KnobImageSourceProperty); } set { SetValue(KnobImageSourceProperty, value); NotifyPropertyChanged(nameof(KnobImageSource)); } } private void KnobImageSourcePropertyChanged(string bsource) { KnobImageSource = bsource; } public static readonly DependencyProperty KnobImageSourceProperty = DependencyProperty.Register("KnobImageSource", typeof(string), typeof(RotatingKnobControl), new FrameworkPropertyMetadata("/EDITING/images/GoldKnob.png", FrameworkPropertyMetadataOptions.None, (o, e) => ((RotatingKnobControl)o).KnobImageSourcePropertyChanged((string)e.NewValue))); private void aKnob_SizeChanged(object sender, SizeChangedEventArgs e) { CenterXYImg = (this.ActualWidth - 6) / 2; } } }