action.ts 8.4 KB

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