|
using System; |
|
using System.Collections.Generic; |
|
using Windows.Foundation; |
|
using Windows.UI; |
|
using Windows.UI.Input; |
|
using Windows.UI.Input.Inking; |
|
using Windows.UI.Xaml; |
|
using Windows.UI.Xaml.Controls; |
|
using Windows.UI.Xaml.Navigation; |
|
|
|
namespace DialASketch |
|
{ |
|
/// <summary> |
|
/// An empty page that can be used on its own or navigated to within a Frame. |
|
/// </summary> |
|
public sealed partial class MainPage : Page |
|
{ |
|
private RadialController Controller; |
|
private RadialControllerMenuItem MenuItem; |
|
|
|
private bool isHorizontal = true; |
|
InkStrokeBuilder b = new InkStrokeBuilder(); |
|
private Point lastPoint; |
|
|
|
public MainPage() |
|
{ |
|
this.InitializeComponent(); |
|
SketchCanvas.SizeChanged += SketchCanvas_SizeChanged; |
|
|
|
Controller = RadialController.CreateForCurrentView(); |
|
Controller.RotationResolutionInDegrees = 2; |
|
Controller.RotationChanged += Controller_RotationChanged; |
|
Controller.ButtonClicked += Controller_ButtonClicked; |
|
|
|
MenuItem = RadialControllerMenuItem.CreateFromKnownIcon("DialASketch", RadialControllerMenuKnownIcon.PenType); |
|
Controller.Menu.Items.Add(MenuItem); |
|
} |
|
|
|
private void SketchCanvas_SizeChanged(object sender, SizeChangedEventArgs e) |
|
{ |
|
if (e.NewSize.Width > 0) |
|
{ |
|
if (lastPoint.X == 0f) |
|
{ |
|
lastPoint = new Windows.Foundation.Point { X = SketchCanvas.ActualWidth / 2, Y = SketchCanvas.ActualHeight / 2 }; |
|
|
|
Canvas.SetLeft(Pointer, lastPoint.X – 2); |
|
Canvas.SetTop(Pointer, lastPoint.Y – 2); |
|
} |
|
} |
|
} |
|
|
|
protected override void OnNavigatedFrom(NavigationEventArgs e) |
|
{ |
|
Controller?.Menu.Items.Clear(); |
|
|
|
base.OnNavigatedFrom(e); |
|
} |
|
|
|
private void Controller_ButtonClicked(RadialController sender, RadialControllerButtonClickedEventArgs args) |
|
{ |
|
// switch direction (since we only have one dial) |
|
isHorizontal = !isHorizontal; |
|
} |
|
|
|
private void Controller_RotationChanged(RadialController sender, RadialControllerRotationChangedEventArgs args) |
|
{ |
|
var delta = args.RotationDeltaInDegrees * 4; |
|
Point newPoint = new Windows.Foundation.Point { X = isHorizontal ? Math.Min(Math.Max(lastPoint.X + delta, 0), SketchCanvas.ActualWidth) : lastPoint.X, Y = !isHorizontal ? Math.Min(Math.Max(lastPoint.Y – delta, 0), SketchCanvas.ActualHeight) : lastPoint.Y }; |
|
|
|
Canvas.SetLeft(Pointer, newPoint.X – 2); |
|
Canvas.SetTop(Pointer, newPoint.Y – 2); |
|
|
|
var stroke = b.CreateStroke(new List<Point> { lastPoint, newPoint }); |
|
lastPoint = newPoint; |
|
stroke.DrawingAttributes.Color = Colors.Black; |
|
stroke.DrawingAttributes.PenTip = PenTipShape.Circle; |
|
SketchCanvas.InkPresenter.StrokeContainer.AddStroke(stroke); |
|
} |
|
|
|
private void ClearButton_Click(object sender, RoutedEventArgs e) |
|
{ |
|
SketchCanvas.InkPresenter.StrokeContainer.Clear(); |
|
|
|
lastPoint = new Windows.Foundation.Point { X = SketchCanvas.ActualWidth / 2, Y = SketchCanvas.ActualHeight / 2 }; |
|
|
|
Canvas.SetLeft(Pointer, lastPoint.X – 2); |
|
Canvas.SetTop(Pointer, lastPoint.Y – 2); |
|
} |
|
} |
|
} |