babylon.actionManager.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. module BABYLON {
  2. /**
  3. * ActionEvent is the event beint sent when an action is triggered.
  4. */
  5. export class ActionEvent {
  6. /**
  7. * @constructor
  8. * @param source The mesh or sprite that triggered the action.
  9. * @param pointerX The X mouse cursor position at the time of the event
  10. * @param pointerY The Y mouse cursor position at the time of the event
  11. * @param meshUnderPointer The mesh that is currently pointed at (can be null)
  12. * @param sourceEvent the original (browser) event that triggered the ActionEvent
  13. */
  14. constructor(public source: any, public pointerX: number, public pointerY: number, public meshUnderPointer: AbstractMesh, public sourceEvent?: any, public additionalData?: any) {
  15. }
  16. /**
  17. * Helper function to auto-create an ActionEvent from a source mesh.
  18. * @param source The source mesh that triggered the event
  19. * @param evt {Event} The original (browser) event
  20. */
  21. public static CreateNew(source: AbstractMesh, evt?: Event, additionalData?: any): ActionEvent {
  22. var scene = source.getScene();
  23. return new ActionEvent(source, scene.pointerX, scene.pointerY, scene.meshUnderPointer, evt, additionalData);
  24. }
  25. /**
  26. * Helper function to auto-create an ActionEvent from a source mesh.
  27. * @param source The source sprite that triggered the event
  28. * @param scene Scene associated with the sprite
  29. * @param evt {Event} The original (browser) event
  30. */
  31. public static CreateNewFromSprite(source: Sprite, scene: Scene, evt?: Event, additionalData?: any): ActionEvent {
  32. return new ActionEvent(source, scene.pointerX, scene.pointerY, scene.meshUnderPointer, evt, additionalData);
  33. }
  34. /**
  35. * Helper function to auto-create an ActionEvent from a scene. If triggered by a mesh use ActionEvent.CreateNew
  36. * @param scene the scene where the event occurred
  37. * @param evt {Event} The original (browser) event
  38. */
  39. public static CreateNewFromScene(scene: Scene, evt: Event): ActionEvent {
  40. return new ActionEvent(null, scene.pointerX, scene.pointerY, scene.meshUnderPointer, evt);
  41. }
  42. public static CreateNewFromPrimitive(prim: any, pointerPos: Vector2, evt?: Event, additionalData?: any): ActionEvent {
  43. return new ActionEvent(prim, pointerPos.x, pointerPos.y, null, evt, additionalData);
  44. }
  45. }
  46. /**
  47. * Action Manager manages all events to be triggered on a given mesh or the global scene.
  48. * A single scene can have many Action Managers to handle predefined actions on specific meshes.
  49. */
  50. export class ActionManager {
  51. // Statics
  52. private static _NothingTrigger = 0;
  53. private static _OnPickTrigger = 1;
  54. private static _OnLeftPickTrigger = 2;
  55. private static _OnRightPickTrigger = 3;
  56. private static _OnCenterPickTrigger = 4;
  57. private static _OnPickDownTrigger = 5;
  58. private static _OnPickUpTrigger = 6;
  59. private static _OnLongPressTrigger = 7;
  60. private static _OnPointerOverTrigger = 8;
  61. private static _OnPointerOutTrigger = 9;
  62. private static _OnEveryFrameTrigger = 10;
  63. private static _OnIntersectionEnterTrigger = 11;
  64. private static _OnIntersectionExitTrigger = 12;
  65. private static _OnKeyDownTrigger = 13;
  66. private static _OnKeyUpTrigger = 14;
  67. private static _OnPickOutTrigger = 15;
  68. public static get NothingTrigger(): number {
  69. return ActionManager._NothingTrigger;
  70. }
  71. public static get OnPickTrigger(): number {
  72. return ActionManager._OnPickTrigger;
  73. }
  74. public static get OnLeftPickTrigger(): number {
  75. return ActionManager._OnLeftPickTrigger;
  76. }
  77. public static get OnRightPickTrigger(): number {
  78. return ActionManager._OnRightPickTrigger;
  79. }
  80. public static get OnCenterPickTrigger(): number {
  81. return ActionManager._OnCenterPickTrigger;
  82. }
  83. public static get OnPickDownTrigger(): number {
  84. return ActionManager._OnPickDownTrigger;
  85. }
  86. public static get OnPickUpTrigger(): number {
  87. return ActionManager._OnPickUpTrigger;
  88. }
  89. /// This trigger will only be raised if you also declared a OnPickDown
  90. public static get OnPickOutTrigger(): number {
  91. return ActionManager._OnPickOutTrigger;
  92. }
  93. public static get OnLongPressTrigger(): number {
  94. return ActionManager._OnLongPressTrigger;
  95. }
  96. public static get OnPointerOverTrigger(): number {
  97. return ActionManager._OnPointerOverTrigger;
  98. }
  99. public static get OnPointerOutTrigger(): number {
  100. return ActionManager._OnPointerOutTrigger;
  101. }
  102. public static get OnEveryFrameTrigger(): number {
  103. return ActionManager._OnEveryFrameTrigger;
  104. }
  105. public static get OnIntersectionEnterTrigger(): number {
  106. return ActionManager._OnIntersectionEnterTrigger;
  107. }
  108. public static get OnIntersectionExitTrigger(): number {
  109. return ActionManager._OnIntersectionExitTrigger;
  110. }
  111. public static get OnKeyDownTrigger(): number {
  112. return ActionManager._OnKeyDownTrigger;
  113. }
  114. public static get OnKeyUpTrigger(): number {
  115. return ActionManager._OnKeyUpTrigger;
  116. }
  117. public static DragMovementThreshold = 10; // in pixels
  118. public static LongPressDelay = 500; // in milliseconds
  119. // Members
  120. public actions = new Array<Action>();
  121. private _scene: Scene;
  122. constructor(scene: Scene) {
  123. this._scene = scene;
  124. scene._actionManagers.push(this);
  125. }
  126. // Methods
  127. public dispose(): void {
  128. var index = this._scene._actionManagers.indexOf(this);
  129. if (index > -1) {
  130. this._scene._actionManagers.splice(index, 1);
  131. }
  132. }
  133. public getScene(): Scene {
  134. return this._scene;
  135. }
  136. /**
  137. * Does this action manager handles actions of any of the given triggers
  138. * @param {number[]} triggers - the triggers to be tested
  139. * @return {boolean} whether one (or more) of the triggers is handeled
  140. */
  141. public hasSpecificTriggers(triggers: number[]): boolean {
  142. for (var index = 0; index < this.actions.length; index++) {
  143. var action = this.actions[index];
  144. if (triggers.indexOf(action.trigger) > -1) {
  145. return true;
  146. }
  147. }
  148. return false;
  149. }
  150. /**
  151. * Does this action manager handles actions of a given trigger
  152. * @param {number} trigger - the trigger to be tested
  153. * @return {boolean} whether the trigger is handeled
  154. */
  155. public hasSpecificTrigger(trigger: number): boolean {
  156. for (var index = 0; index < this.actions.length; index++) {
  157. var action = this.actions[index];
  158. if (action.trigger === trigger) {
  159. return true;
  160. }
  161. }
  162. return false;
  163. }
  164. /**
  165. * Does this action manager has pointer triggers
  166. * @return {boolean} whether or not it has pointer triggers
  167. */
  168. public get hasPointerTriggers(): boolean {
  169. for (var index = 0; index < this.actions.length; index++) {
  170. var action = this.actions[index];
  171. if (action.trigger >= ActionManager._OnPickTrigger && action.trigger <= ActionManager._OnPointerOutTrigger) {
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. /**
  178. * Does this action manager has pick triggers
  179. * @return {boolean} whether or not it has pick triggers
  180. */
  181. public get hasPickTriggers(): boolean {
  182. for (var index = 0; index < this.actions.length; index++) {
  183. var action = this.actions[index];
  184. if (action.trigger >= ActionManager._OnPickTrigger && action.trigger <= ActionManager._OnPickUpTrigger) {
  185. return true;
  186. }
  187. }
  188. return false;
  189. }
  190. /**
  191. * Registers an action to this action manager
  192. * @param {BABYLON.Action} action - the action to be registered
  193. * @return {BABYLON.Action} the action amended (prepared) after registration
  194. */
  195. public registerAction(action: Action): Action {
  196. if (action.trigger === ActionManager.OnEveryFrameTrigger) {
  197. if (this.getScene().actionManager !== this) {
  198. Tools.Warn("OnEveryFrameTrigger can only be used with scene.actionManager");
  199. return null;
  200. }
  201. }
  202. this.actions.push(action);
  203. action._actionManager = this;
  204. action._prepare();
  205. return action;
  206. }
  207. /**
  208. * Process a specific trigger
  209. * @param {number} trigger - the trigger to process
  210. * @param evt {BABYLON.ActionEvent} the event details to be processed
  211. */
  212. public processTrigger(trigger: number, evt: ActionEvent): void {
  213. for (var index = 0; index < this.actions.length; index++) {
  214. var action = this.actions[index];
  215. if (action.trigger === trigger) {
  216. if (trigger === ActionManager.OnKeyUpTrigger
  217. || trigger === ActionManager.OnKeyDownTrigger) {
  218. var parameter = action.getTriggerParameter();
  219. if (parameter) {
  220. var unicode = evt.sourceEvent.charCode ? evt.sourceEvent.charCode : evt.sourceEvent.keyCode;
  221. var actualkey = String.fromCharCode(unicode).toLowerCase();
  222. if (actualkey !== parameter.toLowerCase()) {
  223. continue;
  224. }
  225. }
  226. }
  227. action._executeCurrent(evt);
  228. }
  229. }
  230. }
  231. public _getEffectiveTarget(target: any, propertyPath: string): any {
  232. var properties = propertyPath.split(".");
  233. for (var index = 0; index < properties.length - 1; index++) {
  234. target = target[properties[index]];
  235. }
  236. return target;
  237. }
  238. public _getProperty(propertyPath: string): string {
  239. var properties = propertyPath.split(".");
  240. return properties[properties.length - 1];
  241. }
  242. public serialize(name: string): any {
  243. var root = {
  244. children: [],
  245. name: name,
  246. type: 3, // Root node
  247. properties: [] // Empty for root but required
  248. };
  249. for (var i = 0; i < this.actions.length; i++) {
  250. var triggerObject = {
  251. type: 0, // Trigger
  252. children: [],
  253. name: ActionManager.GetTriggerName(this.actions[i].trigger),
  254. properties: []
  255. };
  256. var triggerOptions = this.actions[i].triggerOptions;
  257. if (triggerOptions && typeof triggerOptions !== "number") {
  258. if (triggerOptions.parameter instanceof Node) {
  259. triggerObject.properties.push(Action._GetTargetProperty(triggerOptions.parameter));
  260. }
  261. else {
  262. triggerObject.properties.push({ name: "parameter", targetType: null, value: triggerOptions.parameter });
  263. }
  264. }
  265. // Serialize child action, recursively
  266. this.actions[i].serialize(triggerObject);
  267. // Add serialized trigger
  268. root.children.push(triggerObject);
  269. }
  270. return root;
  271. }
  272. public static Parse(parsedActions: any, object: AbstractMesh, scene: Scene) {
  273. var actionManager = new BABYLON.ActionManager(scene);
  274. if (object === null)
  275. scene.actionManager = actionManager;
  276. else
  277. object.actionManager = actionManager;
  278. // instanciate a new object
  279. var instanciate = (name: any, params: Array<any>): any => {
  280. var newInstance: Object = Object.create(BABYLON[name].prototype);
  281. newInstance.constructor.apply(newInstance, params);
  282. return newInstance;
  283. };
  284. var parseParameter = (name: string, value: string, target: any, propertyPath: string): any => {
  285. if (propertyPath === null) {
  286. // String, boolean or float
  287. var floatValue = parseFloat(value);
  288. if (value === "true" || value === "false")
  289. return value === "true";
  290. else
  291. return isNaN(floatValue) ? value : floatValue;
  292. }
  293. var effectiveTarget = propertyPath.split(".");
  294. var values = value.split(",");
  295. // Get effective Target
  296. for (var i = 0; i < effectiveTarget.length; i++) {
  297. target = target[effectiveTarget[i]];
  298. }
  299. // Return appropriate value with its type
  300. if (typeof (target) === "boolean")
  301. return values[0] === "true";
  302. if (typeof (target) === "string")
  303. return values[0];
  304. // Parameters with multiple values such as Vector3 etc.
  305. var split = new Array<number>();
  306. for (var i = 0; i < values.length; i++)
  307. split.push(parseFloat(values[i]));
  308. if (target instanceof Vector3)
  309. return Vector3.FromArray(split);
  310. if (target instanceof Vector4)
  311. return Vector4.FromArray(split);
  312. if (target instanceof Color3)
  313. return Color3.FromArray(split);
  314. if (target instanceof Color4)
  315. return Color4.FromArray(split);
  316. return parseFloat(values[0]);
  317. };
  318. // traverse graph per trigger
  319. var traverse = (parsedAction: any, trigger: any, condition: Condition, action: Action, combineArray: Array<Action> = null) => {
  320. if (parsedAction.detached)
  321. return;
  322. var parameters = new Array<any>();
  323. var target: any = null;
  324. var propertyPath: string = null;
  325. var combine = parsedAction.combine && parsedAction.combine.length > 0;
  326. // Parameters
  327. if (parsedAction.type === 2)
  328. parameters.push(actionManager);
  329. else
  330. parameters.push(trigger);
  331. if (combine) {
  332. var actions = new Array<Action>();
  333. for (var j = 0; j < parsedAction.combine.length; j++) {
  334. traverse(parsedAction.combine[j], ActionManager.NothingTrigger, condition, action, actions);
  335. }
  336. parameters.push(actions);
  337. }
  338. else {
  339. for (var i = 0; i < parsedAction.properties.length; i++) {
  340. var value = parsedAction.properties[i].value;
  341. var name = parsedAction.properties[i].name;
  342. var targetType = parsedAction.properties[i].targetType;
  343. if (name === "target")
  344. if (targetType !== null && targetType === "SceneProperties")
  345. value = target = scene;
  346. else
  347. value = target = scene.getNodeByName(value);
  348. else if (name === "parent")
  349. value = scene.getNodeByName(value);
  350. else if (name === "sound")
  351. value = scene.getSoundByName(value);
  352. else if (name !== "propertyPath") {
  353. if (parsedAction.type === 2 && name === "operator")
  354. value = ValueCondition[value];
  355. else
  356. value = parseParameter(name, value, target, name === "value" ? propertyPath : null);
  357. } else {
  358. propertyPath = value;
  359. }
  360. parameters.push(value);
  361. }
  362. }
  363. if (combineArray === null) {
  364. parameters.push(condition);
  365. }
  366. else {
  367. parameters.push(null);
  368. }
  369. // If interpolate value action
  370. if (parsedAction.name === "InterpolateValueAction") {
  371. var param = parameters[parameters.length - 2];
  372. parameters[parameters.length - 1] = param;
  373. parameters[parameters.length - 2] = condition;
  374. }
  375. // Action or condition(s) and not CombineAction
  376. var newAction = instanciate(parsedAction.name, parameters);
  377. if (newAction instanceof Condition && condition !== null) {
  378. var nothing = new DoNothingAction(trigger, condition);
  379. if (action)
  380. action.then(nothing);
  381. else
  382. actionManager.registerAction(nothing);
  383. action = nothing;
  384. }
  385. if (combineArray === null) {
  386. if (newAction instanceof Condition) {
  387. condition = newAction;
  388. newAction = action;
  389. } else {
  390. condition = null;
  391. if (action)
  392. action.then(newAction);
  393. else
  394. actionManager.registerAction(newAction);
  395. }
  396. }
  397. else {
  398. combineArray.push(newAction);
  399. }
  400. for (var i = 0; i < parsedAction.children.length; i++)
  401. traverse(parsedAction.children[i], trigger, condition, newAction, null);
  402. };
  403. // triggers
  404. for (var i = 0; i < parsedActions.children.length; i++) {
  405. var triggerParams: any;
  406. var trigger = parsedActions.children[i];
  407. if (trigger.properties.length > 0) {
  408. var param = trigger.properties[0].value;
  409. var value = trigger.properties[0].targetType === null ? param : scene.getMeshByName(param);
  410. triggerParams = { trigger: BABYLON.ActionManager[trigger.name], parameter: value };
  411. }
  412. else
  413. triggerParams = BABYLON.ActionManager[trigger.name];
  414. for (var j = 0; j < trigger.children.length; j++) {
  415. if (!trigger.detached)
  416. traverse(trigger.children[j], triggerParams, null, null);
  417. }
  418. }
  419. }
  420. public static GetTriggerName(trigger: number): string {
  421. switch (trigger) {
  422. case 0: return "NothingTrigger";
  423. case 1: return "OnPickTrigger";
  424. case 2: return "OnLeftPickTrigger";
  425. case 3: return "OnRightPickTrigger";
  426. case 4: return "OnCenterPickTrigger";
  427. case 5: return "OnPickDownTrigger";
  428. case 6: return "OnPickUpTrigger";
  429. case 7: return "OnLongPressTrigger";
  430. case 8: return "OnPointerOverTrigger";
  431. case 9: return "OnPointerOutTrigger";
  432. case 10: return "OnEveryFrameTrigger";
  433. case 11: return "OnIntersectionEnterTrigger";
  434. case 12: return "OnIntersectionExitTrigger";
  435. case 13: return "OnKeyDownTrigger";
  436. case 14: return "OnKeyUpTrigger";
  437. case 15: return "OnPickOutTrigger";
  438. default: return "";
  439. }
  440. }
  441. }
  442. }