-
Notifications
You must be signed in to change notification settings - Fork 67
/
ColoringBookInkToolbar.xaml.cs
821 lines (708 loc) · 31.5 KB
/
ColoringBookInkToolbar.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
// ---------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ---------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Numerics;
using ColoringBook.Common;
using ColoringBook.Models;
using Windows.Foundation;
using Windows.Graphics.Display;
using Windows.System;
using Windows.UI;
using Windows.UI.Input;
using Windows.UI.Input.Inking;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
namespace ColoringBook.Views
{
public sealed partial class ColoringBookInkToolbar : UserControl
{
public ColoringBookInkToolbar()
{
InitializeComponent();
PenWidthChangedHandler = PenStrokeWidthSlider_PointerReleased;
PenStrokeWidthSlider.AddHandler(PointerReleasedEvent, PenWidthChangedHandler, true);
PencilWidthChangedHandler = PencilStrokeWidthSlider_PointerReleased;
PencilStrokeWidthSlider.AddHandler(PointerReleasedEvent, PencilWidthChangedHandler, true);
Initialize();
Loaded += ColoringBookInkToolbar_Loaded;
AccessibilitySettings.HighContrastChanged += AccessibilitySettings_HighContrastChanged;
InitializeHighContrastFillButtonHover();
DisplayInformation = DisplayInformation.GetForCurrentView();
DisplayInformation.DpiChanged += DisplayInformation_DpiChanged;
UpdateUiScaling();
}
public event EventHandler<DrawingToolChangedEventArgs> DrawingToolChanged;
public InkCanvas TargetInkCanvas { get; set; }
public SolidColorBrush TargetCurrentColorBrush { get; set; }
private void DisplayInformation_DpiChanged(DisplayInformation sender, object args)
{
DisplayInformation = sender;
UpdateUiScaling();
}
private void UpdateUiScaling()
{
int currentScaling = (int)(DisplayInformation.RawPixelsPerViewPixel * 100);
if (currentScaling >= 400)
{
CurrentUiScaling = 400;
}
else if (currentScaling >= 200)
{
CurrentUiScaling = 200;
}
else if (currentScaling >= 150)
{
CurrentUiScaling = 150;
}
else if (currentScaling >= 125)
{
CurrentUiScaling = 125;
}
else
{
CurrentUiScaling = 100;
}
}
private BitmapImage GetBitmapImageForFillIcon(string contrastName, int scale)
{
BitmapImage bitmapImage = new BitmapImage();
string addressOfImage = string.Concat("../Assets/Icons/Bucket/BucketColor.scale-",
scale.ToString(), "_contrast-", contrastName, ".png");
bitmapImage.UriSource = new Uri((this as FrameworkElement).BaseUri, addressOfImage);
return bitmapImage;
}
private void InitializeHighContrastFillButtonHover()
{
if (!AccessibilitySettings.HighContrast)
{
ContrastHandlerAttached = ContrastHandler.None;
return;
}
string highContrastScheme = AccessibilitySettings.HighContrastScheme;
if (highContrastScheme == "High Contrast Black")
{
FillCellButton.PointerEntered += SetFillCellButtonToWhite;
FillCellButton.PointerExited += SetFillCellButtonToBlack;
ContrastHandlerAttached = ContrastHandler.Black;
// On changing HC mode while app is running, it doesn't update icon. Do it explicitly.
FillCellButtonImage.Source = GetBitmapImageForFillIcon("black", CurrentUiScaling);
}
else if (highContrastScheme == "High Contrast White")
{
FillCellButton.PointerEntered += SetFillCellButtonToBlack;
FillCellButton.PointerExited += SetFillCellButtonToWhite;
ContrastHandlerAttached = ContrastHandler.White;
// On changing HC mode while app is running, it doesn't update icon. Do it explicitly.
FillCellButtonImage.Source = GetBitmapImageForFillIcon("white", CurrentUiScaling);
}
else
{
// On changing HC mode while app is running, it doesn't update icon. Do it explicitly.
FillCellButtonImage.Source = GetBitmapImageForFillIcon("black", CurrentUiScaling);
ContrastHandlerAttached = ContrastHandler.None;
}
}
private void SetFillCellButtonToWhite(object sender, PointerRoutedEventArgs e) =>
FillCellButtonImage.Source = GetBitmapImageForFillIcon("white", CurrentUiScaling);
private void SetFillCellButtonToBlack(object sender, PointerRoutedEventArgs e) =>
FillCellButtonImage.Source = GetBitmapImageForFillIcon("black", CurrentUiScaling);
private void AccessibilitySettings_HighContrastChanged(AccessibilitySettings sender, object args)
{
if (ContrastHandlerAttached == ContrastHandler.Black)
{
FillCellButton.PointerEntered -= SetFillCellButtonToWhite;
FillCellButton.PointerExited -= SetFillCellButtonToBlack;
}
else if (ContrastHandlerAttached == ContrastHandler.White)
{
FillCellButton.PointerEntered -= SetFillCellButtonToBlack;
FillCellButton.PointerExited -= SetFillCellButtonToWhite;
}
InitializeHighContrastFillButtonHover();
}
private void ColoringBookInkToolbar_Loaded(object sender, RoutedEventArgs e)
{
// Set initial button state
PenButton.IsChecked = true;
VisualStateManager.GoToState(PenButton, "ShowExtensionGlyph", false);
}
private void Initialize()
{
CreatePenPreviewStroke();
UpdatePenPreviewInkStroke();
CreatePencilPreviewStroke();
UpdatePencilPreviewInkStroke();
InitializeRadialController();
}
private void OnDrawingToolChanged(DrawingTool newTool)
{
DrawingToolChanged?.Invoke(this,
new DrawingToolChangedEventArgs { NewDrawingTool = newTool });
if (newTool == DrawingTool.FillCell)
{
SelectedToolIndex = 0;
}
else if (newTool == DrawingTool.Pen)
{
SelectedToolIndex = 1;
}
else if (newTool == DrawingTool.Pencil)
{
SelectedToolIndex = 2;
}
else
{
// Eraser
SelectedToolIndex = 3;
}
}
private void InitializeRadialController()
{
RadialController = RadialController.CreateForCurrentView();
RadialController.RotationResolutionInDegrees = 15;
RadialController.Menu.Items.Clear();
RadialController.RotationChanged += RadialController_RotationChanged;
RadialController.ButtonClicked += RadialController_ButtonClicked;
RadialControllerMenuItem inkingToolRadialMenuItem =
RadialControllerMenuItem.CreateFromKnownIcon("Ink Tool",
RadialControllerMenuKnownIcon.PenType);
RadialControllerMenuItem strokeSizeRadialMenuItem =
RadialControllerMenuItem.CreateFromKnownIcon("Size",
RadialControllerMenuKnownIcon.InkThickness);
inkingToolRadialMenuItem.Invoked += InkingToolRadialMenuItem_Invoked;
strokeSizeRadialMenuItem.Invoked += StrokeSizeRadialMenuItem_Invoked;
RadialController.Menu.Items.Add(inkingToolRadialMenuItem);
RadialController.Menu.Items.Add(strokeSizeRadialMenuItem);
}
public void UnregisterRadialController()
{
if (RadialController != null)
{
RadialController.Menu.Items.Clear();
RadialController.RotationChanged -= RadialController_RotationChanged;
RadialController.ButtonClicked -= RadialController_ButtonClicked;
}
RadialControllerConfiguration.GetForCurrentView().ResetToDefaultMenuItems();
}
private void StrokeSizeRadialMenuItem_Invoked(RadialControllerMenuItem sender, object args)
{
CurrentRadialSelection = ColoringBookRadialController.StrokeSize;
// Open flyout and bring focus to slider.
if (SelectedToolIndex == 1 || SelectedToolIndex == 2)
{
RadioButton currentTool = (SelectedToolIndex == 1) ? PenButton : PencilButton;
var flyout = FlyoutBase.GetAttachedFlyout(currentTool);
flyout.ShowAt(currentTool as FrameworkElement);
Slider slider = (SelectedToolIndex == 1) ? PenStrokeWidthSlider : PencilStrokeWidthSlider;
slider.Focus(FocusState.Keyboard);
}
}
private void InkingToolRadialMenuItem_Invoked(RadialControllerMenuItem sender, object args) =>
CurrentRadialSelection = ColoringBookRadialController.InkingTool;
private void RadialController_ButtonClicked(RadialController sender,
RadialControllerButtonClickedEventArgs args)
{
if (SelectedToolIndex == 1 || SelectedToolIndex == 2)
{
// Pen or pencil.
RadioButton currentTool = (SelectedToolIndex == 1) ? PenButton : PencilButton;
var flyout = FlyoutBase.GetAttachedFlyout(currentTool);
Slider slider = (SelectedToolIndex == 1) ? PenStrokeWidthSlider : PencilStrokeWidthSlider;
// Flyout is opened and focus is on slider.
if (slider.FocusState == FocusState.Keyboard)
{
flyout.Hide();
currentTool.Focus(FocusState.Keyboard);
}
else
{
// Flyout is not open, and dial click is on pen/pencil.
CurrentRadialSelection = ColoringBookRadialController.StrokeSizeDialClick;
flyout.ShowAt(currentTool as FrameworkElement);
flyout.Closed += DialInvokedPenPencilFlyoutClosed;
slider.Focus(FocusState.Keyboard);
}
}
else if (SelectedToolIndex == 3)
{
if (CurrentRadialSelection == ColoringBookRadialController.InkingTool)
{
// Eraser, open flyout and now rotation goes to selection of erasing tool.
CurrentRadialSelection = ColoringBookRadialController.EraserTool;
var flyout = FlyoutBase.GetAttachedFlyout(EraserButton);
flyout.ShowAt(EraserButton as FrameworkElement);
flyout.Opened += DialInvokedEraserFlyout_Opened;
flyout.Closed += EraserButtonFlyoutClosed;
}
else if (CurrentRadialSelection == ColoringBookRadialController.EraserTool)
{
// If dial has been clicked while selecting erase tool, we collapse the flyout.
var flyout = FlyoutBase.GetAttachedFlyout(EraserButton);
flyout.Hide();
}
}
}
private void DialInvokedPenPencilFlyoutClosed(object sender, object e)
{
CurrentRadialSelection = ColoringBookRadialController.InkingTool;
(sender as FlyoutBase).Closed -= DialInvokedPenPencilFlyoutClosed;
}
private void DialInvokedEraserFlyout_Opened(object sender, object e)
{
// Now set focus on selected erasing tool.
if (DrawingTool == DrawingTool.Eraser)
{
StrokeEraseListItem.Focus(FocusState.Keyboard);
}
else
{
CellEraseListItem.Focus(FocusState.Keyboard);
}
(sender as FlyoutBase).Opened -= DialInvokedEraserFlyout_Opened;
}
private void RadialController_RotationChanged(RadialController sender,
RadialControllerRotationChangedEventArgs args)
{
RadioButton[] inkingTools = { FillCellButton, PenButton, PencilButton, EraserButton };
Action[] inkingActions =
{
OnFillCellButtonClicked,
OnPenButtonClicked,
OnPencilButtonClicked,
OnEraserButtonClicked
};
if (CurrentRadialSelection == ColoringBookRadialController.InkingTool)
{
SelectedToolIndex = (4 + SelectedToolIndex +
(args.RotationDeltaInDegrees > 0 ? 1 : -1)) % 4;
inkingActions[SelectedToolIndex]();
inkingTools[SelectedToolIndex].IsChecked = true;
inkingTools[SelectedToolIndex].Focus(FocusState.Keyboard);
}
else if (CurrentRadialSelection == ColoringBookRadialController.StrokeSize)
{
if (SelectedToolIndex == 1 || SelectedToolIndex == 2)
{
var flyout = FlyoutBase.GetAttachedFlyout(inkingTools[SelectedToolIndex]);
flyout.ShowAt(inkingTools[SelectedToolIndex] as FrameworkElement);
Slider slider = (SelectedToolIndex == 1) ?
PenStrokeWidthSlider : PencilStrokeWidthSlider;
slider.Focus(FocusState.Keyboard);
int val = (int)args.RotationDeltaInDegrees;
int newVal = ((int)slider.Value) + (val > 0 ? 1 : -1);
newVal = (newVal > 24) ? 24 : newVal;
newVal = (newVal < 0) ? 0 : newVal;
slider.Value = newVal;
}
}
else if (CurrentRadialSelection == ColoringBookRadialController.StrokeSizeDialClick)
{
Slider slider = (SelectedToolIndex == 1) ? PenStrokeWidthSlider : PencilStrokeWidthSlider;
slider.Focus(FocusState.Keyboard);
int val = (int)args.RotationDeltaInDegrees;
int newVal = ((int)slider.Value) + (val > 0 ? 1 : -1);
newVal = (newVal > 24) ? 24 : newVal;
newVal = (newVal < 0) ? 0 : newVal;
slider.Value = newVal;
}
else if (CurrentRadialSelection == ColoringBookRadialController.EraserTool)
{
if ((args.RotationDeltaInDegrees > 0) && (DrawingTool == DrawingTool.Eraser))
{
EraserFlyoutList.SelectedIndex = 1;
CellEraseListItem.Focus(FocusState.Keyboard);
}
else if ((args.RotationDeltaInDegrees < 0) && (DrawingTool == DrawingTool.EraseCell))
{
EraserFlyoutList.SelectedIndex = 0;
StrokeEraseListItem.Focus(FocusState.Keyboard);
}
}
}
private void PenButton_Checked(object sender, RoutedEventArgs e) =>
VisualStateManager.GoToState(PenButton, "ShowExtensionGlyph", false);
private void PenButton_Unchecked(object sender, RoutedEventArgs e) =>
VisualStateManager.GoToState(PenButton, "HideExtensionGlyph", false);
private void OnOpenPenButtonFlyout()
{
FlyoutBase.ShowAttachedFlyout(PenButton);
VisualStateManager.GoToState(PenButton, "HideExtensionGlyph", false);
var flyout = FlyoutBase.GetAttachedFlyout(PenButton);
flyout.Closed += PenButtonFlyoutClosed;
}
private void PenButtonFlyoutClosed(object sender, object e)
{
VisualStateManager.GoToState(PenButton, "ShowExtensionGlyph", false);
if (sender is FlyoutBase flyoutBase)
{
flyoutBase.Closed -= PenButtonFlyoutClosed;
}
}
private void CreatePenPreviewStroke()
{
var strokePreviewInkPoints = new List<InkPoint>();
for (var i = 0; i < PreviewStrokeCoordinates.Length; i += 2)
{
var newPoint = new Point(PreviewStrokeCoordinates[i],
PreviewStrokeCoordinates[i + 1] + 10);
var inkPoint = new InkPoint(newPoint, 0.5f);
strokePreviewInkPoints.Add(inkPoint);
}
var inkStrokeBuilder = new InkStrokeBuilder();
var newStroke = inkStrokeBuilder.CreateStrokeFromInkPoints(
strokePreviewInkPoints, Matrix3x2.Identity);
PenPreviewInkStroke = newStroke;
}
private void UpdatePenPreviewInkStroke()
{
var currentStrokeSize = PenStrokeWidthSlider.Value;
var inkDrawingAttributes = PenStrokePreviewInkCanvas.InkPresenter
.CopyDefaultDrawingAttributes();
inkDrawingAttributes.Color = StrokePreviewColor;
inkDrawingAttributes.Size = new Size(currentStrokeSize, currentStrokeSize);
if (PenPreviewInkStroke == null)
{
CreatePenPreviewStroke();
}
PenPreviewInkStroke.DrawingAttributes = inkDrawingAttributes;
var strokeContainer = PenStrokePreviewInkCanvas.InkPresenter.StrokeContainer;
foreach (var stroke in strokeContainer.GetStrokes())
{
stroke.Selected = true;
}
strokeContainer.AddStroke(PenPreviewInkStroke.Clone());
strokeContainer.DeleteSelected();
}
private void PenStrokeWidthSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) =>
UpdatePenPreviewInkStroke();
private void PenStrokeWidthSlider_PointerReleased(object sender, RoutedEventArgs e) =>
FlyoutBase.GetAttachedFlyout(PenButton)?.Hide();
private void PenStrokeWidthSlider_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter || e.Key == VirtualKey.Space)
{
var y = FlyoutBase.GetAttachedFlyout(PenButton);
y?.Hide();
}
}
private void PencilButton_Checked(object sender, RoutedEventArgs e) =>
VisualStateManager.GoToState(PencilButton, "ShowExtensionGlyph", false);
private void PencilButton_Unchecked(object sender, RoutedEventArgs e) =>
VisualStateManager.GoToState(PencilButton, "HideExtensionGlyph", false);
private void OnOpenPencilButtonFlyout()
{
FlyoutBase.ShowAttachedFlyout(PencilButton);
VisualStateManager.GoToState(PencilButton, "HideExtensionGlyph", false);
var flyout = FlyoutBase.GetAttachedFlyout(PencilButton);
flyout.Closed += PencilButtonFlyoutClosed;
}
private void PencilButtonFlyoutClosed(object sender, object e)
{
VisualStateManager.GoToState(PencilButton, "ShowExtensionGlyph", false);
if (sender is FlyoutBase flyoutBase)
{
flyoutBase.Closed -= PencilButtonFlyoutClosed;
}
}
private void CreatePencilPreviewStroke()
{
var strokePreviewInkPoints = new List<InkPoint>();
for (var i = 0; i < PreviewStrokeCoordinates.Length; i += 2)
{
var newPoint = new Point(PreviewStrokeCoordinates[i],
PreviewStrokeCoordinates[i + 1] + 10);
var inkPoint = new InkPoint(newPoint, 1f);
strokePreviewInkPoints.Add(inkPoint);
}
var inkStrokeBuilder = new InkStrokeBuilder();
var newStroke = inkStrokeBuilder.CreateStrokeFromInkPoints(
strokePreviewInkPoints, Matrix3x2.Identity);
PencilPreviewInkStroke = newStroke;
}
private void UpdatePencilPreviewInkStroke()
{
var currentStrokeSize = PencilStrokeWidthSlider.Value;
var inkDrawingAttributes = InkDrawingAttributes.CreateForPencil();
inkDrawingAttributes.Color = StrokePreviewColor;
inkDrawingAttributes.Size = new Size(currentStrokeSize, currentStrokeSize);
if (PencilPreviewInkStroke == null)
{
CreatePencilPreviewStroke();
}
PencilPreviewInkStroke.DrawingAttributes = inkDrawingAttributes;
var strokeContainer = PencilStrokePreviewInkCanvas.InkPresenter.StrokeContainer;
foreach (var stroke in strokeContainer.GetStrokes())
{
stroke.Selected = true;
}
strokeContainer.AddStroke(PencilPreviewInkStroke.Clone());
strokeContainer.DeleteSelected();
}
private void PencilStrokeWidthSlider_PointerReleased(object sender, RoutedEventArgs e) =>
FlyoutBase.GetAttachedFlyout(PencilButton)?.Hide();
private void PencilStrokeWidthSlider_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter || e.Key == VirtualKey.Space)
{
FlyoutBase.GetAttachedFlyout(PencilButton)?.Hide();
}
}
private void EraserButton_Checked(object sender, RoutedEventArgs e) =>
VisualStateManager.GoToState(EraserButton, "ShowExtensionGlyph", false);
private void EraserButton_Unchecked(object sender, RoutedEventArgs e) =>
VisualStateManager.GoToState(EraserButton, "HideExtensionGlyph", false);
private void OnOpenEraserButtonFlyout()
{
FlyoutBase.ShowAttachedFlyout(EraserButton);
VisualStateManager.GoToState(EraserButton, "HideExtensionGlyph", false);
var flyout = FlyoutBase.GetAttachedFlyout(EraserButton);
flyout.Closed += EraserButtonFlyoutClosed;
}
private void EraserButtonFlyoutClosed(object sender, object e)
{
VisualStateManager.GoToState(EraserButton, "ShowExtensionGlyph", false);
if (sender is FlyoutBase flyoutBase)
{
flyoutBase.Closed -= EraserButtonFlyoutClosed;
}
if (CurrentRadialSelection == ColoringBookRadialController.EraserTool)
{
CurrentRadialSelection = ColoringBookRadialController.InkingTool;
EraserButton.Focus(FocusState.Keyboard);
}
}
private void EraserFlyoutList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DrawingToolChanged?.Invoke(this,
new DrawingToolChangedEventArgs { NewDrawingTool = DrawingTool });
if (StrokeEraseListItem.FocusState != FocusState.Keyboard &&
CellEraseListItem.FocusState != FocusState.Keyboard)
{
var flyout = FlyoutBase.GetAttachedFlyout(EraserButton);
flyout?.Hide();
}
}
private void EraserFlyoutList_KeyUp(object sender, KeyRoutedEventArgs e)
{
// Hide flyout when enter pressed on an eraser list item.
if (e.OriginalKey == VirtualKey.Enter || e.OriginalKey == VirtualKey.Space)
{
FlyoutBase.GetAttachedFlyout(EraserButton)?.Hide();
}
}
private static double[] PreviewStrokeCoordinates => Constants.PreviewStrokeCoordinates;
private static Color StrokePreviewColor => Colors.Gray;
private InkStroke PenPreviewInkStroke { get; set; }
private InkStroke PencilPreviewInkStroke { get; set; }
private PointerEventHandler PenWidthChangedHandler { get; set; }
private PointerEventHandler PencilWidthChangedHandler { get; set; }
private RadialController RadialController { get; set; }
private ColoringBookRadialController CurrentRadialSelection { get; set; } =
ColoringBookRadialController.None;
private int SelectedToolIndex { get; set; } = 1;
private AccessibilitySettings AccessibilitySettings { get; set; }
= new AccessibilitySettings();
private DisplayInformation DisplayInformation { get; set; }
private int CurrentUiScaling { get; set; }
private ContrastHandler ContrastHandlerAttached { get; set; } = ContrastHandler.None;
private double InkScaling { get; set; } = 1.0;
public DrawingTool DrawingTool
{
get => _drawingTool;
private set
{
if (_drawingTool != value)
{
_drawingTool = value;
OnDrawingToolChanged(value);
}
}
}
private DrawingTool _drawingTool = DrawingTool.Pen;
public void OnFillCellButtonClicked()
{
DrawingTool = DrawingTool.FillCell;
if (TargetInkCanvas != null)
{
TargetInkCanvas.InkPresenter.InputProcessingConfiguration.Mode =
InkInputProcessingMode.None;
}
}
public void OnPenButtonClicked()
{
if (DrawingTool == DrawingTool.Pen)
{
OnOpenPenButtonFlyout();
}
else
{
DrawingTool = DrawingTool.Pen;
if (TargetInkCanvas != null)
{
var newAttributes = new InkDrawingAttributes();
var oldAttributes = TargetInkCanvas.InkPresenter.CopyDefaultDrawingAttributes();
newAttributes.Color = oldAttributes.Color;
var strokeWidth = PenWidth * InkScaling;
newAttributes.Size = new Size(strokeWidth, strokeWidth);
TargetInkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(newAttributes);
TargetInkCanvas.InkPresenter.InputProcessingConfiguration.Mode =
InkInputProcessingMode.Inking;
}
}
}
public void OnPencilButtonClicked()
{
if (DrawingTool == DrawingTool.Pencil)
{
OnOpenPencilButtonFlyout();
}
else
{
DrawingTool = DrawingTool.Pencil;
if (TargetInkCanvas != null)
{
var newAttributes = InkDrawingAttributes.CreateForPencil();
var oldAttributes = TargetInkCanvas.InkPresenter.CopyDefaultDrawingAttributes();
newAttributes.Color = oldAttributes.Color;
var strokeWidth = PencilWidth * InkScaling;
newAttributes.Size = new Size(strokeWidth, strokeWidth);
TargetInkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(newAttributes);
TargetInkCanvas.InkPresenter.InputProcessingConfiguration.Mode =
InkInputProcessingMode.Inking;
}
}
}
public void OnEraserButtonClicked()
{
if ((DrawingTool == DrawingTool.Eraser) ||
(DrawingTool == DrawingTool.EraseCell))
{
OnOpenEraserButtonFlyout();
}
else
{
DrawingTool = IsStrokeEraseChecked ? DrawingTool.Eraser : DrawingTool.EraseCell;
if (TargetInkCanvas != null)
{
TargetInkCanvas.InkPresenter.InputProcessingConfiguration.Mode =
InkInputProcessingMode.None;
}
}
}
public int PenWidth
{
get => _penWidth;
set
{
if (_penWidth != value)
{
_penWidth = value;
UpdateDrawingWidth(value);
}
}
}
private int _penWidth = Constants.DefaultPenStrokeWidth;
public int PencilWidth
{
get => _pencilWidth;
set
{
if (_pencilWidth != value)
{
_pencilWidth = value;
UpdateDrawingWidth(value);
}
}
}
private int _pencilWidth = Constants.DefaultPencilStrokeWidth;
private void UpdateDrawingWidth(int width)
{
var currentDrawingAttributes = TargetInkCanvas.InkPresenter.CopyDefaultDrawingAttributes();
var newWidth = width * InkScaling;
currentDrawingAttributes.Size = new Size(newWidth, newWidth);
TargetInkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(currentDrawingAttributes);
}
public bool IsStrokeEraseChecked
{
get => _isStrokeEraseChecked;
set
{
_isStrokeEraseChecked = value;
if (value)
{
DrawingTool = DrawingTool.Eraser;
}
}
}
private bool _isStrokeEraseChecked = true;
public bool IsEraseCellChecked
{
get => _isEraseCellChecked;
set
{
_isEraseCellChecked = value;
if (value)
{
DrawingTool = DrawingTool.EraseCell;
}
}
}
private bool _isEraseCellChecked;
public void SetInkScaling(double inkScaling)
{
InkScaling = inkScaling;
// Set initial drawing attributes.
var newAttributes = new InkDrawingAttributes();
var oldAttributes = TargetInkCanvas.InkPresenter.CopyDefaultDrawingAttributes();
newAttributes.Color = oldAttributes.Color;
var strokeWidth = PenWidth * InkScaling;
newAttributes.Size = new Size(strokeWidth, strokeWidth);
TargetInkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(newAttributes);
TargetInkCanvas.InkPresenter.InputProcessingConfiguration.Mode =
InkInputProcessingMode.Inking;
}
private enum ContrastHandler
{
None,
Black,
White
}
private enum ColoringBookRadialController
{
None,
InkingTool,
Color,
StrokeSize,
UndoRedo,
EraserTool,
StrokeSizeDialClick
}
}
}