babylon.action.ts 7.3 KB

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