control3D.ts 13 KB

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