babylon.actionManager.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. module BABYLON {
  2. /**
  3. * ActionEvent is the event being sent when an action is triggered.
  4. */
  5. export class ActionEvent {
  6. /**
  7. * Creates a new ActionEvent
  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. * @param additionalData additional data for the event
  14. */
  15. constructor(
  16. /** The mesh or sprite that triggered the action */
  17. public source: any,
  18. /** The X mouse cursor position at the time of the event */
  19. public pointerX: number,
  20. /** The Y mouse cursor position at the time of the event */
  21. public pointerY: number,
  22. /** The mesh that is currently pointed at (can be null) */
  23. public meshUnderPointer: Nullable<AbstractMesh>,
  24. /** the original (browser) event that triggered the ActionEvent */
  25. public sourceEvent?: any,
  26. /** additional data for the event */
  27. public additionalData?: any) {
  28. }
  29. /**
  30. * Helper function to auto-create an ActionEvent from a source mesh.
  31. * @param source The source mesh that triggered the event
  32. * @param evt The original (browser) event
  33. * @param additionalData additional data for the event
  34. * @returns the new ActionEvent
  35. */
  36. public static CreateNew(source: AbstractMesh, evt?: Event, additionalData?: any): ActionEvent {
  37. var scene = source.getScene();
  38. return new ActionEvent(source, scene.pointerX, scene.pointerY, scene.meshUnderPointer, evt, additionalData);
  39. }
  40. /**
  41. * Helper function to auto-create an ActionEvent from a source sprite
  42. * @param source The source sprite that triggered the event
  43. * @param scene Scene associated with the sprite
  44. * @param evt The original (browser) event
  45. * @param additionalData additional data for the event
  46. * @returns the new ActionEvent
  47. */
  48. public static CreateNewFromSprite(source: Sprite, scene: Scene, evt?: Event, additionalData?: any): ActionEvent {
  49. return new ActionEvent(source, scene.pointerX, scene.pointerY, scene.meshUnderPointer, evt, additionalData);
  50. }
  51. /**
  52. * Helper function to auto-create an ActionEvent from a scene. If triggered by a mesh use ActionEvent.CreateNew
  53. * @param scene the scene where the event occurred
  54. * @param evt The original (browser) event
  55. * @returns the new ActionEvent
  56. */
  57. public static CreateNewFromScene(scene: Scene, evt: Event): ActionEvent {
  58. return new ActionEvent(null, scene.pointerX, scene.pointerY, scene.meshUnderPointer, evt);
  59. }
  60. /**
  61. * Helper function to auto-create an ActionEvent from a primitive
  62. * @param prim defines the target primitive
  63. * @param pointerPos defines the pointer position
  64. * @param evt The original (browser) event
  65. * @param additionalData additional data for the event
  66. * @returns the new ActionEvent
  67. */
  68. public static CreateNewFromPrimitive(prim: any, pointerPos: Vector2, evt?: Event, additionalData?: any): ActionEvent {
  69. return new ActionEvent(prim, pointerPos.x, pointerPos.y, null, evt, additionalData);
  70. }
  71. }
  72. /**
  73. * Action Manager manages all events to be triggered on a given mesh or the global scene.
  74. * A single scene can have many Action Managers to handle predefined actions on specific meshes.
  75. * @see http://doc.babylonjs.com/how_to/how_to_use_actions
  76. */
  77. export class ActionManager {
  78. // Statics
  79. private static _NothingTrigger = 0;
  80. private static _OnPickTrigger = 1;
  81. private static _OnLeftPickTrigger = 2;
  82. private static _OnRightPickTrigger = 3;
  83. private static _OnCenterPickTrigger = 4;
  84. private static _OnPickDownTrigger = 5;
  85. private static _OnDoublePickTrigger = 6;
  86. private static _OnPickUpTrigger = 7;
  87. private static _OnLongPressTrigger = 8;
  88. private static _OnPointerOverTrigger = 9;
  89. private static _OnPointerOutTrigger = 10;
  90. private static _OnEveryFrameTrigger = 11;
  91. private static _OnIntersectionEnterTrigger = 12;
  92. private static _OnIntersectionExitTrigger = 13;
  93. private static _OnKeyDownTrigger = 14;
  94. private static _OnKeyUpTrigger = 15;
  95. private static _OnPickOutTrigger = 16;
  96. /**
  97. * Nothing
  98. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
  99. */
  100. public static get NothingTrigger(): number {
  101. return ActionManager._NothingTrigger;
  102. }
  103. /**
  104. * On pick
  105. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
  106. */
  107. public static get OnPickTrigger(): number {
  108. return ActionManager._OnPickTrigger;
  109. }
  110. /**
  111. * On left pick
  112. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
  113. */
  114. public static get OnLeftPickTrigger(): number {
  115. return ActionManager._OnLeftPickTrigger;
  116. }
  117. /**
  118. * On right pick
  119. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
  120. */
  121. public static get OnRightPickTrigger(): number {
  122. return ActionManager._OnRightPickTrigger;
  123. }
  124. /**
  125. * On center pick
  126. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
  127. */
  128. public static get OnCenterPickTrigger(): number {
  129. return ActionManager._OnCenterPickTrigger;
  130. }
  131. /**
  132. * On pick down
  133. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
  134. */
  135. public static get OnPickDownTrigger(): number {
  136. return ActionManager._OnPickDownTrigger;
  137. }
  138. /**
  139. * On double pick
  140. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
  141. */
  142. public static get OnDoublePickTrigger(): number {
  143. return ActionManager._OnDoublePickTrigger;
  144. }
  145. /**
  146. * On pick up
  147. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
  148. */
  149. public static get OnPickUpTrigger(): number {
  150. return ActionManager._OnPickUpTrigger;
  151. }
  152. /**
  153. * On pick out.
  154. * This trigger will only be raised if you also declared a OnPickDown
  155. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
  156. */
  157. public static get OnPickOutTrigger(): number {
  158. return ActionManager._OnPickOutTrigger;
  159. }
  160. /**
  161. * On long press
  162. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
  163. */
  164. public static get OnLongPressTrigger(): number {
  165. return ActionManager._OnLongPressTrigger;
  166. }
  167. /**
  168. * On pointer over
  169. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
  170. */
  171. public static get OnPointerOverTrigger(): number {
  172. return ActionManager._OnPointerOverTrigger;
  173. }
  174. /**
  175. * On pointer out
  176. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
  177. */
  178. public static get OnPointerOutTrigger(): number {
  179. return ActionManager._OnPointerOutTrigger;
  180. }
  181. /**
  182. * On every frame
  183. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
  184. */
  185. public static get OnEveryFrameTrigger(): number {
  186. return ActionManager._OnEveryFrameTrigger;
  187. }
  188. /**
  189. * On intersection enter
  190. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
  191. */
  192. public static get OnIntersectionEnterTrigger(): number {
  193. return ActionManager._OnIntersectionEnterTrigger;
  194. }
  195. /**
  196. * On intersection exit
  197. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
  198. */
  199. public static get OnIntersectionExitTrigger(): number {
  200. return ActionManager._OnIntersectionExitTrigger;
  201. }
  202. /**
  203. * On key down
  204. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
  205. */
  206. public static get OnKeyDownTrigger(): number {
  207. return ActionManager._OnKeyDownTrigger;
  208. }
  209. /**
  210. * On key up
  211. * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
  212. */
  213. public static get OnKeyUpTrigger(): number {
  214. return ActionManager._OnKeyUpTrigger;
  215. }
  216. /** Gets the list of active triggers */
  217. public static Triggers: { [key: string]: number } = {};
  218. // Members
  219. /** Gets the list of actions */
  220. public actions = new Array<Action>();
  221. /** Gets the cursor to use when hovering items */
  222. public hoverCursor: string = '';
  223. private _scene: Scene;
  224. /**
  225. * Creates a new action manager
  226. * @param scene defines the hosting scene
  227. */
  228. constructor(scene: Scene) {
  229. this._scene = scene || Engine.LastCreatedScene;
  230. scene.actionManagers.push(this);
  231. }
  232. // Methods
  233. /**
  234. * Releases all associated resources
  235. */
  236. public dispose(): void {
  237. var index = this._scene.actionManagers.indexOf(this);
  238. for (var i = 0; i < this.actions.length; i++) {
  239. var action = this.actions[i];
  240. ActionManager.Triggers[action.trigger]--;
  241. if (ActionManager.Triggers[action.trigger] === 0) {
  242. delete ActionManager.Triggers[action.trigger]
  243. }
  244. }
  245. if (index > -1) {
  246. this._scene.actionManagers.splice(index, 1);
  247. }
  248. }
  249. /**
  250. * Gets hosting scene
  251. * @returns the hosting scene
  252. */
  253. public getScene(): Scene {
  254. return this._scene;
  255. }
  256. /**
  257. * Does this action manager handles actions of any of the given triggers
  258. * @param triggers defines the triggers to be tested
  259. * @return a boolean indicating whether one (or more) of the triggers is handled
  260. */
  261. public hasSpecificTriggers(triggers: number[]): boolean {
  262. for (var index = 0; index < this.actions.length; index++) {
  263. var action = this.actions[index];
  264. if (triggers.indexOf(action.trigger) > -1) {
  265. return true;
  266. }
  267. }
  268. return false;
  269. }
  270. /**
  271. * Does this action manager handles actions of any of the given triggers. This function takes two arguments for
  272. * speed.
  273. * @param triggerA defines the trigger to be tested
  274. * @param triggerB defines the trigger to be tested
  275. * @return a boolean indicating whether one (or more) of the triggers is handled
  276. */
  277. public hasSpecificTriggers2(triggerA: number, triggerB: number): boolean {
  278. for (var index = 0; index < this.actions.length; index++) {
  279. var action = this.actions[index];
  280. if (triggerA == action.trigger || triggerB == action.trigger) {
  281. return true;
  282. }
  283. }
  284. return false;
  285. }
  286. /**
  287. * Does this action manager handles actions of a given trigger
  288. * @param trigger defines the trigger to be tested
  289. * @param parameterPredicate defines an optional predicate to filter triggers by parameter
  290. * @return whether the trigger is handled
  291. */
  292. public hasSpecificTrigger(trigger: number, parameterPredicate?: (parameter: any) => boolean): boolean {
  293. for (var index = 0; index < this.actions.length; index++) {
  294. var action = this.actions[index];
  295. if (action.trigger === trigger) {
  296. if (parameterPredicate) {
  297. if (parameterPredicate(action.getTriggerParameter())) {
  298. return true;
  299. }
  300. } else {
  301. return true;
  302. }
  303. }
  304. }
  305. return false;
  306. }
  307. /**
  308. * Does this action manager has pointer triggers
  309. */
  310. public get hasPointerTriggers(): boolean {
  311. for (var index = 0; index < this.actions.length; index++) {
  312. var action = this.actions[index];
  313. if (action.trigger >= ActionManager._OnPickTrigger && action.trigger <= ActionManager._OnPointerOutTrigger) {
  314. return true;
  315. }
  316. }
  317. return false;
  318. }
  319. /**
  320. * Does this action manager has pick triggers
  321. */
  322. public get hasPickTriggers(): boolean {
  323. for (var index = 0; index < this.actions.length; index++) {
  324. var action = this.actions[index];
  325. if (action.trigger >= ActionManager._OnPickTrigger && action.trigger <= ActionManager._OnPickUpTrigger) {
  326. return true;
  327. }
  328. }
  329. return false;
  330. }
  331. /**
  332. * Does exist one action manager with at least one trigger
  333. **/
  334. public static get HasTriggers(): boolean {
  335. for (var t in ActionManager.Triggers) {
  336. if (ActionManager.Triggers.hasOwnProperty(t)) {
  337. return true;
  338. }
  339. }
  340. return false;
  341. }
  342. /**
  343. * Does exist one action manager with at least one pick trigger
  344. **/
  345. public static get HasPickTriggers(): boolean {
  346. for (var t in ActionManager.Triggers) {
  347. if (ActionManager.Triggers.hasOwnProperty(t)) {
  348. let t_int = parseInt(t);
  349. if (t_int >= ActionManager._OnPickTrigger && t_int <= ActionManager._OnPickUpTrigger) {
  350. return true;
  351. }
  352. }
  353. }
  354. return false;
  355. }
  356. /**
  357. * Does exist one action manager that handles actions of a given trigger
  358. * @param trigger defines the trigger to be tested
  359. * @return a boolean indicating whether the trigger is handeled by at least one action manager
  360. **/
  361. public static HasSpecificTrigger(trigger: number): boolean {
  362. for (var t in ActionManager.Triggers) {
  363. if (ActionManager.Triggers.hasOwnProperty(t)) {
  364. let t_int = parseInt(t);
  365. if (t_int === trigger) {
  366. return true;
  367. }
  368. }
  369. }
  370. return false;
  371. }
  372. /**
  373. * Registers an action to this action manager
  374. * @param action defines the action to be registered
  375. * @return the action amended (prepared) after registration
  376. */
  377. public registerAction(action: Action): Nullable<Action> {
  378. if (action.trigger === ActionManager.OnEveryFrameTrigger) {
  379. if (this.getScene().actionManager !== this) {
  380. Tools.Warn("OnEveryFrameTrigger can only be used with scene.actionManager");
  381. return null;
  382. }
  383. }
  384. this.actions.push(action);
  385. if (ActionManager.Triggers[action.trigger]) {
  386. ActionManager.Triggers[action.trigger]++;
  387. }
  388. else {
  389. ActionManager.Triggers[action.trigger] = 1;
  390. }
  391. action._actionManager = this;
  392. action._prepare();
  393. return action;
  394. }
  395. /**
  396. * Unregisters an action to this action manager
  397. * @param action defines the action to be unregistered
  398. * @return a boolean indicating whether the action has been unregistered
  399. */
  400. public unregisterAction(action: Action): Boolean {
  401. var index = this.actions.indexOf(action);
  402. if (index !== -1) {
  403. this.actions.splice(index, 1);
  404. ActionManager.Triggers[action.trigger] -= 1;
  405. if (ActionManager.Triggers[action.trigger] === 0) {
  406. delete ActionManager.Triggers[action.trigger]
  407. }
  408. delete action._actionManager;
  409. return true;
  410. }
  411. return false;
  412. }
  413. /**
  414. * Process a specific trigger
  415. * @param trigger defines the trigger to process
  416. * @param evt defines the event details to be processed
  417. */
  418. public processTrigger(trigger: number, evt?: ActionEvent): void {
  419. for (var index = 0; index < this.actions.length; index++) {
  420. var action = this.actions[index];
  421. if (action.trigger === trigger) {
  422. if (evt) {
  423. if (trigger === ActionManager.OnKeyUpTrigger
  424. || trigger === ActionManager.OnKeyDownTrigger) {
  425. var parameter = action.getTriggerParameter();
  426. if (parameter && parameter !== evt.sourceEvent.keyCode) {
  427. if (!parameter.toLowerCase) {
  428. continue;
  429. }
  430. var lowerCase = parameter.toLowerCase();
  431. if (lowerCase !== evt.sourceEvent.key) {
  432. var unicode = evt.sourceEvent.charCode ? evt.sourceEvent.charCode : evt.sourceEvent.keyCode;
  433. var actualkey = String.fromCharCode(unicode).toLowerCase();
  434. if (actualkey !== lowerCase) {
  435. continue;
  436. }
  437. }
  438. }
  439. }
  440. }
  441. action._executeCurrent(evt);
  442. }
  443. }
  444. }
  445. /** @hidden */
  446. public _getEffectiveTarget(target: any, propertyPath: string): any {
  447. var properties = propertyPath.split(".");
  448. for (var index = 0; index < properties.length - 1; index++) {
  449. target = target[properties[index]];
  450. }
  451. return target;
  452. }
  453. /** @hidden */
  454. public _getProperty(propertyPath: string): string {
  455. var properties = propertyPath.split(".");
  456. return properties[properties.length - 1];
  457. }
  458. /**
  459. * Serialize this manager to a JSON object
  460. * @param name defines the property name to store this manager
  461. * @returns a JSON representation of this manager
  462. */
  463. public serialize(name: string): any {
  464. var root = {
  465. children: new Array(),
  466. name: name,
  467. type: 3, // Root node
  468. properties: new Array() // Empty for root but required
  469. };
  470. for (var i = 0; i < this.actions.length; i++) {
  471. var triggerObject = {
  472. type: 0, // Trigger
  473. children: new Array(),
  474. name: ActionManager.GetTriggerName(this.actions[i].trigger),
  475. properties: new Array()
  476. };
  477. var triggerOptions = this.actions[i].triggerOptions;
  478. if (triggerOptions && typeof triggerOptions !== "number") {
  479. if (triggerOptions.parameter instanceof Node) {
  480. triggerObject.properties.push(Action._GetTargetProperty(triggerOptions.parameter));
  481. }
  482. else {
  483. var parameter = <any>{};
  484. Tools.DeepCopy(triggerOptions.parameter, parameter, ["mesh"]);
  485. if (triggerOptions.parameter.mesh) {
  486. parameter._meshId = triggerOptions.parameter.mesh.id;
  487. }
  488. triggerObject.properties.push({ name: "parameter", targetType: null, value: parameter });
  489. }
  490. }
  491. // Serialize child action, recursively
  492. this.actions[i].serialize(triggerObject);
  493. // Add serialized trigger
  494. root.children.push(triggerObject);
  495. }
  496. return root;
  497. }
  498. /**
  499. * Creates a new ActionManager from a JSON data
  500. * @param parsedActions defines the JSON data to read from
  501. * @param object defines the hosting mesh
  502. * @param scene defines the hosting scene
  503. */
  504. public static Parse(parsedActions: any, object: Nullable<AbstractMesh>, scene: Scene): void {
  505. var actionManager = new ActionManager(scene);
  506. if (object === null)
  507. scene.actionManager = actionManager;
  508. else
  509. object.actionManager = actionManager;
  510. // instanciate a new object
  511. var instanciate = (name: string, params: Array<any>): any => {
  512. // TODO: We will need to find a solution for the next line when using commonjs / es6 .
  513. var newInstance: Object = Object.create(Tools.Instantiate("BABYLON." + name).prototype);
  514. newInstance.constructor.apply(newInstance, params);
  515. return newInstance;
  516. };
  517. var parseParameter = (name: string, value: string, target: any, propertyPath: Nullable<string>): any => {
  518. if (propertyPath === null) {
  519. // String, boolean or float
  520. var floatValue = parseFloat(value);
  521. if (value === "true" || value === "false")
  522. return value === "true";
  523. else
  524. return isNaN(floatValue) ? value : floatValue;
  525. }
  526. var effectiveTarget = propertyPath.split(".");
  527. var values = value.split(",");
  528. // Get effective Target
  529. for (var i = 0; i < effectiveTarget.length; i++) {
  530. target = target[effectiveTarget[i]];
  531. }
  532. // Return appropriate value with its type
  533. if (typeof (target) === "boolean")
  534. return values[0] === "true";
  535. if (typeof (target) === "string")
  536. return values[0];
  537. // Parameters with multiple values such as Vector3 etc.
  538. var split = new Array<number>();
  539. for (var i = 0; i < values.length; i++)
  540. split.push(parseFloat(values[i]));
  541. if (target instanceof Vector3)
  542. return Vector3.FromArray(split);
  543. if (target instanceof Vector4)
  544. return Vector4.FromArray(split);
  545. if (target instanceof Color3)
  546. return Color3.FromArray(split);
  547. if (target instanceof Color4)
  548. return Color4.FromArray(split);
  549. return parseFloat(values[0]);
  550. };
  551. // traverse graph per trigger
  552. var traverse = (parsedAction: any, trigger: any, condition: Nullable<Condition>, action: Nullable<Action>, combineArray: Nullable<Array<Action>> = null) => {
  553. if (parsedAction.detached)
  554. return;
  555. var parameters = new Array<any>();
  556. var target: any = null;
  557. var propertyPath: Nullable<string> = null;
  558. var combine = parsedAction.combine && parsedAction.combine.length > 0;
  559. // Parameters
  560. if (parsedAction.type === 2)
  561. parameters.push(actionManager);
  562. else
  563. parameters.push(trigger);
  564. if (combine) {
  565. var actions = new Array<Action>();
  566. for (var j = 0; j < parsedAction.combine.length; j++) {
  567. traverse(parsedAction.combine[j], ActionManager.NothingTrigger, condition, action, actions);
  568. }
  569. parameters.push(actions);
  570. }
  571. else {
  572. for (var i = 0; i < parsedAction.properties.length; i++) {
  573. var value = parsedAction.properties[i].value;
  574. var name = parsedAction.properties[i].name;
  575. var targetType = parsedAction.properties[i].targetType;
  576. if (name === "target")
  577. if (targetType !== null && targetType === "SceneProperties")
  578. value = target = scene;
  579. else
  580. value = target = scene.getNodeByName(value);
  581. else if (name === "parent")
  582. value = scene.getNodeByName(value);
  583. else if (name === "sound")
  584. value = scene.getSoundByName(value);
  585. else if (name !== "propertyPath") {
  586. if (parsedAction.type === 2 && name === "operator")
  587. value = (<any>ValueCondition)[value];
  588. else
  589. value = parseParameter(name, value, target, name === "value" ? propertyPath : null);
  590. } else {
  591. propertyPath = value;
  592. }
  593. parameters.push(value);
  594. }
  595. }
  596. if (combineArray === null) {
  597. parameters.push(condition);
  598. }
  599. else {
  600. parameters.push(null);
  601. }
  602. // If interpolate value action
  603. if (parsedAction.name === "InterpolateValueAction") {
  604. var param = parameters[parameters.length - 2];
  605. parameters[parameters.length - 1] = param;
  606. parameters[parameters.length - 2] = condition;
  607. }
  608. // Action or condition(s) and not CombineAction
  609. var newAction = instanciate(parsedAction.name, parameters);
  610. if (newAction instanceof Condition && condition !== null) {
  611. var nothing = new DoNothingAction(trigger, condition);
  612. if (action)
  613. action.then(nothing);
  614. else
  615. actionManager.registerAction(nothing);
  616. action = nothing;
  617. }
  618. if (combineArray === null) {
  619. if (newAction instanceof Condition) {
  620. condition = newAction;
  621. newAction = action;
  622. } else {
  623. condition = null;
  624. if (action)
  625. action.then(newAction);
  626. else
  627. actionManager.registerAction(newAction);
  628. }
  629. }
  630. else {
  631. combineArray.push(newAction);
  632. }
  633. for (var i = 0; i < parsedAction.children.length; i++)
  634. traverse(parsedAction.children[i], trigger, condition, newAction, null);
  635. };
  636. // triggers
  637. for (var i = 0; i < parsedActions.children.length; i++) {
  638. var triggerParams: any;
  639. var trigger = parsedActions.children[i];
  640. if (trigger.properties.length > 0) {
  641. var param = trigger.properties[0].value;
  642. var value = trigger.properties[0].targetType === null ? param : scene.getMeshByName(param);
  643. if (value._meshId) {
  644. value.mesh = scene.getMeshByID(value._meshId);
  645. }
  646. triggerParams = { trigger: (<any>ActionManager)[trigger.name], parameter: value };
  647. }
  648. else
  649. triggerParams = (<any>ActionManager)[trigger.name];
  650. for (var j = 0; j < trigger.children.length; j++) {
  651. if (!trigger.detached)
  652. traverse(trigger.children[j], triggerParams, null, null);
  653. }
  654. }
  655. }
  656. /**
  657. * Get a trigger name by index
  658. * @param trigger defines the trigger index
  659. * @returns a trigger name
  660. */
  661. public static GetTriggerName(trigger: number): string {
  662. switch (trigger) {
  663. case 0: return "NothingTrigger";
  664. case 1: return "OnPickTrigger";
  665. case 2: return "OnLeftPickTrigger";
  666. case 3: return "OnRightPickTrigger";
  667. case 4: return "OnCenterPickTrigger";
  668. case 5: return "OnPickDownTrigger";
  669. case 6: return "OnPickUpTrigger";
  670. case 7: return "OnLongPressTrigger";
  671. case 8: return "OnPointerOverTrigger";
  672. case 9: return "OnPointerOutTrigger";
  673. case 10: return "OnEveryFrameTrigger";
  674. case 11: return "OnIntersectionEnterTrigger";
  675. case 12: return "OnIntersectionExitTrigger";
  676. case 13: return "OnKeyDownTrigger";
  677. case 14: return "OnKeyUpTrigger";
  678. case 15: return "OnPickOutTrigger";
  679. default: return "";
  680. }
  681. }
  682. }
  683. }