babylon.actionManager.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. module BABYLON {
  2. /**
  3. * ActionEvent is the event beint sent when an action is triggered.
  4. */
  5. export class ActionEvent {
  6. /**
  7. * @constructor
  8. * @param source The mesh that triggered the action.
  9. * @param pointerX the X mouse cursor position at the time of the event
  10. * @param pointerY the Y mouse cursor position at the time of the event
  11. * @param meshUnderPointer The mesh that is currently pointed at (can be null)
  12. * @param sourceEvent the original (browser) event that triggered the ActionEvent
  13. */
  14. constructor(public source: AbstractMesh, public pointerX: number, public pointerY: number, public meshUnderPointer: AbstractMesh, public sourceEvent?: any) {
  15. }
  16. /**
  17. * Helper function to auto-create an ActionEvent from a source mesh.
  18. * @param source the source mesh that triggered the event
  19. * @param evt {Event} The original (browser) event
  20. */
  21. public static CreateNew(source: AbstractMesh, evt?: Event): ActionEvent {
  22. var scene = source.getScene();
  23. return new ActionEvent(source, scene.pointerX, scene.pointerY, scene.meshUnderPointer, evt);
  24. }
  25. /**
  26. * Helper function to auto-create an ActionEvent from a scene. If triggered by a mesh use ActionEvent.CreateNew
  27. * @param scene the scene where the event occurred
  28. * @param evt {Event} The original (browser) event
  29. */
  30. public static CreateNewFromScene(scene: Scene, evt: Event): ActionEvent {
  31. return new ActionEvent(null, scene.pointerX, scene.pointerY, scene.meshUnderPointer, evt);
  32. }
  33. }
  34. /**
  35. * Action Manager manages all events to be triggered on a given mesh or the global scene.
  36. * A single scene can have many Action Managers to handle predefined actions on specific meshes.
  37. */
  38. export class ActionManager {
  39. // Statics
  40. private static _NothingTrigger = 0;
  41. private static _OnPickTrigger = 1;
  42. private static _OnLeftPickTrigger = 2;
  43. private static _OnRightPickTrigger = 3;
  44. private static _OnCenterPickTrigger = 4;
  45. private static _OnPointerOverTrigger = 5;
  46. private static _OnPointerOutTrigger = 6;
  47. private static _OnEveryFrameTrigger = 7;
  48. private static _OnIntersectionEnterTrigger = 8;
  49. private static _OnIntersectionExitTrigger = 9;
  50. private static _OnKeyDownTrigger = 10;
  51. private static _OnKeyUpTrigger = 11;
  52. public static get NothingTrigger(): number {
  53. return ActionManager._NothingTrigger;
  54. }
  55. public static get OnPickTrigger(): number {
  56. return ActionManager._OnPickTrigger;
  57. }
  58. public static get OnLeftPickTrigger(): number {
  59. return ActionManager._OnLeftPickTrigger;
  60. }
  61. public static get OnRightPickTrigger(): number {
  62. return ActionManager._OnRightPickTrigger;
  63. }
  64. public static get OnCenterPickTrigger(): number {
  65. return ActionManager._OnCenterPickTrigger;
  66. }
  67. public static get OnPointerOverTrigger(): number {
  68. return ActionManager._OnPointerOverTrigger;
  69. }
  70. public static get OnPointerOutTrigger(): number {
  71. return ActionManager._OnPointerOutTrigger;
  72. }
  73. public static get OnEveryFrameTrigger(): number {
  74. return ActionManager._OnEveryFrameTrigger;
  75. }
  76. public static get OnIntersectionEnterTrigger(): number {
  77. return ActionManager._OnIntersectionEnterTrigger;
  78. }
  79. public static get OnIntersectionExitTrigger(): number {
  80. return ActionManager._OnIntersectionExitTrigger;
  81. }
  82. public static get OnKeyDownTrigger(): number {
  83. return ActionManager._OnKeyDownTrigger;
  84. }
  85. public static get OnKeyUpTrigger(): number {
  86. return ActionManager._OnKeyUpTrigger;
  87. }
  88. // Members
  89. public actions = new Array<Action>();
  90. private _scene: Scene;
  91. constructor(scene: Scene) {
  92. this._scene = scene;
  93. scene._actionManagers.push(this);
  94. }
  95. // Methods
  96. public dispose(): void {
  97. var index = this._scene._actionManagers.indexOf(this);
  98. if (index > -1) {
  99. this._scene._actionManagers.splice(index, 1);
  100. }
  101. }
  102. public getScene(): Scene {
  103. return this._scene;
  104. }
  105. /**
  106. * Does this action manager handles actions of any of the given triggers
  107. * @param {number[]} triggers - the triggers to be tested
  108. * @return {boolean} whether one (or more) of the triggers is handeled
  109. */
  110. public hasSpecificTriggers(triggers: number[]): boolean {
  111. for (var index = 0; index < this.actions.length; index++) {
  112. var action = this.actions[index];
  113. if (triggers.indexOf(action.trigger) > -1) {
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119. /**
  120. * Does this action manager has pointer triggers
  121. * @return {boolean} whether or not it has pointer triggers
  122. */
  123. public get hasPointerTriggers(): boolean {
  124. for (var index = 0; index < this.actions.length; index++) {
  125. var action = this.actions[index];
  126. if (action.trigger >= ActionManager._OnPickTrigger && action.trigger <= ActionManager._OnPointerOutTrigger) {
  127. return true;
  128. }
  129. }
  130. return false;
  131. }
  132. /**
  133. * Does this action manager has pick triggers
  134. * @return {boolean} whether or not it has pick triggers
  135. */
  136. public get hasPickTriggers(): boolean {
  137. for (var index = 0; index < this.actions.length; index++) {
  138. var action = this.actions[index];
  139. if (action.trigger >= ActionManager._OnPickTrigger && action.trigger <= ActionManager._OnCenterPickTrigger) {
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. /**
  146. * Registers an action to this action manager
  147. * @param {BABYLON.Action} action - the action to be registered
  148. * @return {BABYLON.Action} the action amended (prepared) after registration
  149. */
  150. public registerAction(action: Action): Action {
  151. if (action.trigger === ActionManager.OnEveryFrameTrigger) {
  152. if (this.getScene().actionManager !== this) {
  153. Tools.Warn("OnEveryFrameTrigger can only be used with scene.actionManager");
  154. return null;
  155. }
  156. }
  157. this.actions.push(action);
  158. action._actionManager = this;
  159. action._prepare();
  160. return action;
  161. }
  162. /**
  163. * Process a specific trigger
  164. * @param {number} trigger - the trigger to process
  165. * @param evt {BABYLON.ActionEvent} the event details to be processed
  166. */
  167. public processTrigger(trigger: number, evt: ActionEvent): void {
  168. for (var index = 0; index < this.actions.length; index++) {
  169. var action = this.actions[index];
  170. if (action.trigger === trigger) {
  171. if (trigger === ActionManager.OnKeyUpTrigger
  172. || trigger === ActionManager.OnKeyDownTrigger) {
  173. var parameter = action.getTriggerParameter();
  174. if (parameter) {
  175. var unicode = evt.sourceEvent.charCode ? evt.sourceEvent.charCode : evt.sourceEvent.keyCode;
  176. var actualkey = String.fromCharCode(unicode).toLowerCase();
  177. if (actualkey !== parameter.toLowerCase()) {
  178. continue;
  179. }
  180. }
  181. }
  182. action._executeCurrent(evt);
  183. }
  184. }
  185. }
  186. public _getEffectiveTarget(target: any, propertyPath: string): any {
  187. var properties = propertyPath.split(".");
  188. for (var index = 0; index < properties.length - 1; index++) {
  189. target = target[properties[index]];
  190. }
  191. return target;
  192. }
  193. public _getProperty(propertyPath: string): string {
  194. var properties = propertyPath.split(".");
  195. return properties[properties.length - 1];
  196. }
  197. }
  198. }