babylon.condition.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. }
  19. export class ValueCondition extends Condition {
  20. // Statics
  21. private static _IsEqual = 0;
  22. private static _IsDifferent = 1;
  23. private static _IsGreater = 2;
  24. private static _IsLesser = 3;
  25. public static get IsEqual(): number {
  26. return ValueCondition._IsEqual;
  27. }
  28. public static get IsDifferent(): number {
  29. return ValueCondition._IsDifferent;
  30. }
  31. public static get IsGreater(): number {
  32. return ValueCondition._IsGreater;
  33. }
  34. public static get IsLesser(): number {
  35. return ValueCondition._IsLesser;
  36. }
  37. // Members
  38. public _actionManager: ActionManager;
  39. private _target: any;
  40. private _property: string;
  41. constructor(actionManager: ActionManager, target: any, public propertyPath: string, public value: any, public operator: number = ValueCondition.IsEqual) {
  42. super(actionManager);
  43. this._target = this._getEffectiveTarget(target, this.propertyPath);
  44. this._property = this._getProperty(this.propertyPath);
  45. }
  46. // Methods
  47. public isValid(): boolean {
  48. switch (this.operator) {
  49. case ValueCondition.IsGreater:
  50. return this._target[this._property] > this.value;
  51. case ValueCondition.IsLesser:
  52. return this._target[this._property] < this.value;
  53. case ValueCondition.IsEqual:
  54. case ValueCondition.IsDifferent:
  55. var check: boolean;
  56. if (this.value.equals) {
  57. check = this.value.equals(this._target[this._property]);
  58. } else {
  59. check = this.value === this._target[this._property];
  60. }
  61. return this.operator === ValueCondition.IsEqual ? check : !check;
  62. }
  63. return false;
  64. }
  65. }
  66. export class PredicateCondition extends Condition {
  67. // Members
  68. public _actionManager: ActionManager;
  69. constructor(actionManager: ActionManager, public predicate: () => boolean) {
  70. super(actionManager);
  71. }
  72. public isValid(): boolean {
  73. return this.predicate();
  74. }
  75. }
  76. export class StateCondition extends Condition {
  77. // Members
  78. public _actionManager: ActionManager;
  79. private _target: any;
  80. constructor(actionManager: ActionManager, target: any, public value: string) {
  81. super(actionManager);
  82. this._target = target;
  83. }
  84. // Methods
  85. public isValid(): boolean {
  86. return this._target.state === this.value;
  87. }
  88. }
  89. }