This commit is contained in:
djes
2016-07-25 13:41:15 +02:00
parent 90ce841007
commit e53f53a975

119
osm.pb
View File

@@ -90,9 +90,10 @@ Module OSM
Gadget.i ; Canvas Gadget Id Gadget.i ; Canvas Gadget Id
TargetLocation.Location ; Latitude and Longitude from focus point TargetLocation.Location ; Latitude and Longitude from focus point
TargetTile.Tile ; Focus tile coords
*Drawing.DrawingParameters ; Drawing parameters based on focus point *Drawing.DrawingParameters ; Drawing parameters based on focus point
CallBackLocation.i ; @Procedure(latitude.d,lontitude.d) CallBackLocation.i ; @Procedure(latitude.d,lontitude.d)
Position.Pixel ; Actual focus Point coords in pixels Position.Pixel ; Actual focus Point coords in pixels
MoveStartingPoint.Pixel ; Start mouse position coords when dragging the map MoveStartingPoint.Pixel ; Start mouse position coords when dragging the map
@@ -112,7 +113,7 @@ Module OSM
Dirty.i ;To signal that drawing need a refresh Dirty.i ;To signal that drawing need a refresh
LoadingMutex.i LoadingMutex.i
DrawingMutex.i DrawingMutex.i
;CurlMutex.i ;seems that I can't thread curl ! :((((( ;CurlMutex.i ;seems that I can't thread curl ! :(((((
List TilesThreads.TileThread() List TilesThreads.TileThread()
MapImageMutex.i ; Mutex to lock MapImageMutex.i ; Mutex to lock
@@ -243,7 +244,7 @@ Module OSM
;-*** PROXY ;-*** PROXY
Global Proxy = #True Global Proxy = #False
;- => Use this to customise your preferences ;- => Use this to customise your preferences
; Result = CreatePreferences(GetHomeDirectory() + "OSM.prefs") ; Result = CreatePreferences(GetHomeDirectory() + "OSM.prefs")
@@ -272,7 +273,7 @@ Module OSM
curl_global_init(#CURL_GLOBAL_ALL); curl_global_init(#CURL_GLOBAL_ALL);
EndProcedure EndProcedure
;- ** ;- ***
Procedure OSMGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) Procedure OSMGadget(Gadget.i, X.i, Y.i, Width.i, Height.i)
If Gadget = #PB_Any If Gadget = #PB_Any
@@ -283,7 +284,7 @@ Module OSM
EndIf EndIf
EndProcedure EndProcedure
;*** Converts coords to tile.decimal
Procedure LatLon2XY(*Location.Location, *Tile.Tile) Procedure LatLon2XY(*Location.Location, *Tile.Tile)
Protected n.d = Pow(2.0, OSM\Zoom) Protected n.d = Pow(2.0, OSM\Zoom)
Protected LatRad.d = Radian(*Location\Latitude) Protected LatRad.d = Radian(*Location\Latitude)
@@ -293,6 +294,7 @@ Module OSM
Debug "Tile X : " + Str(*Tile\x) + " ; Tile Y : " + Str(*Tile\y) Debug "Tile X : " + Str(*Tile\x) + " ; Tile Y : " + Str(*Tile\y)
EndProcedure EndProcedure
;*** Converts tile.decimal to coords
Procedure XY2LatLon(*Tile.Tile, *Location.Location) Procedure XY2LatLon(*Tile.Tile, *Location.Location)
Protected n.d = Pow(2.0, OSM\Zoom) Protected n.d = Pow(2.0, OSM\Zoom)
Protected LatitudeRad.d Protected LatitudeRad.d
@@ -306,8 +308,8 @@ Module OSM
Protected mapHeight.l = Pow(2,OSM\Zoom+8) Protected mapHeight.l = Pow(2,OSM\Zoom+8)
Protected x1.l,y1.l Protected x1.l,y1.l
Protected deltaX = OSM\Position\x - Int(OSM\TargetTile\x) * OSM\TileSize ;Get the position into the tile ;Protected deltaX = OSM\Position\x - Int(OSM\TargetTile\x) * OSM\TileSize ;Get the position into the tile
Protected deltaY = OSM\Position\y - Int(OSM\TargetTile\y) * OSM\TileSize ;Protected deltaY = OSM\Position\y - Int(OSM\TargetTile\y) * OSM\TileSize
; get x value ; get x value
x1 = (*Location\Longitude+180)*(mapWidth/360) x1 = (*Location\Longitude+180)*(mapWidth/360)
@@ -326,8 +328,8 @@ Module OSM
mercN = Log(Tan((#PI/4)+(latRad/2))) ; mercN = Log(Tan((#PI/4)+(latRad/2))) ;
y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)); y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI));
*Pixel\x=GadgetWidth(OSM\Gadget)/2 - (x2-x1) + deltaX *Pixel\x=GadgetWidth(OSM\Gadget)/2 - (x2-x1); + deltaX
*Pixel\y=GadgetHeight(OSM\Gadget)/2 - (y2-y1) + deltaY *Pixel\y=GadgetHeight(OSM\Gadget)/2 - (y2-y1);+ deltaY
EndProcedure EndProcedure
Procedure LoadGpxFile(file.s) Procedure LoadGpxFile(file.s)
@@ -489,8 +491,8 @@ Module OSM
Procedure DrawTile(*Tile.Tile) Procedure DrawTile(*Tile.Tile)
Protected x = *Tile\x - OSM\DeltaX Protected x = *Tile\x ; - OSM\DeltaX
Protected y = *Tile\y - OSM\DeltaY Protected y = *Tile\y ; - OSM\DeltaY
Debug " Drawing tile nb " + " X : " + Str(x) + " Y : " + Str(y) Debug " Drawing tile nb " + " X : " + Str(x) + " Y : " + Str(y)
@@ -498,9 +500,9 @@ Module OSM
If OSM\EmergencyQuit = 0 ;Quit before drawing If OSM\EmergencyQuit = 0 ;Quit before drawing
StartVectorDrawing(CanvasVectorOutput(OSM\Gadget)) StartVectorDrawing(CanvasVectorOutput(OSM\Gadget))
If IsImage(*Tile\nImage) If IsImage(*Tile\nImage)
MovePathCursor(x,y) MovePathCursor(x, y)
DrawVectorImage(ImageID(*Tile\nImage)) DrawVectorImage(ImageID(*Tile\nImage))
MovePathCursor(x,y) MovePathCursor(x, y)
DrawVectorText(Str(x) + ", " + Str(y)) DrawVectorText(Str(x) + ", " + Str(y))
Else Else
Debug "Image missing" Debug "Image missing"
@@ -516,8 +518,8 @@ Module OSM
Protected x.i, y.i Protected x.i, y.i
Protected tx = Int(OSM\TargetTile\x) ;Don't forget the Int() ! Protected tx = Int(*Drawing\x) ;Don't forget the Int() !
Protected ty = Int(OSM\TargetTile\y) Protected ty = Int(*Drawing\y)
Protected CenterX = GadgetWidth(OSM\Gadget) / 2 Protected CenterX = GadgetWidth(OSM\Gadget) / 2
Protected CenterY = GadgetHeight(OSM\Gadget) / 2 Protected CenterY = GadgetHeight(OSM\Gadget) / 2
@@ -543,8 +545,8 @@ Module OSM
OSM\TilesThreads()\Tile = *NewTile OSM\TilesThreads()\Tile = *NewTile
;New tile parameters ;New tile parameters
\x = CenterX + x * OSM\TileSize \x = CenterX + x * OSM\TileSize - *Drawing\DeltaX
\y = CenterY + y * OSM\TileSize \y = CenterY + y * OSM\TileSize - *Drawing\DeltaY
\OSMTileX = tx + x \OSMTileX = tx + x
\OSMTileY = ty + y \OSMTileY = ty + y
\OSMZoom = OSM\Zoom \OSMZoom = OSM\Zoom
@@ -579,7 +581,7 @@ Module OSM
EndProcedure EndProcedure
Procedure DrawTrack() Procedure DrawTrack(*Drawing.DrawingParameters)
Protected Pixel.Pixel Protected Pixel.Pixel
Protected Location.Location Protected Location.Location
If ListSize(OSM\track())>0 If ListSize(OSM\track())>0
@@ -588,9 +590,9 @@ Module OSM
If @OSM\TargetLocation\Latitude<>0 And @OSM\TargetLocation\Longitude<>0 If @OSM\TargetLocation\Latitude<>0 And @OSM\TargetLocation\Longitude<>0
getPixelCoorfromLocation(@OSM\track(),@Pixel) getPixelCoorfromLocation(@OSM\track(),@Pixel)
If ListIndex(OSM\track())=0 If ListIndex(OSM\track())=0
MovePathCursor(Pixel\X,Pixel\Y) MovePathCursor(Pixel\X + *Drawing\DeltaX, Pixel\Y + *Drawing\DeltaY)
Else Else
AddPathLine(Pixel\X,Pixel\Y) AddPathLine(Pixel\X + *Drawing\DeltaX, Pixel\Y + *Drawing\DeltaY)
EndIf EndIf
EndIf EndIf
@@ -630,7 +632,7 @@ Module OSM
LockMutex(OSM\DrawingMutex) LockMutex(OSM\DrawingMutex)
StartVectorDrawing(CanvasVectorOutput(OSM\Gadget)) StartVectorDrawing(CanvasVectorOutput(OSM\Gadget))
DrawTrack() DrawTrack(*Drawing)
Pointer(CenterX, CenterY, #Red) Pointer(CenterX, CenterY, #Red)
StopVectorDrawing() StopVectorDrawing()
UnlockMutex(OSM\DrawingMutex) UnlockMutex(OSM\DrawingMutex)
@@ -642,36 +644,46 @@ Module OSM
If OSM\Dirty And OSM\EmergencyQuit = #False If OSM\Dirty And OSM\EmergencyQuit = #False
Debug "Something was dirty ! We try again to redraw" Debug "Something was dirty ! We try again to redraw"
;Delay(250) ;Delay(250)
*Drawing\PassNb + 1 *Drawing\PassNb + 1
CreateThread(@DrawingThread(), *Drawing) CreateThread(@DrawingThread(), *Drawing)
EndIf EndIf
*Drawing\PassNb - 1 *Drawing\PassNb - 1
If *Drawing\PassNb = 0 If *Drawing\PassNb = 0
FreeMemory(*TargetTile) FreeMemory(*Drawing)
EndIf EndIf
EndProcedure EndProcedure
Procedure SetLocation(latitude.d, longitude.d, zoom = 15) Procedure SetLocation(latitude.d, longitude.d, zoom = 15)
Protected *Drawing.DrawingParameters
If zoom > OSM\ZoomMax : zoom = OSM\ZoomMax : EndIf If zoom > OSM\ZoomMax : zoom = OSM\ZoomMax : EndIf
If zoom < OSM\ZoomMin : zoom = OSM\ZoomMin : EndIf If zoom < OSM\ZoomMin : zoom = OSM\ZoomMin : EndIf
OSM\Zoom = zoom OSM\Zoom = zoom
OSM\TargetLocation\Latitude = latitude OSM\TargetLocation\Latitude = latitude
OSM\TargetLocation\Longitude = longitude OSM\TargetLocation\Longitude = longitude
LatLon2XY(@OSM\TargetLocation, @OSM\TargetTile) LatLon2XY(@OSM\TargetLocation, @OSM\TargetTile)
OSM\Position\x = OSM\TargetTile\x * OSM\TileSize ;Convert X, Y in tile.decimal into real pixels ;*** Creates a drawing thread and fill parameters
OSM\Position\y = OSM\TargetTile\y * OSM\TileSize *Drawing = AllocateMemory(SizeOf(DrawingParameters))
OSM\DeltaX = OSM\Position\x - Int(OSM\Position\x / OSM\TileSize) * OSM\TileSize ;Convert X, Y in tile.decimal into real pixels
OSM\DeltaY = OSM\Position\y - Int(OSM\Position\y / OSM\TileSize) * OSM\TileSize *Drawing\x = OSM\TargetTile\x ;* OSM\TileSize
CreateThread(@DrawingThread(), Null) *Drawing\y = OSM\TargetTile\y ;* OSM\TileSize
;Position in the tile
*Drawing\DeltaX = *Drawing\x - Int(*Drawing\x) * OSM\TileSize
*Drawing\DeltaY = *Drawing\y - Int(*Drawing\y) * OSM\TileSize
OSM\EmergencyQuit = #False
*Drawing\PassNb = 1
CreateThread(@DrawingThread(), *Drawing)
;***
EndProcedure EndProcedure
Procedure SetZoom(Zoom.i, mode.i = #PB_Relative) Procedure SetZoom(Zoom.i, mode.i = #PB_Relative)
Protected *Drawing.DrawingParameters
Select mode Select mode
Case #PB_Relative Case #PB_Relative
OSM\Zoom = OSM\Zoom + zoom OSM\Zoom = OSM\Zoom + zoom
@@ -682,11 +694,18 @@ Module OSM
If OSM\Zoom > OSM\ZoomMax : OSM\Zoom = OSM\ZoomMax : EndIf If OSM\Zoom > OSM\ZoomMax : OSM\Zoom = OSM\ZoomMax : EndIf
If OSM\Zoom < OSM\ZoomMin : OSM\Zoom = OSM\ZoomMin : EndIf If OSM\Zoom < OSM\ZoomMin : OSM\Zoom = OSM\ZoomMin : EndIf
LatLon2XY(@OSM\TargetLocation, @OSM\TargetTile) LatLon2XY(@OSM\TargetLocation, @OSM\TargetTile)
OSM\Position\x = OSM\TargetTile\x * OSM\TileSize ;Convert X, Y in tile.decimal into real pixels ;*** Creates a drawing thread and fill parameters
OSM\Position\y = OSM\TargetTile\y * OSM\TileSize *Drawing = AllocateMemory(SizeOf(DrawingParameters))
OSM\DeltaX = OSM\Position\x - Int(OSM\Position\x / OSM\TileSize) * OSM\TileSize ;Convert X, Y in tile.decimal into real pixels
OSM\DeltaY = OSM\Position\y - Int(OSM\Position\y / OSM\TileSize) * OSM\TileSize *Drawing\x = OSM\TargetTile\x ;* OSM\TileSize
CreateThread(@DrawingThread(), Null) *Drawing\y = OSM\TargetTile\y ;* OSM\TileSize
;Position in the tile
*Drawing\DeltaX = *Drawing\x - Int(*Drawing\x) * OSM\TileSize
*Drawing\DeltaY = *Drawing\y - Int(*Drawing\y) * OSM\TileSize
OSM\EmergencyQuit = #False
*Drawing\PassNb = 1
CreateThread(@DrawingThread(), *Drawing)
;***
EndProcedure EndProcedure
@@ -699,7 +718,6 @@ Module OSM
Protected Gadget.i Protected Gadget.i
Protected MouseX.i, MouseY.i Protected MouseX.i, MouseY.i
Protected OldX.i, OldY.i Protected OldX.i, OldY.i
Protected TileX.d, TileY.d
Protected *Drawing.DrawingParameters Protected *Drawing.DrawingParameters
If IsGadget(OSM\Gadget) And GadgetType(OSM\Gadget) = #PB_GadgetType_Canvas If IsGadget(OSM\Gadget) And GadgetType(OSM\Gadget) = #PB_GadgetType_Canvas
@@ -725,25 +743,27 @@ Module OSM
;New move values ;New move values
OSM\Position\x - MouseX OSM\Position\x - MouseX
OSM\Position\y - MouseY OSM\Position\y - MouseY
;-*** Creates a drawing thread and fill parameters
*Drawing = AllocateMemory(SizeOf(DrawingParameters))
;OSM tile position in tile.decimal ;OSM tile position in tile.decimal
TileX = OSM\Position\x / OSM\TileSize *Drawing\x = OSM\Position\x / OSM\TileSize
TileY = OSM\Position\y / OSM\TileSize *Drawing\y = OSM\Position\y / OSM\TileSize
*Drawing = AllocateMemory(SizeOf(*DrawingParameters))
;Pixel shift ;Pixel shift
*Drawing\DeltaX = OSM\Position\x - Int(TileX) * OSM\TileSize *Drawing\DeltaX = OSM\Position\x - Int(*Drawing\x) * OSM\TileSize
*Drawing\DeltaY = OSM\Position\y - Int(TileY) * OSM\TileSize *Drawing\DeltaY = OSM\Position\y - Int(*Drawing\y) * OSM\TileSize
;Moved to a new tile ? ;Moved to a new tile ?
If (Int(OSM\Position\x / OSM\TileSize)) <> (Int(OldX / OSM\TileSize)) Or (Int(OSM\Position\y / OSM\TileSize)) <> (Int(OldY / OSM\TileSize)) ;If (Int(OSM\Position\x / OSM\TileSize)) <> (Int(OldX / OSM\TileSize)) Or (Int(OSM\Position\y / OSM\TileSize)) <> (Int(OldY / OSM\TileSize))
Debug "--- New tile" ;Debug "--- New tile"
*Drawing\x = TileX ;*Drawing\x = TileX
*Drawing\y = TileY ;*Drawing\y = TileY
Debug "OSM\Position\x " + Str(OSM\Position\x) + " ; OSM\Position\y " + Str(OSM\Position\y) Debug "OSM\Position\x " + Str(OSM\Position\x) + " ; OSM\Position\y " + Str(OSM\Position\y)
XY2LatLon(*Drawing, @OSM\TargetLocation) XY2LatLon(*Drawing, @OSM\TargetLocation)
Debug "OSM\TargetTile\x " + StrD(*Drawing\x) + " ; OSM\TargetTile\y " + StrD(*Drawing\y) Debug "OSM\TargetTile\x " + StrD(*Drawing\x) + " ; OSM\TargetTile\y " + StrD(*Drawing\y)
EndIf ;EndIf
OSM\EmergencyQuit = #False OSM\EmergencyQuit = #False
*Drawing\PassNb = 1 *Drawing\PassNb = 1
CreateThread(@DrawingThread(), *Drawing) CreateThread(@DrawingThread(), *Drawing)
;- ***
OSM\MoveStartingPoint\x = GetGadgetAttribute(OSM\Gadget, #PB_Canvas_MouseX) OSM\MoveStartingPoint\x = GetGadgetAttribute(OSM\Gadget, #PB_Canvas_MouseX)
OSM\MoveStartingPoint\y = GetGadgetAttribute(OSM\Gadget, #PB_Canvas_MouseY) OSM\MoveStartingPoint\y = GetGadgetAttribute(OSM\Gadget, #PB_Canvas_MouseY)
;If CallBackLocation send Location to function ;If CallBackLocation send Location to function
@@ -855,8 +875,9 @@ CompilerIf #PB_Compiler_IsMainFile
CompilerEndIf CompilerEndIf
; IDE Options = PureBasic 5.42 LTS (Windows - x64) ; IDE Options = PureBasic 5.42 LTS (Windows - x64)
; CursorPosition = 831 ; CursorPosition = 750
; FirstLine = 803 ; FirstLine = 724
; Folding = ----- ; Folding = -----
; EnableUnicode ; EnableUnicode
; EnableThread
; EnableXP ; EnableXP