@awayjs/scene 0.13.265 → 0.13.266

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (268) hide show
  1. package/.eslintrc +100 -100
  2. package/README.md +61 -61
  3. package/bundle/awayjs-scene.umd.js +2 -2
  4. package/bundle/awayjs-scene.umd.js.map +1 -1
  5. package/copyVersionToIndex.js +23 -23
  6. package/dist/index.d.ts +76 -75
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +67 -66
  9. package/dist/lib/Scene.d.ts +84 -84
  10. package/dist/lib/Scene.js +288 -288
  11. package/dist/lib/Settings.d.ts +16 -16
  12. package/dist/lib/Settings.js +60 -60
  13. package/dist/lib/adapters/IDisplayObjectAdapter.d.ts +13 -13
  14. package/dist/lib/adapters/IDisplayObjectAdapter.js +1 -1
  15. package/dist/lib/adapters/IFilter.d.ts +24 -24
  16. package/dist/lib/adapters/IFilter.js +1 -1
  17. package/dist/lib/adapters/IMovieClipAdapter.d.ts +14 -14
  18. package/dist/lib/adapters/IMovieClipAdapter.js +1 -1
  19. package/dist/lib/adapters/ITextfieldAdapter.d.ts +4 -4
  20. package/dist/lib/adapters/ITextfieldAdapter.js +1 -1
  21. package/dist/lib/base/IBitmapDrawable.d.ts +11 -11
  22. package/dist/lib/base/IBitmapDrawable.js +1 -1
  23. package/dist/lib/base/IFrameLabel.d.ts +4 -4
  24. package/dist/lib/base/IFrameLabel.js +1 -1
  25. package/dist/lib/base/IFrameScript.d.ts +5 -5
  26. package/dist/lib/base/IFrameScript.js +1 -1
  27. package/dist/lib/base/MouseButtons.d.ts +26 -22
  28. package/dist/lib/base/MouseButtons.d.ts.map +1 -1
  29. package/dist/lib/base/MouseButtons.js +1 -1
  30. package/dist/lib/base/Timeline.d.ts +148 -148
  31. package/dist/lib/base/Timeline.js +535 -535
  32. package/dist/lib/base/TimelineActionType.d.ts +18 -18
  33. package/dist/lib/base/TimelineActionType.js +1 -1
  34. package/dist/lib/controllers/ControllerBase.d.ts +14 -14
  35. package/dist/lib/controllers/ControllerBase.js +60 -60
  36. package/dist/lib/controllers/FirstPersonController.d.ts +78 -78
  37. package/dist/lib/controllers/FirstPersonController.js +228 -228
  38. package/dist/lib/controllers/FollowController.d.ts +12 -12
  39. package/dist/lib/controllers/FollowController.js +27 -27
  40. package/dist/lib/controllers/HoverController.d.ts +105 -105
  41. package/dist/lib/controllers/HoverController.js +302 -302
  42. package/dist/lib/controllers/LookAtController.d.ts +14 -14
  43. package/dist/lib/controllers/LookAtController.js +68 -68
  44. package/dist/lib/controllers/SpringController.d.ts +38 -38
  45. package/dist/lib/controllers/SpringController.js +59 -59
  46. package/dist/lib/display/Billboard.d.ts +147 -147
  47. package/dist/lib/display/Billboard.js +426 -426
  48. package/dist/lib/display/Camera.d.ts +40 -40
  49. package/dist/lib/display/Camera.js +90 -90
  50. package/dist/lib/display/DisplayObject.d.ts +938 -938
  51. package/dist/lib/display/DisplayObject.js +1641 -1641
  52. package/dist/lib/display/DisplayObjectContainer.d.ts +325 -325
  53. package/dist/lib/display/DisplayObjectContainer.js +480 -480
  54. package/dist/lib/display/LineSegment.d.ts +84 -84
  55. package/dist/lib/display/LineSegment.js +215 -215
  56. package/dist/lib/display/LoaderContainer.d.ts +504 -504
  57. package/dist/lib/display/LoaderContainer.js +603 -603
  58. package/dist/lib/display/MorphSprite.d.ts +20 -20
  59. package/dist/lib/display/MorphSprite.js +254 -254
  60. package/dist/lib/display/MovieClip.d.ts +152 -152
  61. package/dist/lib/display/MovieClip.js +735 -735
  62. package/dist/lib/display/SimpleButton.d.ts +158 -158
  63. package/dist/lib/display/SimpleButton.js +228 -228
  64. package/dist/lib/display/Skybox.d.ts +156 -156
  65. package/dist/lib/display/Skybox.js +309 -309
  66. package/dist/lib/display/Sprite.d.ts +70 -70
  67. package/dist/lib/display/Sprite.js +144 -144
  68. package/dist/lib/display/TextField.d.ts +1109 -1109
  69. package/dist/lib/display/TextField.js +3109 -3109
  70. package/dist/lib/display/TextSprite.d.ts +10 -10
  71. package/dist/lib/display/TextSprite.js +25 -25
  72. package/dist/lib/elements/SkyboxElements.d.ts +29 -29
  73. package/dist/lib/elements/SkyboxElements.js +127 -127
  74. package/dist/lib/errors/CastError.d.ts +4 -4
  75. package/dist/lib/errors/CastError.js +10 -10
  76. package/dist/lib/events/CameraEvent.d.ts +16 -16
  77. package/dist/lib/events/CameraEvent.js +30 -30
  78. package/dist/lib/events/FocusEvent.d.ts +42 -42
  79. package/dist/lib/events/FocusEvent.js +43 -43
  80. package/dist/lib/events/KeyboardEvent.d.ts +21 -21
  81. package/dist/lib/events/KeyboardEvent.js +43 -43
  82. package/dist/lib/events/MouseEvent.d.ts +91 -184
  83. package/dist/lib/events/MouseEvent.d.ts.map +1 -1
  84. package/dist/lib/events/MouseEvent.js +122 -191
  85. package/dist/lib/events/PointerEvent.d.ts +94 -0
  86. package/dist/lib/events/PointerEvent.d.ts.map +1 -0
  87. package/dist/lib/events/PointerEvent.js +73 -0
  88. package/dist/lib/events/ResizeEvent.d.ts +15 -15
  89. package/dist/lib/events/ResizeEvent.js +38 -38
  90. package/dist/lib/events/TextfieldEvent.d.ts +19 -19
  91. package/dist/lib/events/TextfieldEvent.js +28 -28
  92. package/dist/lib/events/TouchEvent.d.ts +51 -0
  93. package/dist/lib/events/TouchEvent.d.ts.map +1 -0
  94. package/dist/lib/events/TouchEvent.js +83 -0
  95. package/dist/lib/factories/DefaultSceneGraphFactory.d.ts +26 -26
  96. package/dist/lib/factories/DefaultSceneGraphFactory.js +54 -54
  97. package/dist/lib/factories/ISceneGraphFactory.d.ts +20 -20
  98. package/dist/lib/factories/ISceneGraphFactory.js +1 -1
  99. package/dist/lib/image/SceneImage2D.d.ts +236 -236
  100. package/dist/lib/image/SceneImage2D.js +687 -687
  101. package/dist/lib/managers/DefaultFontManager.d.ts +14 -14
  102. package/dist/lib/managers/DefaultFontManager.js +101 -101
  103. package/dist/lib/managers/DeviceFontManager.d.ts +8 -8
  104. package/dist/lib/managers/DeviceFontManager.js +44 -44
  105. package/dist/lib/managers/FrameScriptManager.d.ts +36 -36
  106. package/dist/lib/managers/FrameScriptManager.js +377 -377
  107. package/dist/lib/managers/IInputRecorder.d.ts +3 -3
  108. package/dist/lib/managers/IInputRecorder.js +1 -1
  109. package/dist/lib/managers/MouseManager.d.ts +76 -89
  110. package/dist/lib/managers/MouseManager.d.ts.map +1 -1
  111. package/dist/lib/managers/MouseManager.js +809 -761
  112. package/dist/lib/prefabs/PrefabBase.d.ts +19 -19
  113. package/dist/lib/prefabs/PrefabBase.js +42 -42
  114. package/dist/lib/prefabs/PrimitiveCapsulePrefab.d.ts +56 -56
  115. package/dist/lib/prefabs/PrimitiveCapsulePrefab.js +269 -269
  116. package/dist/lib/prefabs/PrimitiveConePrefab.d.ts +21 -21
  117. package/dist/lib/prefabs/PrimitiveConePrefab.js +43 -43
  118. package/dist/lib/prefabs/PrimitiveCubePrefab.d.ts +74 -74
  119. package/dist/lib/prefabs/PrimitiveCubePrefab.js +545 -545
  120. package/dist/lib/prefabs/PrimitiveCylinderPrefab.d.ts +79 -79
  121. package/dist/lib/prefabs/PrimitiveCylinderPrefab.js +584 -584
  122. package/dist/lib/prefabs/PrimitivePlanePrefab.d.ts +65 -65
  123. package/dist/lib/prefabs/PrimitivePlanePrefab.js +300 -300
  124. package/dist/lib/prefabs/PrimitivePolygonPrefab.d.ts +29 -29
  125. package/dist/lib/prefabs/PrimitivePolygonPrefab.js +64 -64
  126. package/dist/lib/prefabs/PrimitivePrefabBase.d.ts +67 -67
  127. package/dist/lib/prefabs/PrimitivePrefabBase.js +158 -158
  128. package/dist/lib/prefabs/PrimitiveSpherePrefab.d.ts +48 -48
  129. package/dist/lib/prefabs/PrimitiveSpherePrefab.js +294 -294
  130. package/dist/lib/prefabs/PrimitiveTorusPrefab.d.ts +55 -55
  131. package/dist/lib/prefabs/PrimitiveTorusPrefab.js +260 -260
  132. package/dist/lib/text/AntiAliasType.d.ts +24 -24
  133. package/dist/lib/text/AntiAliasType.js +28 -28
  134. package/dist/lib/text/BitmapFontChar.d.ts +39 -39
  135. package/dist/lib/text/BitmapFontChar.js +42 -42
  136. package/dist/lib/text/BitmapFontTable.d.ts +80 -80
  137. package/dist/lib/text/BitmapFontTable.js +203 -203
  138. package/dist/lib/text/FNTGenerator.d.ts +10 -10
  139. package/dist/lib/text/FNTGenerator.js +74 -74
  140. package/dist/lib/text/FNTGeneratorCanvas.d.ts +22 -22
  141. package/dist/lib/text/FNTGeneratorCanvas.js +205 -205
  142. package/dist/lib/text/FNTGeneratrorBase.d.ts +7 -7
  143. package/dist/lib/text/FNTGeneratrorBase.js +7 -7
  144. package/dist/lib/text/Font.d.ts +38 -38
  145. package/dist/lib/text/Font.js +109 -109
  146. package/dist/lib/text/FontLookUpMode.d.ts +5 -5
  147. package/dist/lib/text/FontLookUpMode.js +7 -7
  148. package/dist/lib/text/FontStyleName.d.ts +6 -6
  149. package/dist/lib/text/FontStyleName.js +7 -7
  150. package/dist/lib/text/GridFitType.d.ts +31 -31
  151. package/dist/lib/text/GridFitType.js +35 -35
  152. package/dist/lib/text/HTMLTextProcessor.d.ts +8 -8
  153. package/dist/lib/text/HTMLTextProcessor.js +423 -423
  154. package/dist/lib/text/IFontTable.d.ts +16 -16
  155. package/dist/lib/text/IFontTable.js +1 -1
  156. package/dist/lib/text/TesselatedFontChar.d.ts +58 -58
  157. package/dist/lib/text/TesselatedFontChar.js +66 -66
  158. package/dist/lib/text/TesselatedFontTable.d.ts +95 -95
  159. package/dist/lib/text/TesselatedFontTable.js +992 -992
  160. package/dist/lib/text/TextFieldAutoSize.d.ts +28 -28
  161. package/dist/lib/text/TextFieldAutoSize.js +32 -32
  162. package/dist/lib/text/TextFieldType.d.ts +20 -20
  163. package/dist/lib/text/TextFieldType.js +24 -24
  164. package/dist/lib/text/TextFormat.d.ts +273 -273
  165. package/dist/lib/text/TextFormat.js +440 -440
  166. package/dist/lib/text/TextFormatAlign.d.ts +26 -26
  167. package/dist/lib/text/TextFormatAlign.js +30 -30
  168. package/dist/lib/text/TextInteractionMode.d.ts +21 -21
  169. package/dist/lib/text/TextInteractionMode.js +25 -25
  170. package/dist/lib/text/TextLineMetrics.d.ts +61 -61
  171. package/dist/lib/text/TextLineMetrics.js +42 -42
  172. package/dist/lib/text/TextShape.d.ts +20 -20
  173. package/dist/lib/text/TextShape.js +75 -75
  174. package/dist/lib/tools/Merge.d.ts +81 -81
  175. package/dist/lib/tools/Merge.js +266 -266
  176. package/dist/lib/utils/Cast.d.ts +20 -20
  177. package/dist/lib/utils/Cast.js +271 -271
  178. package/index.ts +92 -91
  179. package/lib/Scene.ts +343 -343
  180. package/lib/Settings.ts +83 -83
  181. package/lib/adapters/IDisplayObjectAdapter.ts +23 -23
  182. package/lib/adapters/IFilter.ts +25 -25
  183. package/lib/adapters/IMovieClipAdapter.ts +24 -24
  184. package/lib/adapters/ITextfieldAdapter.ts +6 -6
  185. package/lib/base/IBitmapDrawable.ts +13 -13
  186. package/lib/base/IFrameLabel.ts +3 -3
  187. package/lib/base/IFrameScript.ts +4 -4
  188. package/lib/base/MouseButtons.ts +31 -26
  189. package/lib/base/Timeline.ts +727 -727
  190. package/lib/base/TimelineActionType.ts +18 -18
  191. package/lib/controllers/ControllerBase.ts +66 -66
  192. package/lib/controllers/FirstPersonController.ts +241 -241
  193. package/lib/controllers/FollowController.ts +22 -22
  194. package/lib/controllers/HoverController.ts +304 -304
  195. package/lib/controllers/LookAtController.ts +75 -75
  196. package/lib/controllers/SpringController.ts +91 -91
  197. package/lib/display/Billboard.ts +504 -504
  198. package/lib/display/Camera.ts +89 -89
  199. package/lib/display/DisplayObject.ts +1893 -1893
  200. package/lib/display/DisplayObjectContainer.ts +538 -538
  201. package/lib/display/LineSegment.ts +248 -248
  202. package/lib/display/LoaderContainer.ts +651 -651
  203. package/lib/display/MorphSprite.ts +347 -347
  204. package/lib/display/MovieClip.ts +879 -879
  205. package/lib/display/SimpleButton.ts +224 -224
  206. package/lib/display/Skybox.ts +379 -379
  207. package/lib/display/Sprite.ts +165 -165
  208. package/lib/display/TextField.ts +3958 -3958
  209. package/lib/display/TextSprite.ts +22 -22
  210. package/lib/elements/SkyboxElements.ts +141 -141
  211. package/lib/errors/CastError.ts +6 -6
  212. package/lib/events/CameraEvent.ts +29 -29
  213. package/lib/events/FocusEvent.ts +69 -69
  214. package/lib/events/KeyboardEvent.ts +40 -40
  215. package/lib/events/MouseEvent.ts +147 -287
  216. package/lib/events/PointerEvent.ts +146 -0
  217. package/lib/events/ResizeEvent.ts +31 -31
  218. package/lib/events/TextfieldEvent.ts +28 -28
  219. package/lib/events/TouchEvent.ts +101 -0
  220. package/lib/factories/DefaultSceneGraphFactory.ts +56 -56
  221. package/lib/factories/ISceneGraphFactory.ts +28 -28
  222. package/lib/image/SceneImage2D.ts +1020 -1020
  223. package/lib/managers/DefaultFontManager.ts +124 -124
  224. package/lib/managers/DeviceFontManager.ts +55 -55
  225. package/lib/managers/FrameScriptManager.ts +431 -431
  226. package/lib/managers/IInputRecorder.ts +2 -2
  227. package/lib/managers/MouseManager.ts +944 -884
  228. package/lib/prefabs/PrefabBase.ts +47 -47
  229. package/lib/prefabs/PrimitiveCapsulePrefab.ts +286 -286
  230. package/lib/prefabs/PrimitiveConePrefab.ts +37 -37
  231. package/lib/prefabs/PrimitiveCubePrefab.ts +630 -630
  232. package/lib/prefabs/PrimitiveCylinderPrefab.ts +657 -657
  233. package/lib/prefabs/PrimitivePlanePrefab.ts +352 -352
  234. package/lib/prefabs/PrimitivePolygonPrefab.ts +53 -53
  235. package/lib/prefabs/PrimitivePrefabBase.ts +177 -177
  236. package/lib/prefabs/PrimitiveSpherePrefab.ts +336 -336
  237. package/lib/prefabs/PrimitiveTorusPrefab.ts +282 -282
  238. package/lib/text/AntiAliasType.ts +24 -24
  239. package/lib/text/BitmapFontChar.ts +53 -53
  240. package/lib/text/BitmapFontTable.ts +232 -232
  241. package/lib/text/FNTGenerator.ts +100 -100
  242. package/lib/text/FNTGeneratorCanvas.ts +287 -287
  243. package/lib/text/FNTGeneratrorBase.ts +10 -10
  244. package/lib/text/Font.ts +114 -114
  245. package/lib/text/FontLookUpMode.ts +5 -5
  246. package/lib/text/FontStyleName.ts +6 -6
  247. package/lib/text/GridFitType.ts +32 -32
  248. package/lib/text/HTMLTextProcessor.ts +418 -418
  249. package/lib/text/IFontTable.ts +17 -17
  250. package/lib/text/TesselatedFontChar.ts +80 -80
  251. package/lib/text/TesselatedFontTable.ts +1221 -1221
  252. package/lib/text/TextFieldAutoSize.ts +30 -30
  253. package/lib/text/TextFieldType.ts +21 -21
  254. package/lib/text/TextFormat.ts +601 -601
  255. package/lib/text/TextFormatAlign.ts +28 -28
  256. package/lib/text/TextInteractionMode.ts +21 -21
  257. package/lib/text/TextLineMetrics.ts +74 -74
  258. package/lib/text/TextShape.ts +90 -90
  259. package/lib/tools/Merge.ts +311 -311
  260. package/lib/utils/Cast.ts +304 -304
  261. package/package.json +74 -74
  262. package/rollup.config.js +33 -33
  263. package/scripts/build.sh +2 -2
  264. package/scripts/gendocs.sh +14 -14
  265. package/scripts/genexamples.sh +14 -14
  266. package/scripts/genfull.sh +14 -14
  267. package/system.config.js +51 -51
  268. package/tsconfig.json +18 -18
@@ -1,885 +1,945 @@
1
- import { Vector3D } from '@awayjs/core';
2
- import { Stage } from '@awayjs/stage';
3
-
4
- import { PickingCollision, RaycastPicker, ContainerNode } from '@awayjs/view';
5
- import { FocusEvent } from '../events/FocusEvent';
6
-
7
- import { KeyboardEvent } from '../events/KeyboardEvent';
8
- import { MouseEvent as AwayMouseEvent } from '../events/MouseEvent';
9
-
10
- import { IInputRecorder } from './IInputRecorder';
11
-
12
- /**
13
- * MouseManager enforces a singleton pattern and is not intended to be instanced.
14
- * it provides a manager class for detecting mouse hits on scene objects and sending out mouse events.
15
- */
16
- export class MouseManager {
17
- private static _instance: MouseManager;
18
- public static inputRecorder: IInputRecorder;
19
-
20
- private _stage: Stage;
21
- private static _instancePool: Record<number, MouseManager> = {};
22
- private _pickerLookup: Array<RaycastPicker> = new Array<RaycastPicker>();
23
-
24
- private _updateDirty: boolean;
25
- private _prevCollision: PickingCollision;
26
-
27
- private _mouseDragCollision: PickingCollision; // entity hit on mouse-down
28
- private _mouseDragging: boolean; // true while mosue is dragged
29
- private _focusNode: ContainerNode; // entity currently in focus
30
-
31
- public allowKeyInput: boolean=true;
32
-
33
- private _collisionIsEnabledButton: boolean=false;
34
-
35
- private _eventBubbling: boolean=true; // should events bubble up
36
- private _allowFocusOnUnfocusable: boolean=true; // should unfocus-able object steal focus ?
37
-
38
- private _showCursor: boolean;
39
- private _cursorType: 'pointer' | 'auto' = 'auto';
40
-
41
- private _nullVector: Vector3D = new Vector3D();
42
- private _queuedEvents: Array<AwayMouseEvent> = new Array<AwayMouseEvent>();
43
-
44
- private _mouseMoveEvent;
45
- private _mouseUp: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.MOUSE_UP);
46
- private _mouseUpOutside: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.MOUSE_UP_OUTSIDE);
47
- private _mouseClick: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.CLICK);
48
- private _mouseOut: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.MOUSE_OUT);
49
- private _dragOut: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.DRAG_OUT);
50
- private _dragOver: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.DRAG_OVER);
51
- private _mouseDown: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.MOUSE_DOWN);
52
- private _mouseMove: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.MOUSE_MOVE);
53
- private _mouseOver: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.MOUSE_OVER);
54
- private _mouseWheel: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.MOUSE_WHEEL);
55
- private _mouseDoubleClick: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.DOUBLE_CLICK);
56
- private _rollOver: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.ROLL_OVER);
57
- private _rollOut: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.ROLL_OUT);
58
-
59
- private _dragMove: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.DRAG_MOVE);
60
- private _dragStart: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.DRAG_START);
61
- private _dragStop: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.DRAG_STOP);
62
-
63
- private _focusIn: FocusEvent = new FocusEvent(FocusEvent.FOCUS_IN);
64
- private _focusOut: FocusEvent = new FocusEvent(FocusEvent.FOCUS_OUT);
65
-
66
- private _useSoftkeyboard: boolean = false;
67
-
68
- public buttonEnabledDirty: boolean;
69
- private _isTouch: Boolean;
70
- private _isAVM1Dragging: Boolean = false;
71
-
72
- public startDragObject(collision?: PickingCollision) {
73
- this._isAVM1Dragging = true;
74
-
75
- // we MUST overrider collision target if present, because draggable !== drag emitter
76
- if (collision)
77
- this._mouseDragCollision = collision;
78
- }
79
-
80
- public stopDragObject() {
81
- this._isAVM1Dragging = false;
82
- }
83
-
84
- public get showCursor(): boolean {
85
- return this._showCursor;
86
- }
87
-
88
- public set showCursor(value: boolean) {
89
- this._showCursor = value;
90
- this.cursorType = this._cursorType;
91
- }
92
-
93
- public set cursorType(t: 'pointer' | 'auto') {
94
- this._cursorType = t;
95
- this._stage.container.style.cursor = this._showCursor ? t : 'none';
96
- }
97
-
98
- public get cursorType() {
99
- return this._cursorType;
100
- }
101
-
102
- public get eventBubbling(): boolean {
103
- return this._eventBubbling;
104
- }
105
-
106
- public set eventBubbling(value: boolean) {
107
- this._eventBubbling = value;
108
- }
109
-
110
- /**
111
- * Creates a new <code>MouseManager</code> object.
112
- */
113
- constructor(stage: Stage) {
114
- this._stage = stage;
115
- this.onDoubleClick = this.onDoubleClick.bind(this);
116
- this.onMouseDown = this.onMouseDown.bind(this);
117
- this.onMouseMove = this.onMouseMove.bind(this);
118
- this.onMouseUp = this.onMouseUp.bind(this);
119
- this.onMouseWheel = this.onMouseWheel.bind(this);
120
- this.onMouseOver = this.onMouseOver.bind(this);
121
- this.onMouseOut = this.onMouseOut.bind(this);
122
- this.onKeyDown = this.onKeyDown.bind(this);
123
- this.onKeyUp = this.onKeyUp.bind(this);
124
-
125
- this.buttonEnabledDirty = false;
126
- this._isTouch = (('ontouchstart' in self) || navigator['msMaxTouchPoints'] > 0);
127
- this._showCursor = true;
128
- this._mouseDragging = false;
129
-
130
- //register stage
131
- const container = this._stage.container;
132
- container.addEventListener('touchstart', this.onMouseDown);
133
- container.addEventListener('mousedown', this.onMouseDown);
134
-
135
- window.addEventListener('touchmove', this.onMouseMove);
136
- window.addEventListener('mousemove', this.onMouseMove);
137
- window.addEventListener('mouseup', this.onMouseUp);
138
-
139
- container.addEventListener('touchend', this.onMouseUp);
140
- container.addEventListener('mousewheel', this.onMouseWheel);
141
- container.addEventListener('mouseover', this.onMouseOver);
142
- container.addEventListener('mouseout', this.onMouseOut);
143
-
144
- window.addEventListener('keydown', this.onKeyDown);
145
- window.addEventListener('keyup', this.onKeyUp);
146
-
147
- // supress context menu
148
- container.addEventListener('contextmenu', e => e.preventDefault());
149
- }
150
-
151
- public set useSoftkeyboard(value: boolean) {
152
-
153
- this._useSoftkeyboard = value;
154
- // todo: improve this, so that we can
155
- // - use device-softkeyboard on tablet / mobide
156
- // - optionally use a custom softkeyboard
157
- if (!value) {
158
- window.addEventListener('keydown', this.onKeyDown);
159
- window.addEventListener('keyup', this.onKeyUp);
160
- } else if (value) {
161
- window.removeEventListener('keydown', this.onKeyDown);
162
- window.removeEventListener('keyup', this.onKeyUp);
163
- }
164
- }
165
-
166
- public get useSoftkeyboard(): boolean {
167
- return this._useSoftkeyboard;
168
- }
169
-
170
- public dispose() {
171
- //unregister stage
172
- const container = this._stage.container;
173
- container.removeEventListener('dblclick', this.onDoubleClick);
174
- container.removeEventListener('touchstart', this.onMouseDown);
175
- container.removeEventListener('mousedown', this.onMouseDown);
176
- window.removeEventListener('touchmove', this.onMouseMove);
177
- window.removeEventListener('mousemove', this.onMouseMove);
178
- window.removeEventListener('mouseup', this.onMouseUp);
179
- container.removeEventListener('touchend', this.onMouseUp);
180
- container.removeEventListener('wheel', this.onMouseWheel);
181
- container.removeEventListener('mouseover', this.onMouseOver);
182
- container.removeEventListener('mouseout', this.onMouseOut);
183
- window.removeEventListener('keydown', this.onKeyDown);
184
- window.removeEventListener('keyup', this.onKeyUp);
185
-
186
- this._stage = null;
187
-
188
- this._mouseMoveEvent = null;
189
- this._mouseUp = null;
190
- this._mouseUpOutside = null;
191
- this._mouseClick = null;
192
- this._mouseOut = null;
193
- this._dragOut = null;
194
- this._dragOver = null;
195
- this._mouseDown = null;
196
- this._mouseMove = null;
197
- this._mouseOver = null;
198
- this._mouseWheel = null;
199
- this._mouseDoubleClick = null;
200
- this._dragMove = null;
201
- this._dragStart = null;
202
- this._dragStop = null;
203
-
204
- }
205
-
206
- public static clearInstance(stage: Stage) {
207
- if (this._instancePool[stage.id]) {
208
- this._instancePool[stage.id].dispose();
209
- delete this._instancePool[stage.id];
210
- }
211
- }
212
-
213
- public static getInstance(stage: Stage): MouseManager {
214
- return this._instancePool[stage.id]
215
- || (this._instancePool[stage.id] = new MouseManager(stage));
216
-
217
- }
218
-
219
- public setFocus(node: ContainerNode) {
220
- if (this._focusNode == node)
221
- return;
222
-
223
- // in FP6, a mouseclick on non focus-able object still steal the focus
224
- // in newer FP they only steal the focus if the the new hit is focusable
225
- if (this._allowFocusOnUnfocusable || this._focusNode.container.tabEnabled) {
226
- if (this._focusNode) {
227
- this.dispatchEvent(this._focusOut, this._focusNode);
228
- this._focusNode.container.setFocus(false, true);
229
- }
230
-
231
- this._focusNode = node;
232
-
233
- if (this._focusNode) {
234
- this.dispatchEvent(this._focusIn, this._focusNode);
235
- this._focusNode.container.setFocus(true, true);
236
- }
237
- }
238
- }
239
-
240
- public getFocus() {
241
- return this._focusNode;
242
- }
243
-
244
- private dispatchEvent(event: AwayMouseEvent | FocusEvent, dispatcher: ContainerNode) {
245
- if (!this._eventBubbling) {
246
- if (dispatcher) {
247
- event._dispatchEvent(dispatcher, dispatcher.container);
248
- }
249
- return;
250
- }
251
-
252
- const target = dispatcher?.container;
253
-
254
- while (dispatcher) {
255
- if (event.commonAncestor && dispatcher == event.commonAncestor) {
256
- return;
257
- }
258
- event._dispatchEvent(dispatcher, target);
259
-
260
- if (!event._iAllowedToPropagate) {
261
- dispatcher = null;
262
- } else {
263
- dispatcher = dispatcher.parent;
264
- }
265
- }
266
- }
267
-
268
- private setupAndDispatchEvent(event: AwayMouseEvent, sourceEvent,
269
- collision: PickingCollision, commonAncestor: ContainerNode = null) {
270
-
271
- if (sourceEvent) {
272
- event.delta = sourceEvent.wheelDelta;
273
- event.ctrlKey = sourceEvent.ctrlKey;
274
- event.altKey = sourceEvent.altKey;
275
- event.shiftKey = sourceEvent.shiftKey;
276
- event.buttons = sourceEvent.buttons;
277
- }
278
-
279
- event = this.setUpEvent(event, collision, commonAncestor);
280
- this.dispatchEvent(event, event.rootNode);
281
- }
282
-
283
- public fireMouseEvents(forcePicker: RaycastPicker = null): void {
284
-
285
- let collision: PickingCollision;
286
- if (forcePicker) {
287
- collision = forcePicker.getViewCollision(this._stage.screenX, this._stage.screenY);
288
- } else if (this._updateDirty) {
289
- for (let i = 0; i < this._pickerLookup.length; i++)
290
- if (!collision || this._pickerLookup[i].layeredView)
291
- collision = this._pickerLookup[i].getViewCollision(this._stage.screenX,
292
- this._stage.screenY, false, collision);
293
- }
294
-
295
- //if theres nothing to update, return
296
- if (!forcePicker && !this._updateDirty)
297
- return;
298
-
299
- let event: AwayMouseEvent;
300
- let dispatcher: ContainerNode;
301
-
302
- const len = this._queuedEvents.length;
303
- for (let i = 0; i < len; ++i) {
304
- event = this._queuedEvents[i];
305
- dispatcher = collision?.rootNode;
306
-
307
- this.setUpEvent(event, collision);
308
-
309
- if (event.type == AwayMouseEvent.MOUSE_DOWN) {
310
- this._mouseDragging = true;
311
-
312
- // no event-bubbling. dispatch on stage first
313
- if (!this._eventBubbling)
314
- this._stage.dispatchEvent(event);
315
-
316
- // @todo: at this point the object under the mouse might have been changed,
317
- // so we need to recheck the collision ?
318
-
319
- // on Touch dispatch mouseOver Command
320
- if (this._isTouch)
321
- this.setupAndDispatchEvent(this._mouseOver, this._mouseMoveEvent, collision);
322
-
323
- this._mouseDragCollision = collision;
324
-
325
- if (dispatcher) {
326
- //console.log("onPress", dispatcher)
327
- this.dispatchEvent(event, dispatcher);
328
- } else if (this._eventBubbling)
329
- this._stage.dispatchEvent(event);
330
-
331
- if (this._mouseDragCollision) {
332
- this.setFocus(this._mouseDragCollision?.rootNode);
333
- this.setupAndDispatchEvent(this._dragStart, event, this._mouseDragCollision);
334
- }
335
-
336
- } else if (event.type == AwayMouseEvent.MOUSE_UP) {
337
-
338
- // no event-bubbling. dispatch on stage first
339
- if (!this._eventBubbling)
340
- this._stage.dispatchEvent(event);
341
-
342
- // @todo: at this point the object under the mouse might have been changed,
343
- // so we need to recheck the collision ?
344
-
345
- let upContainerNode: ContainerNode = null;
346
- let upRootNode: ContainerNode = null;
347
- if (this._isAVM1Dragging && this._mouseDragCollision) {
348
- // avm1dragging is in process, dispatch the mouse-up on this.
349
- // mouseDragEntity instead of the current collision
350
- upRootNode = this._mouseDragCollision.rootNode;
351
- upContainerNode = this._mouseDragCollision.containerNode;
352
- } else if (this._mouseDragging && this._mouseDragCollision
353
- && this._mouseDragCollision.rootNode != dispatcher) {
354
- // no avm1dragging is in process, but current collision
355
- // is not the same as collision that appeared on mouse-down,
356
- // need to dispatch a MOUSE_UP_OUTSIDE after mouse up
357
- } else if (this._mouseDragging && this._mouseDragCollision
358
- && this._mouseDragCollision.rootNode == dispatcher) {
359
- // no avm1dragging is in process,
360
- // current collision is the same as collision that appeared on
361
- // mouse-down, need to dispatch a MOUSE_CLICK event after mouse up
362
- upRootNode = this._mouseDragCollision.rootNode;
363
- upContainerNode = this._mouseDragCollision.containerNode;
364
- }
365
-
366
- if (upRootNode) {
367
- //console.log("onRelease", upRootNode)
368
- this.dispatchEvent(event, upRootNode);
369
- } else if (this._eventBubbling && dispatcher)
370
- this.dispatchEvent(event, dispatcher);
371
- else if (this._eventBubbling)
372
- this._stage.dispatchEvent(event);
373
-
374
- if (upContainerNode) {
375
- if (!this._isAVM1Dragging)
376
- this.setupAndDispatchEvent(this._mouseClick, this._isTouch ? this._mouseMoveEvent : event, this._mouseDragCollision);
377
- this.setupAndDispatchEvent(this._dragStop, event, this._mouseDragCollision);
378
- } else if (this._mouseDragging && this._mouseDragCollision) {
379
- this.setupAndDispatchEvent(this._mouseUpOutside, event, this._mouseDragCollision);
380
- }
381
-
382
- this._mouseDragCollision = null;
383
- this._mouseDragging = false;
384
- this._isAVM1Dragging = false;
385
-
386
- } else if (event.type == AwayMouseEvent.MOUSE_MOVE) {
387
-
388
- // no event-bubbling. dispatch on stage first
389
- if (!this._eventBubbling) {
390
- this._stage.dispatchEvent(event);
391
- }
392
-
393
- // fire to picker
394
- if (event.rootNode) {
395
- this.dispatchEvent(event, event.rootNode);
396
- }
397
-
398
- if (this._mouseDragCollision) {
399
- this.setupAndDispatchEvent(this._dragMove, event, this._mouseDragCollision);
400
- }
401
-
402
- // if bubling is exist, fire to stage late
403
- if (this._eventBubbling) {
404
- this._stage.dispatchEvent(event);
405
- }
406
-
407
- } else {
408
- // MouseEvent.MOUSE_OVER / MouseEvent.MOUSE_OUT / MouseEvent.DRAG_OVER / MouseEvent.DRAG_OUT
409
- this.dispatchEvent(event, dispatcher);
410
- }
411
- }
412
-
413
- this._queuedEvents.length = 0;
414
-
415
- this._rollOut.commonAncestor = null;
416
- this._rollOver.commonAncestor = null;
417
-
418
- const collisionNode: ContainerNode = collision?.rootNode;
419
- const prevCollisionNode: ContainerNode = this._prevCollision?.rootNode;
420
-
421
- if (collisionNode != prevCollisionNode) {
422
-
423
- // If colliding object has changed, queue OVER and OUT events.
424
- // If the mouse is dragged (mouse-down is hold), use DRAG_OVER and DRAG_OUT as well as MOUSE_OVER MOUSE_OUT
425
- // DRAG_OVER and DRAG_OUT are only dispatched on the object that was hit on the mouse-down
426
- // (_mouseDragEntity)
427
-
428
- // Store the info if the collision is a enabled Button (_collisionIsEnabledButton)
429
-
430
- if (prevCollisionNode) {
431
- this.setupAndDispatchEvent(this._mouseOut, this._mouseMoveEvent, this._prevCollision);
432
-
433
- if (this._mouseDragging && this._mouseDragCollision
434
- && this._mouseDragCollision.rootNode == prevCollisionNode)
435
- this.setupAndDispatchEvent(this._dragOut, this._mouseMoveEvent, this._prevCollision);
436
- }
437
-
438
- if (!prevCollisionNode && collisionNode) {
439
- // rollout / rollover easy case, can just bubble up
440
- this.setupAndDispatchEvent(this._rollOut, this._mouseMoveEvent, this._prevCollision);
441
- this.setupAndDispatchEvent(this._rollOver, this._mouseMoveEvent, collision);
442
- }
443
- if (prevCollisionNode && !collisionNode) {
444
- // rollout / rollover easy case, can just bubble up
445
- this.setupAndDispatchEvent(this._rollOut, this._mouseMoveEvent, this._prevCollision);
446
- this.setupAndDispatchEvent(this._rollOver, this._mouseMoveEvent, collision);
447
- }
448
- if (prevCollisionNode && collisionNode) {
449
- // rollout / rollover find common ancester and only bubble up to that point
450
- const parentsPrev: ContainerNode[] = [];
451
- let parent: ContainerNode = prevCollisionNode;
452
- while (parent && !parent.container.isAVMScene) {
453
- parentsPrev.push(parent);
454
- parent = parent.parent;
455
- }
456
- let commonAncestor: ContainerNode = null;
457
- parent = collisionNode;
458
- while (parent && !parent.container.isAVMScene) {
459
- const oldParentIdx = parentsPrev.indexOf(parent);
460
- if (oldParentIdx == -1) {
461
- parent = parent.parent;
462
- } else {
463
- commonAncestor = parent;
464
- parent = null;
465
- }
466
- }
467
- if (commonAncestor != prevCollisionNode)
468
- this.setupAndDispatchEvent(this._rollOut, this._mouseMoveEvent,
469
- this._prevCollision, commonAncestor);
470
-
471
- if (commonAncestor != collisionNode)
472
- this.setupAndDispatchEvent(this._rollOver, this._mouseMoveEvent, collision, commonAncestor);
473
-
474
- }
475
-
476
- this._collisionIsEnabledButton = collisionNode ? (<any> collisionNode).buttonEnabled : false;
477
-
478
- if (collisionNode) {
479
- this.setupAndDispatchEvent(this._mouseOver, this._mouseMoveEvent, collision);
480
-
481
- if (this._mouseDragging)
482
- this.setupAndDispatchEvent(this._dragOver, this._mouseMoveEvent, collision);
483
- }
484
-
485
- this._prevCollision = collision;
486
- } else {
487
- // colliding object has not changed
488
- // Check if we need to send any MOUSE_OVER/DRAG_OVER event to handle the case
489
- // when a Button has become active while under the mouse.
490
- const isActiveButton = collisionNode ? (<any> collisionNode).buttonEnabled : false;
491
-
492
- if (this._collisionIsEnabledButton != isActiveButton && isActiveButton) {
493
- if (!this._isTouch)
494
- this.setupAndDispatchEvent(this._mouseOver, this._mouseMoveEvent, collision);
495
- else if (this._mouseDragCollision && this._mouseDragCollision.rootNode == collisionNode)
496
- this.setupAndDispatchEvent(this._dragOver, this._mouseMoveEvent, collision);
497
- }
498
-
499
- this._collisionIsEnabledButton = isActiveButton;
500
- }
501
-
502
- // set cursor if not dragging mouse ???
503
- if (!this._mouseDragging) {
504
- this.cursorType = collisionNode ? <any>collisionNode.container.getMouseCursor() : 'auto';
505
- }
506
-
507
- this._updateDirty = false;
508
- }
509
-
510
- public registerPicker(picker: RaycastPicker): void {
511
- this._pickerLookup.push(picker);
512
- }
513
-
514
- public unregisterPicker(picker: RaycastPicker): void {
515
- this._pickerLookup.splice(this._pickerLookup.indexOf(picker), 1);
516
- }
517
-
518
- public addEventsForSceneBinary(touchMessage: ArrayBuffer, sceneIdx: number = 0): void {
519
-
520
- const newTouchEvent: any = {};
521
- newTouchEvent.clientX = null;// we get the x position from the active touch
522
- newTouchEvent.clientY = null;// we get the y position from the active touch
523
- newTouchEvent.touches = [];
524
- newTouchEvent.changedTouches = [];
525
- newTouchEvent.preventDefault = function () { };
526
- const messageScene: Float32Array = new Float32Array(touchMessage);
527
- // transfer touches to event
528
- let i = 0;
529
- let cnt = 0;
530
- const touchCnt = 0;
531
- cnt++;//we temporary added 1 float to transfer fps from java to js. skip this
532
- const numTouches = messageScene[cnt++];
533
- const touchtype = messageScene[cnt++];
534
- const activeTouchID = messageScene[cnt++];
535
-
536
- if ((touchtype != 1) && (touchtype != 6) && (touchtype != 12) && (touchtype != 262) && (touchtype != 518)) {
537
- // if this is not a UP command, we add all touches
538
- for (i = 0; i < numTouches; i++) {
539
- const newTouch: any = {};
540
- newTouch.identifier = messageScene[cnt++];
541
- newTouch.clientX = messageScene[cnt++];
542
- newTouch.clientY = messageScene[cnt++];
543
- newTouchEvent.touches[i] = newTouch;
544
- //newTouchEvent.changedTouches[i] = newTouch;
545
- }
546
- newTouchEvent.changedTouches[0] = newTouchEvent.touches[activeTouchID];
547
-
548
- } else {
549
- // if this is a UP command, we add all touches, except the active one
550
- if (numTouches == 1) {
551
-
552
- const newTouch: any = {};
553
- newTouch.identifier = messageScene[cnt++];
554
- newTouch.clientX = messageScene[cnt++];
555
- newTouch.clientY = messageScene[cnt++];
556
- newTouchEvent.clientX = newTouch.clientX;
557
- newTouchEvent.clientY = newTouch.clientY;
558
-
559
- } else {
560
- for (i = 0; i < numTouches; i++) {
561
- const newTouch: any = {};
562
- newTouch.identifier = messageScene[cnt++];
563
- newTouch.clientX = messageScene[cnt++];
564
- newTouch.clientY = messageScene[cnt++];
565
- if (i != activeTouchID) {
566
- newTouchEvent.touches[touchCnt] = newTouch;
567
- //newTouchEvent.changedTouches[touchCnt++] = newTouch;
568
- } else {
569
- newTouchEvent.clientX = newTouch.clientX;
570
- newTouchEvent.clientY = newTouch.clientY;
571
- }
572
- }
573
- }
574
- }
575
-
576
- //console.log("Touch ID:"+touchtype+" activeTouchID "+activeTouchID+" numTouches "+numTouches+" x"+x+" y"+y);
577
- /*
578
- public static final int ACTION_DOWN = 0;
579
- public static final int ACTION_POINTER_1_DOWN = 5;
580
- public static final int ACTION_POINTER_DOWN = 5;
581
- public static final int ACTION_BUTTON_PRESS = 11;
582
- public static final int ACTION_POINTER_2_DOWN = 261;
583
- public static final int ACTION_POINTER_3_DOWN = 517;
584
-
585
- public static final int ACTION_UP = 1;
586
- public static final int ACTION_POINTER_1_UP = 6;
587
- public static final int ACTION_POINTER_UP = 6;
588
- public static final int ACTION_BUTTON_RELEASE = 12;
589
- public static final int ACTION_POINTER_2_UP = 262;
590
- public static final int ACTION_POINTER_3_UP = 518;
591
-
592
- public static final int ACTION_MOVE = 2;
593
-
594
- */
595
- if ((touchtype == 0) || (touchtype == 5) || (touchtype == 11) || (touchtype == 261) || (touchtype == 517)) {
596
- this.onMouseDown(newTouchEvent);
597
- } else if ((touchtype == 1) || (touchtype == 6)
598
- || (touchtype == 12) || (touchtype == 262) || (touchtype == 518)) {
599
- this.onMouseUp(newTouchEvent);
600
- } else if (touchtype == 2) {
601
- this.onMouseMove(newTouchEvent);
602
- } else {
603
- console.log('recieved unknown touch event-type: ' + touchtype);
604
- }
605
- }
606
-
607
- public fireEventsForSceneFromString(touchMessage: String, _sceneIdx: number = 0): void {
608
-
609
- const newTouchEvent: any = {};
610
- newTouchEvent.clientX = null;// set the x position from the active touch
611
- newTouchEvent.clientY = null;// set the y position from the active touch
612
- newTouchEvent.preventDefault = function () { };
613
- const touchesFromMessage = touchMessage.split(',');
614
- // transfer touches to event
615
- let i = 0;
616
- let cnt = 0;
617
- const numTouches = parseInt(touchesFromMessage[cnt++]);
618
- const touchtype = parseInt(touchesFromMessage[cnt++]);
619
- const activeTouch = parseInt(touchesFromMessage[cnt++]);
620
- newTouchEvent.touches = [];
621
- newTouchEvent.changedTouches = [];
622
- if ((touchtype != 1) && (touchtype != 6)) {
623
- for (i = 0; i < numTouches; i++) {
624
- const newTouch: any = {};
625
- newTouch.identifier = touchesFromMessage[cnt++];
626
- newTouch.clientX = touchesFromMessage[cnt++];
627
- newTouch.clientY = touchesFromMessage[cnt++];
628
- newTouchEvent.touches[i] = newTouch;
629
- newTouchEvent.changedTouches[i] = newTouch;
630
- }
631
- newTouchEvent.changedTouches[i] = newTouchEvent.touches[activeTouch];
632
- } else {
633
- for (i = 0; i < numTouches; i++) {
634
- if (i != activeTouch) {
635
- const newTouch: any = {};
636
- newTouch.identifier = touchesFromMessage[cnt++];
637
- newTouch.clientX = touchesFromMessage[cnt++];
638
- newTouch.clientY = touchesFromMessage[cnt++];
639
- newTouchEvent.touches[i] = newTouch;
640
- newTouchEvent.changedTouches[i] = newTouch;
641
- } else {
642
- newTouchEvent.clientX = touchesFromMessage[cnt++];
643
- newTouchEvent.clientY = touchesFromMessage[cnt++];
644
- cnt++;
645
- }
646
- }
647
-
648
- }
649
- if (touchtype == 0) {//mousedown
650
- this.onMouseDown(newTouchEvent);
651
- } else if (touchtype == 1) {//mouseup
652
- this.onMouseUp(newTouchEvent);
653
- } else if (touchtype == 2) {//mousemove
654
- this.onMouseMove(newTouchEvent);
655
-
656
- } else if (touchtype == 261) {//mousedownPointer
657
- this.onMouseDown(newTouchEvent);
658
-
659
- } else if (touchtype == 6) {//mouseupPointer
660
- this.onMouseUp(newTouchEvent);
661
- }
662
- }
663
-
664
- // ---------------------------------------------------------------------
665
- // Private.
666
- // ---------------------------------------------------------------------
667
- private setUpEvent(event: AwayMouseEvent,
668
- collision: PickingCollision, commonAncestor: ContainerNode = null): AwayMouseEvent {
669
- event._iAllowedToImmediatlyPropagate = true;
670
- event._iAllowedToPropagate = true;
671
- // 2D properties.
672
- event.screenX = this._stage.screenX;
673
- event.screenY = this._stage.screenY;
674
- //console.log("event", event, collisionNode, collisionNode);
675
-
676
- // 3D properties.
677
- if (collision) {
678
- event.containerNode = collision.containerNode;
679
-
680
- event.rootNode = collision.rootNode;
681
- // Object.
682
- event.traversable = collision.traversable;
683
- // UV.
684
- event.uv = collision.uv;
685
- // Position.
686
- event.position = collision.position?.clone();
687
- // Normal.
688
- event.normal = collision.normal?.clone();
689
- // Face index.
690
- event.elementIndex = collision.elementIndex;
691
- } else {
692
- // Set all to null.
693
- event.containerNode = null;
694
- event.rootNode = null;
695
- event.traversable = null;
696
- event.uv = null;
697
- event.position = this._nullVector;
698
- event.normal = this._nullVector;
699
- event.elementIndex = 0;
700
- }
701
- event.commonAncestor = commonAncestor;
702
-
703
- return event;
704
- }
705
-
706
- private queueDispatch(event: AwayMouseEvent, sourceEvent: MouseEvent | TouchEvent | WheelEvent): void {
707
- // Store event to be dispatched later.
708
- event.delta = (sourceEvent instanceof WheelEvent) ? -3 * sourceEvent.deltaY / 100 : 0;
709
- event.ctrlKey = sourceEvent.ctrlKey;
710
- event.altKey = sourceEvent.altKey;
711
- event.shiftKey = sourceEvent.shiftKey;
712
- event.buttons = (sourceEvent instanceof MouseEvent) ? <0> sourceEvent.buttons : 0;
713
-
714
- this._queuedEvents.push(event);
715
- }
716
-
717
- // ---------------------------------------------------------------------
718
- // Listeners.
719
- // ---------------------------------------------------------------------
720
-
721
- public onKeyDown(event): void {
722
- !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
723
-
724
- //console.log("Keydown", event);
725
- if (this.allowKeyInput) {
726
- switch (event.keyCode) {
727
- case 32: // spacebar
728
- case 37: // left
729
- case 39: // right
730
- case 38: // up
731
- case 40: // down
732
- event.preventDefault();
733
- break;
734
- default:
735
- break;
736
- }
737
- if (this._focusNode || this._stage) {
738
- //console.log("dispatch keydown on ", this._focusNode);
739
- const newEvent: KeyboardEvent = new KeyboardEvent(KeyboardEvent.KEYDOWN, event.key, event.code);
740
- newEvent.isShift = event.shiftKey;
741
- newEvent.isCTRL = event.ctrlKey;
742
- newEvent.isAlt = event.altKey;
743
- (<any>newEvent).keyCode = event.keyCode;
744
- if (this._focusNode)
745
- this._focusNode.container.dispatchEvent(newEvent);
746
- if (this._stage)
747
- this._stage.dispatchEvent(newEvent);
748
- }
749
- }
750
-
751
- }
752
-
753
- public onKeyUp(event): void {
754
- !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
755
-
756
- //console.log("Keyup", event);
757
- if (this.allowKeyInput) {
758
- switch (event.keyCode) {
759
- case 32: // spacebar
760
- case 37: // left
761
- case 39: // right
762
- case 38: // up
763
- case 40: // down
764
- event.preventDefault();
765
- break;
766
- default:
767
- break;
768
- }
769
-
770
- if (this._focusNode || this._stage) {
771
- //console.log("dispatch keydown on ", this._focusNode);
772
- const newEvent: KeyboardEvent = new KeyboardEvent(KeyboardEvent.KEYUP, event.key, event.code);
773
- newEvent.isShift = event.shiftKey;
774
- newEvent.isCTRL = event.ctrlKey;
775
- newEvent.isAlt = event.altKey;
776
- (<any>newEvent).keyCode = event.keyCode;
777
- if (this._focusNode)
778
- this._focusNode.container.dispatchEvent(newEvent);
779
- if (this._stage)
780
- this._stage.dispatchEvent(newEvent);
781
- }
782
- }
783
-
784
- }
785
-
786
- public onMouseMove(event: MouseEvent | TouchEvent): void {
787
- !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
788
- this._isTouch = (event.type != 'mousemove');
789
-
790
- this.updateColliders(event);
791
-
792
- this.queueDispatch(this._mouseMove, this._mouseMoveEvent = event);
793
- }
794
-
795
- public onMouseOut(event: MouseEvent): void {
796
- !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
797
- this.updateColliders(event);
798
-
799
- this.queueDispatch(this._mouseOut, event);
800
- }
801
-
802
- public onMouseOver(event: MouseEvent): void {
803
- !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
804
- this.updateColliders(event);
805
-
806
- this.queueDispatch(this._mouseOver, event);
807
- }
808
-
809
- public onDoubleClick(event: MouseEvent): void {
810
- !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
811
- this.updateColliders(event);
812
-
813
- // prevent middle and second button
814
- if (event instanceof MouseEvent && event.buttons & 0b110) {
815
- event.preventDefault();
816
- }
817
-
818
- this.queueDispatch(this._mouseDoubleClick, event);
819
- }
820
-
821
- private _isDown: boolean = false;
822
-
823
- public onMouseDown(event: MouseEvent | TouchEvent): void {
824
- this._isTouch = (event.type !== 'mousedown');
825
- if (this._isDown) {
826
- return;
827
- }
828
-
829
- !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
830
- this._isDown = true;
831
-
832
- this.updateColliders(event);
833
-
834
- let prevent = this._isTouch;
835
-
836
- // prevent middle and second button
837
- if (event instanceof MouseEvent && event.buttons & 0b110) {
838
- prevent = true;
839
- }
840
-
841
- if (prevent) {
842
- event.preventDefault();
843
- this._stage.container.focus();
844
- }
845
-
846
- this.queueDispatch(this._mouseDown, event);
847
- }
848
-
849
- public onMouseUp(event: MouseEvent | TouchEvent): void {
850
- if (!this._isDown) {
851
- return;
852
- }
853
-
854
- !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
855
- this._isDown = false;
856
-
857
- this.updateColliders(event);
858
-
859
- let prevent = this._isTouch;
860
- // prevent middle and second button
861
- if (event instanceof MouseEvent && event.button === 1) {
862
- prevent = true;
863
- }
864
-
865
- if (prevent) {
866
- event.preventDefault();
867
- this._stage.container.focus();
868
- }
869
-
870
- this.queueDispatch(this._mouseUp, event);
871
- }
872
-
873
- public onMouseWheel(event: WheelEvent): void {
874
- !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
875
- this.updateColliders(event);
876
-
877
- this.queueDispatch(this._mouseWheel, event);
878
- }
879
-
880
- private updateColliders(event: Event): void {
881
- this._stage.interactionHandler(event);
882
-
883
- this._updateDirty = true;
884
- }
1
+ import { Vector3D } from '@awayjs/core';
2
+ import { Stage } from '@awayjs/stage';
3
+
4
+ import { PickingCollision, RaycastPicker, ContainerNode } from '@awayjs/view';
5
+ import { FocusEvent } from '../events/FocusEvent';
6
+
7
+ import { KeyboardEvent } from '../events/KeyboardEvent';
8
+ import { MouseEvent as AwayMouseEvent } from '../events/MouseEvent';
9
+ import { TouchEvent as AwayTouchEvent } from '../events/TouchEvent';
10
+ import { PointerEvent } from '../events/PointerEvent';
11
+
12
+ import { IInputRecorder } from './IInputRecorder';
13
+ import { MouseButtons } from '../base/MouseButtons';
14
+
15
+ /**
16
+ * MouseManager enforces a singleton pattern and is not intended to be instanced.
17
+ * it provides a manager class for detecting mouse hits on scene objects and sending out mouse events.
18
+ */
19
+ export class MouseManager {
20
+ private static _instance: MouseManager;
21
+ public static inputRecorder: IInputRecorder;
22
+
23
+ private _stage: Stage;
24
+ private static _instancePool: Record<number, MouseManager> = {};
25
+ private _pickerLookup: Array<RaycastPicker> = new Array<RaycastPicker>();
26
+ private _pointerDataArray: Record<number, PointerData> = {};
27
+
28
+ private _updateDirty: boolean;
29
+
30
+ private _mouseDragCollision: PickingCollision; // entity hit on mouse-down
31
+ private _mouseDragging: boolean; // true while mosue is dragged
32
+ private _focusNode: ContainerNode; // entity currently in focus
33
+
34
+ public allowKeyInput: boolean=true;
35
+
36
+ private _eventBubbling: boolean=true; // should events bubble up
37
+ private _allowFocusOnUnfocusable: boolean=true; // should unfocus-able object steal focus ?
38
+
39
+ private _showCursor: boolean;
40
+ private _cursorType: 'pointer' | 'auto' = 'auto';
41
+
42
+ private _nullVector: Vector3D = new Vector3D();
43
+
44
+ private static _touchToMouseDict: Record<string, string> = {
45
+ touchstart: 'mousedown',
46
+ touchend: 'mouseup',
47
+ touchmove: 'mousemove',
48
+ }
49
+
50
+ public static _pointerDict: Record<string, PointerEvent> = {
51
+ mouseup: new AwayMouseEvent(AwayMouseEvent.MOUSE_UP),
52
+ mouseupoutside: new AwayMouseEvent(AwayMouseEvent.MOUSE_UP_OUTSIDE),
53
+ click: new AwayMouseEvent(AwayMouseEvent.CLICK),
54
+ mouseout: new AwayMouseEvent(AwayMouseEvent.MOUSE_OUT),
55
+ mousedown: new AwayMouseEvent(AwayMouseEvent.MOUSE_DOWN),
56
+ mousemove: new AwayMouseEvent(AwayMouseEvent.MOUSE_MOVE),
57
+ mouseover: new AwayMouseEvent(AwayMouseEvent.MOUSE_OVER),
58
+ mousewheel: new AwayMouseEvent(AwayMouseEvent.MOUSE_WHEEL),
59
+ dblclick: new AwayMouseEvent(AwayMouseEvent.DOUBLE_CLICK),
60
+ rollover: new AwayMouseEvent(AwayMouseEvent.ROLL_OVER),
61
+ rollout: new AwayMouseEvent(AwayMouseEvent.ROLL_OUT),
62
+ touchend: new AwayTouchEvent(AwayTouchEvent.TOUCH_END),
63
+ touchtap: new AwayTouchEvent(AwayTouchEvent.TOUCH_TAP),
64
+ touchout: new AwayTouchEvent(AwayTouchEvent.TOUCH_OUT),
65
+ touchstart: new AwayTouchEvent(AwayTouchEvent.TOUCH_BEGIN),
66
+ touchmove: new AwayTouchEvent(AwayTouchEvent.TOUCH_MOVE),
67
+ touchover: new AwayTouchEvent(AwayTouchEvent.TOUCH_OVER),
68
+ touchrollover: new AwayTouchEvent(AwayTouchEvent.TOUCH_ROLL_OVER),
69
+ touchrollout: new AwayTouchEvent(AwayTouchEvent.TOUCH_ROLL_OUT),
70
+
71
+ }
72
+
73
+ private _dragOut: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.DRAG_OUT);
74
+ private _dragOver: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.DRAG_OVER);
75
+
76
+ private _dragMove: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.DRAG_MOVE);
77
+ private _dragStart: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.DRAG_START);
78
+ private _dragStop: AwayMouseEvent = new AwayMouseEvent(AwayMouseEvent.DRAG_STOP);
79
+
80
+ private _focusIn: FocusEvent = new FocusEvent(FocusEvent.FOCUS_IN);
81
+ private _focusOut: FocusEvent = new FocusEvent(FocusEvent.FOCUS_OUT);
82
+
83
+ private _useSoftkeyboard: boolean = false;
84
+
85
+ private _isAVM1Dragging: Boolean = false;
86
+
87
+ public startDragObject(collision?: PickingCollision) {
88
+ this._isAVM1Dragging = true;
89
+
90
+ // we MUST overrider collision target if present, because draggable !== drag emitter
91
+ if (collision)
92
+ this._mouseDragCollision = collision;
93
+ }
94
+
95
+ public stopDragObject() {
96
+ this._isAVM1Dragging = false;
97
+ }
98
+
99
+ public get showCursor(): boolean {
100
+ return this._showCursor;
101
+ }
102
+
103
+ public set showCursor(value: boolean) {
104
+ this._showCursor = value;
105
+ this.cursorType = this._cursorType;
106
+ }
107
+
108
+ public set cursorType(t: 'pointer' | 'auto') {
109
+ this._cursorType = t;
110
+ this._stage.container.style.cursor = this._showCursor ? t : 'none';
111
+ }
112
+
113
+ public get cursorType() {
114
+ return this._cursorType;
115
+ }
116
+
117
+ public get eventBubbling(): boolean {
118
+ return this._eventBubbling;
119
+ }
120
+
121
+ public set eventBubbling(value: boolean) {
122
+ this._eventBubbling = value;
123
+ }
124
+
125
+ /**
126
+ * Creates a new <code>MouseManager</code> object.
127
+ */
128
+ constructor(stage: Stage) {
129
+ this._stage = stage;
130
+ this.onDoubleClick = this.onDoubleClick.bind(this);
131
+ this.onMouseDown = this.onMouseDown.bind(this);
132
+ this.onMouseMove = this.onMouseMove.bind(this);
133
+ this.onMouseUp = this.onMouseUp.bind(this);
134
+ this.onMouseWheel = this.onMouseWheel.bind(this);
135
+ this.onMouseOver = this.onMouseOver.bind(this);
136
+ this.onMouseOut = this.onMouseOut.bind(this);
137
+ this.onKeyDown = this.onKeyDown.bind(this);
138
+ this.onKeyUp = this.onKeyUp.bind(this);
139
+
140
+ this._showCursor = true;
141
+ this._mouseDragging = false;
142
+
143
+ //register stage
144
+ const container = this._stage.container;
145
+ container.addEventListener('touchstart', this.onMouseDown);
146
+ container.addEventListener('mousedown', this.onMouseDown);
147
+
148
+ window.addEventListener('touchmove', this.onMouseMove);
149
+ window.addEventListener('mousemove', this.onMouseMove);
150
+ window.addEventListener('mouseup', this.onMouseUp);
151
+
152
+ container.addEventListener('touchend', this.onMouseUp);
153
+ container.addEventListener('mousewheel', this.onMouseWheel);
154
+ container.addEventListener('mouseover', this.onMouseOver);
155
+ container.addEventListener('mouseout', this.onMouseOut);
156
+
157
+ window.addEventListener('keydown', this.onKeyDown);
158
+ window.addEventListener('keyup', this.onKeyUp);
159
+
160
+ // supress context menu
161
+ container.addEventListener('contextmenu', e => e.preventDefault());
162
+ }
163
+
164
+ public set useSoftkeyboard(value: boolean) {
165
+
166
+ this._useSoftkeyboard = value;
167
+ // todo: improve this, so that we can
168
+ // - use device-softkeyboard on tablet / mobide
169
+ // - optionally use a custom softkeyboard
170
+ if (!value) {
171
+ window.addEventListener('keydown', this.onKeyDown);
172
+ window.addEventListener('keyup', this.onKeyUp);
173
+ } else if (value) {
174
+ window.removeEventListener('keydown', this.onKeyDown);
175
+ window.removeEventListener('keyup', this.onKeyUp);
176
+ }
177
+ }
178
+
179
+ public get useSoftkeyboard(): boolean {
180
+ return this._useSoftkeyboard;
181
+ }
182
+
183
+ public dispose() {
184
+ //unregister stage
185
+ const container = this._stage.container;
186
+ container.removeEventListener('dblclick', this.onDoubleClick);
187
+ container.removeEventListener('touchstart', this.onMouseDown);
188
+ container.removeEventListener('mousedown', this.onMouseDown);
189
+ window.removeEventListener('touchmove', this.onMouseMove);
190
+ window.removeEventListener('mousemove', this.onMouseMove);
191
+ window.removeEventListener('mouseup', this.onMouseUp);
192
+ container.removeEventListener('touchend', this.onMouseUp);
193
+ container.removeEventListener('wheel', this.onMouseWheel);
194
+ container.removeEventListener('mouseover', this.onMouseOver);
195
+ container.removeEventListener('mouseout', this.onMouseOut);
196
+ window.removeEventListener('keydown', this.onKeyDown);
197
+ window.removeEventListener('keyup', this.onKeyUp);
198
+
199
+ this._stage = null;
200
+
201
+ this._dragOut = null;
202
+ this._dragOver = null;
203
+ this._dragMove = null;
204
+ this._dragStart = null;
205
+ this._dragStop = null;
206
+
207
+ }
208
+
209
+ public static clearInstance(stage: Stage) {
210
+ if (this._instancePool[stage.id]) {
211
+ this._instancePool[stage.id].dispose();
212
+ delete this._instancePool[stage.id];
213
+ }
214
+ }
215
+
216
+ public static getInstance(stage: Stage): MouseManager {
217
+ return this._instancePool[stage.id]
218
+ || (this._instancePool[stage.id] = new MouseManager(stage));
219
+
220
+ }
221
+
222
+ public setFocus(node: ContainerNode) {
223
+ if (this._focusNode == node)
224
+ return;
225
+
226
+ // in FP6, a mouseclick on non focus-able object still steal the focus
227
+ // in newer FP they only steal the focus if the the new hit is focusable
228
+ if (this._allowFocusOnUnfocusable || this._focusNode.container.tabEnabled) {
229
+ if (this._focusNode) {
230
+ this.dispatchEvent(this._focusOut, this._focusNode);
231
+ this._focusNode.container.setFocus(false, true);
232
+ }
233
+
234
+ this._focusNode = node;
235
+
236
+ if (this._focusNode) {
237
+ this.dispatchEvent(this._focusIn, this._focusNode);
238
+ this._focusNode.container.setFocus(true, true);
239
+ }
240
+ }
241
+ }
242
+
243
+ public getFocus() {
244
+ return this._focusNode;
245
+ }
246
+
247
+ private dispatchEvent(event: PointerEvent | FocusEvent, dispatcher: ContainerNode) {
248
+ if (!this._eventBubbling) {
249
+ if (dispatcher) {
250
+ event._dispatchEvent(dispatcher, dispatcher.container);
251
+ }
252
+ return;
253
+ }
254
+
255
+ const target = dispatcher?.container;
256
+
257
+ while (dispatcher) {
258
+ if (event.commonAncestor && dispatcher == event.commonAncestor) {
259
+ return;
260
+ }
261
+ event._dispatchEvent(dispatcher, target);
262
+
263
+ if (!event._iAllowedToPropagate) {
264
+ dispatcher = null;
265
+ } else {
266
+ dispatcher = dispatcher.parent;
267
+ }
268
+ }
269
+ }
270
+
271
+ private setupAndDispatchEvent(event: PointerEvent, sourceEvent,
272
+ collision: PickingCollision, commonAncestor: ContainerNode = null) {
273
+
274
+ if (sourceEvent) {
275
+ if (event instanceof AwayMouseEvent) {
276
+ event.delta = sourceEvent.wheelDelta;
277
+ event.buttons = sourceEvent.buttons;
278
+ }
279
+ event.ctrlKey = sourceEvent.ctrlKey;
280
+ event.altKey = sourceEvent.altKey;
281
+ event.shiftKey = sourceEvent.shiftKey;
282
+ }
283
+
284
+ event = this.setUpEvent(event, collision, commonAncestor);
285
+ this.dispatchEvent(event, event.rootNode);
286
+ }
287
+
288
+ public fireMouseEvents(forcePicker: RaycastPicker = null): void {
289
+ //if theres nothing to update, return
290
+ if (!forcePicker && !this._updateDirty)
291
+ return;
292
+
293
+ for (const key in this._pointerDataArray) {
294
+ this._fireMouseEventsInternal(this._pointerDataArray[key], forcePicker);
295
+ }
296
+ }
297
+
298
+ private _fireMouseEventsInternal(pointerData: PointerData, forcePicker: RaycastPicker = null): void {
299
+ let collision: PickingCollision;
300
+ if (forcePicker) {
301
+ collision = forcePicker.getViewCollision(pointerData.screenX, pointerData.screenY);
302
+ } else if (this._updateDirty) {
303
+ for (let i = 0; i < this._pickerLookup.length; i++)
304
+ if (!collision || this._pickerLookup[i].layeredView)
305
+ collision = this._pickerLookup[i].getViewCollision(pointerData.screenX,
306
+ pointerData.screenY, false, collision);
307
+ }
308
+
309
+ let event: PointerEvent;
310
+ let dispatcher: ContainerNode;
311
+
312
+ const len = pointerData.queuedEvents.length;
313
+ for (let i = 0; i < len; ++i) {
314
+ event = pointerData.queuedEvents[i];
315
+ dispatcher = collision?.rootNode;
316
+
317
+ this.setUpEvent(event, collision);
318
+
319
+ if (event.type == pointerData.down.type) {
320
+ this._mouseDragging = true;
321
+
322
+ // no event-bubbling. dispatch on stage first
323
+ if (!this._eventBubbling)
324
+ this._stage.dispatchEvent(event);
325
+
326
+ // @todo: at this point the object under the mouse might have been changed,
327
+ // so we need to recheck the collision ?
328
+
329
+ // on Touch dispatch mouseOver Command
330
+ if (!pointerData.isMouse)
331
+ this.setupAndDispatchEvent(pointerData.over, pointerData.sourceEvent, collision);
332
+
333
+ this._mouseDragCollision = collision;
334
+
335
+ if (dispatcher) {
336
+ //console.log("onPress", dispatcher)
337
+ this.dispatchEvent(event, dispatcher);
338
+ } else if (this._eventBubbling)
339
+ this._stage.dispatchEvent(event);
340
+
341
+ if (this._mouseDragCollision) {
342
+ this.setFocus(this._mouseDragCollision?.rootNode);
343
+ this.setupAndDispatchEvent(this._dragStart, event, this._mouseDragCollision);
344
+ }
345
+
346
+ } else if (event.type == pointerData.up.type) {
347
+
348
+ // no event-bubbling. dispatch on stage first
349
+ if (!this._eventBubbling)
350
+ this._stage.dispatchEvent(event);
351
+
352
+ // @todo: at this point the object under the mouse might have been changed,
353
+ // so we need to recheck the collision ?
354
+
355
+ let upContainerNode: ContainerNode = null;
356
+ let upRootNode: ContainerNode = null;
357
+ if (this._isAVM1Dragging && this._mouseDragCollision) {
358
+ // avm1dragging is in process, dispatch the mouse-up on this.
359
+ // mouseDragEntity instead of the current collision
360
+ upRootNode = this._mouseDragCollision.rootNode;
361
+ upContainerNode = this._mouseDragCollision.containerNode;
362
+ } else if (this._mouseDragging && this._mouseDragCollision
363
+ && this._mouseDragCollision.rootNode != dispatcher) {
364
+ // no avm1dragging is in process, but current collision
365
+ // is not the same as collision that appeared on mouse-down,
366
+ // need to dispatch a MOUSE_UP_OUTSIDE after mouse up
367
+ } else if (this._mouseDragging && this._mouseDragCollision
368
+ && this._mouseDragCollision.rootNode == dispatcher) {
369
+ // no avm1dragging is in process,
370
+ // current collision is the same as collision that appeared on
371
+ // mouse-down, need to dispatch a MOUSE_CLICK event after mouse up
372
+ upRootNode = this._mouseDragCollision.rootNode;
373
+ upContainerNode = this._mouseDragCollision.containerNode;
374
+ }
375
+
376
+ if (upRootNode) {
377
+ //console.log("onRelease", upRootNode)
378
+ this.dispatchEvent(event, upRootNode);
379
+ } else if (this._eventBubbling && dispatcher)
380
+ this.dispatchEvent(event, dispatcher);
381
+ else if (this._eventBubbling)
382
+ this._stage.dispatchEvent(event);
383
+
384
+ if (upContainerNode) {
385
+ if (!this._isAVM1Dragging)
386
+ this.setupAndDispatchEvent(pointerData.click, pointerData.isMouse ? event : pointerData.sourceEvent, this._mouseDragCollision);
387
+ this.setupAndDispatchEvent(this._dragStop, event, this._mouseDragCollision);
388
+ } else if (this._mouseDragging && this._mouseDragCollision) {
389
+ this.setupAndDispatchEvent(pointerData.upOutside, event, this._mouseDragCollision);
390
+ }
391
+
392
+ this._mouseDragCollision = null;
393
+ this._mouseDragging = false;
394
+ this._isAVM1Dragging = false;
395
+
396
+ } else if (event.type == pointerData.move.type) {
397
+
398
+ // no event-bubbling. dispatch on stage first
399
+ if (!this._eventBubbling) {
400
+ this._stage.dispatchEvent(event);
401
+ }
402
+
403
+ // fire to picker
404
+ if (event.rootNode) {
405
+ this.dispatchEvent(event, event.rootNode);
406
+ }
407
+
408
+ if (this._mouseDragCollision) {
409
+ this.setupAndDispatchEvent(this._dragMove, event, this._mouseDragCollision);
410
+ }
411
+
412
+ // if bubling is exist, fire to stage late
413
+ if (this._eventBubbling) {
414
+ this._stage.dispatchEvent(event);
415
+ }
416
+
417
+ } else {
418
+ // MouseEvent.MOUSE_OVER / MouseEvent.MOUSE_OUT / MouseEvent.DRAG_OVER / MouseEvent.DRAG_OUT
419
+ this.dispatchEvent(event, dispatcher);
420
+ }
421
+ }
422
+
423
+ pointerData.queuedEvents.length = 0;
424
+
425
+ pointerData.rollOut.commonAncestor = null;
426
+ pointerData.rollOver.commonAncestor = null;
427
+
428
+ const collisionNode: ContainerNode = collision?.rootNode;
429
+ const prevCollisionNode: ContainerNode = pointerData.prevCollision?.rootNode;
430
+
431
+ if (collisionNode != prevCollisionNode) {
432
+
433
+ // If colliding object has changed, queue OVER and OUT events.
434
+ // If the mouse is dragged (mouse-down is hold), use DRAG_OVER and DRAG_OUT as well as MOUSE_OVER MOUSE_OUT
435
+ // DRAG_OVER and DRAG_OUT are only dispatched on the object that was hit on the mouse-down
436
+ // (_mouseDragEntity)
437
+
438
+ // Store the info if the collision is a enabled Button (_collisionIsEnabledButton)
439
+
440
+ if (prevCollisionNode) {
441
+ this.setupAndDispatchEvent(pointerData.out, pointerData.sourceEvent, pointerData.prevCollision);
442
+
443
+ if (this._mouseDragging && this._mouseDragCollision
444
+ && this._mouseDragCollision.rootNode == prevCollisionNode)
445
+ this.setupAndDispatchEvent(this._dragOut, pointerData.sourceEvent, pointerData.prevCollision);
446
+ }
447
+
448
+ if (!prevCollisionNode && collisionNode) {
449
+ // rollout / rollover easy case, can just bubble up
450
+ this.setupAndDispatchEvent(pointerData.rollOut, pointerData.sourceEvent, pointerData.prevCollision);
451
+ this.setupAndDispatchEvent(pointerData.rollOver, pointerData.sourceEvent, collision);
452
+ }
453
+ if (prevCollisionNode && !collisionNode) {
454
+ // rollout / rollover easy case, can just bubble up
455
+ this.setupAndDispatchEvent(pointerData.rollOut, pointerData.sourceEvent, pointerData.prevCollision);
456
+ this.setupAndDispatchEvent(pointerData.rollOver, pointerData.sourceEvent, collision);
457
+ }
458
+ if (prevCollisionNode && collisionNode) {
459
+ // rollout / rollover find common ancester and only bubble up to that point
460
+ const parentsPrev: ContainerNode[] = [];
461
+ let parent: ContainerNode = prevCollisionNode;
462
+ while (parent && !parent.container.isAVMScene) {
463
+ parentsPrev.push(parent);
464
+ parent = parent.parent;
465
+ }
466
+ let commonAncestor: ContainerNode = null;
467
+ parent = collisionNode;
468
+ while (parent && !parent.container.isAVMScene) {
469
+ const oldParentIdx = parentsPrev.indexOf(parent);
470
+ if (oldParentIdx == -1) {
471
+ parent = parent.parent;
472
+ } else {
473
+ commonAncestor = parent;
474
+ parent = null;
475
+ }
476
+ }
477
+ if (commonAncestor != prevCollisionNode)
478
+ this.setupAndDispatchEvent(pointerData.rollOut, pointerData.sourceEvent,
479
+ pointerData.prevCollision, commonAncestor);
480
+
481
+ if (commonAncestor != collisionNode)
482
+ this.setupAndDispatchEvent(pointerData.rollOver, pointerData.sourceEvent, collision, commonAncestor);
483
+
484
+ }
485
+
486
+ pointerData.collisionIsEnabledButton = collisionNode ? (<any> collisionNode).buttonEnabled : false;
487
+
488
+ if (collisionNode) {
489
+ this.setupAndDispatchEvent(pointerData.over, pointerData.sourceEvent, collision);
490
+
491
+ if (this._mouseDragging)
492
+ this.setupAndDispatchEvent(this._dragOver, pointerData.sourceEvent, collision);
493
+ }
494
+
495
+ pointerData.prevCollision = collision;
496
+ } else {
497
+ // colliding object has not changed
498
+ // Check if we need to send any MOUSE_OVER/DRAG_OVER event to handle the case
499
+ // when a Button has become active while under the mouse.
500
+ const isActiveButton = collisionNode ? (<any> collisionNode).buttonEnabled : false;
501
+
502
+ if (pointerData.collisionIsEnabledButton != isActiveButton && isActiveButton) {
503
+ if (pointerData.isMouse)
504
+ this.setupAndDispatchEvent(pointerData.over, pointerData.sourceEvent, collision);
505
+ else if (this._mouseDragCollision && this._mouseDragCollision.rootNode == collisionNode)
506
+ this.setupAndDispatchEvent(this._dragOver, pointerData.sourceEvent, collision);
507
+ }
508
+
509
+ pointerData.collisionIsEnabledButton = isActiveButton;
510
+ }
511
+
512
+ // set cursor if not dragging mouse ???
513
+ if (!this._mouseDragging) {
514
+ this.cursorType = collisionNode ? <any>collisionNode.container.getMouseCursor() : 'auto';
515
+ }
516
+
517
+ this._updateDirty = false;
518
+ }
519
+
520
+ public registerPicker(picker: RaycastPicker): void {
521
+ this._pickerLookup.push(picker);
522
+ }
523
+
524
+ public unregisterPicker(picker: RaycastPicker): void {
525
+ this._pickerLookup.splice(this._pickerLookup.indexOf(picker), 1);
526
+ }
527
+
528
+ public addEventsForSceneBinary(touchMessage: ArrayBuffer, sceneIdx: number = 0): void {
529
+
530
+ const newTouchEvent: any = {};
531
+ newTouchEvent.clientX = null;// we get the x position from the active touch
532
+ newTouchEvent.clientY = null;// we get the y position from the active touch
533
+ newTouchEvent.touches = [];
534
+ newTouchEvent.changedTouches = [];
535
+ newTouchEvent.preventDefault = function () { };
536
+ const messageScene: Float32Array = new Float32Array(touchMessage);
537
+ // transfer touches to event
538
+ let i = 0;
539
+ let cnt = 0;
540
+ const touchCnt = 0;
541
+ cnt++;//we temporary added 1 float to transfer fps from java to js. skip this
542
+ const numTouches = messageScene[cnt++];
543
+ const touchtype = messageScene[cnt++];
544
+ const activeTouchID = messageScene[cnt++];
545
+
546
+ if ((touchtype != 1) && (touchtype != 6) && (touchtype != 12) && (touchtype != 262) && (touchtype != 518)) {
547
+ // if this is not a UP command, we add all touches
548
+ for (i = 0; i < numTouches; i++) {
549
+ const newTouch: any = {};
550
+ newTouch.identifier = messageScene[cnt++];
551
+ newTouch.clientX = messageScene[cnt++];
552
+ newTouch.clientY = messageScene[cnt++];
553
+ newTouchEvent.touches[i] = newTouch;
554
+ //newTouchEvent.changedTouches[i] = newTouch;
555
+ }
556
+ newTouchEvent.changedTouches[0] = newTouchEvent.touches[activeTouchID];
557
+
558
+ } else {
559
+ // if this is a UP command, we add all touches, except the active one
560
+ if (numTouches == 1) {
561
+
562
+ const newTouch: any = {};
563
+ newTouch.identifier = messageScene[cnt++];
564
+ newTouch.clientX = messageScene[cnt++];
565
+ newTouch.clientY = messageScene[cnt++];
566
+ newTouchEvent.clientX = newTouch.clientX;
567
+ newTouchEvent.clientY = newTouch.clientY;
568
+
569
+ } else {
570
+ for (i = 0; i < numTouches; i++) {
571
+ const newTouch: any = {};
572
+ newTouch.identifier = messageScene[cnt++];
573
+ newTouch.clientX = messageScene[cnt++];
574
+ newTouch.clientY = messageScene[cnt++];
575
+ if (i != activeTouchID) {
576
+ newTouchEvent.touches[touchCnt] = newTouch;
577
+ //newTouchEvent.changedTouches[touchCnt++] = newTouch;
578
+ } else {
579
+ newTouchEvent.clientX = newTouch.clientX;
580
+ newTouchEvent.clientY = newTouch.clientY;
581
+ }
582
+ }
583
+ }
584
+ }
585
+
586
+ //console.log("Touch ID:"+touchtype+" activeTouchID "+activeTouchID+" numTouches "+numTouches+" x"+x+" y"+y);
587
+ /*
588
+ public static final int ACTION_DOWN = 0;
589
+ public static final int ACTION_POINTER_1_DOWN = 5;
590
+ public static final int ACTION_POINTER_DOWN = 5;
591
+ public static final int ACTION_BUTTON_PRESS = 11;
592
+ public static final int ACTION_POINTER_2_DOWN = 261;
593
+ public static final int ACTION_POINTER_3_DOWN = 517;
594
+
595
+ public static final int ACTION_UP = 1;
596
+ public static final int ACTION_POINTER_1_UP = 6;
597
+ public static final int ACTION_POINTER_UP = 6;
598
+ public static final int ACTION_BUTTON_RELEASE = 12;
599
+ public static final int ACTION_POINTER_2_UP = 262;
600
+ public static final int ACTION_POINTER_3_UP = 518;
601
+
602
+ public static final int ACTION_MOVE = 2;
603
+
604
+ */
605
+ if ((touchtype == 0) || (touchtype == 5) || (touchtype == 11) || (touchtype == 261) || (touchtype == 517)) {
606
+ this.onMouseDown(newTouchEvent);
607
+ } else if ((touchtype == 1) || (touchtype == 6)
608
+ || (touchtype == 12) || (touchtype == 262) || (touchtype == 518)) {
609
+ this.onMouseUp(newTouchEvent);
610
+ } else if (touchtype == 2) {
611
+ this.onMouseMove(newTouchEvent);
612
+ } else {
613
+ console.log('recieved unknown touch event-type: ' + touchtype);
614
+ }
615
+ }
616
+
617
+ public fireEventsForSceneFromString(touchMessage: String, _sceneIdx: number = 0): void {
618
+
619
+ const newTouchEvent: any = {};
620
+ newTouchEvent.clientX = null;// set the x position from the active touch
621
+ newTouchEvent.clientY = null;// set the y position from the active touch
622
+ newTouchEvent.preventDefault = function () { };
623
+ const touchesFromMessage = touchMessage.split(',');
624
+ // transfer touches to event
625
+ let i = 0;
626
+ let cnt = 0;
627
+ const numTouches = parseInt(touchesFromMessage[cnt++]);
628
+ const touchtype = parseInt(touchesFromMessage[cnt++]);
629
+ const activeTouch = parseInt(touchesFromMessage[cnt++]);
630
+ newTouchEvent.touches = [];
631
+ newTouchEvent.changedTouches = [];
632
+ if ((touchtype != 1) && (touchtype != 6)) {
633
+ for (i = 0; i < numTouches; i++) {
634
+ const newTouch: any = {};
635
+ newTouch.identifier = touchesFromMessage[cnt++];
636
+ newTouch.clientX = touchesFromMessage[cnt++];
637
+ newTouch.clientY = touchesFromMessage[cnt++];
638
+ newTouchEvent.touches[i] = newTouch;
639
+ newTouchEvent.changedTouches[i] = newTouch;
640
+ }
641
+ newTouchEvent.changedTouches[i] = newTouchEvent.touches[activeTouch];
642
+ } else {
643
+ for (i = 0; i < numTouches; i++) {
644
+ if (i != activeTouch) {
645
+ const newTouch: any = {};
646
+ newTouch.identifier = touchesFromMessage[cnt++];
647
+ newTouch.clientX = touchesFromMessage[cnt++];
648
+ newTouch.clientY = touchesFromMessage[cnt++];
649
+ newTouchEvent.touches[i] = newTouch;
650
+ newTouchEvent.changedTouches[i] = newTouch;
651
+ } else {
652
+ newTouchEvent.clientX = touchesFromMessage[cnt++];
653
+ newTouchEvent.clientY = touchesFromMessage[cnt++];
654
+ cnt++;
655
+ }
656
+ }
657
+
658
+ }
659
+ if (touchtype == 0) {//mousedown
660
+ this.onMouseDown(newTouchEvent);
661
+ } else if (touchtype == 1) {//mouseup
662
+ this.onMouseUp(newTouchEvent);
663
+ } else if (touchtype == 2) {//mousemove
664
+ this.onMouseMove(newTouchEvent);
665
+
666
+ } else if (touchtype == 261) {//mousedownPointer
667
+ this.onMouseDown(newTouchEvent);
668
+
669
+ } else if (touchtype == 6) {//mouseupPointer
670
+ this.onMouseUp(newTouchEvent);
671
+ }
672
+ }
673
+
674
+ // ---------------------------------------------------------------------
675
+ // Private.
676
+ // ---------------------------------------------------------------------
677
+ private setUpEvent(event: PointerEvent,
678
+ collision: PickingCollision, commonAncestor: ContainerNode = null): PointerEvent {
679
+ event._iAllowedToImmediatlyPropagate = true;
680
+ event._iAllowedToPropagate = true;
681
+ // 2D properties.
682
+ event.screenX = this._stage.screenX;
683
+ event.screenY = this._stage.screenY;
684
+ //console.log("event", event, collisionNode, collisionNode);
685
+
686
+ // 3D properties.
687
+ if (collision) {
688
+ event.containerNode = collision.containerNode;
689
+
690
+ event.rootNode = collision.rootNode;
691
+ // Object.
692
+ event.traversable = collision.traversable;
693
+ // UV.
694
+ event.uv = collision.uv;
695
+ // Position.
696
+ event.position = collision.position?.clone();
697
+ // Normal.
698
+ event.normal = collision.normal?.clone();
699
+ // Face index.
700
+ event.elementIndex = collision.elementIndex;
701
+ } else {
702
+ // Set all to null.
703
+ event.containerNode = null;
704
+ event.rootNode = null;
705
+ event.traversable = null;
706
+ event.uv = null;
707
+ event.position = this._nullVector;
708
+ event.normal = this._nullVector;
709
+ event.elementIndex = 0;
710
+ }
711
+ event.commonAncestor = commonAncestor;
712
+
713
+ return event;
714
+ }
715
+
716
+ private queueDispatch(pointerData: PointerData, event: PointerEvent, sourceEvent: MouseEvent | TouchEvent | WheelEvent): void {
717
+ // Store event to be dispatched later.
718
+ if (event instanceof AwayMouseEvent) {
719
+ event.delta = (sourceEvent instanceof WheelEvent) ? -3 * sourceEvent.deltaY / 100 : 0;
720
+ event.buttons = (sourceEvent instanceof MouseEvent) ? <MouseButtons> sourceEvent.buttons : MouseButtons.NO_BUTTON;
721
+ }
722
+ event.ctrlKey = sourceEvent.ctrlKey;
723
+ event.altKey = sourceEvent.altKey;
724
+ event.shiftKey = sourceEvent.shiftKey;
725
+
726
+ pointerData.queuedEvents.push(event);
727
+
728
+ pointerData.sourceEvent = sourceEvent;
729
+ }
730
+
731
+ // ---------------------------------------------------------------------
732
+ // Listeners.
733
+ // ---------------------------------------------------------------------
734
+
735
+ public onKeyDown(event): void {
736
+ !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
737
+
738
+ //console.log("Keydown", event);
739
+ if (this.allowKeyInput) {
740
+ switch (event.keyCode) {
741
+ case 32: // spacebar
742
+ case 37: // left
743
+ case 39: // right
744
+ case 38: // up
745
+ case 40: // down
746
+ event.preventDefault();
747
+ break;
748
+ default:
749
+ break;
750
+ }
751
+ if (this._focusNode || this._stage) {
752
+ //console.log("dispatch keydown on ", this._focusNode);
753
+ const newEvent: KeyboardEvent = new KeyboardEvent(KeyboardEvent.KEYDOWN, event.key, event.code);
754
+ newEvent.isShift = event.shiftKey;
755
+ newEvent.isCTRL = event.ctrlKey;
756
+ newEvent.isAlt = event.altKey;
757
+ (<any>newEvent).keyCode = event.keyCode;
758
+ if (this._focusNode)
759
+ this._focusNode.container.dispatchEvent(newEvent);
760
+ if (this._stage)
761
+ this._stage.dispatchEvent(newEvent);
762
+ }
763
+ }
764
+
765
+ }
766
+
767
+ public onKeyUp(event): void {
768
+ !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
769
+
770
+ //console.log("Keyup", event);
771
+ if (this.allowKeyInput) {
772
+ switch (event.keyCode) {
773
+ case 32: // spacebar
774
+ case 37: // left
775
+ case 39: // right
776
+ case 38: // up
777
+ case 40: // down
778
+ event.preventDefault();
779
+ break;
780
+ default:
781
+ break;
782
+ }
783
+
784
+ if (this._focusNode || this._stage) {
785
+ //console.log("dispatch keydown on ", this._focusNode);
786
+ const newEvent: KeyboardEvent = new KeyboardEvent(KeyboardEvent.KEYUP, event.key, event.code);
787
+ newEvent.isShift = event.shiftKey;
788
+ newEvent.isCTRL = event.ctrlKey;
789
+ newEvent.isAlt = event.altKey;
790
+ (<any>newEvent).keyCode = event.keyCode;
791
+ if (this._focusNode)
792
+ this._focusNode.container.dispatchEvent(newEvent);
793
+ if (this._stage)
794
+ this._stage.dispatchEvent(newEvent);
795
+ }
796
+ }
797
+
798
+ }
799
+
800
+ public onMouseMove(event: MouseEvent | TouchEvent): void {
801
+ !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
802
+ this.updateColliders(event);
803
+ }
804
+
805
+ public onMouseOut(event: MouseEvent): void {
806
+ !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
807
+ this.updateColliders(event);
808
+ }
809
+
810
+ public onMouseOver(event: MouseEvent): void {
811
+ !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
812
+ this.updateColliders(event);
813
+ }
814
+
815
+ public onDoubleClick(event: MouseEvent): void {
816
+ !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
817
+ this.updateColliders(event);
818
+
819
+ // prevent middle and second button
820
+ if (event instanceof MouseEvent && event.buttons & 0b110) {
821
+ event.preventDefault();
822
+ }
823
+ }
824
+
825
+ public onMouseDown(event: MouseEvent | TouchEvent): void {
826
+
827
+ !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
828
+ this.updateColliders(event);
829
+
830
+ let prevent = (event.type !== 'mousedown');
831
+
832
+ // prevent middle and second button
833
+ if (event instanceof MouseEvent && event.buttons & 0b110) {
834
+ prevent = true;
835
+ }
836
+
837
+ if (prevent) {
838
+ event.preventDefault();
839
+ this._stage.container.focus();
840
+ }
841
+ }
842
+
843
+ public onMouseUp(event: MouseEvent | TouchEvent): void {
844
+
845
+ !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
846
+
847
+ this.updateColliders(event);
848
+
849
+ let prevent = event.type !== 'mouseup';
850
+ // prevent middle and second button
851
+ if (event instanceof MouseEvent && event.button === 1) {
852
+ prevent = true;
853
+ }
854
+
855
+ if (prevent) {
856
+ event.preventDefault();
857
+ this._stage.container.focus();
858
+ }
859
+ }
860
+
861
+ public onMouseWheel(event: WheelEvent): void {
862
+ !MouseManager.inputRecorder || MouseManager.inputRecorder.recordEvent(event);
863
+ this.updateColliders(event);
864
+ }
865
+
866
+ private updateColliders(event: MouseEvent | TouchEvent): void {
867
+ this._stage.interactionHandler(event);
868
+
869
+ this._updateDirty = true;
870
+
871
+ const mouseData = this._pointerDataArray[0] || (this._pointerDataArray[0] = new PointerData(0, true));
872
+
873
+ const type = (event instanceof MouseEvent) ? event.type : MouseManager._touchToMouseDict[event.type];
874
+ this.queueDispatch(mouseData, MouseManager._pointerDict[type], event);
875
+
876
+ mouseData.screenX = this._stage.screenX;
877
+ mouseData.screenY = this._stage.screenY;
878
+
879
+ const touchPoints = this._stage.touchPoints;
880
+ for (const touchPoint of touchPoints) {
881
+ const touchData = this._pointerDataArray[touchPoint.id] || (this._pointerDataArray[touchPoint.id] = new PointerData(touchPoint.id));
882
+ touchData.screenX = touchPoint.x;
883
+ touchData.screenY = touchPoint.y;
884
+ }
885
+
886
+ if ((<TouchEvent> event).changedTouches) {
887
+ for (let i: number = 0; i < (<TouchEvent> event).changedTouches.length; i++) {
888
+ const touch = (<TouchEvent> event).changedTouches[i];
889
+ const touchData = this._pointerDataArray[touch.identifier];
890
+ this.queueDispatch(touchData, MouseManager._pointerDict[event.type], event);
891
+ }
892
+ }
893
+ }
894
+ }
895
+
896
+ class PointerData {
897
+
898
+ public up: PointerEvent;
899
+ public upOutside: PointerEvent;
900
+ public click: PointerEvent;
901
+ public out: PointerEvent;
902
+ public down: PointerEvent;
903
+ public move: PointerEvent;
904
+ public over: PointerEvent;
905
+ public wheel: PointerEvent;
906
+ public doubleClick: PointerEvent;
907
+ public rollOver: PointerEvent;
908
+ public rollOut: PointerEvent;
909
+
910
+ constructor(public id: number, public isMouse: boolean = false) {
911
+ if (isMouse) {
912
+ this.up = MouseManager._pointerDict.mouseup;
913
+ this.upOutside = MouseManager._pointerDict.mouseupoutside;
914
+ this.click = MouseManager._pointerDict.click;
915
+ this.out = MouseManager._pointerDict.mouseout;
916
+ this.down = MouseManager._pointerDict.mousedown;
917
+ this.move = MouseManager._pointerDict.mousemove;
918
+ this.over = MouseManager._pointerDict.mouseover;
919
+ this.wheel = MouseManager._pointerDict.mousewheel;
920
+ this.doubleClick = MouseManager._pointerDict.dblclick;
921
+ this.rollOver = MouseManager._pointerDict.rollover;
922
+ this.rollOut = MouseManager._pointerDict.rollout;
923
+ } else {
924
+ this.up = MouseManager._pointerDict.touchend;
925
+ this.click = MouseManager._pointerDict.touchtap;
926
+ this.out = MouseManager._pointerDict.touchout;
927
+ this.down = MouseManager._pointerDict.touchstart;
928
+ this.move = MouseManager._pointerDict.touchmove;
929
+ this.over = MouseManager._pointerDict.touchover;
930
+ this.rollOver = MouseManager._pointerDict.touchrollover;
931
+ this.rollOut = MouseManager._pointerDict.touchrollout;
932
+ }
933
+ }
934
+
935
+ sourceEvent: MouseEvent | TouchEvent;
936
+
937
+ prevCollision: PickingCollision;
938
+
939
+ collisionIsEnabledButton: boolean = false;
940
+
941
+ queuedEvents: Array<PointerEvent> = new Array<PointerEvent>();
942
+
943
+ screenX: number;
944
+ screenY: number;
885
945
  }