Last active
October 31, 2016 02:32
-
-
Save sakapon/63551342981a5a9fe4fa0450e19902bd to your computer and use it in GitHub Desktop.
RxSample / MouseRx2Wpf / MainWindow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reactive.Linq; | |
using System.Windows; | |
namespace MouseRx2Wpf | |
{ | |
public partial class MainWindow : Window | |
{ | |
public static readonly DependencyProperty DeltaProperty = | |
DependencyProperty.Register(nameof(Delta), typeof(Vector?), typeof(MainWindow), new PropertyMetadata(null, (d, e) => ((MainWindow)d).DeltaChanged())); | |
public Vector? Delta | |
{ | |
get { return (Vector?)GetValue(DeltaProperty); } | |
set { SetValue(DeltaProperty, value); } | |
} | |
public static readonly DependencyProperty OrientationProperty = | |
DependencyProperty.Register(nameof(Orientation), typeof(string), typeof(MainWindow), new PropertyMetadata(null)); | |
public string Orientation | |
{ | |
get { return (string)GetValue(OrientationProperty); } | |
private set { SetValue(OrientationProperty, value); } | |
} | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
var events = new EventsExtension<Window>(this); | |
events.MouseDrag.Subscribe(d => d.Subscribe(v => Delta = v, () => Delta = null)); | |
} | |
void DeltaChanged() | |
{ | |
Orientation = Delta == null ? null : ToOrientation(Delta.Value); | |
} | |
const double π = Math.PI; | |
static readonly string[] orientationSymbols = new[] { "→", "↘", "↓", "↙", "←", "↖", "↑", "↗" }; | |
static readonly double zoneAngleRange = 2 * π / orientationSymbols.Length; | |
static string ToOrientation(Vector v) | |
{ | |
var angle = 2 * π + Math.Atan2(v.Y, v.X); | |
var zone = (int)Math.Round(angle / zoneAngleRange) % orientationSymbols.Length; | |
return orientationSymbols[zone]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment