智慧水务管理系统 - 精河县供水工程综合管理平台

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import {
  2. defined,
  3. destroyObject,
  4. DeveloperError,
  5. EventHelper,
  6. Fullscreen,
  7. getElement,
  8. OrthographicFrustum,
  9. } from "@cesium/engine";
  10. import knockout from "../ThirdParty/knockout.js";
  11. import NoSleep from "nosleep.js";
  12. import createCommand from "../createCommand.js";
  13. function lockScreen(orientation) {
  14. let locked = false;
  15. const screen = window.screen;
  16. if (defined(screen)) {
  17. if (defined(screen.lockOrientation)) {
  18. locked = screen.lockOrientation(orientation);
  19. } else if (defined(screen.mozLockOrientation)) {
  20. locked = screen.mozLockOrientation(orientation);
  21. } else if (defined(screen.msLockOrientation)) {
  22. locked = screen.msLockOrientation(orientation);
  23. } else if (defined(screen.orientation && screen.orientation.lock)) {
  24. locked = screen.orientation.lock(orientation);
  25. }
  26. }
  27. return locked;
  28. }
  29. function unlockScreen() {
  30. const screen = window.screen;
  31. if (defined(screen)) {
  32. if (defined(screen.unlockOrientation)) {
  33. screen.unlockOrientation();
  34. } else if (defined(screen.mozUnlockOrientation)) {
  35. screen.mozUnlockOrientation();
  36. } else if (defined(screen.msUnlockOrientation)) {
  37. screen.msUnlockOrientation();
  38. } else if (defined(screen.orientation && screen.orientation.unlock)) {
  39. screen.orientation.unlock();
  40. }
  41. }
  42. }
  43. function toggleVR(viewModel, scene, isVRMode, isOrthographic) {
  44. if (isOrthographic()) {
  45. return;
  46. }
  47. if (isVRMode()) {
  48. scene.useWebVR = false;
  49. if (viewModel._locked) {
  50. unlockScreen();
  51. viewModel._locked = false;
  52. }
  53. viewModel._noSleep.disable();
  54. Fullscreen.exitFullscreen();
  55. isVRMode(false);
  56. } else {
  57. if (!Fullscreen.fullscreen) {
  58. Fullscreen.requestFullscreen(viewModel._vrElement);
  59. }
  60. viewModel._noSleep.enable();
  61. if (!viewModel._locked) {
  62. viewModel._locked = lockScreen("landscape");
  63. }
  64. scene.useWebVR = true;
  65. isVRMode(true);
  66. }
  67. }
  68. /**
  69. * The view model for {@link VRButton}.
  70. * @alias VRButtonViewModel
  71. * @constructor
  72. *
  73. * @param {Scene} scene The scene.
  74. * @param {Element|string} [vrElement=document.body] The element or id to be placed into VR mode.
  75. */
  76. function VRButtonViewModel(scene, vrElement) {
  77. //>>includeStart('debug', pragmas.debug);
  78. if (!defined(scene)) {
  79. throw new DeveloperError("scene is required.");
  80. }
  81. //>>includeEnd('debug');
  82. const that = this;
  83. const isEnabled = knockout.observable(Fullscreen.enabled);
  84. const isVRMode = knockout.observable(false);
  85. /**
  86. * Gets whether or not VR mode is active.
  87. *
  88. * @type {boolean}
  89. */
  90. this.isVRMode = undefined;
  91. knockout.defineProperty(this, "isVRMode", {
  92. get: function () {
  93. return isVRMode();
  94. },
  95. });
  96. /**
  97. * Gets or sets whether or not VR functionality should be enabled.
  98. *
  99. * @type {boolean}
  100. * @see Fullscreen.enabled
  101. */
  102. this.isVREnabled = undefined;
  103. knockout.defineProperty(this, "isVREnabled", {
  104. get: function () {
  105. return isEnabled();
  106. },
  107. set: function (value) {
  108. isEnabled(value && Fullscreen.enabled);
  109. },
  110. });
  111. /**
  112. * Gets the tooltip. This property is observable.
  113. *
  114. * @type {string}
  115. */
  116. this.tooltip = undefined;
  117. knockout.defineProperty(this, "tooltip", function () {
  118. if (!isEnabled()) {
  119. return "VR mode is unavailable";
  120. }
  121. return isVRMode() ? "Exit VR mode" : "Enter VR mode";
  122. });
  123. const isOrthographic = knockout.observable(false);
  124. this._isOrthographic = undefined;
  125. knockout.defineProperty(this, "_isOrthographic", {
  126. get: function () {
  127. return isOrthographic();
  128. },
  129. });
  130. this._eventHelper = new EventHelper();
  131. this._eventHelper.add(scene.preRender, function () {
  132. isOrthographic(scene.camera.frustum instanceof OrthographicFrustum);
  133. });
  134. this._locked = false;
  135. this._noSleep = new NoSleep();
  136. this._command = createCommand(
  137. function () {
  138. toggleVR(that, scene, isVRMode, isOrthographic);
  139. },
  140. knockout.getObservable(this, "isVREnabled"),
  141. );
  142. this._vrElement = getElement(vrElement) ?? document.body;
  143. this._callback = function () {
  144. if (!Fullscreen.fullscreen && isVRMode()) {
  145. scene.useWebVR = false;
  146. if (that._locked) {
  147. unlockScreen();
  148. that._locked = false;
  149. }
  150. that._noSleep.disable();
  151. isVRMode(false);
  152. }
  153. };
  154. document.addEventListener(Fullscreen.changeEventName, this._callback);
  155. }
  156. Object.defineProperties(VRButtonViewModel.prototype, {
  157. /**
  158. * Gets or sets the HTML element to place into VR mode when the
  159. * corresponding button is pressed.
  160. * @memberof VRButtonViewModel.prototype
  161. *
  162. * @type {Element}
  163. */
  164. vrElement: {
  165. //TODO:@exception {DeveloperError} value must be a valid HTML Element.
  166. get: function () {
  167. return this._vrElement;
  168. },
  169. set: function (value) {
  170. //>>includeStart('debug', pragmas.debug);
  171. if (!(value instanceof Element)) {
  172. throw new DeveloperError("value must be a valid Element.");
  173. }
  174. //>>includeEnd('debug');
  175. this._vrElement = value;
  176. },
  177. },
  178. /**
  179. * Gets the Command to toggle VR mode.
  180. * @memberof VRButtonViewModel.prototype
  181. *
  182. * @type {Command}
  183. */
  184. command: {
  185. get: function () {
  186. return this._command;
  187. },
  188. },
  189. });
  190. /**
  191. * @returns {boolean} true if the object has been destroyed, false otherwise.
  192. */
  193. VRButtonViewModel.prototype.isDestroyed = function () {
  194. return false;
  195. };
  196. /**
  197. * Destroys the view model. Should be called to
  198. * properly clean up the view model when it is no longer needed.
  199. */
  200. VRButtonViewModel.prototype.destroy = function () {
  201. this._eventHelper.removeAll();
  202. document.removeEventListener(Fullscreen.changeEventName, this._callback);
  203. destroyObject(this);
  204. };
  205. export default VRButtonViewModel;