Skip to content

Instantly share code, notes, and snippets.

@sakapon
Last active October 31, 2016 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakapon/63551342981a5a9fe4fa0450e19902bd to your computer and use it in GitHub Desktop.
Save sakapon/63551342981a5a9fe4fa0450e19902bd to your computer and use it in GitHub Desktop.
RxSample / MouseRx2Wpf / MainWindow
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