babylon.directActions.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. module BABYLON {
  2. export class SwitchBooleanAction extends Action {
  3. private _target: any;
  4. private _property: string;
  5. constructor(triggerOptions: any, target: any, public propertyPath: string, condition?: Condition) {
  6. super(triggerOptions, condition);
  7. this._target = target;
  8. }
  9. public _prepare(): void {
  10. this._target = this._getEffectiveTarget(this._target, this.propertyPath);
  11. this._property = this._getProperty(this.propertyPath);
  12. }
  13. public execute(): void {
  14. this._target[this._property] = !this._target[this._property];
  15. }
  16. }
  17. export class SetStateAction extends Action {
  18. private _target: any;
  19. constructor(triggerOptions: any, target: any, public value: string, condition?: Condition) {
  20. super(triggerOptions, condition);
  21. this._target = target;
  22. }
  23. public execute(): void {
  24. this._target.state = this.value;
  25. }
  26. }
  27. export class SetValueAction extends Action {
  28. private _target: any;
  29. private _property: string;
  30. constructor(triggerOptions: any, target: any, public propertyPath: string, public value: any, condition?: Condition) {
  31. super(triggerOptions, condition);
  32. this._target = target;
  33. }
  34. public _prepare(): void {
  35. this._target = this._getEffectiveTarget(this._target, this.propertyPath);
  36. this._property = this._getProperty(this.propertyPath);
  37. }
  38. public execute(): void {
  39. this._target[this._property] = this.value;
  40. }
  41. }
  42. export class IncrementValueAction extends Action {
  43. private _target: any;
  44. private _property: string;
  45. constructor(triggerOptions: any, target: any, public propertyPath: string, public value: any, condition?: Condition) {
  46. super(triggerOptions, condition);
  47. this._target = target;
  48. }
  49. public _prepare(): void {
  50. this._target = this._getEffectiveTarget(this._target, this.propertyPath);
  51. this._property = this._getProperty(this.propertyPath);
  52. if (typeof this._target[this._property] !== "number") {
  53. Tools.Warn("Warning: IncrementValueAction can only be used with number values");
  54. }
  55. }
  56. public execute(): void {
  57. this._target[this._property] += this.value;
  58. }
  59. }
  60. export class PlayAnimationAction extends Action {
  61. private _target: any;
  62. constructor(triggerOptions: any, target: any, public from: number, public to: number, public loop?: boolean, condition?: Condition) {
  63. super(triggerOptions, condition);
  64. this._target = target;
  65. }
  66. public _prepare(): void {
  67. }
  68. public execute(): void {
  69. var scene = this._actionManager.getScene();
  70. scene.beginAnimation(this._target, this.from, this.to, this.loop);
  71. }
  72. }
  73. export class StopAnimationAction extends Action {
  74. private _target: any;
  75. constructor(triggerOptions: any, target: any, condition?: Condition) {
  76. super(triggerOptions, condition);
  77. this._target = target;
  78. }
  79. public _prepare(): void {
  80. }
  81. public execute(): void {
  82. var scene = this._actionManager.getScene();
  83. scene.stopAnimation(this._target);
  84. }
  85. }
  86. export class DoNothingAction extends Action {
  87. constructor(triggerOptions: any = ActionManager.NothingTrigger, condition?: Condition) {
  88. super(triggerOptions, condition);
  89. }
  90. public execute(): void {
  91. }
  92. }
  93. export class CombineAction extends Action {
  94. constructor(triggerOptions: any, public children: Action[], condition?: Condition) {
  95. super(triggerOptions, condition);
  96. }
  97. public _prepare(): void {
  98. for (var index = 0; index < this.children.length; index++) {
  99. this.children[index]._actionManager = this._actionManager;
  100. this.children[index]._prepare();
  101. }
  102. }
  103. public execute(evt: ActionEvent): void {
  104. for (var index = 0; index < this.children.length; index++) {
  105. this.children[index].execute(evt);
  106. }
  107. }
  108. }
  109. export class ExecuteCodeAction extends Action {
  110. constructor(triggerOptions: any, public func: (evt: ActionEvent) => void, condition?: Condition) {
  111. super(triggerOptions, condition);
  112. }
  113. public execute(evt: ActionEvent): void {
  114. this.func(evt);
  115. }
  116. }
  117. export class SetParentAction extends Action {
  118. private _parent: any;
  119. private _target: any;
  120. constructor(triggerOptions: any, target: any, parent: any, condition?: Condition) {
  121. super(triggerOptions, condition);
  122. this._target = target;
  123. this._parent = parent;
  124. }
  125. public _prepare(): void {
  126. }
  127. public execute(): void {
  128. if (this._target.parent === this._parent) {
  129. return;
  130. }
  131. var invertParentWorldMatrix = this._parent.getWorldMatrix().clone();
  132. invertParentWorldMatrix.invert();
  133. this._target.position = BABYLON.Vector3.TransformCoordinates(this._target.position, invertParentWorldMatrix);
  134. this._target.parent = this._parent;
  135. }
  136. }
  137. export class PlaySoundAction extends Action {
  138. private _sound: Sound;
  139. constructor(triggerOptions: any, sound: Sound, condition?: Condition) {
  140. super(triggerOptions, condition);
  141. this._sound = sound;
  142. }
  143. public _prepare(): void {
  144. }
  145. public execute(): void {
  146. if (this._sound !== undefined)
  147. this._sound.play();
  148. }
  149. }
  150. export class StopSoundAction extends Action {
  151. private _sound: Sound;
  152. constructor(triggerOptions: any, sound: Sound, condition?: Condition) {
  153. super(triggerOptions, condition);
  154. this._sound = sound;
  155. }
  156. public _prepare(): void {
  157. }
  158. public execute(): void {
  159. if (this._sound !== undefined)
  160. this._sound.stop();
  161. }
  162. }
  163. }