abstractActionManager.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { IDisposable } from "../scene";
  2. import { IActionEvent } from "./actionEvent";
  3. import { IAction } from "./action";
  4. import { Constants } from "../Engines/constants";
  5. import { Nullable } from "../types";
  6. /**
  7. * Abstract class used to decouple action Manager from scene and meshes.
  8. * Do not instantiate.
  9. * @see https://doc.babylonjs.com/how_to/how_to_use_actions
  10. */
  11. export abstract class AbstractActionManager implements IDisposable {
  12. /** Gets the list of active triggers */
  13. public static Triggers: { [key: string]: number } = {};
  14. /** Gets the cursor to use when hovering items */
  15. public hoverCursor: string = '';
  16. /** Gets the list of actions */
  17. public actions = new Array<IAction>();
  18. /**
  19. * Gets or sets a boolean indicating that the manager is recursive meaning that it can trigger action from children
  20. */
  21. public isRecursive = false;
  22. /**
  23. * Releases all associated resources
  24. */
  25. public abstract dispose(): void;
  26. /**
  27. * Does this action manager has pointer triggers
  28. */
  29. public abstract get hasPointerTriggers(): boolean;
  30. /**
  31. * Does this action manager has pick triggers
  32. */
  33. public abstract get hasPickTriggers(): boolean;
  34. /**
  35. * Process a specific trigger
  36. * @param trigger defines the trigger to process
  37. * @param evt defines the event details to be processed
  38. */
  39. public abstract processTrigger(trigger: number, evt?: IActionEvent): void;
  40. /**
  41. * Does this action manager handles actions of any of the given triggers
  42. * @param triggers defines the triggers to be tested
  43. * @return a boolean indicating whether one (or more) of the triggers is handled
  44. */
  45. public abstract hasSpecificTriggers(triggers: number[]): boolean;
  46. /**
  47. * Does this action manager handles actions of any of the given triggers. This function takes two arguments for
  48. * speed.
  49. * @param triggerA defines the trigger to be tested
  50. * @param triggerB defines the trigger to be tested
  51. * @return a boolean indicating whether one (or more) of the triggers is handled
  52. */
  53. public abstract hasSpecificTriggers2(triggerA: number, triggerB: number): boolean;
  54. /**
  55. * Does this action manager handles actions of a given trigger
  56. * @param trigger defines the trigger to be tested
  57. * @param parameterPredicate defines an optional predicate to filter triggers by parameter
  58. * @return whether the trigger is handled
  59. */
  60. public abstract hasSpecificTrigger(trigger: number, parameterPredicate?: (parameter: any) => boolean): boolean;
  61. /**
  62. * Serialize this manager to a JSON object
  63. * @param name defines the property name to store this manager
  64. * @returns a JSON representation of this manager
  65. */
  66. public abstract serialize(name: string): any;
  67. /**
  68. * Registers an action to this action manager
  69. * @param action defines the action to be registered
  70. * @return the action amended (prepared) after registration
  71. */
  72. public abstract registerAction(action: IAction): Nullable<IAction>;
  73. /**
  74. * Unregisters an action to this action manager
  75. * @param action defines the action to be unregistered
  76. * @return a boolean indicating whether the action has been unregistered
  77. */
  78. public abstract unregisterAction(action: IAction): Boolean;
  79. /**
  80. * Does exist one action manager with at least one trigger
  81. **/
  82. public static get HasTriggers(): boolean {
  83. for (var t in AbstractActionManager.Triggers) {
  84. if (AbstractActionManager.Triggers.hasOwnProperty(t)) {
  85. return true;
  86. }
  87. }
  88. return false;
  89. }
  90. /**
  91. * Does exist one action manager with at least one pick trigger
  92. **/
  93. public static get HasPickTriggers(): boolean {
  94. for (var t in AbstractActionManager.Triggers) {
  95. if (AbstractActionManager.Triggers.hasOwnProperty(t)) {
  96. let t_int = parseInt(t);
  97. if (t_int >= Constants.ACTION_OnPickTrigger && t_int <= Constants.ACTION_OnPickUpTrigger) {
  98. return true;
  99. }
  100. }
  101. }
  102. return false;
  103. }
  104. /**
  105. * Does exist one action manager that handles actions of a given trigger
  106. * @param trigger defines the trigger to be tested
  107. * @return a boolean indicating whether the trigger is handeled by at least one action manager
  108. **/
  109. public static HasSpecificTrigger(trigger: number): boolean {
  110. for (var t in AbstractActionManager.Triggers) {
  111. if (AbstractActionManager.Triggers.hasOwnProperty(t)) {
  112. let t_int = parseInt(t);
  113. if (t_int === trigger) {
  114. return true;
  115. }
  116. }
  117. }
  118. return false;
  119. }
  120. }