directActions.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. import { Logger } from "../Misc/logger";
  2. import { Vector3 } from "../Maths/math.vector";
  3. import { Action } from "./action";
  4. import { Condition } from "./condition";
  5. import { Constants } from "../Engines/constants";
  6. import { _TypeStore } from '../Misc/typeStore';
  7. declare type ActionEvent = import("./actionEvent").ActionEvent;
  8. /**
  9. * This defines an action responsible to toggle a boolean once triggered.
  10. * @see http://doc.babylonjs.com/how_to/how_to_use_actions
  11. */
  12. export class SwitchBooleanAction extends Action {
  13. /**
  14. * The path to the boolean property in the target object
  15. */
  16. public propertyPath: string;
  17. private _target: any;
  18. private _effectiveTarget: any;
  19. private _property: string;
  20. /**
  21. * Instantiate the action
  22. * @param triggerOptions defines the trigger options
  23. * @param target defines the object containing the boolean
  24. * @param propertyPath defines the path to the boolean property in the target object
  25. * @param condition defines the trigger related conditions
  26. */
  27. constructor(triggerOptions: any, target: any, propertyPath: string, condition?: Condition) {
  28. super(triggerOptions, condition);
  29. this.propertyPath = propertyPath;
  30. this._target = this._effectiveTarget = target;
  31. }
  32. /** @hidden */
  33. public _prepare(): void {
  34. this._effectiveTarget = this._getEffectiveTarget(this._effectiveTarget, this.propertyPath);
  35. this._property = this._getProperty(this.propertyPath);
  36. }
  37. /**
  38. * Execute the action toggle the boolean value.
  39. */
  40. public execute(): void {
  41. this._effectiveTarget[this._property] = !this._effectiveTarget[this._property];
  42. }
  43. /**
  44. * Serializes the actions and its related information.
  45. * @param parent defines the object to serialize in
  46. * @returns the serialized object
  47. */
  48. public serialize(parent: any): any {
  49. return super._serialize({
  50. name: "SwitchBooleanAction",
  51. properties: [
  52. Action._GetTargetProperty(this._target),
  53. { name: "propertyPath", value: this.propertyPath }
  54. ]
  55. }, parent);
  56. }
  57. }
  58. /**
  59. * This defines an action responsible to set a the state field of the target
  60. * to a desired value once triggered.
  61. * @see http://doc.babylonjs.com/how_to/how_to_use_actions
  62. */
  63. export class SetStateAction extends Action {
  64. /**
  65. * The value to store in the state field.
  66. */
  67. public value: string;
  68. private _target: any;
  69. /**
  70. * Instantiate the action
  71. * @param triggerOptions defines the trigger options
  72. * @param target defines the object containing the state property
  73. * @param value defines the value to store in the state field
  74. * @param condition defines the trigger related conditions
  75. */
  76. constructor(triggerOptions: any, target: any, value: string, condition?: Condition) {
  77. super(triggerOptions, condition);
  78. this.value = value;
  79. this._target = target;
  80. }
  81. /**
  82. * Execute the action and store the value on the target state property.
  83. */
  84. public execute(): void {
  85. this._target.state = this.value;
  86. }
  87. /**
  88. * Serializes the actions and its related information.
  89. * @param parent defines the object to serialize in
  90. * @returns the serialized object
  91. */
  92. public serialize(parent: any): any {
  93. return super._serialize({
  94. name: "SetStateAction",
  95. properties: [
  96. Action._GetTargetProperty(this._target),
  97. { name: "value", value: this.value }
  98. ]
  99. }, parent);
  100. }
  101. }
  102. /**
  103. * This defines an action responsible to set a property of the target
  104. * to a desired value once triggered.
  105. * @see http://doc.babylonjs.com/how_to/how_to_use_actions
  106. */
  107. export class SetValueAction extends Action {
  108. /**
  109. * The path of the property to set in the target.
  110. */
  111. public propertyPath: string;
  112. /**
  113. * The value to set in the property
  114. */
  115. public value: any;
  116. private _target: any;
  117. private _effectiveTarget: any;
  118. private _property: string;
  119. /**
  120. * Instantiate the action
  121. * @param triggerOptions defines the trigger options
  122. * @param target defines the object containing the property
  123. * @param propertyPath defines the path of the property to set in the target
  124. * @param value defines the value to set in the property
  125. * @param condition defines the trigger related conditions
  126. */
  127. constructor(triggerOptions: any, target: any, propertyPath: string, value: any, condition?: Condition) {
  128. super(triggerOptions, condition);
  129. this.propertyPath = propertyPath;
  130. this.value = value;
  131. this._target = this._effectiveTarget = target;
  132. }
  133. /** @hidden */
  134. public _prepare(): void {
  135. this._effectiveTarget = this._getEffectiveTarget(this._effectiveTarget, this.propertyPath);
  136. this._property = this._getProperty(this.propertyPath);
  137. }
  138. /**
  139. * Execute the action and set the targetted property to the desired value.
  140. */
  141. public execute(): void {
  142. this._effectiveTarget[this._property] = this.value;
  143. if (this._target.markAsDirty) {
  144. this._target.markAsDirty(this._property);
  145. }
  146. }
  147. /**
  148. * Serializes the actions and its related information.
  149. * @param parent defines the object to serialize in
  150. * @returns the serialized object
  151. */
  152. public serialize(parent: any): any {
  153. return super._serialize({
  154. name: "SetValueAction",
  155. properties: [
  156. Action._GetTargetProperty(this._target),
  157. { name: "propertyPath", value: this.propertyPath },
  158. { name: "value", value: Action._SerializeValueAsString(this.value) }
  159. ]
  160. }, parent);
  161. }
  162. }
  163. /**
  164. * This defines an action responsible to increment the target value
  165. * to a desired value once triggered.
  166. * @see http://doc.babylonjs.com/how_to/how_to_use_actions
  167. */
  168. export class IncrementValueAction extends Action {
  169. /**
  170. * The path of the property to increment in the target.
  171. */
  172. public propertyPath: string;
  173. /**
  174. * The value we should increment the property by.
  175. */
  176. public value: any;
  177. private _target: any;
  178. private _effectiveTarget: any;
  179. private _property: string;
  180. /**
  181. * Instantiate the action
  182. * @param triggerOptions defines the trigger options
  183. * @param target defines the object containing the property
  184. * @param propertyPath defines the path of the property to increment in the target
  185. * @param value defines the value value we should increment the property by
  186. * @param condition defines the trigger related conditions
  187. */
  188. constructor(triggerOptions: any, target: any, propertyPath: string, value: any, condition?: Condition) {
  189. super(triggerOptions, condition);
  190. this.propertyPath = propertyPath;
  191. this.value = value;
  192. this._target = this._effectiveTarget = target;
  193. }
  194. /** @hidden */
  195. public _prepare(): void {
  196. this._effectiveTarget = this._getEffectiveTarget(this._effectiveTarget, this.propertyPath);
  197. this._property = this._getProperty(this.propertyPath);
  198. if (typeof this._effectiveTarget[this._property] !== "number") {
  199. Logger.Warn("Warning: IncrementValueAction can only be used with number values");
  200. }
  201. }
  202. /**
  203. * Execute the action and increment the target of the value amount.
  204. */
  205. public execute(): void {
  206. this._effectiveTarget[this._property] += this.value;
  207. if (this._target.markAsDirty) {
  208. this._target.markAsDirty(this._property);
  209. }
  210. }
  211. /**
  212. * Serializes the actions and its related information.
  213. * @param parent defines the object to serialize in
  214. * @returns the serialized object
  215. */
  216. public serialize(parent: any): any {
  217. return super._serialize({
  218. name: "IncrementValueAction",
  219. properties: [
  220. Action._GetTargetProperty(this._target),
  221. { name: "propertyPath", value: this.propertyPath },
  222. { name: "value", value: Action._SerializeValueAsString(this.value) }
  223. ]
  224. }, parent);
  225. }
  226. }
  227. /**
  228. * This defines an action responsible to start an animation once triggered.
  229. * @see http://doc.babylonjs.com/how_to/how_to_use_actions
  230. */
  231. export class PlayAnimationAction extends Action {
  232. /**
  233. * Where the animation should start (animation frame)
  234. */
  235. public from: number;
  236. /**
  237. * Where the animation should stop (animation frame)
  238. */
  239. public to: number;
  240. /**
  241. * Define if the animation should loop or stop after the first play.
  242. */
  243. public loop?: boolean;
  244. private _target: any;
  245. /**
  246. * Instantiate the action
  247. * @param triggerOptions defines the trigger options
  248. * @param target defines the target animation or animation name
  249. * @param from defines from where the animation should start (animation frame)
  250. * @param end defines where the animation should stop (animation frame)
  251. * @param loop defines if the animation should loop or stop after the first play
  252. * @param condition defines the trigger related conditions
  253. */
  254. constructor(triggerOptions: any, target: any, from: number, to: number, loop?: boolean, condition?: Condition) {
  255. super(triggerOptions, condition);
  256. this.from = from;
  257. this.to = to;
  258. this.loop = loop;
  259. this._target = target;
  260. }
  261. /** @hidden */
  262. public _prepare(): void {
  263. }
  264. /**
  265. * Execute the action and play the animation.
  266. */
  267. public execute(): void {
  268. var scene = this._actionManager.getScene();
  269. scene.beginAnimation(this._target, this.from, this.to, this.loop);
  270. }
  271. /**
  272. * Serializes the actions and its related information.
  273. * @param parent defines the object to serialize in
  274. * @returns the serialized object
  275. */
  276. public serialize(parent: any): any {
  277. return super._serialize({
  278. name: "PlayAnimationAction",
  279. properties: [
  280. Action._GetTargetProperty(this._target),
  281. { name: "from", value: String(this.from) },
  282. { name: "to", value: String(this.to) },
  283. { name: "loop", value: Action._SerializeValueAsString(this.loop) || false }
  284. ]
  285. }, parent);
  286. }
  287. }
  288. /**
  289. * This defines an action responsible to stop an animation once triggered.
  290. * @see http://doc.babylonjs.com/how_to/how_to_use_actions
  291. */
  292. export class StopAnimationAction extends Action {
  293. private _target: any;
  294. /**
  295. * Instantiate the action
  296. * @param triggerOptions defines the trigger options
  297. * @param target defines the target animation or animation name
  298. * @param condition defines the trigger related conditions
  299. */
  300. constructor(triggerOptions: any, target: any, condition?: Condition) {
  301. super(triggerOptions, condition);
  302. this._target = target;
  303. }
  304. /** @hidden */
  305. public _prepare(): void {
  306. }
  307. /**
  308. * Execute the action and stop the animation.
  309. */
  310. public execute(): void {
  311. var scene = this._actionManager.getScene();
  312. scene.stopAnimation(this._target);
  313. }
  314. /**
  315. * Serializes the actions and its related information.
  316. * @param parent defines the object to serialize in
  317. * @returns the serialized object
  318. */
  319. public serialize(parent: any): any {
  320. return super._serialize({
  321. name: "StopAnimationAction",
  322. properties: [Action._GetTargetProperty(this._target)]
  323. }, parent);
  324. }
  325. }
  326. /**
  327. * This defines an action responsible that does nothing once triggered.
  328. * @see http://doc.babylonjs.com/how_to/how_to_use_actions
  329. */
  330. export class DoNothingAction extends Action {
  331. /**
  332. * Instantiate the action
  333. * @param triggerOptions defines the trigger options
  334. * @param condition defines the trigger related conditions
  335. */
  336. constructor(triggerOptions: any = Constants.ACTION_NothingTrigger, condition?: Condition) {
  337. super(triggerOptions, condition);
  338. }
  339. /**
  340. * Execute the action and do nothing.
  341. */
  342. public execute(): void {
  343. }
  344. /**
  345. * Serializes the actions and its related information.
  346. * @param parent defines the object to serialize in
  347. * @returns the serialized object
  348. */
  349. public serialize(parent: any): any {
  350. return super._serialize({
  351. name: "DoNothingAction",
  352. properties: []
  353. }, parent);
  354. }
  355. }
  356. /**
  357. * This defines an action responsible to trigger several actions once triggered.
  358. * @see http://doc.babylonjs.com/how_to/how_to_use_actions
  359. */
  360. export class CombineAction extends Action {
  361. /**
  362. * The list of aggregated animations to run.
  363. */
  364. public children: Action[];
  365. /**
  366. * Instantiate the action
  367. * @param triggerOptions defines the trigger options
  368. * @param children defines the list of aggregated animations to run
  369. * @param condition defines the trigger related conditions
  370. */
  371. constructor(triggerOptions: any, children: Action[], condition?: Condition) {
  372. super(triggerOptions, condition);
  373. this.children = children;
  374. }
  375. /** @hidden */
  376. public _prepare(): void {
  377. for (var index = 0; index < this.children.length; index++) {
  378. this.children[index]._actionManager = this._actionManager;
  379. this.children[index]._prepare();
  380. }
  381. }
  382. /**
  383. * Execute the action and executes all the aggregated actions.
  384. */
  385. public execute(evt: ActionEvent): void {
  386. for (var index = 0; index < this.children.length; index++) {
  387. this.children[index].execute(evt);
  388. }
  389. }
  390. /**
  391. * Serializes the actions and its related information.
  392. * @param parent defines the object to serialize in
  393. * @returns the serialized object
  394. */
  395. public serialize(parent: any): any {
  396. var serializationObject = super._serialize({
  397. name: "CombineAction",
  398. properties: [],
  399. combine: []
  400. }, parent);
  401. for (var i = 0; i < this.children.length; i++) {
  402. serializationObject.combine.push(this.children[i].serialize(null));
  403. }
  404. return serializationObject;
  405. }
  406. }
  407. /**
  408. * This defines an action responsible to run code (external event) once triggered.
  409. * @see http://doc.babylonjs.com/how_to/how_to_use_actions
  410. */
  411. export class ExecuteCodeAction extends Action {
  412. /**
  413. * The callback function to run.
  414. */
  415. public func: (evt: ActionEvent) => void;
  416. /**
  417. * Instantiate the action
  418. * @param triggerOptions defines the trigger options
  419. * @param func defines the callback function to run
  420. * @param condition defines the trigger related conditions
  421. */
  422. constructor(triggerOptions: any, func: (evt: ActionEvent) => void, condition?: Condition) {
  423. super(triggerOptions, condition);
  424. this.func = func;
  425. }
  426. /**
  427. * Execute the action and run the attached code.
  428. */
  429. public execute(evt: ActionEvent): void {
  430. this.func(evt);
  431. }
  432. }
  433. /**
  434. * This defines an action responsible to set the parent property of the target once triggered.
  435. * @see http://doc.babylonjs.com/how_to/how_to_use_actions
  436. */
  437. export class SetParentAction extends Action {
  438. private _parent: any;
  439. private _target: any;
  440. /**
  441. * Instantiate the action
  442. * @param triggerOptions defines the trigger options
  443. * @param target defines the target containing the parent property
  444. * @param parent defines from where the animation should start (animation frame)
  445. * @param condition defines the trigger related conditions
  446. */
  447. constructor(triggerOptions: any, target: any, parent: any, condition?: Condition) {
  448. super(triggerOptions, condition);
  449. this._target = target;
  450. this._parent = parent;
  451. }
  452. /** @hidden */
  453. public _prepare(): void {
  454. }
  455. /**
  456. * Execute the action and set the parent property.
  457. */
  458. public execute(): void {
  459. if (this._target.parent === this._parent) {
  460. return;
  461. }
  462. var invertParentWorldMatrix = this._parent.getWorldMatrix().clone();
  463. invertParentWorldMatrix.invert();
  464. this._target.position = Vector3.TransformCoordinates(this._target.position, invertParentWorldMatrix);
  465. this._target.parent = this._parent;
  466. }
  467. /**
  468. * Serializes the actions and its related information.
  469. * @param parent defines the object to serialize in
  470. * @returns the serialized object
  471. */
  472. public serialize(parent: any): any {
  473. return super._serialize({
  474. name: "SetParentAction",
  475. properties: [
  476. Action._GetTargetProperty(this._target),
  477. Action._GetTargetProperty(this._parent),
  478. ]
  479. }, parent);
  480. }
  481. }
  482. _TypeStore.RegisteredTypes["BABYLON.SetParentAction"] = SetParentAction;
  483. _TypeStore.RegisteredTypes["BABYLON.ExecuteCodeAction"] = ExecuteCodeAction;
  484. _TypeStore.RegisteredTypes["BABYLON.DoNothingAction"] = DoNothingAction;
  485. _TypeStore.RegisteredTypes["BABYLON.StopAnimationAction"] = StopAnimationAction;
  486. _TypeStore.RegisteredTypes["BABYLON.PlayAnimationAction"] = PlayAnimationAction;
  487. _TypeStore.RegisteredTypes["BABYLON.IncrementValueAction"] = IncrementValueAction;
  488. _TypeStore.RegisteredTypes["BABYLON.SetValueAction"] = SetValueAction;
  489. _TypeStore.RegisteredTypes["BABYLON.SetStateAction"] = SetStateAction;
  490. _TypeStore.RegisteredTypes["BABYLON.SetParentAction"] = SetParentAction;