condition.js 7.1 KB

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