babylon.actionManager.js 12 KB

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