5 Commits

Author SHA1 Message Date
djes
7c32502fe3 rotation wip 2016-10-08 14:40:47 +02:00
djes
6a91b81bc7 Better mouse handling + rotation wip 2016-10-07 11:59:02 +02:00
djes
287bd29104 Rotation wip 2016-10-04 16:06:09 +02:00
djes
926b2c5c16 bis 2016-10-04 13:57:21 +02:00
djes
29c1b39507 curl better error handling ; enter key fix ; alpha tiles fix 2016-10-04 13:56:49 +02:00

341
PBMap.pb
View File

@@ -26,8 +26,8 @@ DeclareModule PBMap
#Red = 255
;-Show debug infos
Global Verbose = 1
Global MyDebugLevel = 4
Global Verbose = 0
Global MyDebugLevel = 0
#SCALE_NAUTICAL = 1
#SCALE_KM = 0
@@ -55,6 +55,7 @@ DeclareModule PBMap
Declare MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i)
Declare SetLocation(latitude.d, longitude.d, Zoom = -1, mode.i = #PB_Absolute)
Declare Drawing()
Declare SetAngle(Angle.d, Mode = #PB_Absolute)
Declare SetZoom(Zoom.i, mode.i = #PB_Relative)
Declare ZoomToArea(MinY.d, MaxY.d, MinX.d, MaxX.d)
Declare ZoomToTracks(*Tracks)
@@ -77,6 +78,7 @@ DeclareModule PBMap
Declare.d GetLongitude()
Declare.d MouseLatitude()
Declare.d MouseLongitude()
Declare.d GetAngle()
Declare.i GetZoom()
Declare.i GetMode()
Declare SetMode(Mode.i = #MODE_DEFAULT)
@@ -88,8 +90,8 @@ Module PBMap
EnableExplicit
Structure PixelCoordinates
x.i
y.i
x.d
y.d
EndStructure
Structure Coordinates
@@ -110,12 +112,14 @@ Module PBMap
Structure BoundingBox
NorthWest.GeographicCoordinates
SouthEast.GeographicCoordinates
BottomLeft.PixelCoordinates
TopRight.PixelCoordinates
EndStructure
Structure DrawingParameters
Canvas.i
CenterX.i ; Gadget center in screen relative pixels
CenterY.i
CenterX.d ; Gadget center in screen relative pixels
CenterY.d
GeographicCoordinates.GeographicCoordinates ; Real center
TileCoordinates.Coordinates ; Center coordinates in tile.decimal
Bounds.BoundingBox ; Drawing boundaries in lat/lon
@@ -226,6 +230,7 @@ Module PBMap
List Layers.Layer() ;
Angle.d
ZoomMin.i ; Min Zoom supported by server
ZoomMax.i ; Max Zoom supported by server
Zoom.i ; Current zoom
@@ -264,7 +269,7 @@ Module PBMap
;Send debug infos to stdout (allowing mixed debug infos with curl or other libs)
Procedure MyDebug(msg.s, DbgLevel = 0)
If Verbose And MyDebugLevel >= DbgLevel
If Verbose And DbgLevel >= MyDebugLevel
PrintN(msg)
;Debug msg
EndIf
@@ -300,7 +305,7 @@ Module PBMap
EndProcedure
Procedure.i CurlReceiveHTTPToMemory(URL$, ProxyURL$="", ProxyPort$="", ProxyUser$="", ProxyPassword$="")
Protected *Buffer, curl.i, Timeout.i, res.i
Protected *Buffer, curl.i, Timeout.i, res.i, respcode.l
If Len(URL$)
curl = curl_easy_init()
If curl
@@ -311,6 +316,10 @@ Module PBMap
curl_easy_setopt(curl, #CURLOPT_HEADER, 0)
curl_easy_setopt(curl, #CURLOPT_FOLLOWLOCATION, 1)
curl_easy_setopt(curl, #CURLOPT_TIMEOUT, Timeout)
If Verbose
curl_easy_setopt(curl, #CURLOPT_VERBOSE, 1)
EndIf
curl_easy_setopt(curl, #CURLOPT_FAILONERROR, 1)
If Len(ProxyURL$)
;curl_easy_setopt(curl, #CURLOPT_HTTPPROXYTUNNEL, #True)
If Len(ProxyPort$)
@@ -340,7 +349,10 @@ Module PBMap
EndIf
;curl_easy_cleanup(curl) ;Was its original place but moved below as it seems more logical to me.
Else
MyDebug("CURL problem", 4)
curl_easy_getinfo(curl, #CURLINFO_HTTP_CODE, @respcode)
MyDebug("CURL : HTTP ERROR " + Str(respcode) , 8)
curl_easy_cleanup(curl)
ProcedureReturn #False
EndIf
curl_easy_cleanup(curl)
Else
@@ -357,10 +369,10 @@ Module PBMap
EndProcedure
Procedure.i CurlReceiveHTTPToFile(URL$, DestFileName$, ProxyURL$="", ProxyPort$="", ProxyUser$="", ProxyPassword$="")
Protected *Buffer, curl.i, Timeout.i, res.i
Protected *Buffer, curl.i, Timeout.i, res.i, respcode.l
Protected FileHandle.i
MyDebug("CurlReceiveHTTPToFile from " + URL$ + " " + ProxyURL$ + " " + ProxyPort$ + " " + ProxyUser$, 4)
MyDebug(" to file : " + DestFileName$, 4)
MyDebug("CurlReceiveHTTPToFile from " + URL$ + " " + ProxyURL$ + " " + ProxyPort$ + " " + ProxyUser$, 8)
MyDebug(" to file : " + DestFileName$, 8)
FileHandle = CreateFile(#PB_Any, DestFileName$)
If FileHandle And Len(URL$)
curl = curl_easy_init()
@@ -372,20 +384,23 @@ Module PBMap
curl_easy_setopt(curl, #CURLOPT_HEADER, 0)
curl_easy_setopt(curl, #CURLOPT_FOLLOWLOCATION, 1)
curl_easy_setopt(curl, #CURLOPT_TIMEOUT, Timeout)
curl_easy_setopt(curl, #CURLOPT_VERBOSE, 0)
If Verbose
curl_easy_setopt(curl, #CURLOPT_VERBOSE, 1)
EndIf
curl_easy_setopt(curl, #CURLOPT_FAILONERROR, 1)
;curl_easy_setopt(curl, #CURLOPT_CONNECTTIMEOUT, 60)
If Len(ProxyURL$)
;curl_easy_setopt(curl, #CURLOPT_HTTPPROXYTUNNEL, #True)
If Len(ProxyPort$)
ProxyURL$ + ":" + ProxyPort$
EndIf
MyDebug( ProxyURL$)
MyDebug(ProxyURL$, 8)
curl_easy_setopt(curl, #CURLOPT_PROXY, str2curl(ProxyURL$))
If Len(ProxyUser$)
If Len(ProxyPassword$)
ProxyUser$ + ":" + ProxyPassword$
EndIf
MyDebug( ProxyUser$)
MyDebug(ProxyUser$, 8)
curl_easy_setopt(curl, #CURLOPT_PROXYUSERPWD, str2curl(ProxyUser$))
EndIf
EndIf
@@ -393,11 +408,15 @@ Module PBMap
curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @ReceiveHTTPWriteToFileFunction())
res = curl_easy_perform(curl)
If res <> #CURLE_OK
MyDebug("CURL problem", 4)
curl_easy_getinfo(curl, #CURLINFO_HTTP_CODE, @respcode)
MyDebug("CURL : HTTP ERROR " + Str(respcode) , 8)
CloseFile(FileHandle)
curl_easy_cleanup(curl)
ProcedureReturn #False
EndIf
curl_easy_cleanup(curl)
Else
MyDebug("Can't init CURL", 4)
MyDebug("Can't init CURL", 8)
EndIf
CloseFile(FileHandle)
ProcedureReturn FileSize(DestFileName$)
@@ -746,8 +765,8 @@ Module PBMap
Protected cx.d = PBMap\Drawing\CenterX
Protected dpx.d = PBMap\PixelCoordinates\x
Protected LatRad.d = Radian(*Location\Latitude)
Protected px = tilemax * (Mod( *Location\Longitude + 180.0, 360) / 360.0 )
Protected py = tilemax * ( 1.0 - Log(Tan(LatRad) + (1.0/Cos(LatRad))) / #PI ) / 2.0
Protected px.d = tilemax * (Mod( *Location\Longitude + 180.0, 360) / 360.0 )
Protected py.d = tilemax * ( 1.0 - Log(Tan(LatRad) + (1.0/Cos(LatRad))) / #PI ) / 2.0
;check the x boundaries of the map to adjust the position (coz of the longitude wrapping)
If dpx - px >= tilemax / 2
;Debug "c1"
@@ -783,6 +802,7 @@ Module PBMap
ProcedureReturn (1000 * HaversineInKM(@*posA,@*posB));
EndProcedure
; No more used, see LatLon2PixelRel
Procedure GetPixelCoordFromLocation(*Location.GeographicCoordinates, *Pixel.PixelCoordinates, Zoom) ; TODO to Optimize
Protected mapWidth.l = Pow(2, Zoom + 8)
Protected mapHeight.l = Pow(2, Zoom + 8)
@@ -802,14 +822,21 @@ Module PBMap
*Pixel\y=GadgetHeight(PBMap\Gadget)/2 - (y2-y1)
EndProcedure
Procedure IsInDrawingPixelBoundaries(*Drawing.DrawingParameters, *Position.GeographicCoordinates)
Protected Pixel.PixelCoordinates
LatLon2Pixel(*Position, @Pixel, PBMap\Zoom)
If Pixel\x >= *Drawing\Bounds\BottomLeft\x And Pixel\y <= *Drawing\Bounds\BottomLeft\y And Pixel\x <= *Drawing\Bounds\TopRight\x And Pixel\y >= *Drawing\Bounds\TopRight\y
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
;TODO : rotation fix
Procedure IsInDrawingBoundaries(*Drawing.DrawingParameters, *Position.GeographicCoordinates)
Protected Lat.d = *Position\Latitude, Lon.d = *Position\Longitude
Protected LatNW.d = *Drawing\Bounds\NorthWest\Latitude, LonNW.d = *Drawing\Bounds\NorthWest\Longitude
Protected LatSE.d = *Drawing\Bounds\SouthEast\Latitude, LonSE.d = *Drawing\Bounds\SouthEast\Longitude
If LatSE > LatNW
Debug "WTF"
CallDebugger
EndIf
If Lat >= LatSE And Lat <= LatNW
If *Drawing\Width >= 360
ProcedureReturn #True
@@ -895,7 +922,7 @@ Module PBMap
*Tile\RetryNb = 0
Else
MyDebug("Image key : " + *Tile\key + " web image not correctly loaded", 3)
Delay(1000)
Delay(5000)
*Tile\RetryNb - 1
EndIf
Until *Tile\RetryNb <= 0
@@ -923,10 +950,9 @@ Module PBMap
ProcedureReturn *timg
EndIf
Else
AddMapElement(PBMap\MemCache\Images(), key)
PushMapPosition(PBMap\MemCache\Images())
;PushMapPosition(PBMap\MemCache\Images())
;*** Cache management
; if cache size exceeds limit, try to delete the oldest tile used
; if cache size exceeds limit, try to delete the oldest tile used (first in the list)
Protected CacheSize = MapSize(PBMap\MemCache\Images()) * Pow(PBMap\TileSize, 2) * 4 ; Size of a tile = TileSize * TileSize * 4 bytes (RGBA)
Protected CacheLimit = PBMap\Options\MaxMemCache * 1024
MyDebug("Cache size : " + Str(CacheSize/1024) + " / CacheLimit : " + Str(CacheLimit/1024), 4)
@@ -942,9 +968,11 @@ Module PBMap
CacheSize = MapSize(PBMap\MemCache\Images()) * Pow(PBMap\TileSize, 2) * 4 ; Size of a tile = TileSize * TileSize * 4 bytes (RGBA)
EndIf
Wend
PopMapPosition(PBMap\MemCache\Images())
LastElement(PBMap\MemCache\ImagesTimeStack())
;PopMapPosition(PBMap\MemCache\Images())
AddMapElement(PBMap\MemCache\Images(), key)
AddElement(PBMap\MemCache\ImagesTimeStack())
MoveElement(PBMap\MemCache\ImagesTimeStack(), #PB_List_Last)
;MoveElement(PBMap\MemCache\ImagesTimeStack(), #PB_List_Last)
PBMap\MemCache\ImagesTimeStack()\MapKey = MapKey(PBMap\MemCache\Images())
;***
MyDebug("Key : " + key + " added in memory cache!", 3)
@@ -957,7 +985,7 @@ Module PBMap
If img <> -1
MyDebug("Key : " + key + " found on HDD", 3)
*timg\nImage = img
*timg\Alpha = 0
*timg\Alpha = 256
ProcedureReturn *timg
EndIf
MyDebug("Key : " + key + " not found on HDD", 3)
@@ -1028,12 +1056,13 @@ Module PBMap
*timg = GetTile(key, URL, CacheFile)
If *timg\nImage <> -1
MovePathCursor(px, py)
If *timg\Alpha <= 224
DrawVectorImage(ImageID(*timg\nImage), *timg\Alpha)
If *timg\Alpha < 224
*timg\Alpha = (*timg\Alpha + 32) & $FF
*timg\Alpha + 32
PBMap\Redraw = #True
Else
*timg\Alpha = 255
DrawVectorImage(ImageID(*timg\nImage), 255)
*timg\Alpha = 256
EndIf
Else
MovePathCursor(px, py)
@@ -1151,7 +1180,7 @@ Module PBMap
StrokePath(1)
EndProcedure
Procedure TrackPointer(x.i, y.i, dist.l)
Procedure DrawTrackPointer(x.d, y.d, dist.l)
Protected color.l
color=RGBA(0, 0, 0, 255)
MovePathCursor(x,y)
@@ -1170,7 +1199,7 @@ Module PBMap
DrawVectorText(Str(dist))
EndProcedure
Procedure TrackPointerFirst(x.i, y.i, dist.l)
Procedure DrawTrackPointerFirst(x.d, y.d, dist.l)
Protected color.l
color=RGBA(0, 0, 0, 255)
MovePathCursor(x,y)
@@ -1231,7 +1260,7 @@ Module PBMap
;Check visibility
\Visible = #False
ForEach \Track()
If IsInDrawingBoundaries(*Drawing, @PBMap\TracksList()\Track())
If IsInDrawingPixelBoundaries(*Drawing, @PBMap\TracksList()\Track())
\Visible = #True
Break
EndIf
@@ -1241,9 +1270,9 @@ Module PBMap
ForEach \Track()
LatLon2PixelRel(@PBMap\TracksList()\Track(), @Pixel, PBMap\Zoom)
If ListIndex(\Track()) = 0
MovePathCursor(Pixel\X, Pixel\Y)
MovePathCursor(Pixel\x, Pixel\y)
Else
AddPathLine(Pixel\X, Pixel\Y)
AddPathLine(Pixel\x, Pixel\y)
EndIf
Next
; \BoundingBox\x = PathBoundsX()
@@ -1281,11 +1310,13 @@ Module PBMap
LatLon2PixelRel(@PBMap\TracksList()\Track(), @Pixel, PBMap\Zoom)
If Int(km) <> memKm
memKm = Int(km)
RotateCoordinates(Pixel\x, Pixel\y, -PBMap\Angle)
If Int(km) = 0
TrackPointerFirst(Pixel\X , Pixel\Y, Int(km))
DrawTrackPointerFirst(Pixel\x , Pixel\y, Int(km))
Else
TrackPointer(Pixel\X , Pixel\Y, Int(km))
DrawTrackPointer(Pixel\x , Pixel\y, Int(km))
EndIf
RotateCoordinates(Pixel\x, Pixel\y, PBMap\Angle)
EndIf
Next
EndIf
@@ -1465,15 +1496,15 @@ Module PBMap
Procedure DrawMarkers(*Drawing.DrawingParameters)
Protected Pixel.PixelCoordinates
ForEach PBMap\Markers()
If IsInDrawingBoundaries(*Drawing, @PBMap\Markers()\GeographicCoordinates)
LatLon2PixelRel(PBMap\Markers()\GeographicCoordinates, @Pixel, PBMap\Zoom)
If Pixel\X >= 0 And Pixel\Y >= 0 And Pixel\X < GadgetWidth(PBMap\Gadget) And Pixel\Y < GadgetHeight(PBMap\Gadget) ; Only if visible ^_^
If IsInDrawingPixelBoundaries(*Drawing, @PBMap\Markers()\GeographicCoordinates)
LatLon2PixelRel(@PBMap\Markers()\GeographicCoordinates, @Pixel, PBMap\Zoom)
RotateCoordinates(Pixel\x, Pixel\y, -PBMap\Angle)
If PBMap\Markers()\CallBackPointer > 0
CallFunctionFast(PBMap\Markers()\CallBackPointer, Pixel\X, Pixel\Y, PBMap\Markers()\Focus, PBMap\Markers()\Selected)
CallFunctionFast(PBMap\Markers()\CallBackPointer, Pixel\x, Pixel\y, PBMap\Markers()\Focus, PBMap\Markers()\Selected)
Else
DrawMarker(Pixel\X, Pixel\Y, ListIndex(PBMap\Markers()), @PBMap\Markers())
EndIf
DrawMarker(Pixel\x, Pixel\y, ListIndex(PBMap\Markers()), @PBMap\Markers())
EndIf
RotateCoordinates(Pixel\x, Pixel\y, PBMap\Angle)
EndIf
Next
EndProcedure
@@ -1513,6 +1544,7 @@ Module PBMap
;-*** Main drawing
Procedure Drawing()
Protected *Drawing.DrawingParameters = @PBMap\Drawing
Protected PixelCenter.PixelCoordinates
Protected Px.d, Py.d,a, ts = PBMap\TileSize, nx, ny
Protected NW.Coordinates, SE.Coordinates
PBMap\Dirty = #False
@@ -1523,25 +1555,47 @@ Module PBMap
*Drawing\GeographicCoordinates\Latitude = PBMap\GeographicCoordinates\Latitude
*Drawing\GeographicCoordinates\Longitude = PBMap\GeographicCoordinates\Longitude
LatLon2TileXY(*Drawing\GeographicCoordinates, *Drawing\TileCoordinates, PBMap\Zoom)
LatLon2Pixel(*Drawing\GeographicCoordinates, @PixelCenter, PBMap\Zoom)
; Pixel shift, aka position in the tile
Px = *Drawing\TileCoordinates\x
Py = *Drawing\TileCoordinates\y
*Drawing\DeltaX = Px * ts - (Int(Px) * ts) ;Don't forget the Int() !
*Drawing\DeltaY = Py * ts - (Int(Py) * ts)
;Drawing boundaries
nx = *Drawing\CenterX / ts ;How many tiles around the point
ny = *Drawing\CenterY / ts
NW\x = Px - nx - 1
NW\y = Py - ny - 1
SE\x = Px + nx + 2
SE\y = Py + ny + 2
TileXY2LatLon(@NW, *Drawing\Bounds\NorthWest, PBMap\Zoom)
TileXY2LatLon(@SE, *Drawing\Bounds\SouthEast, PBMap\Zoom)
*Drawing\Width = (SE\x / Pow(2, PBMap\Zoom) * 360.0) - (NW\x / Pow(2, PBMap\Zoom) * 360.0) ;Calculus without clipping
*Drawing\Height = *Drawing\Bounds\NorthWest\Latitude - *Drawing\Bounds\SouthEast\Latitude
; nx = *Drawing\CenterX / ts ;How many tiles around the point
; ny = *Drawing\CenterY / ts
; NW\x = Px - nx - 1
; NW\y = Py - ny - 1
; SE\x = Px + nx + 2
; SE\y = Py + ny + 2
; TileXY2LatLon(@NW, *Drawing\Bounds\NorthWest, PBMap\Zoom)
; TileXY2LatLon(@SE, *Drawing\Bounds\SouthEast, PBMap\Zoom)
;TODO : rotation fix
nx = PixelCenter\x - *Drawing\CenterX
ny = PixelCenter\y + *Drawing\CenterY
StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget))
RotateCoordinates(PixelCenter\x, PixelCenter\y, PBMap\Angle)
*Drawing\Bounds\BottomLeft\x = ConvertCoordinateX(nx, ny, #PB_Coordinate_Device, #PB_Coordinate_User)
*Drawing\Bounds\BottomLeft\y = ConvertCoordinateY(nx, ny, #PB_Coordinate_Device, #PB_Coordinate_User)
nx + GadgetWidth(PBMap\Gadget)
ny - GadgetHeight(PBMap\Gadget)
*Drawing\Bounds\TopRight\x = ConvertCoordinateX(nx, ny, #PB_Coordinate_Device, #PB_Coordinate_User)
*Drawing\Bounds\TopRight\y = ConvertCoordinateY(nx, ny, #PB_Coordinate_Device, #PB_Coordinate_User)
StopVectorDrawing()
Pixel2LatLon(*Drawing\Bounds\BottomLeft, *Drawing\Bounds\SouthEast, PBMap\Zoom)
Pixel2LatLon(*Drawing\Bounds\TopRight, *Drawing\Bounds\NorthWest, PBMap\Zoom)
; Debug *Drawing\Bounds\NorthWest\Latitude
; Debug *Drawing\Bounds\NorthWest\Longitude
; Debug *Drawing\Bounds\SouthEast\Latitude
; Debug *Drawing\Bounds\SouthEast\Longitude
;*Drawing\Width = (SE\x / Pow(2, PBMap\Zoom) * 360.0) - (NW\x / Pow(2, PBMap\Zoom) * 360.0) ;Calculus without clipping
;*Drawing\Height = *Drawing\Bounds\NorthWest\Latitude - *Drawing\Bounds\SouthEast\Latitude
;***
; Main drawing stuff
StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget))
;Main rotation
RotateCoordinates(*Drawing\CenterX, *Drawing\CenterY, PBMap\Angle)
;Clearscreen
VectorSourceColor(RGBA(150, 150, 150, 255))
FillVectorOutput()
;TODO add in layers of tiles ;this way we can cache them as 0 base 1.n layers
@@ -1549,21 +1603,22 @@ Module PBMap
ForEach PBMap\Layers()
DrawTiles(*Drawing, ListIndex(PBMap\Layers()))
Next
If PBMap\Options\ShowDegrees And PBMap\Zoom > 2
DrawDegrees(*Drawing, 192)
EndIf
If PBMap\Options\ShowTrack
DrawTracks(*Drawing)
EndIf
If PBMap\Options\ShowMarkers
DrawMarkers(*Drawing)
EndIf
ResetCoordinates()
If PBMap\Options\ShowPointer
DrawPointer(*Drawing)
EndIf
If PBMap\Options\ShowDebugInfos
DrawDebugInfos(*Drawing)
EndIf
If PBMap\Options\ShowDegrees And PBMap\Zoom > 2
DrawDegrees(*Drawing, 192)
EndIf
If PBMap\Options\ShowScale
DrawScale(*Drawing, 10, GadgetHeight(PBMAP\Gadget) - 20, 192)
EndIf
@@ -1708,6 +1763,16 @@ Module PBMap
EndIf
EndProcedure
Procedure SetAngle(Angle.d, Mode = #PB_Absolute)
If Mode = #PB_Absolute
PBmap\Angle = Angle
Else
PBMap\Angle + Angle
PBMap\Angle = Mod(PBMap\Angle,360)
EndIf
PBMap\Redraw = #True
EndProcedure
Procedure SetCallBackLocation(CallBackLocation.i)
PBMap\CallBackLocation = CallBackLocation
EndProcedure
@@ -1735,14 +1800,8 @@ Module PBMap
ProcedureReturn PBMap\Mode
EndProcedure
;Zoom on x, y position relative to the canvas gadget
Procedure SetZoomOnPosition(x, y, zoom)
Protected MouseX.d, MouseY.d
Protected OldPx.d, OldPy.d, OldMx.d, OldMy.d, Px.d, Py.d
Protected CenterX = GadgetWidth(PBMap\Gadget) / 2
Protected CenterY = GadgetHeight(PBMap\Gadget) / 2
x - CenterX
y - CenterY
;Zoom on x, y pixel position from the center
Procedure ZoomOnPixel(x, y, zoom)
;*** First : Zoom
PBMap\Zoom + zoom
If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : ProcedureReturn : EndIf
@@ -1764,7 +1823,16 @@ Module PBMap
EndIf
EndProcedure
;Go to x, y position relative to the canvas gadget
;Zoom on x, y position relative to the canvas gadget
Procedure ZoomOnPixelRel(x, y, zoom)
Protected CenterX = GadgetWidth(PBMap\Gadget) / 2
Protected CenterY = GadgetHeight(PBMap\Gadget) / 2
x - CenterX
y - CenterY
ZoomOnPixel(x, y, zoom)
EndProcedure
;Go to x, y position relative to the canvas gadget left up
Procedure GotoPixelRel(x, y)
Protected CenterX = GadgetWidth(PBMap\Gadget) / 2
Protected CenterY = GadgetHeight(PBMap\Gadget) / 2
@@ -1782,6 +1850,19 @@ Module PBMap
EndIf
EndProcedure
;Go to x, y position relative to the canvas gadget
Procedure GotoPixel(x, y)
PBMap\PixelCoordinates\x = x
PBMap\PixelCoordinates\y = y
Pixel2LatLon(@PBMap\PixelCoordinates, @PBMap\GeographicCoordinates, PBMap\Zoom)
; Start drawing
PBMap\Redraw = #True
; If CallBackLocation send Location to function
If PBMap\CallBackLocation > 0
CallFunctionFast(PBMap\CallBackLocation, @PBMap\GeographicCoordinates)
EndIf
EndProcedure
Procedure.d GetLatitude()
ProcedureReturn PBMap\GeographicCoordinates\Latitude
EndProcedure
@@ -1791,9 +1872,11 @@ Module PBMap
EndProcedure
Procedure.i GetZoom()
Protected Value.d
Value = PBMap\Zoom
ProcedureReturn Value
ProcedureReturn PBMap\Zoom
EndProcedure
Procedure.d GetAngle()
ProcedureReturn PBMap\Angle
EndProcedure
Procedure NominatimGeoLocationQuery(Address.s, *ReturnPosition.GeographicCoordinates = 0)
@@ -1834,13 +1917,23 @@ Module PBMap
EndProcedure
Procedure CanvasEvents()
Protected MouseX.i, MouseY.i
Protected CanvasMouseX.d, CanvasMouseY.d, MouseX.d, MouseY.d
Protected MarkerCoords.PixelCoordinates, *Tile.Tile, MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize
Protected key.s, Touch.i
Protected Pixel.PixelCoordinates
Static CtrlKey
PBMap\Moving = #False
MouseX = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) - PBMap\Drawing\CenterX
MouseY = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) - PBMap\Drawing\CenterY
StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget))
RotateCoordinates(0, 0, PBMap\Angle)
CanvasMouseX = ConvertCoordinateX(MouseX, MouseY, #PB_Coordinate_Device, #PB_Coordinate_User)
CanvasMouseY = ConvertCoordinateY(MouseX, MouseY, #PB_Coordinate_Device, #PB_Coordinate_User)
StopVectorDrawing()
Select EventType()
Case #PB_EventType_Focus
PBMap\Drawing\CenterX = GadgetWidth(PBMap\Gadget) / 2
PBMap\Drawing\CenterY = GadgetHeight(PBMap\Gadget) / 2
Case #PB_EventType_KeyUp
Select GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_Key)
Case #PB_Shortcut_Delete
@@ -1886,8 +1979,8 @@ Module PBMap
EndIf
Case #PB_EventType_LeftDoubleClick
LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom)
MouseX = PBMap\PixelCoordinates\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX)
MouseY = PBMap\PixelCoordinates\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY)
MouseX = PBMap\PixelCoordinates\x + CanvasMouseX
MouseY = PBMap\PixelCoordinates\y + CanvasMouseY
;Clip MouseX to the map range (in X, the map is infinite)
MouseX = Mod(Mod(MouseX, MapWidth) + MapWidth, MapWidth)
Touch = #False
@@ -1907,22 +2000,23 @@ Module PBMap
EndIf
Next
If Not Touch
GotoPixelRel(GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX), GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY))
GotoPixel(MouseX, MouseY)
EndIf
Case #PB_EventType_MouseWheel
If PBMap\Options\WheelMouseRelative
;Relative zoom (centered on the mouse)
SetZoomOnPosition(GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX), GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY), GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_WheelDelta))
ZoomOnPixel(CanvasMouseX, CanvasMouseY, GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_WheelDelta))
Else
;Absolute zoom (centered on the center of the map)
SetZoom(GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_WheelDelta), #PB_Relative)
EndIf
Case #PB_EventType_LeftButtonDown
LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom)
MouseX = PBMap\PixelCoordinates\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX)
MouseY = PBMap\PixelCoordinates\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY)
;LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom)
;Mem cursor Coord
PBMap\MoveStartingPoint\x = CanvasMouseX
PBMap\MoveStartingPoint\y = CanvasMouseY
;Clip MouseX to the map range (in X, the map is infinite)
MouseX = Mod(Mod(MouseX, MapWidth) + MapWidth, MapWidth)
PBMap\MoveStartingPoint\x = Mod(Mod(PBMap\MoveStartingPoint\x, MapWidth) + MapWidth, MapWidth)
If PBMap\Mode = #MODE_DEFAULT Or PBMap\Mode = #MODE_SELECT
PBMap\EditMarker = #False
;Check if we select marker(s)
@@ -1947,14 +2041,14 @@ Module PBMap
EndIf
Next
EndIf
;Mem cursor Coord
PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX)
PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY)
Case #PB_EventType_MouseMove
PBMap\Moving = #True
; Drag
If PBMap\MoveStartingPoint\x <> - 1
MouseX = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) - PBMap\MoveStartingPoint\x
MouseY = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) - PBMap\MoveStartingPoint\y
MouseX = CanvasMouseX - PBMap\MoveStartingPoint\x
MouseY = CanvasMouseY - PBMap\MoveStartingPoint\y
PBMap\MoveStartingPoint\x = CanvasMouseX
PBMap\MoveStartingPoint\y = CanvasMouseY
;Move selected markers
If PBMap\EditMarker And (PBMap\Mode = #MODE_DEFAULT Or PBMap\Mode = #MODE_SELECT)
ForEach PBMap\Markers()
@@ -1979,12 +2073,11 @@ Module PBMap
EndIf
EndIf
PBMap\Redraw = #True
PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX)
PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY)
Else
; Touch test
LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom)
MouseX = PBMap\PixelCoordinates\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX)
MouseY = PBMap\PixelCoordinates\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY)
MouseX = PBMap\PixelCoordinates\x + CanvasMouseX
MouseY = PBMap\PixelCoordinates\y + CanvasMouseY
;Clip MouseX to the map range (in X, the map is infinite)
MouseX = Mod(Mod(MouseX, MapWidth) + MapWidth, MapWidth)
If PBMap\Mode = #MODE_DEFAULT Or PBMap\Mode = #MODE_SELECT Or PBMap\Mode = #MODE_EDIT
@@ -1993,10 +2086,11 @@ Module PBMap
LatLon2Pixel(@PBMap\Markers()\GeographicCoordinates, @MarkerCoords, PBMap\Zoom)
If Distance(MarkerCoords\x, MarkerCoords\y, MouseX, MouseY) < 8
PBMap\Markers()\Focus = #True
Else
PBMap\Redraw = #True
ElseIf PBMap\Markers()\Focus
;If CtrlKey = #False
PBMap\Markers()\Focus = #False
;EndIf
PBMap\Redraw = #True
EndIf
Next
;Check if mouse touch tracks
@@ -2007,19 +2101,22 @@ Module PBMap
If ListSize(\Track()) > 0
If \Visible
StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget))
;Draw tracks
RotateCoordinates(PBMap\Drawing\CenterX, PBMap\Drawing\CenterY, PBMap\Angle)
;Simulate tracks drawing
ForEach \Track()
LatLon2PixelRel(@PBMap\TracksList()\Track(), @Pixel, PBMap\Zoom)
LatLon2Pixel(@PBMap\TracksList()\Track(), @Pixel, PBMap\Zoom)
If ListIndex(\Track()) = 0
MovePathCursor(Pixel\X, Pixel\Y)
MovePathCursor(Pixel\x, Pixel\y)
Else
AddPathLine(Pixel\X, Pixel\Y)
AddPathLine(Pixel\x, Pixel\y)
EndIf
Next
If IsInsideStroke(GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX), GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY), \StrokeWidth)
If IsInsideStroke(MouseX, MouseY, \StrokeWidth)
\Focus = #True
Else
PBMap\Redraw = #True
ElseIf \Focus
\Focus = #False
PBMap\Redraw = #True
EndIf
StopVectorDrawing()
EndIf
@@ -2027,9 +2124,7 @@ Module PBMap
Next
EndIf
EndWith
EndIf
PBMap\Redraw = #True
EndIf
Case #PB_EventType_LeftButtonUp
PBMap\MoveStartingPoint\x = - 1
@@ -2066,6 +2161,8 @@ Module PBMap
BindGadgetEvent(PBMap\Gadget, @CanvasEvents())
AddWindowTimer(PBMap\Window, PBMap\Timer, PBMap\Options\TimerInterval)
BindEvent(#PB_Event_Timer, @TimerEvents())
PBMap\Drawing\CenterX = GadgetWidth(PBMap\Gadget) / 2
PBMap\Drawing\CenterX = GadgetHeight(PBMap\Gadget) / 2
EndProcedure
; Creates a canvas and attach our map
@@ -2116,6 +2213,8 @@ CompilerIf #PB_Compiler_IsMainFile
#Gdt_Right
#Gdt_Up
#Gdt_Down
#Gdt_RotateLeft
#Gdt_RotateRight
#Button_4
#Button_5
#Combo_0
@@ -2135,6 +2234,12 @@ CompilerIf #PB_Compiler_IsMainFile
#StringGeoLocationQuery
EndEnumeration
;Menu events
Enumeration
#MenuEventLonLatStringEnter
#MenuEventGeoLocationStringEnter
EndEnumeration
Structure Location
Longitude.d
Latitude.d
@@ -2177,6 +2282,8 @@ CompilerIf #PB_Compiler_IsMainFile
ResizeGadget(#Text_1,WindowWidth(#Window_0)-170,#PB_Ignore,#PB_Ignore,#PB_Ignore)
ResizeGadget(#Gdt_Left, WindowWidth(#Window_0) - 150 ,#PB_Ignore,#PB_Ignore,#PB_Ignore)
ResizeGadget(#Gdt_Right,WindowWidth(#Window_0) - 90 ,#PB_Ignore,#PB_Ignore,#PB_Ignore)
ResizeGadget(#Gdt_RotateLeft, WindowWidth(#Window_0) - 150 ,#PB_Ignore,#PB_Ignore,#PB_Ignore)
ResizeGadget(#Gdt_RotateRight,WindowWidth(#Window_0) - 90 ,#PB_Ignore,#PB_Ignore,#PB_Ignore)
ResizeGadget(#Gdt_Up, WindowWidth(#Window_0) - 120 ,#PB_Ignore,#PB_Ignore,#PB_Ignore)
ResizeGadget(#Gdt_Down, WindowWidth(#Window_0) - 120 ,#PB_Ignore,#PB_Ignore,#PB_Ignore)
ResizeGadget(#Text_2,WindowWidth(#Window_0)-170,#PB_Ignore,#PB_Ignore,#PB_Ignore)
@@ -2201,8 +2308,11 @@ CompilerIf #PB_Compiler_IsMainFile
LoadFont(0, "Arial", 12)
LoadFont(1, "Arial", 12, #PB_Font_Bold)
LoadFont(2, "Arial", 8)
TextGadget(#Text_1, 530, 50, 60, 15, "Movements")
ButtonGadget(#Gdt_RotateLeft, 550, 070, 30, 30, "LRot") : SetGadgetFont(#Gdt_RotateLeft, FontID(2))
ButtonGadget(#Gdt_RotateRight, 610, 070, 30, 30, "RRot") : SetGadgetFont(#Gdt_RotateRight, FontID(2))
ButtonGadget(#Gdt_Left, 550, 100, 30, 30, Chr($25C4)) : SetGadgetFont(#Gdt_Left, FontID(0))
ButtonGadget(#Gdt_Right, 610, 100, 30, 30, Chr($25BA)) : SetGadgetFont(#Gdt_Right, FontID(0))
ButtonGadget(#Gdt_Up, 580, 070, 30, 30, Chr($25B2)) : SetGadgetFont(#Gdt_Up, FontID(0))
@@ -2222,7 +2332,7 @@ CompilerIf #PB_Compiler_IsMainFile
TextGadget(#TextGeoLocationQuery, 530, 435, 150, 15, "Enter an address")
StringGadget(#StringGeoLocationQuery, 530, 450, 150, 20, "")
SetActiveGadget(#StringGeoLocationQuery)
AddKeyboardShortcut(#Window_0, #PB_Shortcut_Return, 1)
AddKeyboardShortcut(#Window_0, #PB_Shortcut_Return, #MenuEventGeoLocationStringEnter)
;*** TODO : code to remove when the SetActiveGadget(-1) will be fixed
CompilerIf #PB_Compiler_OS = #PB_OS_Linux
Define Dummy = ButtonGadget(#PB_Any, 0, 0, 1, 1, "Dummy")
@@ -2230,6 +2340,7 @@ CompilerIf #PB_Compiler_IsMainFile
CompilerElse
Define Dummy = -1
CompilerEndIf
;***
Define Event.i, Gadget.i, Quit.b = #False
Define pfValue.d
Define OpenSeaMap = 0, Degrees = 1
@@ -2265,6 +2376,12 @@ CompilerIf #PB_Compiler_IsMainFile
PBMap::SetLocation(0, 10* -360 / Pow(2, PBMap::GetZoom() + 8), 0, #PB_Relative)
Case #Gdt_Right
PBMap::SetLocation(0, 10* 360 / Pow(2, PBMap::GetZoom() + 8), 0, #PB_Relative)
Case #Gdt_RotateLeft
PBMAP::SetAngle(-5,#PB_Relative)
PBMap::Refresh()
Case #Gdt_RotateRight
PBMAP::SetAngle(5,#PB_Relative)
PBMap::Refresh()
Case #Button_4
PBMap::SetZoom(1)
Case #Button_5
@@ -2275,11 +2392,9 @@ CompilerIf #PB_Compiler_IsMainFile
Case #StringLatitude, #StringLongitude
Select EventType()
Case #PB_EventType_Focus
AddKeyboardShortcut(#Window_0, #PB_Shortcut_Return, 1)
AddKeyboardShortcut(#Window_0, #PB_Shortcut_Return, #MenuEventLonLatStringEnter)
Case #PB_EventType_LostFocus
RemoveKeyboardShortcut(#Window_0, #PB_Shortcut_Return)
PBMap::SetLocation(ValD(GetGadgetText(#StringLatitude)), ValD(GetGadgetText(#StringLongitude))) ; Change the PBMap coordinates
PBMap::Refresh()
EndSelect
Case #Gdt_AddMarker
PBMap::AddMarker(ValD(GetGadgetText(#StringLatitude)), ValD(GetGadgetText(#StringLongitude)), "", "Test", RGBA(Random(255), Random(255), Random(255), 255))
@@ -2309,21 +2424,27 @@ CompilerIf #PB_Compiler_IsMainFile
Case #StringGeoLocationQuery
Select EventType()
Case #PB_EventType_Focus
AddKeyboardShortcut(#Window_0, #PB_Shortcut_Return, 1)
AddKeyboardShortcut(#Window_0, #PB_Shortcut_Return, #MenuEventGeoLocationStringEnter)
Case #PB_EventType_LostFocus
RemoveKeyboardShortcut(#Window_0, #PB_Shortcut_Return)
PBMap::NominatimGeoLocationQuery(GetGadgetText(#StringGeoLocationQuery))
PBMap::Refresh()
EndSelect
EndSelect
Case #PB_Event_SizeWindow
ResizeAll()
Case #PB_Event_Menu
;Receive "enter" key events
Select EventMenu()
Case 1
Case #MenuEventGeoLocationStringEnter
If GetGadgetText(#StringGeoLocationQuery) <> ""
PBMap::NominatimGeoLocationQuery(GetGadgetText(#StringGeoLocationQuery))
PBMap::Refresh()
EndIf
;*** TODO : code to change when the SetActiveGadget(-1) will be fixed
SetActiveGadget(Dummy)
;***
Case #MenuEventLonLatStringEnter
PBMap::SetLocation(ValD(GetGadgetText(#StringLatitude)), ValD(GetGadgetText(#StringLongitude))) ; Change the PBMap coordinates
PBMap::Refresh()
EndSelect
EndSelect
Until Quit = #True
@@ -2335,9 +2456,9 @@ CompilerEndIf
; IDE Options = PureBasic 5.42 LTS (Windows - x64)
; CursorPosition = 1015
; FirstLine = 1007
; Folding = ----------------
; CursorPosition = 2430
; FirstLine = 2401
; Folding = -----------------
; EnableUnicode
; EnableThread
; EnableXP