control3D.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 http://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 http://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 http://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 http://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._node!.metadata = 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. /**
  229. * Node creation.
  230. * Can be overriden by children
  231. * @param scene defines the scene where the node must be attached
  232. * @returns the attached node or null if none. Must return a Mesh or AbstractMesh if there is an atttached visible object
  233. */
  234. protected _createNode(scene: Scene): Nullable<TransformNode> {
  235. // Do nothing by default
  236. return null;
  237. }
  238. /**
  239. * Affect a material to the given mesh
  240. * @param mesh defines the mesh which will represent the control
  241. */
  242. protected _affectMaterial(mesh: AbstractMesh) {
  243. mesh.material = null;
  244. }
  245. // Pointers
  246. /** @hidden */
  247. public _onPointerMove(target: Control3D, coordinates: Vector3): void {
  248. this.onPointerMoveObservable.notifyObservers(coordinates, -1, target, this);
  249. }
  250. /** @hidden */
  251. public _onPointerEnter(target: Control3D): boolean {
  252. if (this._enterCount > 0) {
  253. return false;
  254. }
  255. if (this._enterCount === -1) { // -1 is for touch input, we are now sure we are with a mouse or pencil
  256. this._enterCount = 0;
  257. }
  258. this._enterCount++;
  259. this.onPointerEnterObservable.notifyObservers(this, -1, target, this);
  260. if (this.pointerEnterAnimation) {
  261. this.pointerEnterAnimation();
  262. }
  263. return true;
  264. }
  265. /** @hidden */
  266. public _onPointerOut(target: Control3D): void {
  267. this._enterCount = 0;
  268. this.onPointerOutObservable.notifyObservers(this, -1, target, this);
  269. if (this.pointerOutAnimation) {
  270. this.pointerOutAnimation();
  271. }
  272. }
  273. /** @hidden */
  274. public _onPointerDown(target: Control3D, coordinates: Vector3, pointerId: number, buttonIndex: number): boolean {
  275. if (this._downCount !== 0) {
  276. this._downCount++;
  277. return false;
  278. }
  279. this._downCount++;
  280. this._downPointerIds[pointerId] = true;
  281. this.onPointerDownObservable.notifyObservers(new Vector3WithInfo(coordinates, buttonIndex), -1, target, this);
  282. if (this.pointerDownAnimation) {
  283. this.pointerDownAnimation();
  284. }
  285. return true;
  286. }
  287. /** @hidden */
  288. public _onPointerUp(target: Control3D, coordinates: Vector3, pointerId: number, buttonIndex: number, notifyClick: boolean): void {
  289. this._downCount--;
  290. delete this._downPointerIds[pointerId];
  291. if (this._downCount < 0) {
  292. // Handle if forcePointerUp was called prior to this
  293. this._downCount = 0;
  294. return;
  295. }
  296. if (this._downCount == 0) {
  297. if (notifyClick && (this._enterCount > 0 || this._enterCount === -1)) {
  298. this.onPointerClickObservable.notifyObservers(new Vector3WithInfo(coordinates, buttonIndex), -1, target, this);
  299. }
  300. this.onPointerUpObservable.notifyObservers(new Vector3WithInfo(coordinates, buttonIndex), -1, target, this);
  301. if (this.pointerUpAnimation) {
  302. this.pointerUpAnimation();
  303. }
  304. }
  305. }
  306. /** @hidden */
  307. public forcePointerUp(pointerId: Nullable<number> = null) {
  308. if (pointerId !== null) {
  309. this._onPointerUp(this, Vector3.Zero(), pointerId, 0, true);
  310. } else {
  311. for (var key in this._downPointerIds) {
  312. this._onPointerUp(this, Vector3.Zero(), +key as number, 0, true);
  313. }
  314. if (this._downCount > 0) {
  315. this._downCount = 1;
  316. this._onPointerUp(this, Vector3.Zero(), 0, 0, true);
  317. }
  318. }
  319. }
  320. /** @hidden */
  321. public _processObservables(type: number, pickedPoint: Vector3, pointerId: number, buttonIndex: number): boolean {
  322. if (type === PointerEventTypes.POINTERMOVE) {
  323. this._onPointerMove(this, pickedPoint);
  324. var previousControlOver = this._host._lastControlOver[pointerId];
  325. if (previousControlOver && previousControlOver !== this) {
  326. previousControlOver._onPointerOut(this);
  327. }
  328. if (previousControlOver !== this) {
  329. this._onPointerEnter(this);
  330. }
  331. this._host._lastControlOver[pointerId] = this;
  332. return true;
  333. }
  334. if (type === PointerEventTypes.POINTERDOWN) {
  335. this._onPointerDown(this, pickedPoint, pointerId, buttonIndex);
  336. this._host._lastControlDown[pointerId] = this;
  337. this._host._lastPickedControl = this;
  338. return true;
  339. }
  340. if (type === PointerEventTypes.POINTERUP || type === PointerEventTypes.POINTERDOUBLETAP) {
  341. if (this._host._lastControlDown[pointerId]) {
  342. this._host._lastControlDown[pointerId]._onPointerUp(this, pickedPoint, pointerId, buttonIndex, true);
  343. }
  344. delete this._host._lastControlDown[pointerId];
  345. return true;
  346. }
  347. return false;
  348. }
  349. /** @hidden */
  350. public _disposeNode(): void {
  351. if (this._node) {
  352. this._node.dispose();
  353. this._node = null;
  354. }
  355. }
  356. /**
  357. * Releases all associated resources
  358. */
  359. public dispose() {
  360. this.onPointerDownObservable.clear();
  361. this.onPointerEnterObservable.clear();
  362. this.onPointerMoveObservable.clear();
  363. this.onPointerOutObservable.clear();
  364. this.onPointerUpObservable.clear();
  365. this.onPointerClickObservable.clear();
  366. this._disposeNode();
  367. // Behaviors
  368. for (var behavior of this._behaviors) {
  369. behavior.detach();
  370. }
  371. }
  372. }