babylon.actionManager.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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, public additionalData?: 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, additionalData?: any): ActionEvent {
  22. var scene = source.getScene();
  23. return new ActionEvent(source, scene.pointerX, scene.pointerY, scene.meshUnderPointer, evt, additionalData);
  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. private static _OnPickUpTrigger = 12;
  53. public static get NothingTrigger(): number {
  54. return ActionManager._NothingTrigger;
  55. }
  56. public static get OnPickTrigger(): number {
  57. return ActionManager._OnPickTrigger;
  58. }
  59. public static get OnLeftPickTrigger(): number {
  60. return ActionManager._OnLeftPickTrigger;
  61. }
  62. public static get OnRightPickTrigger(): number {
  63. return ActionManager._OnRightPickTrigger;
  64. }
  65. public static get OnCenterPickTrigger(): number {
  66. return ActionManager._OnCenterPickTrigger;
  67. }
  68. public static get OnPointerOverTrigger(): number {
  69. return ActionManager._OnPointerOverTrigger;
  70. }
  71. public static get OnPointerOutTrigger(): number {
  72. return ActionManager._OnPointerOutTrigger;
  73. }
  74. public static get OnEveryFrameTrigger(): number {
  75. return ActionManager._OnEveryFrameTrigger;
  76. }
  77. public static get OnIntersectionEnterTrigger(): number {
  78. return ActionManager._OnIntersectionEnterTrigger;
  79. }
  80. public static get OnIntersectionExitTrigger(): number {
  81. return ActionManager._OnIntersectionExitTrigger;
  82. }
  83. public static get OnKeyDownTrigger(): number {
  84. return ActionManager._OnKeyDownTrigger;
  85. }
  86. public static get OnKeyUpTrigger(): number {
  87. return ActionManager._OnKeyUpTrigger;
  88. }
  89. public static get OnPickUpTrigger(): number {
  90. return ActionManager._OnPickUpTrigger;
  91. }
  92. // Members
  93. public actions = new Array<Action>();
  94. private _scene: Scene;
  95. constructor(scene: Scene) {
  96. this._scene = scene;
  97. scene._actionManagers.push(this);
  98. }
  99. // Methods
  100. public dispose(): void {
  101. var index = this._scene._actionManagers.indexOf(this);
  102. if (index > -1) {
  103. this._scene._actionManagers.splice(index, 1);
  104. }
  105. }
  106. public getScene(): Scene {
  107. return this._scene;
  108. }
  109. /**
  110. * Does this action manager handles actions of any of the given triggers
  111. * @param {number[]} triggers - the triggers to be tested
  112. * @return {boolean} whether one (or more) of the triggers is handeled
  113. */
  114. public hasSpecificTriggers(triggers: number[]): boolean {
  115. for (var index = 0; index < this.actions.length; index++) {
  116. var action = this.actions[index];
  117. if (triggers.indexOf(action.trigger) > -1) {
  118. return true;
  119. }
  120. }
  121. return false;
  122. }
  123. /**
  124. * Does this action manager handles actions of a given trigger
  125. * @param {number} trigger - the trigger to be tested
  126. * @return {boolean} whether the trigger is handeled
  127. */
  128. public hasSpecificTrigger(trigger: number): boolean {
  129. for (var index = 0; index < this.actions.length; index++) {
  130. var action = this.actions[index];
  131. if (action.trigger === trigger) {
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. /**
  138. * Does this action manager has pointer triggers
  139. * @return {boolean} whether or not it has pointer triggers
  140. */
  141. public get hasPointerTriggers(): boolean {
  142. for (var index = 0; index < this.actions.length; index++) {
  143. var action = this.actions[index];
  144. if (action.trigger >= ActionManager._OnPickTrigger && action.trigger <= ActionManager._OnPointerOutTrigger) {
  145. return true;
  146. }
  147. if (action.trigger == ActionManager._OnPickUpTrigger) {
  148. return true;
  149. }
  150. }
  151. return false;
  152. }
  153. /**
  154. * Does this action manager has pick triggers
  155. * @return {boolean} whether or not it has pick triggers
  156. */
  157. public get hasPickTriggers(): boolean {
  158. for (var index = 0; index < this.actions.length; index++) {
  159. var action = this.actions[index];
  160. if (action.trigger >= ActionManager._OnPickTrigger && action.trigger <= ActionManager._OnCenterPickTrigger) {
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. /**
  167. * Registers an action to this action manager
  168. * @param {BABYLON.Action} action - the action to be registered
  169. * @return {BABYLON.Action} the action amended (prepared) after registration
  170. */
  171. public registerAction(action: Action): Action {
  172. if (action.trigger === ActionManager.OnEveryFrameTrigger) {
  173. if (this.getScene().actionManager !== this) {
  174. Tools.Warn("OnEveryFrameTrigger can only be used with scene.actionManager");
  175. return null;
  176. }
  177. }
  178. this.actions.push(action);
  179. action._actionManager = this;
  180. action._prepare();
  181. return action;
  182. }
  183. /**
  184. * Process a specific trigger
  185. * @param {number} trigger - the trigger to process
  186. * @param evt {BABYLON.ActionEvent} the event details to be processed
  187. */
  188. public processTrigger(trigger: number, evt: ActionEvent): void {
  189. for (var index = 0; index < this.actions.length; index++) {
  190. var action = this.actions[index];
  191. if (action.trigger === trigger) {
  192. if (trigger === ActionManager.OnKeyUpTrigger
  193. || trigger === ActionManager.OnKeyDownTrigger) {
  194. var parameter = action.getTriggerParameter();
  195. if (parameter) {
  196. var unicode = evt.sourceEvent.charCode ? evt.sourceEvent.charCode : evt.sourceEvent.keyCode;
  197. var actualkey = String.fromCharCode(unicode).toLowerCase();
  198. if (actualkey !== parameter.toLowerCase()) {
  199. continue;
  200. }
  201. }
  202. }
  203. action._executeCurrent(evt);
  204. }
  205. }
  206. }
  207. public _getEffectiveTarget(target: any, propertyPath: string): any {
  208. var properties = propertyPath.split(".");
  209. for (var index = 0; index < properties.length - 1; index++) {
  210. target = target[properties[index]];
  211. }
  212. return target;
  213. }
  214. public _getProperty(propertyPath: string): string {
  215. var properties = propertyPath.split(".");
  216. return properties[properties.length - 1];
  217. }
  218. }
  219. }