ProjectionPicker.js 6.3 KB

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