action.ts 7.7 KB

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