followCameraKeyboardMoveInput.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import { __decorate } from "../../tslib.es6.js";
  2. import { CameraInputTypes } from "../../Cameras/cameraInputsManager.js";
  3. import { serialize } from "../../Misc/decorators.js";
  4. import { KeyboardEventTypes } from "../../Events/keyboardEvents.js";
  5. import { Tools } from "../../Misc/tools.js";
  6. /**
  7. * Manage the keyboard inputs to control the movement of a follow camera.
  8. * @see https://doc.babylonjs.com/features/featuresDeepDive/cameras/customizingCameraInputs
  9. */
  10. export class FollowCameraKeyboardMoveInput {
  11. constructor() {
  12. /**
  13. * Defines the list of key codes associated with the up action (increase heightOffset)
  14. */
  15. this.keysHeightOffsetIncr = [38];
  16. /**
  17. * Defines the list of key codes associated with the down action (decrease heightOffset)
  18. */
  19. this.keysHeightOffsetDecr = [40];
  20. /**
  21. * Defines whether the Alt modifier key is required to move up/down (alter heightOffset)
  22. */
  23. this.keysHeightOffsetModifierAlt = false;
  24. /**
  25. * Defines whether the Ctrl modifier key is required to move up/down (alter heightOffset)
  26. */
  27. this.keysHeightOffsetModifierCtrl = false;
  28. /**
  29. * Defines whether the Shift modifier key is required to move up/down (alter heightOffset)
  30. */
  31. this.keysHeightOffsetModifierShift = false;
  32. /**
  33. * Defines the list of key codes associated with the left action (increase rotationOffset)
  34. */
  35. this.keysRotationOffsetIncr = [37];
  36. /**
  37. * Defines the list of key codes associated with the right action (decrease rotationOffset)
  38. */
  39. this.keysRotationOffsetDecr = [39];
  40. /**
  41. * Defines whether the Alt modifier key is required to move left/right (alter rotationOffset)
  42. */
  43. this.keysRotationOffsetModifierAlt = false;
  44. /**
  45. * Defines whether the Ctrl modifier key is required to move left/right (alter rotationOffset)
  46. */
  47. this.keysRotationOffsetModifierCtrl = false;
  48. /**
  49. * Defines whether the Shift modifier key is required to move left/right (alter rotationOffset)
  50. */
  51. this.keysRotationOffsetModifierShift = false;
  52. /**
  53. * Defines the list of key codes associated with the zoom-in action (decrease radius)
  54. */
  55. this.keysRadiusIncr = [40];
  56. /**
  57. * Defines the list of key codes associated with the zoom-out action (increase radius)
  58. */
  59. this.keysRadiusDecr = [38];
  60. /**
  61. * Defines whether the Alt modifier key is required to zoom in/out (alter radius value)
  62. */
  63. this.keysRadiusModifierAlt = true;
  64. /**
  65. * Defines whether the Ctrl modifier key is required to zoom in/out (alter radius value)
  66. */
  67. this.keysRadiusModifierCtrl = false;
  68. /**
  69. * Defines whether the Shift modifier key is required to zoom in/out (alter radius value)
  70. */
  71. this.keysRadiusModifierShift = false;
  72. /**
  73. * Defines the rate of change of heightOffset.
  74. */
  75. this.heightSensibility = 1;
  76. /**
  77. * Defines the rate of change of rotationOffset.
  78. */
  79. this.rotationSensibility = 1;
  80. /**
  81. * Defines the rate of change of radius.
  82. */
  83. this.radiusSensibility = 1;
  84. this._keys = new Array();
  85. }
  86. /**
  87. * Attach the input controls to a specific dom element to get the input from.
  88. * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  89. */
  90. attachControl(noPreventDefault) {
  91. // eslint-disable-next-line prefer-rest-params
  92. noPreventDefault = Tools.BackCompatCameraNoPreventDefault(arguments);
  93. if (this._onCanvasBlurObserver) {
  94. return;
  95. }
  96. this._scene = this.camera.getScene();
  97. this._engine = this._scene.getEngine();
  98. this._onCanvasBlurObserver = this._engine.onCanvasBlurObservable.add(() => {
  99. this._keys.length = 0;
  100. });
  101. this._onKeyboardObserver = this._scene.onKeyboardObservable.add((info) => {
  102. const evt = info.event;
  103. if (!evt.metaKey) {
  104. if (info.type === KeyboardEventTypes.KEYDOWN) {
  105. this._ctrlPressed = evt.ctrlKey;
  106. this._altPressed = evt.altKey;
  107. this._shiftPressed = evt.shiftKey;
  108. if (this.keysHeightOffsetIncr.indexOf(evt.keyCode) !== -1 ||
  109. this.keysHeightOffsetDecr.indexOf(evt.keyCode) !== -1 ||
  110. this.keysRotationOffsetIncr.indexOf(evt.keyCode) !== -1 ||
  111. this.keysRotationOffsetDecr.indexOf(evt.keyCode) !== -1 ||
  112. this.keysRadiusIncr.indexOf(evt.keyCode) !== -1 ||
  113. this.keysRadiusDecr.indexOf(evt.keyCode) !== -1) {
  114. const index = this._keys.indexOf(evt.keyCode);
  115. if (index === -1) {
  116. this._keys.push(evt.keyCode);
  117. }
  118. if (evt.preventDefault) {
  119. if (!noPreventDefault) {
  120. evt.preventDefault();
  121. }
  122. }
  123. }
  124. }
  125. else {
  126. if (this.keysHeightOffsetIncr.indexOf(evt.keyCode) !== -1 ||
  127. this.keysHeightOffsetDecr.indexOf(evt.keyCode) !== -1 ||
  128. this.keysRotationOffsetIncr.indexOf(evt.keyCode) !== -1 ||
  129. this.keysRotationOffsetDecr.indexOf(evt.keyCode) !== -1 ||
  130. this.keysRadiusIncr.indexOf(evt.keyCode) !== -1 ||
  131. this.keysRadiusDecr.indexOf(evt.keyCode) !== -1) {
  132. const index = this._keys.indexOf(evt.keyCode);
  133. if (index >= 0) {
  134. this._keys.splice(index, 1);
  135. }
  136. if (evt.preventDefault) {
  137. if (!noPreventDefault) {
  138. evt.preventDefault();
  139. }
  140. }
  141. }
  142. }
  143. }
  144. });
  145. }
  146. /**
  147. * Detach the current controls from the specified dom element.
  148. */
  149. detachControl() {
  150. if (this._scene) {
  151. if (this._onKeyboardObserver) {
  152. this._scene.onKeyboardObservable.remove(this._onKeyboardObserver);
  153. }
  154. if (this._onCanvasBlurObserver) {
  155. this._engine.onCanvasBlurObservable.remove(this._onCanvasBlurObserver);
  156. }
  157. this._onKeyboardObserver = null;
  158. this._onCanvasBlurObserver = null;
  159. }
  160. this._keys.length = 0;
  161. }
  162. /**
  163. * Update the current camera state depending on the inputs that have been used this frame.
  164. * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
  165. */
  166. checkInputs() {
  167. if (this._onKeyboardObserver) {
  168. this._keys.forEach((keyCode) => {
  169. if (this.keysHeightOffsetIncr.indexOf(keyCode) !== -1 && this._modifierHeightOffset()) {
  170. this.camera.heightOffset += this.heightSensibility;
  171. }
  172. else if (this.keysHeightOffsetDecr.indexOf(keyCode) !== -1 && this._modifierHeightOffset()) {
  173. this.camera.heightOffset -= this.heightSensibility;
  174. }
  175. else if (this.keysRotationOffsetIncr.indexOf(keyCode) !== -1 && this._modifierRotationOffset()) {
  176. this.camera.rotationOffset += this.rotationSensibility;
  177. this.camera.rotationOffset %= 360;
  178. }
  179. else if (this.keysRotationOffsetDecr.indexOf(keyCode) !== -1 && this._modifierRotationOffset()) {
  180. this.camera.rotationOffset -= this.rotationSensibility;
  181. this.camera.rotationOffset %= 360;
  182. }
  183. else if (this.keysRadiusIncr.indexOf(keyCode) !== -1 && this._modifierRadius()) {
  184. this.camera.radius += this.radiusSensibility;
  185. }
  186. else if (this.keysRadiusDecr.indexOf(keyCode) !== -1 && this._modifierRadius()) {
  187. this.camera.radius -= this.radiusSensibility;
  188. }
  189. });
  190. }
  191. }
  192. /**
  193. * Gets the class name of the current input.
  194. * @returns the class name
  195. */
  196. getClassName() {
  197. return "FollowCameraKeyboardMoveInput";
  198. }
  199. /**
  200. * Get the friendly name associated with the input class.
  201. * @returns the input friendly name
  202. */
  203. getSimpleName() {
  204. return "keyboard";
  205. }
  206. /**
  207. * Check if the pressed modifier keys (Alt/Ctrl/Shift) match those configured to
  208. * allow modification of the heightOffset value.
  209. * @returns true if modifier keys match
  210. */
  211. _modifierHeightOffset() {
  212. return (this.keysHeightOffsetModifierAlt === this._altPressed &&
  213. this.keysHeightOffsetModifierCtrl === this._ctrlPressed &&
  214. this.keysHeightOffsetModifierShift === this._shiftPressed);
  215. }
  216. /**
  217. * Check if the pressed modifier keys (Alt/Ctrl/Shift) match those configured to
  218. * allow modification of the rotationOffset value.
  219. * @returns true if modifier keys match
  220. */
  221. _modifierRotationOffset() {
  222. return (this.keysRotationOffsetModifierAlt === this._altPressed &&
  223. this.keysRotationOffsetModifierCtrl === this._ctrlPressed &&
  224. this.keysRotationOffsetModifierShift === this._shiftPressed);
  225. }
  226. /**
  227. * Check if the pressed modifier keys (Alt/Ctrl/Shift) match those configured to
  228. * allow modification of the radius value.
  229. * @returns true if modifier keys match
  230. */
  231. _modifierRadius() {
  232. return this.keysRadiusModifierAlt === this._altPressed && this.keysRadiusModifierCtrl === this._ctrlPressed && this.keysRadiusModifierShift === this._shiftPressed;
  233. }
  234. }
  235. __decorate([
  236. serialize()
  237. ], FollowCameraKeyboardMoveInput.prototype, "keysHeightOffsetIncr", void 0);
  238. __decorate([
  239. serialize()
  240. ], FollowCameraKeyboardMoveInput.prototype, "keysHeightOffsetDecr", void 0);
  241. __decorate([
  242. serialize()
  243. ], FollowCameraKeyboardMoveInput.prototype, "keysHeightOffsetModifierAlt", void 0);
  244. __decorate([
  245. serialize()
  246. ], FollowCameraKeyboardMoveInput.prototype, "keysHeightOffsetModifierCtrl", void 0);
  247. __decorate([
  248. serialize()
  249. ], FollowCameraKeyboardMoveInput.prototype, "keysHeightOffsetModifierShift", void 0);
  250. __decorate([
  251. serialize()
  252. ], FollowCameraKeyboardMoveInput.prototype, "keysRotationOffsetIncr", void 0);
  253. __decorate([
  254. serialize()
  255. ], FollowCameraKeyboardMoveInput.prototype, "keysRotationOffsetDecr", void 0);
  256. __decorate([
  257. serialize()
  258. ], FollowCameraKeyboardMoveInput.prototype, "keysRotationOffsetModifierAlt", void 0);
  259. __decorate([
  260. serialize()
  261. ], FollowCameraKeyboardMoveInput.prototype, "keysRotationOffsetModifierCtrl", void 0);
  262. __decorate([
  263. serialize()
  264. ], FollowCameraKeyboardMoveInput.prototype, "keysRotationOffsetModifierShift", void 0);
  265. __decorate([
  266. serialize()
  267. ], FollowCameraKeyboardMoveInput.prototype, "keysRadiusIncr", void 0);
  268. __decorate([
  269. serialize()
  270. ], FollowCameraKeyboardMoveInput.prototype, "keysRadiusDecr", void 0);
  271. __decorate([
  272. serialize()
  273. ], FollowCameraKeyboardMoveInput.prototype, "keysRadiusModifierAlt", void 0);
  274. __decorate([
  275. serialize()
  276. ], FollowCameraKeyboardMoveInput.prototype, "keysRadiusModifierCtrl", void 0);
  277. __decorate([
  278. serialize()
  279. ], FollowCameraKeyboardMoveInput.prototype, "keysRadiusModifierShift", void 0);
  280. __decorate([
  281. serialize()
  282. ], FollowCameraKeyboardMoveInput.prototype, "heightSensibility", void 0);
  283. __decorate([
  284. serialize()
  285. ], FollowCameraKeyboardMoveInput.prototype, "rotationSensibility", void 0);
  286. __decorate([
  287. serialize()
  288. ], FollowCameraKeyboardMoveInput.prototype, "radiusSensibility", void 0);
  289. CameraInputTypes["FollowCameraKeyboardMoveInput"] = FollowCameraKeyboardMoveInput;
  290. //# sourceMappingURL=followCameraKeyboardMoveInput.js.map