forked from LykosAI/StabilityMatrix
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAdvancedImageBox.axaml.cs
More file actions
2722 lines (2341 loc) · 90.7 KB
/
AdvancedImageBox.axaml.cs
File metadata and controls
2722 lines (2341 loc) · 90.7 KB
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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.
*/
// Port from: https://github.com/cyotek/Cyotek.Windows.Forms.ImageBox to AvaloniaUI
// Modified from: https://github.com/sn4k3/UVtools/blob/master/UVtools.AvaloniaControls/AdvancedImageBox.axaml.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.IO;
using System.Runtime.CompilerServices;
using AsyncAwaitBestPractices;
using AsyncImageLoader;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Threading;
using Bitmap = Avalonia.Media.Imaging.Bitmap;
using Brushes = Avalonia.Media.Brushes;
using Color = Avalonia.Media.Color;
using Pen = Avalonia.Media.Pen;
using Point = Avalonia.Point;
using Size = Avalonia.Size;
namespace StabilityMatrix.Avalonia.Controls;
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public class AdvancedImageBox : TemplatedControlBase
{
#region Bindable Base
/// <summary>
/// Multicast event for property change notifications.
/// </summary>
private PropertyChangedEventHandler? _propertyChanged;
public new event PropertyChangedEventHandler PropertyChanged
{
add => _propertyChanged += value;
remove => _propertyChanged -= value;
}
protected bool RaiseAndSetIfChanged<T>(
ref T field,
T value,
[CallerMemberName] string? propertyName = null
)
{
if (EqualityComparer<T>.Default.Equals(field, value))
return false;
field = value;
RaisePropertyChanged(propertyName);
return true;
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) { }
/// <summary>
/// Notifies listeners that a property value has changed.
/// </summary>
/// <param name="propertyName">
/// Name of the property used to notify listeners. This
/// value is optional and can be provided automatically when invoked from compilers
/// that support <see cref="CallerMemberNameAttribute" />.
/// </param>
protected void RaisePropertyChanged([CallerMemberName] string? propertyName = null)
{
var e = new PropertyChangedEventArgs(propertyName);
OnPropertyChanged(e);
_propertyChanged?.Invoke(this, e);
}
#endregion
#region Sub Classes
/// <summary>
/// Represents available levels of zoom in an <see cref="AdvancedImageBox"/> control
/// </summary>
public class ZoomLevelCollection : IList<int>
{
#region Public Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ZoomLevelCollection"/> class.
/// </summary>
public ZoomLevelCollection()
{
List = new SortedList<int, int>();
}
/// <summary>
/// Initializes a new instance of the <see cref="ZoomLevelCollection"/> class.
/// </summary>
/// <param name="collection">The default values to populate the collection with.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the <c>collection</c> parameter is null</exception>
public ZoomLevelCollection(IEnumerable<int> collection)
: this()
{
if (collection == null)
{
throw new ArgumentNullException(nameof(collection));
}
AddRange(collection);
}
#endregion
#region Public Class Properties
/// <summary>
/// Returns the default zoom levels
/// </summary>
public static ZoomLevelCollection Default =>
new(
new[]
{
7,
10,
13,
16,
20,
24,
// 10 increments
30,
40,
50,
60,
70,
// 100 increments
100,
200,
300,
400,
500,
600,
700,
800,
// 400 increments
1200,
1600,
2000,
2400,
// 800 increments
3200,
4000,
4800,
// 1000 increments
5800,
6800,
7800,
8800,
}
);
#endregion
#region Public Properties
/// <summary>
/// Gets the number of elements contained in the <see cref="ZoomLevelCollection" />.
/// </summary>
/// <returns>
/// The number of elements contained in the <see cref="ZoomLevelCollection" />.
/// </returns>
public int Count => List.Count;
/// <summary>
/// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1" /> is read-only.
/// </summary>
/// <value><c>true</c> if this instance is read only; otherwise, <c>false</c>.</value>
/// <returns>true if the <see cref="T:System.Collections.Generic.ICollection`1" /> is read-only; otherwise, false.
/// </returns>
public bool IsReadOnly => false;
/// <summary>
/// Gets or sets the zoom level at the specified index.
/// </summary>
/// <param name="index">The index.</param>
public int this[int index]
{
get => List.Values[index];
set
{
List.RemoveAt(index);
Add(value);
}
}
#endregion
#region Protected Properties
/// <summary>
/// Gets or sets the backing list.
/// </summary>
protected SortedList<int, int> List { get; set; }
#endregion
#region Public Members
/// <summary>
/// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1" />.
/// </summary>
/// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1" />.</param>
public void Add(int item)
{
List.Add(item, item);
}
/// <summary>
/// Adds a range of items to the <see cref="ZoomLevelCollection"/>.
/// </summary>
/// <param name="collection">The items to add to the collection.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the <c>collection</c> parameter is null.</exception>
public void AddRange(IEnumerable<int> collection)
{
if (collection == null)
{
throw new ArgumentNullException(nameof(collection));
}
foreach (var value in collection)
{
Add(value);
}
}
/// <summary>
/// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1" />.
/// </summary>
public void Clear()
{
List.Clear();
}
/// <summary>
/// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1" /> contains a specific value.
/// </summary>
/// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1" />.</param>
/// <returns>true if <paramref name="item" /> is found in the <see cref="T:System.Collections.Generic.ICollection`1" />; otherwise, false.</returns>
public bool Contains(int item)
{
return List.ContainsKey(item);
}
/// <summary>
/// Copies a range of elements this collection into a destination <see cref="Array"/>.
/// </summary>
/// <param name="array">The <see cref="Array"/> that receives the data.</param>
/// <param name="arrayIndex">A 64-bit integer that represents the index in the <see cref="Array"/> at which storing begins.</param>
public void CopyTo(int[] array, int arrayIndex)
{
for (var i = 0; i < Count; i++)
{
array[arrayIndex + i] = List.Values[i];
}
}
/// <summary>
/// Finds the index of a zoom level matching or nearest to the specified value.
/// </summary>
/// <param name="zoomLevel">The zoom level.</param>
public int FindNearest(int zoomLevel)
{
var nearestValue = List.Values[0];
var nearestDifference = Math.Abs(nearestValue - zoomLevel);
for (var i = 1; i < Count; i++)
{
var value = List.Values[i];
var difference = Math.Abs(value - zoomLevel);
if (difference < nearestDifference)
{
nearestValue = value;
nearestDifference = difference;
}
}
return nearestValue;
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>A <see cref="T:System.Collections.Generic.IEnumerator`1" /> that can be used to iterate through the collection.</returns>
public IEnumerator<int> GetEnumerator()
{
return List.Values.GetEnumerator();
}
/// <summary>
/// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1" />.
/// </summary>
/// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1" />.</param>
/// <returns>The index of <paramref name="item" /> if found in the list; otherwise, -1.</returns>
public int IndexOf(int item)
{
return List.IndexOfKey(item);
}
/// <summary>
/// Not implemented.
/// </summary>
/// <param name="index">The index.</param>
/// <param name="item">The item.</param>
/// <exception cref="System.NotImplementedException">Not implemented</exception>
public void Insert(int index, int item)
{
throw new NotImplementedException();
}
/// <summary>
/// Returns the next increased zoom level for the given current zoom.
/// </summary>
/// <param name="zoomLevel">The current zoom level.</param>
/// <param name="constrainZoomLevel">When positive, constrain maximum zoom to this value</param>
/// <returns>The next matching increased zoom level for the given current zoom if applicable, otherwise the nearest zoom.</returns>
public int NextZoom(int zoomLevel, int constrainZoomLevel = 0)
{
var index = IndexOf(FindNearest(zoomLevel));
if (index < Count - 1)
index++;
return constrainZoomLevel > 0 && this[index] >= constrainZoomLevel
? constrainZoomLevel
: this[index];
}
/// <summary>
/// Returns the next decreased zoom level for the given current zoom.
/// </summary>
/// <param name="zoomLevel">The current zoom level.</param>
/// <param name="constrainZoomLevel">When positive, constrain minimum zoom to this value</param>
/// <returns>The next matching decreased zoom level for the given current zoom if applicable, otherwise the nearest zoom.</returns>
public int PreviousZoom(int zoomLevel, int constrainZoomLevel = 0)
{
var index = IndexOf(FindNearest(zoomLevel));
if (index > 0)
index--;
return constrainZoomLevel > 0 && this[index] <= constrainZoomLevel
? constrainZoomLevel
: this[index];
}
/// <summary>
/// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1" />.
/// </summary>
/// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1" />.</param>
/// <returns>true if <paramref name="item" /> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1" />; otherwise, false. This method also returns false if <paramref name="item" /> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1" />.</returns>
public bool Remove(int item)
{
return List.Remove(item);
}
/// <summary>
/// Removes the element at the specified index of the <see cref="ZoomLevelCollection"/>.
/// </summary>
/// <param name="index">The zero-based index of the element to remove.</param>
public void RemoveAt(int index)
{
List.RemoveAt(index);
}
/// <summary>
/// Copies the elements of the <see cref="ZoomLevelCollection"/> to a new array.
/// </summary>
/// <returns>An array containing copies of the elements of the <see cref="ZoomLevelCollection"/>.</returns>
public int[] ToArray()
{
var results = new int[Count];
CopyTo(results, 0);
return results;
}
#endregion
#region IList<int> Members
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>An <see cref="ZoomLevelCollection" /> object that can be used to iterate through the collection.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
#endregion
#region Enums
/// <summary>
/// Determines the sizing mode of an image hosted in an <see cref="AdvancedImageBox" /> control.
/// </summary>
public enum SizeModes : byte
{
/// <summary>
/// The image is displayed according to current zoom and scroll properties.
/// </summary>
Normal,
/// <summary>
/// The image is stretched to fill the client area of the control.
/// </summary>
Stretch,
/// <summary>
/// The image is stretched to fill as much of the client area of the control as possible, whilst retaining the same aspect ratio for the width and height.
/// </summary>
Fit,
}
[Flags]
public enum MouseButtons : byte
{
None = 0,
LeftButton = 1,
MiddleButton = 2,
RightButton = 4,
}
/// <summary>
/// Describes the zoom action occurring
/// </summary>
[Flags]
public enum ZoomActions : byte
{
/// <summary>
/// No action.
/// </summary>
None = 0,
/// <summary>
/// The control is increasing the zoom.
/// </summary>
ZoomIn = 1,
/// <summary>
/// The control is decreasing the zoom.
/// </summary>
ZoomOut = 2,
/// <summary>
/// The control zoom was reset.
/// </summary>
ActualSize = 4,
}
public enum SelectionModes
{
/// <summary>
/// No selection.
/// </summary>
None,
/// <summary>
/// Rectangle selection.
/// </summary>
Rectangle,
/// <summary>
/// Zoom selection.
/// </summary>
Zoom,
}
#endregion
#region UI Controls
[NotNull]
public ScrollBar? HorizontalScrollBar { get; private set; }
[NotNull]
public ScrollBar? VerticalScrollBar { get; private set; }
[NotNull]
public ContentPresenter? ViewPort { get; private set; }
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
public bool IsViewPortLoaded => ViewPort is not null;
public Vector Offset
{
get => new(HorizontalScrollBar.Value, VerticalScrollBar.Value);
set
{
HorizontalScrollBar.Value = value.X;
VerticalScrollBar.Value = value.Y;
RaisePropertyChanged();
TriggerRender();
}
}
public Size ViewPortSize => ViewPort?.Bounds.Size ?? new Size(50, 50);
#endregion
#region Private Members
private Point _startMousePosition;
private Vector _startScrollPosition;
private bool _isPanning;
private bool _isSelecting;
private Bitmap? _trackerImage;
private bool _canRender = true;
private Point _pointerPosition;
#endregion
#region Properties
public static readonly DirectProperty<AdvancedImageBox, bool> CanRenderProperty =
AvaloniaProperty.RegisterDirect<AdvancedImageBox, bool>(nameof(CanRender), o => o.CanRender);
/// <summary>
/// Gets or sets if control can render the image
/// </summary>
public bool CanRender
{
get => _canRender;
set
{
if (!SetAndRaise(CanRenderProperty, ref _canRender, value))
return;
if (_canRender)
TriggerRender();
}
}
public static readonly StyledProperty<byte> GridCellSizeProperty = AvaloniaProperty.Register<
AdvancedImageBox,
byte
>(nameof(GridCellSize), 15);
/// <summary>
/// Gets or sets the grid cell size
/// </summary>
public byte GridCellSize
{
get => GetValue(GridCellSizeProperty);
set => SetValue(GridCellSizeProperty, value);
}
public static readonly StyledProperty<ISolidColorBrush> GridColorProperty = AvaloniaProperty.Register<
AdvancedImageBox,
ISolidColorBrush
>(nameof(GridColor), SolidColorBrush.Parse("#181818"));
/// <summary>
/// Gets or sets the color used to create the checkerboard style background
/// </summary>
public ISolidColorBrush GridColor
{
get => GetValue(GridColorProperty);
set => SetValue(GridColorProperty, value);
}
public static readonly StyledProperty<ISolidColorBrush> GridColorAlternateProperty =
AvaloniaProperty.Register<AdvancedImageBox, ISolidColorBrush>(
nameof(GridColorAlternate),
SolidColorBrush.Parse("#252525")
);
/// <summary>
/// Gets or sets the color used to create the checkerboard style background
/// </summary>
public ISolidColorBrush GridColorAlternate
{
get => GetValue(GridColorAlternateProperty);
set => SetValue(GridColorAlternateProperty, value);
}
public static readonly StyledProperty<string?> SourceProperty = AvaloniaProperty.Register<
AdvancedImageBox,
string?
>("Source");
public string? Source
{
get => GetValue(SourceProperty);
set
{
SetValue(SourceProperty, value);
// Also set the image
if (value is not null)
{
var loader = ImageLoader.AsyncImageLoader;
Dispatcher
.UIThread.InvokeAsync(async () =>
{
Image = await loader.ProvideImageAsync(value);
})
.SafeFireAndForget();
}
}
}
public static readonly StyledProperty<Bitmap?> ImageProperty = AvaloniaProperty.Register<
AdvancedImageBox,
Bitmap?
>(nameof(Image));
/// <summary>
/// Gets or sets the image to be displayed
/// </summary>
public Bitmap? Image
{
get => GetValue(ImageProperty);
set => SetValue(ImageProperty, value);
}
/// <inheritdoc />
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == ImageProperty)
{
var (oldImage, newImage) = change.GetOldAndNewValue<Bitmap?>();
Vector? offsetBackup = null;
if (newImage is null)
{
SelectNone();
}
else if (IsViewPortLoaded)
{
if (oldImage is null)
{
ZoomToFit();
RestoreSizeMode();
}
else if (newImage.Size != oldImage.Size)
{
offsetBackup = Offset;
var zoomFactorScale = (float)GetZoomLevelToFit(newImage) / GetZoomLevelToFit(oldImage);
var imageScale = newImage.Size / oldImage.Size;
Debug.WriteLine($"Image scale: {imageScale}");
/*var oldScaledSize = oldImage.Size * ZoomFactor;
var newScaledSize = newImage.Size * ZoomFactor;
Debug.WriteLine($"Old scaled {oldScaledSize} -> new scaled {newScaledSize}");*/
var currentZoom = Zoom;
var currentFactor = ZoomFactor;
// var currentOffset = Offset;
// Scale zoom and offset to new size
Zoom = (int)Math.Floor(Zoom * zoomFactorScale);
/*Offset = new Vector(
Offset.X * imageScale.X,
Offset.Y * imageScale.Y
);*/
Debug.WriteLine($"Zoom changed from {currentZoom} to {Zoom}");
Debug.WriteLine($"Zoom factor changed from {currentFactor} to {ZoomFactor}");
}
if (offsetBackup is not null)
{
Offset = offsetBackup.Value;
}
UpdateViewPort();
TriggerRender();
}
RaisePropertyChanged(nameof(IsImageLoaded));
}
}
/*public WriteableBitmap? ImageAsWriteableBitmap
{
get
{
if (Image is null)
return null;
return (WriteableBitmap)Image;
}
}*/
public bool IsImageLoaded => Image is not null;
public static readonly DirectProperty<AdvancedImageBox, Bitmap?> TrackerImageProperty =
AvaloniaProperty.RegisterDirect<AdvancedImageBox, Bitmap?>(
nameof(TrackerImage),
o => o.TrackerImage,
(o, v) => o.TrackerImage = v
);
/// <summary>
/// Gets or sets an image to follow the mouse pointer
/// </summary>
public Bitmap? TrackerImage
{
get => _trackerImage;
set
{
if (!SetAndRaise(TrackerImageProperty, ref _trackerImage, value))
return;
TriggerRender();
RaisePropertyChanged(nameof(HaveTrackerImage));
}
}
public bool HaveTrackerImage => _trackerImage is not null;
public static readonly StyledProperty<bool> TrackerImageAutoZoomProperty = AvaloniaProperty.Register<
AdvancedImageBox,
bool
>(nameof(TrackerImageAutoZoom), true);
/// <summary>
/// Gets or sets if the tracker image will be scaled to the current zoom
/// </summary>
public bool TrackerImageAutoZoom
{
get => GetValue(TrackerImageAutoZoomProperty);
set => SetValue(TrackerImageAutoZoomProperty, value);
}
public static readonly StyledProperty<bool> IsTrackerImageEnabledProperty = AvaloniaProperty.Register<
AdvancedImageBox,
bool
>("IsTrackerImageEnabled");
public bool IsTrackerImageEnabled
{
get => GetValue(IsTrackerImageEnabledProperty);
set => SetValue(IsTrackerImageEnabledProperty, value);
}
public bool IsHorizontalBarVisible
{
get
{
if (Image is null)
return false;
if (SizeMode != SizeModes.Normal)
return false;
return ScaledImageWidth > ViewPortSize.Width;
}
}
public bool IsVerticalBarVisible
{
get
{
if (Image is null)
return false;
if (SizeMode != SizeModes.Normal)
return false;
return ScaledImageHeight > ViewPortSize.Height;
}
}
public static readonly StyledProperty<bool> ShowGridProperty = AvaloniaProperty.Register<
AdvancedImageBox,
bool
>(nameof(ShowGrid), true);
/// <summary>
/// Gets or sets the grid visibility when reach high zoom levels
/// </summary>
public bool ShowGrid
{
get => GetValue(ShowGridProperty);
set => SetValue(ShowGridProperty, value);
}
public static readonly DirectProperty<AdvancedImageBox, Point> PointerPositionProperty =
AvaloniaProperty.RegisterDirect<AdvancedImageBox, Point>(
nameof(PointerPosition),
o => o.PointerPosition
);
/// <summary>
/// Gets the current pointer position
/// </summary>
public Point PointerPosition
{
get => _pointerPosition;
private set => SetAndRaise(PointerPositionProperty, ref _pointerPosition, value);
}
public static readonly DirectProperty<AdvancedImageBox, bool> IsPanningProperty =
AvaloniaProperty.RegisterDirect<AdvancedImageBox, bool>(nameof(IsPanning), o => o.IsPanning);
/// <summary>
/// Gets if control is currently panning
/// </summary>
public bool IsPanning
{
get => _isPanning;
protected set
{
if (!SetAndRaise(IsPanningProperty, ref _isPanning, value))
return;
_startScrollPosition = Offset;
if (value)
{
Cursor = new Cursor(StandardCursorType.SizeAll);
//this.OnPanStart(EventArgs.Empty);
}
else
{
Cursor = Cursor.Default;
//this.OnPanEnd(EventArgs.Empty);
}
}
}
public static readonly DirectProperty<AdvancedImageBox, bool> IsSelectingProperty =
AvaloniaProperty.RegisterDirect<AdvancedImageBox, bool>(nameof(IsSelecting), o => o.IsSelecting);
/// <summary>
/// Gets if control is currently selecting a ROI
/// </summary>
public bool IsSelecting
{
get => _isSelecting;
protected set => SetAndRaise(IsSelectingProperty, ref _isSelecting, value);
}
/// <summary>
/// Gets the center point of the viewport
/// </summary>
public Point CenterPoint
{
get
{
var viewport = GetImageViewPort();
return new(viewport.Width / 2, viewport.Height / 2);
}
}
public static readonly StyledProperty<bool> AutoPanProperty = AvaloniaProperty.Register<
AdvancedImageBox,
bool
>(nameof(AutoPan), true);
/// <summary>
/// Gets or sets if the control can pan with the mouse
/// </summary>
public bool AutoPan
{
get => GetValue(AutoPanProperty);
set => SetValue(AutoPanProperty, value);
}
public static readonly StyledProperty<MouseButtons> PanWithMouseButtonsProperty =
AvaloniaProperty.Register<AdvancedImageBox, MouseButtons>(
nameof(PanWithMouseButtons),
MouseButtons.LeftButton | MouseButtons.MiddleButton
);
/// <summary>
/// Gets or sets the mouse buttons to pan the image
/// </summary>
public MouseButtons PanWithMouseButtons
{
get => GetValue(PanWithMouseButtonsProperty);
set => SetValue(PanWithMouseButtonsProperty, value);
}
public static readonly StyledProperty<bool> PanWithArrowsProperty = AvaloniaProperty.Register<
AdvancedImageBox,
bool
>(nameof(PanWithArrows), true);
/// <summary>
/// Gets or sets if the control can pan with the keyboard arrows
/// </summary>
public bool PanWithArrows
{
get => GetValue(PanWithArrowsProperty);
set => SetValue(PanWithArrowsProperty, value);
}
public static readonly StyledProperty<MouseButtons> SelectWithMouseButtonsProperty =
AvaloniaProperty.Register<AdvancedImageBox, MouseButtons>(
nameof(SelectWithMouseButtons),
MouseButtons.LeftButton
);
/// <summary>
/// Gets or sets the mouse buttons to select a region on image
/// </summary>
public MouseButtons SelectWithMouseButtons
{
get => GetValue(SelectWithMouseButtonsProperty);
set => SetValue(SelectWithMouseButtonsProperty, value);
}
public static readonly StyledProperty<bool> InvertMousePanProperty = AvaloniaProperty.Register<
AdvancedImageBox,
bool
>(nameof(InvertMousePan));
/// <summary>
/// Gets or sets if mouse pan is inverted
/// </summary>
public bool InvertMousePan
{
get => GetValue(InvertMousePanProperty);
set => SetValue(InvertMousePanProperty, value);
}
public static readonly StyledProperty<bool> AutoCenterProperty = AvaloniaProperty.Register<
AdvancedImageBox,
bool
>(nameof(AutoCenter), true);
/// <summary>
/// Gets or sets if image is auto centered
/// </summary>
public bool AutoCenter
{
get => GetValue(AutoCenterProperty);
set => SetValue(AutoCenterProperty, value);
}
public static readonly StyledProperty<SizeModes> SizeModeProperty = AvaloniaProperty.Register<
AdvancedImageBox,
SizeModes
>(nameof(SizeMode));
/// <summary>
/// Gets or sets the image size mode
/// </summary>
public SizeModes SizeMode
{
get => GetValue(SizeModeProperty);
set
{
SetValue(SizeModeProperty, value);
// Run changed if loaded
if (IsViewPortLoaded)
{
SizeModeChanged();
}
RaisePropertyChanged(nameof(IsHorizontalBarVisible));
RaisePropertyChanged(nameof(IsVerticalBarVisible));
}
}
private void SizeModeChanged()
{
switch (SizeMode)
{
case SizeModes.Normal:
HorizontalScrollBar.Visibility = ScrollBarVisibility.Auto;
VerticalScrollBar.Visibility = ScrollBarVisibility.Auto;
break;
case SizeModes.Stretch:
case SizeModes.Fit:
HorizontalScrollBar.Visibility = ScrollBarVisibility.Hidden;
VerticalScrollBar.Visibility = ScrollBarVisibility.Hidden;
break;
default:
throw new ArgumentOutOfRangeException(nameof(SizeMode), SizeMode, null);
}
}
public static readonly StyledProperty<bool> AllowZoomProperty = AvaloniaProperty.Register<
AdvancedImageBox,
bool
>(nameof(AllowZoom), true);
/// <summary>
/// Gets or sets if zoom is allowed
/// </summary>
public bool AllowZoom
{
get => GetValue(AllowZoomProperty);
set => SetValue(AllowZoomProperty, value);