babylon.actionManager.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. var BABYLON;
  2. (function (BABYLON) {
  3. /**
  4. * ActionEvent is the event beint sent when an action is triggered.
  5. */
  6. var ActionEvent = (function () {
  7. /**
  8. * @constructor
  9. * @param source The mesh or sprite that triggered the action.
  10. * @param pointerX The X mouse cursor position at the time of the event
  11. * @param pointerY The Y mouse cursor position at the time of the event
  12. * @param meshUnderPointer The mesh that is currently pointed at (can be null)
  13. * @param sourceEvent the original (browser) event that triggered the ActionEvent
  14. */
  15. function ActionEvent(source, pointerX, pointerY, meshUnderPointer, sourceEvent, additionalData) {
  16. this.source = source;
  17. this.pointerX = pointerX;
  18. this.pointerY = pointerY;
  19. this.meshUnderPointer = meshUnderPointer;
  20. this.sourceEvent = sourceEvent;
  21. this.additionalData = additionalData;
  22. }
  23. /**
  24. * Helper function to auto-create an ActionEvent from a source mesh.
  25. * @param source The source mesh that triggered the event
  26. * @param evt {Event} The original (browser) event
  27. */
  28. ActionEvent.CreateNew = function (source, evt, additionalData) {
  29. var scene = source.getScene();
  30. return new ActionEvent(source, scene.pointerX, scene.pointerY, scene.meshUnderPointer, evt, additionalData);
  31. };
  32. /**
  33. * Helper function to auto-create an ActionEvent from a source mesh.
  34. * @param source The source sprite that triggered the event
  35. * @param scene Scene associated with the sprite
  36. * @param evt {Event} The original (browser) event
  37. */
  38. ActionEvent.CreateNewFromSprite = function (source, scene, evt, additionalData) {
  39. return new ActionEvent(source, scene.pointerX, scene.pointerY, scene.meshUnderPointer, evt, additionalData);
  40. };
  41. /**
  42. * Helper function to auto-create an ActionEvent from a scene. If triggered by a mesh use ActionEvent.CreateNew
  43. * @param scene the scene where the event occurred
  44. * @param evt {Event} The original (browser) event
  45. */
  46. ActionEvent.CreateNewFromScene = function (scene, evt) {
  47. return new ActionEvent(null, scene.pointerX, scene.pointerY, scene.meshUnderPointer, evt);
  48. };
  49. return ActionEvent;
  50. })();
  51. BABYLON.ActionEvent = ActionEvent;
  52. /**
  53. * Action Manager manages all events to be triggered on a given mesh or the global scene.
  54. * A single scene can have many Action Managers to handle predefined actions on specific meshes.
  55. */
  56. var ActionManager = (function () {
  57. function ActionManager(scene) {
  58. // Members
  59. this.actions = new Array();
  60. this._scene = scene;
  61. scene._actionManagers.push(this);
  62. }
  63. Object.defineProperty(ActionManager, "NothingTrigger", {
  64. get: function () {
  65. return ActionManager._NothingTrigger;
  66. },
  67. enumerable: true,
  68. configurable: true
  69. });
  70. Object.defineProperty(ActionManager, "OnPickTrigger", {
  71. get: function () {
  72. return ActionManager._OnPickTrigger;
  73. },
  74. enumerable: true,
  75. configurable: true
  76. });
  77. Object.defineProperty(ActionManager, "OnLeftPickTrigger", {
  78. get: function () {
  79. return ActionManager._OnLeftPickTrigger;
  80. },
  81. enumerable: true,
  82. configurable: true
  83. });
  84. Object.defineProperty(ActionManager, "OnRightPickTrigger", {
  85. get: function () {
  86. return ActionManager._OnRightPickTrigger;
  87. },
  88. enumerable: true,
  89. configurable: true
  90. });
  91. Object.defineProperty(ActionManager, "OnCenterPickTrigger", {
  92. get: function () {
  93. return ActionManager._OnCenterPickTrigger;
  94. },
  95. enumerable: true,
  96. configurable: true
  97. });
  98. Object.defineProperty(ActionManager, "OnPointerOverTrigger", {
  99. get: function () {
  100. return ActionManager._OnPointerOverTrigger;
  101. },
  102. enumerable: true,
  103. configurable: true
  104. });
  105. Object.defineProperty(ActionManager, "OnPointerOutTrigger", {
  106. get: function () {
  107. return ActionManager._OnPointerOutTrigger;
  108. },
  109. enumerable: true,
  110. configurable: true
  111. });
  112. Object.defineProperty(ActionManager, "OnEveryFrameTrigger", {
  113. get: function () {
  114. return ActionManager._OnEveryFrameTrigger;
  115. },
  116. enumerable: true,
  117. configurable: true
  118. });
  119. Object.defineProperty(ActionManager, "OnIntersectionEnterTrigger", {
  120. get: function () {
  121. return ActionManager._OnIntersectionEnterTrigger;
  122. },
  123. enumerable: true,
  124. configurable: true
  125. });
  126. Object.defineProperty(ActionManager, "OnIntersectionExitTrigger", {
  127. get: function () {
  128. return ActionManager._OnIntersectionExitTrigger;
  129. },
  130. enumerable: true,
  131. configurable: true
  132. });
  133. Object.defineProperty(ActionManager, "OnKeyDownTrigger", {
  134. get: function () {
  135. return ActionManager._OnKeyDownTrigger;
  136. },
  137. enumerable: true,
  138. configurable: true
  139. });
  140. Object.defineProperty(ActionManager, "OnKeyUpTrigger", {
  141. get: function () {
  142. return ActionManager._OnKeyUpTrigger;
  143. },
  144. enumerable: true,
  145. configurable: true
  146. });
  147. Object.defineProperty(ActionManager, "OnPickUpTrigger", {
  148. get: function () {
  149. return ActionManager._OnPickUpTrigger;
  150. },
  151. enumerable: true,
  152. configurable: true
  153. });
  154. // Methods
  155. ActionManager.prototype.dispose = function () {
  156. var index = this._scene._actionManagers.indexOf(this);
  157. if (index > -1) {
  158. this._scene._actionManagers.splice(index, 1);
  159. }
  160. };
  161. ActionManager.prototype.getScene = function () {
  162. return this._scene;
  163. };
  164. /**
  165. * Does this action manager handles actions of any of the given triggers
  166. * @param {number[]} triggers - the triggers to be tested
  167. * @return {boolean} whether one (or more) of the triggers is handeled
  168. */
  169. ActionManager.prototype.hasSpecificTriggers = function (triggers) {
  170. for (var index = 0; index < this.actions.length; index++) {
  171. var action = this.actions[index];
  172. if (triggers.indexOf(action.trigger) > -1) {
  173. return true;
  174. }
  175. }
  176. return false;
  177. };
  178. /**
  179. * Does this action manager handles actions of a given trigger
  180. * @param {number} trigger - the trigger to be tested
  181. * @return {boolean} whether the trigger is handeled
  182. */
  183. ActionManager.prototype.hasSpecificTrigger = function (trigger) {
  184. for (var index = 0; index < this.actions.length; index++) {
  185. var action = this.actions[index];
  186. if (action.trigger === trigger) {
  187. return true;
  188. }
  189. }
  190. return false;
  191. };
  192. Object.defineProperty(ActionManager.prototype, "hasPointerTriggers", {
  193. /**
  194. * Does this action manager has pointer triggers
  195. * @return {boolean} whether or not it has pointer triggers
  196. */
  197. get: function () {
  198. for (var index = 0; index < this.actions.length; index++) {
  199. var action = this.actions[index];
  200. if (action.trigger >= ActionManager._OnPickTrigger && action.trigger <= ActionManager._OnPointerOutTrigger) {
  201. return true;
  202. }
  203. if (action.trigger === ActionManager._OnPickUpTrigger) {
  204. return true;
  205. }
  206. }
  207. return false;
  208. },
  209. enumerable: true,
  210. configurable: true
  211. });
  212. Object.defineProperty(ActionManager.prototype, "hasPickTriggers", {
  213. /**
  214. * Does this action manager has pick triggers
  215. * @return {boolean} whether or not it has pick triggers
  216. */
  217. get: function () {
  218. for (var index = 0; index < this.actions.length; index++) {
  219. var action = this.actions[index];
  220. if (action.trigger >= ActionManager._OnPickTrigger && action.trigger <= ActionManager._OnCenterPickTrigger) {
  221. return true;
  222. }
  223. if (action.trigger === ActionManager._OnPickUpTrigger) {
  224. return true;
  225. }
  226. }
  227. return false;
  228. },
  229. enumerable: true,
  230. configurable: true
  231. });
  232. /**
  233. * Registers an action to this action manager
  234. * @param {BABYLON.Action} action - the action to be registered
  235. * @return {BABYLON.Action} the action amended (prepared) after registration
  236. */
  237. ActionManager.prototype.registerAction = function (action) {
  238. if (action.trigger === ActionManager.OnEveryFrameTrigger) {
  239. if (this.getScene().actionManager !== this) {
  240. BABYLON.Tools.Warn("OnEveryFrameTrigger can only be used with scene.actionManager");
  241. return null;
  242. }
  243. }
  244. this.actions.push(action);
  245. action._actionManager = this;
  246. action._prepare();
  247. return action;
  248. };
  249. /**
  250. * Process a specific trigger
  251. * @param {number} trigger - the trigger to process
  252. * @param evt {BABYLON.ActionEvent} the event details to be processed
  253. */
  254. ActionManager.prototype.processTrigger = function (trigger, evt) {
  255. for (var index = 0; index < this.actions.length; index++) {
  256. var action = this.actions[index];
  257. if (action.trigger === trigger) {
  258. if (trigger === ActionManager.OnKeyUpTrigger
  259. || trigger === ActionManager.OnKeyDownTrigger) {
  260. var parameter = action.getTriggerParameter();
  261. if (parameter) {
  262. var unicode = evt.sourceEvent.charCode ? evt.sourceEvent.charCode : evt.sourceEvent.keyCode;
  263. var actualkey = String.fromCharCode(unicode).toLowerCase();
  264. if (actualkey !== parameter.toLowerCase()) {
  265. continue;
  266. }
  267. }
  268. }
  269. action._executeCurrent(evt);
  270. }
  271. }
  272. };
  273. ActionManager.prototype._getEffectiveTarget = function (target, propertyPath) {
  274. var properties = propertyPath.split(".");
  275. for (var index = 0; index < properties.length - 1; index++) {
  276. target = target[properties[index]];
  277. }
  278. return target;
  279. };
  280. ActionManager.prototype._getProperty = function (propertyPath) {
  281. var properties = propertyPath.split(".");
  282. return properties[properties.length - 1];
  283. };
  284. // Statics
  285. ActionManager._NothingTrigger = 0;
  286. ActionManager._OnPickTrigger = 1;
  287. ActionManager._OnLeftPickTrigger = 2;
  288. ActionManager._OnRightPickTrigger = 3;
  289. ActionManager._OnCenterPickTrigger = 4;
  290. ActionManager._OnPointerOverTrigger = 5;
  291. ActionManager._OnPointerOutTrigger = 6;
  292. ActionManager._OnEveryFrameTrigger = 7;
  293. ActionManager._OnIntersectionEnterTrigger = 8;
  294. ActionManager._OnIntersectionExitTrigger = 9;
  295. ActionManager._OnKeyDownTrigger = 10;
  296. ActionManager._OnKeyUpTrigger = 11;
  297. ActionManager._OnPickUpTrigger = 12;
  298. return ActionManager;
  299. })();
  300. BABYLON.ActionManager = ActionManager;
  301. })(BABYLON || (BABYLON = {}));