| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- import { MapEngine, type MapEngineStageRect, type MapEngineViewState } from '../../engine/map/mapEngine'
- type MapPageData = MapEngineViewState & {
- showDebugPanel: boolean
- }
- const INTERNAL_BUILD_VERSION = 'map-build-63'
- let mapEngine: MapEngine | null = null
- function getFallbackStageRect(): MapEngineStageRect {
- const systemInfo = wx.getSystemInfoSync()
- const width = Math.max(320, systemInfo.windowWidth - 20)
- const height = Math.max(280, Math.floor(systemInfo.windowHeight * 0.66))
- return {
- width,
- height,
- left: 10,
- top: 0,
- }
- }
- Page({
- data: { showDebugPanel: false } as MapPageData,
- onLoad() {
- mapEngine = new MapEngine(INTERNAL_BUILD_VERSION, {
- onData: (patch) => {
- this.setData(patch)
- },
- })
- this.setData({ ...mapEngine.getInitialData(), showDebugPanel: false })
- },
- onReady() {
- this.measureStageAndCanvas()
- },
- onUnload() {
- if (mapEngine) {
- mapEngine.destroy()
- mapEngine = null
- }
- },
- measureStageAndCanvas() {
- const page = this
- const applyStage = (rawRect?: Partial<WechatMiniprogram.BoundingClientRectCallbackResult>) => {
- const fallbackRect = getFallbackStageRect()
- const rect: MapEngineStageRect = {
- width: rawRect && typeof rawRect.width === 'number' ? rawRect.width : fallbackRect.width,
- height: rawRect && typeof rawRect.height === 'number' ? rawRect.height : fallbackRect.height,
- left: rawRect && typeof rawRect.left === 'number' ? rawRect.left : fallbackRect.left,
- top: rawRect && typeof rawRect.top === 'number' ? rawRect.top : fallbackRect.top,
- }
- const currentEngine = mapEngine
- if (!currentEngine) {
- return
- }
- currentEngine.setStage(rect)
- const canvasQuery = wx.createSelectorQuery().in(page)
- canvasQuery.select('#mapCanvas').fields({ node: true, size: true })
- canvasQuery.exec((canvasRes) => {
- const canvasRef = canvasRes[0] as any
- if (!canvasRef || !canvasRef.node) {
- page.setData({
- statusText: `WebGL 引擎初始化失败 (${INTERNAL_BUILD_VERSION})`,
- })
- return
- }
- const dpr = wx.getSystemInfoSync().pixelRatio || 1
- try {
- currentEngine.attachCanvas(canvasRef.node, rect.width, rect.height, dpr)
- } catch (error) {
- page.setData({
- statusText: `WebGL 初始化失败 (${INTERNAL_BUILD_VERSION})`,
- })
- }
- })
- }
- const query = wx.createSelectorQuery().in(page)
- query.select('.map-stage').boundingClientRect()
- query.exec((res) => {
- const rect = res[0] as WechatMiniprogram.BoundingClientRectCallbackResult | undefined
- applyStage(rect)
- })
- },
- handleTouchStart(event: WechatMiniprogram.TouchEvent) {
- if (mapEngine) {
- mapEngine.handleTouchStart(event)
- }
- },
- handleTouchMove(event: WechatMiniprogram.TouchEvent) {
- if (mapEngine) {
- mapEngine.handleTouchMove(event)
- }
- },
- handleTouchEnd(event: WechatMiniprogram.TouchEvent) {
- if (mapEngine) {
- mapEngine.handleTouchEnd(event)
- }
- },
- handleTouchCancel() {
- if (mapEngine) {
- mapEngine.handleTouchCancel()
- }
- },
- handleRecenter() {
- if (mapEngine) {
- mapEngine.handleRecenter()
- }
- },
- handleRotateStep() {
- if (mapEngine) {
- mapEngine.handleRotateStep()
- }
- },
- handleRotationReset() {
- if (mapEngine) {
- mapEngine.handleRotationReset()
- }
- },
- handleSetManualMode() {
- if (mapEngine) {
- mapEngine.handleSetManualMode()
- }
- },
- handleSetNorthUpMode() {
- if (mapEngine) {
- mapEngine.handleSetNorthUpMode()
- }
- },
- handleSetHeadingUpMode() {
- if (mapEngine) {
- mapEngine.handleSetHeadingUpMode()
- }
- },
- handleAutoRotateCalibrate() {
- if (mapEngine) {
- mapEngine.handleAutoRotateCalibrate()
- }
- },
- handleToggleDebugPanel() {
- this.setData({
- showDebugPanel: !this.data.showDebugPanel,
- })
- },
- })
|