babylon.action.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. module BABYLON {
  2. /**
  3. * The action to be carried out following a trigger
  4. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#available-actions
  5. */
  6. export class Action {
  7. /**
  8. * Trigger for the action
  9. */
  10. public trigger: number;
  11. /**
  12. * Internal only - manager for action
  13. * @hidden
  14. */
  15. public _actionManager: ActionManager;
  16. private _nextActiveAction: Action;
  17. private _child: Action;
  18. private _condition?: Condition;
  19. private _triggerParameter: any;
  20. /**
  21. * An event triggered prior to action being executed.
  22. */
  23. public onBeforeExecuteObservable = new Observable<Action>();
  24. /**
  25. * Creates a new Action
  26. * @param triggerOptions the trigger, with or without parameters, for the action
  27. * @param condition an optional determinant of action
  28. */
  29. constructor(
  30. /** the trigger, with or without parameters, for the action */
  31. public triggerOptions: any, condition?: Condition) {
  32. if (triggerOptions.parameter) {
  33. this.trigger = triggerOptions.trigger;
  34. this._triggerParameter = triggerOptions.parameter;
  35. } else {
  36. this.trigger = triggerOptions;
  37. }
  38. this._nextActiveAction = this;
  39. this._condition = condition;
  40. }
  41. /**
  42. * Internal only
  43. * @hidden
  44. */
  45. public _prepare(): void {
  46. }
  47. /**
  48. * Gets the trigger parameters
  49. * @returns the trigger parameters
  50. */
  51. public getTriggerParameter(): any {
  52. return this._triggerParameter;
  53. }
  54. /**
  55. * Internal only - executes current action event
  56. * @hidden
  57. */
  58. public _executeCurrent(evt?: ActionEvent): void {
  59. if (this._nextActiveAction._condition) {
  60. var condition = this._nextActiveAction._condition;
  61. var currentRenderId = this._actionManager.getScene().getRenderId();
  62. // We cache the current evaluation for the current frame
  63. if (condition._evaluationId === currentRenderId) {
  64. if (!condition._currentResult) {
  65. return;
  66. }
  67. } else {
  68. condition._evaluationId = currentRenderId;
  69. if (!condition.isValid()) {
  70. condition._currentResult = false;
  71. return;
  72. }
  73. condition._currentResult = true;
  74. }
  75. }
  76. this.onBeforeExecuteObservable.notifyObservers(this);
  77. this._nextActiveAction.execute(evt);
  78. this.skipToNextActiveAction();
  79. }
  80. /**
  81. * Execute placeholder for child classes
  82. * @param evt optional action event
  83. */
  84. public execute(evt?: ActionEvent): void {
  85. }
  86. /**
  87. * Skips to next active action
  88. */
  89. public skipToNextActiveAction(): void {
  90. if (this._nextActiveAction._child) {
  91. if (!this._nextActiveAction._child._actionManager) {
  92. this._nextActiveAction._child._actionManager = this._actionManager;
  93. }
  94. this._nextActiveAction = this._nextActiveAction._child;
  95. } else {
  96. this._nextActiveAction = this;
  97. }
  98. }
  99. /**
  100. * Adds action to chain of actions, may be a DoNothingAction
  101. * @param action defines the next action to execute
  102. * @returns The action passed in
  103. * @see https://www.babylonjs-playground.com/#1T30HR#0
  104. */
  105. public then(action: Action): Action {
  106. this._child = action;
  107. action._actionManager = this._actionManager;
  108. action._prepare();
  109. return action;
  110. }
  111. /**
  112. * Internal only
  113. * @hidden
  114. */
  115. public _getProperty(propertyPath: string): string {
  116. return this._actionManager._getProperty(propertyPath);
  117. }
  118. /**
  119. * Internal only
  120. * @hidden
  121. */
  122. public _getEffectiveTarget(target: any, propertyPath: string): any {
  123. return this._actionManager._getEffectiveTarget(target, propertyPath);
  124. }
  125. /**
  126. * Serialize placeholder for child classes
  127. * @param parent of child
  128. * @returns the serialized object
  129. */
  130. public serialize(parent: any): any {
  131. }
  132. /**
  133. * Internal only called by serialize
  134. * @hidden
  135. */
  136. protected _serialize(serializedAction: any, parent?: any): any {
  137. var serializationObject: any = {
  138. type: 1,
  139. children: [],
  140. name: serializedAction.name,
  141. properties: serializedAction.properties || []
  142. };
  143. // Serialize child
  144. if (this._child) {
  145. this._child.serialize(serializationObject);
  146. }
  147. // Check if "this" has a condition
  148. if (this._condition) {
  149. var serializedCondition = this._condition.serialize();
  150. serializedCondition.children.push(serializationObject);
  151. if (parent) {
  152. parent.children.push(serializedCondition);
  153. }
  154. return serializedCondition;
  155. }
  156. if (parent) {
  157. parent.children.push(serializationObject);
  158. }
  159. return serializationObject;
  160. }
  161. /**
  162. * Internal only
  163. * @hidden
  164. */
  165. public static _SerializeValueAsString = (value: any): string => {
  166. if (typeof value === "number") {
  167. return value.toString();
  168. }
  169. if (typeof value === "boolean") {
  170. return value ? "true" : "false";
  171. }
  172. if (value instanceof Vector2) {
  173. return value.x + ", " + value.y;
  174. }
  175. if (value instanceof Vector3) {
  176. return value.x + ", " + value.y + ", " + value.z;
  177. }
  178. if (value instanceof Color3) {
  179. return value.r + ", " + value.g + ", " + value.b;
  180. }
  181. if (value instanceof Color4) {
  182. return value.r + ", " + value.g + ", " + value.b + ", " + value.a;
  183. }
  184. return value; // string
  185. };
  186. /**
  187. * Internal only
  188. * @hidden
  189. */
  190. public static _GetTargetProperty = (target: Scene | Node) => {
  191. return {
  192. name: "target",
  193. targetType: target instanceof Mesh ? "MeshProperties"
  194. : target instanceof Light ? "LightProperties"
  195. : target instanceof Camera ? "CameraProperties"
  196. : "SceneProperties",
  197. value: target instanceof Scene ? "Scene" : (<Node>target).name
  198. }
  199. };
  200. }
  201. }