introduce TileMap type

This commit is contained in:
Daniel Barlow 2024-11-23 13:03:12 +00:00
parent 966026b3e7
commit 7bb1b9666a
2 changed files with 13 additions and 7 deletions

View File

@ -515,9 +515,10 @@ ifTrack model content =
Loading -> div [] [Html.text "loading"] Loading -> div [] [Html.text "loading"]
Empty -> div [] [Html.text "no points"] Empty -> div [] [Html.text "no points"]
canvas : Coord -> ZoomLevel -> Int -> Int -> Model -> Html Msg
canvas centre zoom width height model = canvas centre zoom width height model =
let (mintile, maxtile) = boundingTiles centre zoom width height let tm = TileMap centre zoom width height
(mintile, maxtile) = boundingTiles tm
-- offset is pixel difference between centre (which *should* -- offset is pixel difference between centre (which *should*
-- be the middle of the image) and actual middle of the canvas -- be the middle of the image) and actual middle of the canvas
(pixelCentreX,pixelCentreY) = pixelFromCoord centre zoom (pixelCentreX,pixelCentreY) = pixelFromCoord centre zoom
@ -538,7 +539,7 @@ canvas centre zoom width height model =
,Pointer.onUp (\e -> DragFinish (epos e)) ,Pointer.onUp (\e -> DragFinish (epos e))
,Pointer.onMove (\e -> Drag (epos e)) ,Pointer.onMove (\e -> Drag (epos e))
,Pointer.onDown (\e -> DragStart Map (epos e)) ] ,Pointer.onDown (\e -> DragStart Map (epos e)) ]
(tv :: tiles centre zoom width height) (tv :: tiles tm)
portalWidth = 600 portalWidth = 600

View File

@ -2,6 +2,7 @@ module TileMap exposing (tiles
, FineZoomLevel(..) , FineZoomLevel(..)
, ZoomLevel , ZoomLevel
, Coord , Coord
, TileMap(..)
, toCoord , toCoord
, toZoom , toZoom
, translate , translate
@ -26,6 +27,9 @@ type alias Coord = { x: Float, y: Float }
type alias ZoomLevel = Int type alias ZoomLevel = Int
type FineZoomLevel = FineZoomLevel Int type FineZoomLevel = FineZoomLevel Int
type TileMap = TileMap Coord ZoomLevel Int Int
zoomStep = 8 zoomStep = 8
toZoom : FineZoomLevel -> ZoomLevel toZoom : FineZoomLevel -> ZoomLevel
@ -75,8 +79,8 @@ pixelFromCoord c z =
let {x,y} = tileCovering c (z + 8) let {x,y} = tileCovering c (z + 8)
in (x,y) in (x,y)
boundingTiles : Coord -> ZoomLevel -> Int -> Int -> (TileNumber, TileNumber) boundingTiles : TileMap -> (TileNumber, TileNumber)
boundingTiles centre z width height = boundingTiles (TileMap centre z width height) =
-- find the tiles needed to cover the area (`width` x `height`) -- find the tiles needed to cover the area (`width` x `height`)
-- about the point at `centre` -- about the point at `centre`
let delta = pixelsToCoord z ((width // 2), (height // 2)) let delta = pixelsToCoord z ((width // 2), (height // 2))
@ -97,8 +101,9 @@ tileImg zoom tilenumber = img [ width 256,
height 256, height 256,
src (tileUrl tilenumber zoom) ] [] src (tileUrl tilenumber zoom) ] []
tiles centre zoom width height = tiles tmap =
let (mintile, maxtile) = boundingTiles centre zoom width height let (TileMap centre zoom width height) = tmap
(mintile, maxtile) = boundingTiles tmap
xs = List.range mintile.x maxtile.x xs = List.range mintile.x maxtile.x
ys = List.range mintile.y maxtile.y ys = List.range mintile.y maxtile.y
in in