babylon.directActions.ts 19 KB

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