map.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { MapEngine, type MapEngineStageRect, type MapEngineViewState } from '../../engine/map/mapEngine'
  2. type MapPageData = MapEngineViewState & {
  3. showDebugPanel: boolean
  4. }
  5. const INTERNAL_BUILD_VERSION = 'map-build-63'
  6. let mapEngine: MapEngine | null = null
  7. function getFallbackStageRect(): MapEngineStageRect {
  8. const systemInfo = wx.getSystemInfoSync()
  9. const width = Math.max(320, systemInfo.windowWidth - 20)
  10. const height = Math.max(280, Math.floor(systemInfo.windowHeight * 0.66))
  11. return {
  12. width,
  13. height,
  14. left: 10,
  15. top: 0,
  16. }
  17. }
  18. Page({
  19. data: { showDebugPanel: false } as MapPageData,
  20. onLoad() {
  21. mapEngine = new MapEngine(INTERNAL_BUILD_VERSION, {
  22. onData: (patch) => {
  23. this.setData(patch)
  24. },
  25. })
  26. this.setData({ ...mapEngine.getInitialData(), showDebugPanel: false })
  27. },
  28. onReady() {
  29. this.measureStageAndCanvas()
  30. },
  31. onUnload() {
  32. if (mapEngine) {
  33. mapEngine.destroy()
  34. mapEngine = null
  35. }
  36. },
  37. measureStageAndCanvas() {
  38. const page = this
  39. const applyStage = (rawRect?: Partial<WechatMiniprogram.BoundingClientRectCallbackResult>) => {
  40. const fallbackRect = getFallbackStageRect()
  41. const rect: MapEngineStageRect = {
  42. width: rawRect && typeof rawRect.width === 'number' ? rawRect.width : fallbackRect.width,
  43. height: rawRect && typeof rawRect.height === 'number' ? rawRect.height : fallbackRect.height,
  44. left: rawRect && typeof rawRect.left === 'number' ? rawRect.left : fallbackRect.left,
  45. top: rawRect && typeof rawRect.top === 'number' ? rawRect.top : fallbackRect.top,
  46. }
  47. const currentEngine = mapEngine
  48. if (!currentEngine) {
  49. return
  50. }
  51. currentEngine.setStage(rect)
  52. const canvasQuery = wx.createSelectorQuery().in(page)
  53. canvasQuery.select('#mapCanvas').fields({ node: true, size: true })
  54. canvasQuery.exec((canvasRes) => {
  55. const canvasRef = canvasRes[0] as any
  56. if (!canvasRef || !canvasRef.node) {
  57. page.setData({
  58. statusText: `WebGL 引擎初始化失败 (${INTERNAL_BUILD_VERSION})`,
  59. })
  60. return
  61. }
  62. const dpr = wx.getSystemInfoSync().pixelRatio || 1
  63. try {
  64. currentEngine.attachCanvas(canvasRef.node, rect.width, rect.height, dpr)
  65. } catch (error) {
  66. page.setData({
  67. statusText: `WebGL 初始化失败 (${INTERNAL_BUILD_VERSION})`,
  68. })
  69. }
  70. })
  71. }
  72. const query = wx.createSelectorQuery().in(page)
  73. query.select('.map-stage').boundingClientRect()
  74. query.exec((res) => {
  75. const rect = res[0] as WechatMiniprogram.BoundingClientRectCallbackResult | undefined
  76. applyStage(rect)
  77. })
  78. },
  79. handleTouchStart(event: WechatMiniprogram.TouchEvent) {
  80. if (mapEngine) {
  81. mapEngine.handleTouchStart(event)
  82. }
  83. },
  84. handleTouchMove(event: WechatMiniprogram.TouchEvent) {
  85. if (mapEngine) {
  86. mapEngine.handleTouchMove(event)
  87. }
  88. },
  89. handleTouchEnd(event: WechatMiniprogram.TouchEvent) {
  90. if (mapEngine) {
  91. mapEngine.handleTouchEnd(event)
  92. }
  93. },
  94. handleTouchCancel() {
  95. if (mapEngine) {
  96. mapEngine.handleTouchCancel()
  97. }
  98. },
  99. handleRecenter() {
  100. if (mapEngine) {
  101. mapEngine.handleRecenter()
  102. }
  103. },
  104. handleRotateStep() {
  105. if (mapEngine) {
  106. mapEngine.handleRotateStep()
  107. }
  108. },
  109. handleRotationReset() {
  110. if (mapEngine) {
  111. mapEngine.handleRotationReset()
  112. }
  113. },
  114. handleSetManualMode() {
  115. if (mapEngine) {
  116. mapEngine.handleSetManualMode()
  117. }
  118. },
  119. handleSetNorthUpMode() {
  120. if (mapEngine) {
  121. mapEngine.handleSetNorthUpMode()
  122. }
  123. },
  124. handleSetHeadingUpMode() {
  125. if (mapEngine) {
  126. mapEngine.handleSetHeadingUpMode()
  127. }
  128. },
  129. handleAutoRotateCalibrate() {
  130. if (mapEngine) {
  131. mapEngine.handleAutoRotateCalibrate()
  132. }
  133. },
  134. handleToggleDebugPanel() {
  135. this.setData({
  136. showDebugPanel: !this.data.showDebugPanel,
  137. })
  138. },
  139. })