-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathdisplay_object.dart
1852 lines (1683 loc) · 60.8 KB
/
display_object.dart
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
import 'dart:ui' as ui;
import 'package:flutter/painting.dart' as painting;
import '../../graphx.dart';
/// An abstract class that represents a display object in GraphX.
///
/// This class is the base for all visual objects in GraphX, and provides
/// functionality for positioning, scaling, rotating, and transforming
/// display objects. It also provides mouse and touch event handling,
/// masking, and filtering capabilities.
///
/// Use [GDisplayObject] to create a display object that can be added to a
/// [GDisplayObjectContainer] (like GSprite).
///
/// To move or scale the display object, you can use the properties
/// [x], [y], [scaleX], [scaleY], [pivotX], and [pivotY].
/// Rotation can be set using the [rotation] property,
/// and skew using [skewX] and [skewY].
///
/// Display objects can also be made draggable using the [startDrag] method.
/// This will attach an event listener to the stage to
/// detect mouse movement and update the object's position accordingly.
///
/// By default, a display object can receive mouse input events such as
/// [onMouseDown], [onMouseUp], and [onMouseClick].
/// Use the [mouseEnabled] property to disable mouse input events for a specific
/// display object.
///
/// Display objects can also be assigned a [mask] to create a clipping region.
/// The mask must be a [GShape] object.
/// By default, the mask is not inverted.
/// Set the [maskInverted] property to true to invert the [mask].
///
abstract class GDisplayObject
with
DisplayListSignalsMixin,
RenderSignalMixin,
MouseSignalsMixin,
DisplayMasking {
/// TODO: add caching to local bounds (Rect).
// Rect _nativeBounds;
// GxRect _cachedBounds;
/// (Internal usage)
/// The current display object that is being dragged.
static GDisplayObject? $currentDrag;
/// (Internal usage)
/// The bounds for the current drag.
static GRect? $currentDragBounds;
static bool _isWarned3d = false;
/// common parent.
/// A list that contains the ancestors of this object, starting with the
/// immediate parent and going up the display list hierarchy.
static final List<GDisplayObject> _sAncestors = [];
/// A helper point used for various calculations.
static final GPoint _sHelperPoint = GPoint();
// DisplayObject get dropTarget {
// if ($parent == null || !$hasVisibleArea || !inStage) return null;
// if ($parent.children.length > 1) {
// GxRect rect;
// $parent.children.forEach((child) {
// child.getBounds($parent, rect);
// });
// }
// }
/// A helper rectangle used for various calculations.
static final GRect _sHelperRect = GRect();
/// A helper matrix used for various calculations.
static final GMatrix _sHelperMatrix = GMatrix();
/// An alternate helper matrix used for various calculations.
static final GMatrix _sHelperMatrixAlt = GMatrix();
/// Debug paint used to draw the bounds of a DisplayObject.
static final ui.Paint _debugPaint = ui.Paint()
..style = ui.PaintingStyle.stroke
..color = kColorMagenta
..strokeWidth = 1;
/// (Internal usage)
/// The offset for the center of the drag object.
late GPoint _dragCenterOffset;
/// (Internal usage)
/// The [parent] display object container that this object is a child of.
GDisplayObjectContainer? $parent;
/// (Internal usage)
/// Whether to show debug bounds for this object.
bool $debugBounds = false;
/// Whether to use the shape of the object for mouse event detection.
bool mouseUseShape = false;
/// (Internal usage)
/// The filters applied to this object.
List<GBaseFilter>? $filters;
/// (Internal usage)
/// The object that the mouse was last pressed on.
GDisplayObject? $mouseDownObj;
/// (Internal usage)
/// The object that the mouse is currently over.
GDisplayObject? $mouseOverObj;
/// (Internal usage)
/// The time of the last mouse click.
double $lastClickTime = -1;
/// Whether to use a custom cursor for this object.
bool useCursor = false;
/// (Internal usage)
/// The color to apply to this object.
ui.Color? $colorize = kColorTransparent;
/// You can store any user defined data in this property for easy access.
Object? userData;
/// Name to reference this object.
String? name;
/// The x-coordinate of the object.
double _x = 0;
/// The y-coordinate of the object.
double _y = 0;
/// The x-scale factor of the object.
double _scaleX = 1;
/// The y-scale factor of the object.
double _scaleY = 1;
/// The rotation of the object in radians.
double _rotation = 0;
double _pivotX = 0, _pivotY = 0;
double _skewX = 0, _skewY = 0;
double _z = 0, _rotationX = 0, _rotationY = 0;
/// (Internal usage)
/// The alpha (transparency) value of this object,
/// a value between 0.0 (fully transparent) and 1.0 (fully opaque).
double $alpha = 1;
/// (Internal usage)
/// Indicates whether the matrix of this object needs to be recalculated.
bool $matrixDirty = true;
/// Indicates whether this object can receive mouse and touch input events.
bool mouseEnabled = true;
/// (Internal usage)
/// The display object that acts as a mask of this object.
GDisplayObject? $maskee;
/// (Internal usage)
/// The mask of this object, used for masking.
GShape? $mask;
/// (Internal usage)
/// Optimization flag indicating whether this object has a touchable area.
bool $hasTouchableArea = true;
/// (Internal usage)
/// Optimization flag indicating whether this object has a [visible] area.
bool $hasVisibleArea = true;
/// Whether the object should apply an inverted mask.
/// can be set on the Shape mask, or the maskee DisplayObject.
bool maskInverted = false;
/// Flag that indicates if the transformation matrix has changed.
bool _transformationChanged = false;
/// Flag that indicates if the object is in 3D space.
bool _is3D = false;
/// Toggles the visibility state of the object.
/// Whether or not the display object is visible.
/// Display objects that are not visible are disabled.
/// For example, if visible=false it cannot be clicked and it will not be
/// rendered.
bool _visible = true;
/// The transformation matrix of the object.
GMatrix? _transformationMatrix;
/// When enabled, the saveLayer() feature is used for better rendering
/// performance. Disabling it may provide better performance for certain
/// display objects.
/// Childless DisplayObjects, like [GShape] and [GBitmap], have their own
/// Paint() so no need to use an expensive saveLayer().
bool allowSaveLayer = false;
/// (Internal usage)
/// Tracks the last layer bounds used for rendering this display object
/// using getBounds($parent).
GRect? $debugLastLayerBounds;
/// The paint to use for filters that require a paint.
ui.Paint filterPaint = ui.Paint();
/// (Internal usage)
/// When enabled, a rectangle is created to save the layer for better
/// rendering performance.
bool $useSaveLayerBounds = true;
/// (Internal usage)
/// Defines the debug paint used to show the bounds of this display object
/// when debug mode is enabled. By default, it uses a magenta stroke with
/// a width of 1. Override this property to use a custom debug paint.
ui.Paint? $debugBoundsPaint = _debugPaint.clone();
/// Initializes a new instance of the [GDisplayObject] class.
GDisplayObject() {
_x = _y = 0.0;
_rotation = 0.0;
alpha = 1.0;
_pivotX = _pivotY = 0.0;
_scaleX = _scaleY = 1.0;
_skewX = _skewY = 0.0;
_rotationX = _rotationY = 0.0;
mouseEnabled = true;
}
/// (Internal usage)
/// Whether this object has a color applied to it.
bool get $hasColorize {
return $colorize != null && $colorize!.alpha > 0;
}
/// Returns the alpha (transparency) value of this object.
double get alpha {
return $alpha;
}
/// Sets the alpha (transparency) value of this object.
/// The [value] parameter must be a value between 0.0 and 1.0.
/// If the new value is the same as the current one,
/// the method returns immediately.
/// The method also marks the object as requiring redraw.
set alpha(double value) {
if ($alpha != value) {
// value ??= 1;
$alpha = value.clamp(0.0, 1.0);
requiresRedraw();
}
}
/// Returns the base object, i.e. the topmost parent in the display hierarchy.
GDisplayObject get base {
var current = this;
while (current.$parent != null) {
current = current.$parent!;
}
return current;
}
/// TODO: should be cached.
/// Returns the bounds of this object in its local coordinate space.
///
/// This method should be overridden by subclasses to calculate the correct
/// bounds.
///
/// If [out] is non-null, the resulting bounds will be stored in that object
/// and returned, otherwise a new [GRect] object will be created and returned.
GRect? get bounds {
return getBounds(this);
}
/// Gets the color applied to this object.
ui.Color? get colorize {
return $colorize;
}
/// Sets the color to apply to this object.
set colorize(ui.Color? value) {
if ($colorize == value) return;
$colorize = value;
requiresRedraw();
}
/// Gets the filters applied to this object.
List<GBaseFilter>? get filters {
return $filters;
}
/// Sets the filters applied to this object.
set filters(List<GBaseFilter>? value) {
$filters = value;
}
/// Returns true if this object has any filters applied.
bool get hasFilters {
return filters?.isNotEmpty ?? false;
}
/// Indicates the height of the display object, in dp.
/// The `height` is calculated based on the bounds of the content of the
/// display object.
/// When you set the `height` property, the `scaleX` property is adjusted
/// accordingly, as shown in the following code:
/// ```dart
/// var rect:Shape = new Shape();
/// rect.graphics.beginFill(0xFF0000);
/// rect.graphics.drawRect(0, 0, 100, 100);
/// trace(rect.scaleY) // 1;
/// rect.height = 200;
/// trace(rect.scaleY) // 2;
/// ```
/// A display object with no content (such as an empty sprite) has a height
/// of 0, even if you try to set height to a different value.
double get height {
return getBounds($parent, _sHelperRect)!.height;
}
/// Sets the height of this display object to the specified value.
///
/// If the given value is null or NaN, an exception is thrown. If the
/// display object's scale on the Y-axis is zero or very close to zero,
/// it is reset to 1.0 in order to properly calculate the actual height.
/// The actual height is then calculated based on the current width and
/// scale of the display object. Finally, the scale on the Y-axis is set
/// so that the actual height matches the specified value.
///
/// If you need to retrieve the height of the display object, use the
/// [height] getter instead.
///
/// Throws an exception if the given value is null or NaN.
set height(double? value) {
if (value?.isNaN ?? true) throw '[$this.height] can not be NaN nor null';
double? actualH;
var zeroScale = _scaleY < 1e-8 && _scaleY > -1e-8;
if (zeroScale) {
scaleY = 1.0;
actualH = height;
} else {
actualH = (height / _scaleY).abs();
}
scaleY = value! / actualH;
}
/// Returns `true` if this object is on the stage.
bool get inStage {
return base is Stage;
}
/// Returns whether this object is currently being used as a [mask] for another
/// object.
bool get isMask {
return $maskee != null;
}
/// Returns whether this object has any [rotation] or skew transformation.
bool get isRotated {
return _rotation != 0 || _skewX != 0 || _skewY != 0;
}
/// Returns the mask of this object.
GShape? get mask {
return $mask;
}
/// The [GShape] to be used as a mask for this object.
set mask(GShape? value) {
if ($mask != value) {
if ($mask != null) $mask!.$maskee = null;
value?.$maskee = this;
value?.$hasVisibleArea = false;
$mask = value;
requiresRedraw();
}
}
/// Returns the position of the mouse in the local coordinate system of this
/// object.
/// The mouse position is updated every frame, so this method returns the
/// current mouse position relative to the top-left corner of this object.
///
/// Throws an error if the object is not currently added to the Stage.
GPoint get mousePosition {
if (!inStage) {
throw 'To get mousePosition, the object needs to be in the Stage.';
}
return globalToLocal(_sHelperPoint.setTo(
stage!.pointer!.mouseX,
stage!.pointer!.mouseY,
));
}
/// The x-coordinate of the mouse or touch position in the local coordinate
/// system of this object.
///
/// Throws an exception if this object is not a descendant of the Stage.
double get mouseX {
if (inStage) {
return globalToLocal(
_sHelperPoint.setTo(
stage!.pointer!.mouseX,
stage!.pointer!.mouseY,
),
_sHelperPoint,
).x;
} else {
throw 'To get mouseX object needs to be a descendant of Stage.';
}
}
/// The y-coordinate of the mouse or touch position in the local coordinate
/// system of this object.
///
/// Throws an exception if this object is not a descendant of the Stage.
double get mouseY {
if (inStage) {
return globalToLocal(
_sHelperPoint.setTo(
stage!.pointer!.mouseX,
stage!.pointer!.mouseY,
),
_sHelperPoint,
).y;
} else {
throw 'To get mouseY object needs to be a descendant of Stage.';
}
}
/// Returns the parent container.
GDisplayObjectContainer? get parent {
return $parent;
}
/// Returns the x coordinate of the pivot point,
/// the center of scaling and rotation.
double get pivotX {
return _pivotX;
}
/// Sets the x-coordinate of the object's pivot point.
/// Throws an error if [value] is NaN.
/// If the value has not changed, does nothing.
set pivotX(double value) {
if (value.isNaN) throw '[$this.pivotX] can not be NaN nor null';
if (_pivotX == value) {
return;
}
_pivotX = value;
$setTransformationChanged();
}
/// Returns the y coordinate of the pivot point,
/// the center of scaling and rotation.
double get pivotY {
return _pivotY;
}
/// Sets the y-coordinate of the object's pivot point.
/// Throws an error if [value] is NaN.
/// If the value has not changed, does nothing.
set pivotY(double value) {
if (value.isNaN) throw '[$this.pivotY] can not be NaN nor null';
if (_pivotY == value) {
return;
}
_pivotY = value;
$setTransformationChanged();
}
/// Returns the root `GDisplayObject` that is the ancestor of this object
/// (either a `Stage` or a `null`).
GDisplayObject? get root {
var current = this;
while (current.$parent != null) {
if (current.$parent is Stage) return current;
current = current.$parent!;
}
return null;
}
/// Returns the angle of rotation in radians.
double get rotation {
return _rotation;
}
/// Sets the rotation angle in radians.
/// Throws an error if [value] is null or NaN.
/// If the value has not changed, does nothing.
set rotation(double? value) {
if (value?.isNaN ?? true) {
throw '[$this.rotation] can not be NaN nor null';
}
if (_rotation == value) {
return;
}
_rotation = value!;
$setTransformationChanged();
}
/// The rotation angle in radians about the x-axis for 3d transformation.
double get rotationX {
return _rotationX;
}
/// (Experimental)
/// Sets the rotation angle in degrees about the x-axis for 3d transformation.
/// Throws an error if [value] is NaN.
/// If the value has not changed, does nothing.
set rotationX(double value) {
if (value.isNaN) {
throw '[$this.rotationX] can not be NaN';
}
if (_rotationX == value) {
return;
}
_rotationX = value;
if (!_isWarned3d) {
_warn3d();
}
$setTransformationChanged();
}
/// The rotation angle in radians about the y-axis for 3d transformation.
double get rotationY {
return _rotationY;
}
/// (Experimental)
/// Sets the rotation angle in degrees about the x-axis for 3d transformation.
/// Throws an error if [value] is NaN.
/// If the value has not changed, does nothing.
set rotationY(double value) {
if (value.isNaN) {
throw '[$this.rotationY] can not be NaN';
}
if (_rotationY == value) {
return;
}
_rotationY = value;
if (!_isWarned3d) {
_warn3d();
}
$setTransformationChanged();
}
/// Shortcut for setting proportional X and Y scale values.
double get scale {
return _scaleX;
}
/// Sets the X and Y scale values proportionally to [value].
///
/// Setting [value] to the same value as the current scale has no effect.
set scale(double value) {
if (value == _scaleX) {
return;
}
_scaleY = _scaleX = value;
$setTransformationChanged();
}
/// Returns the horizontal scale of the object.
double get scaleX {
return _scaleX;
}
/// Sets the x-axis scale factor of the object.
/// Throws an error if [value] is null or NaN.
/// If the value has not changed, does nothing.
set scaleX(double? value) {
if (value?.isNaN ?? true) {
throw '[$this.scaleX] can not be NaN nor null';
}
if (_scaleX == value) {
return;
}
_scaleX = value!;
$setTransformationChanged();
}
/// Returns the vertical scale of the object.
double get scaleY {
return _scaleY;
}
/// Sets the y-axis scale factor of the object.
/// Throws an error if [value] is null or NaN.
/// If the value has not changed, does nothing.
set scaleY(double? value) {
if (value?.isNaN ?? true) {
throw '[$this.scaleY] can not be NaN nor null';
}
if (_scaleY == value) {
return;
}
_scaleY = value!;
$setTransformationChanged();
}
/// Returns the angle of skew on the x-axis in radians.
double get skewX {
return _skewX;
}
/// Sets the skew factor along the x-axis.
/// Throws an error if [value] is NaN.
/// If the value has not changed, does nothing.
set skewX(double value) {
if (value.isNaN) {
throw '[$this.skewX] can not be NaN nor null';
}
if (_skewX == value) {
return;
}
_skewX = value;
$setTransformationChanged();
}
/// Returns the angle of skew on the y-axis in radians.
double get skewY {
return _skewY;
}
/// Sets the skew factor along the y-axis.
/// Throws an error if [value] is NaN.
/// If the value has not changed, does nothing.
set skewY(double value) {
if (value.isNaN) {
throw '[$this.skewY] can not be NaN';
}
if (_skewY == value) {
return;
}
_skewY = value;
$setTransformationChanged();
}
/// Returns the stage this object is on or `null` if it's not on a stage.
Stage? get stage {
return base is Stage ? base as Stage? : null;
}
/// Gets the transformation matrix that represents the object's position,
/// scale, rotation and skew.
///
/// If the transformation matrix has changed since the last time this method
/// was called, a new matrix will be computed and cached until the next time
/// this method is called.
GMatrix get transformationMatrix {
if (_transformationChanged || _transformationMatrix == null) {
_transformationChanged = false;
_transformationMatrix ??= GMatrix();
$updateTransformationMatrices(
x,
y,
pivotX,
pivotY,
scaleX,
scaleY,
skewX,
skewY,
rotation,
_transformationMatrix!,
);
}
return _transformationMatrix!;
}
/// Sets the transformation matrix that represents the object's position,
/// scale, rotation and skew.
///
/// The object's position, scale, rotation, and skew values are updated to
/// match the new transformation matrix.
set transformationMatrix(GMatrix matrix) {
const piQuarter = Math.PI / 4.0;
requiresRedraw();
_transformationChanged = false;
_transformationMatrix ??= GMatrix();
_transformationMatrix!.copyFrom(matrix);
_pivotX = _pivotY = 0;
_x = matrix.tx;
_y = matrix.ty;
_skewX = Math.atan(-matrix.c / matrix.d);
_skewY = Math.atan(matrix.b / matrix.a);
_scaleY = (_skewX > -piQuarter && _skewX < piQuarter)
? matrix.d / Math.cos(_skewX)
: -matrix.c / Math.sin(_skewX);
_scaleX = (_skewY > -piQuarter && _skewY < piQuarter)
? matrix.a / Math.cos(_skewY)
: -matrix.b / Math.sin(_skewY);
if (MathUtils.isEquivalent(_skewX, _skewY)) {
_rotation = _skewX;
_skewX = _skewY = 0;
} else {
_rotation = 0;
}
}
/// Returns whether the display object is visible or not.
bool get visible {
return _visible;
}
/// Sets whether the display object is visible or not.
/// It helps to optimize performance skipping the rendering process.
set visible(bool flag) {
if (_visible != flag) {
_visible = flag;
requiresRedraw();
}
}
/// Indicates the width of the display object, in dp.
/// The `width` is calculated based on the bounds of the content of the
/// display object.
/// When you set the `width` property, the `scaleX` property is adjusted
/// accordingly, as shown in the following code:
/// ```dart
/// var rect:Shape = new Shape();
/// rect.graphics.beginFill(0xFF0000);
/// rect.graphics.drawRect(0, 0, 100, 100);
/// trace(rect.scaleX) // 1;
/// rect.width = 200;
/// trace(rect.scaleX) // 2;
/// ```
/// A display object with no content (such as an empty sprite) has a width
/// of 0, even if you try to set width to a different value.
double get width {
return getBounds($parent, _sHelperRect)!.width;
}
/// Sets the width of the object to the given [value].
/// If the given value is null or NaN, an error is thrown.
///
/// If the object has a zero scale, it is first set to 1.0 before computing
/// the new scale based on the new width value.
/// Otherwise, the current scale is used to compute the current actual width
/// of the object, which is then used to
/// determine the new scale based on the new width value.
/// The [value] parameter should be the desired new width value of the object.
set width(double? value) {
if (value?.isNaN ?? true) {
throw '[$this.width] can not be NaN nor null';
}
double? actualW;
var zeroScale = _scaleX < 1e-8 && _scaleX > -1e-8;
if (zeroScale) {
scaleX = 1.0;
actualW = width;
} else {
actualW = (width / _scaleX).abs();
}
scaleX = value! / actualW;
}
/// Returns the alpha value of the object relative to its parent and all its
/// ancestors.
double get worldAlpha {
return alpha * ($parent?.worldAlpha ?? 1);
}
/// Returns the horizontal scaling factor of the object relative to its
/// parent and all its ancestors.
double get worldScaleX {
return scaleX * ($parent?.worldScaleX ?? 1);
}
/// Returns the vertical scaling factor of the object relative to its parent
/// and all its ancestors.
double get worldScaleY {
return scaleX * ($parent?.worldScaleY ?? 1);
}
/// Returns the x coordinate of the object relative to the stage and all its
/// ancestors.
double get worldX {
return x - pivotX * scaleX + ($parent?.worldX ?? 0);
}
/// Returns the y coordinate of the object relative to the stage and all its
/// ancestors.
double get worldY {
return y - pivotY * scaleY + ($parent?.worldY ?? 0);
}
/// The [x] coordinate of the display object relative to its parent's
/// coordinate system.
double get x {
return _x;
}
/// Sets the [x] coordinate of the display object relative to its parent's
/// coordinate system.
/// Throws an exception if the value is `null` or `NaN`.
/// If the value has not changed, does nothing.
set x(double? value) {
if (value?.isNaN ?? true) {
throw '[$this.x] can not be NaN nor null';
}
if (_x == value) {
return;
}
_x = value!;
$setTransformationChanged();
}
/// The [y] coordinate of the display object relative to its parent's
/// coordinate system.
double get y {
return _y;
}
/// Sets the [y] coordinate of the object's position.
/// Throws an error if [value] is null or NaN.
/// If the value has not changed, does nothing.
set y(double? value) {
if (value?.isNaN ?? true) {
throw '[$this.y] can not be NaN nor null';
}
if (_y == value) {
return;
}
_y = value!;
$setTransformationChanged();
}
/// (Experimental)
/// The z-coordinate of this object in 3D space.
double get z {
return _z;
}
/// (Experimental)
/// Sets the z-axis coordinate of this object in 3D space.
///
/// Throws an error if the given value is NaN.
/// If the new value is the same as the current one,
/// the method returns immediately.
set z(double value) {
if (value.isNaN) {
throw '[$this.z] can not be NaN';
}
if (_z == value) {
return;
}
_z = value;
if (!_isWarned3d) {
_warn3d();
}
$setTransformationChanged();
}
/// (Internal usage)
/// Override this method to implement custom drawing using Flutter's API.
/// You can access the [canvas] object from here. Make sure to also implement
/// [getBounds] and [hitTest] for this display object to work properly.
void $applyPaint(ui.Canvas canvas) {}
/// (Internal usage)
/// Dispatches a mouse event to the appropriate callback functions.
///
/// If [mouseEnabled] is false, no mouse events will be dispatched.
/// The `type` argument specifies the type of mouse event.
/// The `object` argument specifies the object that the event originated from.
/// The `input` argument specifies the mouse input data for the event.
///
/// The following types of mouse events are dispatched:
///
/// - `MouseInputType.zoomPan`: dispatched when the user uses pinch zoom or
/// pan gestures.
/// - `MouseInputType.wheel`: dispatched when the user scrolls the mouse
/// wheel.
/// - `MouseInputType.down`: dispatched when the user presses the left mouse
/// button.
/// - `MouseInputType.move`: dispatched when the user moves the mouse cursor.
/// - `MouseInputType.up`: dispatched when the user releases the left mouse
/// button.
/// - `MouseInputType.over`: dispatched when the mouse cursor enters the
/// bounds of the object.
/// - `MouseInputType.out`: dispatched when the mouse cursor leaves the bounds
/// of the object.
///
/// Depending on the type of mouse event, the appropriate callback function
/// will be invoked.
/// The `mouseInput` argument is a cloned copy of the `input` argument, with
/// its `target` and `currentTarget` fields set to `this` and `object`,
/// respectively.
/// If the mouse event is a `down` event, the `mouseDownObj` field of this
/// object will be set to `object`.
/// If the mouse event is an `up` event, the `mouseDownObj` field will be set
/// to `null`, and the appropriate `onMouseClick` or `onMouseDoubleClick`
/// callback function will be invoked if necessary.
///
/// If the mouse event is a `over` event, the `mouseOverObj` field of this
/// object will be set to `object`. If the `useCursor` field of this object is
/// `true` and the cursor is showing, the cursor will be set to the click
/// cursor.
///
/// If the mouse event is an `out` event, the `mouseOverObj` field will be set
/// to `null`. If the `useCursor` field of this object is `true` and the
/// cursor is showing, the cursor will be set to the basic cursor.
///
/// The `parent` of this object will also receive the mouse event,
/// recursively, until there are no more parents.
void $dispatchMouseCallback(
MouseInputType type,
GDisplayObject object,
MouseInputData input,
) {
if (mouseEnabled) {
var mouseInput = input.clone(this, object, type);
switch (type) {
case MouseInputType.zoomPan:
$onZoomPan?.dispatch(mouseInput);
break;
case MouseInputType.wheel:
$onMouseWheel?.dispatch(mouseInput);
break;
case MouseInputType.down:
$mouseDownObj = object;
$onMouseDown?.dispatch(mouseInput);
break;
// case MouseInputType.rightDown:
// $rightMouseDownObj = object;
// $onRightMouseDown?.dispatch(mouseInput);
// break;
case MouseInputType.move:
$onMouseMove?.dispatch(mouseInput);
break;
case MouseInputType.up:
if ($mouseDownObj == object &&
($onMouseClick != null || $onMouseDoubleClick != null)) {
var mouseClickInput = input.clone(this, object, MouseInputType.up);
$onMouseClick?.dispatch(mouseClickInput);
if ($lastClickTime > 0 &&
input.time - $lastClickTime < MouseInputData.doubleClickTime) {
$onMouseDoubleClick?.dispatch(mouseClickInput);
$lastClickTime = -1;
} else {
$lastClickTime = input.time;
}
}
$mouseDownObj = null;
$onMouseUp?.dispatch(mouseInput);
break;
case MouseInputType.over:
$mouseOverObj = object;
if (useCursor && GMouse.isShowing()) {
GMouse.setClickCursor();
}
$onMouseOver?.dispatch(mouseInput);
break;
case MouseInputType.out:
$mouseOverObj = null;
/// TODO: check if other object on top receives the event
/// todo. before this one, and Cursor loses the stage.
if (useCursor && GMouse.isShowing()) {
GMouse.basic();
}
$onMouseOut?.dispatch(mouseInput);
break;
default:
break;
}
}
$parent?.$dispatchMouseCallback(type, object, input);
}
/// (Internal usage)
/// Sets the [GDisplayObjectContainer] that contains this [GDisplayObject].
void $setParent(GDisplayObjectContainer? value) {
var ancestor = value;
while (ancestor != this && ancestor != null) {
ancestor = ancestor.$parent;
}
if (ancestor == this) {
throw ArgumentError(
'An object cannot be added as a child to itself or one '
'of its children (or children\'s children, etc.)');
} else {
$parent = value;
}
}
/// (Internal usage)
/// Sets the transformationChanged flag and updates the 3D flag based on