API Docs for: 2.0.0
Show:

File: src/viewer/input/keyboardOrbitCamera.js

  1. /**
  2. A **KeyboardOrbitCamera** lets you orbit a {{#crossLink "Camera"}}{{/crossLink}} about its point-of-interest using the keyboard's arrow keys.
  3.  
  4. ## Overview
  5.  
  6. <ul>
  7. <li>Orbiting involves rotating the {{#crossLink "Camera"}}Camera's{{/crossLink}} {{#crossLink "Camera/eye:property"}}{{/crossLink}}
  8. position about its current {{#crossLink "Camera/look:property"}}{{/crossLink}} position.</li>
  9. <li>The orbit is freely rotating, without gimbal-lock.</li>
  10. <li>If desired, you can have multiple KeyboardOrbitCameras within the same {{#crossLink "Viewer"}}{{/crossLink}}.</li>
  11. <li>Multiple KeyboardOrbitCameras can drive the same {{#crossLink "Camera"}}{{/crossLink}}, or can each drive their own separate {{#crossLink "Camera"}}Cameras{{/crossLink}}.</li>
  12. <li>At any instant, the KeyboardOrbitCameras we're driving is the one whose {{#crossLink "KeyboardOrbitCamera/active:property"}}active{{/crossLink}} property is true.</li>
  13. <li>You can switch a KeyboardOrbitCameras to a different {{#crossLink "Camera"}}{{/crossLink}} at any time.</li>
  14. </ul>
  15.  
  16. ## Example
  17.  
  18. <iframe style="width: 600px; height: 400px" src="../../examples/control_KeyboardOrbitCamera.html"></iframe>
  19.  
  20. @class KeyboardOrbitCamera
  21. @module BIMSURFER
  22. @submodule input
  23. @constructor
  24. @param [viewer] {Viewer} Parent {{#crossLink "Viewer"}}{{/crossLink}}.
  25. @param [cfg] {*} Configs
  26. @param [cfg.id] {String} Optional ID, unique among all components in the parent viewer, generated automatically when omitted.
  27. @param [cfg.meta] {String:Object} Optional map of user-defined metadata to attach to this KeyboardAxisCamera.
  28. @param [camera] {Camera} The {{#crossLink "Camera"}}{{/crossLink}} to control.
  29. @extends Component
  30. */
  31. (function () {
  32.  
  33. "use strict";
  34.  
  35. BIMSURFER.KeyboardOrbitCamera = BIMSURFER.Component.extend({
  36.  
  37. /**
  38. JavaScript class name for this Component.
  39.  
  40. @property className
  41. @type String
  42. @final
  43. */
  44. className: "BIMSURFER.KeyboardOrbitCamera",
  45.  
  46. _init: function (cfg) {
  47.  
  48. this.camera = cfg.camera;
  49.  
  50. this._onTick = null;
  51.  
  52. this.active = cfg.active !== false;
  53. },
  54.  
  55. _props: {
  56.  
  57. /**
  58. * Flag which indicates whether this KeyboardOrbitCamera is active or not.
  59. *
  60. * Fires an {{#crossLink "KeyboardOrbitCamera/active:event"}}{{/crossLink}} event on change.
  61. *
  62. * @property active
  63. * @type Boolean
  64. */
  65. active: {
  66.  
  67. set: function (value) {
  68.  
  69. if (this._active === value) {
  70. return;
  71. }
  72.  
  73. var input = this.viewer.input;
  74.  
  75. if (value) {
  76.  
  77. var self = this;
  78.  
  79. this._onTick = this.viewer.on("tick",
  80. function (params) {
  81.  
  82. if (!self._camera) {
  83. return;
  84. }
  85.  
  86. var elapsed = params.elapsed;
  87.  
  88. var yawRate = 50;
  89. var pitchRate = 50;
  90.  
  91. if (!input.ctrlDown && !input.altDown) {
  92.  
  93. var left = input.keyDown[input.KEY_LEFT_ARROW];
  94. var right = input.keyDown[input.KEY_RIGHT_ARROW];
  95. var up = input.keyDown[input.KEY_UP_ARROW];
  96. var down = input.keyDown[input.KEY_DOWN_ARROW];
  97.  
  98. if (left || right || up || down) {
  99.  
  100. var yaw = 0;
  101. var pitch = 0;
  102.  
  103. if (right) {
  104. yaw = -elapsed * yawRate;
  105.  
  106. } else if (left) {
  107. yaw = elapsed * yawRate;
  108. }
  109.  
  110. if (down) {
  111. pitch = elapsed * pitchRate;
  112.  
  113. } else if (up) {
  114. pitch = -elapsed * pitchRate;
  115. }
  116.  
  117. if (Math.abs(yaw) > Math.abs(pitch)) {
  118. pitch = 0;
  119. } else {
  120. yaw = 0;
  121. }
  122.  
  123. if (yaw != 0) {
  124. self._camera.rotateEyeY(yaw);
  125. }
  126.  
  127. if (pitch != 0) {
  128. self._camera.rotateEyeX(pitch);
  129. }
  130. }
  131. }
  132. });
  133.  
  134. } else {
  135.  
  136. this.viewer.off(this._onTick);
  137. }
  138.  
  139. /**
  140. * Fired whenever this KeyboardOrbitCamera's {{#crossLink "KeyboardOrbitCamera/active:property"}}{{/crossLink}} property changes.
  141. * @event active
  142. * @param value The property's new value
  143. */
  144. this.fire('active', this._active = value);
  145. },
  146.  
  147. get: function () {
  148. return this._active;
  149. }
  150. },
  151.  
  152. camera: {
  153.  
  154. set: function (value) {
  155. var camera = value;
  156. if (camera) {
  157. if (BIMSURFER._isString(camera)) {
  158. camera = this.viewer.components[camera];
  159. if (!camera) {
  160. this.error("camera", "Camera not found in Viewer: " + value);
  161. return;
  162. }
  163. }
  164. if (camera.className != "BIMSURFER.Camera") {
  165. this.error("camera", "Value is not a BIMSURFER.Camera");
  166. return;
  167. }
  168. }
  169. this._camera = camera;
  170. },
  171.  
  172. get: function () {
  173. return this._camera;
  174. }
  175. }
  176. },
  177.  
  178. _destroy: function () {
  179. this.active = false;
  180. }
  181. });
  182.  
  183. })();
  184.