|
| 1 | +import { CesiumZondy } from '../core/Base'; |
| 2 | + |
| 3 | +/** |
| 4 | + * 三维视图的主要类 |
| 5 | + * @alias WebSceneControl |
| 6 | + * @constructor |
| 7 | + * @class module:客户端视图管理.WebSceneControl |
| 8 | + * @param {Element|String} elementId 放置视图的div的id |
| 9 | + * @param {Object} [options] 包含以下属性的对象 |
| 10 | + * @param {String} [options.viewerMode=‘3D’] 初始视图模式默认为三维球视图 '2D'表示二维视图 'COLUMBUS_VIEW' 表示三维平面视图 |
| 11 | + * @param {Boolean} [options.showInfo=false] 是否显示默认的属性信息框 |
| 12 | + * @param {Boolean} [options.animation=true] 默认动画控制不显示 |
| 13 | + * @param {Boolean} [options.baseLayerPicker=true] If set to false, the BaseLayerPicker widget will not be created. |
| 14 | + * @param {Boolean} [options.fullscreenButton=true] If set to false, the FullscreenButton widget will not be created. |
| 15 | + * @param {Boolean} [options.vrButton=false] If set to true, the VRButton widget will be created. |
| 16 | + * @param {Boolean} [options.onCopy=false] 是否禁用复制,默认为false禁用 |
| 17 | + * @example |
| 18 | + * var webGlobe = new CesiumZondy.WebSceneControl('GlobeView'); |
| 19 | + * |
| 20 | + * var webGlobe = new CesiumZondy.WebSceneControl('GlobeView',{showInfo:true}); |
| 21 | + * //或者如下 |
| 22 | + * var options ={ |
| 23 | + * showInfo:false, |
| 24 | + * viewerMode:'3D', |
| 25 | + * keyEventEnable:false |
| 26 | + * }; |
| 27 | + * var webGlobe = new CesiumZondy.WebSceneControl('GlobeView',options); |
| 28 | + */ |
| 29 | +export default class WebSceneControl { |
| 30 | + constructor(elementId, op) { |
| 31 | + const options = Cesium.defaultValue(op, {}); |
| 32 | + |
| 33 | + /** 默认动画控制不显示 */ |
| 34 | + options.animation = Cesium.defaultValue(options.animation, false); |
| 35 | + // 默认不显示图层控制显示 |
| 36 | + options.baseLayerPicker = Cesium.defaultValue(options.baseLayerPicker, false); |
| 37 | + // 默认不显示全屏控制按钮 |
| 38 | + options.fullscreenButton = Cesium.defaultValue(options.fullscreenButton, false); |
| 39 | + // 默认不显示地名查询框 |
| 40 | + options.geocoder = Cesium.defaultValue(options.geocoder, false); |
| 41 | + // 默认不显示复位按钮 |
| 42 | + options.homeButton = Cesium.defaultValue(options.homeButton, false); |
| 43 | + // 默认不显示信息框 |
| 44 | + options.infoBox = Cesium.defaultValue(options.infoBox, false); |
| 45 | + // 默认不显示3D/2D选择器 |
| 46 | + options.sceneModePicker = Cesium.defaultValue(options.sceneModePicker, false); |
| 47 | + // 默认不显示选取指示器组件 |
| 48 | + options.selectionIndicator = Cesium.defaultValue(options.selectionIndicator, false); |
| 49 | + // 默认创建但不显示时间轴 |
| 50 | + options.timeline = Cesium.defaultValue(options.timeline, false); |
| 51 | + // 默认不显示帮助按钮 |
| 52 | + options.navigationHelpButton = Cesium.defaultValue(options.navigationHelpButton, false); |
| 53 | + |
| 54 | + options.navigationInstructionsInitiallyVisible = Cesium.defaultValue(options.navigationInstructionsInitiallyVisible, true); |
| 55 | + // 默认不显示渲染错误信息面板 |
| 56 | + options.showRenderLoopErrors = Cesium.defaultValue(options.showRenderLoopErrors, false); |
| 57 | + // 默认场景为三维球面视图 |
| 58 | + options.sceneMode = Cesium.defaultValue(options.sceneMode, Cesium.SceneMode.SCENE3D); |
| 59 | + // 默认地图投影为web 墨卡托 |
| 60 | + options.mapProjection = Cesium.defaultValue(options.mapProjection, new Cesium.WebMercatorProjection()); |
| 61 | + // 默认可视化数据源集合 |
| 62 | + options.dataSources = Cesium.defaultValue(options.dataSources, new Cesium.DataSourceCollection()); |
| 63 | + // 默认支持阴影 |
| 64 | + options.shadows = Cesium.defaultValue(options.shadows, false); |
| 65 | + |
| 66 | + // 使用 ThreeJS 默认要关闭自动渲染 |
| 67 | + if (this._useThreeJs) { |
| 68 | + options.useDefaultRenderLoop = false; |
| 69 | + } |
| 70 | + |
| 71 | + this._threeContainer = undefined; |
| 72 | + |
| 73 | + // 管理append添加的图层组 |
| 74 | + this._appendCollection = []; |
| 75 | + |
| 76 | + // 默认支持键盘事件 |
| 77 | + this._keyEventEnable = Cesium.defaultValue(options.keyEventEnable, true); |
| 78 | + // 创建默认视图对象 |
| 79 | + this._viewer = new Cesium.Viewer(elementId, options); |
| 80 | + |
| 81 | + //隐藏版权信息 |
| 82 | + this._viewer.cesiumWidget.creditContainer.style.display = 'none'; |
| 83 | + |
| 84 | + // 场景对象 |
| 85 | + this._scene = this._viewer.scene; |
| 86 | + |
| 87 | + this._screenSpaceEventHandler = new Cesium.ScreenSpaceEventHandler(this._viewer.scene.canvas); |
| 88 | + |
| 89 | + this._elementID = elementId; |
| 90 | + |
| 91 | + this._popupContain = []; // 用于管理多个popup,主要考虑到多个popup场景变化时需响应其事件,改变其位置 |
| 92 | + |
| 93 | + const screenSpaceCameraController = this._viewer.scene.screenSpaceCameraController; |
| 94 | + //默认关闭hdr |
| 95 | + this._viewer.scene.highDynamicRange = false; |
| 96 | + screenSpaceCameraController.minimumZoomDistance = 1; |
| 97 | + screenSpaceCameraController.maximumZoomDistance = 2400000000000000; |
| 98 | + this._viewer.canvas.onclick = function () { |
| 99 | + this.focus(); |
| 100 | + }; |
| 101 | + |
| 102 | + this._cameraParameter = {}; |
| 103 | + const flags = { |
| 104 | + looking: false, |
| 105 | + rotateLeft: false, |
| 106 | + rotateRight: false, |
| 107 | + moveUp: false, |
| 108 | + moveDown: false, |
| 109 | + moveLeft: false, |
| 110 | + moveRight: false, |
| 111 | + goHome: false, |
| 112 | + wireFrame: false, |
| 113 | + showFPS: false |
| 114 | + }; |
| 115 | + |
| 116 | + //与activex球保持一致 |
| 117 | + function getFlagForKeyCode(keyCode) { |
| 118 | + switch (keyCode) { |
| 119 | + case 'W'.charCodeAt(0): //向下平移镜头 |
| 120 | + return 'moveDown'; |
| 121 | + case 'S'.charCodeAt(0): //向上平移镜头 |
| 122 | + return 'moveUp'; |
| 123 | + case 'A'.charCodeAt(0): //向右平移镜头 |
| 124 | + return 'moveRight'; |
| 125 | + case 'D'.charCodeAt(0): //向左平移镜头 |
| 126 | + return 'moveLeft'; |
| 127 | + case 'Q'.charCodeAt(0): //向右旋转镜头 |
| 128 | + return 'rotateRight'; |
| 129 | + case 'E'.charCodeAt(0): //向左旋转镜头 |
| 130 | + return 'rotateLeft'; |
| 131 | + case 'Z'.charCodeAt(0): //空格键复位 |
| 132 | + return 'goHome'; |
| 133 | + case 'G'.charCodeAt(0): //G键显示网 |
| 134 | + return 'wireFrame'; |
| 135 | + case 'F'.charCodeAt(0): //F键显示帧率 |
| 136 | + return 'showFPS'; |
| 137 | + default: |
| 138 | + return undefined; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + document.addEventListener( |
| 143 | + 'keydown', |
| 144 | + function (e) { |
| 145 | + const flagName = getFlagForKeyCode(e.keyCode); |
| 146 | + if (typeof flagName !== 'undefined') { |
| 147 | + flags[flagName] = true; |
| 148 | + } |
| 149 | + }, |
| 150 | + false |
| 151 | + ); |
| 152 | + document.addEventListener( |
| 153 | + 'keyup', |
| 154 | + function (e) { |
| 155 | + const flagName = getFlagForKeyCode(e.keyCode); |
| 156 | + if (typeof flagName !== 'undefined') { |
| 157 | + flags[flagName] = false; |
| 158 | + } |
| 159 | + }, |
| 160 | + false |
| 161 | + ); |
| 162 | + |
| 163 | + this._shouldAnimate = Cesium.defaultValue(options.shouldAnimate, false); //记录全局是否允许动画 |
| 164 | + var that = this; |
| 165 | + |
| 166 | + this._viewer.clock.onTick.addEventListener(function () { |
| 167 | + //获取相机高度 |
| 168 | + if (that.keyEventEnable) { |
| 169 | + const position = that._viewer.camera.position; |
| 170 | + const cameraHeight = that._viewer.scene.globe.ellipsoid.cartesianToCartographic(position).height; |
| 171 | + const moveRate = cameraHeight / 40.0; |
| 172 | + if (flags.rotateLeft) { |
| 173 | + that._viewer.camera.rotateLeft(0.01); |
| 174 | + } |
| 175 | + if (flags.rotateRight) { |
| 176 | + that._viewer.camera.rotateRight(0.01); |
| 177 | + } |
| 178 | + if (flags.moveUp) { |
| 179 | + that._viewer.camera.moveBackward(moveRate); |
| 180 | + } |
| 181 | + if (flags.moveDown) { |
| 182 | + that._viewer.camera.moveForward(moveRate); |
| 183 | + } |
| 184 | + if (flags.moveLeft) { |
| 185 | + that._viewer.camera.moveLeft(moveRate); |
| 186 | + } |
| 187 | + if (flags.moveRight) { |
| 188 | + that._viewer.camera.moveRight(moveRate); |
| 189 | + } |
| 190 | + if (flags.goHome) { |
| 191 | + that._viewer.camera.flyTo({ |
| 192 | + destination: Cesium.Cartesian3.fromDegrees(104, 30, 15682725) |
| 193 | + }); |
| 194 | + } |
| 195 | + if (flags.wireFrame) { |
| 196 | + var bShowWireframe = that._viewer.scene.globe._surface.tileProvider._debug.wireframe; |
| 197 | + that._viewer.scene.globe._surface.tileProvider._debug.wireframe = !bShowWireframe; |
| 198 | + flags.wireFrame = false; |
| 199 | + } |
| 200 | + if (flags.showFPS) { |
| 201 | + var bShowFPS = that._viewer.scene.debugShowFramesPerSecond; |
| 202 | + that._viewer.scene.debugShowFramesPerSecond = !bShowFPS; |
| 203 | + flags.showFPS = false; |
| 204 | + } |
| 205 | + } |
| 206 | + }); |
| 207 | + /** |
| 208 | + * 禁用右键菜单 |
| 209 | + */ |
| 210 | + document.oncontextmenu = function () { |
| 211 | + event.returnValue = false; |
| 212 | + }; |
| 213 | + // /** |
| 214 | + // * 禁用选中功能 |
| 215 | + // */ |
| 216 | + // document.onselectstart = function(){ |
| 217 | + // event.returnValue = false; |
| 218 | + // }; |
| 219 | + /** |
| 220 | + * 禁用复制功能 |
| 221 | + */ |
| 222 | + document.oncopy = function () { |
| 223 | + event.returnValue = that._onCopy; |
| 224 | + }; |
| 225 | + |
| 226 | + this.scene.skyAtmosphere.showGroundAtmosphere = false; |
| 227 | + this._isRecoverExplosion = false; |
| 228 | + } |
| 229 | + |
| 230 | + /** |
| 231 | + * 视图 |
| 232 | + * @memberof WebSceneControl.prototype |
| 233 | + * @type {Viewer} |
| 234 | + * @readonly |
| 235 | + */ |
| 236 | + get viewer() { |
| 237 | + return this._viewer; |
| 238 | + } |
| 239 | + |
| 240 | + /** |
| 241 | + * 场景 |
| 242 | + * @memberof WebSceneControl.prototype |
| 243 | + * @readonly |
| 244 | + * @type {Scene} |
| 245 | + */ |
| 246 | + get scene() { |
| 247 | + return this._scene; |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * 事件句柄 |
| 252 | + * @memberof WebSceneControl.prototype |
| 253 | + * @readonly |
| 254 | + */ |
| 255 | + get screenSpaceEventHandler() { |
| 256 | + return this._screenSpaceEventHandler; |
| 257 | + } |
| 258 | + |
| 259 | + /** |
| 260 | + * 当前椭球 |
| 261 | + * @memberof WebSceneControl.prototype |
| 262 | + * @type {Ellipsoid} |
| 263 | + * @readonly |
| 264 | + */ |
| 265 | + get ellipsoid() { |
| 266 | + return this._viewer.scene.globe.ellipsoid; |
| 267 | + } |
| 268 | +} |
| 269 | + |
| 270 | +CesiumZondy.WebSceneControl = WebSceneControl; |
0 commit comments