control3D.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. import { Nullable } from "babylonjs/types";
  2. import { Observable } from "babylonjs/Misc/observable";
  3. import { Vector3 } from "babylonjs/Maths/math.vector";
  4. import { PointerEventTypes } from "babylonjs/Events/pointerEvents";
  5. import { TransformNode } from "babylonjs/Meshes/transformNode";
  6. import { AbstractMesh } from "babylonjs/Meshes/abstractMesh";
  7. import { IBehaviorAware, Behavior } from "babylonjs/Behaviors/behavior";
  8. import { IDisposable, Scene } from "babylonjs/scene";
  9. import { GUI3DManager } from "../gui3DManager";
  10. import { Vector3WithInfo } from "../vector3WithInfo";
  11. import { Container3D } from "./container3D";
  12. /**
  13. * Class used as base class for controls
  14. */
  15. export class Control3D implements IDisposable, IBehaviorAware<Control3D> {
  16. /** @hidden */
  17. public _host: GUI3DManager;
  18. private _node: Nullable<TransformNode>;
  19. private _downCount = 0;
  20. private _enterCount = -1;
  21. private _downPointerIds: { [id: number]: boolean } = {};
  22. private _isVisible = true;
  23. /** Gets or sets the control position in world space */
  24. public get position(): Vector3 {
  25. if (!this._node) {
  26. return Vector3.Zero();
  27. }
  28. return this._node.position;
  29. }
  30. public set position(value: Vector3) {
  31. if (!this._node) {
  32. return;
  33. }
  34. this._node.position = value;
  35. }
  36. /** Gets or sets the control scaling in world space */
  37. public get scaling(): Vector3 {
  38. if (!this._node) {
  39. return new Vector3(1, 1, 1);
  40. }
  41. return this._node.scaling;
  42. }
  43. public set scaling(value: Vector3) {
  44. if (!this._node) {
  45. return;
  46. }
  47. this._node.scaling = value;
  48. }
  49. /** Callback used to start pointer enter animation */
  50. public pointerEnterAnimation: () => void;
  51. /** Callback used to start pointer out animation */
  52. public pointerOutAnimation: () => void;
  53. /** Callback used to start pointer down animation */
  54. public pointerDownAnimation: () => void;
  55. /** Callback used to start pointer up animation */
  56. public pointerUpAnimation: () => void;
  57. /**
  58. * An event triggered when the pointer move over the control
  59. */
  60. public onPointerMoveObservable = new Observable<Vector3>();
  61. /**
  62. * An event triggered when the pointer move out of the control
  63. */
  64. public onPointerOutObservable = new Observable<Control3D>();
  65. /**
  66. * An event triggered when the pointer taps the control
  67. */
  68. public onPointerDownObservable = new Observable<Vector3WithInfo>();
  69. /**
  70. * An event triggered when pointer is up
  71. */
  72. public onPointerUpObservable = new Observable<Vector3WithInfo>();
  73. /**
  74. * An event triggered when a control is clicked on (with a mouse)
  75. */
  76. public onPointerClickObservable = new Observable<Vector3WithInfo>();
  77. /**
  78. * An event triggered when pointer enters the control
  79. */
  80. public onPointerEnterObservable = new Observable<Control3D>();
  81. /**
  82. * Gets or sets the parent container
  83. */
  84. public parent: Nullable<Container3D>;
  85. // Behaviors
  86. private _behaviors = new Array<Behavior<Control3D>>();
  87. /**
  88. * Gets the list of attached behaviors
  89. * @see https://doc.babylonjs.com/features/behaviour
  90. */
  91. public get behaviors(): Behavior<Control3D>[] {
  92. return this._behaviors;
  93. }
  94. /**
  95. * Attach a behavior to the control
  96. * @see https://doc.babylonjs.com/features/behaviour
  97. * @param behavior defines the behavior to attach
  98. * @returns the current control
  99. */
  100. public addBehavior(behavior: Behavior<Control3D>): Control3D {
  101. var index = this._behaviors.indexOf(behavior);
  102. if (index !== -1) {
  103. return this;
  104. }
  105. behavior.init();
  106. let scene = this._host.scene;
  107. if (scene.isLoading) {
  108. // We defer the attach when the scene will be loaded
  109. scene.onDataLoadedObservable.addOnce(() => {
  110. behavior.attach(this);
  111. });
  112. } else {
  113. behavior.attach(this);
  114. }
  115. this._behaviors.push(behavior);
  116. return this;
  117. }
  118. /**
  119. * Remove an attached behavior
  120. * @see https://doc.babylonjs.com/features/behaviour
  121. * @param behavior defines the behavior to attach
  122. * @returns the current control
  123. */
  124. public removeBehavior(behavior: Behavior<Control3D>): Control3D {
  125. var index = this._behaviors.indexOf(behavior);
  126. if (index === -1) {
  127. return this;
  128. }
  129. this._behaviors[index].detach();
  130. this._behaviors.splice(index, 1);
  131. return this;
  132. }
  133. /**
  134. * Gets an attached behavior by name
  135. * @param name defines the name of the behavior to look for
  136. * @see https://doc.babylonjs.com/features/behaviour
  137. * @returns null if behavior was not found else the requested behavior
  138. */
  139. public getBehaviorByName(name: string): Nullable<Behavior<Control3D>> {
  140. for (var behavior of this._behaviors) {
  141. if (behavior.name === name) {
  142. return behavior;
  143. }
  144. }
  145. return null;
  146. }
  147. /** Gets or sets a boolean indicating if the control is visible */
  148. public get isVisible(): boolean {
  149. return this._isVisible;
  150. }
  151. public set isVisible(value: boolean) {
  152. if (this._isVisible === value) {
  153. return;
  154. }
  155. this._isVisible = value;
  156. let mesh = this.mesh;
  157. if (mesh) {
  158. mesh.setEnabled(value);
  159. }
  160. }
  161. /**
  162. * Creates a new control
  163. * @param name defines the control name
  164. */
  165. constructor(
  166. /** Defines the control name */
  167. public name?: string) {
  168. }
  169. /**
  170. * Gets a string representing the class name
  171. */
  172. public get typeName(): string {
  173. return this._getTypeName();
  174. }
  175. /**
  176. * Get the current class name of the control.
  177. * @returns current class name
  178. */
  179. public getClassName(): string {
  180. return this._getTypeName();
  181. }
  182. protected _getTypeName(): string {
  183. return "Control3D";
  184. }
  185. /**
  186. * Gets the transform node used by this control
  187. */
  188. public get node(): Nullable<TransformNode> {
  189. return this._node;
  190. }
  191. /**
  192. * Gets the mesh used to render this control
  193. */
  194. public get mesh(): Nullable<AbstractMesh> {
  195. if (this._node instanceof AbstractMesh) {
  196. return this._node as AbstractMesh;
  197. }
  198. return null;
  199. }
  200. /**
  201. * Link the control as child of the given node
  202. * @param node defines the node to link to. Use null to unlink the control
  203. * @returns the current control
  204. */
  205. public linkToTransformNode(node: Nullable<TransformNode>): Control3D {
  206. if (this._node) {
  207. this._node.parent = node;
  208. }
  209. return this;
  210. }
  211. /** @hidden **/
  212. public _prepareNode(scene: Scene): void {
  213. if (!this._node) {
  214. this._node = this._createNode(scene);
  215. if (!this.node) {
  216. return;
  217. }
  218. this._injectGUI3DMetadata(this._node!).control = this; // Store the control on the metadata field in order to get it when picking
  219. this._node!.position = this.position;
  220. this._node!.scaling = this.scaling;
  221. let mesh = this.mesh;
  222. if (mesh) {
  223. mesh.isPickable = true;
  224. this._affectMaterial(mesh);
  225. }
  226. }
  227. }
  228. protected _injectGUI3DMetadata(node: TransformNode): any {
  229. node.metadata = node.metadata ?? {};
  230. node.metadata.GUI3D = node.metadata.GUI3D ?? {};
  231. return node.metadata.GUI3D;
  232. }
  233. /**
  234. * Node creation.
  235. * Can be overriden by children
  236. * @param scene defines the scene where the node must be attached
  237. * @returns the attached node or null if none. Must return a Mesh or AbstractMesh if there is an atttached visible object
  238. */
  239. protected _createNode(scene: Scene): Nullable<TransformNode> {
  240. // Do nothing by default
  241. return null;
  242. }
  243. /**
  244. * Affect a material to the given mesh
  245. * @param mesh defines the mesh which will represent the control
  246. */
  247. protected _affectMaterial(mesh: AbstractMesh) {
  248. mesh.material = null;
  249. }
  250. // Pointers
  251. /** @hidden */
  252. public _onPointerMove(target: Control3D, coordinates: Vector3): void {
  253. this.onPointerMoveObservable.notifyObservers(coordinates, -1, target, this);
  254. }
  255. /** @hidden */
  256. public _onPointerEnter(target: Control3D): boolean {
  257. if (this._enterCount > 0) {
  258. return false;
  259. }
  260. if (this._enterCount === -1) { // -1 is for touch input, we are now sure we are with a mouse or pencil
  261. this._enterCount = 0;
  262. }
  263. this._enterCount++;
  264. this.onPointerEnterObservable.notifyObservers(this, -1, target, this);
  265. if (this.pointerEnterAnimation) {
  266. this.pointerEnterAnimation();
  267. }
  268. return true;
  269. }
  270. /** @hidden */
  271. public _onPointerOut(target: Control3D): void {
  272. this._enterCount = 0;
  273. this.onPointerOutObservable.notifyObservers(this, -1, target, this);
  274. if (this.pointerOutAnimation) {
  275. this.pointerOutAnimation();
  276. }
  277. }
  278. /** @hidden */
  279. public _onPointerDown(target: Control3D, coordinates: Vector3, pointerId: number, buttonIndex: number): boolean {
  280. if (this._downCount !== 0) {
  281. this._downCount++;
  282. return false;
  283. }
  284. this._downCount++;
  285. this._downPointerIds[pointerId] = true;
  286. this.onPointerDownObservable.notifyObservers(new Vector3WithInfo(coordinates, buttonIndex), -1, target, this);
  287. if (this.pointerDownAnimation) {
  288. this.pointerDownAnimation();
  289. }
  290. return true;
  291. }
  292. /** @hidden */
  293. public _onPointerUp(target: Control3D, coordinates: Vector3, pointerId: number, buttonIndex: number, notifyClick: boolean): void {
  294. this._downCount--;
  295. delete this._downPointerIds[pointerId];
  296. if (this._downCount < 0) {
  297. // Handle if forcePointerUp was called prior to this
  298. this._downCount = 0;
  299. return;
  300. }
  301. if (this._downCount == 0) {
  302. if (notifyClick && (this._enterCount > 0 || this._enterCount === -1)) {
  303. this.onPointerClickObservable.notifyObservers(new Vector3WithInfo(coordinates, buttonIndex), -1, target, this);
  304. }
  305. this.onPointerUpObservable.notifyObservers(new Vector3WithInfo(coordinates, buttonIndex), -1, target, this);
  306. if (this.pointerUpAnimation) {
  307. this.pointerUpAnimation();
  308. }
  309. }
  310. }
  311. /** @hidden */
  312. public forcePointerUp(pointerId: Nullable<number> = null) {
  313. if (pointerId !== null) {
  314. this._onPointerUp(this, Vector3.Zero(), pointerId, 0, true);
  315. } else {
  316. for (var key in this._downPointerIds) {
  317. this._onPointerUp(this, Vector3.Zero(), +key as number, 0, true);
  318. }
  319. if (this._downCount > 0) {
  320. this._downCount = 1;
  321. this._onPointerUp(this, Vector3.Zero(), 0, 0, true);
  322. }
  323. }
  324. }
  325. /** @hidden */
  326. public _processObservables(type: number, pickedPoint: Vector3, pointerId: number, buttonIndex: number): boolean {
  327. if (type === PointerEventTypes.POINTERMOVE) {
  328. this._onPointerMove(this, pickedPoint);
  329. var previousControlOver = this._host._lastControlOver[pointerId];
  330. if (previousControlOver && previousControlOver !== this) {
  331. previousControlOver._onPointerOut(this);
  332. }
  333. if (previousControlOver !== this) {
  334. this._onPointerEnter(this);
  335. }
  336. this._host._lastControlOver[pointerId] = this;
  337. return true;
  338. }
  339. if (type === PointerEventTypes.POINTERDOWN) {
  340. this._onPointerDown(this, pickedPoint, pointerId, buttonIndex);
  341. this._host._lastControlDown[pointerId] = this;
  342. this._host._lastPickedControl = this;
  343. return true;
  344. }
  345. if (type === PointerEventTypes.POINTERUP || type === PointerEventTypes.POINTERDOUBLETAP) {
  346. if (this._host._lastControlDown[pointerId]) {
  347. this._host._lastControlDown[pointerId]._onPointerUp(this, pickedPoint, pointerId, buttonIndex, true);
  348. }
  349. delete this._host._lastControlDown[pointerId];
  350. return true;
  351. }
  352. return false;
  353. }
  354. /** @hidden */
  355. public _disposeNode(): void {
  356. if (this._node) {
  357. this._node.dispose();
  358. this._node = null;
  359. }
  360. }
  361. /**
  362. * Releases all associated resources
  363. */
  364. public dispose() {
  365. this.onPointerDownObservable.clear();
  366. this.onPointerEnterObservable.clear();
  367. this.onPointerMoveObservable.clear();
  368. this.onPointerOutObservable.clear();
  369. this.onPointerUpObservable.clear();
  370. this.onPointerClickObservable.clear();
  371. this._disposeNode();
  372. // Behaviors
  373. for (var behavior of this._behaviors) {
  374. behavior.detach();
  375. }
  376. }
  377. }