action.ts 8.5 KB

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