babylon.action.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. module BABYLON {
  2. export class Action {
  3. public trigger: number;
  4. public _actionManager: ActionManager;
  5. private _nextActiveAction: Action;
  6. private _child: Action;
  7. private _condition: Condition;
  8. private _triggerParameter: any;
  9. constructor(public triggerOptions: any, condition?: Condition) {
  10. if (triggerOptions.parameter) {
  11. this.trigger = triggerOptions.trigger;
  12. this._triggerParameter = triggerOptions.parameter;
  13. } else {
  14. this.trigger = triggerOptions;
  15. }
  16. this._nextActiveAction = this;
  17. this._condition = condition;
  18. }
  19. // Methods
  20. public _prepare(): void {
  21. }
  22. public getTriggerParameter(): any {
  23. return this._triggerParameter;
  24. }
  25. public _executeCurrent(evt: ActionEvent): void {
  26. if (this._nextActiveAction._condition) {
  27. var condition = this._nextActiveAction._condition;
  28. var currentRenderId = this._actionManager.getScene().getRenderId();
  29. // We cache the current evaluation for the current frame
  30. if (condition._evaluationId === currentRenderId) {
  31. if (!condition._currentResult) {
  32. return;
  33. }
  34. } else {
  35. condition._evaluationId = currentRenderId;
  36. if (!condition.isValid()) {
  37. condition._currentResult = false;
  38. return;
  39. }
  40. condition._currentResult = true;
  41. }
  42. }
  43. this._nextActiveAction.execute(evt);
  44. this.skipToNextActiveAction();
  45. }
  46. public execute(evt: ActionEvent): void {
  47. }
  48. public skipToNextActiveAction(): void {
  49. if (this._nextActiveAction._child) {
  50. if (!this._nextActiveAction._child._actionManager) {
  51. this._nextActiveAction._child._actionManager = this._actionManager;
  52. }
  53. this._nextActiveAction = this._nextActiveAction._child;
  54. } else {
  55. this._nextActiveAction = this;
  56. }
  57. }
  58. public then(action: Action): Action {
  59. this._child = action;
  60. action._actionManager = this._actionManager;
  61. action._prepare();
  62. return action;
  63. }
  64. public _getProperty(propertyPath: string): string {
  65. return this._actionManager._getProperty(propertyPath);
  66. }
  67. public _getEffectiveTarget(target: any, propertyPath: string): any {
  68. return this._actionManager._getEffectiveTarget(target, propertyPath);
  69. }
  70. public serialize(parent: any): any {
  71. }
  72. // Called by BABYLON.Action objects in serialize(...). Internal use
  73. protected _serialize(serializedAction: any, parent?: any): any {
  74. var serializationObject: any = {
  75. type: 1,
  76. children: [],
  77. name: serializedAction.name,
  78. properties: serializedAction.properties || []
  79. };
  80. // Serialize child
  81. if (this._child) {
  82. this._child.serialize(serializationObject);
  83. }
  84. // Check if "this" has a condition
  85. if (this._condition) {
  86. var serializedCondition = this._condition.serialize();
  87. serializedCondition.children.push(serializationObject);
  88. if (parent) {
  89. parent.children.push(serializedCondition);
  90. }
  91. return serializedCondition;
  92. }
  93. if (parent) {
  94. parent.children.push(serializationObject);
  95. }
  96. return serializationObject;
  97. }
  98. public static _SerializeValueAsString = (value: any): string => {
  99. if (typeof value === "number") {
  100. return value.toString();
  101. }
  102. if (typeof value === "boolean") {
  103. return value ? "true" : "false";
  104. }
  105. if (value instanceof Vector2) {
  106. return value.x + ", " + value.y;
  107. }
  108. if (value instanceof Vector3) {
  109. return value.x + ", " + value.y + ", " + value.z;
  110. }
  111. if (value instanceof Color3) {
  112. return value.r + ", " + value.g + ", " + value.b;
  113. }
  114. if (value instanceof Color4) {
  115. return value.r + ", " + value.g + ", " + value.b + ", " + value.a;
  116. }
  117. return value; // string
  118. };
  119. public static _GetTargetProperty = (target: Scene | Node) => {
  120. return {
  121. name: "target",
  122. targetType: target instanceof Mesh ? "MeshProperties"
  123. : target instanceof Light ? "LightProperties"
  124. : target instanceof Camera ? "CameraProperties"
  125. : "SceneProperties",
  126. value: target instanceof Scene ? "Scene" : (<Node>target).name
  127. }
  128. };
  129. }
  130. }