directActions.js 15 KB

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