/
stage.dart
1171 lines (958 loc) · 34.9 KB
/
stage.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
part of stagexl.display;
/// The StageRenderMode defines how often the Stage is rendered by
/// the [RenderLoop] where the Stage is attached to.
enum StageRenderMode {
/// Render the stage automatically on each frame.
AUTO,
/// Render the stage automatically after a stage invalidation.
AUTO_INVALID,
/// Render the stage once then change to ´STOP´.
ONCE,
/// Do not render the stage.
STOP
}
/// The StageScaleMode defines how the Stage is scaled inside of the Canvas.
enum StageScaleMode { EXACT_FIT, NO_BORDER, NO_SCALE, SHOW_ALL }
/// The StageAlign defines how the content of the Stage is aligned inside
/// of the Canvas. The setting controls where the origin (point 0,0) of the
/// Stage will be placed on the Canvas.
enum StageAlign {
TOP_LEFT,
TOP,
TOP_RIGHT,
LEFT,
NONE,
RIGHT,
BOTTOM_LEFT,
BOTTOM,
BOTTOM_RIGHT
}
/// The Stage is the drawing area where all display objects are rendered to.
/// Place a Canvas element to your HTML and use the Stage to wrap all the
/// rendering functions to this Canvas element.
///
/// Example HTML:
///
/// <canvas id="stage"></canvas>
///
/// Example Dart:
///
/// var canvas = querySelector('#stage');
/// var stage = Stage(canvas, width: 800, height: 600);
class Stage extends DisplayObjectContainer {
static StageOptions defaultOptions = StageOptions();
late CanvasElement _canvas;
late final RenderContext _renderContext;
RenderLoop? _renderLoop;
late final StageConsole _console;
int _sourceWidth = 0;
int _sourceHeight = 0;
int _stageWidth = 0;
int _stageHeight = 0;
num _pixelRatio = 1.0;
bool _invalid = false;
double _avgFrameTime = 0;
double _avgDrawCalls = 0;
double _avgVertexCount = 0;
double _avgIdexCount = 0;
final Rectangle<num> _contentRectangle = Rectangle<num>(0.0, 0.0, 0.0, 0.0);
final Matrix _clientTransformation = Matrix.fromIdentity();
final Matrix _stageTransformation = Matrix.fromIdentity();
final Matrix _consoleTransformation = Matrix.fromIdentity();
final RenderEvent _renderEvent = RenderEvent();
late RenderState _renderState;
InputEventMode _inputEventMode = InputEventMode.MouseOnly;
/// Gets and sets the render mode of this Stage. You can choose between
/// three different modes defined in [StageRenderMode].
StageRenderMode renderMode = StageRenderMode.AUTO;
StageScaleMode _stageScaleMode = StageScaleMode.SHOW_ALL;
StageAlign _stageAlign = StageAlign.NONE;
String _mouseCursor = MouseCursor.DEFAULT;
Point<num> _mousePosition = Point<num>(0.0, 0.0);
InteractiveObject? _mouseTarget;
final List<_Drag> _drags = <_Drag>[];
final Map<int, _TouchPoint> _touchPoints = <int, _TouchPoint>{};
final List<_MouseButton> _mouseButtons = _MouseButton.createDefaults();
//----------------------------------------------------------------------------
/// A dedicated [Juggler] for this Stage. This Juggler is driven by the
/// [RenderLoop] where this Stage is added to. If this Stage is not added
/// to a RenderLoop, the [Juggler] will not advance in time.
final Juggler juggler = Juggler();
/// The interactive object with keyboard focus or null if focus is not set.
InteractiveObject? focus;
/// Gets and sets the background color of this Stage.
int backgroundColor = Color.White;
/// Prevents the browser's default behavior for touch events.
bool preventDefaultOnTouch = true;
/// Prevents the browser's default behavior for mouse events.
bool preventDefaultOnMouse = true;
/// Prevents the browser's default behavior for wheel events.
bool preventDefaultOnWheel = false;
/// Prevents the browser's default behavior for keyboard events.
bool preventDefaultOnKeyboard = false;
//----------------------------------------------------------------------------
static const EventStreamProvider<Event> resizeEvent =
EventStreamProvider<Event>(Event.RESIZE);
static const EventStreamProvider<Event> mouseLeaveEvent =
EventStreamProvider<Event>(Event.MOUSE_LEAVE);
EventStream<Event> get onResize => Stage.resizeEvent.forTarget(this);
EventStream<Event> get onMouseLeave => Stage.mouseLeaveEvent.forTarget(this);
//----------------------------------------------------------------------------
Stage(CanvasElement canvas,
{int? width, int? height, StageOptions? options}) {
if (canvas.tabIndex! <= 0) canvas.tabIndex = 1;
if (canvas.style.outline == '') canvas.style.outline = 'none';
options ??= Stage.defaultOptions;
width ??= canvas.width!;
height ??= canvas.height!;
backgroundColor = options.backgroundColor;
preventDefaultOnTouch = options.preventDefaultOnTouch;
preventDefaultOnMouse = options.preventDefaultOnMouse;
preventDefaultOnWheel = options.preventDefaultOnWheel;
preventDefaultOnKeyboard = options.preventDefaultOnKeyboard;
_canvas = canvas;
_stageAlign = options.stageAlign;
_stageScaleMode = options.stageScaleMode;
renderMode = options.stageRenderMode;
_inputEventMode = options.inputEventMode;
_sourceWidth = width;
_sourceHeight = height;
_pixelRatio = min(options.maxPixelRatio, env.devicePixelRatio);
_renderContext = _createRenderContext(canvas, options);
_renderState = RenderState(_renderContext);
_console = StageConsole()..visible = false;
print('StageXL render engine : ${_renderContext.renderEngine}');
canvas.onKeyDown.listen(_onKeyEvent);
canvas.onKeyUp.listen(_onKeyEvent);
canvas.onKeyPress.listen(_onKeyEvent);
final listenToMouseEvents = _inputEventMode == InputEventMode.MouseOnly ||
_inputEventMode == InputEventMode.MouseAndTouch;
if (listenToMouseEvents) {
canvas.onMouseDown.listen(_onMouseEvent);
canvas.onMouseUp.listen(_onMouseEvent);
canvas.onMouseMove.listen(_onMouseEvent);
canvas.onMouseOut.listen(_onMouseEvent);
canvas.onContextMenu.listen(_onMouseEvent);
canvas.onMouseWheel.listen(_onMouseWheelEvent);
canvas.onDragEnter.listen(_onMouseEvent);
canvas.onDragLeave.listen(_onMouseEvent);
canvas.onDragOver.listen(_onMouseEvent);
canvas.onDrop.listen(_onMouseEvent);
}
final listenToTouchEvents = _inputEventMode == InputEventMode.TouchOnly ||
_inputEventMode == InputEventMode.MouseAndTouch;
if (listenToTouchEvents && env.isTouchEventSupported) {
canvas.onTouchStart.listen(_onTouchEvent);
canvas.onTouchEnd.listen(_onTouchEvent);
canvas.onTouchMove.listen(_onTouchEvent);
canvas.onTouchEnter.listen(_onTouchEvent);
canvas.onTouchLeave.listen(_onTouchEvent);
canvas.onTouchCancel.listen(_onTouchEvent);
}
Mouse.onCursorChanged.listen((cursorName) => _updateMouseCursor());
_updateMouseCursor();
_updateCanvasSize();
_renderContext.clear(backgroundColor);
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
/// Gets the underlying render engine used to draw the pixels to the screen.
/// The returned string is defined in [RenderEngine] and is either "WebGL"
/// or "Canvas2D".
RenderEngine get renderEngine => _renderContext.renderEngine;
/// Gets the [RenderLoop] where this Stage was added to, or
/// NULL in case this Stage is not added to a [RenderLoop].
RenderLoop? get renderLoop => _renderLoop;
/// Gets the [StageConsole] to show render information about the previous
/// frame as well as other custom information.
StageConsole get console => _console;
/// Gets the last known mouse position in Stage coordinates.
@override
Point<num> get mousePosition => _mousePosition;
/// Gets the available content area on the stage. The value of this rectangle
/// changes with the scaleMode and the alignment of the stage, as well as the
/// size of the underlying Canvas element.
Rectangle<num> get contentRectangle => _contentRectangle.clone();
/// Gets the width of the Stage in screen coordinates/pixels.
int get stageWidth => _stageWidth;
/// Gets the height of the Stage in screen coordinates/pixels.
int get stageHeight => _stageHeight;
/// Gets and sets the width of the Stage in world coordinates.
/// The initial value of [sourceWidth] is the width of the canvas
/// element or the width provided in the constructor of the Stage.
int get sourceWidth => _sourceWidth;
set sourceWidth(int value) {
_sourceWidth = value;
_updateCanvasSize();
}
/// Get the max texture size from the current WebGL canvas.
/// Will return null if called on Canvas2D
int? get maxTextureSize => _renderContext.maxTextureSize as int?;
/// Access WebGL parameters, you need to get the int's for access through
/// dart:web_gl. Will return null if called on Canvas2D
Object? getParameter(int parameter) => _renderContext.getParameter(parameter);
/// Gets and sets the height of the Stage in world coordinates.
/// The initial value of [sourceHeight] is the height of the canvas
/// element or the height provided in the constructor of the Stage.
int get sourceHeight => _sourceHeight;
set sourceHeight(int value) {
_sourceHeight = value;
_updateCanvasSize();
}
/// Gets and sets the pixel ratio of the Stage.
num get pixelRatio => _pixelRatio;
set pixelRatio(num value) {
_pixelRatio = value;
_updateCanvasSize();
}
/// Gets and sets the scale mode of this Stage. You can choose between
/// four different modes defined in [StageScaleMode].
StageScaleMode get scaleMode => _stageScaleMode;
set scaleMode(StageScaleMode value) {
_stageScaleMode = value;
_updateCanvasSize();
}
/// Gets and sets the alignment of this Stage inside of the Canvas element.
/// You can choose between nine different align modes defined in [StageAlign].
StageAlign get align => _stageAlign;
set align(StageAlign value) {
_stageAlign = value;
_updateCanvasSize();
}
//----------------------------------------------------------------------------
void _throwStageException() {
throw UnsupportedError(
'The Stage class does not implement this property or method.');
}
@override
set x(num value) {
_throwStageException();
}
@override
set y(num value) {
_throwStageException();
}
@override
set pivotX(num value) {
_throwStageException();
}
@override
set pivotY(num value) {
_throwStageException();
}
@override
set scaleX(num value) {
_throwStageException();
}
@override
set scaleY(num value) {
_throwStageException();
}
@override
set skewX(num value) {
_throwStageException();
}
@override
set skewY(num value) {
_throwStageException();
}
@override
set rotation(num value) {
_throwStageException();
}
@override
set alpha(num value) {
_throwStageException();
}
@override
set width(num value) {
_throwStageException();
}
@override
set height(num value) {
_throwStageException();
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
@override
DisplayObject hitTestInput(num localX, num localY) {
final target = super.hitTestInput(localX, localY);
return target ?? this;
}
//----------------------------------------------------------------------------
/// Invalidate the content of the [Stage]. This will render the stage on the
/// next frame if the [renderMode] is set to [StageRenderMode.AUTO_INVALID].
void invalidate() {
_invalid = true;
}
//----------------------------------------------------------------------------
/// This method is called by the [RenderLoop] where this Stage is added to.
/// If this Stage is not added to a [RenderLoop] you could call this method
/// on your own and therefore get full control of the rendering of this Stage.
void materialize(num currentTime, num deltaTime) {
if (renderMode == StageRenderMode.AUTO ||
renderMode == StageRenderMode.AUTO_INVALID && _invalid ||
renderMode == StageRenderMode.ONCE) {
final stopwatch = Stopwatch()..start();
_updateCanvasSize();
_renderEvent.dispatch();
_renderContext.reset();
_renderContext.renderStatistics.reset();
_renderContext.clear(backgroundColor);
_renderState.reset(_stageTransformation);
_renderState.currentTime = currentTime;
_renderState.deltaTime = deltaTime;
_renderState.renderObject(this);
_renderState.flush();
_invalid = false;
final stats = _renderContext.renderStatistics;
final frameTime = stopwatch.elapsedMilliseconds;
_avgDrawCalls = _avgDrawCalls * 0.75 + stats.drawCount * 0.25;
_avgVertexCount = _avgVertexCount * 0.75 + stats.vertexCount * 0.25;
_avgIdexCount = _avgIdexCount * 0.75 + stats.indexCount * 0.25;
_avgFrameTime = _avgFrameTime * 0.95 + frameTime * 0.05;
if (_console.visible && _console.off == false) {
_console.clear();
_console
.print('FRAMETIME${_avgFrameTime.round().toString().padLeft(6)}');
_console
.print('DRAWCALLS${_avgDrawCalls.round().toString().padLeft(6)}');
_console
.print('VERTICES${_avgVertexCount.round().toString().padLeft(7)}');
_console.print('INDICES${_avgIdexCount.round().toString().padLeft(8)}');
_renderState.reset(_consoleTransformation);
_renderState.renderObject(_console);
_renderState.flush();
}
}
if (renderMode == StageRenderMode.ONCE) {
renderMode = StageRenderMode.STOP;
}
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
RenderContext _createRenderContext(
CanvasElement canvas, StageOptions options) {
if (options.renderEngine == RenderEngine.WebGL) {
try {
return RenderContextWebGL(canvas,
alpha: options.transparent, antialias: options.antialias);
} catch (e) {
return RenderContextCanvas(canvas);
}
} else if (options.renderEngine == RenderEngine.Canvas2D) {
return RenderContextCanvas(canvas);
} else {
throw StateError('Unknown RenderEngine');
}
}
//----------------------------------------------------------------------------
void _startDrag(Sprite sprite, Point<num> globalPoint, Point<num> anchorPoint,
Rectangle<num>? bounds, int touchPointID) {
final drag = _Drag(this, sprite, anchorPoint, bounds, touchPointID);
drag.update(touchPointID, globalPoint);
_drags.removeWhere(
(d) => d.touchPointID == touchPointID || d.sprite == sprite);
_drags.add(drag);
}
void _stopDrag(Sprite sprite) {
_drags.removeWhere((d) => d.sprite == sprite);
}
//----------------------------------------------------------------------------
void _updateCanvasSize() {
var clientLeft = 0, clientTop = 0;
var clientWidth = 0, clientHeight = 0;
final sourceWidth = _sourceWidth;
final sourceHeight = _sourceHeight;
final clientRectangle = _canvas.getBoundingClientRect();
clientLeft = _canvas.clientLeft! + clientRectangle.left.round();
clientTop = _canvas.clientTop! + clientRectangle.top.round();
clientWidth = _canvas.clientWidth;
clientHeight = _canvas.clientHeight;
if (clientWidth == 0 || clientHeight == 0) return;
//----------------------------
var scaleX = 1.0;
var scaleY = 1.0;
var pivotX = 0.0;
var pivotY = 0.0;
final ratioWidth = clientWidth / sourceWidth;
final ratioHeight = clientHeight / sourceHeight;
switch (_stageScaleMode) {
case StageScaleMode.EXACT_FIT:
scaleX = ratioWidth;
scaleY = ratioHeight;
break;
case StageScaleMode.NO_BORDER:
scaleX = scaleY = (ratioWidth > ratioHeight) ? ratioWidth : ratioHeight;
break;
case StageScaleMode.NO_SCALE:
scaleX = scaleY = 1.0;
break;
case StageScaleMode.SHOW_ALL:
scaleX = scaleY = (ratioWidth < ratioHeight) ? ratioWidth : ratioHeight;
break;
}
switch (_stageAlign) {
case StageAlign.LEFT:
case StageAlign.BOTTOM_LEFT:
case StageAlign.TOP_LEFT:
pivotX = 0.0;
break;
case StageAlign.TOP:
case StageAlign.NONE:
case StageAlign.BOTTOM:
pivotX = (clientWidth - sourceWidth * scaleX) / 2;
break;
case StageAlign.TOP_RIGHT:
case StageAlign.RIGHT:
case StageAlign.BOTTOM_RIGHT:
pivotX = clientWidth - sourceWidth * scaleX;
break;
}
switch (_stageAlign) {
case StageAlign.TOP_LEFT:
case StageAlign.TOP:
case StageAlign.TOP_RIGHT:
pivotY = 0.0;
break;
case StageAlign.LEFT:
case StageAlign.NONE:
case StageAlign.RIGHT:
pivotY = (clientHeight - sourceHeight * scaleY) / 2;
break;
case StageAlign.BOTTOM_LEFT:
case StageAlign.BOTTOM:
case StageAlign.BOTTOM_RIGHT:
pivotY = clientHeight - sourceHeight * scaleY;
break;
}
//----------------------------
_contentRectangle.left = -pivotX / scaleX;
_contentRectangle.top = -pivotY / scaleY;
_contentRectangle.width = clientWidth / scaleX;
_contentRectangle.height = clientHeight / scaleY;
// stage to canvas coordinate transformation
_stageTransformation.setTo(scaleX, 0.0, 0.0, scaleY, pivotX, pivotY);
_stageTransformation.scale(pixelRatio, pixelRatio);
// client to stage coordinate transformation
_clientTransformation.setTo(
1.0, 0.0, 0.0, 1.0, -clientLeft - pivotX, -clientTop - pivotY);
_clientTransformation.scale(1.0 / scaleX, 1.0 / scaleY);
_consoleTransformation.identity();
_consoleTransformation.scale(pixelRatio, pixelRatio);
//----------------------------
if (_stageWidth != clientWidth || _stageHeight != clientHeight) {
_stageWidth = clientWidth;
_stageHeight = clientHeight;
_canvas.width = (clientWidth * pixelRatio).round();
_canvas.height = (clientHeight * pixelRatio).round();
// update hi-dpi canvas style size if client size has changed
if (_canvas.clientWidth != clientWidth ||
_canvas.clientHeight != clientHeight) {
_canvas.style.width = '${clientWidth}px';
_canvas.style.height = '${clientHeight}px';
}
dispatchEvent(Event(Event.RESIZE));
}
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void _updateMouseCursor() {
final mouseTarget = _mouseTarget;
var mouseCursor = Mouse.cursor;
if (mouseTarget != null && mouseCursor == MouseCursor.AUTO) {
final mc = mouseTarget.mouseCursor;
if (mc != MouseCursor.AUTO) mouseCursor = mc;
}
if (mouseCursor == MouseCursor.AUTO) {
mouseCursor = MouseCursor.DEFAULT;
}
if (_mouseCursor != mouseCursor) {
_mouseCursor = mouseCursor;
_canvas.style.cursor = Mouse.getCursorStyle(mouseCursor);
}
}
//----------------------------------------------------------------------------
void _onMouseEvent(html.MouseEvent event) {
if (preventDefaultOnMouse) event.preventDefault();
final time = DateTime.now().millisecondsSinceEpoch;
final button = event.button;
InteractiveObject? target;
final stagePoint = _clientTransformation.transformPoint(event.client);
final localPoint = Point<num>(0.0, 0.0);
if (button < 0 || button > 2) return;
if (event.type == 'mousemove' && _mousePosition == stagePoint) return;
final mouseButton = _mouseButtons[button];
_mousePosition = stagePoint;
_drags.forEach((d) => d.update(0, stagePoint));
if (event.type != 'mouseout') {
target = hitTestInput(stagePoint.x, stagePoint.y) as InteractiveObject;
} else {
dispatchEvent(Event(Event.MOUSE_LEAVE));
}
var isDnD = event.type == 'dragenter' ||
event.type == 'dragleave' ||
event.type == 'dragover' ||
event.type == 'drop';
//-----------------------------------------------------------------
// MOUSE_OUT, ROLL_OUT, ROLL_OVER, MOUSE_OVER
if (_mouseTarget != target) {
final oldTarget = _mouseTarget;
final newTarget = target;
final oldTargetList = <DisplayObject>[];
final newTargetList = <DisplayObject>[];
var commonCount = 0;
for (DisplayObject? p = oldTarget; p != null; p = p.parent) {
oldTargetList.add(p);
}
for (DisplayObject? p = newTarget; p != null; p = p.parent) {
newTargetList.add(p);
}
for (;; commonCount++) {
if (commonCount == oldTargetList.length) break;
if (commonCount == newTargetList.length) break;
final ot = oldTargetList[oldTargetList.length - commonCount - 1];
final nt = newTargetList[newTargetList.length - commonCount - 1];
if (ot != nt) break;
}
if (oldTarget != null) {
oldTarget.globalToLocal(stagePoint, localPoint);
oldTarget.dispatchEvent(MouseEvent(
MouseEvent.MOUSE_OUT,
true,
localPoint.x,
localPoint.y,
stagePoint.x,
stagePoint.y,
event.altKey,
event.ctrlKey,
event.shiftKey,
0.0,
0.0,
mouseButton.buttonDown,
0,
null));
if (isDnD) {
oldTarget.dispatchEvent(MouseEvent(
MouseEvent.DRAG_LEAVE,
true,
localPoint.x,
localPoint.y,
stagePoint.x,
stagePoint.y,
event.altKey,
event.ctrlKey,
event.shiftKey,
0.0,
0.0,
mouseButton.buttonDown,
0,
event.dataTransfer));
}
}
for (var i = 0; i < oldTargetList.length - commonCount; i++) {
final target = oldTargetList[i];
target.globalToLocal(stagePoint, localPoint);
target.dispatchEvent(MouseEvent(
MouseEvent.ROLL_OUT,
false,
localPoint.x,
localPoint.y,
stagePoint.x,
stagePoint.y,
event.altKey,
event.ctrlKey,
event.shiftKey,
0.0,
0.0,
mouseButton.buttonDown,
0,
null));
}
for (var i = newTargetList.length - commonCount - 1; i >= 0; i--) {
final target = newTargetList[i];
target.globalToLocal(stagePoint, localPoint);
target.dispatchEvent(MouseEvent(
MouseEvent.ROLL_OVER,
false,
localPoint.x,
localPoint.y,
stagePoint.x,
stagePoint.y,
event.altKey,
event.ctrlKey,
event.shiftKey,
0.0,
0.0,
mouseButton.buttonDown,
0,
null));
}
if (newTarget != null) {
newTarget.globalToLocal(stagePoint, localPoint);
newTarget.dispatchEvent(MouseEvent(
MouseEvent.MOUSE_OVER,
true,
localPoint.x,
localPoint.y,
stagePoint.x,
stagePoint.y,
event.altKey,
event.ctrlKey,
event.shiftKey,
0.0,
0.0,
mouseButton.buttonDown,
0,
null));
if (isDnD) {
newTarget.dispatchEvent(MouseEvent(
MouseEvent.DRAG_ENTER,
true,
localPoint.x,
localPoint.y,
stagePoint.x,
stagePoint.y,
event.altKey,
event.ctrlKey,
event.shiftKey,
0.0,
0.0,
mouseButton.buttonDown,
0,
event.dataTransfer));
}
}
_mouseTarget = newTarget;
}
_updateMouseCursor();
//-----------------------------------------------------------------
// MOUSE_DOWN, MOUSE_MOVE, MOUSE_UP, CLICK, DOUBLE_CLICK
String? mouseEventType;
var isClick = false;
var isDoubleClick = false;
if (event.type == 'mousedown') {
_canvas.focus();
mouseEventType = mouseButton.mouseDownEventType;
if (target != mouseButton.target || time > mouseButton.clickTime + 500) {
mouseButton.clickCount = 0;
}
mouseButton.buttonDown = true;
mouseButton.target = target;
mouseButton.clickTime = time;
mouseButton.clickCount++;
} else if (event.type == 'mouseup') {
mouseEventType = mouseButton.mouseUpEventType;
mouseButton.buttonDown = false;
isClick = mouseButton.target == target;
isDoubleClick = isClick &&
mouseButton.clickCount.isEven &&
(time < mouseButton.clickTime + 500);
} else if (event.type == 'mousemove') {
mouseEventType = MouseEvent.MOUSE_MOVE;
} else if (event.type == 'contextmenu') {
mouseEventType = MouseEvent.CONTEXT_MENU;
} else if (event.type == 'dragenter') {
mouseEventType = MouseEvent.DRAG_ENTER;
} else if (event.type == 'dragleave') {
mouseEventType = MouseEvent.DRAG_LEAVE;
} else if (event.type == 'drop') {
mouseEventType = MouseEvent.DROP;
}
//-----------------------------------------------------------------
if (mouseEventType != null && target != null) {
target.globalToLocal(stagePoint, localPoint);
target.dispatchEvent(MouseEvent(
mouseEventType,
true,
localPoint.x,
localPoint.y,
stagePoint.x,
stagePoint.y,
event.altKey,
event.ctrlKey,
event.shiftKey,
0.0,
0.0,
mouseButton.buttonDown,
mouseButton.clickCount,
isDnD ? event.dataTransfer : null));
if (isClick) {
mouseEventType = isDoubleClick && target.doubleClickEnabled
? mouseButton.mouseDoubleClickEventType
: mouseButton.mouseClickEventType;
target.dispatchEvent(MouseEvent(
mouseEventType,
true,
localPoint.x,
localPoint.y,
stagePoint.x,
stagePoint.y,
event.altKey,
event.ctrlKey,
event.shiftKey,
0.0,
0.0,
mouseButton.buttonDown,
0,
null));
}
}
}
//----------------------------------------------------------------------------
void _onMouseWheelEvent(html.WheelEvent event) {
if (preventDefaultOnWheel) event.preventDefault();
final stagePoint = _clientTransformation.transformPoint(event.client);
final localPoint = Point<num>(0.0, 0.0);
final target =
hitTestInput(stagePoint.x, stagePoint.y) as InteractiveObject;
target.globalToLocal(stagePoint, localPoint);
final mouseEvent = MouseEvent(
MouseEvent.MOUSE_WHEEL,
true,
localPoint.x,
localPoint.y,
stagePoint.x,
stagePoint.y,
event.altKey,
event.ctrlKey,
event.shiftKey,
event.deltaX,
event.deltaY,
false,
0,
null);
target.dispatchEvent(mouseEvent);
if (mouseEvent.isImmediatePropagationStopped) {
event.stopImmediatePropagation();
}
if (mouseEvent.isPropagationStopped) {
event.stopPropagation();
}
if (mouseEvent.isDefaultPrevented) {
event.preventDefault();
}
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void _onTouchEvent(html.TouchEvent event) {
if (preventDefaultOnTouch) event.preventDefault();
final eventType = event.type;
final altKey = event.altKey;
final ctrlKey = event.ctrlKey;
final shiftKey = event.shiftKey;
if (event.changedTouches == null) return;
final touches =
event.changedTouches!.where((touch) => touch.identifier != null);
for (var changedTouch in touches) {
final identifier = changedTouch.identifier!;
final clientPoint = changedTouch.client;
final stagePoint = _clientTransformation.transformPoint(clientPoint);
final localPoint = Point<num>(0.0, 0.0);
final target =
hitTestInput(stagePoint.x, stagePoint.y) as InteractiveObject;
final touchPoint = _touchPoints.putIfAbsent(
identifier, () => _TouchPoint(target, _touchPoints.isEmpty));
final touchPointID = touchPoint.touchPointID;
final primaryTouchPoint = touchPoint.primaryTouchPoint;
_drags.forEach((d) => d.update(touchPointID, stagePoint));
//---------------------------------------------------------------
// TOUCH_OUT, TOUCH_ROLL_OUT, TOUCH_ROLL_OVER, TOUCH_OVER
if (touchPoint.currentTarget != target) {
final oldTarget = touchPoint.currentTarget;
final newTarget = target;
final oldTargetList = <DisplayObject>[];
final newTargetList = <DisplayObject>[];
var commonCount = 0;
for (DisplayObject? p = oldTarget; p != null; p = p.parent) {
oldTargetList.add(p);
}
for (DisplayObject? p = newTarget; p != null; p = p.parent) {
newTargetList.add(p);
}
for (;; commonCount++) {
if (commonCount == oldTargetList.length) break;
if (commonCount == newTargetList.length) break;
final ot = oldTargetList[oldTargetList.length - commonCount - 1];
final nt = newTargetList[newTargetList.length - commonCount - 1];
if (ot != nt) break;
}
oldTarget.globalToLocal(stagePoint, localPoint);
oldTarget.dispatchEvent(TouchEvent(
TouchEvent.TOUCH_OUT,
true,
localPoint.x,
localPoint.y,
stagePoint.x,
stagePoint.y,
altKey,
ctrlKey,
shiftKey,
touchPointID,
primaryTouchPoint));
for (var i = 0; i < oldTargetList.length - commonCount; i++) {
final target = oldTargetList[i];
target.globalToLocal(stagePoint, localPoint);
target.dispatchEvent(TouchEvent(
TouchEvent.TOUCH_ROLL_OUT,
false,
localPoint.x,
localPoint.y,
stagePoint.x,
stagePoint.y,
altKey,
ctrlKey,
shiftKey,
touchPointID,
primaryTouchPoint));
}