babylon.condition.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. module BABYLON {
  2. /**
  3. * A Condition applied to an Action
  4. */
  5. export class Condition {
  6. /**
  7. * Internal only - manager for action
  8. * @hidden
  9. */
  10. public _actionManager: ActionManager;
  11. /**
  12. * Internal only
  13. * @hidden
  14. */
  15. public _evaluationId: number;
  16. /**
  17. * Internal only
  18. * @hidden
  19. */
  20. public _currentResult: boolean;
  21. /**
  22. * Creates a new Condition
  23. * @param actionManager the manager of the action the condition is applied to
  24. */
  25. constructor(actionManager: ActionManager) {
  26. this._actionManager = actionManager;
  27. }
  28. /**
  29. * Check if the current condition is valid
  30. * @returns a boolean
  31. */
  32. public isValid(): boolean {
  33. return true;
  34. }
  35. /**
  36. * Internal only
  37. * @hidden
  38. */
  39. public _getProperty(propertyPath: string): string {
  40. return this._actionManager._getProperty(propertyPath);
  41. }
  42. /**
  43. * Internal only
  44. * @hidden
  45. */
  46. public _getEffectiveTarget(target: any, propertyPath: string): any {
  47. return this._actionManager._getEffectiveTarget(target, propertyPath);
  48. }
  49. /**
  50. * Serialize placeholder for child classes
  51. * @returns the serialized object
  52. */
  53. public serialize(): any {
  54. }
  55. /**
  56. * Internal only
  57. * @hidden
  58. */
  59. protected _serialize(serializedCondition: any): any {
  60. return {
  61. type: 2, // Condition
  62. children: [],
  63. name: serializedCondition.name,
  64. properties: serializedCondition.properties
  65. };
  66. }
  67. }
  68. /**
  69. * Defines specific conditional operators as extensions of Condition
  70. */
  71. export class ValueCondition extends Condition {
  72. /**
  73. * Internal only
  74. * @hidden
  75. */
  76. private static _IsEqual = 0;
  77. /**
  78. * Internal only
  79. * @hidden
  80. */
  81. private static _IsDifferent = 1;
  82. /**
  83. * Internal only
  84. * @hidden
  85. */
  86. private static _IsGreater = 2;
  87. /**
  88. * Internal only
  89. * @hidden
  90. */
  91. private static _IsLesser = 3;
  92. /**
  93. * returns the number for IsEqual
  94. */
  95. public static get IsEqual(): number {
  96. return ValueCondition._IsEqual;
  97. }
  98. /**
  99. * Returns the number for IsDifferent
  100. */
  101. public static get IsDifferent(): number {
  102. return ValueCondition._IsDifferent;
  103. }
  104. /**
  105. * Returns the number for IsGreater
  106. */
  107. public static get IsGreater(): number {
  108. return ValueCondition._IsGreater;
  109. }
  110. /**
  111. * Returns the number for IsLesser
  112. */
  113. public static get IsLesser(): number {
  114. return ValueCondition._IsLesser;
  115. }
  116. /**
  117. * Internal only The action manager for the condition
  118. * @hidden
  119. */
  120. public _actionManager: ActionManager;
  121. /**
  122. * Internal only
  123. * @hidden
  124. */
  125. private _target: any;
  126. /**
  127. * Internal only
  128. * @hidden
  129. */
  130. private _effectiveTarget: any;
  131. /**
  132. * Internal only
  133. * @hidden
  134. */
  135. private _property: string;
  136. /**
  137. * Creates a new ValueCondition
  138. * @param actionManager manager for the action the condition applies to
  139. * @param target for the action
  140. * @param propertyPath path to specify the property of the target the conditional operator uses
  141. * @param value the value compared by the conditional operator against the current value of the property
  142. * @param operator the conditional operator, default ValueCondition.IsEqual
  143. */
  144. constructor(actionManager: ActionManager, target: any,
  145. /** path to specify the property of the target the conditional operator uses */
  146. public propertyPath: string,
  147. /** the value compared by the conditional operator against the current value of the property */
  148. public value: any,
  149. /** the conditional operator, default ValueCondition.IsEqual */
  150. public operator: number = ValueCondition.IsEqual) {
  151. super(actionManager);
  152. this._target = target;
  153. this._effectiveTarget = this._getEffectiveTarget(target, this.propertyPath);
  154. this._property = this._getProperty(this.propertyPath);
  155. }
  156. /**
  157. * Compares the given value with the property value for the specified conditional operator
  158. * @returns the result of the comparison
  159. */
  160. public isValid(): boolean {
  161. switch (this.operator) {
  162. case ValueCondition.IsGreater:
  163. return this._effectiveTarget[this._property] > this.value;
  164. case ValueCondition.IsLesser:
  165. return this._effectiveTarget[this._property] < this.value;
  166. case ValueCondition.IsEqual:
  167. case ValueCondition.IsDifferent:
  168. var check: boolean;
  169. if (this.value.equals) {
  170. check = this.value.equals(this._effectiveTarget[this._property]);
  171. } else {
  172. check = this.value === this._effectiveTarget[this._property];
  173. }
  174. return this.operator === ValueCondition.IsEqual ? check : !check;
  175. }
  176. return false;
  177. }
  178. /**
  179. * Serialize the ValueCondition into a JSON compatible object
  180. * @returns serialization object
  181. */
  182. public serialize(): any {
  183. return this._serialize({
  184. name: "ValueCondition",
  185. properties: [
  186. Action._GetTargetProperty(this._target),
  187. { name: "propertyPath", value: this.propertyPath },
  188. { name: "value", value: Action._SerializeValueAsString(this.value) },
  189. { name: "operator", value: ValueCondition.GetOperatorName(this.operator) }
  190. ]
  191. });
  192. }
  193. /**
  194. * Gets the name of the conditional operator for the ValueCondition
  195. * @param operator the conditional operator
  196. * @returns the name
  197. */
  198. public static GetOperatorName(operator: number): string {
  199. switch (operator) {
  200. case ValueCondition._IsEqual: return "IsEqual";
  201. case ValueCondition._IsDifferent: return "IsDifferent";
  202. case ValueCondition._IsGreater: return "IsGreater";
  203. case ValueCondition._IsLesser: return "IsLesser";
  204. default: return "";
  205. }
  206. }
  207. }
  208. /**
  209. * Defines a predicate condition as an extension of Condition
  210. */
  211. export class PredicateCondition extends Condition {
  212. /**
  213. * Internal only - manager for action
  214. * @hidden
  215. */
  216. public _actionManager: ActionManager;
  217. /**
  218. * Creates a new PredicateCondition
  219. * @param actionManager manager for the action the condition applies to
  220. * @param predicate defines the predicate function used to validate the condition
  221. */
  222. constructor(actionManager: ActionManager,
  223. /** defines the predicate function used to validate the condition */
  224. public predicate: () => boolean) {
  225. super(actionManager);
  226. }
  227. /**
  228. * @returns the validity of the predicate condition
  229. */
  230. public isValid(): boolean {
  231. return this.predicate();
  232. }
  233. }
  234. /**
  235. * Defines a state condition as an extension of Condition
  236. */
  237. export class StateCondition extends Condition {
  238. /**
  239. * Internal only - manager for action
  240. * @hidden
  241. */
  242. public _actionManager: ActionManager;
  243. /**
  244. * Internal only
  245. * @hidden
  246. */
  247. private _target: any;
  248. /**
  249. * Creates a new StateCondition
  250. * @param actionManager manager for the action the condition applies to
  251. * @param target of the condition
  252. * @param value to compare with target state
  253. */
  254. constructor(actionManager: ActionManager, target: any, public value: string) {
  255. super(actionManager);
  256. this._target = target;
  257. }
  258. /**
  259. * @returns the validity of the state
  260. */
  261. public isValid(): boolean {
  262. return this._target.state === this.value;
  263. }
  264. /**
  265. * Serialize the StateCondition into a JSON compatible object
  266. * @returns serialization object
  267. */
  268. public serialize(): any {
  269. return this._serialize({
  270. name: "StateCondition",
  271. properties: [
  272. Action._GetTargetProperty(this._target),
  273. { name: "value", value: this.value }
  274. ]
  275. });
  276. }
  277. }
  278. }