babylon.actionManager.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. module BABYLON {
  2. export class ActionEvent {
  3. constructor(public source: AbstractMesh, public pointerX: number, public pointerY: number, public meshUnderPointer: AbstractMesh, public sourceEvent?: any) {
  4. }
  5. public static CreateNew(source: AbstractMesh): ActionEvent {
  6. var scene = source.getScene();
  7. return new ActionEvent(source, scene.pointerX, scene.pointerY, scene.meshUnderPointer);
  8. }
  9. public static CreateNewFromScene(scene: Scene, evt:Event): ActionEvent {
  10. return new ActionEvent(null, scene.pointerX, scene.pointerY, scene.meshUnderPointer, evt);
  11. }
  12. }
  13. export class ActionManager {
  14. // Statics
  15. private static _NothingTrigger = 0;
  16. private static _OnPickTrigger = 1;
  17. private static _OnLeftPickTrigger = 2;
  18. private static _OnRightPickTrigger = 3;
  19. private static _OnCenterPickTrigger = 4;
  20. private static _OnPointerOverTrigger = 5;
  21. private static _OnPointerOutTrigger = 6;
  22. private static _OnEveryFrameTrigger = 7;
  23. private static _OnIntersectionEnterTrigger = 8;
  24. private static _OnIntersectionExitTrigger = 9;
  25. private static _OnKeyDownTrigger = 10;
  26. private static _OnKeyUpTrigger = 11;
  27. public static get NothingTrigger(): number {
  28. return ActionManager._NothingTrigger;
  29. }
  30. public static get OnPickTrigger(): number {
  31. return ActionManager._OnPickTrigger;
  32. }
  33. public static get OnLeftPickTrigger(): number {
  34. return ActionManager._OnLeftPickTrigger;
  35. }
  36. public static get OnRightPickTrigger(): number {
  37. return ActionManager._OnRightPickTrigger;
  38. }
  39. public static get OnCenterPickTrigger(): number {
  40. return ActionManager._OnCenterPickTrigger;
  41. }
  42. public static get OnPointerOverTrigger(): number {
  43. return ActionManager._OnPointerOverTrigger;
  44. }
  45. public static get OnPointerOutTrigger(): number {
  46. return ActionManager._OnPointerOutTrigger;
  47. }
  48. public static get OnEveryFrameTrigger(): number {
  49. return ActionManager._OnEveryFrameTrigger;
  50. }
  51. public static get OnIntersectionEnterTrigger(): number {
  52. return ActionManager._OnIntersectionEnterTrigger;
  53. }
  54. public static get OnIntersectionExitTrigger(): number {
  55. return ActionManager._OnIntersectionExitTrigger;
  56. }
  57. public static get OnKeyDownTrigger(): number {
  58. return ActionManager._OnKeyDownTrigger;
  59. }
  60. public static get OnKeyUpTrigger(): number {
  61. return ActionManager._OnKeyUpTrigger;
  62. }
  63. // Members
  64. public actions = new Array<Action>();
  65. private _scene: Scene;
  66. constructor(scene: Scene) {
  67. this._scene = scene;
  68. scene._actionManagers.push(this);
  69. }
  70. // Methods
  71. public dispose(): void {
  72. var index = this._scene._actionManagers.indexOf(this);
  73. if (index > -1) {
  74. this._scene._actionManagers.splice(index, 1);
  75. }
  76. }
  77. public getScene(): Scene {
  78. return this._scene;
  79. }
  80. public hasSpecificTriggers(triggers: number[]): boolean {
  81. for (var index = 0; index < this.actions.length; index++) {
  82. var action = this.actions[index];
  83. if (triggers.indexOf(action.trigger) > -1) {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. public get hasPointerTriggers(): boolean {
  90. for (var index = 0; index < this.actions.length; index++) {
  91. var action = this.actions[index];
  92. if (action.trigger >= ActionManager._OnPickTrigger && action.trigger <= ActionManager._OnPointerOutTrigger) {
  93. return true;
  94. }
  95. }
  96. return false;
  97. }
  98. public get hasPickTriggers(): boolean {
  99. for (var index = 0; index < this.actions.length; index++) {
  100. var action = this.actions[index];
  101. if (action.trigger >= ActionManager._OnPickTrigger && action.trigger <= ActionManager._OnCenterPickTrigger) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. public registerAction(action: Action): Action {
  108. if (action.trigger === ActionManager.OnEveryFrameTrigger) {
  109. if (this.getScene().actionManager !== this) {
  110. Tools.Warn("OnEveryFrameTrigger can only be used with scene.actionManager");
  111. return null;
  112. }
  113. }
  114. this.actions.push(action);
  115. action._actionManager = this;
  116. action._prepare();
  117. return action;
  118. }
  119. public processTrigger(trigger: number, evt: ActionEvent): void {
  120. for (var index = 0; index < this.actions.length; index++) {
  121. var action = this.actions[index];
  122. if (action.trigger === trigger) {
  123. if (trigger === ActionManager.OnKeyUpTrigger
  124. || trigger === ActionManager.OnKeyDownTrigger) {
  125. var parameter = action.getTriggerParameter();
  126. if (parameter) {
  127. if (evt.sourceEvent.key !== parameter) {
  128. continue;
  129. }
  130. }
  131. }
  132. action._executeCurrent(evt);
  133. }
  134. }
  135. }
  136. public _getEffectiveTarget(target: any, propertyPath: string): any {
  137. var properties = propertyPath.split(".");
  138. for (var index = 0; index < properties.length - 1; index++) {
  139. target = target[properties[index]];
  140. }
  141. return target;
  142. }
  143. public _getProperty(propertyPath: string): string {
  144. var properties = propertyPath.split(".");
  145. return properties[properties.length - 1];
  146. }
  147. }
  148. }