babylon.directActions.ts 9.5 KB

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