babylon.actionManager.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. // Methods
  138. ActionManager.prototype.dispose = function () {
  139. var index = this._scene._actionManagers.indexOf(this);
  140. if (index > -1) {
  141. this._scene._actionManagers.splice(index, 1);
  142. }
  143. };
  144. ActionManager.prototype.getScene = function () {
  145. return this._scene;
  146. };
  147. /**
  148. * Does this action manager handles actions of any of the given triggers
  149. * @param {number[]} triggers - the triggers to be tested
  150. * @return {boolean} whether one (or more) of the triggers is handeled
  151. */
  152. ActionManager.prototype.hasSpecificTriggers = function (triggers) {
  153. for (var index = 0; index < this.actions.length; index++) {
  154. var action = this.actions[index];
  155. if (triggers.indexOf(action.trigger) > -1) {
  156. return true;
  157. }
  158. }
  159. return false;
  160. };
  161. Object.defineProperty(ActionManager.prototype, "hasPointerTriggers", {
  162. /**
  163. * Does this action manager has pointer triggers
  164. * @return {boolean} whether or not it has pointer triggers
  165. */
  166. get: function () {
  167. for (var index = 0; index < this.actions.length; index++) {
  168. var action = this.actions[index];
  169. if (action.trigger >= ActionManager._OnPickTrigger && action.trigger <= ActionManager._OnPointerOutTrigger) {
  170. return true;
  171. }
  172. }
  173. return false;
  174. },
  175. enumerable: true,
  176. configurable: true
  177. });
  178. Object.defineProperty(ActionManager.prototype, "hasPickTriggers", {
  179. /**
  180. * Does this action manager has pick triggers
  181. * @return {boolean} whether or not it has pick triggers
  182. */
  183. get: function () {
  184. for (var index = 0; index < this.actions.length; index++) {
  185. var action = this.actions[index];
  186. if (action.trigger >= ActionManager._OnPickTrigger && action.trigger <= ActionManager._OnCenterPickTrigger) {
  187. return true;
  188. }
  189. }
  190. return false;
  191. },
  192. enumerable: true,
  193. configurable: true
  194. });
  195. /**
  196. * Registers an action to this action manager
  197. * @param {BABYLON.Action} action - the action to be registered
  198. * @return {BABYLON.Action} the action amended (prepared) after registration
  199. */
  200. ActionManager.prototype.registerAction = function (action) {
  201. if (action.trigger === ActionManager.OnEveryFrameTrigger) {
  202. if (this.getScene().actionManager !== this) {
  203. BABYLON.Tools.Warn("OnEveryFrameTrigger can only be used with scene.actionManager");
  204. return null;
  205. }
  206. }
  207. this.actions.push(action);
  208. action._actionManager = this;
  209. action._prepare();
  210. return action;
  211. };
  212. /**
  213. * Process a specific trigger
  214. * @param {number} trigger - the trigger to process
  215. * @param evt {BABYLON.ActionEvent} the event details to be processed
  216. */
  217. ActionManager.prototype.processTrigger = function (trigger, evt) {
  218. for (var index = 0; index < this.actions.length; index++) {
  219. var action = this.actions[index];
  220. if (action.trigger === trigger) {
  221. if (trigger === ActionManager.OnKeyUpTrigger || trigger === ActionManager.OnKeyDownTrigger) {
  222. var parameter = action.getTriggerParameter();
  223. if (parameter) {
  224. var unicode = evt.sourceEvent.charCode ? evt.sourceEvent.charCode : evt.sourceEvent.keyCode;
  225. var actualkey = String.fromCharCode(unicode).toLowerCase();
  226. if (actualkey !== parameter.toLowerCase()) {
  227. continue;
  228. }
  229. }
  230. }
  231. action._executeCurrent(evt);
  232. }
  233. }
  234. };
  235. ActionManager.prototype._getEffectiveTarget = function (target, propertyPath) {
  236. var properties = propertyPath.split(".");
  237. for (var index = 0; index < properties.length - 1; index++) {
  238. target = target[properties[index]];
  239. }
  240. return target;
  241. };
  242. ActionManager.prototype._getProperty = function (propertyPath) {
  243. var properties = propertyPath.split(".");
  244. return properties[properties.length - 1];
  245. };
  246. // Statics
  247. ActionManager._NothingTrigger = 0;
  248. ActionManager._OnPickTrigger = 1;
  249. ActionManager._OnLeftPickTrigger = 2;
  250. ActionManager._OnRightPickTrigger = 3;
  251. ActionManager._OnCenterPickTrigger = 4;
  252. ActionManager._OnPointerOverTrigger = 5;
  253. ActionManager._OnPointerOutTrigger = 6;
  254. ActionManager._OnEveryFrameTrigger = 7;
  255. ActionManager._OnIntersectionEnterTrigger = 8;
  256. ActionManager._OnIntersectionExitTrigger = 9;
  257. ActionManager._OnKeyDownTrigger = 10;
  258. ActionManager._OnKeyUpTrigger = 11;
  259. return ActionManager;
  260. })();
  261. BABYLON.ActionManager = ActionManager;
  262. })(BABYLON || (BABYLON = {}));
  263. //# sourceMappingURL=babylon.actionManager.js.map