babylon.actionManager.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. module BABYLON {
  2. export class ActionEvent {
  3. constructor(public source: AbstractMesh, public pointerX: number, public pointerY: number, public meshUnderPointer: AbstractMesh) {
  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. }
  10. export class ActionManager {
  11. // Statics
  12. private static _NothingTrigger = 0;
  13. private static _OnPickTrigger = 1;
  14. private static _OnLeftPickTrigger = 2;
  15. private static _OnRightPickTrigger = 3;
  16. private static _OnCenterPickTrigger = 4;
  17. private static _OnPointerOverTrigger = 5;
  18. private static _OnPointerOutTrigger = 6;
  19. private static _OnEveryFrameTrigger = 7;
  20. private static _OnIntersectionEnterTrigger = 8;
  21. private static _OnIntersectionExitTrigger = 9;
  22. public static get NothingTrigger(): number {
  23. return ActionManager._NothingTrigger;
  24. }
  25. public static get OnPickTrigger(): number {
  26. return ActionManager._OnPickTrigger;
  27. }
  28. public static get OnLeftPickTrigger(): number {
  29. return ActionManager._OnLeftPickTrigger;
  30. }
  31. public static get OnRightPickTrigger(): number {
  32. return ActionManager._OnRightPickTrigger;
  33. }
  34. public static get OnCenterPickTrigger(): number {
  35. return ActionManager._OnCenterPickTrigger;
  36. }
  37. public static get OnPointerOverTrigger(): number {
  38. return ActionManager._OnPointerOverTrigger;
  39. }
  40. public static get OnPointerOutTrigger(): number {
  41. return ActionManager._OnPointerOutTrigger;
  42. }
  43. public static get OnEveryFrameTrigger(): number {
  44. return ActionManager._OnEveryFrameTrigger;
  45. }
  46. public static get OnIntersectionEnterTrigger(): number {
  47. return ActionManager._OnIntersectionEnterTrigger;
  48. }
  49. public static get OnIntersectionExitTrigger(): number {
  50. return ActionManager._OnIntersectionExitTrigger;
  51. }
  52. // Members
  53. public actions = new Array<Action>();
  54. private _scene: Scene;
  55. constructor(scene: Scene) {
  56. this._scene = scene;
  57. scene._actionManagers.push(this);
  58. }
  59. // Methods
  60. public dispose(): void {
  61. var index = this._scene._actionManagers.indexOf(this);
  62. if (index > -1) {
  63. this._scene._actionManagers.splice(index, 1);
  64. }
  65. }
  66. public getScene(): Scene {
  67. return this._scene;
  68. }
  69. public hasSpecificTriggers(triggers: number[]): boolean {
  70. for (var index = 0; index < this.actions.length; index++) {
  71. var action = this.actions[index];
  72. if (triggers.indexOf(action.trigger) > -1) {
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. public registerAction(action: Action): Action {
  79. if (action.trigger === ActionManager.OnEveryFrameTrigger) {
  80. if (this.getScene().actionManager !== this) {
  81. Tools.Warn("OnEveryFrameTrigger can only be used with scene.actionManager");
  82. return null;
  83. }
  84. }
  85. this.actions.push(action);
  86. action._actionManager = this;
  87. action._prepare();
  88. return action;
  89. }
  90. public processTrigger(trigger: number, evt: ActionEvent): void {
  91. for (var index = 0; index < this.actions.length; index++) {
  92. var action = this.actions[index];
  93. if (action.trigger === trigger) {
  94. action._executeCurrent(evt);
  95. }
  96. }
  97. }
  98. public _getEffectiveTarget(target: any, propertyPath: string): any {
  99. var properties = propertyPath.split(".");
  100. for (var index = 0; index < properties.length - 1; index++) {
  101. target = target[properties[index]];
  102. }
  103. return target;
  104. }
  105. public _getProperty(propertyPath: string): string {
  106. var properties = propertyPath.split(".");
  107. return properties[properties.length - 1];
  108. }
  109. }
  110. }