map.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. import { MapEngine, type MapEngineStageRect, type MapEngineViewState } from '../../engine/map/mapEngine'
  2. import { loadRemoteMapConfig } from '../../utils/remoteMapConfig'
  3. type CompassTickData = {
  4. angle: number
  5. long: boolean
  6. major: boolean
  7. }
  8. type CompassLabelData = {
  9. text: string
  10. angle: number
  11. rotateBack: number
  12. radius: number
  13. className: string
  14. }
  15. type SideButtonMode = 'all' | 'left' | 'right' | 'hidden'
  16. type MapPageData = MapEngineViewState & {
  17. showDebugPanel: boolean
  18. statusBarHeight: number
  19. topInsetHeight: number
  20. panelTimerText: string
  21. panelMileageText: string
  22. panelDistanceValueText: string
  23. panelProgressText: string
  24. panelSpeedValueText: string
  25. compassTicks: CompassTickData[]
  26. compassLabels: CompassLabelData[]
  27. sideButtonMode: SideButtonMode
  28. showLeftButtonGroup: boolean
  29. showRightButtonGroups: boolean
  30. showBottomDebugButton: boolean
  31. }
  32. const INTERNAL_BUILD_VERSION = 'map-build-134'
  33. const REMOTE_GAME_CONFIG_URL = 'https://oss-mbh5.colormaprun.com/wxmini/test/game.json'
  34. let mapEngine: MapEngine | null = null
  35. function buildSideButtonVisibility(mode: SideButtonMode) {
  36. return {
  37. sideButtonMode: mode,
  38. showLeftButtonGroup: mode === 'all' || mode === 'left' || mode === 'right',
  39. showRightButtonGroups: mode === 'all' || mode === 'right',
  40. showBottomDebugButton: mode !== 'hidden',
  41. }
  42. }
  43. function getNextSideButtonMode(currentMode: SideButtonMode): SideButtonMode {
  44. if (currentMode === 'all') {
  45. return 'left'
  46. }
  47. if (currentMode === 'left') {
  48. return 'right'
  49. }
  50. if (currentMode === 'right') {
  51. return 'hidden'
  52. }
  53. return 'left'
  54. }
  55. function buildCompassTicks(): CompassTickData[] {
  56. const ticks: CompassTickData[] = []
  57. for (let angle = 0; angle < 360; angle += 5) {
  58. ticks.push({
  59. angle,
  60. long: angle % 15 === 0,
  61. major: angle % 45 === 0,
  62. })
  63. }
  64. return ticks
  65. }
  66. function buildCompassLabels(): CompassLabelData[] {
  67. return [
  68. { text: '\u5317', angle: 0, rotateBack: 0, radius: 68, className: 'compass-widget__mark--cardinal compass-widget__mark--north' },
  69. { text: '\u4e1c\u5317', angle: 45, rotateBack: 0, radius: 58, className: 'compass-widget__mark--intermediate compass-widget__mark--northeast' },
  70. { text: '\u4e1c', angle: 90, rotateBack: 0, radius: 68, className: 'compass-widget__mark--cardinal' },
  71. { text: '\u4e1c\u5357', angle: 135, rotateBack: 0, radius: 58, className: 'compass-widget__mark--intermediate' },
  72. { text: '\u5357', angle: 180, rotateBack: 0, radius: 68, className: 'compass-widget__mark--cardinal' },
  73. { text: '\u897f\u5357', angle: 225, rotateBack: 0, radius: 58, className: 'compass-widget__mark--intermediate' },
  74. { text: '\u897f', angle: 270, rotateBack: 0, radius: 68, className: 'compass-widget__mark--cardinal' },
  75. { text: '\u897f\u5317', angle: 315, rotateBack: 0, radius: 58, className: 'compass-widget__mark--intermediate compass-widget__mark--northwest' },
  76. ]
  77. }
  78. function getFallbackStageRect(): MapEngineStageRect {
  79. const systemInfo = wx.getSystemInfoSync()
  80. const width = Math.max(320, systemInfo.windowWidth)
  81. const height = Math.max(280, systemInfo.windowHeight)
  82. return {
  83. width,
  84. height,
  85. left: 0,
  86. top: 0,
  87. }
  88. }
  89. Page({
  90. data: {
  91. showDebugPanel: false,
  92. statusBarHeight: 0,
  93. topInsetHeight: 12,
  94. panelTimerText: '00:00:00',
  95. panelMileageText: '0m',
  96. panelDistanceValueText: '108',
  97. panelProgressText: '0/14',
  98. panelSpeedValueText: '0',
  99. compassTicks: buildCompassTicks(),
  100. compassLabels: buildCompassLabels(),
  101. ...buildSideButtonVisibility('left'),
  102. } as MapPageData,
  103. onLoad() {
  104. const systemInfo = wx.getSystemInfoSync()
  105. const statusBarHeight = systemInfo.statusBarHeight || 0
  106. const menuButtonRect = wx.getMenuButtonBoundingClientRect()
  107. const menuButtonBottom = menuButtonRect && typeof menuButtonRect.bottom === 'number' ? menuButtonRect.bottom : statusBarHeight
  108. mapEngine = new MapEngine(INTERNAL_BUILD_VERSION, {
  109. onData: (patch) => {
  110. this.setData(patch)
  111. },
  112. })
  113. this.setData({
  114. ...mapEngine.getInitialData(),
  115. showDebugPanel: false,
  116. statusBarHeight,
  117. topInsetHeight: Math.max(statusBarHeight + 12, menuButtonBottom + 20),
  118. panelTimerText: '00:00:00',
  119. panelMileageText: '0m',
  120. panelDistanceValueText: '108',
  121. panelProgressText: '0/14',
  122. panelSpeedValueText: '0',
  123. compassTicks: buildCompassTicks(),
  124. compassLabels: buildCompassLabels(),
  125. ...buildSideButtonVisibility('left'),
  126. })
  127. },
  128. onReady() {
  129. this.measureStageAndCanvas()
  130. this.loadMapConfigFromRemote()
  131. },
  132. onUnload() {
  133. if (mapEngine) {
  134. mapEngine.destroy()
  135. mapEngine = null
  136. }
  137. },
  138. loadMapConfigFromRemote() {
  139. const currentEngine = mapEngine
  140. if (!currentEngine) {
  141. return
  142. }
  143. loadRemoteMapConfig(REMOTE_GAME_CONFIG_URL)
  144. .then((config) => {
  145. if (mapEngine !== currentEngine) {
  146. return
  147. }
  148. currentEngine.applyRemoteMapConfig(config)
  149. })
  150. .catch((error) => {
  151. if (mapEngine !== currentEngine) {
  152. return
  153. }
  154. const errorMessage = error && error.message ? error.message : '鏈煡閿欒'
  155. this.setData({
  156. configStatusText: `杞藉叆澶辫触: ${errorMessage}`,
  157. statusText: `杩滅▼鍦板浘閰嶇疆杞藉叆澶辫触: ${errorMessage} (${INTERNAL_BUILD_VERSION})`,
  158. })
  159. })
  160. },
  161. measureStageAndCanvas() {
  162. const page = this
  163. const applyStage = (rawRect?: Partial<WechatMiniprogram.BoundingClientRectCallbackResult>) => {
  164. const fallbackRect = getFallbackStageRect()
  165. const rect: MapEngineStageRect = {
  166. width: rawRect && typeof rawRect.width === 'number' ? rawRect.width : fallbackRect.width,
  167. height: rawRect && typeof rawRect.height === 'number' ? rawRect.height : fallbackRect.height,
  168. left: rawRect && typeof rawRect.left === 'number' ? rawRect.left : fallbackRect.left,
  169. top: rawRect && typeof rawRect.top === 'number' ? rawRect.top : fallbackRect.top,
  170. }
  171. const currentEngine = mapEngine
  172. if (!currentEngine) {
  173. return
  174. }
  175. currentEngine.setStage(rect)
  176. const canvasQuery = wx.createSelectorQuery().in(page)
  177. canvasQuery.select('#mapCanvas').fields({ node: true, size: true })
  178. canvasQuery.select('#routeLabelCanvas').fields({ node: true, size: true })
  179. canvasQuery.exec((canvasRes) => {
  180. const canvasRef = canvasRes[0] as any
  181. const labelCanvasRef = canvasRes[1] as any
  182. if (!canvasRef || !canvasRef.node) {
  183. page.setData({
  184. statusText: `WebGL 寮曟搸鍒濆鍖栧け璐?(${INTERNAL_BUILD_VERSION})`,
  185. })
  186. return
  187. }
  188. const dpr = wx.getSystemInfoSync().pixelRatio || 1
  189. try {
  190. currentEngine.attachCanvas(
  191. canvasRef.node,
  192. rect.width,
  193. rect.height,
  194. dpr,
  195. labelCanvasRef && labelCanvasRef.node ? labelCanvasRef.node : undefined,
  196. )
  197. } catch (error) {
  198. page.setData({
  199. statusText: `WebGL 鍒濆鍖栧け璐?(${INTERNAL_BUILD_VERSION})`,
  200. })
  201. }
  202. })
  203. }
  204. const query = wx.createSelectorQuery().in(page)
  205. query.select('.map-stage').boundingClientRect()
  206. query.exec((res) => {
  207. const rect = res[0] as WechatMiniprogram.BoundingClientRectCallbackResult | undefined
  208. applyStage(rect)
  209. })
  210. },
  211. handleTouchStart(event: WechatMiniprogram.TouchEvent) {
  212. if (mapEngine) {
  213. mapEngine.handleTouchStart(event)
  214. }
  215. },
  216. handleTouchMove(event: WechatMiniprogram.TouchEvent) {
  217. if (mapEngine) {
  218. mapEngine.handleTouchMove(event)
  219. }
  220. },
  221. handleTouchEnd(event: WechatMiniprogram.TouchEvent) {
  222. if (mapEngine) {
  223. mapEngine.handleTouchEnd(event)
  224. }
  225. },
  226. handleTouchCancel() {
  227. if (mapEngine) {
  228. mapEngine.handleTouchCancel()
  229. }
  230. },
  231. handleRecenter() {
  232. if (mapEngine) {
  233. mapEngine.handleRecenter()
  234. }
  235. },
  236. handleRotateStep() {
  237. if (mapEngine) {
  238. mapEngine.handleRotateStep()
  239. }
  240. },
  241. handleRotationReset() {
  242. if (mapEngine) {
  243. mapEngine.handleRotationReset()
  244. }
  245. },
  246. handleSetManualMode() {
  247. if (mapEngine) {
  248. mapEngine.handleSetManualMode()
  249. }
  250. },
  251. handleSetNorthUpMode() {
  252. if (mapEngine) {
  253. mapEngine.handleSetNorthUpMode()
  254. }
  255. },
  256. handleSetHeadingUpMode() {
  257. if (mapEngine) {
  258. mapEngine.handleSetHeadingUpMode()
  259. }
  260. },
  261. handleCycleNorthReferenceMode() {
  262. if (mapEngine) {
  263. mapEngine.handleCycleNorthReferenceMode()
  264. }
  265. },
  266. handleAutoRotateCalibrate() {
  267. if (mapEngine) {
  268. mapEngine.handleAutoRotateCalibrate()
  269. }
  270. },
  271. handleToggleGpsTracking() {
  272. if (mapEngine) {
  273. mapEngine.handleToggleGpsTracking()
  274. }
  275. },
  276. handleToggleOsmReference() {
  277. if (mapEngine) {
  278. mapEngine.handleToggleOsmReference()
  279. }
  280. },
  281. handleCycleSideButtons() {
  282. this.setData(buildSideButtonVisibility(getNextSideButtonMode(this.data.sideButtonMode)))
  283. },
  284. handleToggleMapRotateMode() {
  285. if (!mapEngine) {
  286. return
  287. }
  288. if (this.data.orientationMode === 'heading-up') {
  289. mapEngine.handleSetManualMode()
  290. return
  291. }
  292. mapEngine.handleSetHeadingUpMode()
  293. },
  294. handleToggleDebugPanel() {
  295. this.setData({
  296. showDebugPanel: !this.data.showDebugPanel,
  297. })
  298. },
  299. handleCloseDebugPanel() {
  300. this.setData({
  301. showDebugPanel: false,
  302. })
  303. },
  304. handleDebugPanelTap() {},
  305. })