/** * Copyright Sourcepole AG * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. */ import React from 'react'; import {connect, Provider} from 'react-redux'; import isEmpty from 'lodash.isempty'; import ol from 'openlayers'; import PropTypes from 'prop-types'; import * as bookmarkExports from '../actions/bookmark'; import * as displayExports from '../actions/display'; import {setViewMode, ViewMode} from '../actions/display'; import * as layersExports from '../actions/layers'; import * as localConfigExports from '../actions/localConfig'; import {panTo, zoomToPoint} from '../actions/map'; import * as mapExports from '../actions/map'; import * as themeExports from '../actions/theme'; import PluginsContainer from '../components/PluginsContainer'; import ResizeableWindow from '../components/ResizeableWindow'; import Spinner from '../components/widgets/Spinner'; import ReducerIndex from '../reducers/index'; import personIconE from '../resources/person_e.svg'; import personIconN from '../resources/person_n.svg'; import personIconNE from '../resources/person_ne.svg'; import personIconS from '../resources/person_s.svg'; import personIconSE from '../resources/person_se.svg'; import {createStore, getStore} from '../stores/StandardStore'; import ConfigUtils from '../utils/ConfigUtils'; import LocaleUtils from '../utils/LocaleUtils'; import MapUtils from '../utils/MapUtils'; import {UrlParams} from '../utils/PermaLinkUtils'; import './style/View3D.css'; /** * Displays a 3D map view. * * See [3D View](../../topics/View3D). */ class View3D extends React.Component { static propTypes = { bookmark: PropTypes.object, /** The position of the navigation controls. Either `top` or `bottom`. */ controlsPosition: PropTypes.string, /** The pan/tilt/zoom speed when navigating with the respective map controls. */ controlsSpeed: PropTypes.shape({ pan: PropTypes.number, tilt: PropTypes.number, zoom: PropTypes.number }), /** Whether 2D layers should be visible by default when opening the 3D view. */ defaultColorLayerVisibility: PropTypes.bool, /** The default field of view (`20`: min, `100`: max). */ defaultFov: PropTypes.number, /** The default point size for point cloud tiles (`0`: min, `20`: max). Automatic size computation when the value is `0`. */ defaultPointSize: PropTypes.number, /** The default scene quality factor (`20`: min, `100`: max). */ defaultSceneQuality: PropTypes.number, /** Whether to enable synchronization of 2D layer states (visibility, opacity) to the 3D view by default. */ defaultSync2dLayers: PropTypes.bool, display: PropTypes.object, /** Whether to allow opening the inspector in production environment. */ forceAllowInspector: PropTypes.bool, /** Default window geometry. */ geometry: PropTypes.shape({ initialWidth: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), initialHeight: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), initialX: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), initialY: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), initiallyDocked: PropTypes.bool }), layers: PropTypes.object, localConfig: PropTypes.object, map: PropTypes.object, /** Mouse buttons assignment. You can assign `pan`, `rotate`, `zoom` to each button. */ mouseButtons: PropTypes.shape({ left: PropTypes.string, middle: PropTypes.string, right: PropTypes.string }), panTo: PropTypes.func, /** Options to pass to the 3D plugins, in the form `{"": {}}`. * Refer to the documentation of the 3D plugins for settable options. */ pluginOptions: PropTypes.object, plugins3d: PropTypes.object, searchProviders: PropTypes.object, setViewMode: PropTypes.func, startupParams: PropTypes.object, startupState: PropTypes.object, theme: PropTypes.object, viewMode: PropTypes.number, zoomToPoint: PropTypes.func }; static defaultProps = { controlsPosition: 'top', controlsSpeed: {pan: 1, tilt: 1, zoom: 1}, defaultColorLayerVisibility: false, defaultFov: 30, defaultPointSize: 0, defaultSceneQuality: 100, forceAllowInspector: false, geometry: { initialWidth: 600, initialHeight: 800, initialX: 0, initialY: 0, initiallyDocked: true }, pluginOptions: {}, mouseButtons: { left: 'pan', middle: 'zoom', right: 'rotate' } }; EnabledState = {DISABLED: 0, ENABLED: 1, DISABLING: 2}; state = { componentLoaded: false, windowDetached: false, viewsLocked: false, storedState: null, enabledState: this.EnabledState.DISABLED }; constructor(props) { super(props); this.map3dComponent = null; this.map3dComponentRef = null; this.focusedMap = null; this.firstPersonMarker = null; this.pendingViewState = null; // Subset of 2d reducers const { processNotifications, task, windows } = ReducerIndex.reducers; // Reducer for syncronization with parent store const forwardReducer = (key, forwardActions, syncAction) => (state = {}, action) => { if (forwardActions.includes(action.type)) { // Forward to parent store getStore().dispatch(action); return state; } else { return action.type === syncAction ? action[key] : state; } }; const bookmarkActions = Object.values(bookmarkExports).filter(x => typeof(x) === 'string'); const displayActions = Object.values(displayExports).filter(x => typeof(x) === 'string'); const layersActions = Object.values(layersExports).filter(x => typeof(x) === 'string'); const mapActions = Object.values(mapExports).filter(x => typeof(x) === 'string'); const themeActions = Object.values(themeExports).filter(x => typeof(x) === 'string'); const localConfigActions = Object.values(localConfigExports).filter(x => typeof(x) === 'string'); const bookmark = forwardReducer("bookmark", bookmarkActions, "SYNC_BOOKMARK_FROM_PARENT_STORE"); const display = forwardReducer("display", displayActions, "SYNC_DISPLAY_FROM_PARENT_STORE"); const layers = forwardReducer("layers", layersActions, "SYNC_LAYERS_FROM_PARENT_STORE"); const map = forwardReducer("map", mapActions, "SYNC_MAP_FROM_PARENT_STORE"); const localConfig = forwardReducer("localConfig", localConfigActions, "SYNC_LOCAL_CONFIG_FROM_PARENT_STORE"); const theme = forwardReducer("theme", themeActions, "SYNC_THEME_FROM_PARENT_STORE"); this.store = createStore({display, bookmark, layers, localConfig, map, processNotifications, theme, task, windows}); // Set stored state const storedState = { ...props.startupState.map3d?.state }; if (props.startupParams.v3d) { const values = props.startupParams.v3d.split(",").map(parseFloat).filter(x => !isNaN(x)); if (values.length >= 6) { storedState.camera = [values[0], values[1], values[2]]; storedState.target = [values[3], values[4], values[5]]; storedState.personHeight = values[6] ?? 0; } } if (props.startupParams.bl3d !== undefined) { storedState.baseLayer = props.startupParams.bl3d; } this.state.storedState = storedState; } componentDidMount() { if (this.props.startupParams.v === "3d") { this.props.setViewMode(ViewMode._3DFullscreen); } else if (this.props.startupParams.v === "3d2d") { this.props.setViewMode(ViewMode._3DSplitscreen); } window.addEventListener('focus', this.trackFocus, true); window.addEventListener('pointerdown', this.trackPointerFocus); this.syncParentStore({}); } componentWillUnmount() { window.removeEventListener('focus', this.trackFocus); window.removeEventListener('pointerdown', this.trackPointerFocus); } componentDidUpdate(prevProps, prevState) { const is3DViewMode = (viewMode) => [ViewMode._3DFullscreen, ViewMode._3DSplitscreen].includes(viewMode); // Honour theme startupView on theme change unless first loaded theme and startupParams.v is set if (this.props.theme.current !== prevProps.theme.current && this.props.theme.current?.startupView && (prevProps.theme.current !== null || !this.props.startupParams.v)) { if (this.props.theme.current.startupView === "3d2d") { this.props.setViewMode(ViewMode._3DSplitscreen); } else if (this.props.theme.current.startupView === "3d") { this.props.setViewMode(ViewMode._3DFullscreen); } } if (this.state.enabledState === this.EnabledState.DISABLING && this.props.viewMode !== prevProps.viewMode) { this.pendingViewState = [ViewMode._3DFullscreen, ViewMode._3DSplitscreen].includes(this.props.viewMode) ? this.EnabledState.ENABLED : this.EnabledState.DISABLED; } else if (is3DViewMode(this.props.viewMode) && !is3DViewMode(prevProps.viewMode)) { this.setState({enabledState: this.EnabledState.ENABLED}); } else if (!is3DViewMode(this.props.viewMode) && is3DViewMode(prevProps.viewMode)) { this.setState({enabledState: this.EnabledState.DISABLING}); } if (this.state.enabledState !== this.EnabledState.DISABLED && prevState.enabledState === this.EnabledState.DISABLED) { import('../components/map3d/Map3D').then(component => { this.map3dComponent = component.default; this.map3dComponentRef = null; this.setState({componentLoaded: true}); }); this.syncParentStore(this.props, true); } else if (this.state.enabledState === this.EnabledState.DISABLING && prevState.enabledState !== this.EnabledState.DISABLING) { if (this.map3dComponentRef) { this.map3dComponentRef.store3dState().then(result => { this.setState({storedState: result.state, enabledState: this.EnabledState.DISABLED}, () => { if (this.pendingViewState !== null) { this.setState({enabledState: this.pendingViewState}); } }); }); } else { this.setState({enabledState: this.EnabledState.DISABLED}); } } else if (this.state.enabledState === this.EnabledState.DISABLED && prevState.enabledState !== this.EnabledState.DISABLED) { UrlParams.updateParams({v3d: undefined, bl3d: undefined}); this.map3dComponent = null; this.map3dComponentRef = null; this.setState({componentLoaded: false}); if (this.firstPersonMarker) { MapUtils.removeUnmanagedLayer(this.firstPersonMarker); this.firstPersonMarker = null; } } // Sync parts of parent store this.syncParentStore(prevProps); // Unlock views when switching to fullscreen 3D mode if (this.props.viewMode !== prevProps.viewMode && this.props.viewMode === ViewMode._3DFullscreen) { this.setState({viewsLocked: false}); } // Switch to 2D mode if new theme has no 3D configuration if ( this.props.theme.current !== prevProps.theme.current && !this.props.theme.current?.map3d && this.props.viewMode !== ViewMode._2D ) { this.props.setViewMode(ViewMode._2D); } // Lock views if (this.state.viewsLocked && this.props.map.bbox !== prevProps.map.bbox && this.focusedMap === "map") { this.sync2DExtent(); } // Clear stored state when switching away from a theme if (prevProps.theme.current && this.props.theme.current !== prevProps.theme.current) { this.setState({storedState: null}); } } syncParentStore(prevProps, force = false) { if (this.state.enabledState !== this.EnabledState.ENABLED) { return; } if (this.props.bookmark !== prevProps.bookmark || force) { this.store.dispatch({type: "SYNC_BOOKMARK_FROM_PARENT_STORE", bookmark: this.props.bookmark}); } if (this.props.display !== prevProps.display || force) { this.store.dispatch({type: "SYNC_DISPLAY_FROM_PARENT_STORE", display: this.props.display}); } if (this.props.theme !== prevProps.theme || force) { this.store.dispatch({type: "SYNC_THEME_FROM_PARENT_STORE", theme: this.props.theme}); } if (this.props.localConfig !== prevProps.localConfig || force) { this.store.dispatch({type: "SYNC_LOCAL_CONFIG_FROM_PARENT_STORE", localConfig: this.props.localConfig}); } if (this.props.layers !== prevProps.layers || force) { this.store.dispatch({type: "SYNC_LAYERS_FROM_PARENT_STORE", layers: this.props.layers}); } if (this.props.map !== prevProps.map || force) { this.store.dispatch({type: "SYNC_MAP_FROM_PARENT_STORE", map: this.props.map}); } } render() { if (this.state.enabledState > this.EnabledState.DISABLED) { const extraControls = [{ icon: "sync", callback: this.sync2DExtent, title: LocaleUtils.tr("common.sync2dview") }, { icon: "lock", callback: this.setLockViews, title: LocaleUtils.tr("common.lock2dview"), active: this.state.viewsLocked }]; if (!this.state.windowDetached) { extraControls.push({ icon: "maximize", callback: () => this.props.setViewMode(ViewMode._3DFullscreen), title: LocaleUtils.tr("window.maximize") }); } const Map3D = this.map3dComponent; const device = ConfigUtils.isMobile() ? 'mobile' : 'desktop'; const pluginsConfig = this.props.viewMode === ViewMode._3DFullscreen ? this.props.localConfig.plugins[device].filter(entry => { return entry.availableIn3D; }) : []; return ( {this.state.componentLoaded ? ( { this.state.enabledState === this.EnabledState.DISABLING ? (
{LocaleUtils.tr("view3d.storingstate")}
) : null }
) : null}
); } return null; } onClose = () => { this.props.setViewMode(ViewMode._2D); }; onGeometryChanged = (geometry) => { if (geometry.maximized && this.props.viewMode !== ViewMode._3DFullscreen) { this.props.setViewMode(ViewMode._3DFullscreen); } this.setState({windowDetached: geometry.detached}); }; onCameraChanged = (center, camera, fov, az) => { // Note: If camera pos is NULL, we are in first-person-view if (this.state.viewsLocked && this.focusedMap === "map3d") { let rotation = undefined; if (camera) { rotation = Math.atan2(center[1] - camera[1], center[0] - camera[0]) - 0.5 * Math.PI; const distance = Math.sqrt( (camera[0] - center[0]) * (camera[0] - center[0]) + (camera[1] - center[1]) * (camera[1] - center[1]) + (camera[2] - center[2]) * (camera[2] - center[2]) ); const fovrad = fov / 180 * Math.PI; const bboxWidth = distance * (2 * Math.tan(fovrad / 2)); const bbox = [-0.5 * bboxWidth, 0, 0.5 * bboxWidth, 0]; const zoom = MapUtils.getZoomForExtent(bbox, this.props.map.resolutions, this.props.map.size, 0, this.props.map.scales.length - 1); this.props.zoomToPoint(center.slice(0, 2), zoom, this.props.theme.mapCrs, rotation); if (this.firstPersonMarker) { MapUtils.removeUnmanagedLayer(this.firstPersonMarker); this.firstPersonMarker = null; } } else { this.props.panTo(center.slice(0, 2), this.props.theme.mapCrs, rotation); if (!this.firstPersonMarker) { const options = { type: 'vector', features: [{ id: 'person', geometry: { type: 'Point', coordinates: center.slice(0, 2) }, properties: { az: az } }], styleFunction: (f) => { const img = (baseIcon, flip = false) => ( new ol.style.Icon({src: baseIcon, scale: [flip ? -2 : 2, 2]}) ); return new ol.style.Style({ image: [ img(personIconN), img(personIconNE), img(personIconE), img(personIconSE), img(personIconS), img(personIconSE, true), img(personIconE, true), img(personIconNE, true) ][Math.round(f.get('az') / 45) % 8] }); } }; this.firstPersonMarker = MapUtils.addUnmanagedLayer(options); } else { const markerFeature = this.firstPersonMarker.getSource().getFeatures()[0]; markerFeature.set('az', az, false); markerFeature.getGeometry().setCoordinates(center); } } } else if (this.firstPersonMarker) { MapUtils.removeUnmanagedLayer(this.firstPersonMarker); this.firstPersonMarker = null; } }; setRef = (ref) => { this.map3dComponentRef = ref; }; sync2DExtent = () => { if (this.map3dComponentRef) { this.map3dComponentRef.setViewToExtent(this.props.map.bbox.bounds, this.props.map.bbox.rotation); } }; setLockViews = () => { this.setState(state => ({viewsLocked: !state.viewsLocked})); if (this.firstPersonMarker) { MapUtils.removeUnmanagedLayer(this.firstPersonMarker); this.firstPersonMarker = null; } }; setupMap = () => { if (this.map3dComponentRef) { if (!isEmpty(this.state.storedState)) { this.map3dComponentRef.restore3dState(this.state.storedState); } else if (this.props.theme.current.map3d.initialView) { this.map3dComponentRef.restore3dState(this.props.theme.current.map3d.initialView); } else { this.sync2DExtent(); } } }; redrawScene = (ev) => { if (this.map3dComponentRef) { this.map3dComponentRef.redrawScene(ev); } }; trackFocus = (ev) => { const inContainer = document.activeElement.closest('.plugins-container-3d') !== null; if (inContainer) { this.focusedMap = "map3d"; } else { this.focusedMap = "map"; } if (this.map3dComponentRef) { this.map3dComponentRef.focusedMap = this.focusedMap; } }; trackPointerFocus = (ev) => { const inContainer = ev.target.closest('.plugins-container-3d') !== null; if (inContainer) { this.focusedMap = "map3d"; } else { this.focusedMap = "map"; } if (this.map3dComponentRef) { this.map3dComponentRef.focusedMap = this.focusedMap; } }; } export default (plugins3d) => connect( (state) => ({ plugins3d: plugins3d, bookmark: state.bookmark, display: state.display, map: state.map, layers: state.layers, theme: state.theme, localConfig: state.localConfig, viewMode: state.display.viewMode, startupParams: state.localConfig.startupParams, startupState: state.localConfig.startupState }), { panTo: panTo, zoomToPoint: zoomToPoint, setViewMode: setViewMode } )(View3D);