babylon.actionManager.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. public hoverCursor: string = '';
  122. private _scene: Scene;
  123. constructor(scene: Scene) {
  124. this._scene = scene;
  125. scene._actionManagers.push(this);
  126. }
  127. // Methods
  128. public dispose(): void {
  129. var index = this._scene._actionManagers.indexOf(this);
  130. if (index > -1) {
  131. this._scene._actionManagers.splice(index, 1);
  132. }
  133. }
  134. public getScene(): Scene {
  135. return this._scene;
  136. }
  137. /**
  138. * Does this action manager handles actions of any of the given triggers
  139. * @param {number[]} triggers - the triggers to be tested
  140. * @return {boolean} whether one (or more) of the triggers is handeled
  141. */
  142. public hasSpecificTriggers(triggers: number[]): boolean {
  143. for (var index = 0; index < this.actions.length; index++) {
  144. var action = this.actions[index];
  145. if (triggers.indexOf(action.trigger) > -1) {
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. /**
  152. * Does this action manager handles actions of a given trigger
  153. * @param {number} trigger - the trigger to be tested
  154. * @return {boolean} whether the trigger is handeled
  155. */
  156. public hasSpecificTrigger(trigger: number): boolean {
  157. for (var index = 0; index < this.actions.length; index++) {
  158. var action = this.actions[index];
  159. if (action.trigger === trigger) {
  160. return true;
  161. }
  162. }
  163. return false;
  164. }
  165. /**
  166. * Does this action manager has pointer triggers
  167. * @return {boolean} whether or not it has pointer triggers
  168. */
  169. public get hasPointerTriggers(): boolean {
  170. for (var index = 0; index < this.actions.length; index++) {
  171. var action = this.actions[index];
  172. if (action.trigger >= ActionManager._OnPickTrigger && action.trigger <= ActionManager._OnPointerOutTrigger) {
  173. return true;
  174. }
  175. }
  176. return false;
  177. }
  178. /**
  179. * Does this action manager has pick triggers
  180. * @return {boolean} whether or not it has pick triggers
  181. */
  182. public get hasPickTriggers(): boolean {
  183. for (var index = 0; index < this.actions.length; index++) {
  184. var action = this.actions[index];
  185. if (action.trigger >= ActionManager._OnPickTrigger && action.trigger <= ActionManager._OnPickUpTrigger) {
  186. return true;
  187. }
  188. }
  189. return false;
  190. }
  191. /**
  192. * Registers an action to this action manager
  193. * @param {BABYLON.Action} action - the action to be registered
  194. * @return {BABYLON.Action} the action amended (prepared) after registration
  195. */
  196. public registerAction(action: Action): Action {
  197. if (action.trigger === ActionManager.OnEveryFrameTrigger) {
  198. if (this.getScene().actionManager !== this) {
  199. Tools.Warn("OnEveryFrameTrigger can only be used with scene.actionManager");
  200. return null;
  201. }
  202. }
  203. this.actions.push(action);
  204. action._actionManager = this;
  205. action._prepare();
  206. return action;
  207. }
  208. /**
  209. * Process a specific trigger
  210. * @param {number} trigger - the trigger to process
  211. * @param evt {BABYLON.ActionEvent} the event details to be processed
  212. */
  213. public processTrigger(trigger: number, evt: ActionEvent): void {
  214. for (var index = 0; index < this.actions.length; index++) {
  215. var action = this.actions[index];
  216. if (action.trigger === trigger) {
  217. if (trigger === ActionManager.OnKeyUpTrigger
  218. || trigger === ActionManager.OnKeyDownTrigger) {
  219. var parameter = action.getTriggerParameter();
  220. if (parameter) {
  221. var unicode = evt.sourceEvent.charCode ? evt.sourceEvent.charCode : evt.sourceEvent.keyCode;
  222. var actualkey = String.fromCharCode(unicode).toLowerCase();
  223. if (actualkey !== parameter.toLowerCase()) {
  224. continue;
  225. }
  226. }
  227. }
  228. action._executeCurrent(evt);
  229. }
  230. }
  231. }
  232. public _getEffectiveTarget(target: any, propertyPath: string): any {
  233. var properties = propertyPath.split(".");
  234. for (var index = 0; index < properties.length - 1; index++) {
  235. target = target[properties[index]];
  236. }
  237. return target;
  238. }
  239. public _getProperty(propertyPath: string): string {
  240. var properties = propertyPath.split(".");
  241. return properties[properties.length - 1];
  242. }
  243. public serialize(name: string): any {
  244. var root = {
  245. children: [],
  246. name: name,
  247. type: 3, // Root node
  248. properties: [] // Empty for root but required
  249. };
  250. for (var i = 0; i < this.actions.length; i++) {
  251. var triggerObject = {
  252. type: 0, // Trigger
  253. children: [],
  254. name: ActionManager.GetTriggerName(this.actions[i].trigger),
  255. properties: []
  256. };
  257. var triggerOptions = this.actions[i].triggerOptions;
  258. if (triggerOptions && typeof triggerOptions !== "number") {
  259. if (triggerOptions.parameter instanceof Node) {
  260. triggerObject.properties.push(Action._GetTargetProperty(triggerOptions.parameter));
  261. }
  262. else {
  263. triggerObject.properties.push({ name: "parameter", targetType: null, value: triggerOptions.parameter });
  264. }
  265. }
  266. // Serialize child action, recursively
  267. this.actions[i].serialize(triggerObject);
  268. // Add serialized trigger
  269. root.children.push(triggerObject);
  270. }
  271. return root;
  272. }
  273. public static Parse(parsedActions: any, object: AbstractMesh, scene: Scene) {
  274. var actionManager = new BABYLON.ActionManager(scene);
  275. if (object === null)
  276. scene.actionManager = actionManager;
  277. else
  278. object.actionManager = actionManager;
  279. // instanciate a new object
  280. var instanciate = (name: any, params: Array<any>): any => {
  281. var newInstance: Object = Object.create(BABYLON[name].prototype);
  282. newInstance.constructor.apply(newInstance, params);
  283. return newInstance;
  284. };
  285. var parseParameter = (name: string, value: string, target: any, propertyPath: string): any => {
  286. if (propertyPath === null) {
  287. // String, boolean or float
  288. var floatValue = parseFloat(value);
  289. if (value === "true" || value === "false")
  290. return value === "true";
  291. else
  292. return isNaN(floatValue) ? value : floatValue;
  293. }
  294. var effectiveTarget = propertyPath.split(".");
  295. var values = value.split(",");
  296. // Get effective Target
  297. for (var i = 0; i < effectiveTarget.length; i++) {
  298. target = target[effectiveTarget[i]];
  299. }
  300. // Return appropriate value with its type
  301. if (typeof (target) === "boolean")
  302. return values[0] === "true";
  303. if (typeof (target) === "string")
  304. return values[0];
  305. // Parameters with multiple values such as Vector3 etc.
  306. var split = new Array<number>();
  307. for (var i = 0; i < values.length; i++)
  308. split.push(parseFloat(values[i]));
  309. if (target instanceof Vector3)
  310. return Vector3.FromArray(split);
  311. if (target instanceof Vector4)
  312. return Vector4.FromArray(split);
  313. if (target instanceof Color3)
  314. return Color3.FromArray(split);
  315. if (target instanceof Color4)
  316. return Color4.FromArray(split);
  317. return parseFloat(values[0]);
  318. };
  319. // traverse graph per trigger
  320. var traverse = (parsedAction: any, trigger: any, condition: Condition, action: Action, combineArray: Array<Action> = null) => {
  321. if (parsedAction.detached)
  322. return;
  323. var parameters = new Array<any>();
  324. var target: any = null;
  325. var propertyPath: string = null;
  326. var combine = parsedAction.combine && parsedAction.combine.length > 0;
  327. // Parameters
  328. if (parsedAction.type === 2)
  329. parameters.push(actionManager);
  330. else
  331. parameters.push(trigger);
  332. if (combine) {
  333. var actions = new Array<Action>();
  334. for (var j = 0; j < parsedAction.combine.length; j++) {
  335. traverse(parsedAction.combine[j], ActionManager.NothingTrigger, condition, action, actions);
  336. }
  337. parameters.push(actions);
  338. }
  339. else {
  340. for (var i = 0; i < parsedAction.properties.length; i++) {
  341. var value = parsedAction.properties[i].value;
  342. var name = parsedAction.properties[i].name;
  343. var targetType = parsedAction.properties[i].targetType;
  344. if (name === "target")
  345. if (targetType !== null && targetType === "SceneProperties")
  346. value = target = scene;
  347. else
  348. value = target = scene.getNodeByName(value);
  349. else if (name === "parent")
  350. value = scene.getNodeByName(value);
  351. else if (name === "sound")
  352. value = scene.getSoundByName(value);
  353. else if (name !== "propertyPath") {
  354. if (parsedAction.type === 2 && name === "operator")
  355. value = ValueCondition[value];
  356. else
  357. value = parseParameter(name, value, target, name === "value" ? propertyPath : null);
  358. } else {
  359. propertyPath = value;
  360. }
  361. parameters.push(value);
  362. }
  363. }
  364. if (combineArray === null) {
  365. parameters.push(condition);
  366. }
  367. else {
  368. parameters.push(null);
  369. }
  370. // If interpolate value action
  371. if (parsedAction.name === "InterpolateValueAction") {
  372. var param = parameters[parameters.length - 2];
  373. parameters[parameters.length - 1] = param;
  374. parameters[parameters.length - 2] = condition;
  375. }
  376. // Action or condition(s) and not CombineAction
  377. var newAction = instanciate(parsedAction.name, parameters);
  378. if (newAction instanceof Condition && condition !== null) {
  379. var nothing = new DoNothingAction(trigger, condition);
  380. if (action)
  381. action.then(nothing);
  382. else
  383. actionManager.registerAction(nothing);
  384. action = nothing;
  385. }
  386. if (combineArray === null) {
  387. if (newAction instanceof Condition) {
  388. condition = newAction;
  389. newAction = action;
  390. } else {
  391. condition = null;
  392. if (action)
  393. action.then(newAction);
  394. else
  395. actionManager.registerAction(newAction);
  396. }
  397. }
  398. else {
  399. combineArray.push(newAction);
  400. }
  401. for (var i = 0; i < parsedAction.children.length; i++)
  402. traverse(parsedAction.children[i], trigger, condition, newAction, null);
  403. };
  404. // triggers
  405. for (var i = 0; i < parsedActions.children.length; i++) {
  406. var triggerParams: any;
  407. var trigger = parsedActions.children[i];
  408. if (trigger.properties.length > 0) {
  409. var param = trigger.properties[0].value;
  410. var value = trigger.properties[0].targetType === null ? param : scene.getMeshByName(param);
  411. triggerParams = { trigger: BABYLON.ActionManager[trigger.name], parameter: value };
  412. }
  413. else
  414. triggerParams = BABYLON.ActionManager[trigger.name];
  415. for (var j = 0; j < trigger.children.length; j++) {
  416. if (!trigger.detached)
  417. traverse(trigger.children[j], triggerParams, null, null);
  418. }
  419. }
  420. }
  421. public static GetTriggerName(trigger: number): string {
  422. switch (trigger) {
  423. case 0: return "NothingTrigger";
  424. case 1: return "OnPickTrigger";
  425. case 2: return "OnLeftPickTrigger";
  426. case 3: return "OnRightPickTrigger";
  427. case 4: return "OnCenterPickTrigger";
  428. case 5: return "OnPickDownTrigger";
  429. case 6: return "OnPickUpTrigger";
  430. case 7: return "OnLongPressTrigger";
  431. case 8: return "OnPointerOverTrigger";
  432. case 9: return "OnPointerOutTrigger";
  433. case 10: return "OnEveryFrameTrigger";
  434. case 11: return "OnIntersectionEnterTrigger";
  435. case 12: return "OnIntersectionExitTrigger";
  436. case 13: return "OnKeyDownTrigger";
  437. case 14: return "OnKeyUpTrigger";
  438. case 15: return "OnPickOutTrigger";
  439. default: return "";
  440. }
  441. }
  442. }
  443. }