babylon.condition.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. module BABYLON {
  2. export class Condition {
  3. public _actionManager: ActionManager;
  4. public _evaluationId: number;
  5. public _currentResult: boolean;
  6. constructor(actionManager: ActionManager) {
  7. this._actionManager = actionManager;
  8. }
  9. public isValid(): boolean {
  10. return true;
  11. }
  12. public _getProperty(propertyPath: string): string {
  13. return this._actionManager._getProperty(propertyPath);
  14. }
  15. public _getEffectiveTarget(target: any, propertyPath: string): any {
  16. return this._actionManager._getEffectiveTarget(target, propertyPath);
  17. }
  18. public serialize(): any {
  19. }
  20. protected _serialize(serializedCondition: any): any {
  21. return {
  22. type: 2, // Condition
  23. children: [],
  24. name: serializedCondition.name,
  25. properties: serializedCondition.properties
  26. };
  27. }
  28. }
  29. export class ValueCondition extends Condition {
  30. // Statics
  31. private static _IsEqual = 0;
  32. private static _IsDifferent = 1;
  33. private static _IsGreater = 2;
  34. private static _IsLesser = 3;
  35. public static get IsEqual(): number {
  36. return ValueCondition._IsEqual;
  37. }
  38. public static get IsDifferent(): number {
  39. return ValueCondition._IsDifferent;
  40. }
  41. public static get IsGreater(): number {
  42. return ValueCondition._IsGreater;
  43. }
  44. public static get IsLesser(): number {
  45. return ValueCondition._IsLesser;
  46. }
  47. // Members
  48. public _actionManager: ActionManager;
  49. private _target: any;
  50. private _effectiveTarget: any;
  51. private _property: string;
  52. constructor(actionManager: ActionManager, target: any, public propertyPath: string, public value: any, public operator: number = ValueCondition.IsEqual) {
  53. super(actionManager);
  54. this._target = target;
  55. this._effectiveTarget = this._getEffectiveTarget(target, this.propertyPath);
  56. this._property = this._getProperty(this.propertyPath);
  57. }
  58. // Methods
  59. public isValid(): boolean {
  60. switch (this.operator) {
  61. case ValueCondition.IsGreater:
  62. return this._effectiveTarget[this._property] > this.value;
  63. case ValueCondition.IsLesser:
  64. return this._effectiveTarget[this._property] < this.value;
  65. case ValueCondition.IsEqual:
  66. case ValueCondition.IsDifferent:
  67. var check: boolean;
  68. if (this.value.equals) {
  69. check = this.value.equals(this._effectiveTarget[this._property]);
  70. } else {
  71. check = this.value === this._effectiveTarget[this._property];
  72. }
  73. return this.operator === ValueCondition.IsEqual ? check : !check;
  74. }
  75. return false;
  76. }
  77. public serialize(): any {
  78. return this._serialize({
  79. name: "ValueCondition",
  80. properties: [
  81. Action._GetTargetProperty(this._target),
  82. { name: "propertyPath", value: this.propertyPath },
  83. { name: "value", value: Action._SerializeValueAsString(this.value) },
  84. { name: "operator", value: ValueCondition.GetOperatorName(this.operator) }
  85. ]
  86. });
  87. }
  88. public static GetOperatorName(operator: number): string {
  89. switch (operator) {
  90. case ValueCondition._IsEqual: return "IsEqual";
  91. case ValueCondition._IsDifferent: return "IsDifferent";
  92. case ValueCondition._IsGreater: return "IsGreater";
  93. case ValueCondition._IsLesser: return "IsLesser";
  94. default: return "";
  95. }
  96. }
  97. }
  98. export class PredicateCondition extends Condition {
  99. // Members
  100. public _actionManager: ActionManager;
  101. constructor(actionManager: ActionManager, public predicate: () => boolean) {
  102. super(actionManager);
  103. }
  104. public isValid(): boolean {
  105. return this.predicate();
  106. }
  107. }
  108. export class StateCondition extends Condition {
  109. // Members
  110. public _actionManager: ActionManager;
  111. private _target: any;
  112. constructor(actionManager: ActionManager, target: any, public value: string) {
  113. super(actionManager);
  114. this._target = target;
  115. }
  116. // Methods
  117. public isValid(): boolean {
  118. return this._target.state === this.value;
  119. }
  120. public serialize(): any {
  121. return this._serialize({
  122. name: "StateCondition",
  123. properties: [
  124. Action._GetTargetProperty(this._target),
  125. { name: "value", value: this.value }
  126. ]
  127. });
  128. }
  129. }
  130. }