babylon.directActions.ts 11 KB

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