ProjectionPicker.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import {
  2. defined,
  3. destroyObject,
  4. DeveloperError,
  5. FeatureDetection,
  6. getElement,
  7. } from "@cesium/engine";
  8. import knockout from "../ThirdParty/knockout.js";
  9. import ProjectionPickerViewModel from "./ProjectionPickerViewModel.js";
  10. const perspectivePath =
  11. "M 28.15625,10.4375 9.125,13.21875 13.75,43.25 41.75,55.09375 50.8125,37 54.5,11.9375 z m 0.125,3 19.976451,0.394265 L 43.03125,16.875 22.6875,14.28125 z M 50.971746,15.705477 47.90625,36.03125 42.53125,46 44.84375,19.3125 z M 12.625,16.03125 l 29.15625,3.6875 -2.65625,31 L 16.4375,41.125 z";
  12. const orthographicPath =
  13. "m 31.560594,6.5254438 -20.75,12.4687502 0.1875,24.5625 22.28125,11.8125 19.5,-12 0.65625,-0.375 0,-0.75 0.0312,-23.21875 z m 0.0625,3.125 16.65625,9.5000002 -16.125,10.28125 -17.34375,-9.71875 z m 18.96875,11.1875002 0.15625,20.65625 -17.46875,10.59375 0.15625,-20.28125 z m -37.0625,1.25 17.21875,9.625 -0.15625,19.21875 -16.9375,-9 z";
  14. /**
  15. * The ProjectionPicker is a single button widget for switching between perspective and orthographic projections.
  16. *
  17. * @alias ProjectionPicker
  18. * @constructor
  19. *
  20. * @param {Element|string} container The DOM element or ID that will contain the widget.
  21. * @param {Scene} scene The Scene instance to use.
  22. *
  23. * @exception {DeveloperError} Element with id "container" does not exist in the document.
  24. *
  25. * @example
  26. * // In HTML head, include a link to the ProjectionPicker.css stylesheet,
  27. * // and in the body, include: <div id="projectionPickerContainer"></div>
  28. * // Note: This code assumes you already have a Scene instance.
  29. *
  30. * const projectionPicker = new Cesium.ProjectionPicker('projectionPickerContainer', scene);
  31. */
  32. function ProjectionPicker(container, scene) {
  33. //>>includeStart('debug', pragmas.debug);
  34. if (!defined(container)) {
  35. throw new DeveloperError("container is required.");
  36. }
  37. if (!defined(scene)) {
  38. throw new DeveloperError("scene is required.");
  39. }
  40. //>>includeEnd('debug');
  41. container = getElement(container);
  42. const viewModel = new ProjectionPickerViewModel(scene);
  43. viewModel._perspectivePath = perspectivePath;
  44. viewModel._orthographicPath = orthographicPath;
  45. const wrapper = document.createElement("span");
  46. wrapper.className = "cesium-projectionPicker-wrapper cesium-toolbar-button";
  47. container.appendChild(wrapper);
  48. const button = document.createElement("button");
  49. button.type = "button";
  50. button.className = "cesium-button cesium-toolbar-button";
  51. button.setAttribute(
  52. "data-bind",
  53. '\
  54. css: { "cesium-projectionPicker-buttonPerspective": !_orthographic,\
  55. "cesium-projectionPicker-buttonOrthographic": _orthographic,\
  56. "cesium-button-disabled" : sceneMode === _sceneMode.SCENE2D || _flightInProgress, \
  57. "cesium-projectionPicker-selected": dropDownVisible },\
  58. attr: { title: selectedTooltip },\
  59. click: toggleDropDown'
  60. );
  61. button.innerHTML =
  62. '\
  63. <!-- ko cesiumSvgPath: { path: _perspectivePath, width: 64, height: 64, css: "cesium-projectionPicker-iconPerspective" } --><!-- /ko -->\
  64. <!-- ko cesiumSvgPath: { path: _orthographicPath, width: 64, height: 64, css: "cesium-projectionPicker-iconOrthographic" } --><!-- /ko -->';
  65. wrapper.appendChild(button);
  66. const perspectiveButton = document.createElement("button");
  67. perspectiveButton.type = "button";
  68. perspectiveButton.className =
  69. "cesium-button cesium-toolbar-button cesium-projectionPicker-dropDown-icon";
  70. perspectiveButton.setAttribute(
  71. "data-bind",
  72. '\
  73. css: { "cesium-projectionPicker-visible" : (dropDownVisible && _orthographic),\
  74. "cesium-projectionPicker-none" : !_orthographic,\
  75. "cesium-projectionPicker-hidden" : !dropDownVisible },\
  76. attr: { title: tooltipPerspective },\
  77. click: switchToPerspective,\
  78. cesiumSvgPath: { path: _perspectivePath, width: 64, height: 64 }'
  79. );
  80. wrapper.appendChild(perspectiveButton);
  81. const orthographicButton = document.createElement("button");
  82. orthographicButton.type = "button";
  83. orthographicButton.className =
  84. "cesium-button cesium-toolbar-button cesium-projectionPicker-dropDown-icon";
  85. orthographicButton.setAttribute(
  86. "data-bind",
  87. '\
  88. css: { "cesium-projectionPicker-visible" : (dropDownVisible && !_orthographic),\
  89. "cesium-projectionPicker-none" : _orthographic,\
  90. "cesium-projectionPicker-hidden" : !dropDownVisible},\
  91. attr: { title: tooltipOrthographic },\
  92. click: switchToOrthographic,\
  93. cesiumSvgPath: { path: _orthographicPath, width: 64, height: 64 }'
  94. );
  95. wrapper.appendChild(orthographicButton);
  96. knockout.applyBindings(viewModel, wrapper);
  97. this._viewModel = viewModel;
  98. this._container = container;
  99. this._wrapper = wrapper;
  100. this._closeDropDown = function (e) {
  101. if (!wrapper.contains(e.target)) {
  102. viewModel.dropDownVisible = false;
  103. }
  104. };
  105. if (FeatureDetection.supportsPointerEvents()) {
  106. document.addEventListener("pointerdown", this._closeDropDown, true);
  107. } else {
  108. document.addEventListener("mousedown", this._closeDropDown, true);
  109. document.addEventListener("touchstart", this._closeDropDown, true);
  110. }
  111. }
  112. Object.defineProperties(ProjectionPicker.prototype, {
  113. /**
  114. * Gets the parent container.
  115. * @memberof ProjectionPicker.prototype
  116. *
  117. * @type {Element}
  118. */
  119. container: {
  120. get: function () {
  121. return this._container;
  122. },
  123. },
  124. /**
  125. * Gets the view model.
  126. * @memberof ProjectionPicker.prototype
  127. *
  128. * @type {ProjectionPickerViewModel}
  129. */
  130. viewModel: {
  131. get: function () {
  132. return this._viewModel;
  133. },
  134. },
  135. });
  136. /**
  137. * @returns {boolean} true if the object has been destroyed, false otherwise.
  138. */
  139. ProjectionPicker.prototype.isDestroyed = function () {
  140. return false;
  141. };
  142. /**
  143. * Destroys the widget. Should be called if permanently
  144. * removing the widget from layout.
  145. */
  146. ProjectionPicker.prototype.destroy = function () {
  147. this._viewModel.destroy();
  148. if (FeatureDetection.supportsPointerEvents()) {
  149. document.removeEventListener("pointerdown", this._closeDropDown, true);
  150. } else {
  151. document.removeEventListener("mousedown", this._closeDropDown, true);
  152. document.removeEventListener("touchstart", this._closeDropDown, true);
  153. }
  154. knockout.cleanNode(this._wrapper);
  155. this._container.removeChild(this._wrapper);
  156. return destroyObject(this);
  157. };
  158. export default ProjectionPicker;