babylon.actionManager.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. public static get NothingTrigger(): number {
  21. return ActionManager._NothingTrigger;
  22. }
  23. public static get OnPickTrigger(): number {
  24. return ActionManager._OnPickTrigger;
  25. }
  26. public static get OnLeftPickTrigger(): number {
  27. return ActionManager._OnLeftPickTrigger;
  28. }
  29. public static get OnRightPickTrigger(): number {
  30. return ActionManager._OnRightPickTrigger;
  31. }
  32. public static get OnCenterPickTrigger(): number {
  33. return ActionManager._OnCenterPickTrigger;
  34. }
  35. public static get OnPointerOverTrigger(): number {
  36. return ActionManager._OnPointerOverTrigger;
  37. }
  38. public static get OnPointerOutTrigger(): number {
  39. return ActionManager._OnPointerOutTrigger;
  40. }
  41. public static get OnEveryFrameTrigger(): number {
  42. return ActionManager._OnEveryFrameTrigger;
  43. }
  44. // Members
  45. public actions = new Array<Action>();
  46. private _scene: Scene;
  47. constructor(scene: Scene) {
  48. this._scene = scene;
  49. }
  50. // Methods
  51. public getScene(): Scene {
  52. return this._scene;
  53. }
  54. public registerAction(action: Action): Action {
  55. if (action.trigger === ActionManager.OnEveryFrameTrigger) {
  56. if (this.getScene().actionManager !== this) {
  57. Tools.Warn("OnEveryFrameTrigger can only be used with scene.actionManager");
  58. return null;
  59. }
  60. }
  61. this.actions.push(action);
  62. action._actionManager = this;
  63. action._prepare();
  64. return action;
  65. }
  66. public processTrigger(trigger: number, evt: ActionEvent): void {
  67. for (var index = 0; index < this.actions.length; index++) {
  68. var action = this.actions[index];
  69. if (action.trigger === trigger) {
  70. action._executeCurrent(evt);
  71. }
  72. }
  73. }
  74. public _getEffectiveTarget(target: any, propertyPath: string): any {
  75. var properties = propertyPath.split(".");
  76. for (var index = 0; index < properties.length - 1; index++) {
  77. target = target[properties[index]];
  78. }
  79. return target;
  80. }
  81. public _getProperty(propertyPath: string): string {
  82. var properties = propertyPath.split(".");
  83. return properties[properties.length - 1];
  84. }
  85. }
  86. }