condition.ts 8.9 KB

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