babylon.directActions.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. module BABYLON {
  2. export class SwitchBooleanAction extends Action {
  3. private _target: any;
  4. private _effectiveTarget: any;
  5. private _property: string;
  6. constructor(triggerOptions: any, target: any, public propertyPath: string, condition?: Condition) {
  7. super(triggerOptions, condition);
  8. this._target = this._effectiveTarget = target;
  9. }
  10. public _prepare(): void {
  11. this._effectiveTarget = this._getEffectiveTarget(this._effectiveTarget, this.propertyPath);
  12. this._property = this._getProperty(this.propertyPath);
  13. }
  14. public execute(): void {
  15. this._effectiveTarget[this._property] = !this._effectiveTarget[this._property];
  16. }
  17. public serialize(parent: any): any {
  18. return super._serialize({
  19. name: "SwitchBooleanAction",
  20. properties: [
  21. Action._GetTargetProperty(this._target),
  22. { name: "propertyPath", value: this.propertyPath }
  23. ]
  24. }, parent);
  25. }
  26. }
  27. export class SetStateAction extends Action {
  28. private _target: any;
  29. constructor(triggerOptions: any, target: any, public value: string, condition?: Condition) {
  30. super(triggerOptions, condition);
  31. this._target = target;
  32. }
  33. public execute(): void {
  34. this._target.state = this.value;
  35. }
  36. public serialize(parent: any): any {
  37. return super._serialize({
  38. name: "SetStateAction",
  39. properties: [
  40. Action._GetTargetProperty(this._target),
  41. { name: "value", value: this.value }
  42. ]
  43. }, parent);
  44. }
  45. }
  46. export class SetValueAction extends Action {
  47. private _target: any;
  48. private _effectiveTarget: any;
  49. private _property: string;
  50. constructor(triggerOptions: any, target: any, public propertyPath: string, public value: any, condition?: Condition) {
  51. super(triggerOptions, condition);
  52. this._target = this._effectiveTarget = target;
  53. }
  54. public _prepare(): void {
  55. this._effectiveTarget = this._getEffectiveTarget(this._effectiveTarget, this.propertyPath);
  56. this._property = this._getProperty(this.propertyPath);
  57. }
  58. public execute(): void {
  59. this._effectiveTarget[this._property] = this.value;
  60. if (this._target.markAsDirty) {
  61. this._target.markAsDirty(this._property);
  62. }
  63. }
  64. public serialize(parent: any): any {
  65. return super._serialize({
  66. name: "SetValueAction",
  67. properties: [
  68. Action._GetTargetProperty(this._target),
  69. { name: "propertyPath", value: this.propertyPath },
  70. { name: "value", value: Action._SerializeValueAsString(this.value) }
  71. ]
  72. }, parent);
  73. }
  74. }
  75. export class IncrementValueAction extends Action {
  76. private _target: any;
  77. private _effectiveTarget: any;
  78. private _property: string;
  79. constructor(triggerOptions: any, target: any, public propertyPath: string, public value: any, condition?: Condition) {
  80. super(triggerOptions, condition);
  81. this._target = this._effectiveTarget = target;
  82. }
  83. public _prepare(): void {
  84. this._effectiveTarget = this._getEffectiveTarget(this._effectiveTarget, this.propertyPath);
  85. this._property = this._getProperty(this.propertyPath);
  86. if (typeof this._effectiveTarget[this._property] !== "number") {
  87. Tools.Warn("Warning: IncrementValueAction can only be used with number values");
  88. }
  89. }
  90. public execute(): void {
  91. this._effectiveTarget[this._property] += this.value;
  92. if (this._target.markAsDirty) {
  93. this._target.markAsDirty(this._property);
  94. }
  95. }
  96. public serialize(parent: any): any {
  97. return super._serialize({
  98. name: "IncrementValueAction",
  99. properties: [
  100. Action._GetTargetProperty(this._target),
  101. { name: "propertyPath", value: this.propertyPath },
  102. { name: "value", value: Action._SerializeValueAsString(this.value) }
  103. ]
  104. }, parent);
  105. }
  106. }
  107. export class PlayAnimationAction extends Action {
  108. private _target: any;
  109. constructor(triggerOptions: any, target: any, public from: number, public to: number, public loop?: boolean, condition?: Condition) {
  110. super(triggerOptions, condition);
  111. this._target = target;
  112. }
  113. public _prepare(): void {
  114. }
  115. public execute(): void {
  116. var scene = this._actionManager.getScene();
  117. scene.beginAnimation(this._target, this.from, this.to, this.loop);
  118. }
  119. public serialize(parent: any): any {
  120. return super._serialize({
  121. name: "PlayAnimationAction",
  122. properties: [
  123. Action._GetTargetProperty(this._target),
  124. { name: "from", value: String(this.from) },
  125. { name: "to", value: String(this.to) },
  126. { name: "loop", value: Action._SerializeValueAsString(this.loop) || false }
  127. ]
  128. }, parent);
  129. }
  130. }
  131. export class StopAnimationAction extends Action {
  132. private _target: any;
  133. constructor(triggerOptions: any, target: any, condition?: Condition) {
  134. super(triggerOptions, condition);
  135. this._target = target;
  136. }
  137. public _prepare(): void {
  138. }
  139. public execute(): void {
  140. var scene = this._actionManager.getScene();
  141. scene.stopAnimation(this._target);
  142. }
  143. public serialize(parent: any): any {
  144. return super._serialize({
  145. name: "StopAnimationAction",
  146. properties: [Action._GetTargetProperty(this._target)]
  147. }, parent);
  148. }
  149. }
  150. export class DoNothingAction extends Action {
  151. constructor(triggerOptions: any = ActionManager.NothingTrigger, condition?: Condition) {
  152. super(triggerOptions, condition);
  153. }
  154. public execute(): void {
  155. }
  156. public serialize(parent: any): any {
  157. return super._serialize({
  158. name: "DoNothingAction",
  159. properties: []
  160. }, parent);
  161. }
  162. }
  163. export class CombineAction extends Action {
  164. constructor(triggerOptions: any, public children: Action[], condition?: Condition) {
  165. super(triggerOptions, condition);
  166. }
  167. public _prepare(): void {
  168. for (var index = 0; index < this.children.length; index++) {
  169. this.children[index]._actionManager = this._actionManager;
  170. this.children[index]._prepare();
  171. }
  172. }
  173. public execute(evt: ActionEvent): void {
  174. for (var index = 0; index < this.children.length; index++) {
  175. this.children[index].execute(evt);
  176. }
  177. }
  178. public serialize(parent: any): any {
  179. var serializationObject = super._serialize({
  180. name: "CombineAction",
  181. properties: [],
  182. combine: []
  183. }, parent);
  184. for (var i=0; i < this.children.length; i++) {
  185. serializationObject.combine.push(this.children[i].serialize(null));
  186. }
  187. return serializationObject;
  188. }
  189. }
  190. export class ExecuteCodeAction extends Action {
  191. constructor(triggerOptions: any, public func: (evt: ActionEvent) => void, condition?: Condition) {
  192. super(triggerOptions, condition);
  193. }
  194. public execute(evt: ActionEvent): void {
  195. this.func(evt);
  196. }
  197. }
  198. export class SetParentAction extends Action {
  199. private _parent: any;
  200. private _target: any;
  201. constructor(triggerOptions: any, target: any, parent: any, condition?: Condition) {
  202. super(triggerOptions, condition);
  203. this._target = target;
  204. this._parent = parent;
  205. }
  206. public _prepare(): void {
  207. }
  208. public execute(): void {
  209. if (this._target.parent === this._parent) {
  210. return;
  211. }
  212. var invertParentWorldMatrix = this._parent.getWorldMatrix().clone();
  213. invertParentWorldMatrix.invert();
  214. this._target.position = Vector3.TransformCoordinates(this._target.position, invertParentWorldMatrix);
  215. this._target.parent = this._parent;
  216. }
  217. public serialize(parent: any): any {
  218. return super._serialize({
  219. name: "SetParentAction",
  220. properties: [
  221. Action._GetTargetProperty(this._target),
  222. Action._GetTargetProperty(this._parent),
  223. ]
  224. }, parent);
  225. }
  226. }
  227. export class PlaySoundAction extends Action {
  228. private _sound: Sound;
  229. constructor(triggerOptions: any, sound: Sound, condition?: Condition) {
  230. super(triggerOptions, condition);
  231. this._sound = sound;
  232. }
  233. public _prepare(): void {
  234. }
  235. public execute(): void {
  236. if (this._sound !== undefined)
  237. this._sound.play();
  238. }
  239. public serialize(parent: any): any {
  240. return super._serialize({
  241. name: "PlaySoundAction",
  242. properties: [{ name: "sound", value: this._sound.name }]
  243. }, parent);
  244. }
  245. }
  246. export class StopSoundAction extends Action {
  247. private _sound: Sound;
  248. constructor(triggerOptions: any, sound: Sound, condition?: Condition) {
  249. super(triggerOptions, condition);
  250. this._sound = sound;
  251. }
  252. public _prepare(): void {
  253. }
  254. public execute(): void {
  255. if (this._sound !== undefined)
  256. this._sound.stop();
  257. }
  258. public serialize(parent: any): any {
  259. return super._serialize({
  260. name: "StopSoundAction",
  261. properties: [{ name: "sound", value: this._sound.name }]
  262. }, parent);
  263. }
  264. }
  265. }