From 035b26d9dbf30d754d476dd6dfbe0faf06bd6a7b Mon Sep 17 00:00:00 2001 From: djes Date: Tue, 23 Aug 2016 09:48:35 +0200 Subject: [PATCH 01/49] Cache cleaning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit premier test de nettoyage du cache mémoire pour éviter que l'application grignote de plus en plus de mémoire --- PBMap.pb | 215 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 150 insertions(+), 65 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 5795e5f..52ef232 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -26,7 +26,6 @@ UsePNGImageEncoder() DeclareModule PBMap ;-Show debug infos Global Verbose = #False - ;-Proxy ON/OFF Global Proxy = #False Declare InitPBMap() Declare SetMapServer(ServerURL.s="http://tile.openstreetmap.org/",TileSize.l=256,ZoomMin.l=0,ZoomMax.l=18) @@ -102,7 +101,7 @@ Module PBMap Structure ImgMemCach nImage.i - Usage.i + Location.Location EndStructure Structure TileMemCach @@ -115,37 +114,43 @@ Module PBMap CallBackPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) EndStructure + Structure Option + WheelMouseRelative.i + EndStructure + ;-PBMap Structure Structure PBMap Gadget.i ; Canvas Gadget Id Font.i ; Font to uses when write on the map TargetLocation.Location ; Latitude and Longitude from focus point Drawing.DrawingParameters ; Drawing parameters based on focus point - ; + ; CallBackLocation.i ; @Procedure(latitude.d,lontitude.d) CallBackMainPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) - ; + ; Position.PixelPosition ; Actual focus point coords in pixels (global) MoveStartingPoint.PixelPosition ; Start mouse position coords when dragging the map - ; + ; ServerURL.s ; Web URL ex: http://tile.openstreetmap.org/ ZoomMin.i ; Min Zoom supported by server ZoomMax.i ; Max Zoom supported by server Zoom.i ; Current zoom TileSize.i ; Tile size downloaded on the server ex : 256 - ; + ; HDDCachePath.S ; Path where to load and save tiles downloaded from server MemCache.TileMemCach ; Images in memory cache - ; - Moving.i + ; + Moving.i ; Dirty.i ; To signal that drawing need a refresh - ; - MainDrawingThread.i - List TilesThreads.TileThread() - ; + ; + MainDrawingThread.i ; + List TilesThreads.TileThread() ; + ; List track.Location() ; To display a GPX track List Marker.Marker() ; To diplay marker - EditMarkerIndex.l + EditMarkerIndex.l ; + ; + Options.option ; EndStructure Global PBMap.PBMap, Null.i @@ -163,7 +168,7 @@ Module PBMap EndIf EndProcedure - ;- *** CURL specific *** + ;- *** CURL specific ; (program has To be compiled in console format for curl debug infos) IncludeFile "libcurl.pbi" ; https://github.com/deseven/pbsamples/tree/master/crossplatform/libcurl @@ -239,30 +244,33 @@ Module PBMap PBMap\Drawing\Semaphore = CreateSemaphore() PBMap\EditMarkerIndex = -1 ;<- You must initialize with No Marker selected PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold) - ;- Proxy details + ;-Options + PBMap\Options\WheelMouseRelative = #True + ;-Preferences ;Use this to create and customize your preferences file for the first time - ; Result = CreatePreferences(GetHomeDirectory() + "PBMap.prefs") - ; If Proxy - ; PreferenceGroup("PROXY") - ; WritePreferenceString("ProxyURL", "myproxy.fr") - ; WritePreferenceString("ProxyPort", "myproxyport") - ; WritePreferenceString("ProxyUser", "myproxyname") - ; EndIf - ; If Result - ; ClosePreferences() - ; EndIf - Result = OpenPreferences(GetHomeDirectory() + "PBMap.prefs") + ; CreatePreferences(GetHomeDirectory() + "PBMap.prefs") + ; ;Or this to modify + ; ;OpenPreferences(GetHomeDirectory() + "PBMap.prefs") + ; ;Or this + ; ;RunProgram("notepad.exe", GetHomeDirectory() + "PBMap.prefs", GetHomeDirectory()) + ; PreferenceGroup("PROXY") + ; WritePreferenceInteger("Proxy", #True) + ; WritePreferenceString("ProxyURL", "myproxy.fr") + ; WritePreferenceString("ProxyPort", "myproxyport") + ; WritePreferenceString("ProxyUser", "myproxyname") + ; WritePreferenceString("ProxyPass", "myproxypass") ;TODO !Warning! !not encoded! + ; ClosePreferences() + OpenPreferences(GetHomeDirectory() + "PBMap.prefs") + PreferenceGroup("PROXY") + Proxy = ReadPreferenceInteger("Proxy", #False) If Proxy - PreferenceGroup("PROXY") Global ProxyURL$ = ReadPreferenceString("ProxyURL", "") ;InputRequester("ProxyServer", "Do you use a Proxy Server? Then enter the full url:", "") Global ProxyPort$ = ReadPreferenceString("ProxyPort", "") ;InputRequester("ProxyPort" , "Do you use a specific port? Then enter it", "") Global ProxyUser$ = ReadPreferenceString("ProxyUser", "") ;InputRequester("ProxyUser" , "Do you use a user name? Then enter it", "") - Global ProxyPassword$ = InputRequester("ProxyPass", "Do you use a password ? Then enter it", "") + Global ProxyPassword$ = InputRequester("ProxyPass", "Do you use a password ? Then enter it", "") ;TODO EndIf - If Result - ClosePreferences() - EndIf - curl_global_init(#CURL_GLOBAL_WIN32); + ClosePreferences() + curl_global_init(#CURL_GLOBAL_WIN32) ;- Main drawing thread launching PBMap\MainDrawingThread = CreateThread(@DrawingThread(), @PBMap\Drawing) If PBMap\MainDrawingThread = 0 @@ -310,10 +318,10 @@ Module PBMap Procedure MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) If Gadget = #PB_Any - PBMap\Gadget = CanvasGadget(PBMap\Gadget, X, Y, Width, Height) + PBMap\Gadget = CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) ;#PB_Canvas_Keyboard has to be set for mousewheel to work on windows Else PBMap\Gadget = Gadget - CanvasGadget(PBMap\Gadget, X, Y, Width, Height) + CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) EndIf EndProcedure @@ -371,7 +379,7 @@ Module PBMap x2 = (PBMap\TargetLocation\Longitude+180)*(mapWidth/360) ; convert from degrees To radians latRad = PBMap\TargetLocation\Latitude*#PI/180; - ; get y value + ; get y value mercN = Log(Tan((#PI/4)+(latRad/2))) y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)); *Pixel\x=GadgetWidth(PBMap\Gadget)/2 - (x2-x1) @@ -470,6 +478,7 @@ Module PBMap Protected nImage.i = -1 Protected key.s = "Z" + RSet(Str(*Tile\PBMapZoom), 4, "0") + "X" + RSet(Str(*Tile\PBMapTileX), 8, "0") + "Y" + RSet(Str(*Tile\PBMapTileY), 8, "0") Protected CacheFile.s = PBMap\HDDCachePath + "PBMap_" + Str(*Tile\PBMapZoom) + "_" + Str(*Tile\PBMapTileX) + "_" + Str(*Tile\PBMapTileY) + ".png" + Protected Tile.position ;Adding the image to the cache if possible AddMapElement(PBMap\MemCache\Images(), key) nImage = GetTileFromHDD(CacheFile) @@ -478,6 +487,9 @@ Module PBMap EndIf If nImage <> -1 PBMap\MemCache\Images(key)\nImage = nImage + Tile\x=*Tile\PBMapTileX + Tile\y=*Tile\PBMapTiley + XY2LatLon(@Tile,@PBMap\MemCache\Images(key)\Location) MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") MyDebug("With the following key : " + key) Else @@ -571,20 +583,39 @@ Module PBMap DeleteElement(PBMap\TilesThreads()) EndIf Next + ;-****Clean Mem Cache + ForEach PBMap\MemCache\Images() + ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize + Protected MaxNbTile.l + If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) + MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize + Else + MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize + EndIf + Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) + Protected Limit.d=Scale*(MaxNbTile)*1.5 + Protected Distance.d=HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) + Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) + If Distance>Limit + Debug "delete" + DeleteMapElement(PBMap\MemCache\Images()) + EndIf + Next + EndProcedure - Procedure Pointer(x.i, y.i, color.l = #Red) - color=RGBA(255, 0, 0, 255) + Procedure Pointer(x.i, y.i, color.l = 0) VectorSourceColor(color) MovePathCursor(x, y) - AddPathLine(-8,-16,#PB_Path_Relative) - AddPathCircle(8,0,8,180,0,#PB_Path_Relative) - AddPathLine(-8,16,#PB_Path_Relative) + AddPathLine(-8, -16, #PB_Path_Relative) + AddPathCircle(8, 0, 8, 180, 0, #PB_Path_Relative) + AddPathLine(-8, 16, #PB_Path_Relative) ;FillPath(#PB_Path_Preserve) ;ClipPath(#PB_Path_Preserve) - AddPathCircle(0,-16,5,0,360,#PB_Path_Relative) + AddPathCircle(0, -16, 5, 0, 360, #PB_Path_Relative) VectorSourceColor(color) - FillPath(#PB_Path_Preserve):VectorSourceColor(RGBA(0, 0, 0, 255)):StrokePath(1) + FillPath(#PB_Path_Preserve):VectorSourceColor(color);RGBA(0, 0, 0, 255)) + StrokePath(1) EndProcedure Procedure TrackPointer(x.i, y.i,dist.l) @@ -627,7 +658,7 @@ Module PBMap StrokePath(10, #PB_Path_RoundEnd|#PB_Path_RoundCorner) ;Draw Distance ForEach PBMap\track() - ;-Test Distance + ;Distance test If ListIndex(PBMap\track())=0 Location\Latitude=PBMap\track()\Latitude Location\Longitude=PBMap\track()\Longitude @@ -707,8 +738,20 @@ Module PBMap If PBMap\CallBackMainPointer > 0 CallFunctionFast(PBMap\CallBackMainPointer, Drawing\CenterX, Drawing\CenterY) Else - Pointer(Drawing\CenterX, Drawing\CenterY, #Red) + Pointer(Drawing\CenterX, Drawing\CenterY, RGBA($FF, 0, 0, $FF)) EndIf + ;TODO Add Option and function to display Scale on Map + ;Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) + ;VectorFont(FontID(PBMap\Font), 30) + ;VectorSourceColor(RGBA(0, 0, 0, 80)) + ;MovePathCursor(50,50) + ;DrawVectorText(StrD(Scale)) + + ;- Display How Many Image in Cache + VectorFont(FontID(PBMap\Font), 30) + VectorSourceColor(RGBA(0, 0, 0, 80)) + MovePathCursor(50,50) + DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) StopVectorDrawing() ;Redraw ; If something was not correctly drawn, redraw after a while @@ -818,7 +861,43 @@ Module PBMap If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) EndIf - EndProcedure + 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 + ;Fast and dirty code + OldPx = PBMap\Position\x : OldPy = PBMap\Position\y + OldMx = OldPx + GadgetWidth(PBMap\Gadget) / 2 - x + OldMy = OldPy + GadgetHeight(PBMap\Gadget) / 2 - y + PBMap\Zoom = PBMap\Zoom + zoom + If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf + If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf + ;Centered Zoom + LockMutex(PBMap\Drawing\Mutex) + LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing) + ;Convert X, Y in tile.decimal into real pixels + PBMap\Position\x = PBMap\Drawing\Position\x * PBMap\TileSize + PBMap\Position\y = PBMap\Drawing\Position\y * PBMap\TileSize + MouseX = PBMap\Position\x + GadgetWidth(PBMap\Gadget) / 2 - x + MouseY = PBMap\Position\y + GadgetHeight(PBMap\Gadget) / 2 - y + ;Cross-multiply to get the new center + PBMap\Position\x = (OldPx * MouseX) / OldMx + PBMap\Position\y = (OldPy * MouseY) / OldMy + ;PBMap tile position in tile.decimal + PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize + PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize + PBMap\Drawing\PassNb = 1 + XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) + UnlockMutex(PBMap\Drawing\Mutex) + ;Start drawing + SignalSemaphore(PBMap\Drawing\Semaphore) + ;If CallBackLocation send Location to function + If PBMap\CallBackLocation > 0 + CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) + EndIf + EndProcedure Procedure SetCallBackLocation(CallBackLocation.i) PBMap\CallBackLocation = CallBackLocation @@ -852,12 +931,11 @@ Module PBMap ProcedureReturn Value EndProcedure - + Procedure Event(Event.l) Protected Gadget.i Protected MouseX.i, MouseY.i Protected Marker.Position - Protected *Drawing.DrawingParameters If IsGadget(PBMap\Gadget) And GadgetType(PBMap\Gadget) = #PB_GadgetType_Canvas Select Event Case #PB_Event_Gadget ;{ @@ -866,7 +944,13 @@ Module PBMap Case PBMap\Gadget Select EventType() Case #PB_EventType_MouseWheel - SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta),#PB_Relative) + 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)) + Else + ;Absolute zoom (centered on the center of the map) + SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta), #PB_Relative) + EndIf Case #PB_EventType_LeftButtonDown ;Check if we select a marker MouseX = PBMap\Position\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) @@ -884,11 +968,12 @@ Module PBMap PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) Case #PB_EventType_MouseMove + ;If a move has been initiated by a left click 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 PBMap\Moving = #True - ;move Marker + ;If it's marker move If PBMap\EditMarkerIndex > -1 SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) LatLon2XY(@PBMap\Marker()\Location, @Marker) @@ -899,38 +984,39 @@ Module PBMap ;New move values PBMap\Position\x - MouseX PBMap\Position\y - MouseY - ;-*** Fill parameters and signal the drawing thread + ;Fill parameters and signal the drawing thread LockMutex(PBMap\Drawing\Mutex) ;PBMap tile position in tile.decimal PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize PBMap\Drawing\PassNb = 1 XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) - ;If CallBackLocation send Location to function - If PBMap\CallBackLocation > 0 - CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) - EndIf UnlockMutex(PBMap\Drawing\Mutex) EndIf ;Start drawing SignalSemaphore(PBMap\Drawing\Semaphore) - ;- *** + ;If CallBackLocation send Location to function + If PBMap\CallBackLocation > 0 + CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) + EndIf PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) EndIf Case #PB_EventType_LeftButtonUp PBMap\Moving = #False PBMap\MoveStartingPoint\x = - 1 + ;Stop marker move If PBMap\EditMarkerIndex > -1 PBMap\EditMarkerIndex = -1 - Else ;Move Map + Else + ;Stop map move LockMutex(PBMap\Drawing\Mutex) PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize MyDebug("PBMap\Drawing\Position\x " + Str(PBMap\Drawing\Position\x) + " ; PBMap\Drawing\Position\y " + Str(PBMap\Drawing\Position\y) ) XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) UnlockMutex(PBMap\Drawing\Mutex) - EndIf + EndIf EndSelect EndSelect EndSelect @@ -1019,7 +1105,7 @@ CompilerIf #PB_Compiler_IsMainFile LoadFont(0, "Wingdings", 12) LoadFont(1, "Arial", 12, #PB_Font_Bold) - + TextGadget(#Text_1, 530, 50, 60, 15, "Movements") ButtonGadget(#Gdt_Left, 550, 100, 30, 30, Chr($E7)) : SetGadgetFont(#Gdt_Left, FontID(0)) ButtonGadget(#Gdt_Right, 610, 100, 30, 30, Chr($E8)) : SetGadgetFont(#Gdt_Right, FontID(0)) @@ -1067,10 +1153,10 @@ CompilerIf #PB_Compiler_IsMainFile Case #Button_5 PBMap::SetZoom( - 1) Case #Gdt_LoadGpx - PBMap::LoadGpxFile(OpenFileRequester("Choisissez un fichier à charger", "", "*.gpx", 0)) - PBMap::ZoomToArea() ; <-To center the view, and to viex all the track + PBMap::LoadGpxFile(OpenFileRequester("Choose a file to load", "", "*.gpx", 0)) + PBMap::ZoomToArea() ; <-To center the view, and zoom on the tracks Case #Gdt_AddMarker - PBMap:: AddMarker(ValD(GetGadgetText(#String_0)),ValD(GetGadgetText(#String_1)),RGBA(Random(255),Random(255),Random(255),255)) + PBMap:: AddMarker(ValD(GetGadgetText(#String_0)), ValD(GetGadgetText(#String_1)), RGBA(Random(255), Random(255), Random(255),255)) EndSelect Case #PB_Event_SizeWindow ResizeAll() @@ -1080,10 +1166,9 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::Quit() EndIf CompilerEndIf - -; IDE Options = PureBasic 5.50 (Windows - x86) -; CursorPosition = 994 -; FirstLine = 969 +; IDE Options = PureBasic 5.50 (Windows - x64) +; CursorPosition = 1167 +; FirstLine = 1123 ; Folding = --------- ; EnableThread ; EnableXP From 4d3c3ffffcc71ea875aa32e40ee1203af503da62 Mon Sep 17 00:00:00 2001 From: djes Date: Tue, 23 Aug 2016 09:51:27 +0200 Subject: [PATCH 02/49] Revert "Cache cleaning" This reverts commit 035b26d9dbf30d754d476dd6dfbe0faf06bd6a7b. --- PBMap.pb | 215 +++++++++++++++++-------------------------------------- 1 file changed, 65 insertions(+), 150 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 52ef232..5795e5f 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -26,6 +26,7 @@ UsePNGImageEncoder() DeclareModule PBMap ;-Show debug infos Global Verbose = #False + ;-Proxy ON/OFF Global Proxy = #False Declare InitPBMap() Declare SetMapServer(ServerURL.s="http://tile.openstreetmap.org/",TileSize.l=256,ZoomMin.l=0,ZoomMax.l=18) @@ -101,7 +102,7 @@ Module PBMap Structure ImgMemCach nImage.i - Location.Location + Usage.i EndStructure Structure TileMemCach @@ -114,43 +115,37 @@ Module PBMap CallBackPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) EndStructure - Structure Option - WheelMouseRelative.i - EndStructure - ;-PBMap Structure Structure PBMap Gadget.i ; Canvas Gadget Id Font.i ; Font to uses when write on the map TargetLocation.Location ; Latitude and Longitude from focus point Drawing.DrawingParameters ; Drawing parameters based on focus point - ; + ; CallBackLocation.i ; @Procedure(latitude.d,lontitude.d) CallBackMainPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) - ; + ; Position.PixelPosition ; Actual focus point coords in pixels (global) MoveStartingPoint.PixelPosition ; Start mouse position coords when dragging the map - ; + ; ServerURL.s ; Web URL ex: http://tile.openstreetmap.org/ ZoomMin.i ; Min Zoom supported by server ZoomMax.i ; Max Zoom supported by server Zoom.i ; Current zoom TileSize.i ; Tile size downloaded on the server ex : 256 - ; + ; HDDCachePath.S ; Path where to load and save tiles downloaded from server MemCache.TileMemCach ; Images in memory cache - ; - Moving.i ; + ; + Moving.i Dirty.i ; To signal that drawing need a refresh - ; - MainDrawingThread.i ; - List TilesThreads.TileThread() ; - ; + ; + MainDrawingThread.i + List TilesThreads.TileThread() + ; List track.Location() ; To display a GPX track List Marker.Marker() ; To diplay marker - EditMarkerIndex.l ; - ; - Options.option ; + EditMarkerIndex.l EndStructure Global PBMap.PBMap, Null.i @@ -168,7 +163,7 @@ Module PBMap EndIf EndProcedure - ;- *** CURL specific + ;- *** CURL specific *** ; (program has To be compiled in console format for curl debug infos) IncludeFile "libcurl.pbi" ; https://github.com/deseven/pbsamples/tree/master/crossplatform/libcurl @@ -244,33 +239,30 @@ Module PBMap PBMap\Drawing\Semaphore = CreateSemaphore() PBMap\EditMarkerIndex = -1 ;<- You must initialize with No Marker selected PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold) - ;-Options - PBMap\Options\WheelMouseRelative = #True - ;-Preferences + ;- Proxy details ;Use this to create and customize your preferences file for the first time - ; CreatePreferences(GetHomeDirectory() + "PBMap.prefs") - ; ;Or this to modify - ; ;OpenPreferences(GetHomeDirectory() + "PBMap.prefs") - ; ;Or this - ; ;RunProgram("notepad.exe", GetHomeDirectory() + "PBMap.prefs", GetHomeDirectory()) - ; PreferenceGroup("PROXY") - ; WritePreferenceInteger("Proxy", #True) - ; WritePreferenceString("ProxyURL", "myproxy.fr") - ; WritePreferenceString("ProxyPort", "myproxyport") - ; WritePreferenceString("ProxyUser", "myproxyname") - ; WritePreferenceString("ProxyPass", "myproxypass") ;TODO !Warning! !not encoded! - ; ClosePreferences() - OpenPreferences(GetHomeDirectory() + "PBMap.prefs") - PreferenceGroup("PROXY") - Proxy = ReadPreferenceInteger("Proxy", #False) + ; Result = CreatePreferences(GetHomeDirectory() + "PBMap.prefs") + ; If Proxy + ; PreferenceGroup("PROXY") + ; WritePreferenceString("ProxyURL", "myproxy.fr") + ; WritePreferenceString("ProxyPort", "myproxyport") + ; WritePreferenceString("ProxyUser", "myproxyname") + ; EndIf + ; If Result + ; ClosePreferences() + ; EndIf + Result = OpenPreferences(GetHomeDirectory() + "PBMap.prefs") If Proxy + PreferenceGroup("PROXY") Global ProxyURL$ = ReadPreferenceString("ProxyURL", "") ;InputRequester("ProxyServer", "Do you use a Proxy Server? Then enter the full url:", "") Global ProxyPort$ = ReadPreferenceString("ProxyPort", "") ;InputRequester("ProxyPort" , "Do you use a specific port? Then enter it", "") Global ProxyUser$ = ReadPreferenceString("ProxyUser", "") ;InputRequester("ProxyUser" , "Do you use a user name? Then enter it", "") - Global ProxyPassword$ = InputRequester("ProxyPass", "Do you use a password ? Then enter it", "") ;TODO + Global ProxyPassword$ = InputRequester("ProxyPass", "Do you use a password ? Then enter it", "") EndIf - ClosePreferences() - curl_global_init(#CURL_GLOBAL_WIN32) + If Result + ClosePreferences() + EndIf + curl_global_init(#CURL_GLOBAL_WIN32); ;- Main drawing thread launching PBMap\MainDrawingThread = CreateThread(@DrawingThread(), @PBMap\Drawing) If PBMap\MainDrawingThread = 0 @@ -318,10 +310,10 @@ Module PBMap Procedure MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) If Gadget = #PB_Any - PBMap\Gadget = CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) ;#PB_Canvas_Keyboard has to be set for mousewheel to work on windows + PBMap\Gadget = CanvasGadget(PBMap\Gadget, X, Y, Width, Height) Else PBMap\Gadget = Gadget - CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) + CanvasGadget(PBMap\Gadget, X, Y, Width, Height) EndIf EndProcedure @@ -379,7 +371,7 @@ Module PBMap x2 = (PBMap\TargetLocation\Longitude+180)*(mapWidth/360) ; convert from degrees To radians latRad = PBMap\TargetLocation\Latitude*#PI/180; - ; get y value + ; get y value mercN = Log(Tan((#PI/4)+(latRad/2))) y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)); *Pixel\x=GadgetWidth(PBMap\Gadget)/2 - (x2-x1) @@ -478,7 +470,6 @@ Module PBMap Protected nImage.i = -1 Protected key.s = "Z" + RSet(Str(*Tile\PBMapZoom), 4, "0") + "X" + RSet(Str(*Tile\PBMapTileX), 8, "0") + "Y" + RSet(Str(*Tile\PBMapTileY), 8, "0") Protected CacheFile.s = PBMap\HDDCachePath + "PBMap_" + Str(*Tile\PBMapZoom) + "_" + Str(*Tile\PBMapTileX) + "_" + Str(*Tile\PBMapTileY) + ".png" - Protected Tile.position ;Adding the image to the cache if possible AddMapElement(PBMap\MemCache\Images(), key) nImage = GetTileFromHDD(CacheFile) @@ -487,9 +478,6 @@ Module PBMap EndIf If nImage <> -1 PBMap\MemCache\Images(key)\nImage = nImage - Tile\x=*Tile\PBMapTileX - Tile\y=*Tile\PBMapTiley - XY2LatLon(@Tile,@PBMap\MemCache\Images(key)\Location) MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") MyDebug("With the following key : " + key) Else @@ -583,39 +571,20 @@ Module PBMap DeleteElement(PBMap\TilesThreads()) EndIf Next - ;-****Clean Mem Cache - ForEach PBMap\MemCache\Images() - ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize - Protected MaxNbTile.l - If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) - MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize - Else - MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize - EndIf - Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) - Protected Limit.d=Scale*(MaxNbTile)*1.5 - Protected Distance.d=HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) - Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) - If Distance>Limit - Debug "delete" - DeleteMapElement(PBMap\MemCache\Images()) - EndIf - Next - EndProcedure - Procedure Pointer(x.i, y.i, color.l = 0) + Procedure Pointer(x.i, y.i, color.l = #Red) + color=RGBA(255, 0, 0, 255) VectorSourceColor(color) MovePathCursor(x, y) - AddPathLine(-8, -16, #PB_Path_Relative) - AddPathCircle(8, 0, 8, 180, 0, #PB_Path_Relative) - AddPathLine(-8, 16, #PB_Path_Relative) + AddPathLine(-8,-16,#PB_Path_Relative) + AddPathCircle(8,0,8,180,0,#PB_Path_Relative) + AddPathLine(-8,16,#PB_Path_Relative) ;FillPath(#PB_Path_Preserve) ;ClipPath(#PB_Path_Preserve) - AddPathCircle(0, -16, 5, 0, 360, #PB_Path_Relative) + AddPathCircle(0,-16,5,0,360,#PB_Path_Relative) VectorSourceColor(color) - FillPath(#PB_Path_Preserve):VectorSourceColor(color);RGBA(0, 0, 0, 255)) - StrokePath(1) + FillPath(#PB_Path_Preserve):VectorSourceColor(RGBA(0, 0, 0, 255)):StrokePath(1) EndProcedure Procedure TrackPointer(x.i, y.i,dist.l) @@ -658,7 +627,7 @@ Module PBMap StrokePath(10, #PB_Path_RoundEnd|#PB_Path_RoundCorner) ;Draw Distance ForEach PBMap\track() - ;Distance test + ;-Test Distance If ListIndex(PBMap\track())=0 Location\Latitude=PBMap\track()\Latitude Location\Longitude=PBMap\track()\Longitude @@ -738,20 +707,8 @@ Module PBMap If PBMap\CallBackMainPointer > 0 CallFunctionFast(PBMap\CallBackMainPointer, Drawing\CenterX, Drawing\CenterY) Else - Pointer(Drawing\CenterX, Drawing\CenterY, RGBA($FF, 0, 0, $FF)) + Pointer(Drawing\CenterX, Drawing\CenterY, #Red) EndIf - ;TODO Add Option and function to display Scale on Map - ;Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) - ;VectorFont(FontID(PBMap\Font), 30) - ;VectorSourceColor(RGBA(0, 0, 0, 80)) - ;MovePathCursor(50,50) - ;DrawVectorText(StrD(Scale)) - - ;- Display How Many Image in Cache - VectorFont(FontID(PBMap\Font), 30) - VectorSourceColor(RGBA(0, 0, 0, 80)) - MovePathCursor(50,50) - DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) StopVectorDrawing() ;Redraw ; If something was not correctly drawn, redraw after a while @@ -861,43 +818,7 @@ Module PBMap If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) EndIf - 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 - ;Fast and dirty code - OldPx = PBMap\Position\x : OldPy = PBMap\Position\y - OldMx = OldPx + GadgetWidth(PBMap\Gadget) / 2 - x - OldMy = OldPy + GadgetHeight(PBMap\Gadget) / 2 - y - PBMap\Zoom = PBMap\Zoom + zoom - If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf - If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf - ;Centered Zoom - LockMutex(PBMap\Drawing\Mutex) - LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing) - ;Convert X, Y in tile.decimal into real pixels - PBMap\Position\x = PBMap\Drawing\Position\x * PBMap\TileSize - PBMap\Position\y = PBMap\Drawing\Position\y * PBMap\TileSize - MouseX = PBMap\Position\x + GadgetWidth(PBMap\Gadget) / 2 - x - MouseY = PBMap\Position\y + GadgetHeight(PBMap\Gadget) / 2 - y - ;Cross-multiply to get the new center - PBMap\Position\x = (OldPx * MouseX) / OldMx - PBMap\Position\y = (OldPy * MouseY) / OldMy - ;PBMap tile position in tile.decimal - PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize - PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize - PBMap\Drawing\PassNb = 1 - XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) - UnlockMutex(PBMap\Drawing\Mutex) - ;Start drawing - SignalSemaphore(PBMap\Drawing\Semaphore) - ;If CallBackLocation send Location to function - If PBMap\CallBackLocation > 0 - CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) - EndIf - EndProcedure + EndProcedure Procedure SetCallBackLocation(CallBackLocation.i) PBMap\CallBackLocation = CallBackLocation @@ -931,11 +852,12 @@ Module PBMap ProcedureReturn Value EndProcedure - + Procedure Event(Event.l) Protected Gadget.i Protected MouseX.i, MouseY.i Protected Marker.Position + Protected *Drawing.DrawingParameters If IsGadget(PBMap\Gadget) And GadgetType(PBMap\Gadget) = #PB_GadgetType_Canvas Select Event Case #PB_Event_Gadget ;{ @@ -944,13 +866,7 @@ Module PBMap Case PBMap\Gadget Select EventType() 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)) - Else - ;Absolute zoom (centered on the center of the map) - SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta), #PB_Relative) - EndIf + SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta),#PB_Relative) Case #PB_EventType_LeftButtonDown ;Check if we select a marker MouseX = PBMap\Position\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) @@ -968,12 +884,11 @@ Module PBMap PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) Case #PB_EventType_MouseMove - ;If a move has been initiated by a left click 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 PBMap\Moving = #True - ;If it's marker move + ;move Marker If PBMap\EditMarkerIndex > -1 SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) LatLon2XY(@PBMap\Marker()\Location, @Marker) @@ -984,39 +899,38 @@ Module PBMap ;New move values PBMap\Position\x - MouseX PBMap\Position\y - MouseY - ;Fill parameters and signal the drawing thread + ;-*** Fill parameters and signal the drawing thread LockMutex(PBMap\Drawing\Mutex) ;PBMap tile position in tile.decimal PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize PBMap\Drawing\PassNb = 1 XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) + ;If CallBackLocation send Location to function + If PBMap\CallBackLocation > 0 + CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) + EndIf UnlockMutex(PBMap\Drawing\Mutex) EndIf ;Start drawing SignalSemaphore(PBMap\Drawing\Semaphore) - ;If CallBackLocation send Location to function - If PBMap\CallBackLocation > 0 - CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) - EndIf + ;- *** PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) EndIf Case #PB_EventType_LeftButtonUp PBMap\Moving = #False PBMap\MoveStartingPoint\x = - 1 - ;Stop marker move If PBMap\EditMarkerIndex > -1 PBMap\EditMarkerIndex = -1 - Else - ;Stop map move + Else ;Move Map LockMutex(PBMap\Drawing\Mutex) PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize MyDebug("PBMap\Drawing\Position\x " + Str(PBMap\Drawing\Position\x) + " ; PBMap\Drawing\Position\y " + Str(PBMap\Drawing\Position\y) ) XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) UnlockMutex(PBMap\Drawing\Mutex) - EndIf + EndIf EndSelect EndSelect EndSelect @@ -1105,7 +1019,7 @@ CompilerIf #PB_Compiler_IsMainFile LoadFont(0, "Wingdings", 12) LoadFont(1, "Arial", 12, #PB_Font_Bold) - + TextGadget(#Text_1, 530, 50, 60, 15, "Movements") ButtonGadget(#Gdt_Left, 550, 100, 30, 30, Chr($E7)) : SetGadgetFont(#Gdt_Left, FontID(0)) ButtonGadget(#Gdt_Right, 610, 100, 30, 30, Chr($E8)) : SetGadgetFont(#Gdt_Right, FontID(0)) @@ -1153,10 +1067,10 @@ CompilerIf #PB_Compiler_IsMainFile Case #Button_5 PBMap::SetZoom( - 1) Case #Gdt_LoadGpx - PBMap::LoadGpxFile(OpenFileRequester("Choose a file to load", "", "*.gpx", 0)) - PBMap::ZoomToArea() ; <-To center the view, and zoom on the tracks + PBMap::LoadGpxFile(OpenFileRequester("Choisissez un fichier à charger", "", "*.gpx", 0)) + PBMap::ZoomToArea() ; <-To center the view, and to viex all the track Case #Gdt_AddMarker - PBMap:: AddMarker(ValD(GetGadgetText(#String_0)), ValD(GetGadgetText(#String_1)), RGBA(Random(255), Random(255), Random(255),255)) + PBMap:: AddMarker(ValD(GetGadgetText(#String_0)),ValD(GetGadgetText(#String_1)),RGBA(Random(255),Random(255),Random(255),255)) EndSelect Case #PB_Event_SizeWindow ResizeAll() @@ -1166,9 +1080,10 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::Quit() EndIf CompilerEndIf -; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1167 -; FirstLine = 1123 + +; IDE Options = PureBasic 5.50 (Windows - x86) +; CursorPosition = 994 +; FirstLine = 969 ; Folding = --------- ; EnableThread ; EnableXP From 654a17fec66da8c82f181b423c366bcb63c5fd2d Mon Sep 17 00:00:00 2001 From: djes Date: Tue, 23 Aug 2016 09:52:26 +0200 Subject: [PATCH 03/49] Cache cleaning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit premier test de nettoyage du cache mémoire pour éviter que l'application grignote de plus en plus de mémoire --- PBMap.pb | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index f6f955a..9985f29 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -101,7 +101,7 @@ Module PBMap Structure ImgMemCach nImage.i - Usage.i + Location.Location EndStructure Structure TileMemCach @@ -478,6 +478,7 @@ Module PBMap Protected nImage.i = -1 Protected key.s = "Z" + RSet(Str(*Tile\PBMapZoom), 4, "0") + "X" + RSet(Str(*Tile\PBMapTileX), 8, "0") + "Y" + RSet(Str(*Tile\PBMapTileY), 8, "0") Protected CacheFile.s = PBMap\HDDCachePath + "PBMap_" + Str(*Tile\PBMapZoom) + "_" + Str(*Tile\PBMapTileX) + "_" + Str(*Tile\PBMapTileY) + ".png" + Protected Tile.position ;Adding the image to the cache if possible AddMapElement(PBMap\MemCache\Images(), key) nImage = GetTileFromHDD(CacheFile) @@ -486,6 +487,9 @@ Module PBMap EndIf If nImage <> -1 PBMap\MemCache\Images(key)\nImage = nImage + Tile\x=*Tile\PBMapTileX + Tile\y=*Tile\PBMapTiley + XY2LatLon(@Tile,@PBMap\MemCache\Images(key)\Location) MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") MyDebug("With the following key : " + key) Else @@ -579,6 +583,25 @@ Module PBMap DeleteElement(PBMap\TilesThreads()) EndIf Next + ;-****Clean Mem Cache + ForEach PBMap\MemCache\Images() + ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize + Protected MaxNbTile.l + If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) + MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize + Else + MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize + EndIf + Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) + Protected Limit.d=Scale*(MaxNbTile)*1.5 + Protected Distance.d=HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) + Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) + If Distance>Limit + Debug "delete" + DeleteMapElement(PBMap\MemCache\Images()) + EndIf + Next + EndProcedure Procedure Pointer(x.i, y.i, color.l = 0) @@ -717,6 +740,18 @@ Module PBMap Else Pointer(Drawing\CenterX, Drawing\CenterY, RGBA($FF, 0, 0, $FF)) EndIf + ;TODO Add Option and function to display Scale on Map + ;Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) + ;VectorFont(FontID(PBMap\Font), 30) + ;VectorSourceColor(RGBA(0, 0, 0, 80)) + ;MovePathCursor(50,50) + ;DrawVectorText(StrD(Scale)) + + ;- Display How Many Image in Cache + VectorFont(FontID(PBMap\Font), 30) + VectorSourceColor(RGBA(0, 0, 0, 80)) + MovePathCursor(50,50) + DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) StopVectorDrawing() ;Redraw ; If something was not correctly drawn, redraw after a while @@ -1131,11 +1166,10 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::Quit() EndIf CompilerEndIf - -; IDE Options = PureBasic 5.42 LTS (Windows - x86) -; CursorPosition = 1120 -; FirstLine = 1077 +; IDE Options = PureBasic 5.50 (Windows - x64) +; CursorPosition = 1132 +; FirstLine = 1123 ; Folding = --------- -; EnableUnicode ; EnableThread -; EnableXP \ No newline at end of file +; EnableXP +; EnableUnicode \ No newline at end of file From c679cfbd7330cd050005f14492fec28e8a00bbd4 Mon Sep 17 00:00:00 2001 From: djes Date: Wed, 24 Aug 2016 18:00:08 +0200 Subject: [PATCH 04/49] WIP typhoon cache cleaning --- PBMap.pb | 76 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 9985f29..dd6f07f 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -102,6 +102,7 @@ Module PBMap Structure ImgMemCach nImage.i Location.Location + Mutex.i EndStructure Structure TileMemCach @@ -481,23 +482,26 @@ Module PBMap Protected Tile.position ;Adding the image to the cache if possible AddMapElement(PBMap\MemCache\Images(), key) + PBMap\MemCache\Images(key)\Mutex = CreateMutex() + LockMutex(PBMap\MemCache\Images(key)\Mutex) nImage = GetTileFromHDD(CacheFile) If nImage = -1 nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, CacheFile) + If nImage = -1 + MyDebug("Error GetImageThread procedure, image not loaded - " + key) + *Tile\nImage = -1 + ProcedureReturn + EndIf EndIf - If nImage <> -1 - PBMap\MemCache\Images(key)\nImage = nImage - Tile\x=*Tile\PBMapTileX - Tile\y=*Tile\PBMapTiley - XY2LatLon(@Tile,@PBMap\MemCache\Images(key)\Location) - MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") - MyDebug("With the following key : " + key) - Else - MyDebug("Error GetImageThread procedure, image not loaded - " + key) - nImage = -1 - EndIf + PBMap\MemCache\Images(key)\nImage = nImage + Tile\x=*Tile\PBMapTileX + Tile\y=*Tile\PBMapTiley + XY2LatLon(@Tile,@PBMap\MemCache\Images(key)\Location) + MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") + MyDebug("With the following key : " + key) ;Define this tile image nb *Tile\nImage = nImage + UnlockMutex(PBMap\MemCache\Images(key)\Mutex) EndProcedure Procedure DrawTile(*Tile.Tile) @@ -583,23 +587,30 @@ Module PBMap DeleteElement(PBMap\TilesThreads()) EndIf Next - ;-****Clean Mem Cache + ;-**** Clean Mem Cache + ;TODO in development, by now there's many cache problem as the loading thread could be perturbed + ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize + Protected MaxNbTile.l + If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) + MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize + Else + MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize + EndIf + Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) + Protected Limit.d=Scale*(MaxNbTile)*1.5 + Debug "Cache cleaning" ForEach PBMap\MemCache\Images() - ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize - Protected MaxNbTile.l - If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) - MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize - Else - MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize - EndIf - Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) - Protected Limit.d=Scale*(MaxNbTile)*1.5 - Protected Distance.d=HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) - Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) - If Distance>Limit - Debug "delete" - DeleteMapElement(PBMap\MemCache\Images()) - EndIf + Protected Distance.d = HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) + Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) + If Distance>Limit And IsImage(PBMap\MemCache\Images()\nImage) + LockMutex(PBMap\MemCache\Images()\Mutex) + Debug "delete" + Debug PBMap\MemCache\Images() + FreeImage(PBMap\MemCache\Images()\nImage) + UnlockMutex(PBMap\MemCache\Images()\Mutex) + FreeMutex(PBMap\MemCache\Images()\Mutex) + DeleteMapElement(PBMap\MemCache\Images()) + EndIf Next EndProcedure @@ -984,7 +995,7 @@ Module PBMap ;New move values PBMap\Position\x - MouseX PBMap\Position\y - MouseY - ;Fill parameters and signal the drawing thread + ;Fill parameters and send a signal to the drawing thread LockMutex(PBMap\Drawing\Mutex) ;PBMap tile position in tile.decimal PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize @@ -992,9 +1003,9 @@ Module PBMap PBMap\Drawing\PassNb = 1 XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) UnlockMutex(PBMap\Drawing\Mutex) + ;Start drawing + SignalSemaphore(PBMap\Drawing\Semaphore) EndIf - ;Start drawing - SignalSemaphore(PBMap\Drawing\Semaphore) ;If CallBackLocation send Location to function If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) @@ -1167,8 +1178,9 @@ CompilerIf #PB_Compiler_IsMainFile EndIf CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1132 -; FirstLine = 1123 +; ExecutableFormat = Console +; CursorPosition = 595 +; FirstLine = 581 ; Folding = --------- ; EnableThread ; EnableXP From 46d4a37c982e70b0c4c44e9feb30cde5eaf349f4 Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 25 Aug 2016 11:56:54 +0200 Subject: [PATCH 05/49] Debug, code cleaning, debug infos Map list add was theaded and could lead to crash. --- PBMap.pb | 206 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 107 insertions(+), 99 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index dd6f07f..616e36d 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -25,7 +25,8 @@ UsePNGImageEncoder() DeclareModule PBMap ;-Show debug infos - Global Verbose = #False + Global Verbose = #True + Global MyDebugLevel = 1 Global Proxy = #False Declare InitPBMap() Declare SetMapServer(ServerURL.s="http://tile.openstreetmap.org/",TileSize.l=256,ZoomMin.l=0,ZoomMax.l=18) @@ -73,6 +74,7 @@ Module PBMap PBMapTileY.i PBMapZoom.i nImage.i + key.s GetImageThread.i EndStructure @@ -95,8 +97,8 @@ Module PBMap EndStructure Structure TileThread + *Tile.Tile GetImageThread.i - *Tile.Tile EndStructure Structure ImgMemCach @@ -162,9 +164,9 @@ Module PBMap End EndProcedure - ;Send debug infos to stdout - Procedure MyDebug(msg.s) - If Verbose + ;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 PrintN(msg) EndIf EndProcedure @@ -417,14 +419,13 @@ Module PBMap EndIf EndProcedure - Procedure.i GetTileFromMem(Zoom.i, XTile.i, YTile.i) - Protected key.s = "Z" + RSet(Str(Zoom), 4, "0") + "X" + RSet(Str(XTile), 8, "0") + "Y" + RSet(Str(YTile), 8, "0") - MyDebug("Check if we have this image in memory") + Procedure.i GetTileFromMem(key.s) + MyDebug("Check if we have this image in memory", 3) If FindMapElement(PBMap\MemCache\Images(), key) - MyDebug("Key : " + key + " found !") + MyDebug("Key : " + key + " found !", 3) ProcedureReturn PBMap\MemCache\Images()\nImage Else - MyDebug("Key : " + key + " not found !") + MyDebug("Key : " + key + " not found !", 3) ProcedureReturn -1 EndIf EndProcedure @@ -434,11 +435,11 @@ Module PBMap If FileSize(CacheFile) > 0 nImage = LoadImage(#PB_Any, CacheFile) If IsImage(nImage) - MyDebug("Loadimage " + CacheFile + " -> Success !") + MyDebug("Loadimage " + CacheFile + " -> Success !", 3) ProcedureReturn nImage EndIf EndIf - MyDebug("Loadimage " + CacheFile + " -> Failed !") + MyDebug("Loadimage " + CacheFile + " -> Failed !", 3) ProcedureReturn -1 EndProcedure @@ -447,29 +448,29 @@ Module PBMap Protected nImage.i = -1 Protected FileHandle.i Protected TileURL.s = PBMap\ServerURL + Str(Zoom) + "/" + Str(XTile) + "/" + Str(YTile) + ".png" - MyDebug("Check if we have this image on Web") + MyDebug("Check if we have this image on Web", 3) If Proxy FileHandle = CurlReceiveHTTPToFile(TileURL, CacheFile, ProxyURL$, ProxyPort$, ProxyUser$, ProxyPassword$) If FileHandle nImage = GetTileFromHDD(CacheFile) Else - MyDebug("File " + TileURL + " not correctly received with Curl and proxy") + MyDebug("File " + TileURL + " not correctly received with Curl and proxy", 3) EndIf Else *Buffer = ReceiveHTTPMemory(TileURL) ;TODO to thread by using #PB_HTTP_Asynchronous If *Buffer nImage = CatchImage(#PB_Any, *Buffer, MemorySize(*Buffer)) If IsImage(nImage) - MyDebug("Load from web " + TileURL + " as Tile nb " + nImage) + MyDebug("Load from web " + TileURL + " as Tile nb " + nImage, 3) SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG) FreeMemory(*Buffer) Else - MyDebug("Can't catch image " + TileURL) + MyDebug("Can't catch image " + TileURL, 3) nImage = -1 ;ShowMemoryViewer(*Buffer, MemorySize(*Buffer)) EndIf Else - MyDebug("ReceiveHTTPMemory's buffer is empty") + MyDebug("ReceiveHTTPMemory's buffer is empty", 3) EndIf EndIf ProcedureReturn nImage @@ -477,38 +478,35 @@ Module PBMap Procedure GetImageThread(*Tile.Tile) Protected nImage.i = -1 - Protected key.s = "Z" + RSet(Str(*Tile\PBMapZoom), 4, "0") + "X" + RSet(Str(*Tile\PBMapTileX), 8, "0") + "Y" + RSet(Str(*Tile\PBMapTileY), 8, "0") Protected CacheFile.s = PBMap\HDDCachePath + "PBMap_" + Str(*Tile\PBMapZoom) + "_" + Str(*Tile\PBMapTileX) + "_" + Str(*Tile\PBMapTileY) + ".png" Protected Tile.position - ;Adding the image to the cache if possible - AddMapElement(PBMap\MemCache\Images(), key) - PBMap\MemCache\Images(key)\Mutex = CreateMutex() - LockMutex(PBMap\MemCache\Images(key)\Mutex) +; PBMap\MemCache\Images(*Tile\key)\Mutex = CreateMutex() +; LockMutex(PBMap\MemCache\Images(*Tile\key)\Mutex) nImage = GetTileFromHDD(CacheFile) If nImage = -1 nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, CacheFile) If nImage = -1 - MyDebug("Error GetImageThread procedure, image not loaded - " + key) + MyDebug("Error GetImageThread procedure, image not loaded - " + *Tile\key, 3) *Tile\nImage = -1 ProcedureReturn EndIf EndIf - PBMap\MemCache\Images(key)\nImage = nImage - Tile\x=*Tile\PBMapTileX - Tile\y=*Tile\PBMapTiley - XY2LatLon(@Tile,@PBMap\MemCache\Images(key)\Location) - MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") - MyDebug("With the following key : " + key) + PBMap\MemCache\Images(*Tile\key)\nImage = nImage +; Tile\x=*Tile\PBMapTileX +; Tile\y=*Tile\PBMapTiley +; XY2LatLon(@Tile,@PBMap\MemCache\Images(*Tile\key)\Location) + MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache", 3) + MyDebug("With the following key : " + *Tile\key, 3) ;Define this tile image nb *Tile\nImage = nImage - UnlockMutex(PBMap\MemCache\Images(key)\Mutex) +; UnlockMutex(PBMap\MemCache\Images(key)\Mutex) EndProcedure Procedure DrawTile(*Tile.Tile) Protected x = *Tile\Position\x Protected y = *Tile\Position\y - MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX)) - MyDebug(" at coords " + Str(x) + "," + Str(y)) + MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) + MyDebug(" at coords " + Str(x) + "," + Str(y), 2) MovePathCursor(x, y) DrawVectorImage(ImageID(*Tile\nImage)) EndProcedure @@ -517,8 +515,8 @@ Module PBMap Protected x = *Tile\Position\x Protected y = *Tile\Position\y Protected Text$ = "Loading" - MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX)) - MyDebug(" at coords " + Str(x) + "," + Str(y)) + MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) + MyDebug(" at coords " + Str(x) + "," + Str(y), 2) BeginVectorLayer() ;MovePathCursor(x, y) VectorSourceColor(RGBA(255, 255, 255, 128)) @@ -538,17 +536,18 @@ Module PBMap Protected ty = Int(*Drawing\Position\y) Protected nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point Protected ny = *Drawing\CenterY / PBMap\TileSize - MyDebug("Drawing tiles") + MyDebug("Drawing tiles", 2) For y = - ny - 1 To ny + 1 For x = - nx - 1 To nx + 1 ;Was quiting the loop if a move occured, giving maybe smoother movement ;If PBMap\Moving ; Break 2 ;EndIf + ;Store parameters in only one memory place, and give it to the thread if needed Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) If *NewTile With *NewTile - ;Keep a track of tiles (especially to free memory) + ;Keep a track of tiles and eventually threads associated (to free memory) AddElement(PBMap\TilesThreads()) PBMap\TilesThreads()\Tile = *NewTile ;New tile parameters @@ -557,78 +556,98 @@ Module PBMap \PBMapTileX = tx + x \PBMapTileY = ty + y \PBMapZoom = PBMap\Zoom + \key = "Z" + RSet(Str(\PBMapZoom), 4, "0") + "X" + RSet(Str(\PBMapTileX), 8, "0") + "Y" + RSet(Str(\PBMapTileY), 8, "0") ;Check if the image exists - \nImage = GetTileFromMem(\PBMapZoom, \PBMapTileX, \PBMapTileY) - If \nImage = -1 - ;If not, load it in the background - \GetImageThread = CreateThread(@GetImageThread(), *NewTile) - PBMap\TilesThreads()\GetImageThread = \GetImageThread - MyDebug(" Creating get image thread nb " + Str(\GetImageThread)) + \nImage = GetTileFromMem(\key) + If \nImage = -1 + ;If not, load it in the background + If AddMapElement(PBMap\MemCache\Images(), \key) ;Add the image to the cache *only* in this loop + \GetImageThread = CreateThread(@GetImageThread(), *NewTile) + PBMap\TilesThreads()\GetImageThread = \GetImageThread + MyDebug(" Creating get image thread nb " + Str(\GetImageThread), 2) + Else + MyDebug(" Can't add a new image to the map list", 2) + EndIf EndIf If IsImage(\nImage) DrawTile(*NewTile) Else - MyDebug("Image missing") + MyDebug("Image missing", 2) DrawLoading(*NewTile) *Drawing\Dirty = #True ;Signals that this image is missing so we should have to redraw EndIf EndWith Else - MyDebug(" Error, can't create a new tile") + MyDebug(" Error, can't create a new tile", 2) Break 2 EndIf Next Next - ;Free tile memory when the loading thread has finished + ;Free tile memory ;TODO : get out this proc from drawtiles in a special "free ressources" task ForEach PBMap\TilesThreads() + ;Check if there's no more loading thread If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 FreeMemory(PBMap\TilesThreads()\Tile) DeleteElement(PBMap\TilesThreads()) EndIf Next - ;-**** Clean Mem Cache - ;TODO in development, by now there's many cache problem as the loading thread could be perturbed - ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize - Protected MaxNbTile.l - If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) - MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize - Else - MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize - EndIf - Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) - Protected Limit.d=Scale*(MaxNbTile)*1.5 - Debug "Cache cleaning" - ForEach PBMap\MemCache\Images() - Protected Distance.d = HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) - Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) - If Distance>Limit And IsImage(PBMap\MemCache\Images()\nImage) - LockMutex(PBMap\MemCache\Images()\Mutex) - Debug "delete" - Debug PBMap\MemCache\Images() - FreeImage(PBMap\MemCache\Images()\nImage) - UnlockMutex(PBMap\MemCache\Images()\Mutex) - FreeMutex(PBMap\MemCache\Images()\Mutex) - DeleteMapElement(PBMap\MemCache\Images()) - EndIf - Next +; ;-**** Clean Mem Cache +; ;TODO in development, by now there's many cache problem as the loading thread could be perturbed +; ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize +; Protected MaxNbTile.l +; If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) +; MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize +; Else +; MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize +; EndIf +; Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) +; Protected Limit.d=Scale*(MaxNbTile)*1.5 +; Debug "Cache cleaning" +; ForEach PBMap\MemCache\Images() +; Protected Distance.d = HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) +; Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) +; If Distance>Limit And IsImage(PBMap\MemCache\Images()\nImage) +; LockMutex(PBMap\MemCache\Images()\Mutex) +; Debug "delete" +; Debug PBMap\MemCache\Images() +; FreeImage(PBMap\MemCache\Images()\nImage) +; UnlockMutex(PBMap\MemCache\Images()\Mutex) +; FreeMutex(PBMap\MemCache\Images()\Mutex) +; DeleteMapElement(PBMap\MemCache\Images()) +; EndIf +; Next EndProcedure - Procedure Pointer(x.i, y.i, color.l = 0) - VectorSourceColor(color) - MovePathCursor(x, y) - AddPathLine(-8, -16, #PB_Path_Relative) - AddPathCircle(8, 0, 8, 180, 0, #PB_Path_Relative) - AddPathLine(-8, 16, #PB_Path_Relative) - ;FillPath(#PB_Path_Preserve) - ;ClipPath(#PB_Path_Preserve) - AddPathCircle(0, -16, 5, 0, 360, #PB_Path_Relative) - VectorSourceColor(color) - FillPath(#PB_Path_Preserve):VectorSourceColor(color);RGBA(0, 0, 0, 255)) - StrokePath(1) + Procedure DrawPointer(*Drawing.DrawingParameters) + If PBMap\CallBackMainPointer > 0 + ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) + CallFunctionFast(PBMap\CallBackMainPointer, *Drawing\CenterX, *Drawing\CenterY) + Else + VectorSourceColor(RGBA($FF, 0, 0, $FF)) + MovePathCursor(*Drawing\CenterX, *Drawing\CenterY) + AddPathLine(-8, -16, #PB_Path_Relative) + AddPathCircle(8, 0, 8, 180, 0, #PB_Path_Relative) + AddPathLine(-8, 16, #PB_Path_Relative) + ;FillPath(#PB_Path_Preserve) + ;ClipPath(#PB_Path_Preserve) + AddPathCircle(0, -16, 5, 0, 360, #PB_Path_Relative) + VectorSourceColor(RGBA($FF, 0, 0, $FF)) + FillPath(#PB_Path_Preserve):VectorSourceColor(RGBA($FF, 0, 0, $FF));RGBA(0, 0, 0, 255)) + StrokePath(1) + EndIf EndProcedure + Procedure DrawScale(*Drawing.DrawingParameters) + ;TODO Add Option and function to display Scale on Map + Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) + VectorFont(FontID(PBMap\Font), 30) + VectorSourceColor(RGBA(0, 0, 0, 80)) + MovePathCursor(50,50) + DrawVectorText(StrD(Scale)) + EndProcedure + Procedure TrackPointer(x.i, y.i,dist.l) Protected color.l color=RGBA(0, 0, 0, 255) @@ -711,7 +730,7 @@ Module PBMap If PBMap\Marker()\CallBackPointer > 0 CallFunctionFast(PBMap\Marker()\CallBackPointer, Pixel\X, Pixel\Y) Else - Pointer(Pixel\X, Pixel\Y, PBMap\Marker()\color) + DrawPointer(*Drawing) EndIf EndIf EndIf @@ -745,20 +764,9 @@ Module PBMap DrawTiles(@Drawing) DrawTrack(@Drawing) DrawMarker(@Drawing) - ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) - If PBMap\CallBackMainPointer > 0 - CallFunctionFast(PBMap\CallBackMainPointer, Drawing\CenterX, Drawing\CenterY) - Else - Pointer(Drawing\CenterX, Drawing\CenterY, RGBA($FF, 0, 0, $FF)) - EndIf - ;TODO Add Option and function to display Scale on Map - ;Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) - ;VectorFont(FontID(PBMap\Font), 30) - ;VectorSourceColor(RGBA(0, 0, 0, 80)) - ;MovePathCursor(50,50) - ;DrawVectorText(StrD(Scale)) - - ;- Display How Many Image in Cache + DrawPointer(@Drawing) + ;DrawScale(@Drawing) + ;- Display how many images in cache VectorFont(FontID(PBMap\Font), 30) VectorSourceColor(RGBA(0, 0, 0, 80)) MovePathCursor(50,50) @@ -1179,8 +1187,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) ; ExecutableFormat = Console -; CursorPosition = 595 -; FirstLine = 581 +; CursorPosition = 445 +; FirstLine = 442 ; Folding = --------- ; EnableThread ; EnableXP From a4f2ea5bb71b9ff32f4d058c30c268254238a0fb Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 25 Aug 2016 12:52:36 +0200 Subject: [PATCH 06/49] code cleanup --- PBMap.pb | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 616e36d..b069392 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -26,7 +26,7 @@ UsePNGImageEncoder() DeclareModule PBMap ;-Show debug infos Global Verbose = #True - Global MyDebugLevel = 1 + Global MyDebugLevel = 3 Global Proxy = #False Declare InitPBMap() Declare SetMapServer(ServerURL.s="http://tile.openstreetmap.org/",TileSize.l=256,ZoomMin.l=0,ZoomMax.l=18) @@ -103,8 +103,8 @@ Module PBMap Structure ImgMemCach nImage.i - Location.Location - Mutex.i + ;Location.Location + ;Mutex.i EndStructure Structure TileMemCach @@ -184,8 +184,8 @@ Module PBMap Procedure.i CurlReceiveHTTPToFile(URL$, DestFileName$, ProxyURL$="", ProxyPort$="", ProxyUser$="", ProxyPassword$="") Protected *Buffer, curl.i, Timeout.i, res.i Protected FileHandle.i - MyDebug("ReceiveHTTPToFile from " + URL$ + " " + ProxyURL$ + ProxyPort$ + ProxyUser$) - MyDebug(" to file : " + DestFileName$) + MyDebug("ReceiveHTTPToFile from " + URL$ + " " + ProxyURL$ + ProxyPort$ + ProxyUser$, 3) + MyDebug(" to file : " + DestFileName$, 3) FileHandle = CreateFile(#PB_Any, DestFileName$) If FileHandle And Len(URL$) curl = curl_easy_init() @@ -218,11 +218,11 @@ Module PBMap curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @ReceiveHTTPWriteToFileFunction()) res = curl_easy_perform(curl) If res <> #CURLE_OK - MyDebug("CURL problem") + MyDebug("CURL problem", 3) EndIf curl_easy_cleanup(curl) Else - MyDebug("Can't init CURL") + MyDebug("Can't init CURL", 3) EndIf CloseFile(FileHandle) ProcedureReturn FileSize(DestFileName$) @@ -245,7 +245,7 @@ Module PBMap PBMap\Dirty = #False PBMap\Drawing\Mutex = CreateMutex() PBMap\Drawing\Semaphore = CreateSemaphore() - PBMap\EditMarkerIndex = -1 ;<- You must initialize with No Marker selected + PBMap\EditMarkerIndex = -1 ;Initialised with "no marker selected" PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold) ;-Options PBMap\Options\WheelMouseRelative = #True @@ -289,10 +289,12 @@ Module PBMap EndProcedure Procedure Quit() - ;kill main drawing thread (nicer than KillThread(PBMap\MainDrawingThread)) + ;Ask main drawing thread to stop and wait for it (nicer than KillThread(PBMap\MainDrawingThread)) LockMutex(PBMap\Drawing\Mutex) PBMap\Drawing\End = #True - UnlockMutex(PBMap\Drawing\Mutex) + ;KillThread(PBMap\MainDrawingThread) + UnlockMutex(PBMap\Drawing\Mutex) + Repeat : Until Not IsThread(PBMap\MainDrawingThread) ;wait for loading threads to finish nicely ResetList(PBMap\TilesThreads()) While NextElement(PBMap\TilesThreads()) @@ -302,7 +304,7 @@ Module PBMap ResetList( PBMap\TilesThreads()) EndIf Wend - curl_global_cleanup() + curl_global_cleanup() EndProcedure Macro Min(a,b) @@ -547,7 +549,7 @@ Module PBMap Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) If *NewTile With *NewTile - ;Keep a track of tiles and eventually threads associated (to free memory) + ;Keep a track of tiles, and eventually associated threads, to free memory AddElement(PBMap\TilesThreads()) PBMap\TilesThreads()\Tile = *NewTile ;New tile parameters @@ -556,12 +558,12 @@ Module PBMap \PBMapTileX = tx + x \PBMapTileY = ty + y \PBMapZoom = PBMap\Zoom - \key = "Z" + RSet(Str(\PBMapZoom), 4, "0") + "X" + RSet(Str(\PBMapTileX), 8, "0") + "Y" + RSet(Str(\PBMapTileY), 8, "0") + \key = "Z" + RSet(Str(\PBMapZoom), 4, "0") + "X" + RSet(Str(\PBMapTileX), 8, "0") + "Y" + RSet(Str(\PBMapTileY), 8, "0") ;Unique identifier ;Check if the image exists \nImage = GetTileFromMem(\key) If \nImage = -1 ;If not, load it in the background - If AddMapElement(PBMap\MemCache\Images(), \key) ;Add the image to the cache *only* in this loop + If AddMapElement(PBMap\MemCache\Images(), \key) ;Add the image to the cache, once in this loop \GetImageThread = CreateThread(@GetImageThread(), *NewTile) PBMap\TilesThreads()\GetImageThread = \GetImageThread MyDebug(" Creating get image thread nb " + Str(\GetImageThread), 2) @@ -583,7 +585,7 @@ Module PBMap EndIf Next Next - ;Free tile memory + ;Free tile memory ;TODO : get out this proc from drawtiles in a special "free ressources" task ForEach PBMap\TilesThreads() ;Check if there's no more loading thread @@ -617,7 +619,7 @@ Module PBMap ; DeleteMapElement(PBMap\MemCache\Images()) ; EndIf ; Next - + EndProcedure Procedure DrawPointer(*Drawing.DrawingParameters) @@ -771,7 +773,9 @@ Module PBMap VectorSourceColor(RGBA(0, 0, 0, 80)) MovePathCursor(50,50) DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) - StopVectorDrawing() + MovePathCursor(50,60) + DrawVectorText(Str(ListSize(PBMap\TilesThreads()))) + StopVectorDrawing() ;Redraw ; If something was not correctly drawn, redraw after a while LockMutex(*SharedDrawing\Mutex) ;Be sure that we're not modifying variables while moving (seems not useful, but it is, especially to clean the semaphore) @@ -784,7 +788,7 @@ Module PBMap Repeat : Until TrySemaphore(*SharedDrawing\Semaphore) = 0 EndIf UnlockMutex(*SharedDrawing\Mutex) - Until Drawing\End + Until Drawing\End EndProcedure Procedure Refresh() @@ -1187,8 +1191,6 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) ; ExecutableFormat = Console -; CursorPosition = 445 -; FirstLine = 442 ; Folding = --------- ; EnableThread ; EnableXP From 5fc43503e05c9fe29ab787eec764b769ae83e0c0 Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 25 Aug 2016 13:51:34 +0200 Subject: [PATCH 07/49] Better quit handling --- PBMap.pb | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index b069392..e94fce6 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -25,7 +25,7 @@ UsePNGImageEncoder() DeclareModule PBMap ;-Show debug infos - Global Verbose = #True + Global Verbose = #false Global MyDebugLevel = 3 Global Proxy = #False Declare InitPBMap() @@ -292,18 +292,19 @@ Module PBMap ;Ask main drawing thread to stop and wait for it (nicer than KillThread(PBMap\MainDrawingThread)) LockMutex(PBMap\Drawing\Mutex) PBMap\Drawing\End = #True - ;KillThread(PBMap\MainDrawingThread) UnlockMutex(PBMap\Drawing\Mutex) Repeat : Until Not IsThread(PBMap\MainDrawingThread) - ;wait for loading threads to finish nicely - ResetList(PBMap\TilesThreads()) - While NextElement(PBMap\TilesThreads()) - If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 - FreeMemory(PBMap\TilesThreads()\Tile) - DeleteElement(PBMap\TilesThreads()) - ResetList( PBMap\TilesThreads()) - EndIf - Wend + ;Wait for loading threads to finish nicely + Repeat + ResetList(PBMap\TilesThreads()) + While NextElement(PBMap\TilesThreads()) + If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 + FreeMemory(PBMap\TilesThreads()\Tile) + DeleteElement(PBMap\TilesThreads()) + EndIf + Wend + Delay(20) + Until ListSize(PBMap\TilesThreads()) = 0 curl_global_cleanup() EndProcedure @@ -1191,6 +1192,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) ; ExecutableFormat = Console +; CursorPosition = 305 +; FirstLine = 278 ; Folding = --------- ; EnableThread ; EnableXP From 836e3b70bb13a3d18a69eff0b7fd99196945592d Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 25 Aug 2016 17:55:39 +0200 Subject: [PATCH 08/49] Tracking infamous GetTileFromHDD bug hanging sometimes don't know why. --- PBMap.pb | 52 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index e94fce6..32ba62b 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -25,7 +25,7 @@ UsePNGImageEncoder() DeclareModule PBMap ;-Show debug infos - Global Verbose = #false + Global Verbose = #False Global MyDebugLevel = 3 Global Proxy = #False Declare InitPBMap() @@ -156,7 +156,9 @@ Module PBMap Options.option ; EndStructure + ;-Global variables Global PBMap.PBMap, Null.i + Global LoadMutexTest = CreateMutex() ;Shows an error msg and terminates the program Procedure Error(msg.s) @@ -168,6 +170,7 @@ Module PBMap Procedure MyDebug(msg.s, DbgLevel = 0) If Verbose And MyDebugLevel >= DbgLevel PrintN(msg) + ;Debug msg EndIf EndProcedure @@ -289,21 +292,33 @@ Module PBMap EndProcedure Procedure Quit() - ;Ask main drawing thread to stop and wait for it (nicer than KillThread(PBMap\MainDrawingThread)) + Protected TimeCounter.q + ;Ask main drawing thread to stop and wait for it for 2 seconds LockMutex(PBMap\Drawing\Mutex) PBMap\Drawing\End = #True - UnlockMutex(PBMap\Drawing\Mutex) - Repeat : Until Not IsThread(PBMap\MainDrawingThread) - ;Wait for loading threads to finish nicely + UnlockMutex(PBMap\Drawing\Mutex) + TimeCounter = ElapsedMilliseconds() + Repeat + Delay(100) + If ElapsedMilliseconds() - TimeCounter > 2000 + ;Should not occur + KillThread(PBMap\MainDrawingThread) + EndIf + Until Not IsThread(PBMap\MainDrawingThread) + ;Wait for loading threads to finish nicely. Passed 2 seconds, kills them. + TimeCounter = ElapsedMilliseconds() Repeat ResetList(PBMap\TilesThreads()) While NextElement(PBMap\TilesThreads()) If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 FreeMemory(PBMap\TilesThreads()\Tile) DeleteElement(PBMap\TilesThreads()) + ElseIf ElapsedMilliseconds() - TimeCounter > 2000 + ;Should not occur + KillThread(PBMap\TilesThreads()\GetImageThread) EndIf Wend - Delay(20) + Delay(10) Until ListSize(PBMap\TilesThreads()) = 0 curl_global_cleanup() EndProcedure @@ -434,15 +449,22 @@ Module PBMap EndProcedure Procedure.i GetTileFromHDD(CacheFile.s) - Protected nImage.i + Protected nImage.i + Debug "Loading image " + CacheFile + " ; Size : " + Str(FileSize(CacheFile)) If FileSize(CacheFile) > 0 nImage = LoadImage(#PB_Any, CacheFile) If IsImage(nImage) - MyDebug("Loadimage " + CacheFile + " -> Success !", 3) + Debug "Success loading " + CacheFile + " as nImage " + Str(nImage) + MyDebug("Success loading " + CacheFile + " as nImage " + Str(nImage), 3) ProcedureReturn nImage + Else + Debug "Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !" + MyDebug("Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !", 3) EndIf + Else + Debug "Failed loading " + CacheFile + " -> Size = 0" + MyDebug("Failed loading " + CacheFile + " -> Size = 0", 3) EndIf - MyDebug("Loadimage " + CacheFile + " -> Failed !", 3) ProcedureReturn -1 EndProcedure @@ -485,7 +507,9 @@ Module PBMap Protected Tile.position ; PBMap\MemCache\Images(*Tile\key)\Mutex = CreateMutex() ; LockMutex(PBMap\MemCache\Images(*Tile\key)\Mutex) +; LockMutex(LoadMutexTest) nImage = GetTileFromHDD(CacheFile) +; UnlockMutex(LoadMutexTest) If nImage = -1 nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, CacheFile) If nImage = -1 @@ -502,7 +526,7 @@ Module PBMap MyDebug("With the following key : " + *Tile\key, 3) ;Define this tile image nb *Tile\nImage = nImage -; UnlockMutex(PBMap\MemCache\Images(key)\Mutex) + ; UnlockMutex(PBMap\MemCache\Images(key)\Mutex) EndProcedure Procedure DrawTile(*Tile.Tile) @@ -570,6 +594,7 @@ Module PBMap MyDebug(" Creating get image thread nb " + Str(\GetImageThread), 2) Else MyDebug(" Can't add a new image to the map list", 2) + CallDebugger EndIf EndIf If IsImage(\nImage) @@ -774,7 +799,7 @@ Module PBMap VectorSourceColor(RGBA(0, 0, 0, 80)) MovePathCursor(50,50) DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) - MovePathCursor(50,60) + MovePathCursor(50,80) DrawVectorText(Str(ListSize(PBMap\TilesThreads()))) StopVectorDrawing() ;Redraw @@ -1191,9 +1216,8 @@ CompilerIf #PB_Compiler_IsMainFile EndIf CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; ExecutableFormat = Console -; CursorPosition = 305 -; FirstLine = 278 +; CursorPosition = 450 +; FirstLine = 444 ; Folding = --------- ; EnableThread ; EnableXP From bb00a6b0395194698acb54c1a33aa2de843f52c6 Mon Sep 17 00:00:00 2001 From: djes Date: Fri, 26 Aug 2016 09:41:26 +0200 Subject: [PATCH 09/49] First version --- PBMap.pb | 255 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 169 insertions(+), 86 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index f6f955a..537af4d 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -26,6 +26,7 @@ UsePNGImageEncoder() DeclareModule PBMap ;-Show debug infos Global Verbose = #False + Global MyDebugLevel = 3 Global Proxy = #False Declare InitPBMap() Declare SetMapServer(ServerURL.s="http://tile.openstreetmap.org/",TileSize.l=256,ZoomMin.l=0,ZoomMax.l=18) @@ -73,6 +74,7 @@ Module PBMap PBMapTileY.i PBMapZoom.i nImage.i + key.s GetImageThread.i EndStructure @@ -95,13 +97,14 @@ Module PBMap EndStructure Structure TileThread + *Tile.Tile GetImageThread.i - *Tile.Tile EndStructure Structure ImgMemCach nImage.i - Usage.i + ;Location.Location + ;Mutex.i EndStructure Structure TileMemCach @@ -153,7 +156,9 @@ Module PBMap Options.option ; EndStructure + ;-Global variables Global PBMap.PBMap, Null.i + Global LoadMutexTest = CreateMutex() ;Shows an error msg and terminates the program Procedure Error(msg.s) @@ -161,10 +166,11 @@ Module PBMap End EndProcedure - ;Send debug infos to stdout - Procedure MyDebug(msg.s) - If Verbose + ;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 PrintN(msg) + ;Debug msg EndIf EndProcedure @@ -181,8 +187,8 @@ Module PBMap Procedure.i CurlReceiveHTTPToFile(URL$, DestFileName$, ProxyURL$="", ProxyPort$="", ProxyUser$="", ProxyPassword$="") Protected *Buffer, curl.i, Timeout.i, res.i Protected FileHandle.i - MyDebug("ReceiveHTTPToFile from " + URL$ + " " + ProxyURL$ + ProxyPort$ + ProxyUser$) - MyDebug(" to file : " + DestFileName$) + MyDebug("ReceiveHTTPToFile from " + URL$ + " " + ProxyURL$ + ProxyPort$ + ProxyUser$, 3) + MyDebug(" to file : " + DestFileName$, 3) FileHandle = CreateFile(#PB_Any, DestFileName$) If FileHandle And Len(URL$) curl = curl_easy_init() @@ -215,11 +221,11 @@ Module PBMap curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @ReceiveHTTPWriteToFileFunction()) res = curl_easy_perform(curl) If res <> #CURLE_OK - MyDebug("CURL problem") + MyDebug("CURL problem", 3) EndIf curl_easy_cleanup(curl) Else - MyDebug("Can't init CURL") + MyDebug("Can't init CURL", 3) EndIf CloseFile(FileHandle) ProcedureReturn FileSize(DestFileName$) @@ -242,7 +248,7 @@ Module PBMap PBMap\Dirty = #False PBMap\Drawing\Mutex = CreateMutex() PBMap\Drawing\Semaphore = CreateSemaphore() - PBMap\EditMarkerIndex = -1 ;<- You must initialize with No Marker selected + PBMap\EditMarkerIndex = -1 ;Initialised with "no marker selected" PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold) ;-Options PBMap\Options\WheelMouseRelative = #True @@ -286,20 +292,35 @@ Module PBMap EndProcedure Procedure Quit() - ;kill main drawing thread (nicer than KillThread(PBMap\MainDrawingThread)) + Protected TimeCounter.q + ;Ask main drawing thread to stop and wait for it for 2 seconds LockMutex(PBMap\Drawing\Mutex) PBMap\Drawing\End = #True UnlockMutex(PBMap\Drawing\Mutex) - ;wait for loading threads to finish nicely - ResetList(PBMap\TilesThreads()) - While NextElement(PBMap\TilesThreads()) - If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 - FreeMemory(PBMap\TilesThreads()\Tile) - DeleteElement(PBMap\TilesThreads()) - ResetList( PBMap\TilesThreads()) + TimeCounter = ElapsedMilliseconds() + Repeat + Delay(100) + If ElapsedMilliseconds() - TimeCounter > 2000 + ;Should not occur + KillThread(PBMap\MainDrawingThread) EndIf - Wend - curl_global_cleanup() + Until Not IsThread(PBMap\MainDrawingThread) + ;Wait for loading threads to finish nicely. Passed 2 seconds, kills them. + TimeCounter = ElapsedMilliseconds() + Repeat + ResetList(PBMap\TilesThreads()) + While NextElement(PBMap\TilesThreads()) + If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 + FreeMemory(PBMap\TilesThreads()\Tile) + DeleteElement(PBMap\TilesThreads()) + ElseIf ElapsedMilliseconds() - TimeCounter > 2000 + ;Should not occur + KillThread(PBMap\TilesThreads()\GetImageThread) + EndIf + Wend + Delay(10) + Until ListSize(PBMap\TilesThreads()) = 0 + curl_global_cleanup() EndProcedure Macro Min(a,b) @@ -416,28 +437,34 @@ Module PBMap EndIf EndProcedure - Procedure.i GetTileFromMem(Zoom.i, XTile.i, YTile.i) - Protected key.s = "Z" + RSet(Str(Zoom), 4, "0") + "X" + RSet(Str(XTile), 8, "0") + "Y" + RSet(Str(YTile), 8, "0") - MyDebug("Check if we have this image in memory") + Procedure.i GetTileFromMem(key.s) + MyDebug("Check if we have this image in memory", 3) If FindMapElement(PBMap\MemCache\Images(), key) - MyDebug("Key : " + key + " found !") + MyDebug("Key : " + key + " found !", 3) ProcedureReturn PBMap\MemCache\Images()\nImage Else - MyDebug("Key : " + key + " not found !") + MyDebug("Key : " + key + " not found !", 3) ProcedureReturn -1 EndIf EndProcedure Procedure.i GetTileFromHDD(CacheFile.s) - Protected nImage.i + Protected nImage.i + Debug "Loading image " + CacheFile + " ; Size : " + Str(FileSize(CacheFile)) If FileSize(CacheFile) > 0 nImage = LoadImage(#PB_Any, CacheFile) If IsImage(nImage) - MyDebug("Loadimage " + CacheFile + " -> Success !") + Debug "Success loading " + CacheFile + " as nImage " + Str(nImage) + MyDebug("Success loading " + CacheFile + " as nImage " + Str(nImage), 3) ProcedureReturn nImage + Else + Debug "Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !" + MyDebug("Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !", 3) EndIf + Else + Debug "Failed loading " + CacheFile + " -> Size = 0" + MyDebug("Failed loading " + CacheFile + " -> Size = 0", 3) EndIf - MyDebug("Loadimage " + CacheFile + " -> Failed !") ProcedureReturn -1 EndProcedure @@ -446,29 +473,29 @@ Module PBMap Protected nImage.i = -1 Protected FileHandle.i Protected TileURL.s = PBMap\ServerURL + Str(Zoom) + "/" + Str(XTile) + "/" + Str(YTile) + ".png" - MyDebug("Check if we have this image on Web") + MyDebug("Check if we have this image on Web", 3) If Proxy FileHandle = CurlReceiveHTTPToFile(TileURL, CacheFile, ProxyURL$, ProxyPort$, ProxyUser$, ProxyPassword$) If FileHandle nImage = GetTileFromHDD(CacheFile) Else - MyDebug("File " + TileURL + " not correctly received with Curl and proxy") + MyDebug("File " + TileURL + " not correctly received with Curl and proxy", 3) EndIf Else *Buffer = ReceiveHTTPMemory(TileURL) ;TODO to thread by using #PB_HTTP_Asynchronous If *Buffer nImage = CatchImage(#PB_Any, *Buffer, MemorySize(*Buffer)) If IsImage(nImage) - MyDebug("Load from web " + TileURL + " as Tile nb " + nImage) + MyDebug("Load from web " + TileURL + " as Tile nb " + nImage, 3) SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG) FreeMemory(*Buffer) Else - MyDebug("Can't catch image " + TileURL) + MyDebug("Can't catch image " + TileURL, 3) nImage = -1 ;ShowMemoryViewer(*Buffer, MemorySize(*Buffer)) EndIf Else - MyDebug("ReceiveHTTPMemory's buffer is empty") + MyDebug("ReceiveHTTPMemory's buffer is empty", 3) EndIf EndIf ProcedureReturn nImage @@ -476,31 +503,37 @@ Module PBMap Procedure GetImageThread(*Tile.Tile) Protected nImage.i = -1 - Protected key.s = "Z" + RSet(Str(*Tile\PBMapZoom), 4, "0") + "X" + RSet(Str(*Tile\PBMapTileX), 8, "0") + "Y" + RSet(Str(*Tile\PBMapTileY), 8, "0") Protected CacheFile.s = PBMap\HDDCachePath + "PBMap_" + Str(*Tile\PBMapZoom) + "_" + Str(*Tile\PBMapTileX) + "_" + Str(*Tile\PBMapTileY) + ".png" - ;Adding the image to the cache if possible - AddMapElement(PBMap\MemCache\Images(), key) + Protected Tile.position +; PBMap\MemCache\Images(*Tile\key)\Mutex = CreateMutex() +; LockMutex(PBMap\MemCache\Images(*Tile\key)\Mutex) +; LockMutex(LoadMutexTest) nImage = GetTileFromHDD(CacheFile) +; UnlockMutex(LoadMutexTest) If nImage = -1 nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, CacheFile) + If nImage = -1 + MyDebug("Error GetImageThread procedure, image not loaded - " + *Tile\key, 3) + *Tile\nImage = -1 + ProcedureReturn + EndIf EndIf - If nImage <> -1 - PBMap\MemCache\Images(key)\nImage = nImage - MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") - MyDebug("With the following key : " + key) - Else - MyDebug("Error GetImageThread procedure, image not loaded - " + key) - nImage = -1 - EndIf + PBMap\MemCache\Images(*Tile\key)\nImage = nImage +; Tile\x=*Tile\PBMapTileX +; Tile\y=*Tile\PBMapTiley +; XY2LatLon(@Tile,@PBMap\MemCache\Images(*Tile\key)\Location) + MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache", 3) + MyDebug("With the following key : " + *Tile\key, 3) ;Define this tile image nb *Tile\nImage = nImage + ; UnlockMutex(PBMap\MemCache\Images(key)\Mutex) EndProcedure Procedure DrawTile(*Tile.Tile) Protected x = *Tile\Position\x Protected y = *Tile\Position\y - MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX)) - MyDebug(" at coords " + Str(x) + "," + Str(y)) + MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) + MyDebug(" at coords " + Str(x) + "," + Str(y), 2) MovePathCursor(x, y) DrawVectorImage(ImageID(*Tile\nImage)) EndProcedure @@ -509,8 +542,8 @@ Module PBMap Protected x = *Tile\Position\x Protected y = *Tile\Position\y Protected Text$ = "Loading" - MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX)) - MyDebug(" at coords " + Str(x) + "," + Str(y)) + MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) + MyDebug(" at coords " + Str(x) + "," + Str(y), 2) BeginVectorLayer() ;MovePathCursor(x, y) VectorSourceColor(RGBA(255, 255, 255, 128)) @@ -530,17 +563,18 @@ Module PBMap Protected ty = Int(*Drawing\Position\y) Protected nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point Protected ny = *Drawing\CenterY / PBMap\TileSize - MyDebug("Drawing tiles") + MyDebug("Drawing tiles", 2) For y = - ny - 1 To ny + 1 For x = - nx - 1 To nx + 1 ;Was quiting the loop if a move occured, giving maybe smoother movement ;If PBMap\Moving ; Break 2 ;EndIf + ;Store parameters in only one memory place, and give it to the thread if needed Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) If *NewTile With *NewTile - ;Keep a track of tiles (especially to free memory) + ;Keep a track of tiles, and eventually associated threads, to free memory AddElement(PBMap\TilesThreads()) PBMap\TilesThreads()\Tile = *NewTile ;New tile parameters @@ -549,52 +583,99 @@ Module PBMap \PBMapTileX = tx + x \PBMapTileY = ty + y \PBMapZoom = PBMap\Zoom + \key = "Z" + RSet(Str(\PBMapZoom), 4, "0") + "X" + RSet(Str(\PBMapTileX), 8, "0") + "Y" + RSet(Str(\PBMapTileY), 8, "0") ;Unique identifier ;Check if the image exists - \nImage = GetTileFromMem(\PBMapZoom, \PBMapTileX, \PBMapTileY) - If \nImage = -1 - ;If not, load it in the background - \GetImageThread = CreateThread(@GetImageThread(), *NewTile) - PBMap\TilesThreads()\GetImageThread = \GetImageThread - MyDebug(" Creating get image thread nb " + Str(\GetImageThread)) + \nImage = GetTileFromMem(\key) + If \nImage = -1 + ;If not, load it in the background + If AddMapElement(PBMap\MemCache\Images(), \key) ;Add the image to the cache, once in this loop + \GetImageThread = CreateThread(@GetImageThread(), *NewTile) + PBMap\TilesThreads()\GetImageThread = \GetImageThread + MyDebug(" Creating get image thread nb " + Str(\GetImageThread), 2) + Else + MyDebug(" Can't add a new image to the map list", 2) + CallDebugger + EndIf EndIf If IsImage(\nImage) DrawTile(*NewTile) Else - MyDebug("Image missing") + MyDebug("Image missing", 2) DrawLoading(*NewTile) *Drawing\Dirty = #True ;Signals that this image is missing so we should have to redraw EndIf EndWith Else - MyDebug(" Error, can't create a new tile") + MyDebug(" Error, can't create a new tile", 2) Break 2 EndIf Next Next - ;Free tile memory when the loading thread has finished + ;Free tile memory ;TODO : get out this proc from drawtiles in a special "free ressources" task ForEach PBMap\TilesThreads() + ;Check if there's no more loading thread If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 FreeMemory(PBMap\TilesThreads()\Tile) DeleteElement(PBMap\TilesThreads()) EndIf Next +; ;-**** Clean Mem Cache +; ;TODO in development, by now there's many cache problem as the loading thread could be perturbed +; ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize +; Protected MaxNbTile.l +; If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) +; MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize +; Else +; MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize +; EndIf +; Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) +; Protected Limit.d=Scale*(MaxNbTile)*1.5 +; Debug "Cache cleaning" +; ForEach PBMap\MemCache\Images() +; Protected Distance.d = HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) +; Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) +; If Distance>Limit And IsImage(PBMap\MemCache\Images()\nImage) +; LockMutex(PBMap\MemCache\Images()\Mutex) +; Debug "delete" +; Debug PBMap\MemCache\Images() +; FreeImage(PBMap\MemCache\Images()\nImage) +; UnlockMutex(PBMap\MemCache\Images()\Mutex) +; FreeMutex(PBMap\MemCache\Images()\Mutex) +; DeleteMapElement(PBMap\MemCache\Images()) +; EndIf +; Next + EndProcedure - Procedure Pointer(x.i, y.i, color.l = 0) - VectorSourceColor(color) - MovePathCursor(x, y) - AddPathLine(-8, -16, #PB_Path_Relative) - AddPathCircle(8, 0, 8, 180, 0, #PB_Path_Relative) - AddPathLine(-8, 16, #PB_Path_Relative) - ;FillPath(#PB_Path_Preserve) - ;ClipPath(#PB_Path_Preserve) - AddPathCircle(0, -16, 5, 0, 360, #PB_Path_Relative) - VectorSourceColor(color) - FillPath(#PB_Path_Preserve):VectorSourceColor(color);RGBA(0, 0, 0, 255)) - StrokePath(1) + Procedure DrawPointer(*Drawing.DrawingParameters) + If PBMap\CallBackMainPointer > 0 + ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) + CallFunctionFast(PBMap\CallBackMainPointer, *Drawing\CenterX, *Drawing\CenterY) + Else + VectorSourceColor(RGBA($FF, 0, 0, $FF)) + MovePathCursor(*Drawing\CenterX, *Drawing\CenterY) + AddPathLine(-8, -16, #PB_Path_Relative) + AddPathCircle(8, 0, 8, 180, 0, #PB_Path_Relative) + AddPathLine(-8, 16, #PB_Path_Relative) + ;FillPath(#PB_Path_Preserve) + ;ClipPath(#PB_Path_Preserve) + AddPathCircle(0, -16, 5, 0, 360, #PB_Path_Relative) + VectorSourceColor(RGBA($FF, 0, 0, $FF)) + FillPath(#PB_Path_Preserve):VectorSourceColor(RGBA($FF, 0, 0, $FF));RGBA(0, 0, 0, 255)) + StrokePath(1) + EndIf EndProcedure + Procedure DrawScale(*Drawing.DrawingParameters) + ;TODO Add Option and function to display Scale on Map + Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) + VectorFont(FontID(PBMap\Font), 30) + VectorSourceColor(RGBA(0, 0, 0, 80)) + MovePathCursor(50,50) + DrawVectorText(StrD(Scale)) + EndProcedure + Procedure TrackPointer(x.i, y.i,dist.l) Protected color.l color=RGBA(0, 0, 0, 255) @@ -677,7 +758,7 @@ Module PBMap If PBMap\Marker()\CallBackPointer > 0 CallFunctionFast(PBMap\Marker()\CallBackPointer, Pixel\X, Pixel\Y) Else - Pointer(Pixel\X, Pixel\Y, PBMap\Marker()\color) + DrawPointer(*Drawing) EndIf EndIf EndIf @@ -711,13 +792,16 @@ Module PBMap DrawTiles(@Drawing) DrawTrack(@Drawing) DrawMarker(@Drawing) - ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) - If PBMap\CallBackMainPointer > 0 - CallFunctionFast(PBMap\CallBackMainPointer, Drawing\CenterX, Drawing\CenterY) - Else - Pointer(Drawing\CenterX, Drawing\CenterY, RGBA($FF, 0, 0, $FF)) - EndIf - StopVectorDrawing() + DrawPointer(@Drawing) + ;DrawScale(@Drawing) + ;- Display how many images in cache + VectorFont(FontID(PBMap\Font), 30) + VectorSourceColor(RGBA(0, 0, 0, 80)) + MovePathCursor(50,50) + DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) + MovePathCursor(50,80) + DrawVectorText(Str(ListSize(PBMap\TilesThreads()))) + StopVectorDrawing() ;Redraw ; If something was not correctly drawn, redraw after a while LockMutex(*SharedDrawing\Mutex) ;Be sure that we're not modifying variables while moving (seems not useful, but it is, especially to clean the semaphore) @@ -730,7 +814,7 @@ Module PBMap Repeat : Until TrySemaphore(*SharedDrawing\Semaphore) = 0 EndIf UnlockMutex(*SharedDrawing\Mutex) - Until Drawing\End + Until Drawing\End EndProcedure Procedure Refresh() @@ -949,7 +1033,7 @@ Module PBMap ;New move values PBMap\Position\x - MouseX PBMap\Position\y - MouseY - ;Fill parameters and signal the drawing thread + ;Fill parameters and send a signal to the drawing thread LockMutex(PBMap\Drawing\Mutex) ;PBMap tile position in tile.decimal PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize @@ -957,9 +1041,9 @@ Module PBMap PBMap\Drawing\PassNb = 1 XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) UnlockMutex(PBMap\Drawing\Mutex) + ;Start drawing + SignalSemaphore(PBMap\Drawing\Semaphore) EndIf - ;Start drawing - SignalSemaphore(PBMap\Drawing\Semaphore) ;If CallBackLocation send Location to function If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) @@ -1131,10 +1215,9 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::Quit() EndIf CompilerEndIf - ; IDE Options = PureBasic 5.42 LTS (Windows - x86) -; CursorPosition = 1120 -; FirstLine = 1077 +; CursorPosition = 478 +; FirstLine = 468 ; Folding = --------- ; EnableUnicode ; EnableThread From f2a3b06f7a6e97902eee3bdedfc433a64427ee71 Mon Sep 17 00:00:00 2001 From: djes Date: Fri, 26 Aug 2016 10:14:55 +0200 Subject: [PATCH 10/49] First changes --- PBMap.pb | 672 +++++++++++++++++++++++-------------------------------- 1 file changed, 281 insertions(+), 391 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 537af4d..e22d193 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -12,6 +12,8 @@ ; (see also Proxy Details) ;************************************************************** +#Red = 255 + CompilerIf #PB_Compiler_Thread = #False MessageRequester("Warning !!","You must enable ThreadSafe support in compiler options",#PB_MessageRequester_Ok ) End @@ -25,19 +27,18 @@ UsePNGImageEncoder() DeclareModule PBMap ;-Show debug infos - Global Verbose = #False - Global MyDebugLevel = 3 + #Red = 255 + Global Verbose = 1;#False + ;-Proxy ON/OFF Global Proxy = #False - Declare InitPBMap() - Declare SetMapServer(ServerURL.s="http://tile.openstreetmap.org/",TileSize.l=256,ZoomMin.l=0,ZoomMax.l=18) + Declare InitPBMap(window) Declare MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) Declare Event(Event.l) Declare SetLocation(latitude.d, longitude.d, zoom = 15, mode.i = #PB_Absolute) - Declare DrawingThread(Null) + Declare DrawingThread() Declare SetZoom(Zoom.i, mode.i = #PB_Relative) Declare ZoomToArea() - Declare SetCallBackLocation(CallBackLocation.i) - Declare SetCallBackMainPointer(CallBackMainPointer.i) + Declare SetCallBackLocation(*CallBackLocation) Declare LoadGpxFile(file.s); Declare AddMarker(Latitude.d,Longitude.d,color.l=-1, CallBackPointer.i = -1) Declare Quit() @@ -74,7 +75,6 @@ Module PBMap PBMapTileY.i PBMapZoom.i nImage.i - key.s GetImageThread.i EndStructure @@ -97,14 +97,13 @@ Module PBMap EndStructure Structure TileThread - *Tile.Tile GetImageThread.i + *Tile.Tile EndStructure Structure ImgMemCach nImage.i - ;Location.Location - ;Mutex.i + Usage.i EndStructure Structure TileMemCach @@ -117,48 +116,44 @@ Module PBMap CallBackPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) EndStructure - Structure Option - WheelMouseRelative.i - EndStructure - ;-PBMap Structure Structure PBMap + Window.i ; Parent Window Gadget.i ; Canvas Gadget Id Font.i ; Font to uses when write on the map TargetLocation.Location ; Latitude and Longitude from focus point Drawing.DrawingParameters ; Drawing parameters based on focus point - ; + ; CallBackLocation.i ; @Procedure(latitude.d,lontitude.d) - CallBackMainPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) - ; + ; Position.PixelPosition ; Actual focus point coords in pixels (global) MoveStartingPoint.PixelPosition ; Start mouse position coords when dragging the map - ; + ; ServerURL.s ; Web URL ex: http://tile.openstreetmap.org/ ZoomMin.i ; Min Zoom supported by server ZoomMax.i ; Max Zoom supported by server Zoom.i ; Current zoom TileSize.i ; Tile size downloaded on the server ex : 256 - ; + ; HDDCachePath.S ; Path where to load and save tiles downloaded from server MemCache.TileMemCach ; Images in memory cache - ; - Moving.i ; + ; + Moving.i Dirty.i ; To signal that drawing need a refresh - ; - MainDrawingThread.i ; - List TilesThreads.TileThread() ; - ; + ; + MainDrawingThread.i + List TilesThreads.TileThread() + TileThreadMutex.i; ;Mutex to protect resources List track.Location() ; To display a GPX track List Marker.Marker() ; To diplay marker - EditMarkerIndex.l ; - ; - Options.option ; + EditMarkerIndex.l + + ImgLoading.i ;Image Loading Tile EndStructure - ;-Global variables + #PB_MAP_REDRAW = #PB_EventType_FirstCustomValue + 1 + Global PBMap.PBMap, Null.i - Global LoadMutexTest = CreateMutex() ;Shows an error msg and terminates the program Procedure Error(msg.s) @@ -166,15 +161,14 @@ Module PBMap End EndProcedure - ;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 + ;Send debug infos to stdout + Procedure MyDebug(msg.s) + If Verbose PrintN(msg) - ;Debug msg EndIf EndProcedure - ;- *** CURL specific + ;- *** CURL specific *** ; (program has To be compiled in console format for curl debug infos) IncludeFile "libcurl.pbi" ; https://github.com/deseven/pbsamples/tree/master/crossplatform/libcurl @@ -187,8 +181,8 @@ Module PBMap Procedure.i CurlReceiveHTTPToFile(URL$, DestFileName$, ProxyURL$="", ProxyPort$="", ProxyUser$="", ProxyPassword$="") Protected *Buffer, curl.i, Timeout.i, res.i Protected FileHandle.i - MyDebug("ReceiveHTTPToFile from " + URL$ + " " + ProxyURL$ + ProxyPort$ + ProxyUser$, 3) - MyDebug(" to file : " + DestFileName$, 3) + MyDebug("ReceiveHTTPToFile from " + URL$ + " " + ProxyURL$ + ProxyPort$ + ProxyUser$) + MyDebug(" to file : " + DestFileName$) FileHandle = CreateFile(#PB_Any, DestFileName$) If FileHandle And Len(URL$) curl = curl_easy_init() @@ -221,11 +215,11 @@ Module PBMap curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @ReceiveHTTPWriteToFileFunction()) res = curl_easy_perform(curl) If res <> #CURLE_OK - MyDebug("CURL problem", 3) + MyDebug("CURL problem") EndIf curl_easy_cleanup(curl) Else - MyDebug("Can't init CURL", 3) + MyDebug("Can't init CURL") EndIf CloseFile(FileHandle) ProcedureReturn FileSize(DestFileName$) @@ -234,7 +228,7 @@ Module PBMap EndProcedure ;- *** - Procedure InitPBMap() + Procedure InitPBMap(window) Protected Result.i If Verbose OpenConsole() @@ -246,81 +240,75 @@ Module PBMap PBMap\MoveStartingPoint\x = - 1 PBMap\TileSize = 256 PBMap\Dirty = #False + PBMap\TileThreadMutex = CreateMutex() PBMap\Drawing\Mutex = CreateMutex() PBMap\Drawing\Semaphore = CreateSemaphore() - PBMap\EditMarkerIndex = -1 ;Initialised with "no marker selected" + PBMap\EditMarkerIndex = -1 ;<- You must initialize with No Marker selected PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold) - ;-Options - PBMap\Options\WheelMouseRelative = #True - ;-Preferences + PBMap\Window = window + ;- Proxy details ;Use this to create and customize your preferences file for the first time - ; CreatePreferences(GetHomeDirectory() + "PBMap.prefs") - ; ;Or this to modify - ; ;OpenPreferences(GetHomeDirectory() + "PBMap.prefs") - ; ;Or this - ; ;RunProgram("notepad.exe", GetHomeDirectory() + "PBMap.prefs", GetHomeDirectory()) - ; PreferenceGroup("PROXY") - ; WritePreferenceInteger("Proxy", #True) - ; WritePreferenceString("ProxyURL", "myproxy.fr") - ; WritePreferenceString("ProxyPort", "myproxyport") - ; WritePreferenceString("ProxyUser", "myproxyname") - ; WritePreferenceString("ProxyPass", "myproxypass") ;TODO !Warning! !not encoded! - ; ClosePreferences() - OpenPreferences(GetHomeDirectory() + "PBMap.prefs") - PreferenceGroup("PROXY") - Proxy = ReadPreferenceInteger("Proxy", #False) + ; Result = CreatePreferences(GetHomeDirectory() + "PBMap.prefs") + ; If Proxy + ; PreferenceGroup("PROXY") + ; WritePreferenceString("ProxyURL", "myproxy.fr") + ; WritePreferenceString("ProxyPort", "myproxyport") + ; WritePreferenceString("ProxyUser", "myproxyname") + ; EndIf + ; If Result + ; ClosePreferences() + ; EndIf + Result = OpenPreferences(GetHomeDirectory() + "PBMap.prefs") If Proxy + PreferenceGroup("PROXY") Global ProxyURL$ = ReadPreferenceString("ProxyURL", "") ;InputRequester("ProxyServer", "Do you use a Proxy Server? Then enter the full url:", "") Global ProxyPort$ = ReadPreferenceString("ProxyPort", "") ;InputRequester("ProxyPort" , "Do you use a specific port? Then enter it", "") Global ProxyUser$ = ReadPreferenceString("ProxyUser", "") ;InputRequester("ProxyUser" , "Do you use a user name? Then enter it", "") - Global ProxyPassword$ = InputRequester("ProxyPass", "Do you use a password ? Then enter it", "") ;TODO + Global ProxyPassword$ = InputRequester("ProxyPass", "Do you use a password ? Then enter it", "") EndIf - ClosePreferences() - curl_global_init(#CURL_GLOBAL_WIN32) + If Result + ClosePreferences() + EndIf + curl_global_init(#CURL_GLOBAL_WIN32); ;- Main drawing thread launching - PBMap\MainDrawingThread = CreateThread(@DrawingThread(), @PBMap\Drawing) - If PBMap\MainDrawingThread = 0 - Error("MapGadget : can't create main drawing thread.") + + ;PBMap\MainDrawingThread = CreateThread(@DrawingThread(), @PBMap\Drawing) + ;If PBMap\MainDrawingThread = 0 + ; Error("MapGadget : can't create main drawing thread.") + Protected Text$ = "Loading" + PBmap\ImgLoading = CreateImage(#PB_Any,256,256) + If PBmap\ImgLoading + StartVectorDrawing(ImageVectorOutput(PBMap\Imgloading)) + BeginVectorLayer() + VectorSourceColor(RGBA(255, 255, 255, 128)) + AddPathBox(0, 0, 256, 256) + FillPath() + MovePathCursor(0, 0) + VectorFont(FontID(PBMap\Font), 256 / 20) + VectorSourceColor(RGBA(150, 150, 150, 255)) + MovePathCursor(0 + (256 - VectorTextWidth(Text$)) / 2, 0 + (256 - VectorTextHeight(Text$)) / 2) + DrawVectorText(Text$) + EndVectorLayer() + StopVectorDrawing() EndIf - EndProcedure - - Procedure SetMapServer(ServerURL.s="http://tile.openstreetmap.org/",TileSize.l=256,ZoomMin.l=0,ZoomMax.l=18) - PBMap\ServerURL = ServerURL - PBMap\ZoomMin = ZoomMin - PBMap\ZoomMax = ZoomMax - PBMap\TileSize = TileSize + EndProcedure Procedure Quit() - Protected TimeCounter.q - ;Ask main drawing thread to stop and wait for it for 2 seconds - LockMutex(PBMap\Drawing\Mutex) + ;kill main drawing thread (nicer than KillThread(PBMap\MainDrawingThread)) + ;LockMutex(PBMap\Drawing\Mutex) PBMap\Drawing\End = #True - UnlockMutex(PBMap\Drawing\Mutex) - TimeCounter = ElapsedMilliseconds() - Repeat - Delay(100) - If ElapsedMilliseconds() - TimeCounter > 2000 - ;Should not occur - KillThread(PBMap\MainDrawingThread) + ;UnlockMutex(PBMap\Drawing\Mutex) + ;wait for loading threads to finish nicely + ResetList(PBMap\TilesThreads()) + While NextElement(PBMap\TilesThreads()) + If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 + FreeMemory(PBMap\TilesThreads()\Tile) + DeleteElement(PBMap\TilesThreads()) + ResetList( PBMap\TilesThreads()) EndIf - Until Not IsThread(PBMap\MainDrawingThread) - ;Wait for loading threads to finish nicely. Passed 2 seconds, kills them. - TimeCounter = ElapsedMilliseconds() - Repeat - ResetList(PBMap\TilesThreads()) - While NextElement(PBMap\TilesThreads()) - If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 - FreeMemory(PBMap\TilesThreads()\Tile) - DeleteElement(PBMap\TilesThreads()) - ElseIf ElapsedMilliseconds() - TimeCounter > 2000 - ;Should not occur - KillThread(PBMap\TilesThreads()\GetImageThread) - EndIf - Wend - Delay(10) - Until ListSize(PBMap\TilesThreads()) = 0 - curl_global_cleanup() + Wend + curl_global_cleanup() EndProcedure Macro Min(a,b) @@ -339,10 +327,10 @@ Module PBMap Procedure MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) If Gadget = #PB_Any - PBMap\Gadget = CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) ;#PB_Canvas_Keyboard has to be set for mousewheel to work on windows + PBMap\Gadget = CanvasGadget(PBMap\Gadget, X, Y, Width, Height) Else PBMap\Gadget = Gadget - CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) + CanvasGadget(PBMap\Gadget, X, Y, Width, Height) EndIf EndProcedure @@ -400,7 +388,7 @@ Module PBMap x2 = (PBMap\TargetLocation\Longitude+180)*(mapWidth/360) ; convert from degrees To radians latRad = PBMap\TargetLocation\Latitude*#PI/180; - ; get y value + ; get y value mercN = Log(Tan((#PI/4)+(latRad/2))) y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)); *Pixel\x=GadgetWidth(PBMap\Gadget)/2 - (x2-x1) @@ -437,65 +425,62 @@ Module PBMap EndIf EndProcedure - Procedure.i GetTileFromMem(key.s) - MyDebug("Check if we have this image in memory", 3) - If FindMapElement(PBMap\MemCache\Images(), key) - MyDebug("Key : " + key + " found !", 3) - ProcedureReturn PBMap\MemCache\Images()\nImage - Else - MyDebug("Key : " + key + " not found !", 3) - ProcedureReturn -1 - EndIf - EndProcedure - Procedure.i GetTileFromHDD(CacheFile.s) - Protected nImage.i - Debug "Loading image " + CacheFile + " ; Size : " + Str(FileSize(CacheFile)) + Protected nImage.i If FileSize(CacheFile) > 0 nImage = LoadImage(#PB_Any, CacheFile) If IsImage(nImage) - Debug "Success loading " + CacheFile + " as nImage " + Str(nImage) - MyDebug("Success loading " + CacheFile + " as nImage " + Str(nImage), 3) + MyDebug("Loadimage " + CacheFile + " -> Success !") ProcedureReturn nImage - Else - Debug "Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !" - MyDebug("Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !", 3) EndIf - Else - Debug "Failed loading " + CacheFile + " -> Size = 0" - MyDebug("Failed loading " + CacheFile + " -> Size = 0", 3) EndIf + MyDebug("Loadimage " + CacheFile + " -> Failed !") ProcedureReturn -1 EndProcedure + Procedure.i GetTileFromMem(Zoom.i, XTile.i, YTile.i) + Protected key.s = "Z" + RSet(Str(Zoom), 4, "0") + "X" + RSet(Str(XTile), 8, "0") + "Y" + RSet(Str(YTile), 8, "0") + MyDebug("Check if we have this image in memory") + If FindMapElement(PBMap\MemCache\Images(), key) + MyDebug("Key : " + key + " found !") + ProcedureReturn PBMap\MemCache\Images()\nImage + Else + Protected CacheFile.s = PBMap\HDDCachePath + "PBMap_" + Str(Zoom) + "_" + Str(XTile) + "_" + Str(YTile) + ".png" + MyDebug("Key : " + key + " Try HDD!") + ProcedureReturn GetTileFromHDD(CacheFile.s) + EndIf + EndProcedure + Procedure.i GetTileFromWeb(Zoom.i, XTile.i, YTile.i, CacheFile.s) Protected *Buffer Protected nImage.i = -1 Protected FileHandle.i Protected TileURL.s = PBMap\ServerURL + Str(Zoom) + "/" + Str(XTile) + "/" + Str(YTile) + ".png" - MyDebug("Check if we have this image on Web", 3) + MyDebug("Check if we have this image on Web") If Proxy FileHandle = CurlReceiveHTTPToFile(TileURL, CacheFile, ProxyURL$, ProxyPort$, ProxyUser$, ProxyPassword$) If FileHandle nImage = GetTileFromHDD(CacheFile) Else - MyDebug("File " + TileURL + " not correctly received with Curl and proxy", 3) + MyDebug("File " + TileURL + " not correctly received with Curl and proxy") EndIf Else *Buffer = ReceiveHTTPMemory(TileURL) ;TODO to thread by using #PB_HTTP_Asynchronous If *Buffer nImage = CatchImage(#PB_Any, *Buffer, MemorySize(*Buffer)) If IsImage(nImage) - MyDebug("Load from web " + TileURL + " as Tile nb " + nImage, 3) + MyDebug("Load from web " + TileURL + " as Tile nb " + nImage) + Debug "url: " + TileURL + Debug "cache file: " + CacheFile SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG) FreeMemory(*Buffer) Else - MyDebug("Can't catch image " + TileURL, 3) + MyDebug("Can't catch image " + TileURL) nImage = -1 ;ShowMemoryViewer(*Buffer, MemorySize(*Buffer)) EndIf Else - MyDebug("ReceiveHTTPMemory's buffer is empty", 3) + Debug("ReceiveHTTPMemory's buffer is empty") EndIf EndIf ProcedureReturn nImage @@ -503,37 +488,41 @@ Module PBMap Procedure GetImageThread(*Tile.Tile) Protected nImage.i = -1 + LockMutex(PBMap\TileThreadMutex) + Protected key.s = "Z" + RSet(Str(*Tile\PBMapZoom), 4, "0") + "X" + RSet(Str(*Tile\PBMapTileX), 8, "0") + "Y" + RSet(Str(*Tile\PBMapTileY), 8, "0") Protected CacheFile.s = PBMap\HDDCachePath + "PBMap_" + Str(*Tile\PBMapZoom) + "_" + Str(*Tile\PBMapTileX) + "_" + Str(*Tile\PBMapTileY) + ".png" - Protected Tile.position -; PBMap\MemCache\Images(*Tile\key)\Mutex = CreateMutex() -; LockMutex(PBMap\MemCache\Images(*Tile\key)\Mutex) -; LockMutex(LoadMutexTest) - nImage = GetTileFromHDD(CacheFile) -; UnlockMutex(LoadMutexTest) - If nImage = -1 + ;Adding the image to the cache if possible + + AddMapElement(PBMap\MemCache\Images(), key) + PBMap\MemCache\Images()\nImage= PBMap\ImgLoading + + UnlockMutex(PBMap\TileThreadMutex) + ;nImage = GetTileFromHDD(CacheFile) + ;If nImage = -1 + ; LockMutex(PBMap\TileThreadMutex) nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, CacheFile) - If nImage = -1 - MyDebug("Error GetImageThread procedure, image not loaded - " + *Tile\key, 3) - *Tile\nImage = -1 - ProcedureReturn - EndIf + ;UnlockMutex(PBMap\TileThreadMutex) + ;EndIf + If nImage <> -1 + LockMutex(PBMap\TileThreadMutex) + PBMap\MemCache\Images(key)\nImage = nImage + UnlockMutex(PBMap\TileThreadMutex) + MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") + MyDebug("With the following key : " + key) + Else + MyDebug("Error GetImageThread procedure, image not loaded - " + key) + nImage = -1 EndIf - PBMap\MemCache\Images(*Tile\key)\nImage = nImage -; Tile\x=*Tile\PBMapTileX -; Tile\y=*Tile\PBMapTiley -; XY2LatLon(@Tile,@PBMap\MemCache\Images(*Tile\key)\Location) - MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache", 3) - MyDebug("With the following key : " + *Tile\key, 3) ;Define this tile image nb *Tile\nImage = nImage - ; UnlockMutex(PBMap\MemCache\Images(key)\Mutex) + PostEvent(#PB_Event_Gadget,PBMap\window,PBmap\Gadget,#PB_MAP_REDRAW,*tile) EndProcedure Procedure DrawTile(*Tile.Tile) Protected x = *Tile\Position\x Protected y = *Tile\Position\y - MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) - MyDebug(" at coords " + Str(x) + "," + Str(y), 2) + MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX)) + MyDebug(" at coords " + Str(x) + "," + Str(y)) MovePathCursor(x, y) DrawVectorImage(ImageID(*Tile\nImage)) EndProcedure @@ -542,140 +531,93 @@ Module PBMap Protected x = *Tile\Position\x Protected y = *Tile\Position\y Protected Text$ = "Loading" - MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) - MyDebug(" at coords " + Str(x) + "," + Str(y), 2) - BeginVectorLayer() - ;MovePathCursor(x, y) - VectorSourceColor(RGBA(255, 255, 255, 128)) - AddPathBox(x, y, PBMap\TileSize, PBMap\TileSize) - FillPath() - MovePathCursor(x, y) - VectorFont(FontID(PBMap\Font), PBMap\TileSize / 20) - VectorSourceColor(RGBA(150, 150, 150, 255)) - MovePathCursor(x + (PBMap\TileSize - VectorTextWidth(Text$)) / 2, y + (PBMap\TileSize - VectorTextHeight(Text$)) / 2) - DrawVectorText(Text$) - EndVectorLayer() + MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX)) + MyDebug(" at coords " + Str(x) + "," + Str(y)) +; BeginVectorLayer() +; ;MovePathCursor(x, y) +; VectorSourceColor(RGBA(255, 255, 255, 128)) +; AddPathBox(x, y, PBMap\TileSize, PBMap\TileSize) +; FillPath() +; MovePathCursor(x, y) +; VectorFont(FontID(PBMap\Font), PBMap\TileSize / 20) +; VectorSourceColor(RGBA(150, 150, 150, 255)) +; MovePathCursor(x + (PBMap\TileSize - VectorTextWidth(Text$)) / 2, y + (PBMap\TileSize - VectorTextHeight(Text$)) / 2) +; DrawVectorText(Text$) +; EndVectorLayer() EndProcedure Procedure DrawTiles(*Drawing.DrawingParameters) + ;DisableDebugger Protected x.i, y.i Protected tx = Int(*Drawing\Position\x) ;Don't forget the Int() ! Protected ty = Int(*Drawing\Position\y) Protected nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point Protected ny = *Drawing\CenterY / PBMap\TileSize - MyDebug("Drawing tiles", 2) + Protected px,py,img + + MyDebug("Drawing tiles") For y = - ny - 1 To ny + 1 For x = - nx - 1 To nx + 1 - ;Was quiting the loop if a move occured, giving maybe smoother movement - ;If PBMap\Moving - ; Break 2 - ;EndIf - ;Store parameters in only one memory place, and give it to the thread if needed - Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) - If *NewTile - With *NewTile - ;Keep a track of tiles, and eventually associated threads, to free memory + px = *Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX + py = *Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY + img = GetTileFromMem(PBMap\Zoom,tx+x,ty+y) + If img <> -1 + MovePathCursor(px,py) + DrawVectorImage(ImageID(img)) + Else + MovePathCursor(px,py) + DrawVectorImage(ImageID(PBMap\ImgLoading)) + + Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) + If *NewTile + With *NewTile + ;Keep a track of tiles (especially to free memory) AddElement(PBMap\TilesThreads()) PBMap\TilesThreads()\Tile = *NewTile ;New tile parameters - \Position\x = *Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX - \Position\y = *Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY + \Position\x = px ;*Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX + \Position\y = py ;*Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY \PBMapTileX = tx + x \PBMapTileY = ty + y \PBMapZoom = PBMap\Zoom - \key = "Z" + RSet(Str(\PBMapZoom), 4, "0") + "X" + RSet(Str(\PBMapTileX), 8, "0") + "Y" + RSet(Str(\PBMapTileY), 8, "0") ;Unique identifier - ;Check if the image exists - \nImage = GetTileFromMem(\key) - If \nImage = -1 - ;If not, load it in the background - If AddMapElement(PBMap\MemCache\Images(), \key) ;Add the image to the cache, once in this loop - \GetImageThread = CreateThread(@GetImageThread(), *NewTile) - PBMap\TilesThreads()\GetImageThread = \GetImageThread - MyDebug(" Creating get image thread nb " + Str(\GetImageThread), 2) - Else - MyDebug(" Can't add a new image to the map list", 2) - CallDebugger - EndIf - EndIf - If IsImage(\nImage) - DrawTile(*NewTile) - Else - MyDebug("Image missing", 2) - DrawLoading(*NewTile) - *Drawing\Dirty = #True ;Signals that this image is missing so we should have to redraw - EndIf - EndWith - Else - MyDebug(" Error, can't create a new tile", 2) - Break 2 + \GetImageThread = CreateThread(@GetImageThread(), *NewTile) + PBMap\TilesThreads()\GetImageThread = \GetImageThread + myDebug(" Creating get image thread nb " + Str(\GetImageThread)) + ; DrawLoading(*NewTile) + ;*Drawing\Dirty = #True ;Signals that this image is missing so we should have to redraw + EndWith + Else + MyDebug(" Error, can't create a new tile") + Break 2 + EndIf EndIf Next Next - ;Free tile memory + ;Free tile memory when the loading thread has finished ;TODO : get out this proc from drawtiles in a special "free ressources" task ForEach PBMap\TilesThreads() - ;Check if there's no more loading thread If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 FreeMemory(PBMap\TilesThreads()\Tile) DeleteElement(PBMap\TilesThreads()) EndIf Next -; ;-**** Clean Mem Cache -; ;TODO in development, by now there's many cache problem as the loading thread could be perturbed -; ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize -; Protected MaxNbTile.l -; If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) -; MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize -; Else -; MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize -; EndIf -; Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) -; Protected Limit.d=Scale*(MaxNbTile)*1.5 -; Debug "Cache cleaning" -; ForEach PBMap\MemCache\Images() -; Protected Distance.d = HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) -; Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) -; If Distance>Limit And IsImage(PBMap\MemCache\Images()\nImage) -; LockMutex(PBMap\MemCache\Images()\Mutex) -; Debug "delete" -; Debug PBMap\MemCache\Images() -; FreeImage(PBMap\MemCache\Images()\nImage) -; UnlockMutex(PBMap\MemCache\Images()\Mutex) -; FreeMutex(PBMap\MemCache\Images()\Mutex) -; DeleteMapElement(PBMap\MemCache\Images()) -; EndIf -; Next - + ;EnableDebugger EndProcedure - Procedure DrawPointer(*Drawing.DrawingParameters) - If PBMap\CallBackMainPointer > 0 - ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) - CallFunctionFast(PBMap\CallBackMainPointer, *Drawing\CenterX, *Drawing\CenterY) - Else - VectorSourceColor(RGBA($FF, 0, 0, $FF)) - MovePathCursor(*Drawing\CenterX, *Drawing\CenterY) - AddPathLine(-8, -16, #PB_Path_Relative) - AddPathCircle(8, 0, 8, 180, 0, #PB_Path_Relative) - AddPathLine(-8, 16, #PB_Path_Relative) - ;FillPath(#PB_Path_Preserve) - ;ClipPath(#PB_Path_Preserve) - AddPathCircle(0, -16, 5, 0, 360, #PB_Path_Relative) - VectorSourceColor(RGBA($FF, 0, 0, $FF)) - FillPath(#PB_Path_Preserve):VectorSourceColor(RGBA($FF, 0, 0, $FF));RGBA(0, 0, 0, 255)) - StrokePath(1) - EndIf + Procedure Pointer(x.i, y.i, color.l = #Red) + color=RGBA(255, 0, 0, 255) + VectorSourceColor(color) + MovePathCursor(x, y) + AddPathLine(-8,-16,#PB_Path_Relative) + AddPathCircle(8,0,8,180,0,#PB_Path_Relative) + AddPathLine(-8,16,#PB_Path_Relative) + ;FillPath(#PB_Path_Preserve) + ;ClipPath(#PB_Path_Preserve) + AddPathCircle(0,-16,5,0,360,#PB_Path_Relative) + VectorSourceColor(color) + FillPath(#PB_Path_Preserve):VectorSourceColor(RGBA(0, 0, 0, 255)):StrokePath(1) EndProcedure - Procedure DrawScale(*Drawing.DrawingParameters) - ;TODO Add Option and function to display Scale on Map - Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) - VectorFont(FontID(PBMap\Font), 30) - VectorSourceColor(RGBA(0, 0, 0, 80)) - MovePathCursor(50,50) - DrawVectorText(StrD(Scale)) - EndProcedure - Procedure TrackPointer(x.i, y.i,dist.l) Protected color.l color=RGBA(0, 0, 0, 255) @@ -701,7 +643,7 @@ Module PBMap Protected km.f, memKm.i If ListSize(PBMap\track())>0 ;Trace Track - LockMutex(PBMap\Drawing\Mutex) + ; LockMutex(PBMap\Drawing\Mutex) ForEach PBMap\track() If *Drawing\TargetLocation\Latitude<>0 And *Drawing\TargetLocation\Longitude<>0 GetPixelCoordFromLocation(@PBMap\track(),@Pixel) @@ -716,7 +658,7 @@ Module PBMap StrokePath(10, #PB_Path_RoundEnd|#PB_Path_RoundCorner) ;Draw Distance ForEach PBMap\track() - ;Distance test + ;-Test Distance If ListIndex(PBMap\track())=0 Location\Latitude=PBMap\track()\Latitude Location\Longitude=PBMap\track()\Longitude @@ -735,7 +677,7 @@ Module PBMap EndIf EndIf Next - UnlockMutex(PBMap\Drawing\Mutex) + ; UnlockMutex(PBMap\Drawing\Mutex) EndIf EndProcedure @@ -758,7 +700,7 @@ Module PBMap If PBMap\Marker()\CallBackPointer > 0 CallFunctionFast(PBMap\Marker()\CallBackPointer, Pixel\X, Pixel\Y) Else - DrawPointer(*Drawing) + Pointer(Pixel\X, Pixel\Y, PBMap\Marker()\color) EndIf EndIf EndIf @@ -767,58 +709,51 @@ Module PBMap ;-*** Main drawing thread ; always running, waiting for a semaphore to start refreshing - Procedure DrawingThread(*SharedDrawing.DrawingParameters) - Protected Drawing.DrawingParameters + Procedure DrawingThread();*SharedDrawing.DrawingParameters) + Protected *Drawing.DrawingParameters = @PBMap\Drawing Protected Px.d, Py.d - Repeat - WaitSemaphore(*SharedDrawing\Semaphore) - MyDebug("--------- Main drawing thread ------------") + ;Repeat + ; WaitSemaphore(*SharedDrawing\Semaphore) + ; MyDebug("--------- Main drawing thread ------------") ;Creates a copy of the structure to work with to avoid multiple mutex locks - LockMutex(*SharedDrawing\Mutex) - CopyStructure(*SharedDrawing, @Drawing, DrawingParameters) - UnlockMutex(*SharedDrawing\Mutex) + ; LockMutex(*SharedDrawing\Mutex) + ; CopyStructure(*SharedDrawing, @Drawing, DrawingParameters) + ; UnlockMutex(*SharedDrawing\Mutex) ;Precalc some values - Drawing\CenterX = GadgetWidth(PBMap\Gadget) / 2 - Drawing\CenterY = GadgetHeight(PBMap\Gadget) / 2 + *Drawing\CenterX = GadgetWidth(PBMap\Gadget) / 2 + *Drawing\CenterY = GadgetHeight(PBMap\Gadget) / 2 ;Pixel shift, aka position in the tile - Px = Drawing\Position\x : Py = Drawing\Position\y - Drawing\DeltaX = Px * PBMap\TileSize - (Int(Px) * PBMap\TileSize) ;Don't forget the Int() ! - Drawing\DeltaY = Py * PBMap\TileSize - (Int(Py) * PBMap\TileSize) - Drawing\TargetLocation\Latitude = PBMap\TargetLocation\Latitude - Drawing\TargetLocation\Longitude = PBMap\TargetLocation\Longitude - Drawing\Dirty = #False + Px = *Drawing\Position\x : Py = *Drawing\Position\y + *Drawing\DeltaX = Px * PBMap\TileSize - (Int(Px) * PBMap\TileSize) ;Don't forget the Int() ! + *Drawing\DeltaY = Py * PBMap\TileSize - (Int(Py) * PBMap\TileSize) + *Drawing\TargetLocation\Latitude = PBMap\TargetLocation\Latitude + *Drawing\TargetLocation\Longitude = PBMap\TargetLocation\Longitude + ;Drawing\Dirty = #False ;Main drawing stuff StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget)) - DrawTiles(@Drawing) - DrawTrack(@Drawing) - DrawMarker(@Drawing) - DrawPointer(@Drawing) - ;DrawScale(@Drawing) - ;- Display how many images in cache - VectorFont(FontID(PBMap\Font), 30) - VectorSourceColor(RGBA(0, 0, 0, 80)) - MovePathCursor(50,50) - DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) - MovePathCursor(50,80) - DrawVectorText(Str(ListSize(PBMap\TilesThreads()))) - StopVectorDrawing() + DrawTiles(*Drawing) + ;DrawTrack(*Drawing) + ;DrawMarker(*Drawing) + ;Pointer(*Drawing\CenterX, *Drawing\CenterY, #Red) + StopVectorDrawing() ;Redraw ; If something was not correctly drawn, redraw after a while - LockMutex(*SharedDrawing\Mutex) ;Be sure that we're not modifying variables while moving (seems not useful, but it is, especially to clean the semaphore) - If Drawing\Dirty - MyDebug("Something was dirty ! We try again to redraw") - Drawing\PassNb + 1 - SignalSemaphore(*SharedDrawing\Semaphore) - Else - ;Clean the semaphore to avoid multiple unuseful redraws - Repeat : Until TrySemaphore(*SharedDrawing\Semaphore) = 0 - EndIf - UnlockMutex(*SharedDrawing\Mutex) - Until Drawing\End + ; LockMutex(*SharedDrawing\Mutex) ;Be sure that we're not modifying variables while moving (seems not useful, but it is, especially to clean the semaphore) + ; If Drawing\Dirty + ; MyDebug("Something was dirty ! We try again to redraw") + ; Drawing\PassNb + 1 + ; SignalSemaphore(*SharedDrawing\Semaphore) + ; Else + ; ;Clean the semaphore to avoid multiple unuseful redraws + ; Repeat : Until TrySemaphore(*SharedDrawing\Semaphore) = 0 + ; EndIf + ;UnlockMutex(*SharedDrawing\Mutex) + ;Until Drawing\End EndProcedure Procedure Refresh() - SignalSemaphore(PBMap\Drawing\Semaphore) + DrawingThread() + ;SignalSemaphore(PBMap\Drawing\Semaphore) EndProcedure Procedure SetLocation(latitude.d, longitude.d, zoom = 15, Mode.i = #PB_Absolute) @@ -840,7 +775,8 @@ Module PBMap PBMap\Position\y = PBMap\Drawing\Position\y * PBMap\TileSize PBMap\Drawing\PassNb = 1 ;Start drawing - SignalSemaphore(PBMap\Drawing\Semaphore) + ;SignalSemaphore(PBMap\Drawing\Semaphore) + DrawingThread() ;*** If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) @@ -903,88 +839,49 @@ Module PBMap PBMap\Position\X = PBMap\Drawing\Position\x * PBMap\TileSize PBMap\Position\Y = PBMap\Drawing\Position\y * PBMap\TileSize ;*** Creates a drawing thread and fill parameters - PBMap\Drawing\PassNb = 1 + ;PBMap\Drawing\PassNb = 1 ;Start drawing - SignalSemaphore(PBMap\Drawing\Semaphore) + ;SignalSemaphore(PBMap\Drawing\Semaphore) + DrawingThread() ;*** If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) EndIf - 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 - ;Fast and dirty code - OldPx = PBMap\Position\x : OldPy = PBMap\Position\y - OldMx = OldPx + GadgetWidth(PBMap\Gadget) / 2 - x - OldMy = OldPy + GadgetHeight(PBMap\Gadget) / 2 - y - PBMap\Zoom = PBMap\Zoom + zoom - If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf - If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf - ;Centered Zoom - LockMutex(PBMap\Drawing\Mutex) - LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing) - ;Convert X, Y in tile.decimal into real pixels - PBMap\Position\x = PBMap\Drawing\Position\x * PBMap\TileSize - PBMap\Position\y = PBMap\Drawing\Position\y * PBMap\TileSize - MouseX = PBMap\Position\x + GadgetWidth(PBMap\Gadget) / 2 - x - MouseY = PBMap\Position\y + GadgetHeight(PBMap\Gadget) / 2 - y - ;Cross-multiply to get the new center - PBMap\Position\x = (OldPx * MouseX) / OldMx - PBMap\Position\y = (OldPy * MouseY) / OldMy - ;PBMap tile position in tile.decimal - PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize - PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize - PBMap\Drawing\PassNb = 1 - XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) - UnlockMutex(PBMap\Drawing\Mutex) - ;Start drawing - SignalSemaphore(PBMap\Drawing\Semaphore) - ;If CallBackLocation send Location to function - If PBMap\CallBackLocation > 0 - CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) - EndIf - EndProcedure + EndProcedure Procedure SetCallBackLocation(CallBackLocation.i) PBMap\CallBackLocation = CallBackLocation EndProcedure - Procedure SetCallBackMainPointer(CallBackMainPointer.i) - PBMap\CallBackMainPointer = CallBackMainPointer - EndProcedure - Procedure.d GetLatitude() Protected Value.d - LockMutex(PBMap\Drawing\Mutex) + ; LockMutex(PBMap\Drawing\Mutex) Value = PBMap\TargetLocation\Latitude - UnlockMutex(PBMap\Drawing\Mutex) + ; UnlockMutex(PBMap\Drawing\Mutex) ProcedureReturn Value EndProcedure Procedure.d GetLongitude() Protected Value.d - LockMutex(PBMap\Drawing\Mutex) + ; LockMutex(PBMap\Drawing\Mutex) Value = PBMap\TargetLocation\Longitude - UnlockMutex(PBMap\Drawing\Mutex) + ; UnlockMutex(PBMap\Drawing\Mutex) ProcedureReturn Value EndProcedure Procedure.i GetZoom() Protected Value.d - LockMutex(PBMap\Drawing\Mutex) + ; LockMutex(PBMap\Drawing\Mutex) Value = PBMap\Zoom - UnlockMutex(PBMap\Drawing\Mutex) + ; UnlockMutex(PBMap\Drawing\Mutex) ProcedureReturn Value EndProcedure - Procedure Event(Event.l) Protected Gadget.i Protected MouseX.i, MouseY.i Protected Marker.Position + Protected *Drawing.DrawingParameters If IsGadget(PBMap\Gadget) And GadgetType(PBMap\Gadget) = #PB_GadgetType_Canvas Select Event Case #PB_Event_Gadget ;{ @@ -992,14 +889,6 @@ Module PBMap Select Gadget Case PBMap\Gadget Select EventType() - 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)) - Else - ;Absolute zoom (centered on the center of the map) - SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta), #PB_Relative) - EndIf Case #PB_EventType_LeftButtonDown ;Check if we select a marker MouseX = PBMap\Position\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) @@ -1017,12 +906,11 @@ Module PBMap PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) Case #PB_EventType_MouseMove - ;If a move has been initiated by a left click 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 PBMap\Moving = #True - ;If it's marker move + ;move Marker If PBMap\EditMarkerIndex > -1 SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) LatLon2XY(@PBMap\Marker()\Location, @Marker) @@ -1033,39 +921,43 @@ Module PBMap ;New move values PBMap\Position\x - MouseX PBMap\Position\y - MouseY - ;Fill parameters and send a signal to the drawing thread - LockMutex(PBMap\Drawing\Mutex) + ;-*** Fill parameters and signal the drawing thread + ; LockMutex(PBMap\Drawing\Mutex) ;PBMap tile position in tile.decimal PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize PBMap\Drawing\PassNb = 1 XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) - UnlockMutex(PBMap\Drawing\Mutex) - ;Start drawing - SignalSemaphore(PBMap\Drawing\Semaphore) + ;If CallBackLocation send Location to function + If PBMap\CallBackLocation > 0 + CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) + EndIf + ; UnlockMutex(PBMap\Drawing\Mutex) EndIf - ;If CallBackLocation send Location to function - If PBMap\CallBackLocation > 0 - CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) - EndIf + ;Start drawing + ;SignalSemaphore(PBMap\Drawing\Semaphore) + DrawingThread() + ;- *** PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) + ; DrawingThread() EndIf Case #PB_EventType_LeftButtonUp PBMap\Moving = #False PBMap\MoveStartingPoint\x = - 1 - ;Stop marker move If PBMap\EditMarkerIndex > -1 PBMap\EditMarkerIndex = -1 - Else - ;Stop map move - LockMutex(PBMap\Drawing\Mutex) + Else ;Move Map + ;LockMutex(PBMap\Drawing\Mutex) PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize MyDebug("PBMap\Drawing\Position\x " + Str(PBMap\Drawing\Position\x) + " ; PBMap\Drawing\Position\y " + Str(PBMap\Drawing\Position\y) ) XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) - UnlockMutex(PBMap\Drawing\Mutex) + ; UnlockMutex(PBMap\Drawing\Mutex) + DrawingThread() EndIf + Case #PB_MAP_REDRAW + DrawingThread() EndSelect EndSelect EndSelect @@ -1125,11 +1017,6 @@ CompilerIf #PB_Compiler_IsMainFile FillPath(#PB_Path_Preserve):VectorSourceColor(RGBA(0, 0, 0, 255)):StrokePath(1) EndProcedure - Procedure MainPointer(x.i, y.i) - VectorSourceColor(RGBA(255, 255,255, 255)):AddPathCircle(x, y,32):StrokePath(1) - VectorSourceColor(RGBA(0,0,0, 255)):AddPathCircle(x, y,29):StrokePath(2) - EndProcedure - Procedure ResizeAll() ResizeGadget(#Map,10,10,WindowWidth(#Window_0)-198,WindowHeight(#Window_0)-59) ResizeGadget(#Text_1,WindowWidth(#Window_0)-170,#PB_Ignore,#PB_Ignore,#PB_Ignore) @@ -1150,11 +1037,12 @@ CompilerIf #PB_Compiler_IsMainFile EndProcedure ;- MAIN TEST + OpenConsole() If OpenWindow(#Window_0, 260, 225, 700, 571, "PBMap", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered | #PB_Window_SizeGadget) - LoadFont(0, "Wingdings", 12) + LoadFont(0, "Arial", 12) LoadFont(1, "Arial", 12, #PB_Font_Bold) - + TextGadget(#Text_1, 530, 50, 60, 15, "Movements") ButtonGadget(#Gdt_Left, 550, 100, 30, 30, Chr($E7)) : SetGadgetFont(#Gdt_Left, FontID(0)) ButtonGadget(#Gdt_Right, 610, 100, 30, 30, Chr($E8)) : SetGadgetFont(#Gdt_Right, FontID(0)) @@ -1174,12 +1062,11 @@ CompilerIf #PB_Compiler_IsMainFile Define pfValue.d ;Our main gadget - PBMap::InitPBMap() + PBMap::InitPBMap(#Window_0) PBMap::MapGadget(#Map, 10, 10, 512, 512) PBMap::SetCallBackLocation(@UpdateLocation()) - PBMap::SetCallBackMainPointer(@MainPointer()) ;To change the Main Pointer - PBMap::SetLocation(49.04599, 2.03347, 17) - PBMap::AddMarker(49.0446828398, 2.0349812508, -1, @MyPointer()) + PBMap::SetLocation(-36.8485,174.7633,10) + ;PBMap::AddMarker(49.0446828398, 2.0349812508, -1, @MyPointer()) Repeat Event = WaitWindowEvent() @@ -1202,10 +1089,10 @@ CompilerIf #PB_Compiler_IsMainFile Case #Button_5 PBMap::SetZoom( - 1) Case #Gdt_LoadGpx - PBMap::LoadGpxFile(OpenFileRequester("Choose a file to load", "", "*.gpx", 0)) - PBMap::ZoomToArea() ; <-To center the view, and zoom on the tracks + PBMap::LoadGpxFile(OpenFileRequester("Choisissez un fichier � charger", "", "*.gpx", 0)) + PBMap::ZoomToArea() ; <-To center the view, and to viex all the track Case #Gdt_AddMarker - PBMap:: AddMarker(ValD(GetGadgetText(#String_0)), ValD(GetGadgetText(#String_1)), RGBA(Random(255), Random(255), Random(255),255)) + PBMap:: AddMarker(ValD(GetGadgetText(#String_0)),ValD(GetGadgetText(#String_1)),RGBA(Random(255),Random(255),Random(255),255)) EndSelect Case #PB_Event_SizeWindow ResizeAll() @@ -1214,11 +1101,14 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::Quit() EndIf + + CloseConsole() + CompilerEndIf ; IDE Options = PureBasic 5.42 LTS (Windows - x86) -; CursorPosition = 478 -; FirstLine = 468 -; Folding = --------- +; CursorPosition = 1073 +; FirstLine = 1062 +; Folding = -------- ; EnableUnicode ; EnableThread ; EnableXP \ No newline at end of file From bc94b653b6b6f5ddbb583ab191defb0d31fa736f Mon Sep 17 00:00:00 2001 From: djes Date: Fri, 26 Aug 2016 12:06:18 +0200 Subject: [PATCH 11/49] Latest idle and djes code mixing --- PBMap.pb | 459 ++++++++++++++++++------------------------------------- 1 file changed, 149 insertions(+), 310 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 95d1dab..403fbbd 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -77,6 +77,7 @@ Module PBMap PBMapZoom.i nImage.i key.s + CacheFile.s GetImageThread.i EndStructure @@ -119,6 +120,10 @@ Module PBMap CallBackPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) EndStructure + Structure Option + WheelMouseRelative.i + EndStructure + ;-PBMap Structure Structure PBMap Window.i ; Parent Window @@ -128,6 +133,7 @@ Module PBMap Drawing.DrawingParameters ; Drawing parameters based on focus point ; CallBackLocation.i ; @Procedure(latitude.d,lontitude.d) + CallBackMainPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) ; Position.PixelPosition ; Actual focus point coords in pixels (global) MoveStartingPoint.PixelPosition ; Start mouse position coords when dragging the map @@ -151,7 +157,9 @@ Module PBMap List Marker.Marker() ; To diplay marker EditMarkerIndex.l - ImgLoading.i ;Image Loading Tile + ImgLoading.i ;Image Loading Tile + + Options.option ; EndStructure #PB_MAP_REDRAW = #PB_EventType_FirstCustomValue + 1 @@ -251,84 +259,69 @@ Module PBMap PBMap\EditMarkerIndex = -1 ;Initialised with "no marker selected" PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold) PBMap\Window = window - ;- Proxy details + PBMap\Options\WheelMouseRelative = #True + ;-Options + ;-Preferences ;Use this to create and customize your preferences file for the first time - ; Result = CreatePreferences(GetHomeDirectory() + "PBMap.prefs") - ; If Proxy - ; PreferenceGroup("PROXY") - ; WritePreferenceString("ProxyURL", "myproxy.fr") - ; WritePreferenceString("ProxyPort", "myproxyport") - ; WritePreferenceString("ProxyUser", "myproxyname") - ; EndIf - ; If Result - ; ClosePreferences() - ; EndIf - Result = OpenPreferences(GetHomeDirectory() + "PBMap.prefs") + ; CreatePreferences(GetHomeDirectory() + "PBMap.prefs") + ; ;Or this to modify + ; ;OpenPreferences(GetHomeDirectory() + "PBMap.prefs") + ; ;Or this + ; ;RunProgram("notepad.exe", GetHomeDirectory() + "PBMap.prefs", GetHomeDirectory()) + ; PreferenceGroup("PROXY") + ; WritePreferenceInteger("Proxy", #True) + ; WritePreferenceString("ProxyURL", "myproxy.fr") + ; WritePreferenceString("ProxyPort", "myproxyport") + ; WritePreferenceString("ProxyUser", "myproxyname") + ; WritePreferenceString("ProxyPass", "myproxypass") ;TODO !Warning! !not encoded! + ; ClosePreferences() + OpenPreferences(GetHomeDirectory() + "PBMap.prefs") + PreferenceGroup("PROXY") + Proxy = ReadPreferenceInteger("Proxy", #False) If Proxy - PreferenceGroup("PROXY") Global ProxyURL$ = ReadPreferenceString("ProxyURL", "") ;InputRequester("ProxyServer", "Do you use a Proxy Server? Then enter the full url:", "") Global ProxyPort$ = ReadPreferenceString("ProxyPort", "") ;InputRequester("ProxyPort" , "Do you use a specific port? Then enter it", "") Global ProxyUser$ = ReadPreferenceString("ProxyUser", "") ;InputRequester("ProxyUser" , "Do you use a user name? Then enter it", "") - Global ProxyPassword$ = InputRequester("ProxyPass", "Do you use a password ? Then enter it", "") - EndIf - If Result - ClosePreferences() + Global ProxyPassword$ = InputRequester("ProxyPass", "Do you use a password ? Then enter it", "") ;TODO EndIf + ClosePreferences() curl_global_init(#CURL_GLOBAL_WIN32); ;- Main drawing thread launching - ;PBMap\MainDrawingThread = CreateThread(@DrawingThread(), @PBMap\Drawing) ;If PBMap\MainDrawingThread = 0 ; Error("MapGadget : can't create main drawing thread.") Protected Text$ = "Loading" PBmap\ImgLoading = CreateImage(#PB_Any,256,256) If PBmap\ImgLoading - StartVectorDrawing(ImageVectorOutput(PBMap\Imgloading)) - BeginVectorLayer() - VectorSourceColor(RGBA(255, 255, 255, 128)) - AddPathBox(0, 0, 256, 256) - FillPath() - MovePathCursor(0, 0) - VectorFont(FontID(PBMap\Font), 256 / 20) - VectorSourceColor(RGBA(150, 150, 150, 255)) - MovePathCursor(0 + (256 - VectorTextWidth(Text$)) / 2, 0 + (256 - VectorTextHeight(Text$)) / 2) - DrawVectorText(Text$) - EndVectorLayer() - StopVectorDrawing() + StartVectorDrawing(ImageVectorOutput(PBMap\Imgloading)) + BeginVectorLayer() + VectorSourceColor(RGBA(255, 255, 255, 128)) + AddPathBox(0, 0, 256, 256) + FillPath() + MovePathCursor(0, 0) + VectorFont(FontID(PBMap\Font), 256 / 20) + VectorSourceColor(RGBA(150, 150, 150, 255)) + MovePathCursor(0 + (256 - VectorTextWidth(Text$)) / 2, 0 + (256 - VectorTextHeight(Text$)) / 2) + DrawVectorText(Text$) + EndVectorLayer() + StopVectorDrawing() EndIf EndProcedure Procedure Quit() -<<<<<<< HEAD - ;kill main drawing thread (nicer than KillThread(PBMap\MainDrawingThread)) - ;LockMutex(PBMap\Drawing\Mutex) PBMap\Drawing\End = #True - ;UnlockMutex(PBMap\Drawing\Mutex) - ;wait for loading threads to finish nicely - ResetList(PBMap\TilesThreads()) - While NextElement(PBMap\TilesThreads()) - If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 - FreeMemory(PBMap\TilesThreads()\Tile) - DeleteElement(PBMap\TilesThreads()) - ResetList( PBMap\TilesThreads()) -======= - Protected TimeCounter.q - ;Ask main drawing thread to stop and wait for it for 2 seconds - LockMutex(PBMap\Drawing\Mutex) - PBMap\Drawing\End = #True - UnlockMutex(PBMap\Drawing\Mutex) - TimeCounter = ElapsedMilliseconds() - Repeat - Delay(100) - If ElapsedMilliseconds() - TimeCounter > 2000 - ;Should not occur - KillThread(PBMap\MainDrawingThread) ->>>>>>> refs/remotes/origin/djes - EndIf - Until Not IsThread(PBMap\MainDrawingThread) +; ;wait for loading threads to finish nicely +; ResetList(PBMap\TilesThreads()) +; While NextElement(PBMap\TilesThreads()) +; If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 +; FreeMemory(PBMap\TilesThreads()\Tile) +; DeleteElement(PBMap\TilesThreads()) +; ResetList( PBMap\TilesThreads()) +; EndIf +; Wend ;Wait for loading threads to finish nicely. Passed 2 seconds, kills them. - TimeCounter = ElapsedMilliseconds() + Protected TimeCounter = ElapsedMilliseconds() Repeat ResetList(PBMap\TilesThreads()) While NextElement(PBMap\TilesThreads()) @@ -459,20 +452,6 @@ Module PBMap EndIf EndProcedure -<<<<<<< HEAD -======= - Procedure.i GetTileFromMem(key.s) - MyDebug("Check if we have this image in memory", 3) - If FindMapElement(PBMap\MemCache\Images(), key) - MyDebug("Key : " + key + " found !", 3) - ProcedureReturn PBMap\MemCache\Images()\nImage - Else - MyDebug("Key : " + key + " not found !", 3) - ProcedureReturn -1 - EndIf - EndProcedure - ->>>>>>> refs/remotes/origin/djes Procedure.i GetTileFromHDD(CacheFile.s) Protected nImage.i Debug "Loading image " + CacheFile + " ; Size : " + Str(FileSize(CacheFile)) @@ -487,25 +466,36 @@ Module PBMap MyDebug("Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !", 3) EndIf Else - Debug "Failed loading " + CacheFile + " -> Size = 0" - MyDebug("Failed loading " + CacheFile + " -> Size = 0", 3) + Debug "Failed loading " + CacheFile + " -> Size <= 0" + MyDebug("Failed loading " + CacheFile + " -> Size <= 0", 3) EndIf ProcedureReturn -1 EndProcedure - Procedure.i GetTileFromMem(Zoom.i, XTile.i, YTile.i) - Protected key.s = "Z" + RSet(Str(Zoom), 4, "0") + "X" + RSet(Str(XTile), 8, "0") + "Y" + RSet(Str(YTile), 8, "0") - MyDebug("Check if we have this image in memory") + ; Procedure.i GetTileFromMem(Zoom.i, XTile.i, YTile.i) +; Protected key.s = "Z" + RSet(Str(Zoom), 4, "0") + "X" + RSet(Str(XTile), 8, "0") + "Y" + RSet(Str(YTile), 8, "0") +; MyDebug("Check if we have this image in memory") +; If FindMapElement(PBMap\MemCache\Images(), key) +; MyDebug("Key : " + key + " found !") +; ProcedureReturn PBMap\MemCache\Images()\nImage +; Else +; Protected CacheFile.s = PBMap\HDDCachePath + "PBMap_" + Str(Zoom) + "_" + Str(XTile) + "_" + Str(YTile) + ".png" +; MyDebug("Key : " + key + " Try HDD!") +; ProcedureReturn GetTileFromHDD(CacheFile.s) +; EndIf +; EndProcedure + + Procedure.i GetLocalTile(key.s, CacheFile.s) + MyDebug("Check if we have this image in memory", 3) If FindMapElement(PBMap\MemCache\Images(), key) - MyDebug("Key : " + key + " found !") + MyDebug("Key : " + key + " found !", 3) ProcedureReturn PBMap\MemCache\Images()\nImage Else - Protected CacheFile.s = PBMap\HDDCachePath + "PBMap_" + Str(Zoom) + "_" + Str(XTile) + "_" + Str(YTile) + ".png" MyDebug("Key : " + key + " Try HDD!") ProcedureReturn GetTileFromHDD(CacheFile.s) EndIf EndProcedure - + Procedure.i GetTileFromWeb(Zoom.i, XTile.i, YTile.i, CacheFile.s) Protected *Buffer Protected nImage.i = -1 @@ -524,13 +514,9 @@ Module PBMap If *Buffer nImage = CatchImage(#PB_Any, *Buffer, MemorySize(*Buffer)) If IsImage(nImage) -<<<<<<< HEAD - MyDebug("Load from web " + TileURL + " as Tile nb " + nImage) + MyDebug("Load from web " + TileURL + " as Tile nb " + nImage, 3) Debug "url: " + TileURL Debug "cache file: " + CacheFile -======= - MyDebug("Load from web " + TileURL + " as Tile nb " + nImage, 3) ->>>>>>> refs/remotes/origin/djes SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG) FreeMemory(*Buffer) Else @@ -539,11 +525,8 @@ Module PBMap ;ShowMemoryViewer(*Buffer, MemorySize(*Buffer)) EndIf Else -<<<<<<< HEAD Debug("ReceiveHTTPMemory's buffer is empty") -======= MyDebug("ReceiveHTTPMemory's buffer is empty", 3) ->>>>>>> refs/remotes/origin/djes EndIf EndIf ProcedureReturn nImage @@ -551,61 +534,23 @@ Module PBMap Procedure GetImageThread(*Tile.Tile) Protected nImage.i = -1 -<<<<<<< HEAD - LockMutex(PBMap\TileThreadMutex) - Protected key.s = "Z" + RSet(Str(*Tile\PBMapZoom), 4, "0") + "X" + RSet(Str(*Tile\PBMapTileX), 8, "0") + "Y" + RSet(Str(*Tile\PBMapTileY), 8, "0") - Protected CacheFile.s = PBMap\HDDCachePath + "PBMap_" + Str(*Tile\PBMapZoom) + "_" + Str(*Tile\PBMapTileX) + "_" + Str(*Tile\PBMapTileY) + ".png" - ;Adding the image to the cache if possible - - AddMapElement(PBMap\MemCache\Images(), key) - PBMap\MemCache\Images()\nImage= PBMap\ImgLoading - - UnlockMutex(PBMap\TileThreadMutex) ;nImage = GetTileFromHDD(CacheFile) ;If nImage = -1 - ; LockMutex(PBMap\TileThreadMutex) - nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, CacheFile) - ;UnlockMutex(PBMap\TileThreadMutex) + nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, *Tile\CacheFile) ;EndIf If nImage <> -1 LockMutex(PBMap\TileThreadMutex) - PBMap\MemCache\Images(key)\nImage = nImage + PBMap\MemCache\Images(*Tile\key)\nImage = nImage UnlockMutex(PBMap\TileThreadMutex) MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") - MyDebug("With the following key : " + key) + MyDebug("With the following key : " + *Tile\key) Else - MyDebug("Error GetImageThread procedure, image not loaded - " + key) + MyDebug("Error GetImageThread procedure, image not loaded - " + *Tile\key) nImage = -1 EndIf ;Define this tile image nb *Tile\nImage = nImage PostEvent(#PB_Event_Gadget,PBMap\window,PBmap\Gadget,#PB_MAP_REDRAW,*tile) -======= - Protected CacheFile.s = PBMap\HDDCachePath + "PBMap_" + Str(*Tile\PBMapZoom) + "_" + Str(*Tile\PBMapTileX) + "_" + Str(*Tile\PBMapTileY) + ".png" - Protected Tile.position -; PBMap\MemCache\Images(*Tile\key)\Mutex = CreateMutex() -; LockMutex(PBMap\MemCache\Images(*Tile\key)\Mutex) -; LockMutex(LoadMutexTest) - nImage = GetTileFromHDD(CacheFile) -; UnlockMutex(LoadMutexTest) - If nImage = -1 - nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, CacheFile) - If nImage = -1 - MyDebug("Error GetImageThread procedure, image not loaded - " + *Tile\key, 3) - *Tile\nImage = -1 - ProcedureReturn - EndIf - EndIf - PBMap\MemCache\Images(*Tile\key)\nImage = nImage -; Tile\x=*Tile\PBMapTileX -; Tile\y=*Tile\PBMapTiley -; XY2LatLon(@Tile,@PBMap\MemCache\Images(*Tile\key)\Location) - MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache", 3) - MyDebug("With the following key : " + *Tile\key, 3) - ;Define this tile image nb - *Tile\nImage = nImage - ; UnlockMutex(PBMap\MemCache\Images(key)\Mutex) ->>>>>>> refs/remotes/origin/djes EndProcedure Procedure DrawTile(*Tile.Tile) @@ -621,35 +566,8 @@ Module PBMap Protected x = *Tile\Position\x Protected y = *Tile\Position\y Protected Text$ = "Loading" -<<<<<<< HEAD - MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX)) - MyDebug(" at coords " + Str(x) + "," + Str(y)) -; BeginVectorLayer() -; ;MovePathCursor(x, y) -; VectorSourceColor(RGBA(255, 255, 255, 128)) -; AddPathBox(x, y, PBMap\TileSize, PBMap\TileSize) -; FillPath() -; MovePathCursor(x, y) -; VectorFont(FontID(PBMap\Font), PBMap\TileSize / 20) -; VectorSourceColor(RGBA(150, 150, 150, 255)) -; MovePathCursor(x + (PBMap\TileSize - VectorTextWidth(Text$)) / 2, y + (PBMap\TileSize - VectorTextHeight(Text$)) / 2) -; DrawVectorText(Text$) -; EndVectorLayer() -======= MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) - MyDebug(" at coords " + Str(x) + "," + Str(y), 2) - BeginVectorLayer() - ;MovePathCursor(x, y) - VectorSourceColor(RGBA(255, 255, 255, 128)) - AddPathBox(x, y, PBMap\TileSize, PBMap\TileSize) - FillPath() - MovePathCursor(x, y) - VectorFont(FontID(PBMap\Font), PBMap\TileSize / 20) - VectorSourceColor(RGBA(150, 150, 150, 255)) - MovePathCursor(x + (PBMap\TileSize - VectorTextWidth(Text$)) / 2, y + (PBMap\TileSize - VectorTextHeight(Text$)) / 2) - DrawVectorText(Text$) - EndVectorLayer() ->>>>>>> refs/remotes/origin/djes + MyDebug(" at coords " + Str(x) + "," + Str(y)) EndProcedure Procedure DrawTiles(*Drawing.DrawingParameters) @@ -659,89 +577,53 @@ Module PBMap Protected ty = Int(*Drawing\Position\y) Protected nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point Protected ny = *Drawing\CenterY / PBMap\TileSize -<<<<<<< HEAD - Protected px,py,img - + + Protected px, py, img, key.s, CacheFile.s MyDebug("Drawing tiles") For y = - ny - 1 To ny + 1 For x = - nx - 1 To nx + 1 px = *Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX py = *Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY - img = GetTileFromMem(PBMap\Zoom,tx+x,ty+y) + CacheFile = PBMap\HDDCachePath + "PBMap_" + Str(PBMap\Zoom) + "_" + Str(tx + x) + "_" + Str(ty + y) + ".png" + key = "Z" + RSet(Str(PBMap\Zoom), 4, "0") + "X" + RSet(Str(tx + x), 8, "0") + "Y" + RSet(Str(ty + y), 8, "0") ;Unique identifier + img = GetLocalTile(key, CacheFile) If img <> -1 - MovePathCursor(px,py) - DrawVectorImage(ImageID(img)) + MovePathCursor(px, py) + DrawVectorImage(ImageID(img)) Else - MovePathCursor(px,py) + MovePathCursor(px, py) DrawVectorImage(ImageID(PBMap\ImgLoading)) - Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) If *NewTile - With *NewTile - ;Keep a track of tiles (especially to free memory) -======= - MyDebug("Drawing tiles", 2) - For y = - ny - 1 To ny + 1 - For x = - nx - 1 To nx + 1 - ;Was quiting the loop if a move occured, giving maybe smoother movement - ;If PBMap\Moving - ; Break 2 - ;EndIf - ;Store parameters in only one memory place, and give it to the thread if needed - Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) - If *NewTile - With *NewTile - ;Keep a track of tiles, and eventually associated threads, to free memory ->>>>>>> refs/remotes/origin/djes - AddElement(PBMap\TilesThreads()) - PBMap\TilesThreads()\Tile = *NewTile - ;New tile parameters - \Position\x = px ;*Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX - \Position\y = py ;*Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY - \PBMapTileX = tx + x - \PBMapTileY = ty + y - \PBMapZoom = PBMap\Zoom -<<<<<<< HEAD - \GetImageThread = CreateThread(@GetImageThread(), *NewTile) - PBMap\TilesThreads()\GetImageThread = \GetImageThread - myDebug(" Creating get image thread nb " + Str(\GetImageThread)) - ; DrawLoading(*NewTile) - ;*Drawing\Dirty = #True ;Signals that this image is missing so we should have to redraw - EndWith - Else - MyDebug(" Error, can't create a new tile") - Break 2 - EndIf -======= - \key = "Z" + RSet(Str(\PBMapZoom), 4, "0") + "X" + RSet(Str(\PBMapTileX), 8, "0") + "Y" + RSet(Str(\PBMapTileY), 8, "0") ;Unique identifier - ;Check if the image exists - \nImage = GetTileFromMem(\key) - If \nImage = -1 - ;If not, load it in the background - If AddMapElement(PBMap\MemCache\Images(), \key) ;Add the image to the cache, once in this loop - \GetImageThread = CreateThread(@GetImageThread(), *NewTile) - PBMap\TilesThreads()\GetImageThread = \GetImageThread - MyDebug(" Creating get image thread nb " + Str(\GetImageThread), 2) - Else - MyDebug(" Can't add a new image to the map list", 2) - CallDebugger - EndIf - EndIf - If IsImage(\nImage) - DrawTile(*NewTile) - Else - MyDebug("Image missing", 2) - DrawLoading(*NewTile) - *Drawing\Dirty = #True ;Signals that this image is missing so we should have to redraw - EndIf - EndWith - Else - MyDebug(" Error, can't create a new tile", 2) - Break 2 ->>>>>>> refs/remotes/origin/djes - EndIf - Next - Next + With *NewTile + ;Keep a track of tiles (especially to free memory) + AddElement(PBMap\TilesThreads()) + PBMap\TilesThreads()\Tile = *NewTile + ;New tile parameters + \Position\x = px ;*Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX + \Position\y = py ;*Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY + \PBMapTileX = tx + x + \PBMapTileY = ty + y + \PBMapZoom = PBMap\Zoom + \key = key + \CacheFile = CacheFile + LockMutex(PBMap\TileThreadMutex) + AddMapElement(PBMap\MemCache\Images(), \key) ;Add the image to the cache, once in this loop + PBMap\MemCache\Images()\nImage = PBMap\ImgLoading + UnlockMutex(PBMap\TileThreadMutex) + \GetImageThread = CreateThread(@GetImageThread(), *NewTile) + PBMap\TilesThreads()\GetImageThread = \GetImageThread + myDebug(" Creating get image thread nb " + Str(\GetImageThread)) + ; DrawLoading(*NewTile) + ;*Drawing\Dirty = #True ;Signals that this image is missing so we should have to redraw + EndWith + Else + MyDebug(" Error, can't create a new tile") + Break 2 + EndIf + EndIf + Next + Next ;Free tile memory ;TODO : get out this proc from drawtiles in a special "free ressources" task ForEach PBMap\TilesThreads() @@ -751,23 +633,22 @@ Module PBMap DeleteElement(PBMap\TilesThreads()) EndIf Next -<<<<<<< HEAD ;EnableDebugger EndProcedure - Procedure Pointer(x.i, y.i, color.l = #Red) - color=RGBA(255, 0, 0, 255) - VectorSourceColor(color) - MovePathCursor(x, y) - AddPathLine(-8,-16,#PB_Path_Relative) - AddPathCircle(8,0,8,180,0,#PB_Path_Relative) - AddPathLine(-8,16,#PB_Path_Relative) - ;FillPath(#PB_Path_Preserve) - ;ClipPath(#PB_Path_Preserve) - AddPathCircle(0,-16,5,0,360,#PB_Path_Relative) - VectorSourceColor(color) - FillPath(#PB_Path_Preserve):VectorSourceColor(RGBA(0, 0, 0, 255)):StrokePath(1) -======= +; Procedure Pointer(x.i, y.i, color.l = #Red) +; color=RGBA(255, 0, 0, 255) +; VectorSourceColor(color) +; MovePathCursor(x, y) +; AddPathLine(-8,-16,#PB_Path_Relative) +; AddPathCircle(8,0,8,180,0,#PB_Path_Relative) +; AddPathLine(-8,16,#PB_Path_Relative) +; ;FillPath(#PB_Path_Preserve) +; ;ClipPath(#PB_Path_Preserve) +; AddPathCircle(0,-16,5,0,360,#PB_Path_Relative) +; VectorSourceColor(color) +; FillPath(#PB_Path_Preserve):VectorSourceColor(RGBA(0, 0, 0, 255)):StrokePath(1) +; EndProcedure ; ;-**** Clean Mem Cache ; ;TODO in development, by now there's many cache problem as the loading thread could be perturbed ; ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize @@ -794,7 +675,6 @@ Module PBMap ; EndIf ; Next - EndProcedure Procedure DrawPointer(*Drawing.DrawingParameters) If PBMap\CallBackMainPointer > 0 @@ -813,7 +693,6 @@ Module PBMap FillPath(#PB_Path_Preserve):VectorSourceColor(RGBA($FF, 0, 0, $FF));RGBA(0, 0, 0, 255)) StrokePath(1) EndIf ->>>>>>> refs/remotes/origin/djes EndProcedure Procedure DrawScale(*Drawing.DrawingParameters) @@ -938,12 +817,21 @@ Module PBMap ;Drawing\Dirty = #False ;Main drawing stuff StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget)) -<<<<<<< HEAD DrawTiles(*Drawing) + DrawTrack(*Drawing) + DrawMarker(*Drawing) + DrawPointer(*Drawing) ;DrawTrack(*Drawing) ;DrawMarker(*Drawing) ;Pointer(*Drawing\CenterX, *Drawing\CenterY, #Red) - StopVectorDrawing() + ;- Display how many images in cache + VectorFont(FontID(PBMap\Font), 30) + VectorSourceColor(RGBA(0, 0, 0, 80)) + MovePathCursor(50,50) + DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) + MovePathCursor(50,80) + DrawVectorText(Str(ListSize(PBMap\TilesThreads()))) + StopVectorDrawing() ;Redraw ; If something was not correctly drawn, redraw after a while ; LockMutex(*SharedDrawing\Mutex) ;Be sure that we're not modifying variables while moving (seems not useful, but it is, especially to clean the semaphore) @@ -957,34 +845,6 @@ Module PBMap ; EndIf ;UnlockMutex(*SharedDrawing\Mutex) ;Until Drawing\End -======= - DrawTiles(@Drawing) - DrawTrack(@Drawing) - DrawMarker(@Drawing) - DrawPointer(@Drawing) - ;DrawScale(@Drawing) - ;- Display how many images in cache - VectorFont(FontID(PBMap\Font), 30) - VectorSourceColor(RGBA(0, 0, 0, 80)) - MovePathCursor(50,50) - DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) - MovePathCursor(50,80) - DrawVectorText(Str(ListSize(PBMap\TilesThreads()))) - StopVectorDrawing() - ;Redraw - ; If something was not correctly drawn, redraw after a while - LockMutex(*SharedDrawing\Mutex) ;Be sure that we're not modifying variables while moving (seems not useful, but it is, especially to clean the semaphore) - If Drawing\Dirty - MyDebug("Something was dirty ! We try again to redraw") - Drawing\PassNb + 1 - SignalSemaphore(*SharedDrawing\Semaphore) - Else - ;Clean the semaphore to avoid multiple unuseful redraws - Repeat : Until TrySemaphore(*SharedDrawing\Semaphore) = 0 - EndIf - UnlockMutex(*SharedDrawing\Mutex) - Until Drawing\End ->>>>>>> refs/remotes/origin/djes EndProcedure Procedure Refresh() @@ -1089,6 +949,10 @@ Module PBMap PBMap\CallBackLocation = CallBackLocation EndProcedure + Procedure SetCallBackMainPointer(CallBackMainPointer.i) + PBMap\CallBackMainPointer = CallBackMainPointer + EndProcedure + Procedure.d GetLatitude() Protected Value.d ; LockMutex(PBMap\Drawing\Mutex) @@ -1157,19 +1021,16 @@ Module PBMap ;New move values PBMap\Position\x - MouseX PBMap\Position\y - MouseY -<<<<<<< HEAD + ;-*** Fill parameters and signal the drawing thread ; LockMutex(PBMap\Drawing\Mutex) -======= - ;Fill parameters and send a signal to the drawing thread - LockMutex(PBMap\Drawing\Mutex) ->>>>>>> refs/remotes/origin/djes + ;PBMap tile position in tile.decimal PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize PBMap\Drawing\PassNb = 1 XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) -<<<<<<< HEAD + ;If CallBackLocation send Location to function If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) @@ -1180,16 +1041,6 @@ Module PBMap ;SignalSemaphore(PBMap\Drawing\Semaphore) DrawingThread() ;- *** -======= - UnlockMutex(PBMap\Drawing\Mutex) - ;Start drawing - SignalSemaphore(PBMap\Drawing\Semaphore) - EndIf - ;If CallBackLocation send Location to function - If PBMap\CallBackLocation > 0 - CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) - EndIf ->>>>>>> refs/remotes/origin/djes PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) ; DrawingThread() @@ -1357,24 +1208,12 @@ CompilerIf #PB_Compiler_IsMainFile CloseConsole() CompilerEndIf -<<<<<<< HEAD + ; IDE Options = PureBasic 5.42 LTS (Windows - x86) -; CursorPosition = 1073 -; FirstLine = 1062 -; Folding = -------- -; EnableUnicode -======= -; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 450 -; FirstLine = 444 +; ExecutableFormat = Console +; CursorPosition = 333 +; FirstLine = 331 ; Folding = --------- ->>>>>>> refs/remotes/origin/djes +; EnableUnicode ; EnableThread -; EnableXP -; EnableUnicode -; IDE Options = PureBasic 5.42 LTS (Windows - x86) -; CursorPosition = 30 -; FirstLine = 21 -; Folding = --------- -; EnableUnicode ; EnableXP \ No newline at end of file From 71b4c66856526c3899872d887a37616349aa6f64 Mon Sep 17 00:00:00 2001 From: djes Date: Fri, 26 Aug 2016 12:40:07 +0200 Subject: [PATCH 12/49] Code cleaning --- PBMap.pb | 222 ++++++++++++++++--------------------------------------- 1 file changed, 62 insertions(+), 160 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 403fbbd..0bc78e5 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -28,7 +28,7 @@ UsePNGImageEncoder() DeclareModule PBMap #Red = 255 ;-Show debug infos - Global Verbose = 1 + Global Verbose = 0 Global MyDebugLevel = 3 ;-Proxy ON/OFF Global Proxy = #False @@ -36,7 +36,7 @@ DeclareModule PBMap Declare MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) Declare Event(Event.l) Declare SetLocation(latitude.d, longitude.d, zoom = 15, mode.i = #PB_Absolute) - Declare DrawingThread() + Declare Drawing() Declare SetZoom(Zoom.i, mode.i = #PB_Relative) Declare ZoomToArea() Declare SetCallBackLocation(*CallBackLocation) @@ -241,6 +241,25 @@ Module PBMap EndProcedure ;- *** + Procedure LoadingImageCreation() + Protected Text$ = "Loading" + PBmap\ImgLoading = CreateImage(#PB_Any, 256, 256) + If PBmap\ImgLoading + StartVectorDrawing(ImageVectorOutput(PBMap\Imgloading)) + BeginVectorLayer() + VectorSourceColor(RGBA(255, 255, 255, 128)) + AddPathBox(0, 0, 256, 256) + FillPath() + MovePathCursor(0, 0) + VectorFont(FontID(PBMap\Font), 256 / 20) + VectorSourceColor(RGBA(150, 150, 150, 255)) + MovePathCursor(0 + (256 - VectorTextWidth(Text$)) / 2, 0 + (256 - VectorTextHeight(Text$)) / 2) + DrawVectorText(Text$) + EndVectorLayer() + StopVectorDrawing() + EndIf + EndProcedure + Procedure InitPBMap(window) Protected Result.i If Verbose @@ -260,7 +279,6 @@ Module PBMap PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold) PBMap\Window = window PBMap\Options\WheelMouseRelative = #True - ;-Options ;-Preferences ;Use this to create and customize your preferences file for the first time ; CreatePreferences(GetHomeDirectory() + "PBMap.prefs") @@ -285,41 +303,12 @@ Module PBMap Global ProxyPassword$ = InputRequester("ProxyPass", "Do you use a password ? Then enter it", "") ;TODO EndIf ClosePreferences() - curl_global_init(#CURL_GLOBAL_WIN32); - ;- Main drawing thread launching - ;PBMap\MainDrawingThread = CreateThread(@DrawingThread(), @PBMap\Drawing) - ;If PBMap\MainDrawingThread = 0 - ; Error("MapGadget : can't create main drawing thread.") - Protected Text$ = "Loading" - PBmap\ImgLoading = CreateImage(#PB_Any,256,256) - If PBmap\ImgLoading - StartVectorDrawing(ImageVectorOutput(PBMap\Imgloading)) - BeginVectorLayer() - VectorSourceColor(RGBA(255, 255, 255, 128)) - AddPathBox(0, 0, 256, 256) - FillPath() - MovePathCursor(0, 0) - VectorFont(FontID(PBMap\Font), 256 / 20) - VectorSourceColor(RGBA(150, 150, 150, 255)) - MovePathCursor(0 + (256 - VectorTextWidth(Text$)) / 2, 0 + (256 - VectorTextHeight(Text$)) / 2) - DrawVectorText(Text$) - EndVectorLayer() - StopVectorDrawing() - EndIf - + curl_global_init(#CURL_GLOBAL_WIN32) + LoadingImageCreation() EndProcedure Procedure Quit() PBMap\Drawing\End = #True -; ;wait for loading threads to finish nicely -; ResetList(PBMap\TilesThreads()) -; While NextElement(PBMap\TilesThreads()) -; If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 -; FreeMemory(PBMap\TilesThreads()\Tile) -; DeleteElement(PBMap\TilesThreads()) -; ResetList( PBMap\TilesThreads()) -; EndIf -; Wend ;Wait for loading threads to finish nicely. Passed 2 seconds, kills them. Protected TimeCounter = ElapsedMilliseconds() Repeat @@ -472,19 +461,6 @@ Module PBMap ProcedureReturn -1 EndProcedure - ; Procedure.i GetTileFromMem(Zoom.i, XTile.i, YTile.i) -; Protected key.s = "Z" + RSet(Str(Zoom), 4, "0") + "X" + RSet(Str(XTile), 8, "0") + "Y" + RSet(Str(YTile), 8, "0") -; MyDebug("Check if we have this image in memory") -; If FindMapElement(PBMap\MemCache\Images(), key) -; MyDebug("Key : " + key + " found !") -; ProcedureReturn PBMap\MemCache\Images()\nImage -; Else -; Protected CacheFile.s = PBMap\HDDCachePath + "PBMap_" + Str(Zoom) + "_" + Str(XTile) + "_" + Str(YTile) + ".png" -; MyDebug("Key : " + key + " Try HDD!") -; ProcedureReturn GetTileFromHDD(CacheFile.s) -; EndIf -; EndProcedure - Procedure.i GetLocalTile(key.s, CacheFile.s) MyDebug("Check if we have this image in memory", 3) If FindMapElement(PBMap\MemCache\Images(), key) @@ -534,10 +510,7 @@ Module PBMap Procedure GetImageThread(*Tile.Tile) Protected nImage.i = -1 - ;nImage = GetTileFromHDD(CacheFile) - ;If nImage = -1 nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, *Tile\CacheFile) - ;EndIf If nImage <> -1 LockMutex(PBMap\TileThreadMutex) PBMap\MemCache\Images(*Tile\key)\nImage = nImage @@ -550,7 +523,7 @@ Module PBMap EndIf ;Define this tile image nb *Tile\nImage = nImage - PostEvent(#PB_Event_Gadget,PBMap\window,PBmap\Gadget,#PB_MAP_REDRAW,*tile) + PostEvent(#PB_Event_Gadget, PBMap\window,PBmap\Gadget, #PB_MAP_REDRAW, *Tile) EndProcedure Procedure DrawTile(*Tile.Tile) @@ -577,7 +550,6 @@ Module PBMap Protected ty = Int(*Drawing\Position\y) Protected nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point Protected ny = *Drawing\CenterY / PBMap\TileSize - Protected px, py, img, key.s, CacheFile.s MyDebug("Drawing tiles") For y = - ny - 1 To ny + 1 @@ -614,8 +586,6 @@ Module PBMap \GetImageThread = CreateThread(@GetImageThread(), *NewTile) PBMap\TilesThreads()\GetImageThread = \GetImageThread myDebug(" Creating get image thread nb " + Str(\GetImageThread)) - ; DrawLoading(*NewTile) - ;*Drawing\Dirty = #True ;Signals that this image is missing so we should have to redraw EndWith Else MyDebug(" Error, can't create a new tile") @@ -625,7 +595,7 @@ Module PBMap Next Next ;Free tile memory - ;TODO : get out this proc from drawtiles in a special "free ressources" task + ;TODO : maybe get out this proc from drawtiles in a special "free ressources" task ForEach PBMap\TilesThreads() ;Check if there's no more loading thread If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 @@ -636,19 +606,6 @@ Module PBMap ;EnableDebugger EndProcedure -; Procedure Pointer(x.i, y.i, color.l = #Red) -; color=RGBA(255, 0, 0, 255) -; VectorSourceColor(color) -; MovePathCursor(x, y) -; AddPathLine(-8,-16,#PB_Path_Relative) -; AddPathCircle(8,0,8,180,0,#PB_Path_Relative) -; AddPathLine(-8,16,#PB_Path_Relative) -; ;FillPath(#PB_Path_Preserve) -; ;ClipPath(#PB_Path_Preserve) -; AddPathCircle(0,-16,5,0,360,#PB_Path_Relative) -; VectorSourceColor(color) -; FillPath(#PB_Path_Preserve):VectorSourceColor(RGBA(0, 0, 0, 255)):StrokePath(1) -; EndProcedure ; ;-**** Clean Mem Cache ; ;TODO in development, by now there's many cache problem as the loading thread could be perturbed ; ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize @@ -675,7 +632,6 @@ Module PBMap ; EndIf ; Next - Procedure DrawPointer(*Drawing.DrawingParameters) If PBMap\CallBackMainPointer > 0 ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) @@ -729,7 +685,6 @@ Module PBMap Protected km.f, memKm.i If ListSize(PBMap\track())>0 ;Trace Track - ; LockMutex(PBMap\Drawing\Mutex) ForEach PBMap\track() If *Drawing\TargetLocation\Latitude<>0 And *Drawing\TargetLocation\Longitude<>0 GetPixelCoordFromLocation(@PBMap\track(),@Pixel) @@ -763,7 +718,6 @@ Module PBMap EndIf EndIf Next - ; UnlockMutex(PBMap\Drawing\Mutex) EndIf EndProcedure @@ -793,63 +747,37 @@ Module PBMap Next EndProcedure - ;-*** Main drawing thread - ; always running, waiting for a semaphore to start refreshing - Procedure DrawingThread();*SharedDrawing.DrawingParameters) + ;-*** Main drawing + Procedure Drawing() Protected *Drawing.DrawingParameters = @PBMap\Drawing Protected Px.d, Py.d - ;Repeat - ; WaitSemaphore(*SharedDrawing\Semaphore) - ; MyDebug("--------- Main drawing thread ------------") - ;Creates a copy of the structure to work with to avoid multiple mutex locks - ; LockMutex(*SharedDrawing\Mutex) - ; CopyStructure(*SharedDrawing, @Drawing, DrawingParameters) - ; UnlockMutex(*SharedDrawing\Mutex) - ;Precalc some values - *Drawing\CenterX = GadgetWidth(PBMap\Gadget) / 2 - *Drawing\CenterY = GadgetHeight(PBMap\Gadget) / 2 - ;Pixel shift, aka position in the tile - Px = *Drawing\Position\x : Py = *Drawing\Position\y - *Drawing\DeltaX = Px * PBMap\TileSize - (Int(Px) * PBMap\TileSize) ;Don't forget the Int() ! - *Drawing\DeltaY = Py * PBMap\TileSize - (Int(Py) * PBMap\TileSize) - *Drawing\TargetLocation\Latitude = PBMap\TargetLocation\Latitude - *Drawing\TargetLocation\Longitude = PBMap\TargetLocation\Longitude - ;Drawing\Dirty = #False - ;Main drawing stuff - StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget)) - DrawTiles(*Drawing) - DrawTrack(*Drawing) - DrawMarker(*Drawing) - DrawPointer(*Drawing) - ;DrawTrack(*Drawing) - ;DrawMarker(*Drawing) - ;Pointer(*Drawing\CenterX, *Drawing\CenterY, #Red) - ;- Display how many images in cache - VectorFont(FontID(PBMap\Font), 30) - VectorSourceColor(RGBA(0, 0, 0, 80)) - MovePathCursor(50,50) - DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) - MovePathCursor(50,80) - DrawVectorText(Str(ListSize(PBMap\TilesThreads()))) - StopVectorDrawing() - ;Redraw - ; If something was not correctly drawn, redraw after a while - ; LockMutex(*SharedDrawing\Mutex) ;Be sure that we're not modifying variables while moving (seems not useful, but it is, especially to clean the semaphore) - ; If Drawing\Dirty - ; MyDebug("Something was dirty ! We try again to redraw") - ; Drawing\PassNb + 1 - ; SignalSemaphore(*SharedDrawing\Semaphore) - ; Else - ; ;Clean the semaphore to avoid multiple unuseful redraws - ; Repeat : Until TrySemaphore(*SharedDrawing\Semaphore) = 0 - ; EndIf - ;UnlockMutex(*SharedDrawing\Mutex) - ;Until Drawing\End + ;Precalc some values + *Drawing\CenterX = GadgetWidth(PBMap\Gadget) / 2 + *Drawing\CenterY = GadgetHeight(PBMap\Gadget) / 2 + ;Pixel shift, aka position in the tile + Px = *Drawing\Position\x : Py = *Drawing\Position\y + *Drawing\DeltaX = Px * PBMap\TileSize - (Int(Px) * PBMap\TileSize) ;Don't forget the Int() ! + *Drawing\DeltaY = Py * PBMap\TileSize - (Int(Py) * PBMap\TileSize) + *Drawing\TargetLocation\Latitude = PBMap\TargetLocation\Latitude + *Drawing\TargetLocation\Longitude = PBMap\TargetLocation\Longitude + ;Main drawing stuff + StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget)) + DrawTiles(*Drawing) + DrawTrack(*Drawing) + DrawMarker(*Drawing) + DrawPointer(*Drawing) + ;- Display how many images in cache + VectorFont(FontID(PBMap\Font), 30) + VectorSourceColor(RGBA(0, 0, 0, 80)) + MovePathCursor(50,50) + DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) + MovePathCursor(50,80) + DrawVectorText(Str(ListSize(PBMap\TilesThreads()))) + StopVectorDrawing() EndProcedure Procedure Refresh() - DrawingThread() - ;SignalSemaphore(PBMap\Drawing\Semaphore) + Drawing() EndProcedure Procedure SetLocation(latitude.d, longitude.d, zoom = 15, Mode.i = #PB_Absolute) @@ -870,10 +798,7 @@ Module PBMap PBMap\Position\x = PBMap\Drawing\Position\x * PBMap\TileSize PBMap\Position\y = PBMap\Drawing\Position\y * PBMap\TileSize PBMap\Drawing\PassNb = 1 - ;Start drawing - ;SignalSemaphore(PBMap\Drawing\Semaphore) - DrawingThread() - ;*** + Drawing() If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) EndIf @@ -934,12 +859,8 @@ Module PBMap ;Convert X, Y in tile.decimal into real pixels PBMap\Position\X = PBMap\Drawing\Position\x * PBMap\TileSize PBMap\Position\Y = PBMap\Drawing\Position\y * PBMap\TileSize - ;*** Creates a drawing thread and fill parameters - ;PBMap\Drawing\PassNb = 1 - ;Start drawing - ;SignalSemaphore(PBMap\Drawing\Semaphore) - DrawingThread() - ;*** + ;First drawing + Drawing() If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) EndIf @@ -955,25 +876,19 @@ Module PBMap Procedure.d GetLatitude() Protected Value.d - ; LockMutex(PBMap\Drawing\Mutex) Value = PBMap\TargetLocation\Latitude - ; UnlockMutex(PBMap\Drawing\Mutex) ProcedureReturn Value EndProcedure Procedure.d GetLongitude() Protected Value.d - ; LockMutex(PBMap\Drawing\Mutex) Value = PBMap\TargetLocation\Longitude - ; UnlockMutex(PBMap\Drawing\Mutex) ProcedureReturn Value EndProcedure Procedure.i GetZoom() Protected Value.d - ; LockMutex(PBMap\Drawing\Mutex) Value = PBMap\Zoom - ; UnlockMutex(PBMap\Drawing\Mutex) ProcedureReturn Value EndProcedure @@ -1010,7 +925,7 @@ Module PBMap MouseX = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) - PBMap\MoveStartingPoint\x MouseY = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) - PBMap\MoveStartingPoint\y PBMap\Moving = #True - ;move Marker + ;Move marker If PBMap\EditMarkerIndex > -1 SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) LatLon2XY(@PBMap\Marker()\Location, @Marker) @@ -1021,29 +936,19 @@ Module PBMap ;New move values PBMap\Position\x - MouseX PBMap\Position\y - MouseY - - ;-*** Fill parameters and signal the drawing thread - ; LockMutex(PBMap\Drawing\Mutex) - ;PBMap tile position in tile.decimal PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize PBMap\Drawing\PassNb = 1 XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) - ;If CallBackLocation send Location to function If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) EndIf - ; UnlockMutex(PBMap\Drawing\Mutex) EndIf - ;Start drawing - ;SignalSemaphore(PBMap\Drawing\Semaphore) - DrawingThread() - ;- *** + Drawing() PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) - ; DrawingThread() EndIf Case #PB_EventType_LeftButtonUp PBMap\Moving = #False @@ -1051,16 +956,14 @@ Module PBMap If PBMap\EditMarkerIndex > -1 PBMap\EditMarkerIndex = -1 Else ;Move Map - ;LockMutex(PBMap\Drawing\Mutex) PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize MyDebug("PBMap\Drawing\Position\x " + Str(PBMap\Drawing\Position\x) + " ; PBMap\Drawing\Position\y " + Str(PBMap\Drawing\Position\y) ) XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) - ; UnlockMutex(PBMap\Drawing\Mutex) - DrawingThread() + Drawing() EndIf Case #PB_MAP_REDRAW - DrawingThread() + Drawing() EndSelect EndSelect EndSelect @@ -1140,7 +1043,6 @@ CompilerIf #PB_Compiler_IsMainFile EndProcedure ;- MAIN TEST - OpenConsole() If OpenWindow(#Window_0, 260, 225, 700, 571, "PBMap", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered | #PB_Window_SizeGadget) LoadFont(0, "Arial", 12) @@ -1210,10 +1112,10 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.42 LTS (Windows - x86) -; ExecutableFormat = Console -; CursorPosition = 333 -; FirstLine = 331 +; CursorPosition = 272 +; FirstLine = 262 ; Folding = --------- ; EnableUnicode ; EnableThread -; EnableXP \ No newline at end of file +; EnableXP +; DisableDebugger \ No newline at end of file From bd805db44a8665a05f2de83d7b2ca16e3c14e8ae Mon Sep 17 00:00:00 2001 From: djes Date: Fri, 26 Aug 2016 14:00:16 +0200 Subject: [PATCH 13/49] Re-added wheelmouse support and other fixes --- PBMap.pb | 77 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 12 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 0bc78e5..53b6e38 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -33,6 +33,7 @@ DeclareModule PBMap ;-Proxy ON/OFF Global Proxy = #False Declare InitPBMap(window) + Declare SetMapServer(ServerURL.s = "http://tile.openstreetmap.org/", TileSize = 256, ZoomMin = 0, ZoomMax = 18) Declare MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) Declare Event(Event.l) Declare SetLocation(latitude.d, longitude.d, zoom = 15, mode.i = #PB_Absolute) @@ -40,6 +41,7 @@ DeclareModule PBMap Declare SetZoom(Zoom.i, mode.i = #PB_Relative) Declare ZoomToArea() Declare SetCallBackLocation(*CallBackLocation) + Declare SetCallBackMainPointer(CallBackMainPointer.i) Declare LoadGpxFile(file.s); Declare AddMarker(Latitude.d,Longitude.d,color.l=-1, CallBackPointer.i = -1) Declare Quit() @@ -87,13 +89,11 @@ Module PBMap PBMapTileX.i PBMapTileY.i PBMapZoom.i - Mutex.i TargetLocation.Location CenterX.i CenterY.i DeltaX.i DeltaY.i - Semaphore.i Dirty.i PassNB.i End.i @@ -273,8 +273,6 @@ Module PBMap PBMap\TileSize = 256 PBMap\Dirty = #False PBMap\TileThreadMutex = CreateMutex() - PBMap\Drawing\Mutex = CreateMutex() - PBMap\Drawing\Semaphore = CreateSemaphore() PBMap\EditMarkerIndex = -1 ;Initialised with "no marker selected" PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold) PBMap\Window = window @@ -307,6 +305,13 @@ Module PBMap LoadingImageCreation() EndProcedure + Procedure SetMapServer(ServerURL.s = "http://tile.openstreetmap.org/", TileSize = 256, ZoomMin = 0, ZoomMax = 18) + PBMap\ServerURL = ServerURL + PBMap\ZoomMin = ZoomMin + PBMap\ZoomMax = ZoomMax + PBMap\TileSize = TileSize + EndProcedure + Procedure Quit() PBMap\Drawing\End = #True ;Wait for loading threads to finish nicely. Passed 2 seconds, kills them. @@ -343,10 +348,10 @@ Module PBMap Procedure MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) If Gadget = #PB_Any - PBMap\Gadget = CanvasGadget(PBMap\Gadget, X, Y, Width, Height) + PBMap\Gadget = CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) ;#PB_Canvas_Keyboard has to be set for mousewheel to work on windows Else PBMap\Gadget = Gadget - CanvasGadget(PBMap\Gadget, X, Y, Width, Height) + CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) EndIf EndProcedure @@ -874,6 +879,40 @@ Module PBMap PBMap\CallBackMainPointer = CallBackMainPointer 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 + ;Fast and dirty code + OldPx = PBMap\Position\x : OldPy = PBMap\Position\y + OldMx = OldPx + GadgetWidth(PBMap\Gadget) / 2 - x + OldMy = OldPy + GadgetHeight(PBMap\Gadget) / 2 - y + PBMap\Zoom = PBMap\Zoom + zoom + If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf + If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf + ;Centered Zoom + LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing) + ;Convert X, Y in tile.decimal into real pixels + PBMap\Position\x = PBMap\Drawing\Position\x * PBMap\TileSize + PBMap\Position\y = PBMap\Drawing\Position\y * PBMap\TileSize + MouseX = PBMap\Position\x + GadgetWidth(PBMap\Gadget) / 2 - x + MouseY = PBMap\Position\y + GadgetHeight(PBMap\Gadget) / 2 - y + ;Cross-multiply to get the new center + PBMap\Position\x = (OldPx * MouseX) / OldMx + PBMap\Position\y = (OldPy * MouseY) / OldMy + ;PBMap tile position in tile.decimal + PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize + PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize + PBMap\Drawing\PassNb = 1 + XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) + ;Start drawing + Drawing() + ;If CallBackLocation send Location to function + If PBMap\CallBackLocation > 0 + CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) + EndIf + EndProcedure + Procedure.d GetLatitude() Protected Value.d Value = PBMap\TargetLocation\Latitude @@ -903,7 +942,15 @@ Module PBMap Gadget = EventGadget() Select Gadget Case PBMap\Gadget - Select EventType() + Select EventType() + 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)) + Else + ;Absolute zoom (centered on the center of the map) + SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta), #PB_Relative) + EndIf Case #PB_EventType_LeftButtonDown ;Check if we select a marker MouseX = PBMap\Position\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) @@ -1023,6 +1070,11 @@ CompilerIf #PB_Compiler_IsMainFile FillPath(#PB_Path_Preserve):VectorSourceColor(RGBA(0, 0, 0, 255)):StrokePath(1) EndProcedure + Procedure MainPointer(x.i, y.i) + VectorSourceColor(RGBA(255, 255,255, 255)) : AddPathCircle(x, y,32) : StrokePath(1) + VectorSourceColor(RGBA(0, 0, 0, 255)) : AddPathCircle(x, y, 29):StrokePath(2) + EndProcedure + Procedure ResizeAll() ResizeGadget(#Map,10,10,WindowWidth(#Window_0)-198,WindowHeight(#Window_0)-59) ResizeGadget(#Text_1,WindowWidth(#Window_0)-170,#PB_Ignore,#PB_Ignore,#PB_Ignore) @@ -1069,6 +1121,7 @@ CompilerIf #PB_Compiler_IsMainFile ;Our main gadget PBMap::InitPBMap(#Window_0) PBMap::MapGadget(#Map, 10, 10, 512, 512) + PBMap::SetCallBackMainPointer(@MainPointer()) ;To change the Main Pointer PBMap::SetCallBackLocation(@UpdateLocation()) PBMap::SetLocation(-36.8485,174.7633,10) ;PBMap::AddMarker(49.0446828398, 2.0349812508, -1, @MyPointer()) @@ -1094,10 +1147,10 @@ CompilerIf #PB_Compiler_IsMainFile Case #Button_5 PBMap::SetZoom( - 1) Case #Gdt_LoadGpx - PBMap::LoadGpxFile(OpenFileRequester("Choisissez un fichier � charger", "", "*.gpx", 0)) - PBMap::ZoomToArea() ; <-To center the view, and to viex all the track + PBMap::LoadGpxFile(OpenFileRequester("Choose a file to load", "", "*.gpx", 0)) + PBMap::ZoomToArea() ; <-To center the view, and zoom on the tracks Case #Gdt_AddMarker - PBMap:: AddMarker(ValD(GetGadgetText(#String_0)),ValD(GetGadgetText(#String_1)),RGBA(Random(255),Random(255),Random(255),255)) + PBMap:: AddMarker(ValD(GetGadgetText(#String_0)), ValD(GetGadgetText(#String_1)), RGBA(Random(255), Random(255), Random(255),255)) EndSelect Case #PB_Event_SizeWindow ResizeAll() @@ -1112,8 +1165,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.42 LTS (Windows - x86) -; CursorPosition = 272 -; FirstLine = 262 +; CursorPosition = 362 +; FirstLine = 339 ; Folding = --------- ; EnableUnicode ; EnableThread From eca7a01c84b0ef899343fc7c0dd63fc9281932be Mon Sep 17 00:00:00 2001 From: djes Date: Mon, 29 Aug 2016 09:31:18 +0200 Subject: [PATCH 14/49] New key, cache fix, layering, scale --- PBMap.pb | 188 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 130 insertions(+), 58 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 53b6e38..a3e75e3 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -2,7 +2,7 @@ ; Program: PBMap ; Description: Permits the use of tiled maps like ; OpenStreetMap in a handy PureBASIC module -; Author: Thyphoon And Djes +; Author: Thyphoon, Djes And Idle ; Date: Mai 17, 2016 ; License: Free, unrestricted, credit appreciated ; but not required. @@ -12,7 +12,7 @@ ; (see also Proxy Details) ;************************************************************** -#Red = 255 +;#Red = 255 CompilerIf #PB_Compiler_Thread = #False MessageRequester("Warning !!","You must enable ThreadSafe support in compiler options",#PB_MessageRequester_Ok ) @@ -28,10 +28,14 @@ UsePNGImageEncoder() DeclareModule PBMap #Red = 255 ;-Show debug infos - Global Verbose = 0 + Global Verbose = 1 Global MyDebugLevel = 3 ;-Proxy ON/OFF Global Proxy = #False + + #SCALE_NAUTICAL = 1 + #SCALE_KM = 0 + Declare InitPBMap(window) Declare SetMapServer(ServerURL.s = "http://tile.openstreetmap.org/", TileSize = 256, ZoomMin = 0, ZoomMax = 18) Declare MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) @@ -42,6 +46,7 @@ DeclareModule PBMap Declare ZoomToArea() Declare SetCallBackLocation(*CallBackLocation) Declare SetCallBackMainPointer(CallBackMainPointer.i) + Declare SetMapScaleUnit(ScaleUnit=PBMAP::#SCALE_KM) Declare LoadGpxFile(file.s); Declare AddMarker(Latitude.d,Longitude.d,color.l=-1, CallBackPointer.i = -1) Declare Quit() @@ -50,6 +55,7 @@ DeclareModule PBMap Declare.d GetLatitude() Declare.d GetLongitude() Declare.i GetZoom() + EndDeclareModule Module PBMap @@ -81,6 +87,7 @@ Module PBMap key.s CacheFile.s GetImageThread.i + layer.i EndStructure Structure DrawingParameters @@ -111,7 +118,7 @@ Module PBMap EndStructure Structure TileMemCach - Map Images.ImgMemCach() + Map Images.ImgMemCach(4096) EndStructure Structure Marker @@ -122,6 +129,7 @@ Module PBMap Structure Option WheelMouseRelative.i + ScaleUnit.i ; Scale unit to use for measurements EndStructure ;-PBMap Structure @@ -138,7 +146,9 @@ Module PBMap Position.PixelPosition ; Actual focus point coords in pixels (global) MoveStartingPoint.PixelPosition ; Start mouse position coords when dragging the map ; - ServerURL.s ; Web URL ex: http://tile.openstreetmap.org/ + Array ServerURL.s(0) ; Web URL ex: http://tile.openstreetmap.org/ + NumberOfMapLayers.i ; The number of map tile layers; + ZoomMin.i ; Min Zoom supported by server ZoomMax.i ; Max Zoom supported by server Zoom.i ; Current zoom @@ -266,7 +276,6 @@ Module PBMap OpenConsole() EndIf PBMap\HDDCachePath = GetTemporaryDirectory() - PBMap\ServerURL = "http://tile.openstreetmap.org/" PBMap\ZoomMin = 0 PBMap\ZoomMax = 18 PBMap\MoveStartingPoint\x = - 1 @@ -277,6 +286,8 @@ Module PBMap PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold) PBMap\Window = window PBMap\Options\WheelMouseRelative = #True + SetMapServer("http://tile.openstreetmap.org/") + ;-Preferences ;Use this to create and customize your preferences file for the first time ; CreatePreferences(GetHomeDirectory() + "PBMap.prefs") @@ -306,11 +317,14 @@ Module PBMap EndProcedure Procedure SetMapServer(ServerURL.s = "http://tile.openstreetmap.org/", TileSize = 256, ZoomMin = 0, ZoomMax = 18) - PBMap\ServerURL = ServerURL + PBMAP\NumberOfMapLayers + 1 + ReDim PBMap\ServerURL(PBMAP\NumberOfMapLayers) + PBMap\ServerURL(PBMAP\NumberOfMapLayers-1) = ServerURL PBMap\ZoomMin = ZoomMin PBMap\ZoomMax = ZoomMax PBMap\TileSize = TileSize EndProcedure + Procedure Quit() PBMap\Drawing\End = #True @@ -448,40 +462,46 @@ Module PBMap Procedure.i GetTileFromHDD(CacheFile.s) Protected nImage.i - Debug "Loading image " + CacheFile + " ; Size : " + Str(FileSize(CacheFile)) + ; Debug "Loading image " + CacheFile + " ; Size : " + Str(FileSize(CacheFile)) If FileSize(CacheFile) > 0 nImage = LoadImage(#PB_Any, CacheFile) If IsImage(nImage) - Debug "Success loading " + CacheFile + " as nImage " + Str(nImage) + ;Debug "Success loading " + CacheFile + " as nImage " + Str(nImage) MyDebug("Success loading " + CacheFile + " as nImage " + Str(nImage), 3) ProcedureReturn nImage Else - Debug "Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !" + ; Debug "Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !" MyDebug("Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !", 3) EndIf Else - Debug "Failed loading " + CacheFile + " -> Size <= 0" + ; Debug "Failed loading " + CacheFile + " -> Size <= 0" MyDebug("Failed loading " + CacheFile + " -> Size <= 0", 3) EndIf ProcedureReturn -1 EndProcedure Procedure.i GetLocalTile(key.s, CacheFile.s) + Protected timg MyDebug("Check if we have this image in memory", 3) If FindMapElement(PBMap\MemCache\Images(), key) MyDebug("Key : " + key + " found !", 3) ProcedureReturn PBMap\MemCache\Images()\nImage Else MyDebug("Key : " + key + " Try HDD!") - ProcedureReturn GetTileFromHDD(CacheFile.s) + timg = GetTileFromHDD(CacheFile.s) + If timg <> -1 + AddMapElement(PBMap\MemCache\Images(),key) + PBMap\MemCache\Images()\nImage = timg + EndIf + ProcedureReturn timg EndIf EndProcedure - Procedure.i GetTileFromWeb(Zoom.i, XTile.i, YTile.i, CacheFile.s) + Procedure.i GetTileFromWeb(Zoom.i, XTile.i, YTile.i, CacheFile.s,layer.i) Protected *Buffer Protected nImage.i = -1 - Protected FileHandle.i - Protected TileURL.s = PBMap\ServerURL + Str(Zoom) + "/" + Str(XTile) + "/" + Str(YTile) + ".png" + Protected FileHandle.i,timg + Protected TileURL.s = PBMap\ServerURL(layer) + Str(Zoom) + "/" + Str(XTile) + "/" + Str(YTile) + ".png" MyDebug("Check if we have this image on Web", 3) If Proxy FileHandle = CurlReceiveHTTPToFile(TileURL, CacheFile, ProxyURL$, ProxyPort$, ProxyUser$, ProxyPassword$) @@ -496,17 +516,26 @@ Module PBMap nImage = CatchImage(#PB_Any, *Buffer, MemorySize(*Buffer)) If IsImage(nImage) MyDebug("Load from web " + TileURL + " as Tile nb " + nImage, 3) - Debug "url: " + TileURL - Debug "cache file: " + CacheFile - SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG) - FreeMemory(*Buffer) + ; Debug "url: " + TileURL + ; Debug "cache file: " + CacheFile + ; timg = LoadImage(#PB_Any,CacheFile) + ; If timg + ; StartDrawing(ImageOutput(timg)) + ; DrawImage(ImageID(nimage)) + ; StopDrawing() + ; SaveImage(timg, CacheFile, #PB_ImagePlugin_PNG) + ; FreeImage(timg) + ; Else + SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG) + FreeMemory(*Buffer) + ; EndIf Else MyDebug("Can't catch image " + TileURL, 3) nImage = -1 ;ShowMemoryViewer(*Buffer, MemorySize(*Buffer)) EndIf Else - Debug("ReceiveHTTPMemory's buffer is empty") + ; Debug("ReceiveHTTPMemory's buffer is empty") MyDebug("ReceiveHTTPMemory's buffer is empty", 3) EndIf EndIf @@ -515,20 +544,20 @@ Module PBMap Procedure GetImageThread(*Tile.Tile) Protected nImage.i = -1 - nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, *Tile\CacheFile) + nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, *Tile\CacheFile,*tile\layer) If nImage <> -1 LockMutex(PBMap\TileThreadMutex) PBMap\MemCache\Images(*Tile\key)\nImage = nImage UnlockMutex(PBMap\TileThreadMutex) MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") MyDebug("With the following key : " + *Tile\key) - Else + *Tile\nImage = nImage + PostEvent(#PB_Event_Gadget, PBMap\window,PBmap\Gadget, #PB_MAP_REDRAW, *Tile) + Else MyDebug("Error GetImageThread procedure, image not loaded - " + *Tile\key) - nImage = -1 - EndIf - ;Define this tile image nb - *Tile\nImage = nImage - PostEvent(#PB_Event_Gadget, PBMap\window,PBmap\Gadget, #PB_MAP_REDRAW, *Tile) + ;nImage = -1 + EndIf + EndProcedure Procedure DrawTile(*Tile.Tile) @@ -548,42 +577,52 @@ Module PBMap MyDebug(" at coords " + Str(x) + "," + Str(y)) EndProcedure - Procedure DrawTiles(*Drawing.DrawingParameters) + Procedure DrawTiles(*Drawing.DrawingParameters,layer.i,alpha.i=255) ;DisableDebugger - Protected x.i, y.i + Protected x.i, y.i,kq.q Protected tx = Int(*Drawing\Position\x) ;Don't forget the Int() ! Protected ty = Int(*Drawing\Position\y) Protected nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point Protected ny = *Drawing\CenterY / PBMap\TileSize - Protected px, py, img, key.s, CacheFile.s + Protected px, py, img, tilex,tiley, key.s, CacheFile.s MyDebug("Drawing tiles") For y = - ny - 1 To ny + 1 For x = - nx - 1 To nx + 1 px = *Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX py = *Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY - CacheFile = PBMap\HDDCachePath + "PBMap_" + Str(PBMap\Zoom) + "_" + Str(tx + x) + "_" + Str(ty + y) + ".png" - key = "Z" + RSet(Str(PBMap\Zoom), 4, "0") + "X" + RSet(Str(tx + x), 8, "0") + "Y" + RSet(Str(ty + y), 8, "0") ;Unique identifier + tilex = ((tx+x) % (1<< PBMap\Zoom)) + tiley = ty+y + kq = layer | (pbmap\zoom << 8) | (tilex << 16) | (tiley << 36) + key = Str(kq) + CacheFile = PBMap\HDDCachePath + key + ".png" + + img = GetLocalTile(key, CacheFile) If img <> -1 MovePathCursor(px, py) - DrawVectorImage(ImageID(img)) + DrawVectorImage(ImageID(img),alpha) Else MovePathCursor(px, py) - DrawVectorImage(ImageID(PBMap\ImgLoading)) + DrawVectorImage(ImageID(PBMap\ImgLoading),alpha) Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) If *NewTile With *NewTile ;Keep a track of tiles (especially to free memory) + ;TODO : not sure if it really needs a mutex here, i'm still trying to find where the white tile issue is coming from + ;a tile download is either getting clobbered or timing out or getting kicked and it results in a corrupted png file on linux + LockMutex(PBMap\TileThreadMutex) AddElement(PBMap\TilesThreads()) PBMap\TilesThreads()\Tile = *NewTile + UnlockMutex(PBMap\TileThreadMutex) ;New tile parameters - \Position\x = px ;*Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX - \Position\y = py ;*Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY - \PBMapTileX = tx + x - \PBMapTileY = ty + y + \Position\x = px + \Position\y = py + \PBMapTileX = tilex + \PBMapTileY = tiley \PBMapZoom = PBMap\Zoom \key = key \CacheFile = CacheFile + \layer = layer LockMutex(PBMap\TileThreadMutex) AddMapElement(PBMap\MemCache\Images(), \key) ;Add the image to the cache, once in this loop PBMap\MemCache\Images()\nImage = PBMap\ImgLoading @@ -596,9 +635,13 @@ Module PBMap MyDebug(" Error, can't create a new tile") Break 2 EndIf - EndIf + EndIf + Next - Next + + Next + + ;Free tile memory ;TODO : maybe get out this proc from drawtiles in a special "free ressources" task ForEach PBMap\TilesThreads() @@ -656,15 +699,29 @@ Module PBMap EndIf EndProcedure - Procedure DrawScale(*Drawing.DrawingParameters) + Procedure DrawScale(*Drawing.DrawingParameters,x,y,alpha=80) ;TODO Add Option and function to display Scale on Map - Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) - VectorFont(FontID(PBMap\Font), 30) - VectorSourceColor(RGBA(0, 0, 0, 80)) - MovePathCursor(50,50) - DrawVectorText(StrD(Scale)) + Protected sunit.s + Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) / 2 + + Select PBMap\Options\ScaleUnit + Case #SCALE_Nautical + Scale * 0.539957 + sunit = " Nm" + Case #SCALE_KM; + sunit = " Km" + EndSelect + + VectorFont(FontID(PBMap\Font), 10) + VectorSourceColor(RGBA(0, 0, 0,alpha)) + MovePathCursor(x,y) + DrawVectorText(StrD(Scale,3)+sunit) + MovePathCursor(x,y+12) + AddPathLine(x+128,y+10) + StrokePath(1) + EndProcedure - + Procedure TrackPointer(x.i, y.i,dist.l) Protected color.l color=RGBA(0, 0, 0, 255) @@ -755,7 +812,7 @@ Module PBMap ;-*** Main drawing Procedure Drawing() Protected *Drawing.DrawingParameters = @PBMap\Drawing - Protected Px.d, Py.d + Protected Px.d, Py.d,a ;Precalc some values *Drawing\CenterX = GadgetWidth(PBMap\Gadget) / 2 *Drawing\CenterY = GadgetHeight(PBMap\Gadget) / 2 @@ -767,7 +824,11 @@ Module PBMap *Drawing\TargetLocation\Longitude = PBMap\TargetLocation\Longitude ;Main drawing stuff StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget)) - DrawTiles(*Drawing) + ;TODO add in layers of tiles ;this way we can cache them as 0 base 1.n layers + ;such as for openseamap tiles which are overlaid. not that efficent from here though. + For a = 0 To PBMap\NumberOfMapLayers-1 + DrawTiles(*Drawing,a) + Next DrawTrack(*Drawing) DrawMarker(*Drawing) DrawPointer(*Drawing) @@ -778,6 +839,10 @@ Module PBMap DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) MovePathCursor(50,80) DrawVectorText(Str(ListSize(PBMap\TilesThreads()))) + + ;If PBMap\Options\ShowScale + DrawScale(*Drawing,10,GadgetHeight(PBMAP\Gadget)-20,192) + ;EndIf StopVectorDrawing() EndProcedure @@ -854,7 +919,7 @@ Module PBMap Procedure SetZoom(Zoom.i, mode.i = #PB_Relative) Select mode Case #PB_Relative - PBMap\Zoom = PBMap\Zoom + zoom + PBMap\Zoom = PBMap\Zoom + zoom Case #PB_Absolute PBMap\Zoom = zoom EndSelect @@ -879,6 +944,11 @@ Module PBMap PBMap\CallBackMainPointer = CallBackMainPointer EndProcedure + Procedure SetMapScaleUnit(ScaleUnit.i = PBMAP::#SCALE_KM) + PBMap\Options\ScaleUnit = ScaleUnit + Drawing() + EndProcedure + ;Zoom on x, y position relative to the canvas gadget Procedure SetZoomOnPosition(x, y, zoom) Protected MouseX.d, MouseY.d @@ -1094,6 +1164,7 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::Refresh() EndProcedure + OpenConsole() ;- MAIN TEST If OpenWindow(#Window_0, 260, 225, 700, 571, "PBMap", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered | #PB_Window_SizeGadget) @@ -1123,7 +1194,9 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::MapGadget(#Map, 10, 10, 512, 512) PBMap::SetCallBackMainPointer(@MainPointer()) ;To change the Main Pointer PBMap::SetCallBackLocation(@UpdateLocation()) - PBMap::SetLocation(-36.8485,174.7633,10) + PBMap::SetLocation(-36.81148,175.08634,12) + PBMap::SetMapServer("http://t1.openseamap.org/seamark/") ;add a special osm overlay map + PBMAP::SetMapScaleUnit(PBMAP::#SCALE_NAUTICAL) ;PBMap::AddMarker(49.0446828398, 2.0349812508, -1, @MyPointer()) Repeat @@ -1163,12 +1236,11 @@ CompilerIf #PB_Compiler_IsMainFile CloseConsole() CompilerEndIf - -; IDE Options = PureBasic 5.42 LTS (Windows - x86) -; CursorPosition = 362 -; FirstLine = 339 -; Folding = --------- -; EnableUnicode +; IDE Options = PureBasic 5.50 (Windows - x64) +; CursorPosition = 1219 +; FirstLine = 1193 +; Folding = ---------- ; EnableThread ; EnableXP -; DisableDebugger \ No newline at end of file +; DisableDebugger +; EnableUnicode \ No newline at end of file From 31d763455fdf061e0a4a63c70b3f02e898e23fd7 Mon Sep 17 00:00:00 2001 From: djes Date: Mon, 29 Aug 2016 11:52:31 +0200 Subject: [PATCH 15/49] Code cleaning, event handling work --- PBMap.pb | 418 +++++++++++++++++++++++++++---------------------------- 1 file changed, 202 insertions(+), 216 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index a3e75e3..021903e 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -31,7 +31,7 @@ DeclareModule PBMap Global Verbose = 1 Global MyDebugLevel = 3 ;-Proxy ON/OFF - Global Proxy = #False + Global Proxy = #True #SCALE_NAUTICAL = 1 #SCALE_KM = 0 @@ -39,7 +39,6 @@ DeclareModule PBMap Declare InitPBMap(window) Declare SetMapServer(ServerURL.s = "http://tile.openstreetmap.org/", TileSize = 256, ZoomMin = 0, ZoomMax = 18) Declare MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) - Declare Event(Event.l) Declare SetLocation(latitude.d, longitude.d, zoom = 15, mode.i = #PB_Absolute) Declare Drawing() Declare SetZoom(Zoom.i, mode.i = #PB_Relative) @@ -55,7 +54,6 @@ DeclareModule PBMap Declare.d GetLatitude() Declare.d GetLongitude() Declare.i GetZoom() - EndDeclareModule Module PBMap @@ -87,7 +85,7 @@ Module PBMap key.s CacheFile.s GetImageThread.i - layer.i + Layer.i EndStructure Structure DrawingParameters @@ -173,6 +171,7 @@ Module PBMap EndStructure #PB_MAP_REDRAW = #PB_EventType_FirstCustomValue + 1 + #PB_MAP_RETRY = #PB_EventType_FirstCustomValue + 2 ;-Global variables Global PBMap.PBMap, Null.i @@ -204,8 +203,8 @@ Module PBMap Procedure.i CurlReceiveHTTPToFile(URL$, DestFileName$, ProxyURL$="", ProxyPort$="", ProxyUser$="", ProxyPassword$="") Protected *Buffer, curl.i, Timeout.i, res.i Protected FileHandle.i - MyDebug("ReceiveHTTPToFile from " + URL$ + " " + ProxyURL$ + ProxyPort$ + ProxyUser$, 3) - MyDebug(" to file : " + DestFileName$, 3) + MyDebug("CurlReceiveHTTPToFile from " + URL$ + " " + ProxyURL$ + " " + ProxyPort$ + " " + ProxyUser$, 4) + MyDebug(" to file : " + DestFileName$, 4) FileHandle = CreateFile(#PB_Any, DestFileName$) If FileHandle And Len(URL$) curl = curl_easy_init() @@ -238,11 +237,11 @@ Module PBMap curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @ReceiveHTTPWriteToFileFunction()) res = curl_easy_perform(curl) If res <> #CURLE_OK - MyDebug("CURL problem", 3) + MyDebug("CURL problem", 4) EndIf curl_easy_cleanup(curl) Else - MyDebug("Can't init CURL", 3) + MyDebug("Can't init CURL", 4) EndIf CloseFile(FileHandle) ProcedureReturn FileSize(DestFileName$) @@ -270,7 +269,7 @@ Module PBMap EndIf EndProcedure - Procedure InitPBMap(window) + Procedure InitPBMap(Window) Protected Result.i If Verbose OpenConsole() @@ -284,7 +283,7 @@ Module PBMap PBMap\TileThreadMutex = CreateMutex() PBMap\EditMarkerIndex = -1 ;Initialised with "no marker selected" PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold) - PBMap\Window = window + PBMap\Window = Window PBMap\Options\WheelMouseRelative = #True SetMapServer("http://tile.openstreetmap.org/") @@ -324,7 +323,6 @@ Module PBMap PBMap\ZoomMax = ZoomMax PBMap\TileSize = TileSize EndProcedure - Procedure Quit() PBMap\Drawing\End = #True @@ -359,16 +357,7 @@ Module PBMap Result = Sqr( (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) ProcedureReturn Result EndProcedure - - Procedure MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) - If Gadget = #PB_Any - PBMap\Gadget = CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) ;#PB_Canvas_Keyboard has to be set for mousewheel to work on windows - Else - PBMap\Gadget = Gadget - CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) - EndIf - EndProcedure - + ;*** Converts coords to tile.decimal ;Warning, structures used in parameters are not tested Procedure LatLon2XY(*Location.Location, *Coords.Position) @@ -460,83 +449,92 @@ Module PBMap EndIf EndProcedure + Procedure LoadErrorHandler() + MessageRequester("Error", "") + EndProcedure + Procedure.i GetTileFromHDD(CacheFile.s) Protected nImage.i - ; Debug "Loading image " + CacheFile + " ; Size : " + Str(FileSize(CacheFile)) If FileSize(CacheFile) > 0 + OnErrorCall(@LoadErrorHandler()) nImage = LoadImage(#PB_Any, CacheFile) + OnErrorDefault() If IsImage(nImage) - ;Debug "Success loading " + CacheFile + " as nImage " + Str(nImage) MyDebug("Success loading " + CacheFile + " as nImage " + Str(nImage), 3) ProcedureReturn nImage Else - ; Debug "Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !" + MyDebug("Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !", 3) MyDebug("Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !", 3) EndIf Else - ; Debug "Failed loading " + CacheFile + " -> Size <= 0" MyDebug("Failed loading " + CacheFile + " -> Size <= 0", 3) EndIf ProcedureReturn -1 EndProcedure Procedure.i GetLocalTile(key.s, CacheFile.s) - Protected timg - MyDebug("Check if we have this image in memory", 3) + Protected timg = -1 If FindMapElement(PBMap\MemCache\Images(), key) - MyDebug("Key : " + key + " found !", 3) + MyDebug("Key : " + key + " found in memory cache!", 3) ProcedureReturn PBMap\MemCache\Images()\nImage Else - MyDebug("Key : " + key + " Try HDD!") + MyDebug("Trying to load from HDD " + CacheFile) timg = GetTileFromHDD(CacheFile.s) - If timg <> -1 - AddMapElement(PBMap\MemCache\Images(),key) - PBMap\MemCache\Images()\nImage = timg - EndIf - ProcedureReturn timg + If timg <> -1 + MyDebug("Key : " + key + " found on HDD") + LockMutex(PBMap\TileThreadMutex) + AddMapElement(PBMap\MemCache\Images(), key) + PBMap\MemCache\Images()\nImage = timg + UnlockMutex(PBMap\TileThreadMutex) + Else + MyDebug("Key : " + key + " not found") + EndIf + ProcedureReturn timg EndIf EndProcedure - Procedure.i GetTileFromWeb(Zoom.i, XTile.i, YTile.i, CacheFile.s,layer.i) + Procedure.i GetTileFromWeb(Zoom.i, XTile.i, YTile.i, CacheFile.s, Layer.i) Protected *Buffer Protected nImage.i = -1 - Protected FileHandle.i,timg - Protected TileURL.s = PBMap\ServerURL(layer) + Str(Zoom) + "/" + Str(XTile) + "/" + Str(YTile) + ".png" - MyDebug("Check if we have this image on Web", 3) + Protected FileSize.i, timg + Protected TileURL.s = PBMap\ServerURL(Layer) + Str(Zoom) + "/" + Str(XTile) + "/" + Str(YTile) + ".png" If Proxy - FileHandle = CurlReceiveHTTPToFile(TileURL, CacheFile, ProxyURL$, ProxyPort$, ProxyUser$, ProxyPassword$) - If FileHandle + FileSize = CurlReceiveHTTPToFile(TileURL, CacheFile, ProxyURL$, ProxyPort$, ProxyUser$, ProxyPassword$) + If FileSize > 0 + MyDebug("Loaded from web " + TileURL + " as CacheFile " + CacheFile, 3) nImage = GetTileFromHDD(CacheFile) Else - MyDebug("File " + TileURL + " not correctly received with Curl and proxy", 3) + MyDebug("Problem loading from web " + TileURL, 3) EndIf Else *Buffer = ReceiveHTTPMemory(TileURL) ;TODO to thread by using #PB_HTTP_Asynchronous If *Buffer nImage = CatchImage(#PB_Any, *Buffer, MemorySize(*Buffer)) If IsImage(nImage) - MyDebug("Load from web " + TileURL + " as Tile nb " + nImage, 3) - ; Debug "url: " + TileURL - ; Debug "cache file: " + CacheFile - ; timg = LoadImage(#PB_Any,CacheFile) - ; If timg - ; StartDrawing(ImageOutput(timg)) - ; DrawImage(ImageID(nimage)) - ; StopDrawing() - ; SaveImage(timg, CacheFile, #PB_ImagePlugin_PNG) - ; FreeImage(timg) - ; Else - SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG) - FreeMemory(*Buffer) - ; EndIf + ; Debug "url: " + TileURL + ; Debug "cache file: " + CacheFile + ; timg = LoadImage(#PB_Any,CacheFile) + ; If timg + ; StartDrawing(ImageOutput(timg)) + ; DrawImage(ImageID(nimage)) + ; StopDrawing() + ; SaveImage(timg, CacheFile, #PB_ImagePlugin_PNG) + ; FreeImage(timg) + ; Else + If SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG) + MyDebug("Loaded from web " + TileURL + " as CacheFile " + CacheFile, 3) + Else + MyDebug("Loaded from web " + TileURL + " but cannot save to CacheFile " + CacheFile, 3) + EndIf + FreeMemory(*Buffer) + ; EndIf Else - MyDebug("Can't catch image " + TileURL, 3) + MyDebug("Can't catch image loaded from web " + TileURL, 3) nImage = -1 ;ShowMemoryViewer(*Buffer, MemorySize(*Buffer)) EndIf Else - ; Debug("ReceiveHTTPMemory's buffer is empty") - MyDebug("ReceiveHTTPMemory's buffer is empty", 3) + MyDebug(" Problem loading from web " + TileURL, 3) EndIf EndIf ProcedureReturn nImage @@ -544,20 +542,19 @@ Module PBMap Procedure GetImageThread(*Tile.Tile) Protected nImage.i = -1 - nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, *Tile\CacheFile,*tile\layer) + nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, *Tile\CacheFile, *Tile\Layer) If nImage <> -1 LockMutex(PBMap\TileThreadMutex) + AddMapElement(PBMap\MemCache\Images(), *Tile\key) ;Add the image to the cache, once PBMap\MemCache\Images(*Tile\key)\nImage = nImage UnlockMutex(PBMap\TileThreadMutex) - MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") - MyDebug("With the following key : " + *Tile\key) + MyDebug("Image key : " + *Tile\key + " added in memory cache!", 3) *Tile\nImage = nImage - PostEvent(#PB_Event_Gadget, PBMap\window,PBmap\Gadget, #PB_MAP_REDRAW, *Tile) - Else - MyDebug("Error GetImageThread procedure, image not loaded - " + *Tile\key) - ;nImage = -1 - EndIf - + PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW, *Tile) ;If image is loaded from web, redraw + Else + PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_RETRY, *Tile) ;If image is not loaded, retry + EndIf + EndProcedure Procedure DrawTile(*Tile.Tile) @@ -577,10 +574,10 @@ Module PBMap MyDebug(" at coords " + Str(x) + "," + Str(y)) EndProcedure - Procedure DrawTiles(*Drawing.DrawingParameters,layer.i,alpha.i=255) + Procedure DrawTiles(*Drawing.DrawingParameters, Layer.i, alpha.i=255) ;DisableDebugger Protected x.i, y.i,kq.q - Protected tx = Int(*Drawing\Position\x) ;Don't forget the Int() ! + Protected tx = Int(*Drawing\Position\x) ;Don't forget the Int() ! Protected ty = Int(*Drawing\Position\y) Protected nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point Protected ny = *Drawing\CenterY / PBMap\TileSize @@ -588,61 +585,53 @@ Module PBMap MyDebug("Drawing tiles") For y = - ny - 1 To ny + 1 For x = - nx - 1 To nx + 1 - px = *Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX - py = *Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY - tilex = ((tx+x) % (1<< PBMap\Zoom)) - tiley = ty+y - kq = layer | (pbmap\zoom << 8) | (tilex << 16) | (tiley << 36) - key = Str(kq) - CacheFile = PBMap\HDDCachePath + key + ".png" - - - img = GetLocalTile(key, CacheFile) - If img <> -1 - MovePathCursor(px, py) - DrawVectorImage(ImageID(img),alpha) - Else - MovePathCursor(px, py) - DrawVectorImage(ImageID(PBMap\ImgLoading),alpha) - Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) - If *NewTile - With *NewTile - ;Keep a track of tiles (especially to free memory) - ;TODO : not sure if it really needs a mutex here, i'm still trying to find where the white tile issue is coming from - ;a tile download is either getting clobbered or timing out or getting kicked and it results in a corrupted png file on linux - LockMutex(PBMap\TileThreadMutex) - AddElement(PBMap\TilesThreads()) - PBMap\TilesThreads()\Tile = *NewTile - UnlockMutex(PBMap\TileThreadMutex) - ;New tile parameters - \Position\x = px - \Position\y = py - \PBMapTileX = tilex - \PBMapTileY = tiley - \PBMapZoom = PBMap\Zoom - \key = key - \CacheFile = CacheFile - \layer = layer - LockMutex(PBMap\TileThreadMutex) - AddMapElement(PBMap\MemCache\Images(), \key) ;Add the image to the cache, once in this loop - PBMap\MemCache\Images()\nImage = PBMap\ImgLoading - UnlockMutex(PBMap\TileThreadMutex) - \GetImageThread = CreateThread(@GetImageThread(), *NewTile) - PBMap\TilesThreads()\GetImageThread = \GetImageThread - myDebug(" Creating get image thread nb " + Str(\GetImageThread)) - EndWith - Else - MyDebug(" Error, can't create a new tile") - Break 2 - EndIf - EndIf - - Next - - Next + px = *Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX + py = *Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY + tilex = ((tx+x) % (1<< PBMap\Zoom)) + tiley = ty+y + kq = Layer | (pbmap\zoom << 8) | (tilex << 16) | (tiley << 36) + key = Str(kq) + CacheFile = PBMap\HDDCachePath + key + ".png" + + img = GetLocalTile(key, CacheFile) + If img <> -1 + MovePathCursor(px, py) + DrawVectorImage(ImageID(img), alpha) + Else + MovePathCursor(px, py) + DrawVectorImage(ImageID(PBMap\ImgLoading), alpha) + Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) + If *NewTile + With *NewTile + ;Keep a track of tiles (especially to free memory) + ;TODO : not sure if it really needs a mutex here, i'm still trying to find where the white tile issue is coming from + ;a tile download is either getting clobbered or timing out or getting kicked and it results in a corrupted png file on linux + LockMutex(PBMap\TileThreadMutex) + AddElement(PBMap\TilesThreads()) + PBMap\TilesThreads()\Tile = *NewTile + UnlockMutex(PBMap\TileThreadMutex) + ;New tile parameters + \Position\x = px + \Position\y = py + \PBMapTileX = tilex + \PBMapTileY = tiley + \PBMapZoom = PBMap\Zoom + \key = key + \CacheFile = CacheFile + \Layer = Layer + \GetImageThread = CreateThread(@GetImageThread(), *NewTile) + PBMap\TilesThreads()\GetImageThread = \GetImageThread + myDebug(" Creating get image thread nb " + Str(\GetImageThread)) + EndWith + Else + MyDebug(" Error, can't create a new tile") + Break 2 + EndIf + EndIf + Next + Next - - ;Free tile memory + ;Free tile memory ;TODO : maybe get out this proc from drawtiles in a special "free ressources" task ForEach PBMap\TilesThreads() ;Check if there's no more loading thread @@ -651,7 +640,7 @@ Module PBMap DeleteElement(PBMap\TilesThreads()) EndIf Next - ;EnableDebugger + EndProcedure ; ;-**** Clean Mem Cache @@ -1001,95 +990,94 @@ Module PBMap ProcedureReturn Value EndProcedure - Procedure Event(Event.l) - Protected Gadget.i + Procedure Events() +; Protected Gadget.i Protected MouseX.i, MouseY.i Protected Marker.Position Protected *Drawing.DrawingParameters - If IsGadget(PBMap\Gadget) And GadgetType(PBMap\Gadget) = #PB_GadgetType_Canvas - Select Event - Case #PB_Event_Gadget ;{ - Gadget = EventGadget() - Select Gadget - Case PBMap\Gadget - Select EventType() - 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)) - Else - ;Absolute zoom (centered on the center of the map) - SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta), #PB_Relative) - EndIf - Case #PB_EventType_LeftButtonDown - ;Check if we select a marker - MouseX = PBMap\Position\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) - MouseY = PBMap\Position\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) - ForEach PBMap\Marker() - LatLon2XY(@PBMap\Marker()\Location, @Marker) - Marker\x * PBMap\TileSize - Marker\y * PBMap\TileSize - If Distance(Marker\x, Marker\y, MouseX, MouseY) < 8 - PBMap\EditMarkerIndex = ListIndex(PBMap\Marker()) - Break - EndIf - Next - ;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 - 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 - PBMap\Moving = #True - ;Move marker - If PBMap\EditMarkerIndex > -1 - SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) - LatLon2XY(@PBMap\Marker()\Location, @Marker) - Marker\x + MouseX / PBMap\TileSize - Marker\y + MouseY / PBMap\TileSize - XY2LatLon(@Marker, @PBMap\Marker()\Location) - Else - ;New move values - PBMap\Position\x - MouseX - PBMap\Position\y - MouseY - ;PBMap tile position in tile.decimal - PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize - PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize - PBMap\Drawing\PassNb = 1 - XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) - ;If CallBackLocation send Location to function - If PBMap\CallBackLocation > 0 - CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) - EndIf - EndIf - Drawing() - PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) - PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) - EndIf - Case #PB_EventType_LeftButtonUp - PBMap\Moving = #False - PBMap\MoveStartingPoint\x = - 1 - If PBMap\EditMarkerIndex > -1 - PBMap\EditMarkerIndex = -1 - Else ;Move Map - PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize - PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize - MyDebug("PBMap\Drawing\Position\x " + Str(PBMap\Drawing\Position\x) + " ; PBMap\Drawing\Position\y " + Str(PBMap\Drawing\Position\y) ) - XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) - Drawing() - EndIf - Case #PB_MAP_REDRAW - Drawing() - EndSelect - EndSelect - EndSelect - Else - MessageRequester("Module PBMap", "You must use PBMapGadget before", #PB_MessageRequester_Ok ) - End - EndIf + Select EventType() + 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)) + Else + ;Absolute zoom (centered on the center of the map) + SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta), #PB_Relative) + EndIf + Case #PB_EventType_LeftButtonDown + ;Check if we select a marker + MouseX = PBMap\Position\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) + MouseY = PBMap\Position\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) + ForEach PBMap\Marker() + LatLon2XY(@PBMap\Marker()\Location, @Marker) + Marker\x * PBMap\TileSize + Marker\y * PBMap\TileSize + If Distance(Marker\x, Marker\y, MouseX, MouseY) < 8 + PBMap\EditMarkerIndex = ListIndex(PBMap\Marker()) + Break + EndIf + Next + ;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 + 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 + PBMap\Moving = #True + ;Move marker + If PBMap\EditMarkerIndex > -1 + SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) + LatLon2XY(@PBMap\Marker()\Location, @Marker) + Marker\x + MouseX / PBMap\TileSize + Marker\y + MouseY / PBMap\TileSize + XY2LatLon(@Marker, @PBMap\Marker()\Location) + Else + ;New move values + PBMap\Position\x - MouseX + PBMap\Position\y - MouseY + ;PBMap tile position in tile.decimal + PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize + PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize + PBMap\Drawing\PassNb = 1 + XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) + ;If CallBackLocation send Location to function + If PBMap\CallBackLocation > 0 + CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) + EndIf + EndIf + Drawing() + PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) + PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) + EndIf + Case #PB_EventType_LeftButtonUp + PBMap\Moving = #False + PBMap\MoveStartingPoint\x = - 1 + If PBMap\EditMarkerIndex > -1 + PBMap\EditMarkerIndex = -1 + Else ;Move Map + PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize + PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize + MyDebug("PBMap\Drawing\Position\x " + Str(PBMap\Drawing\Position\x) + " ; PBMap\Drawing\Position\y " + Str(PBMap\Drawing\Position\y) ) + XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) + Drawing() + EndIf + Case #PB_MAP_REDRAW + Drawing() + EndSelect EndProcedure + + Procedure MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) + If Gadget = #PB_Any + PBMap\Gadget = CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) ;#PB_Canvas_Keyboard has to be set for mousewheel to work on windows + Else + PBMap\Gadget = Gadget + CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) + EndIf + BindGadgetEvent(PBMap\Gadget, @Events()) + EndProcedure + EndModule ;-Exemple @@ -1172,10 +1160,10 @@ CompilerIf #PB_Compiler_IsMainFile LoadFont(1, "Arial", 12, #PB_Font_Bold) TextGadget(#Text_1, 530, 50, 60, 15, "Movements") - ButtonGadget(#Gdt_Left, 550, 100, 30, 30, Chr($E7)) : SetGadgetFont(#Gdt_Left, FontID(0)) - ButtonGadget(#Gdt_Right, 610, 100, 30, 30, Chr($E8)) : SetGadgetFont(#Gdt_Right, FontID(0)) - ButtonGadget(#Gdt_Up, 580, 070, 30, 30, Chr($E9)) : SetGadgetFont(#Gdt_Up, FontID(0)) - ButtonGadget(#Gdt_Down, 580, 130, 30, 30, Chr($EA)) : SetGadgetFont(#Gdt_Down, FontID(0)) + 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)) + ButtonGadget(#Gdt_Down, 580, 130, 30, 30, Chr($25BC)) : SetGadgetFont(#Gdt_Down, FontID(0)) TextGadget(#Text_2, 530, 160, 60, 15, "Zoom") ButtonGadget(#Button_4, 550, 180, 50, 30, " + ") : SetGadgetFont(#Button_4, FontID(1)) ButtonGadget(#Button_5, 600, 180, 50, 30, " - ") : SetGadgetFont(#Button_5, FontID(1)) @@ -1201,7 +1189,6 @@ CompilerIf #PB_Compiler_IsMainFile Repeat Event = WaitWindowEvent() - PBMap::Event(Event) Select Event Case #PB_Event_CloseWindow : Quit = 1 Case #PB_Event_Gadget ;{ @@ -1237,10 +1224,9 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1219 -; FirstLine = 1193 +; CursorPosition = 452 +; FirstLine = 442 ; Folding = ---------- ; EnableThread ; EnableXP -; DisableDebugger ; EnableUnicode \ No newline at end of file From afab1a76d1e4977ae5eb17312fe0096534f67523 Mon Sep 17 00:00:00 2001 From: djes Date: Mon, 29 Aug 2016 14:03:34 +0200 Subject: [PATCH 16/49] Debuging.... --- PBMap.pb | 138 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 125 insertions(+), 13 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 021903e..e9a60c7 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -30,8 +30,8 @@ DeclareModule PBMap ;-Show debug infos Global Verbose = 1 Global MyDebugLevel = 3 - ;-Proxy ON/OFF - Global Proxy = #True + ;-Proxy ON/OFF + Global Proxy = #False #SCALE_NAUTICAL = 1 #SCALE_KM = 0 @@ -449,16 +449,16 @@ Module PBMap EndIf EndProcedure - Procedure LoadErrorHandler() - MessageRequester("Error", "") - EndProcedure +; Procedure LoadErrorHandler() +; MessageRequester("Error", "") +; EndProcedure Procedure.i GetTileFromHDD(CacheFile.s) Protected nImage.i If FileSize(CacheFile) > 0 - OnErrorCall(@LoadErrorHandler()) + ; OnErrorCall(@LoadErrorHandler()) nImage = LoadImage(#PB_Any, CacheFile) - OnErrorDefault() + ; OnErrorDefault() If IsImage(nImage) MyDebug("Success loading " + CacheFile + " as nImage " + Str(nImage), 3) ProcedureReturn nImage @@ -991,7 +991,6 @@ Module PBMap EndProcedure Procedure Events() -; Protected Gadget.i Protected MouseX.i, MouseY.i Protected Marker.Position Protected *Drawing.DrawingParameters @@ -1063,10 +1062,116 @@ Module PBMap XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) Drawing() EndIf - Case #PB_MAP_REDRAW - Drawing() + Case #PB_MAP_REDRAW + Debug "Redraw" + Drawing() + Case #PB_MAP_RETRY + Debug "Reload" + Drawing() EndSelect EndProcedure + +; Procedure Event_MouseWheel() +; Protected MouseX.i, MouseY.i +; Protected Marker.Position +; Protected *Drawing.DrawingParameters +; +; 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)) +; Else +; ;Absolute zoom (centered on the center of the map) +; SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta), #PB_Relative) +; EndIf +; +; EndProcedure +; Procedure Event_LeftButtonDown() +; Protected MouseX.i, MouseY.i +; Protected Marker.Position +; Protected *Drawing.DrawingParameters +; +; ;Check if we select a marker +; MouseX = PBMap\Position\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) +; MouseY = PBMap\Position\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) +; ForEach PBMap\Marker() +; LatLon2XY(@PBMap\Marker()\Location, @Marker) +; Marker\x * PBMap\TileSize +; Marker\y * PBMap\TileSize +; If Distance(Marker\x, Marker\y, MouseX, MouseY) < 8 +; PBMap\EditMarkerIndex = ListIndex(PBMap\Marker()) +; Break +; EndIf +; Next +; ;Mem cursor Coord +; PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) +; PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) +; +; +; EndProcedure +; Procedure Event_MouseMove() +; Protected MouseX.i, MouseY.i +; Protected Marker.Position +; Protected *Drawing.DrawingParameters +; 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 +; PBMap\Moving = #True +; ;Move marker +; If PBMap\EditMarkerIndex > -1 +; SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) +; LatLon2XY(@PBMap\Marker()\Location, @Marker) +; Marker\x + MouseX / PBMap\TileSize +; Marker\y + MouseY / PBMap\TileSize +; XY2LatLon(@Marker, @PBMap\Marker()\Location) +; Else +; ;New move values +; PBMap\Position\x - MouseX +; PBMap\Position\y - MouseY +; ;PBMap tile position in tile.decimal +; PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize +; PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize +; PBMap\Drawing\PassNb = 1 +; XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) +; ;If CallBackLocation send Location to function +; If PBMap\CallBackLocation > 0 +; CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) +; EndIf +; EndIf +; Drawing() +; PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) +; PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) +; EndIf +; +; EndProcedure +; Procedure Event_LeftButtonUp() +; Protected MouseX.i, MouseY.i +; Protected Marker.Position +; Protected *Drawing.DrawingParameters +; +; PBMap\Moving = #False +; PBMap\MoveStartingPoint\x = - 1 +; If PBMap\EditMarkerIndex > -1 +; PBMap\EditMarkerIndex = -1 +; Else ;Move Map +; PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize +; PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize +; MyDebug("PBMap\Drawing\Position\x " + Str(PBMap\Drawing\Position\x) + " ; PBMap\Drawing\Position\y " + Str(PBMap\Drawing\Position\y) ) +; XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) +; Drawing() +; EndIf +; +; EndProcedure +; +; Procedure Event_Redraw() +; Debug "Redraw" +; Drawing() +; EndProcedure; + +; Procedure Event_Retry() +; Debug "Reload" +; Delay(2000) +; Drawing() +; EndProcedure Procedure MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) If Gadget = #PB_Any @@ -1076,6 +1181,13 @@ Module PBMap CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) EndIf BindGadgetEvent(PBMap\Gadget, @Events()) + ;BindGadgetEvent(PBMap\Gadget, @Event_MouseWheel(), #PB_EventType_MouseWheel) + ;BindGadgetEvent(PBMap\Gadget, @Event_LeftButtonDown(), #PB_EventType_LeftButtonDown) + ;BindGadgetEvent(PBMap\Gadget, @Event_MouseMove(), #PB_EventType_MouseMove) + ;BindGadgetEvent(PBMap\Gadget, @Event_LeftButtonUp(), #PB_EventType_LeftButtonUp) + ;BindGadgetEvent(PBMap\Gadget, Event_Redraw(), #PB_MAP_REDRAW) + ;BindGadgetEvent(PBMap\Gadget, @Event_Retry(), #PB_MAP_RETRY) + EndProcedure EndModule @@ -1224,9 +1336,9 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 452 -; FirstLine = 442 -; Folding = ---------- +; CursorPosition = 1183 +; FirstLine = 1033 +; Folding = --------- ; EnableThread ; EnableXP ; EnableUnicode \ No newline at end of file From 612fb30bab13822e982e51d75133239ec25260aa Mon Sep 17 00:00:00 2001 From: djes Date: Mon, 29 Aug 2016 15:49:44 +0200 Subject: [PATCH 17/49] Cleanup --- PBMap.pb | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index e9a60c7..70d2aa8 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -1072,10 +1072,6 @@ Module PBMap EndProcedure ; Procedure Event_MouseWheel() -; Protected MouseX.i, MouseY.i -; Protected Marker.Position -; Protected *Drawing.DrawingParameters -; ; 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)) @@ -1083,12 +1079,10 @@ Module PBMap ; ;Absolute zoom (centered on the center of the map) ; SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta), #PB_Relative) ; EndIf -; ; EndProcedure + ; Procedure Event_LeftButtonDown() -; Protected MouseX.i, MouseY.i ; Protected Marker.Position -; Protected *Drawing.DrawingParameters ; ; ;Check if we select a marker ; MouseX = PBMap\Position\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) @@ -1105,13 +1099,11 @@ Module PBMap ; ;Mem cursor Coord ; PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) ; PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) -; -; ; EndProcedure + ; Procedure Event_MouseMove() ; Protected MouseX.i, MouseY.i ; Protected Marker.Position -; Protected *Drawing.DrawingParameters ; 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 @@ -1141,13 +1133,11 @@ Module PBMap ; PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) ; PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) ; EndIf -; ; EndProcedure + ; Procedure Event_LeftButtonUp() ; Protected MouseX.i, MouseY.i ; Protected Marker.Position -; Protected *Drawing.DrawingParameters -; ; PBMap\Moving = #False ; PBMap\MoveStartingPoint\x = - 1 ; If PBMap\EditMarkerIndex > -1 @@ -1159,7 +1149,6 @@ Module PBMap ; XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) ; Drawing() ; EndIf -; ; EndProcedure ; ; Procedure Event_Redraw() @@ -1336,8 +1325,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1183 -; FirstLine = 1033 +; CursorPosition = 1197 +; FirstLine = 1154 ; Folding = --------- ; EnableThread ; EnableXP From e25a66206b2db0e75180c77b0ac8eb784c9dab45 Mon Sep 17 00:00:00 2001 From: djes Date: Mon, 29 Aug 2016 16:47:48 +0200 Subject: [PATCH 18/49] Drawing based on timer --- PBMap.pb | 159 ++++++++++++++----------------------------------------- 1 file changed, 40 insertions(+), 119 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 70d2aa8..6eb29e4 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -135,6 +135,7 @@ Module PBMap Window.i ; Parent Window Gadget.i ; Canvas Gadget Id Font.i ; Font to uses when write on the map + Timer.i TargetLocation.Location ; Latitude and Longitude from focus point Drawing.DrawingParameters ; Drawing parameters based on focus point ; @@ -155,6 +156,7 @@ Module PBMap HDDCachePath.S ; Path where to load and save tiles downloaded from server MemCache.TileMemCach ; Images in memory cache ; + Redraw.i Moving.i Dirty.i ; To signal that drawing need a refresh ; @@ -284,6 +286,7 @@ Module PBMap PBMap\EditMarkerIndex = -1 ;Initialised with "no marker selected" PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold) PBMap\Window = Window + PBMap\Timer = 1 PBMap\Options\WheelMouseRelative = #True SetMapServer("http://tile.openstreetmap.org/") @@ -550,9 +553,11 @@ Module PBMap UnlockMutex(PBMap\TileThreadMutex) MyDebug("Image key : " + *Tile\key + " added in memory cache!", 3) *Tile\nImage = nImage - PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW, *Tile) ;If image is loaded from web, redraw + PBMap\Dirty = #True +; PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW, *Tile) ;If image is loaded from web, redraw Else - PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_RETRY, *Tile) ;If image is not loaded, retry + PBMap\Dirty = #True +; PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_RETRY, *Tile) ;If image is not loaded, retry EndIf EndProcedure @@ -585,6 +590,9 @@ Module PBMap MyDebug("Drawing tiles") For y = - ny - 1 To ny + 1 For x = - nx - 1 To nx + 1 +; If PBMap\Moving ;If drawing was threaded, this would exit the loop when the user is moving +; Break 2 +; EndIf px = *Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX py = *Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY tilex = ((tx+x) % (1<< PBMap\Zoom)) @@ -708,7 +716,6 @@ Module PBMap MovePathCursor(x,y+12) AddPathLine(x+128,y+10) StrokePath(1) - EndProcedure Procedure TrackPointer(x.i, y.i,dist.l) @@ -802,6 +809,8 @@ Module PBMap Procedure Drawing() Protected *Drawing.DrawingParameters = @PBMap\Drawing Protected Px.d, Py.d,a + PBMap\Dirty = #False + PBMap\Redraw = #False ;Precalc some values *Drawing\CenterX = GadgetWidth(PBMap\Gadget) / 2 *Drawing\CenterY = GadgetHeight(PBMap\Gadget) / 2 @@ -828,11 +837,15 @@ Module PBMap DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) MovePathCursor(50,80) DrawVectorText(Str(ListSize(PBMap\TilesThreads()))) - ;If PBMap\Options\ShowScale - DrawScale(*Drawing,10,GadgetHeight(PBMAP\Gadget)-20,192) + DrawScale(*Drawing,10,GadgetHeight(PBMAP\Gadget)-20,192) ;EndIf - StopVectorDrawing() + StopVectorDrawing() + ;If there was a problem while drawing, redraw + If PBMap\Dirty + PBMap\Redraw = #True + ;PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW) + EndIf EndProcedure Procedure Refresh() @@ -990,11 +1003,10 @@ Module PBMap ProcedureReturn Value EndProcedure - Procedure Events() + Procedure CanvasEvents() Protected MouseX.i, MouseY.i Protected Marker.Position - Protected *Drawing.DrawingParameters - + PBMap\Moving = #False Select EventType() Case #PB_EventType_MouseWheel If PBMap\Options\WheelMouseRelative @@ -1021,10 +1033,10 @@ Module PBMap 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 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 - PBMap\Moving = #True ;Move marker If PBMap\EditMarkerIndex > -1 SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) @@ -1046,12 +1058,11 @@ Module PBMap CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) EndIf EndIf - Drawing() + PBMap\Redraw = #True PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) EndIf Case #PB_EventType_LeftButtonUp - PBMap\Moving = #False PBMap\MoveStartingPoint\x = - 1 If PBMap\EditMarkerIndex > -1 PBMap\EditMarkerIndex = -1 @@ -1060,108 +1071,23 @@ Module PBMap PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize MyDebug("PBMap\Drawing\Position\x " + Str(PBMap\Drawing\Position\x) + " ; PBMap\Drawing\Position\y " + Str(PBMap\Drawing\Position\y) ) XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) - Drawing() + PBMap\Redraw = #True EndIf - Case #PB_MAP_REDRAW + Case #PB_MAP_REDRAW Debug "Redraw" - Drawing() - Case #PB_MAP_RETRY - Debug "Reload" - Drawing() + PBMap\Redraw = #True + Case #PB_MAP_RETRY + Debug "Reload" + PBMap\Redraw = #True EndSelect EndProcedure -; Procedure Event_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)) -; Else -; ;Absolute zoom (centered on the center of the map) -; SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta), #PB_Relative) -; EndIf -; EndProcedure + Procedure TimerEvents() + If EventTimer() = PBMap\Timer And PBMap\Redraw + Drawing() + EndIf + EndProcedure -; Procedure Event_LeftButtonDown() -; Protected Marker.Position -; -; ;Check if we select a marker -; MouseX = PBMap\Position\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) -; MouseY = PBMap\Position\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) -; ForEach PBMap\Marker() -; LatLon2XY(@PBMap\Marker()\Location, @Marker) -; Marker\x * PBMap\TileSize -; Marker\y * PBMap\TileSize -; If Distance(Marker\x, Marker\y, MouseX, MouseY) < 8 -; PBMap\EditMarkerIndex = ListIndex(PBMap\Marker()) -; Break -; EndIf -; Next -; ;Mem cursor Coord -; PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) -; PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) -; EndProcedure - -; Procedure Event_MouseMove() -; Protected MouseX.i, MouseY.i -; Protected Marker.Position -; 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 -; PBMap\Moving = #True -; ;Move marker -; If PBMap\EditMarkerIndex > -1 -; SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) -; LatLon2XY(@PBMap\Marker()\Location, @Marker) -; Marker\x + MouseX / PBMap\TileSize -; Marker\y + MouseY / PBMap\TileSize -; XY2LatLon(@Marker, @PBMap\Marker()\Location) -; Else -; ;New move values -; PBMap\Position\x - MouseX -; PBMap\Position\y - MouseY -; ;PBMap tile position in tile.decimal -; PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize -; PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize -; PBMap\Drawing\PassNb = 1 -; XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) -; ;If CallBackLocation send Location to function -; If PBMap\CallBackLocation > 0 -; CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) -; EndIf -; EndIf -; Drawing() -; PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) -; PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) -; EndIf -; EndProcedure - -; Procedure Event_LeftButtonUp() -; Protected MouseX.i, MouseY.i -; Protected Marker.Position -; PBMap\Moving = #False -; PBMap\MoveStartingPoint\x = - 1 -; If PBMap\EditMarkerIndex > -1 -; PBMap\EditMarkerIndex = -1 -; Else ;Move Map -; PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize -; PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize -; MyDebug("PBMap\Drawing\Position\x " + Str(PBMap\Drawing\Position\x) + " ; PBMap\Drawing\Position\y " + Str(PBMap\Drawing\Position\y) ) -; XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) -; Drawing() -; EndIf -; EndProcedure -; -; Procedure Event_Redraw() -; Debug "Redraw" -; Drawing() -; EndProcedure; - -; Procedure Event_Retry() -; Debug "Reload" -; Delay(2000) -; Drawing() -; EndProcedure - Procedure MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) If Gadget = #PB_Any PBMap\Gadget = CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) ;#PB_Canvas_Keyboard has to be set for mousewheel to work on windows @@ -1169,14 +1095,9 @@ Module PBMap PBMap\Gadget = Gadget CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) EndIf - BindGadgetEvent(PBMap\Gadget, @Events()) - ;BindGadgetEvent(PBMap\Gadget, @Event_MouseWheel(), #PB_EventType_MouseWheel) - ;BindGadgetEvent(PBMap\Gadget, @Event_LeftButtonDown(), #PB_EventType_LeftButtonDown) - ;BindGadgetEvent(PBMap\Gadget, @Event_MouseMove(), #PB_EventType_MouseMove) - ;BindGadgetEvent(PBMap\Gadget, @Event_LeftButtonUp(), #PB_EventType_LeftButtonUp) - ;BindGadgetEvent(PBMap\Gadget, Event_Redraw(), #PB_MAP_REDRAW) - ;BindGadgetEvent(PBMap\Gadget, @Event_Retry(), #PB_MAP_RETRY) - + BindGadgetEvent(PBMap\Gadget, @CanvasEvents()) + AddWindowTimer(PBMap\Window, PBMap\Timer, 20) + BindEvent(#PB_Event_Timer, @TimerEvents()) EndProcedure EndModule @@ -1325,9 +1246,9 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1197 -; FirstLine = 1154 -; Folding = --------- +; CursorPosition = 603 +; FirstLine = 563 +; Folding = ---------- ; EnableThread ; EnableXP ; EnableUnicode \ No newline at end of file From ff2bcc0f5912bdfefcdf1b0e4d1f82b913b91000 Mon Sep 17 00:00:00 2001 From: djes Date: Mon, 29 Aug 2016 17:10:35 +0200 Subject: [PATCH 19/49] Redraw bugfix --- PBMap.pb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 6eb29e4..c3def8b 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -842,10 +842,10 @@ Module PBMap ;EndIf StopVectorDrawing() ;If there was a problem while drawing, redraw - If PBMap\Dirty - PBMap\Redraw = #True - ;PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW) - EndIf +; If PBMap\Dirty +; PBMap\Redraw = #True +; ;PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW) +; EndIf EndProcedure Procedure Refresh() @@ -1083,7 +1083,7 @@ Module PBMap EndProcedure Procedure TimerEvents() - If EventTimer() = PBMap\Timer And PBMap\Redraw + If EventTimer() = PBMap\Timer And (PBMap\Redraw Or PBMap\Dirty) Drawing() EndIf EndProcedure @@ -1246,8 +1246,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 603 -; FirstLine = 563 +; CursorPosition = 840 +; FirstLine = 819 ; Folding = ---------- ; EnableThread ; EnableXP From c815fbf6f2112052acd2c190508e80e65c64702e Mon Sep 17 00:00:00 2001 From: djes Date: Tue, 30 Aug 2016 10:35:41 +0200 Subject: [PATCH 20/49] Complete rework of tile loading/thread --- PBMap.pb | 342 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 183 insertions(+), 159 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index c3def8b..6f0fc52 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -85,6 +85,7 @@ Module PBMap key.s CacheFile.s GetImageThread.i + RetryNb.i Layer.i EndStructure @@ -111,6 +112,7 @@ Module PBMap Structure ImgMemCach nImage.i + *Tile.Tile ;Location.Location ;Mutex.i EndStructure @@ -138,13 +140,13 @@ Module PBMap Timer.i TargetLocation.Location ; Latitude and Longitude from focus point Drawing.DrawingParameters ; Drawing parameters based on focus point - ; + ; CallBackLocation.i ; @Procedure(latitude.d,lontitude.d) CallBackMainPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) - ; + ; Position.PixelPosition ; Actual focus point coords in pixels (global) MoveStartingPoint.PixelPosition ; Start mouse position coords when dragging the map - ; + ; Array ServerURL.s(0) ; Web URL ex: http://tile.openstreetmap.org/ NumberOfMapLayers.i ; The number of map tile layers; @@ -152,16 +154,17 @@ Module PBMap ZoomMax.i ; Max Zoom supported by server Zoom.i ; Current zoom TileSize.i ; Tile size downloaded on the server ex : 256 - ; + ; HDDCachePath.S ; Path where to load and save tiles downloaded from server MemCache.TileMemCach ; Images in memory cache - ; + ; + OnStage.i Redraw.i Moving.i Dirty.i ; To signal that drawing need a refresh - ; + ; MainDrawingThread.i - List TilesThreads.TileThread() + ;List TilesThreads.TileThread() TileThreadMutex.i; ;Mutex to protect resources List track.Location() ; To display a GPX track List Marker.Marker() ; To diplay marker @@ -289,7 +292,7 @@ Module PBMap PBMap\Timer = 1 PBMap\Options\WheelMouseRelative = #True SetMapServer("http://tile.openstreetmap.org/") - + ;-Preferences ;Use this to create and customize your preferences file for the first time ; CreatePreferences(GetHomeDirectory() + "PBMap.prefs") @@ -332,18 +335,21 @@ Module PBMap ;Wait for loading threads to finish nicely. Passed 2 seconds, kills them. Protected TimeCounter = ElapsedMilliseconds() Repeat - ResetList(PBMap\TilesThreads()) - While NextElement(PBMap\TilesThreads()) - If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 - FreeMemory(PBMap\TilesThreads()\Tile) - DeleteElement(PBMap\TilesThreads()) - ElseIf ElapsedMilliseconds() - TimeCounter > 2000 - ;Should not occur - KillThread(PBMap\TilesThreads()\GetImageThread) + ForEach PBMap\MemCache\Images() + If PBMap\MemCache\Images()\Tile <> 0 + If IsThread(PBMap\MemCache\Images()\Tile\GetImageThread) + PBMap\MemCache\Images()\Tile\RetryNb = 0 + If ElapsedMilliseconds() - TimeCounter > 2000 + ;Should not occur + KillThread(PBMap\MemCache\Images()\Tile\GetImageThread) + EndIf + EndIf + Else + DeleteMapElement(PBMap\MemCache\Images()) EndIf - Wend + Next Delay(10) - Until ListSize(PBMap\TilesThreads()) = 0 + Until MapSize(PBMap\MemCache\Images()) = 0 curl_global_cleanup() EndProcedure @@ -360,7 +366,7 @@ Module PBMap Result = Sqr( (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) ProcedureReturn Result EndProcedure - + ;*** Converts coords to tile.decimal ;Warning, structures used in parameters are not tested Procedure LatLon2XY(*Location.Location, *Coords.Position) @@ -415,7 +421,7 @@ Module PBMap x2 = (PBMap\TargetLocation\Longitude+180)*(mapWidth/360) ; convert from degrees To radians latRad = PBMap\TargetLocation\Latitude*#PI/180; - ; get y value + ; get y value mercN = Log(Tan((#PI/4)+(latRad/2))) y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)); *Pixel\x=GadgetWidth(PBMap\Gadget)/2 - (x2-x1) @@ -452,16 +458,16 @@ Module PBMap EndIf EndProcedure -; Procedure LoadErrorHandler() -; MessageRequester("Error", "") -; EndProcedure + ; Procedure LoadErrorHandler() + ; MessageRequester("Error", "") + ; EndProcedure Procedure.i GetTileFromHDD(CacheFile.s) Protected nImage.i If FileSize(CacheFile) > 0 - ; OnErrorCall(@LoadErrorHandler()) + ; OnErrorCall(@LoadErrorHandler()) nImage = LoadImage(#PB_Any, CacheFile) - ; OnErrorDefault() + ; OnErrorDefault() If IsImage(nImage) MyDebug("Success loading " + CacheFile + " as nImage " + Str(nImage), 3) ProcedureReturn nImage @@ -475,27 +481,6 @@ Module PBMap ProcedureReturn -1 EndProcedure - Procedure.i GetLocalTile(key.s, CacheFile.s) - Protected timg = -1 - If FindMapElement(PBMap\MemCache\Images(), key) - MyDebug("Key : " + key + " found in memory cache!", 3) - ProcedureReturn PBMap\MemCache\Images()\nImage - Else - MyDebug("Trying to load from HDD " + CacheFile) - timg = GetTileFromHDD(CacheFile.s) - If timg <> -1 - MyDebug("Key : " + key + " found on HDD") - LockMutex(PBMap\TileThreadMutex) - AddMapElement(PBMap\MemCache\Images(), key) - PBMap\MemCache\Images()\nImage = timg - UnlockMutex(PBMap\TileThreadMutex) - Else - MyDebug("Key : " + key + " not found") - EndIf - ProcedureReturn timg - EndIf - EndProcedure - Procedure.i GetTileFromWeb(Zoom.i, XTile.i, YTile.i, CacheFile.s, Layer.i) Protected *Buffer Protected nImage.i = -1 @@ -545,39 +530,95 @@ Module PBMap Procedure GetImageThread(*Tile.Tile) Protected nImage.i = -1 - nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, *Tile\CacheFile, *Tile\Layer) - If nImage <> -1 - LockMutex(PBMap\TileThreadMutex) - AddMapElement(PBMap\MemCache\Images(), *Tile\key) ;Add the image to the cache, once - PBMap\MemCache\Images(*Tile\key)\nImage = nImage - UnlockMutex(PBMap\TileThreadMutex) - MyDebug("Image key : " + *Tile\key + " added in memory cache!", 3) - *Tile\nImage = nImage - PBMap\Dirty = #True -; PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW, *Tile) ;If image is loaded from web, redraw - Else - PBMap\Dirty = #True -; PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_RETRY, *Tile) ;If image is not loaded, retry + Repeat + nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, *Tile\CacheFile, *Tile\Layer) + If nImage <> -1 + LockMutex(PBMap\TileThreadMutex) + PBMap\MemCache\Images(*Tile\key)\nImage = nImage + UnlockMutex(PBMap\TileThreadMutex) + MyDebug("Image key : " + *Tile\key + " web image loaded", 3) + PBMap\Dirty = #True + *Tile\RetryNb = 0 + ; PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW, *Tile) ;If image is loaded from web, redraw + Else + MyDebug("Image key : " + *Tile\key + " web image not correctly loaded", 3) + Delay(5000) + *Tile\RetryNb - 1 + ; PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_RETRY, *Tile) ;If image is not loaded, retry + EndIf + Until *Tile\RetryNb <= 0 + ;End of the thread + LockMutex(PBMap\TileThreadMutex) + FreeMemory(PBMap\MemCache\Images(*Tile\key)\Tile) + PBMap\MemCache\Images(*Tile\key)\Tile = 0 + UnlockMutex(PBMap\TileThreadMutex) + EndProcedure + + Procedure.i GetTile(key.s, CacheFile.s, px.i, py.i, tilex.i, tiley.i, Layer.i) + Protected timg = -1 + If FindMapElement(PBMap\MemCache\Images(), key) + MyDebug("Key : " + key + " found in memory cache!", 3) + timg = PBMap\MemCache\Images()\nImage + If timg <> -1 + MyDebug("Image : " + timg + " found in memory cache!", 3) + ProcedureReturn timg + EndIf + Else + AddMapElement(PBMap\MemCache\Images(), key) + MyDebug("Key : " + key + " added in memory cache!", 3) + PBMap\MemCache\Images()\nImage = -1 + ;UnlockMutex(PBMap\TileThreadMutex) EndIf - + If PBMap\MemCache\Images()\Tile = 0 ;Check if a loading thread is not running + MyDebug("Trying to load from HDD " + CacheFile) + timg = GetTileFromHDD(CacheFile.s) + If timg <> -1 + MyDebug("Key : " + key + " found on HDD") + PBMap\MemCache\Images()\nImage = timg + ProcedureReturn timg + EndIf + MyDebug("Key : " + key + " not found on HDD") + ;Launch a new thread + Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) + If *NewTile + With *NewTile + PBMap\MemCache\Images()\Tile = *NewTile + ;New tile parameters + \Position\x = px + \Position\y = py + \PBMapTileX = tilex + \PBMapTileY = tiley + \PBMapZoom = PBMap\Zoom + \key = key + \CacheFile = CacheFile + \Layer = Layer + \RetryNb = 5 + \GetImageThread = CreateThread(@GetImageThread(), *NewTile) + myDebug(" Creating get image thread nb " + Str(\GetImageThread)) + EndWith + Else + MyDebug(" Error, can't create a new tile loading thread") + EndIf + EndIf + ProcedureReturn timg EndProcedure - Procedure DrawTile(*Tile.Tile) - Protected x = *Tile\Position\x - Protected y = *Tile\Position\y - MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) - MyDebug(" at coords " + Str(x) + "," + Str(y), 2) - MovePathCursor(x, y) - DrawVectorImage(ImageID(*Tile\nImage)) - EndProcedure - - Procedure DrawLoading(*Tile.Tile) - Protected x = *Tile\Position\x - Protected y = *Tile\Position\y - Protected Text$ = "Loading" - MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) - MyDebug(" at coords " + Str(x) + "," + Str(y)) - EndProcedure + ; Procedure DrawTile(*Tile.Tile) + ; Protected x = *Tile\Position\x + ; Protected y = *Tile\Position\y + ; MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) + ; MyDebug(" at coords " + Str(x) + "," + Str(y), 2) + ; MovePathCursor(x, y) + ; DrawVectorImage(ImageID(*Tile\nImage)) + ; EndProcedure + ; + ; Procedure DrawLoading(*Tile.Tile) + ; Protected x = *Tile\Position\x + ; Protected y = *Tile\Position\y + ; Protected Text$ = "Loading" + ; MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) + ; MyDebug(" at coords " + Str(x) + "," + Str(y)) + ; EndProcedure Procedure DrawTiles(*Drawing.DrawingParameters, Layer.i, alpha.i=255) ;DisableDebugger @@ -590,9 +631,9 @@ Module PBMap MyDebug("Drawing tiles") For y = - ny - 1 To ny + 1 For x = - nx - 1 To nx + 1 -; If PBMap\Moving ;If drawing was threaded, this would exit the loop when the user is moving -; Break 2 -; EndIf + ; If PBMap\Moving ;If drawing was threaded, this would exit the loop when the user is moving + ; Break 2 + ; EndIf px = *Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX py = *Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY tilex = ((tx+x) % (1<< PBMap\Zoom)) @@ -601,81 +642,54 @@ Module PBMap key = Str(kq) CacheFile = PBMap\HDDCachePath + key + ".png" - img = GetLocalTile(key, CacheFile) + img = GetTile(key, CacheFile, px, py, tilex, tiley, Layer) If img <> -1 MovePathCursor(px, py) DrawVectorImage(ImageID(img), alpha) Else MovePathCursor(px, py) DrawVectorImage(ImageID(PBMap\ImgLoading), alpha) - Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) - If *NewTile - With *NewTile - ;Keep a track of tiles (especially to free memory) - ;TODO : not sure if it really needs a mutex here, i'm still trying to find where the white tile issue is coming from - ;a tile download is either getting clobbered or timing out or getting kicked and it results in a corrupted png file on linux - LockMutex(PBMap\TileThreadMutex) - AddElement(PBMap\TilesThreads()) - PBMap\TilesThreads()\Tile = *NewTile - UnlockMutex(PBMap\TileThreadMutex) - ;New tile parameters - \Position\x = px - \Position\y = py - \PBMapTileX = tilex - \PBMapTileY = tiley - \PBMapZoom = PBMap\Zoom - \key = key - \CacheFile = CacheFile - \Layer = Layer - \GetImageThread = CreateThread(@GetImageThread(), *NewTile) - PBMap\TilesThreads()\GetImageThread = \GetImageThread - myDebug(" Creating get image thread nb " + Str(\GetImageThread)) - EndWith - Else - MyDebug(" Error, can't create a new tile") - Break 2 - EndIf EndIf Next Next - - ;Free tile memory - ;TODO : maybe get out this proc from drawtiles in a special "free ressources" task - ForEach PBMap\TilesThreads() - ;Check if there's no more loading thread - If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 - FreeMemory(PBMap\TilesThreads()\Tile) - DeleteElement(PBMap\TilesThreads()) - EndIf - Next + + ; ;Free tile memory + ; ;TODO : maybe get out this proc from drawtiles in a special "free ressources" task + ; ForEach PBMap\TilesThreads() + ; ;Check if there's no more loading thread + ; If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 + ; FreeMemory(PBMap\TilesThreads()\Tile) + ; DeleteElement(PBMap\TilesThreads()) + ; EndIf + ; Next EndProcedure -; ;-**** Clean Mem Cache -; ;TODO in development, by now there's many cache problem as the loading thread could be perturbed -; ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize -; Protected MaxNbTile.l -; If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) -; MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize -; Else -; MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize -; EndIf -; Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) -; Protected Limit.d=Scale*(MaxNbTile)*1.5 -; Debug "Cache cleaning" -; ForEach PBMap\MemCache\Images() -; Protected Distance.d = HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) -; Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) -; If Distance>Limit And IsImage(PBMap\MemCache\Images()\nImage) -; LockMutex(PBMap\MemCache\Images()\Mutex) -; Debug "delete" -; Debug PBMap\MemCache\Images() -; FreeImage(PBMap\MemCache\Images()\nImage) -; UnlockMutex(PBMap\MemCache\Images()\Mutex) -; FreeMutex(PBMap\MemCache\Images()\Mutex) -; DeleteMapElement(PBMap\MemCache\Images()) -; EndIf -; Next + ; ;-**** Clean Mem Cache + ; ;TODO in development, by now there's many cache problem as the loading thread could be perturbed + ; ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize + ; Protected MaxNbTile.l + ; If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) + ; MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize + ; Else + ; MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize + ; EndIf + ; Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) + ; Protected Limit.d=Scale*(MaxNbTile)*1.5 + ; Debug "Cache cleaning" + ; ForEach PBMap\MemCache\Images() + ; Protected Distance.d = HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) + ; Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) + ; If Distance>Limit And IsImage(PBMap\MemCache\Images()\nImage) + ; LockMutex(PBMap\MemCache\Images()\Mutex) + ; Debug "delete" + ; Debug PBMap\MemCache\Images() + ; FreeImage(PBMap\MemCache\Images()\nImage) + ; UnlockMutex(PBMap\MemCache\Images()\Mutex) + ; FreeMutex(PBMap\MemCache\Images()\Mutex) + ; DeleteMapElement(PBMap\MemCache\Images()) + ; EndIf + ; Next Procedure DrawPointer(*Drawing.DrawingParameters) If PBMap\CallBackMainPointer > 0 @@ -717,7 +731,7 @@ Module PBMap AddPathLine(x+128,y+10) StrokePath(1) EndProcedure - + Procedure TrackPointer(x.i, y.i,dist.l) Protected color.l color=RGBA(0, 0, 0, 255) @@ -809,6 +823,7 @@ Module PBMap Procedure Drawing() Protected *Drawing.DrawingParameters = @PBMap\Drawing Protected Px.d, Py.d,a + PBMap\OnStage = #True PBMap\Dirty = #False PBMap\Redraw = #False ;Precalc some values @@ -824,8 +839,8 @@ Module PBMap StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget)) ;TODO add in layers of tiles ;this way we can cache them as 0 base 1.n layers ;such as for openseamap tiles which are overlaid. not that efficent from here though. - For a = 0 To PBMap\NumberOfMapLayers-1 - DrawTiles(*Drawing,a) + For a = 0 To PBMap\NumberOfMapLayers - 1 + DrawTiles(*Drawing, a) Next DrawTrack(*Drawing) DrawMarker(*Drawing) @@ -836,16 +851,25 @@ Module PBMap MovePathCursor(50,50) DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) MovePathCursor(50,80) - DrawVectorText(Str(ListSize(PBMap\TilesThreads()))) + Protected ThreadCounter = 0 + ForEach PBMap\MemCache\Images() + If PBMap\MemCache\Images()\Tile <> 0 + If PBMap\MemCache\Images()\Tile\GetImageThread <> 0 + ThreadCounter + 1 + EndIf + EndIf + Next + DrawVectorText(Str(ThreadCounter)) ;If PBMap\Options\ShowScale DrawScale(*Drawing,10,GadgetHeight(PBMAP\Gadget)-20,192) ;EndIf StopVectorDrawing() ;If there was a problem while drawing, redraw -; If PBMap\Dirty -; PBMap\Redraw = #True -; ;PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW) -; EndIf + ; If PBMap\Dirty + ; PBMap\Redraw = #True + ; ;PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW) + ; EndIf + PBMap\OnStage = #False EndProcedure Procedure Refresh() @@ -950,8 +974,8 @@ Module PBMap PBMap\Options\ScaleUnit = ScaleUnit Drawing() EndProcedure - - ;Zoom on x, y position relative to the canvas gadget + + ;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 @@ -1083,9 +1107,9 @@ Module PBMap EndProcedure Procedure TimerEvents() - If EventTimer() = PBMap\Timer And (PBMap\Redraw Or PBMap\Dirty) + If EventTimer() = PBMap\Timer And (PBMap\Redraw Or PBMap\Dirty) And PBMap\OnStage = #False Drawing() - EndIf + EndIf EndProcedure Procedure MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) @@ -1180,7 +1204,7 @@ CompilerIf #PB_Compiler_IsMainFile LoadFont(0, "Arial", 12) LoadFont(1, "Arial", 12, #PB_Font_Bold) - + TextGadget(#Text_1, 530, 50, 60, 15, "Movements") 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)) @@ -1241,14 +1265,14 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::Quit() EndIf - + CloseConsole() CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 840 -; FirstLine = 819 -; Folding = ---------- +; CursorPosition = 570 +; FirstLine = 520 +; Folding = --------- ; EnableThread ; EnableXP ; EnableUnicode \ No newline at end of file From 941fd5a7a0c6cd120c31fbd53afd021ad9fc5cc1 Mon Sep 17 00:00:00 2001 From: djes Date: Tue, 30 Aug 2016 12:16:52 +0200 Subject: [PATCH 21/49] Better thread handling --- PBMap.pb | 85 +++++++++++++++++++++----------------------------------- 1 file changed, 31 insertions(+), 54 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 6f0fc52..70c12c5 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -158,7 +158,6 @@ Module PBMap HDDCachePath.S ; Path where to load and save tiles downloaded from server MemCache.TileMemCach ; Images in memory cache ; - OnStage.i Redraw.i Moving.i Dirty.i ; To signal that drawing need a refresh @@ -177,6 +176,7 @@ Module PBMap #PB_MAP_REDRAW = #PB_EventType_FirstCustomValue + 1 #PB_MAP_RETRY = #PB_EventType_FirstCustomValue + 2 + #PB_MAP_TILE_CLEANUP = #PB_EventType_FirstCustomValue + 3 ;-Global variables Global PBMap.PBMap, Null.i @@ -343,6 +343,9 @@ Module PBMap ;Should not occur KillThread(PBMap\MemCache\Images()\Tile\GetImageThread) EndIf + Else + FreeMemory(PBMap\MemCache\Images()\Tile) + PBMap\MemCache\Images()\Tile = 0 EndIf Else DeleteMapElement(PBMap\MemCache\Images()) @@ -533,25 +536,19 @@ Module PBMap Repeat nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, *Tile\CacheFile, *Tile\Layer) If nImage <> -1 - LockMutex(PBMap\TileThreadMutex) - PBMap\MemCache\Images(*Tile\key)\nImage = nImage - UnlockMutex(PBMap\TileThreadMutex) MyDebug("Image key : " + *Tile\key + " web image loaded", 3) - PBMap\Dirty = #True *Tile\RetryNb = 0 ; PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW, *Tile) ;If image is loaded from web, redraw Else MyDebug("Image key : " + *Tile\key + " web image not correctly loaded", 3) - Delay(5000) + Delay(1000) *Tile\RetryNb - 1 ; PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_RETRY, *Tile) ;If image is not loaded, retry EndIf Until *Tile\RetryNb <= 0 - ;End of the thread - LockMutex(PBMap\TileThreadMutex) - FreeMemory(PBMap\MemCache\Images(*Tile\key)\Tile) - PBMap\MemCache\Images(*Tile\key)\Tile = 0 - UnlockMutex(PBMap\TileThreadMutex) + *Tile\nImage = nImage + *Tile\RetryNb = -2 ;End of the thread + PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ;To free memory outside the thread EndProcedure Procedure.i GetTile(key.s, CacheFile.s, px.i, py.i, tilex.i, tiley.i, Layer.i) @@ -567,17 +564,16 @@ Module PBMap AddMapElement(PBMap\MemCache\Images(), key) MyDebug("Key : " + key + " added in memory cache!", 3) PBMap\MemCache\Images()\nImage = -1 - ;UnlockMutex(PBMap\TileThreadMutex) EndIf If PBMap\MemCache\Images()\Tile = 0 ;Check if a loading thread is not running - MyDebug("Trying to load from HDD " + CacheFile) + MyDebug("Trying to load from HDD " + CacheFile, 3) timg = GetTileFromHDD(CacheFile.s) If timg <> -1 - MyDebug("Key : " + key + " found on HDD") + MyDebug("Key : " + key + " found on HDD", 3) PBMap\MemCache\Images()\nImage = timg ProcedureReturn timg EndIf - MyDebug("Key : " + key + " not found on HDD") + MyDebug("Key : " + key + " not found on HDD", 3) ;Launch a new thread Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) If *NewTile @@ -593,33 +589,17 @@ Module PBMap \CacheFile = CacheFile \Layer = Layer \RetryNb = 5 + \nImage = -1 + MyDebug(" Creating get image thread nb " + Str(\GetImageThread) + " to get " + CacheFile, 3) \GetImageThread = CreateThread(@GetImageThread(), *NewTile) - myDebug(" Creating get image thread nb " + Str(\GetImageThread)) EndWith Else - MyDebug(" Error, can't create a new tile loading thread") - EndIf - EndIf + MyDebug(" Error, can't create a new tile loading thread", 3) + EndIf + EndIf ProcedureReturn timg EndProcedure - ; Procedure DrawTile(*Tile.Tile) - ; Protected x = *Tile\Position\x - ; Protected y = *Tile\Position\y - ; MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) - ; MyDebug(" at coords " + Str(x) + "," + Str(y), 2) - ; MovePathCursor(x, y) - ; DrawVectorImage(ImageID(*Tile\nImage)) - ; EndProcedure - ; - ; Procedure DrawLoading(*Tile.Tile) - ; Protected x = *Tile\Position\x - ; Protected y = *Tile\Position\y - ; Protected Text$ = "Loading" - ; MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) - ; MyDebug(" at coords " + Str(x) + "," + Str(y)) - ; EndProcedure - Procedure DrawTiles(*Drawing.DrawingParameters, Layer.i, alpha.i=255) ;DisableDebugger Protected x.i, y.i,kq.q @@ -636,7 +616,7 @@ Module PBMap ; EndIf px = *Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX py = *Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY - tilex = ((tx+x) % (1<< PBMap\Zoom)) + tilex = ((tx+x) % (1< 0 - If PBMap\MemCache\Images()\Tile\GetImageThread <> 0 + If IsThread(PBMap\MemCache\Images()\Tile\GetImageThread) ThreadCounter + 1 EndIf EndIf @@ -869,7 +838,6 @@ Module PBMap ; PBMap\Redraw = #True ; ;PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW) ; EndIf - PBMap\OnStage = #False EndProcedure Procedure Refresh() @@ -1029,7 +997,7 @@ Module PBMap Procedure CanvasEvents() Protected MouseX.i, MouseY.i - Protected Marker.Position + Protected Marker.Position, *Tile.Tile PBMap\Moving = #False Select EventType() Case #PB_EventType_MouseWheel @@ -1103,11 +1071,20 @@ Module PBMap Case #PB_MAP_RETRY Debug "Reload" PBMap\Redraw = #True + Case #PB_MAP_TILE_CLEANUP + *Tile = EventData() + ;If PBMap\MemCache\Images(*Tile\key)\Tile\RetryNb = -2 ;Check the end of the thread + Protected timg = PBMap\MemCache\Images(*Tile\key)\Tile\nImage + PBMap\MemCache\Images(*Tile\key)\nImage = timg + FreeMemory(PBMap\MemCache\Images(*Tile\key)\Tile) + PBMap\MemCache\Images(*Tile\key)\Tile = 0 + PBMap\Redraw = #True + ;EndIf EndSelect EndProcedure Procedure TimerEvents() - If EventTimer() = PBMap\Timer And (PBMap\Redraw Or PBMap\Dirty) And PBMap\OnStage = #False + If EventTimer() = PBMap\Timer And (PBMap\Redraw Or PBMap\Dirty) Drawing() EndIf EndProcedure @@ -1270,8 +1247,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 570 -; FirstLine = 520 +; CursorPosition = 600 +; FirstLine = 680 ; Folding = --------- ; EnableThread ; EnableXP From 19ce16e8d0ba2622b0c8ab7f393a133fa1275692 Mon Sep 17 00:00:00 2001 From: djes Date: Tue, 30 Aug 2016 12:20:31 +0200 Subject: [PATCH 22/49] Grid of degrees --- PBMap.pb | 701 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 391 insertions(+), 310 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index a3e75e3..d7f6348 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -1,4 +1,4 @@ -;************************************************************** +;;************************************************************** ; Program: PBMap ; Description: Permits the use of tiled maps like ; OpenStreetMap in a handy PureBASIC module @@ -30,7 +30,7 @@ DeclareModule PBMap ;-Show debug infos Global Verbose = 1 Global MyDebugLevel = 3 - ;-Proxy ON/OFF + ;-Proxy ON/OFF Global Proxy = #False #SCALE_NAUTICAL = 1 @@ -39,7 +39,6 @@ DeclareModule PBMap Declare InitPBMap(window) Declare SetMapServer(ServerURL.s = "http://tile.openstreetmap.org/", TileSize = 256, ZoomMin = 0, ZoomMax = 18) Declare MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) - Declare Event(Event.l) Declare SetLocation(latitude.d, longitude.d, zoom = 15, mode.i = #PB_Absolute) Declare Drawing() Declare SetZoom(Zoom.i, mode.i = #PB_Relative) @@ -55,7 +54,6 @@ DeclareModule PBMap Declare.d GetLatitude() Declare.d GetLongitude() Declare.i GetZoom() - EndDeclareModule Module PBMap @@ -87,11 +85,18 @@ Module PBMap key.s CacheFile.s GetImageThread.i - layer.i + RetryNb.i + Layer.i + EndStructure + + Structure TileBounds + NorthWest.Position + SouthEast.Position EndStructure Structure DrawingParameters Position.Position + Bounds.TileBounds Canvas.i PBMapTileX.i PBMapTileY.i @@ -113,6 +118,7 @@ Module PBMap Structure ImgMemCach nImage.i + *Tile.Tile ;Location.Location ;Mutex.i EndStructure @@ -137,15 +143,16 @@ Module PBMap Window.i ; Parent Window Gadget.i ; Canvas Gadget Id Font.i ; Font to uses when write on the map + Timer.i TargetLocation.Location ; Latitude and Longitude from focus point Drawing.DrawingParameters ; Drawing parameters based on focus point - ; + ; CallBackLocation.i ; @Procedure(latitude.d,lontitude.d) CallBackMainPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) - ; + ; Position.PixelPosition ; Actual focus point coords in pixels (global) MoveStartingPoint.PixelPosition ; Start mouse position coords when dragging the map - ; + ; Array ServerURL.s(0) ; Web URL ex: http://tile.openstreetmap.org/ NumberOfMapLayers.i ; The number of map tile layers; @@ -153,15 +160,17 @@ Module PBMap ZoomMax.i ; Max Zoom supported by server Zoom.i ; Current zoom TileSize.i ; Tile size downloaded on the server ex : 256 - ; + ; HDDCachePath.S ; Path where to load and save tiles downloaded from server MemCache.TileMemCach ; Images in memory cache - ; + ; + OnStage.i + Redraw.i Moving.i Dirty.i ; To signal that drawing need a refresh - ; + ; MainDrawingThread.i - List TilesThreads.TileThread() + ;List TilesThreads.TileThread() TileThreadMutex.i; ;Mutex to protect resources List track.Location() ; To display a GPX track List Marker.Marker() ; To diplay marker @@ -173,6 +182,7 @@ Module PBMap EndStructure #PB_MAP_REDRAW = #PB_EventType_FirstCustomValue + 1 + #PB_MAP_RETRY = #PB_EventType_FirstCustomValue + 2 ;-Global variables Global PBMap.PBMap, Null.i @@ -204,8 +214,8 @@ Module PBMap Procedure.i CurlReceiveHTTPToFile(URL$, DestFileName$, ProxyURL$="", ProxyPort$="", ProxyUser$="", ProxyPassword$="") Protected *Buffer, curl.i, Timeout.i, res.i Protected FileHandle.i - MyDebug("ReceiveHTTPToFile from " + URL$ + " " + ProxyURL$ + ProxyPort$ + ProxyUser$, 3) - MyDebug(" to file : " + DestFileName$, 3) + MyDebug("CurlReceiveHTTPToFile from " + URL$ + " " + ProxyURL$ + " " + ProxyPort$ + " " + ProxyUser$, 4) + MyDebug(" to file : " + DestFileName$, 4) FileHandle = CreateFile(#PB_Any, DestFileName$) If FileHandle And Len(URL$) curl = curl_easy_init() @@ -238,11 +248,11 @@ Module PBMap curl_easy_setopt(curl, #CURLOPT_WRITEFUNCTION, @ReceiveHTTPWriteToFileFunction()) res = curl_easy_perform(curl) If res <> #CURLE_OK - MyDebug("CURL problem", 3) + MyDebug("CURL problem", 4) EndIf curl_easy_cleanup(curl) Else - MyDebug("Can't init CURL", 3) + MyDebug("Can't init CURL", 4) EndIf CloseFile(FileHandle) ProcedureReturn FileSize(DestFileName$) @@ -270,7 +280,7 @@ Module PBMap EndIf EndProcedure - Procedure InitPBMap(window) + Procedure InitPBMap(Window) Protected Result.i If Verbose OpenConsole() @@ -284,10 +294,11 @@ Module PBMap PBMap\TileThreadMutex = CreateMutex() PBMap\EditMarkerIndex = -1 ;Initialised with "no marker selected" PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold) - PBMap\Window = window + PBMap\Window = Window + PBMap\Timer = 1 PBMap\Options\WheelMouseRelative = #True SetMapServer("http://tile.openstreetmap.org/") - + ;-Preferences ;Use this to create and customize your preferences file for the first time ; CreatePreferences(GetHomeDirectory() + "PBMap.prefs") @@ -324,25 +335,27 @@ Module PBMap PBMap\ZoomMax = ZoomMax PBMap\TileSize = TileSize EndProcedure - Procedure Quit() PBMap\Drawing\End = #True ;Wait for loading threads to finish nicely. Passed 2 seconds, kills them. Protected TimeCounter = ElapsedMilliseconds() Repeat - ResetList(PBMap\TilesThreads()) - While NextElement(PBMap\TilesThreads()) - If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 - FreeMemory(PBMap\TilesThreads()\Tile) - DeleteElement(PBMap\TilesThreads()) - ElseIf ElapsedMilliseconds() - TimeCounter > 2000 - ;Should not occur - KillThread(PBMap\TilesThreads()\GetImageThread) + ForEach PBMap\MemCache\Images() + If PBMap\MemCache\Images()\Tile <> 0 + If IsThread(PBMap\MemCache\Images()\Tile\GetImageThread) + PBMap\MemCache\Images()\Tile\RetryNb = 0 + If ElapsedMilliseconds() - TimeCounter > 2000 + ;Should not occur + KillThread(PBMap\MemCache\Images()\Tile\GetImageThread) + EndIf + EndIf + Else + DeleteMapElement(PBMap\MemCache\Images()) EndIf - Wend + Next Delay(10) - Until ListSize(PBMap\TilesThreads()) = 0 + Until MapSize(PBMap\MemCache\Images()) = 0 curl_global_cleanup() EndProcedure @@ -360,15 +373,6 @@ Module PBMap ProcedureReturn Result EndProcedure - Procedure MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) - If Gadget = #PB_Any - PBMap\Gadget = CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) ;#PB_Canvas_Keyboard has to be set for mousewheel to work on windows - Else - PBMap\Gadget = Gadget - CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) - EndIf - EndProcedure - ;*** Converts coords to tile.decimal ;Warning, structures used in parameters are not tested Procedure LatLon2XY(*Location.Location, *Coords.Position) @@ -423,7 +427,7 @@ Module PBMap x2 = (PBMap\TargetLocation\Longitude+180)*(mapWidth/360) ; convert from degrees To radians latRad = PBMap\TargetLocation\Latitude*#PI/180; - ; get y value + ; get y value mercN = Log(Tan((#PI/4)+(latRad/2))) y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)); *Pixel\x=GadgetWidth(PBMap\Gadget)/2 - (x2-x1) @@ -460,83 +464,71 @@ Module PBMap EndIf EndProcedure + ; Procedure LoadErrorHandler() + ; MessageRequester("Error", "") + ; EndProcedure + Procedure.i GetTileFromHDD(CacheFile.s) Protected nImage.i - ; Debug "Loading image " + CacheFile + " ; Size : " + Str(FileSize(CacheFile)) If FileSize(CacheFile) > 0 + ; OnErrorCall(@LoadErrorHandler()) nImage = LoadImage(#PB_Any, CacheFile) + ; OnErrorDefault() If IsImage(nImage) - ;Debug "Success loading " + CacheFile + " as nImage " + Str(nImage) MyDebug("Success loading " + CacheFile + " as nImage " + Str(nImage), 3) ProcedureReturn nImage Else - ; Debug "Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !" + MyDebug("Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !", 3) MyDebug("Failed loading " + CacheFile + " as nImage " + Str(nImage) + " -> not an image !", 3) EndIf Else - ; Debug "Failed loading " + CacheFile + " -> Size <= 0" MyDebug("Failed loading " + CacheFile + " -> Size <= 0", 3) EndIf ProcedureReturn -1 EndProcedure - Procedure.i GetLocalTile(key.s, CacheFile.s) - Protected timg - MyDebug("Check if we have this image in memory", 3) - If FindMapElement(PBMap\MemCache\Images(), key) - MyDebug("Key : " + key + " found !", 3) - ProcedureReturn PBMap\MemCache\Images()\nImage - Else - MyDebug("Key : " + key + " Try HDD!") - timg = GetTileFromHDD(CacheFile.s) - If timg <> -1 - AddMapElement(PBMap\MemCache\Images(),key) - PBMap\MemCache\Images()\nImage = timg - EndIf - ProcedureReturn timg - EndIf - EndProcedure - - Procedure.i GetTileFromWeb(Zoom.i, XTile.i, YTile.i, CacheFile.s,layer.i) + Procedure.i GetTileFromWeb(Zoom.i, XTile.i, YTile.i, CacheFile.s, Layer.i) Protected *Buffer Protected nImage.i = -1 - Protected FileHandle.i,timg - Protected TileURL.s = PBMap\ServerURL(layer) + Str(Zoom) + "/" + Str(XTile) + "/" + Str(YTile) + ".png" - MyDebug("Check if we have this image on Web", 3) + Protected FileSize.i, timg + Protected TileURL.s = PBMap\ServerURL(Layer) + Str(Zoom) + "/" + Str(XTile) + "/" + Str(YTile) + ".png" If Proxy - FileHandle = CurlReceiveHTTPToFile(TileURL, CacheFile, ProxyURL$, ProxyPort$, ProxyUser$, ProxyPassword$) - If FileHandle + FileSize = CurlReceiveHTTPToFile(TileURL, CacheFile, ProxyURL$, ProxyPort$, ProxyUser$, ProxyPassword$) + If FileSize > 0 + MyDebug("Loaded from web " + TileURL + " as CacheFile " + CacheFile, 3) nImage = GetTileFromHDD(CacheFile) Else - MyDebug("File " + TileURL + " not correctly received with Curl and proxy", 3) + MyDebug("Problem loading from web " + TileURL, 3) EndIf Else *Buffer = ReceiveHTTPMemory(TileURL) ;TODO to thread by using #PB_HTTP_Asynchronous If *Buffer nImage = CatchImage(#PB_Any, *Buffer, MemorySize(*Buffer)) If IsImage(nImage) - MyDebug("Load from web " + TileURL + " as Tile nb " + nImage, 3) - ; Debug "url: " + TileURL - ; Debug "cache file: " + CacheFile - ; timg = LoadImage(#PB_Any,CacheFile) - ; If timg - ; StartDrawing(ImageOutput(timg)) - ; DrawImage(ImageID(nimage)) - ; StopDrawing() - ; SaveImage(timg, CacheFile, #PB_ImagePlugin_PNG) - ; FreeImage(timg) - ; Else - SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG) - FreeMemory(*Buffer) - ; EndIf + ; Debug "url: " + TileURL + ; Debug "cache file: " + CacheFile + ; timg = LoadImage(#PB_Any,CacheFile) + ; If timg + ; StartDrawing(ImageOutput(timg)) + ; DrawImage(ImageID(nimage)) + ; StopDrawing() + ; SaveImage(timg, CacheFile, #PB_ImagePlugin_PNG) + ; FreeImage(timg) + ; Else + If SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG,0,32) + MyDebug("Loaded from web " + TileURL + " as CacheFile " + CacheFile, 3) + Else + MyDebug("Loaded from web " + TileURL + " but cannot save to CacheFile " + CacheFile, 3) + EndIf + FreeMemory(*Buffer) + ; EndIf Else - MyDebug("Can't catch image " + TileURL, 3) + MyDebug("Can't catch image loaded from web " + TileURL, 3) nImage = -1 ;ShowMemoryViewer(*Buffer, MemorySize(*Buffer)) EndIf Else - ; Debug("ReceiveHTTPMemory's buffer is empty") - MyDebug("ReceiveHTTPMemory's buffer is empty", 3) + MyDebug(" Problem loading from web " + TileURL, 3) EndIf EndIf ProcedureReturn nImage @@ -544,141 +536,172 @@ Module PBMap Procedure GetImageThread(*Tile.Tile) Protected nImage.i = -1 - nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, *Tile\CacheFile,*tile\layer) - If nImage <> -1 - LockMutex(PBMap\TileThreadMutex) - PBMap\MemCache\Images(*Tile\key)\nImage = nImage - UnlockMutex(PBMap\TileThreadMutex) - MyDebug("Image nb " + Str(nImage) + " successfully added to mem cache") - MyDebug("With the following key : " + *Tile\key) - *Tile\nImage = nImage - PostEvent(#PB_Event_Gadget, PBMap\window,PBmap\Gadget, #PB_MAP_REDRAW, *Tile) - Else - MyDebug("Error GetImageThread procedure, image not loaded - " + *Tile\key) - ;nImage = -1 - EndIf - + Repeat + nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, *Tile\CacheFile, *Tile\Layer) + If nImage <> -1 + LockMutex(PBMap\TileThreadMutex) + PBMap\MemCache\Images(*Tile\key)\nImage = nImage + UnlockMutex(PBMap\TileThreadMutex) + MyDebug("Image key : " + *Tile\key + " web image loaded", 3) + PBMap\Dirty = #True + *Tile\RetryNb = 0 + ; PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW, *Tile) ;If image is loaded from web, redraw + Else + MyDebug("Image key : " + *Tile\key + " web image not correctly loaded", 3) + Delay(5000) + *Tile\RetryNb - 1 + ; PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_RETRY, *Tile) ;If image is not loaded, retry + EndIf + Until *Tile\RetryNb <= 0 + ;End of the thread + LockMutex(PBMap\TileThreadMutex) + FreeMemory(PBMap\MemCache\Images(*Tile\key)\Tile) + PBMap\MemCache\Images(*Tile\key)\Tile = 0 + UnlockMutex(PBMap\TileThreadMutex) EndProcedure - Procedure DrawTile(*Tile.Tile) - Protected x = *Tile\Position\x - Protected y = *Tile\Position\y - MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) - MyDebug(" at coords " + Str(x) + "," + Str(y), 2) - MovePathCursor(x, y) - DrawVectorImage(ImageID(*Tile\nImage)) + Procedure.i GetTile(key.s, CacheFile.s, px.i, py.i, tilex.i, tiley.i, Layer.i) + Protected timg = -1 + If FindMapElement(PBMap\MemCache\Images(), key) + MyDebug("Key : " + key + " found in memory cache!", 3) + timg = PBMap\MemCache\Images()\nImage + If timg <> -1 + MyDebug("Image : " + timg + " found in memory cache!", 3) + ProcedureReturn timg + EndIf + Else + AddMapElement(PBMap\MemCache\Images(), key) + MyDebug("Key : " + key + " added in memory cache!", 3) + PBMap\MemCache\Images()\nImage = -1 + ;UnlockMutex(PBMap\TileThreadMutex) + EndIf + If PBMap\MemCache\Images()\Tile = 0 ;Check if a loading thread is not running + MyDebug("Trying to load from HDD " + CacheFile) + timg = GetTileFromHDD(CacheFile.s) + If timg <> -1 + MyDebug("Key : " + key + " found on HDD") + PBMap\MemCache\Images()\nImage = timg + ProcedureReturn timg + EndIf + MyDebug("Key : " + key + " not found on HDD") + ;Launch a new thread + Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) + If *NewTile + With *NewTile + PBMap\MemCache\Images()\Tile = *NewTile + ;New tile parameters + \Position\x = px + \Position\y = py + \PBMapTileX = tilex + \PBMapTileY = tiley + \PBMapZoom = PBMap\Zoom + \key = key + \CacheFile = CacheFile + \Layer = Layer + \RetryNb = 5 + \GetImageThread = CreateThread(@GetImageThread(), *NewTile) + myDebug(" Creating get image thread nb " + Str(\GetImageThread)) + EndWith + Else + MyDebug(" Error, can't create a new tile loading thread") + EndIf + EndIf + ProcedureReturn timg EndProcedure - Procedure DrawLoading(*Tile.Tile) - Protected x = *Tile\Position\x - Protected y = *Tile\Position\y - Protected Text$ = "Loading" - MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) - MyDebug(" at coords " + Str(x) + "," + Str(y)) - EndProcedure + ; Procedure DrawTile(*Tile.Tile) + ; Protected x = *Tile\Position\x + ; Protected y = *Tile\Position\y + ; MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) + ; MyDebug(" at coords " + Str(x) + "," + Str(y), 2) + ; MovePathCursor(x, y) + ; DrawVectorImage(ImageID(*Tile\nImage)) + ; EndProcedure + ; + ; Procedure DrawLoading(*Tile.Tile) + ; Protected x = *Tile\Position\x + ; Protected y = *Tile\Position\y + ; Protected Text$ = "Loading" + ; MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) + ; MyDebug(" at coords " + Str(x) + "," + Str(y)) + ; EndProcedure - Procedure DrawTiles(*Drawing.DrawingParameters,layer.i,alpha.i=255) + Procedure DrawTiles(*Drawing.DrawingParameters, Layer.i, alpha.i=255) ;DisableDebugger Protected x.i, y.i,kq.q - Protected tx = Int(*Drawing\Position\x) ;Don't forget the Int() ! + Protected tx = Int(*Drawing\Position\x) ;Don't forget the Int() ! Protected ty = Int(*Drawing\Position\y) Protected nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point Protected ny = *Drawing\CenterY / PBMap\TileSize Protected px, py, img, tilex,tiley, key.s, CacheFile.s MyDebug("Drawing tiles") + + *Drawing\Bounds\NorthWest\x = tx-nx-1 + *Drawing\Bounds\NorthWest\y = ty-ny-1 + *Drawing\Bounds\SouthEast\x = tx+nx+1 + *Drawing\Bounds\SouthEast\y = ty+ny+1 + For y = - ny - 1 To ny + 1 For x = - nx - 1 To nx + 1 - px = *Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX - py = *Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY - tilex = ((tx+x) % (1<< PBMap\Zoom)) - tiley = ty+y - kq = layer | (pbmap\zoom << 8) | (tilex << 16) | (tiley << 36) - key = Str(kq) - CacheFile = PBMap\HDDCachePath + key + ".png" - - - img = GetLocalTile(key, CacheFile) - If img <> -1 - MovePathCursor(px, py) - DrawVectorImage(ImageID(img),alpha) - Else - MovePathCursor(px, py) - DrawVectorImage(ImageID(PBMap\ImgLoading),alpha) - Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) - If *NewTile - With *NewTile - ;Keep a track of tiles (especially to free memory) - ;TODO : not sure if it really needs a mutex here, i'm still trying to find where the white tile issue is coming from - ;a tile download is either getting clobbered or timing out or getting kicked and it results in a corrupted png file on linux - LockMutex(PBMap\TileThreadMutex) - AddElement(PBMap\TilesThreads()) - PBMap\TilesThreads()\Tile = *NewTile - UnlockMutex(PBMap\TileThreadMutex) - ;New tile parameters - \Position\x = px - \Position\y = py - \PBMapTileX = tilex - \PBMapTileY = tiley - \PBMapZoom = PBMap\Zoom - \key = key - \CacheFile = CacheFile - \layer = layer - LockMutex(PBMap\TileThreadMutex) - AddMapElement(PBMap\MemCache\Images(), \key) ;Add the image to the cache, once in this loop - PBMap\MemCache\Images()\nImage = PBMap\ImgLoading - UnlockMutex(PBMap\TileThreadMutex) - \GetImageThread = CreateThread(@GetImageThread(), *NewTile) - PBMap\TilesThreads()\GetImageThread = \GetImageThread - myDebug(" Creating get image thread nb " + Str(\GetImageThread)) - EndWith - Else - MyDebug(" Error, can't create a new tile") - Break 2 - EndIf - EndIf - - Next - - Next - - - ;Free tile memory - ;TODO : maybe get out this proc from drawtiles in a special "free ressources" task - ForEach PBMap\TilesThreads() - ;Check if there's no more loading thread - If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 - FreeMemory(PBMap\TilesThreads()\Tile) - DeleteElement(PBMap\TilesThreads()) - EndIf - Next - ;EnableDebugger + ; If PBMap\Moving ;If drawing was threaded, this would exit the loop when the user is moving + ; Break 2 + ; EndIf + px = *Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX + py = *Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY + tilex = ((tx+x) % (1<< PBMap\Zoom)) + tiley = ty+y + kq = Layer | (pbmap\zoom << 8) | (tilex << 16) | (tiley << 36) + key = Str(kq) + CacheFile = PBMap\HDDCachePath + key + ".png" + + img = GetTile(key, CacheFile, px, py, tilex, tiley, Layer) + If img <> -1 + MovePathCursor(px, py) + DrawVectorImage(ImageID(img), alpha) + Else + MovePathCursor(px, py) + DrawVectorImage(ImageID(PBMap\ImgLoading), alpha) + EndIf + Next + Next + + ; ;Free tile memory + ; ;TODO : maybe get out this proc from drawtiles in a special "free ressources" task + ; ForEach PBMap\TilesThreads() + ; ;Check if there's no more loading thread + ; If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 + ; FreeMemory(PBMap\TilesThreads()\Tile) + ; DeleteElement(PBMap\TilesThreads()) + ; EndIf + ; Next + EndProcedure -; ;-**** Clean Mem Cache -; ;TODO in development, by now there's many cache problem as the loading thread could be perturbed -; ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize -; Protected MaxNbTile.l -; If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) -; MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize -; Else -; MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize -; EndIf -; Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) -; Protected Limit.d=Scale*(MaxNbTile)*1.5 -; Debug "Cache cleaning" -; ForEach PBMap\MemCache\Images() -; Protected Distance.d = HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) -; Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) -; If Distance>Limit And IsImage(PBMap\MemCache\Images()\nImage) -; LockMutex(PBMap\MemCache\Images()\Mutex) -; Debug "delete" -; Debug PBMap\MemCache\Images() -; FreeImage(PBMap\MemCache\Images()\nImage) -; UnlockMutex(PBMap\MemCache\Images()\Mutex) -; FreeMutex(PBMap\MemCache\Images()\Mutex) -; DeleteMapElement(PBMap\MemCache\Images()) -; EndIf -; Next + ; ;-**** Clean Mem Cache + ; ;TODO in development, by now there's many cache problem as the loading thread could be perturbed + ; ;GadgetWidth(PBMap\Gadget)/PBMap\TileSize + ; Protected MaxNbTile.l + ; If GadgetWidth(PBMap\Gadget)>GadgetHeight(PBMap\Gadget) + ; MaxNbTile=GadgetWidth(PBMap\Gadget)/PBMap\TileSize + ; Else + ; MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize + ; EndIf + ; Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) + ; Protected Limit.d=Scale*(MaxNbTile)*1.5 + ; Debug "Cache cleaning" + ; ForEach PBMap\MemCache\Images() + ; Protected Distance.d = HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) + ; Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) + ; If Distance>Limit And IsImage(PBMap\MemCache\Images()\nImage) + ; LockMutex(PBMap\MemCache\Images()\Mutex) + ; Debug "delete" + ; Debug PBMap\MemCache\Images() + ; FreeImage(PBMap\MemCache\Images()\nImage) + ; UnlockMutex(PBMap\MemCache\Images()\Mutex) + ; FreeMutex(PBMap\MemCache\Images()\Mutex) + ; DeleteMapElement(PBMap\MemCache\Images()) + ; EndIf + ; Next Procedure DrawPointer(*Drawing.DrawingParameters) If PBMap\CallBackMainPointer > 0 @@ -719,9 +742,43 @@ Module PBMap MovePathCursor(x,y+12) AddPathLine(x+128,y+10) StrokePath(1) - EndProcedure - + + Procedure DrawDegrees(*Drawing.DrawingParameters,alpha=192) + Protected nx,ny,nx1,ny1,x,y,n,cx,dperpixel.d + Protected pos1.PixelPosition,pos2.PixelPosition,Degrees1.Location,degrees2.Location + + ;VectorFont(FontID(PBMap\Font), 10) + VectorSourceColor(RGBA(0, 0, 0,Alpha)) + + ;GetMapRegionDegrees(@Degrees1,@degrees2) + + XY2LatLon(*Drawing\Bounds\NorthWest,@Degrees1) + XY2LatLon(*Drawing\Bounds\SouthEast,@Degrees2) + + ny = Round(Degrees1\Latitude,#PB_Round_Up)+1 + ny1 = Round(degrees2\Latitude,#PB_Round_Down)-1 + nx = Round(Degrees1\Longitude,#PB_Round_Down)-1 + nx1 = Round(degrees2\Longitude,#PB_Round_Up) +1 + + For y = ny1 To ny + Degrees1\Latitude = y + degrees2\Latitude = y + 1 + For x = nx To nx1 + Degrees1\Longitude =x + Degrees2\Longitude =x+ 1 + GetPixelCoordFromLocation(@Degrees1,@pos1) + MovePathCursor(pos1\x,pos1\y) + AddPathLine(pos2\x,pos1\y) + MovePathCursor(pos1\x,pos1\y) + AddPathLine(pos1\x,pos2\y) + Next + Next + StrokePath(1) + + EndProcedure + + Procedure TrackPointer(x.i, y.i,dist.l) Protected color.l color=RGBA(0, 0, 0, 255) @@ -813,6 +870,9 @@ Module PBMap Procedure Drawing() Protected *Drawing.DrawingParameters = @PBMap\Drawing Protected Px.d, Py.d,a + PBMap\OnStage = #True + PBMap\Dirty = #False + PBMap\Redraw = #False ;Precalc some values *Drawing\CenterX = GadgetWidth(PBMap\Gadget) / 2 *Drawing\CenterY = GadgetHeight(PBMap\Gadget) / 2 @@ -826,8 +886,8 @@ Module PBMap StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget)) ;TODO add in layers of tiles ;this way we can cache them as 0 base 1.n layers ;such as for openseamap tiles which are overlaid. not that efficent from here though. - For a = 0 To PBMap\NumberOfMapLayers-1 - DrawTiles(*Drawing,a) + For a = 0 To PBMap\NumberOfMapLayers - 1 + DrawTiles(*Drawing, a) Next DrawTrack(*Drawing) DrawMarker(*Drawing) @@ -838,12 +898,26 @@ Module PBMap MovePathCursor(50,50) DrawVectorText(Str(MapSize(PBMap\MemCache\Images()))) MovePathCursor(50,80) - DrawVectorText(Str(ListSize(PBMap\TilesThreads()))) - + Protected ThreadCounter = 0 + ForEach PBMap\MemCache\Images() + If PBMap\MemCache\Images()\Tile <> 0 + If PBMap\MemCache\Images()\Tile\GetImageThread <> 0 + ThreadCounter + 1 + EndIf + EndIf + Next + DrawVectorText(Str(ThreadCounter)) ;If PBMap\Options\ShowScale - DrawScale(*Drawing,10,GadgetHeight(PBMAP\Gadget)-20,192) + DrawScale(*Drawing,10,GadgetHeight(PBMAP\Gadget)-20,192) + DrawDegrees(*Drawing,192) ;EndIf - StopVectorDrawing() + StopVectorDrawing() + ;If there was a problem while drawing, redraw + ; If PBMap\Dirty + ; PBMap\Redraw = #True + ; ;PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW) + ; EndIf + PBMap\OnStage = #False EndProcedure Procedure Refresh() @@ -948,8 +1022,8 @@ Module PBMap PBMap\Options\ScaleUnit = ScaleUnit Drawing() EndProcedure - - ;Zoom on x, y position relative to the canvas gadget + + ;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 @@ -1001,95 +1075,103 @@ Module PBMap ProcedureReturn Value EndProcedure - Procedure Event(Event.l) - Protected Gadget.i + Procedure CanvasEvents() Protected MouseX.i, MouseY.i Protected Marker.Position - Protected *Drawing.DrawingParameters - If IsGadget(PBMap\Gadget) And GadgetType(PBMap\Gadget) = #PB_GadgetType_Canvas - Select Event - Case #PB_Event_Gadget ;{ - Gadget = EventGadget() - Select Gadget - Case PBMap\Gadget - Select EventType() - 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)) - Else - ;Absolute zoom (centered on the center of the map) - SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta), #PB_Relative) - EndIf - Case #PB_EventType_LeftButtonDown - ;Check if we select a marker - MouseX = PBMap\Position\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) - MouseY = PBMap\Position\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) - ForEach PBMap\Marker() - LatLon2XY(@PBMap\Marker()\Location, @Marker) - Marker\x * PBMap\TileSize - Marker\y * PBMap\TileSize - If Distance(Marker\x, Marker\y, MouseX, MouseY) < 8 - PBMap\EditMarkerIndex = ListIndex(PBMap\Marker()) - Break - EndIf - Next - ;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 - 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 - PBMap\Moving = #True - ;Move marker - If PBMap\EditMarkerIndex > -1 - SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) - LatLon2XY(@PBMap\Marker()\Location, @Marker) - Marker\x + MouseX / PBMap\TileSize - Marker\y + MouseY / PBMap\TileSize - XY2LatLon(@Marker, @PBMap\Marker()\Location) - Else - ;New move values - PBMap\Position\x - MouseX - PBMap\Position\y - MouseY - ;PBMap tile position in tile.decimal - PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize - PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize - PBMap\Drawing\PassNb = 1 - XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) - ;If CallBackLocation send Location to function - If PBMap\CallBackLocation > 0 - CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) - EndIf - EndIf - Drawing() - PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) - PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) - EndIf - Case #PB_EventType_LeftButtonUp - PBMap\Moving = #False - PBMap\MoveStartingPoint\x = - 1 - If PBMap\EditMarkerIndex > -1 - PBMap\EditMarkerIndex = -1 - Else ;Move Map - PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize - PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize - MyDebug("PBMap\Drawing\Position\x " + Str(PBMap\Drawing\Position\x) + " ; PBMap\Drawing\Position\y " + Str(PBMap\Drawing\Position\y) ) - XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) - Drawing() - EndIf - Case #PB_MAP_REDRAW - Drawing() - EndSelect - EndSelect - EndSelect - Else - MessageRequester("Module PBMap", "You must use PBMapGadget before", #PB_MessageRequester_Ok ) - End - EndIf - + PBMap\Moving = #False + Select EventType() + 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)) + Else + ;Absolute zoom (centered on the center of the map) + SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta), #PB_Relative) + EndIf + Case #PB_EventType_LeftButtonDown + ;Check if we select a marker + MouseX = PBMap\Position\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) + MouseY = PBMap\Position\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) + ForEach PBMap\Marker() + LatLon2XY(@PBMap\Marker()\Location, @Marker) + Marker\x * PBMap\TileSize + Marker\y * PBMap\TileSize + If Distance(Marker\x, Marker\y, MouseX, MouseY) < 8 + PBMap\EditMarkerIndex = ListIndex(PBMap\Marker()) + Break + EndIf + Next + ;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 + 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 + ;Move marker + If PBMap\EditMarkerIndex > -1 + SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) + LatLon2XY(@PBMap\Marker()\Location, @Marker) + Marker\x + MouseX / PBMap\TileSize + Marker\y + MouseY / PBMap\TileSize + XY2LatLon(@Marker, @PBMap\Marker()\Location) + Else + ;New move values + PBMap\Position\x - MouseX + PBMap\Position\y - MouseY + ;PBMap tile position in tile.decimal + PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize + PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize + PBMap\Drawing\PassNb = 1 + XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) + ;If CallBackLocation send Location to function + If PBMap\CallBackLocation > 0 + CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) + EndIf + EndIf + PBMap\Redraw = #True + PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) + PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) + EndIf + Case #PB_EventType_LeftButtonUp + PBMap\MoveStartingPoint\x = - 1 + If PBMap\EditMarkerIndex > -1 + PBMap\EditMarkerIndex = -1 + Else ;Move Map + PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize + PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize + MyDebug("PBMap\Drawing\Position\x " + Str(PBMap\Drawing\Position\x) + " ; PBMap\Drawing\Position\y " + Str(PBMap\Drawing\Position\y) ) + XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) + PBMap\Redraw = #True + EndIf + Case #PB_MAP_REDRAW + Debug "Redraw" + PBMap\Redraw = #True + Case #PB_MAP_RETRY + Debug "Reload" + PBMap\Redraw = #True + EndSelect EndProcedure + + Procedure TimerEvents() + If EventTimer() = PBMap\Timer And (PBMap\Redraw Or PBMap\Dirty) And PBMap\OnStage = #False + Drawing() + EndIf + EndProcedure + + Procedure MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) + If Gadget = #PB_Any + PBMap\Gadget = CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) ;#PB_Canvas_Keyboard has to be set for mousewheel to work on windows + Else + PBMap\Gadget = Gadget + CanvasGadget(PBMap\Gadget, X, Y, Width, Height, #PB_Canvas_Keyboard) + EndIf + BindGadgetEvent(PBMap\Gadget, @CanvasEvents()) + AddWindowTimer(PBMap\Window, PBMap\Timer, 20) + BindEvent(#PB_Event_Timer, @TimerEvents()) + EndProcedure + EndModule ;-Exemple @@ -1170,12 +1252,12 @@ CompilerIf #PB_Compiler_IsMainFile LoadFont(0, "Arial", 12) LoadFont(1, "Arial", 12, #PB_Font_Bold) - + TextGadget(#Text_1, 530, 50, 60, 15, "Movements") - ButtonGadget(#Gdt_Left, 550, 100, 30, 30, Chr($E7)) : SetGadgetFont(#Gdt_Left, FontID(0)) - ButtonGadget(#Gdt_Right, 610, 100, 30, 30, Chr($E8)) : SetGadgetFont(#Gdt_Right, FontID(0)) - ButtonGadget(#Gdt_Up, 580, 070, 30, 30, Chr($E9)) : SetGadgetFont(#Gdt_Up, FontID(0)) - ButtonGadget(#Gdt_Down, 580, 130, 30, 30, Chr($EA)) : SetGadgetFont(#Gdt_Down, FontID(0)) + 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)) + ButtonGadget(#Gdt_Down, 580, 130, 30, 30, Chr($25BC)) : SetGadgetFont(#Gdt_Down, FontID(0)) TextGadget(#Text_2, 530, 160, 60, 15, "Zoom") ButtonGadget(#Button_4, 550, 180, 50, 30, " + ") : SetGadgetFont(#Button_4, FontID(1)) ButtonGadget(#Button_5, 600, 180, 50, 30, " - ") : SetGadgetFont(#Button_5, FontID(1)) @@ -1201,7 +1283,6 @@ CompilerIf #PB_Compiler_IsMainFile Repeat Event = WaitWindowEvent() - PBMap::Event(Event) Select Event Case #PB_Event_CloseWindow : Quit = 1 Case #PB_Event_Gadget ;{ @@ -1232,14 +1313,14 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::Quit() EndIf - + CloseConsole() CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1219 -; FirstLine = 1193 -; Folding = ---------- +; CursorPosition = 1290 +; FirstLine = 1275 +; Folding = --------- ; EnableThread ; EnableXP ; DisableDebugger From 2b2ffac2eedcd8e719b87f762c0e9275de065de6 Mon Sep 17 00:00:00 2001 From: djes Date: Tue, 30 Aug 2016 12:44:23 +0200 Subject: [PATCH 23/49] Merging idle and mine --- PBMap.pb | 171 ++++++------------------------------------------------- 1 file changed, 16 insertions(+), 155 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index b01b96f..e18815f 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -87,14 +87,11 @@ Module PBMap GetImageThread.i RetryNb.i Layer.i -<<<<<<< HEAD -======= EndStructure Structure TileBounds NorthWest.Position SouthEast.Position ->>>>>>> refs/remotes/origin/idle EndStructure Structure DrawingParameters @@ -167,10 +164,6 @@ Module PBMap HDDCachePath.S ; Path where to load and save tiles downloaded from server MemCache.TileMemCach ; Images in memory cache ; -<<<<<<< HEAD -======= - OnStage.i ->>>>>>> refs/remotes/origin/idle Redraw.i Moving.i Dirty.i ; To signal that drawing need a refresh @@ -189,10 +182,7 @@ Module PBMap #PB_MAP_REDRAW = #PB_EventType_FirstCustomValue + 1 #PB_MAP_RETRY = #PB_EventType_FirstCustomValue + 2 -<<<<<<< HEAD #PB_MAP_TILE_CLEANUP = #PB_EventType_FirstCustomValue + 3 -======= ->>>>>>> refs/remotes/origin/idle ;-Global variables Global PBMap.PBMap, Null.i @@ -359,12 +349,9 @@ Module PBMap ;Should not occur KillThread(PBMap\MemCache\Images()\Tile\GetImageThread) EndIf -<<<<<<< HEAD Else FreeMemory(PBMap\MemCache\Images()\Tile) PBMap\MemCache\Images()\Tile = 0 -======= ->>>>>>> refs/remotes/origin/idle EndIf Else DeleteMapElement(PBMap\MemCache\Images()) @@ -531,11 +518,7 @@ Module PBMap ; SaveImage(timg, CacheFile, #PB_ImagePlugin_PNG) ; FreeImage(timg) ; Else -<<<<<<< HEAD - If SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG) -======= - If SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG,0,32) ->>>>>>> refs/remotes/origin/idle + If SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG, 0, 32) MyDebug("Loaded from web " + TileURL + " as CacheFile " + CacheFile, 3) Else MyDebug("Loaded from web " + TileURL + " but cannot save to CacheFile " + CacheFile, 3) @@ -559,39 +542,19 @@ Module PBMap Repeat nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, *Tile\CacheFile, *Tile\Layer) If nImage <> -1 -<<<<<<< HEAD MyDebug("Image key : " + *Tile\key + " web image loaded", 3) -======= - LockMutex(PBMap\TileThreadMutex) - PBMap\MemCache\Images(*Tile\key)\nImage = nImage - UnlockMutex(PBMap\TileThreadMutex) - MyDebug("Image key : " + *Tile\key + " web image loaded", 3) - PBMap\Dirty = #True ->>>>>>> refs/remotes/origin/idle *Tile\RetryNb = 0 ; PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW, *Tile) ;If image is loaded from web, redraw Else MyDebug("Image key : " + *Tile\key + " web image not correctly loaded", 3) -<<<<<<< HEAD Delay(1000) -======= - Delay(5000) ->>>>>>> refs/remotes/origin/idle *Tile\RetryNb - 1 ; PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_RETRY, *Tile) ;If image is not loaded, retry EndIf Until *Tile\RetryNb <= 0 -<<<<<<< HEAD *Tile\nImage = nImage *Tile\RetryNb = -2 ;End of the thread PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ;To free memory outside the thread -======= - ;End of the thread - LockMutex(PBMap\TileThreadMutex) - FreeMemory(PBMap\MemCache\Images(*Tile\key)\Tile) - PBMap\MemCache\Images(*Tile\key)\Tile = 0 - UnlockMutex(PBMap\TileThreadMutex) ->>>>>>> refs/remotes/origin/idle EndProcedure Procedure.i GetTile(key.s, CacheFile.s, px.i, py.i, tilex.i, tiley.i, Layer.i) @@ -607,7 +570,6 @@ Module PBMap AddMapElement(PBMap\MemCache\Images(), key) MyDebug("Key : " + key + " added in memory cache!", 3) PBMap\MemCache\Images()\nImage = -1 -<<<<<<< HEAD EndIf If PBMap\MemCache\Images()\Tile = 0 ;Check if a loading thread is not running MyDebug("Trying to load from HDD " + CacheFile, 3) @@ -618,19 +580,6 @@ Module PBMap ProcedureReturn timg EndIf MyDebug("Key : " + key + " not found on HDD", 3) -======= - ;UnlockMutex(PBMap\TileThreadMutex) - EndIf - If PBMap\MemCache\Images()\Tile = 0 ;Check if a loading thread is not running - MyDebug("Trying to load from HDD " + CacheFile) - timg = GetTileFromHDD(CacheFile.s) - If timg <> -1 - MyDebug("Key : " + key + " found on HDD") - PBMap\MemCache\Images()\nImage = timg - ProcedureReturn timg - EndIf - MyDebug("Key : " + key + " not found on HDD") ->>>>>>> refs/remotes/origin/idle ;Launch a new thread Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) If *NewTile @@ -646,7 +595,6 @@ Module PBMap \CacheFile = CacheFile \Layer = Layer \RetryNb = 5 -<<<<<<< HEAD \nImage = -1 MyDebug(" Creating get image thread nb " + Str(\GetImageThread) + " to get " + CacheFile, 3) \GetImageThread = CreateThread(@GetImageThread(), *NewTile) @@ -658,35 +606,6 @@ Module PBMap ProcedureReturn timg EndProcedure -======= - \GetImageThread = CreateThread(@GetImageThread(), *NewTile) - myDebug(" Creating get image thread nb " + Str(\GetImageThread)) - EndWith - Else - MyDebug(" Error, can't create a new tile loading thread") - EndIf - EndIf - ProcedureReturn timg - EndProcedure - - ; Procedure DrawTile(*Tile.Tile) - ; Protected x = *Tile\Position\x - ; Protected y = *Tile\Position\y - ; MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) - ; MyDebug(" at coords " + Str(x) + "," + Str(y), 2) - ; MovePathCursor(x, y) - ; DrawVectorImage(ImageID(*Tile\nImage)) - ; EndProcedure - ; - ; Procedure DrawLoading(*Tile.Tile) - ; Protected x = *Tile\Position\x - ; Protected y = *Tile\Position\y - ; Protected Text$ = "Loading" - ; MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) - ; MyDebug(" at coords " + Str(x) + "," + Str(y)) - ; EndProcedure - ->>>>>>> refs/remotes/origin/idle Procedure DrawTiles(*Drawing.DrawingParameters, Layer.i, alpha.i=255) ;DisableDebugger Protected x.i, y.i,kq.q @@ -696,12 +615,6 @@ Module PBMap Protected ny = *Drawing\CenterY / PBMap\TileSize Protected px, py, img, tilex,tiley, key.s, CacheFile.s MyDebug("Drawing tiles") - - *Drawing\Bounds\NorthWest\x = tx-nx-1 - *Drawing\Bounds\NorthWest\y = ty-ny-1 - *Drawing\Bounds\SouthEast\x = tx+nx+1 - *Drawing\Bounds\SouthEast\y = ty+ny+1 - For y = - ny - 1 To ny + 1 For x = - nx - 1 To nx + 1 ; If PBMap\Moving ;If drawing was threaded, this would exit the loop when the user is moving @@ -709,11 +622,7 @@ Module PBMap ; EndIf px = *Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX py = *Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY -<<<<<<< HEAD tilex = ((tx+x) % (1<>>>>>> refs/remotes/origin/idle tiley = ty+y kq = Layer | (pbmap\zoom << 8) | (tilex << 16) | (tiley << 36) key = Str(kq) @@ -729,20 +638,6 @@ Module PBMap EndIf Next Next - -<<<<<<< HEAD -======= - ; ;Free tile memory - ; ;TODO : maybe get out this proc from drawtiles in a special "free ressources" task - ; ForEach PBMap\TilesThreads() - ; ;Check if there's no more loading thread - ; If IsThread(PBMap\TilesThreads()\GetImageThread) = 0 - ; FreeMemory(PBMap\TilesThreads()\Tile) - ; DeleteElement(PBMap\TilesThreads()) - ; EndIf - ; Next - ->>>>>>> refs/remotes/origin/idle EndProcedure ; ;-**** Clean Mem Cache @@ -811,13 +706,11 @@ Module PBMap AddPathLine(x+128,y+10) StrokePath(1) EndProcedure - -<<<<<<< HEAD -======= - Procedure DrawDegrees(*Drawing.DrawingParameters,alpha=192) + + Procedure DrawDegrees(*Drawing.DrawingParameters,alpha=192) Protected nx,ny,nx1,ny1,x,y,n,cx,dperpixel.d Protected pos1.PixelPosition,pos2.PixelPosition,Degrees1.Location,degrees2.Location - + ;VectorFont(FontID(PBMap\Font), 10) VectorSourceColor(RGBA(0, 0, 0,Alpha)) @@ -825,31 +718,29 @@ Module PBMap XY2LatLon(*Drawing\Bounds\NorthWest,@Degrees1) XY2LatLon(*Drawing\Bounds\SouthEast,@Degrees2) - + ny = Round(Degrees1\Latitude,#PB_Round_Up)+1 ny1 = Round(degrees2\Latitude,#PB_Round_Down)-1 nx = Round(Degrees1\Longitude,#PB_Round_Down)-1 nx1 = Round(degrees2\Longitude,#PB_Round_Up) +1 - + For y = ny1 To ny Degrees1\Latitude = y degrees2\Latitude = y + 1 For x = nx To nx1 Degrees1\Longitude =x Degrees2\Longitude =x+ 1 - GetPixelCoordFromLocation(@Degrees1,@pos1) - MovePathCursor(pos1\x,pos1\y) - AddPathLine(pos2\x,pos1\y) - MovePathCursor(pos1\x,pos1\y) - AddPathLine(pos1\x,pos2\y) + GetPixelCoordFromLocation(@Degrees1,@pos1) + MovePathCursor(pos1\x,pos1\y) + AddPathLine(pos2\x,pos1\y) + MovePathCursor(pos1\x,pos1\y) + AddPathLine(pos1\x,pos2\y) Next Next StrokePath(1) - + EndProcedure - ->>>>>>> refs/remotes/origin/idle Procedure TrackPointer(x.i, y.i,dist.l) Protected color.l color=RGBA(0, 0, 0, 255) @@ -941,10 +832,6 @@ Module PBMap Procedure Drawing() Protected *Drawing.DrawingParameters = @PBMap\Drawing Protected Px.d, Py.d,a -<<<<<<< HEAD -======= - PBMap\OnStage = #True ->>>>>>> refs/remotes/origin/idle PBMap\Dirty = #False PBMap\Redraw = #False ;Precalc some values @@ -975,11 +862,7 @@ Module PBMap Protected ThreadCounter = 0 ForEach PBMap\MemCache\Images() If PBMap\MemCache\Images()\Tile <> 0 -<<<<<<< HEAD If IsThread(PBMap\MemCache\Images()\Tile\GetImageThread) -======= - If PBMap\MemCache\Images()\Tile\GetImageThread <> 0 ->>>>>>> refs/remotes/origin/idle ThreadCounter + 1 EndIf EndIf @@ -987,10 +870,7 @@ Module PBMap DrawVectorText(Str(ThreadCounter)) ;If PBMap\Options\ShowScale DrawScale(*Drawing,10,GadgetHeight(PBMAP\Gadget)-20,192) -<<<<<<< HEAD -======= - DrawDegrees(*Drawing,192) ->>>>>>> refs/remotes/origin/idle + DrawDegrees(*Drawing, 192) ;EndIf StopVectorDrawing() ;If there was a problem while drawing, redraw @@ -998,10 +878,6 @@ Module PBMap ; PBMap\Redraw = #True ; ;PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW) ; EndIf -<<<<<<< HEAD -======= - PBMap\OnStage = #False ->>>>>>> refs/remotes/origin/idle EndProcedure Procedure Refresh() @@ -1161,11 +1037,7 @@ Module PBMap Procedure CanvasEvents() Protected MouseX.i, MouseY.i -<<<<<<< HEAD Protected Marker.Position, *Tile.Tile -======= - Protected Marker.Position ->>>>>>> refs/remotes/origin/idle PBMap\Moving = #False Select EventType() Case #PB_EventType_MouseWheel @@ -1239,7 +1111,6 @@ Module PBMap Case #PB_MAP_RETRY Debug "Reload" PBMap\Redraw = #True -<<<<<<< HEAD Case #PB_MAP_TILE_CLEANUP *Tile = EventData() ;If PBMap\MemCache\Images(*Tile\key)\Tile\RetryNb = -2 ;Check the end of the thread @@ -1249,17 +1120,12 @@ Module PBMap PBMap\MemCache\Images(*Tile\key)\Tile = 0 PBMap\Redraw = #True ;EndIf -======= ->>>>>>> refs/remotes/origin/idle + EndSelect EndProcedure Procedure TimerEvents() -<<<<<<< HEAD If EventTimer() = PBMap\Timer And (PBMap\Redraw Or PBMap\Dirty) -======= - If EventTimer() = PBMap\Timer And (PBMap\Redraw Or PBMap\Dirty) And PBMap\OnStage = #False ->>>>>>> refs/remotes/origin/idle Drawing() EndIf EndProcedure @@ -1422,13 +1288,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -<<<<<<< HEAD -; CursorPosition = 600 -; FirstLine = 680 -======= -; CursorPosition = 1290 -; FirstLine = 1275 ->>>>>>> refs/remotes/origin/idle +; CursorPosition = 1264 +; FirstLine = 1245 ; Folding = --------- ; EnableThread ; EnableXP From 295d0d72bc820ba002a56b2cb537b71f3fe62dc8 Mon Sep 17 00:00:00 2001 From: djes Date: Tue, 30 Aug 2016 12:51:41 +0200 Subject: [PATCH 24/49] Lil fix --- PBMap.pb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index e18815f..df8664f 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -615,6 +615,12 @@ Module PBMap Protected ny = *Drawing\CenterY / PBMap\TileSize Protected px, py, img, tilex,tiley, key.s, CacheFile.s MyDebug("Drawing tiles") + + *Drawing\Bounds\NorthWest\x = tx-nx-1 + *Drawing\Bounds\NorthWest\y = ty-ny-1 + *Drawing\Bounds\SouthEast\x = tx+nx+1 + *Drawing\Bounds\SouthEast\y = ty+ny+1 + For y = - ny - 1 To ny + 1 For x = - nx - 1 To nx + 1 ; If PBMap\Moving ;If drawing was threaded, this would exit the loop when the user is moving @@ -1288,9 +1294,10 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1264 -; FirstLine = 1245 +; CursorPosition = 622 +; FirstLine = 608 ; Folding = --------- ; EnableThread ; EnableXP +; DisableDebugger ; EnableUnicode \ No newline at end of file From 0e063a5623ead6fdb5d33a90cd6ba7ea13319337 Mon Sep 17 00:00:00 2001 From: djes Date: Tue, 30 Aug 2016 16:48:28 +0200 Subject: [PATCH 25/49] DrawDegrees bugfix (not fully fixed...) --- PBMap.pb | 91 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index df8664f..e032ebf 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -28,7 +28,7 @@ UsePNGImageEncoder() DeclareModule PBMap #Red = 255 ;-Show debug infos - Global Verbose = 1 + Global Verbose = 0 Global MyDebugLevel = 3 ;-Proxy ON/OFF Global Proxy = #False @@ -615,12 +615,7 @@ Module PBMap Protected ny = *Drawing\CenterY / PBMap\TileSize Protected px, py, img, tilex,tiley, key.s, CacheFile.s MyDebug("Drawing tiles") - - *Drawing\Bounds\NorthWest\x = tx-nx-1 - *Drawing\Bounds\NorthWest\y = ty-ny-1 - *Drawing\Bounds\SouthEast\x = tx+nx+1 - *Drawing\Bounds\SouthEast\y = ty+ny+1 - + For y = - ny - 1 To ny + 1 For x = - nx - 1 To nx + 1 ; If PBMap\Moving ;If drawing was threaded, this would exit the loop when the user is moving @@ -714,35 +709,50 @@ Module PBMap EndProcedure Procedure DrawDegrees(*Drawing.DrawingParameters,alpha=192) - Protected nx,ny,nx1,ny1,x,y,n,cx,dperpixel.d + Protected tx, ty, nx,ny,nx1,ny1,x,y,n,cx,dperpixel.d Protected pos1.PixelPosition,pos2.PixelPosition,Degrees1.Location,degrees2.Location + tx = Int(*Drawing\Position\x) ;Don't forget the Int() ! + ty = Int(*Drawing\Position\y) + nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point + ny = *Drawing\CenterY / PBMap\TileSize + + *Drawing\Bounds\NorthWest\x = tx-nx-1 + *Drawing\Bounds\NorthWest\y = ty-ny-1 + *Drawing\Bounds\SouthEast\x = tx+nx+1 + *Drawing\Bounds\SouthEast\y = ty+ny+1 + ;VectorFont(FontID(PBMap\Font), 10) VectorSourceColor(RGBA(0, 0, 0,Alpha)) ;GetMapRegionDegrees(@Degrees1,@degrees2) - XY2LatLon(*Drawing\Bounds\NorthWest,@Degrees1) - XY2LatLon(*Drawing\Bounds\SouthEast,@Degrees2) + XY2LatLon(*Drawing\Bounds\NorthWest, @Degrees1) + XY2LatLon(*Drawing\Bounds\SouthEast, @Degrees2) - ny = Round(Degrees1\Latitude,#PB_Round_Up)+1 - ny1 = Round(degrees2\Latitude,#PB_Round_Down)-1 - nx = Round(Degrees1\Longitude,#PB_Round_Down)-1 - nx1 = Round(degrees2\Longitude,#PB_Round_Up) +1 + nx = Round(Degrees1\Longitude, #PB_Round_Down)-1 + ny = Round(Degrees1\Latitude, #PB_Round_Up) +1 + nx1 = Round(Degrees2\Longitude, #PB_Round_Up) +1 + ny1 = Round(Degrees2\Latitude, #PB_Round_Down)-1 - For y = ny1 To ny - Degrees1\Latitude = y - degrees2\Latitude = y + 1 - For x = nx To nx1 - Degrees1\Longitude =x - Degrees2\Longitude =x+ 1 - GetPixelCoordFromLocation(@Degrees1,@pos1) - MovePathCursor(pos1\x,pos1\y) - AddPathLine(pos2\x,pos1\y) - MovePathCursor(pos1\x,pos1\y) - AddPathLine(pos1\x,pos2\y) + GetPixelCoordFromLocation(@Degrees2, @pos2) + + x = nx + For y = ny1 To ny + Degrees1\Longitude = x + Degrees1\Latitude = y + GetPixelCoordFromLocation(@Degrees1, @pos1) + MovePathCursor(pos1\x, pos1\y) + AddPathLine( pos2\x, pos1\y) + Next + y = ny + For x = nx To nx1 + Degrees1\Longitude = x + Degrees1\Latitude = y + GetPixelCoordFromLocation(@Degrees1, @pos1) + MovePathCursor(pos1\x, pos1\y) + AddPathLine( pos1\x, pos2\y) Next - Next StrokePath(1) EndProcedure @@ -818,7 +828,7 @@ Module PBMap EndProcedure ; Draw all markers on the screen ! - Procedure DrawMarker(*Drawing.DrawingParameters) + Procedure DrawMarkers(*Drawing.DrawingParameters) Protected Pixel.PixelPosition ForEach PBMap\Marker() If PBMap\Marker()\Location\Latitude <> 0 And PBMap\Marker()\Location\Longitude <> 0 @@ -849,6 +859,7 @@ Module PBMap *Drawing\DeltaY = Py * PBMap\TileSize - (Int(Py) * PBMap\TileSize) *Drawing\TargetLocation\Latitude = PBMap\TargetLocation\Latitude *Drawing\TargetLocation\Longitude = PBMap\TargetLocation\Longitude + ;Main drawing stuff StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget)) ;TODO add in layers of tiles ;this way we can cache them as 0 base 1.n layers @@ -857,7 +868,7 @@ Module PBMap DrawTiles(*Drawing, a) Next DrawTrack(*Drawing) - DrawMarker(*Drawing) + DrawMarkers(*Drawing) DrawPointer(*Drawing) ;- Display how many images in cache VectorFont(FontID(PBMap\Font), 30) @@ -874,9 +885,9 @@ Module PBMap EndIf Next DrawVectorText(Str(ThreadCounter)) + DrawDegrees(*Drawing, 192) ;If PBMap\Options\ShowScale DrawScale(*Drawing,10,GadgetHeight(PBMAP\Gadget)-20,192) - DrawDegrees(*Drawing, 192) ;EndIf StopVectorDrawing() ;If there was a problem while drawing, redraw @@ -1118,19 +1129,19 @@ Module PBMap Debug "Reload" PBMap\Redraw = #True Case #PB_MAP_TILE_CLEANUP - *Tile = EventData() - ;If PBMap\MemCache\Images(*Tile\key)\Tile\RetryNb = -2 ;Check the end of the thread + *Tile = EventData() + ;After a Web tile loading thread, clean the tile structure memory and set the image nb in the cache + ;avoid to have threads accessing vars (and avoid mutex), see GetImageThread() Protected timg = PBMap\MemCache\Images(*Tile\key)\Tile\nImage PBMap\MemCache\Images(*Tile\key)\nImage = timg FreeMemory(PBMap\MemCache\Images(*Tile\key)\Tile) PBMap\MemCache\Images(*Tile\key)\Tile = 0 PBMap\Redraw = #True - ;EndIf - EndSelect EndProcedure Procedure TimerEvents() + ;Redraw at regular intervals If EventTimer() = PBMap\Timer And (PBMap\Redraw Or PBMap\Dirty) Drawing() EndIf @@ -1222,7 +1233,6 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::Refresh() EndProcedure - OpenConsole() ;- MAIN TEST If OpenWindow(#Window_0, 260, 225, 700, 571, "PBMap", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered | #PB_Window_SizeGadget) @@ -1252,10 +1262,10 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::MapGadget(#Map, 10, 10, 512, 512) PBMap::SetCallBackMainPointer(@MainPointer()) ;To change the Main Pointer PBMap::SetCallBackLocation(@UpdateLocation()) - PBMap::SetLocation(-36.81148,175.08634,12) + PBMap::SetLocation(-36.81148, 175.08634,12) PBMap::SetMapServer("http://t1.openseamap.org/seamark/") ;add a special osm overlay map PBMAP::SetMapScaleUnit(PBMAP::#SCALE_NAUTICAL) - ;PBMap::AddMarker(49.0446828398, 2.0349812508, -1, @MyPointer()) + PBMap::AddMarker(49.0446828398, 2.0349812508, -1, @MyPointer()) Repeat Event = WaitWindowEvent() @@ -1289,15 +1299,12 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::Quit() EndIf - - CloseConsole() - + CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 622 -; FirstLine = 608 +; CursorPosition = 751 +; FirstLine = 719 ; Folding = --------- ; EnableThread ; EnableXP -; DisableDebugger ; EnableUnicode \ No newline at end of file From da7b626a9ae59aa7b9824e186b05447727124c4b Mon Sep 17 00:00:00 2001 From: djes Date: Wed, 31 Aug 2016 10:06:17 +0200 Subject: [PATCH 26/49] djes merging and lat/long drawing --- PBMap.pb | 213 ++++++++++++++++++++++++------------------------------- 1 file changed, 94 insertions(+), 119 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index d7f6348..d323ef1 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -1,4 +1,4 @@ -;;************************************************************** +;************************************************************** ; Program: PBMap ; Description: Permits the use of tiled maps like ; OpenStreetMap in a handy PureBASIC module @@ -28,7 +28,7 @@ UsePNGImageEncoder() DeclareModule PBMap #Red = 255 ;-Show debug infos - Global Verbose = 1 + Global Verbose = 0 Global MyDebugLevel = 3 ;-Proxy ON/OFF Global Proxy = #False @@ -164,13 +164,11 @@ Module PBMap HDDCachePath.S ; Path where to load and save tiles downloaded from server MemCache.TileMemCach ; Images in memory cache ; - OnStage.i Redraw.i Moving.i Dirty.i ; To signal that drawing need a refresh ; MainDrawingThread.i - ;List TilesThreads.TileThread() TileThreadMutex.i; ;Mutex to protect resources List track.Location() ; To display a GPX track List Marker.Marker() ; To diplay marker @@ -183,6 +181,7 @@ Module PBMap #PB_MAP_REDRAW = #PB_EventType_FirstCustomValue + 1 #PB_MAP_RETRY = #PB_EventType_FirstCustomValue + 2 + #PB_MAP_TILE_CLEANUP = #PB_EventType_FirstCustomValue + 3 ;-Global variables Global PBMap.PBMap, Null.i @@ -349,6 +348,9 @@ Module PBMap ;Should not occur KillThread(PBMap\MemCache\Images()\Tile\GetImageThread) EndIf + Else + FreeMemory(PBMap\MemCache\Images()\Tile) + PBMap\MemCache\Images()\Tile = 0 EndIf Else DeleteMapElement(PBMap\MemCache\Images()) @@ -515,7 +517,7 @@ Module PBMap ; SaveImage(timg, CacheFile, #PB_ImagePlugin_PNG) ; FreeImage(timg) ; Else - If SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG,0,32) + If SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG, 0, 32) MyDebug("Loaded from web " + TileURL + " as CacheFile " + CacheFile, 3) Else MyDebug("Loaded from web " + TileURL + " but cannot save to CacheFile " + CacheFile, 3) @@ -539,25 +541,17 @@ Module PBMap Repeat nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, *Tile\CacheFile, *Tile\Layer) If nImage <> -1 - LockMutex(PBMap\TileThreadMutex) - PBMap\MemCache\Images(*Tile\key)\nImage = nImage - UnlockMutex(PBMap\TileThreadMutex) MyDebug("Image key : " + *Tile\key + " web image loaded", 3) - PBMap\Dirty = #True *Tile\RetryNb = 0 - ; PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW, *Tile) ;If image is loaded from web, redraw Else MyDebug("Image key : " + *Tile\key + " web image not correctly loaded", 3) - Delay(5000) + Delay(1000) *Tile\RetryNb - 1 - ; PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_RETRY, *Tile) ;If image is not loaded, retry EndIf Until *Tile\RetryNb <= 0 - ;End of the thread - LockMutex(PBMap\TileThreadMutex) - FreeMemory(PBMap\MemCache\Images(*Tile\key)\Tile) - PBMap\MemCache\Images(*Tile\key)\Tile = 0 - UnlockMutex(PBMap\TileThreadMutex) + *Tile\nImage = nImage + *Tile\RetryNb = -2 ;End of the thread + PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ;To free memory outside the thread EndProcedure Procedure.i GetTile(key.s, CacheFile.s, px.i, py.i, tilex.i, tiley.i, Layer.i) @@ -573,17 +567,16 @@ Module PBMap AddMapElement(PBMap\MemCache\Images(), key) MyDebug("Key : " + key + " added in memory cache!", 3) PBMap\MemCache\Images()\nImage = -1 - ;UnlockMutex(PBMap\TileThreadMutex) EndIf If PBMap\MemCache\Images()\Tile = 0 ;Check if a loading thread is not running - MyDebug("Trying to load from HDD " + CacheFile) + MyDebug("Trying to load from HDD " + CacheFile, 3) timg = GetTileFromHDD(CacheFile.s) If timg <> -1 - MyDebug("Key : " + key + " found on HDD") + MyDebug("Key : " + key + " found on HDD", 3) PBMap\MemCache\Images()\nImage = timg ProcedureReturn timg EndIf - MyDebug("Key : " + key + " not found on HDD") + MyDebug("Key : " + key + " not found on HDD", 3) ;Launch a new thread Protected *NewTile.Tile = AllocateMemory(SizeOf(Tile)) If *NewTile @@ -599,33 +592,17 @@ Module PBMap \CacheFile = CacheFile \Layer = Layer \RetryNb = 5 + \nImage = -1 + MyDebug(" Creating get image thread nb " + Str(\GetImageThread) + " to get " + CacheFile, 3) \GetImageThread = CreateThread(@GetImageThread(), *NewTile) - myDebug(" Creating get image thread nb " + Str(\GetImageThread)) EndWith Else - MyDebug(" Error, can't create a new tile loading thread") - EndIf - EndIf + MyDebug(" Error, can't create a new tile loading thread", 3) + EndIf + EndIf ProcedureReturn timg EndProcedure - ; Procedure DrawTile(*Tile.Tile) - ; Protected x = *Tile\Position\x - ; Protected y = *Tile\Position\y - ; MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) - ; MyDebug(" at coords " + Str(x) + "," + Str(y), 2) - ; MovePathCursor(x, y) - ; DrawVectorImage(ImageID(*Tile\nImage)) - ; EndProcedure - ; - ; Procedure DrawLoading(*Tile.Tile) - ; Protected x = *Tile\Position\x - ; Protected y = *Tile\Position\y - ; Protected Text$ = "Loading" - ; MyDebug(" Drawing tile nb " + " X : " + Str(*Tile\PBMapTileX) + " Y : " + Str(*Tile\PBMapTileX), 2) - ; MyDebug(" at coords " + Str(x) + "," + Str(y)) - ; EndProcedure - Procedure DrawTiles(*Drawing.DrawingParameters, Layer.i, alpha.i=255) ;DisableDebugger Protected x.i, y.i,kq.q @@ -635,12 +612,7 @@ Module PBMap Protected ny = *Drawing\CenterY / PBMap\TileSize Protected px, py, img, tilex,tiley, key.s, CacheFile.s MyDebug("Drawing tiles") - - *Drawing\Bounds\NorthWest\x = tx-nx-1 - *Drawing\Bounds\NorthWest\y = ty-ny-1 - *Drawing\Bounds\SouthEast\x = tx+nx+1 - *Drawing\Bounds\SouthEast\y = ty+ny+1 - + For y = - ny - 1 To ny + 1 For x = - nx - 1 To nx + 1 ; If PBMap\Moving ;If drawing was threaded, this would exit the loop when the user is moving @@ -648,7 +620,7 @@ Module PBMap ; EndIf px = *Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX py = *Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY - tilex = ((tx+x) % (1<< PBMap\Zoom)) + tilex = ((tx+x) % (1< 0 And PBMap\Marker()\Location\Longitude <> 0 @@ -870,7 +848,6 @@ Module PBMap Procedure Drawing() Protected *Drawing.DrawingParameters = @PBMap\Drawing Protected Px.d, Py.d,a - PBMap\OnStage = #True PBMap\Dirty = #False PBMap\Redraw = #False ;Precalc some values @@ -882,6 +859,7 @@ Module PBMap *Drawing\DeltaY = Py * PBMap\TileSize - (Int(Py) * PBMap\TileSize) *Drawing\TargetLocation\Latitude = PBMap\TargetLocation\Latitude *Drawing\TargetLocation\Longitude = PBMap\TargetLocation\Longitude + ;Main drawing stuff StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget)) ;TODO add in layers of tiles ;this way we can cache them as 0 base 1.n layers @@ -890,7 +868,7 @@ Module PBMap DrawTiles(*Drawing, a) Next DrawTrack(*Drawing) - DrawMarker(*Drawing) + DrawMarkers(*Drawing) DrawPointer(*Drawing) ;- Display how many images in cache VectorFont(FontID(PBMap\Font), 30) @@ -901,23 +879,18 @@ Module PBMap Protected ThreadCounter = 0 ForEach PBMap\MemCache\Images() If PBMap\MemCache\Images()\Tile <> 0 - If PBMap\MemCache\Images()\Tile\GetImageThread <> 0 + If IsThread(PBMap\MemCache\Images()\Tile\GetImageThread) ThreadCounter + 1 EndIf EndIf Next DrawVectorText(Str(ThreadCounter)) + DrawDegrees(*Drawing, 192) ;If PBMap\Options\ShowScale DrawScale(*Drawing,10,GadgetHeight(PBMAP\Gadget)-20,192) - DrawDegrees(*Drawing,192) ;EndIf StopVectorDrawing() - ;If there was a problem while drawing, redraw - ; If PBMap\Dirty - ; PBMap\Redraw = #True - ; ;PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW) - ; EndIf - PBMap\OnStage = #False + EndProcedure Procedure Refresh() @@ -1058,15 +1031,11 @@ Module PBMap EndProcedure Procedure.d GetLatitude() - Protected Value.d - Value = PBMap\TargetLocation\Latitude - ProcedureReturn Value + ProcedureReturn 0-(90-Mod((PBMap\TargetLocation\Latitude+90),180)) EndProcedure Procedure.d GetLongitude() - Protected Value.d - Value = PBMap\TargetLocation\Longitude - ProcedureReturn Value + ProcedureReturn 0-(180-Mod((PBMap\TargetLocation\Longitude+180),360)) EndProcedure Procedure.i GetZoom() @@ -1077,7 +1046,7 @@ Module PBMap Procedure CanvasEvents() Protected MouseX.i, MouseY.i - Protected Marker.Position + Protected Marker.Position, *Tile.Tile PBMap\Moving = #False Select EventType() Case #PB_EventType_MouseWheel @@ -1151,11 +1120,21 @@ Module PBMap Case #PB_MAP_RETRY Debug "Reload" PBMap\Redraw = #True + Case #PB_MAP_TILE_CLEANUP + *Tile = EventData() + ;After a Web tile loading thread, clean the tile structure memory and set the image nb in the cache + ;avoid to have threads accessing vars (and avoid mutex), see GetImageThread() + Protected timg = PBMap\MemCache\Images(*Tile\key)\Tile\nImage + PBMap\MemCache\Images(*Tile\key)\nImage = timg + FreeMemory(PBMap\MemCache\Images(*Tile\key)\Tile) + PBMap\MemCache\Images(*Tile\key)\Tile = 0 + PBMap\Redraw = #True EndSelect EndProcedure Procedure TimerEvents() - If EventTimer() = PBMap\Timer And (PBMap\Redraw Or PBMap\Dirty) And PBMap\OnStage = #False + ;Redraw at regular intervals + If EventTimer() = PBMap\Timer And (PBMap\Redraw Or PBMap\Dirty) Drawing() EndIf EndProcedure @@ -1205,8 +1184,8 @@ CompilerIf #PB_Compiler_IsMainFile EndStructure Procedure UpdateLocation(*Location.Location) - SetGadgetText(#String_0, StrD(*Location\Latitude)) - SetGadgetText(#String_1, StrD(*Location\Longitude)) + SetGadgetText(#String_0, StrD(0-(90-Mod((*Location\Latitude+90),180)))) + SetGadgetText(#String_1, StrD(0-(180-Mod((*Location\Longitude+180),360)))) ProcedureReturn 0 EndProcedure @@ -1246,7 +1225,6 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::Refresh() EndProcedure - OpenConsole() ;- MAIN TEST If OpenWindow(#Window_0, 260, 225, 700, 571, "PBMap", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered | #PB_Window_SizeGadget) @@ -1279,7 +1257,7 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::SetLocation(-36.81148,175.08634,12) PBMap::SetMapServer("http://t1.openseamap.org/seamark/") ;add a special osm overlay map PBMAP::SetMapScaleUnit(PBMAP::#SCALE_NAUTICAL) - ;PBMap::AddMarker(49.0446828398, 2.0349812508, -1, @MyPointer()) + PBMap::AddMarker(49.0446828398, 2.0349812508, -1, @MyPointer()) Repeat Event = WaitWindowEvent() @@ -1312,14 +1290,11 @@ CompilerIf #PB_Compiler_IsMainFile Until Quit = #True PBMap::Quit() - EndIf - - CloseConsole() - -CompilerEndIf + EndIf + CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1290 -; FirstLine = 1275 +; CursorPosition = 1263 +; FirstLine = 1250 ; Folding = --------- ; EnableThread ; EnableXP From b937a377e3f274298b3c622e4cc216604ae3d1e9 Mon Sep 17 00:00:00 2001 From: djes Date: Wed, 31 Aug 2016 10:41:42 +0200 Subject: [PATCH 27/49] wip --- PBMap.pb | 90 +++++++++++++++----------------------------------------- 1 file changed, 23 insertions(+), 67 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 341e5f6..7e2d542 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -615,9 +615,6 @@ Module PBMap For y = - ny - 1 To ny + 1 For x = - nx - 1 To nx + 1 - ; If PBMap\Moving ;If drawing was threaded, this would exit the loop when the user is moving - ; Break 2 - ; EndIf px = *Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX py = *Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY tilex = ((tx+x) % (1<>>>>>> refs/remotes/origin/idle - - tx = *Drawing\Position\x - ty = *Drawing\Position\y - nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point - ny = *Drawing\CenterY / PBMap\TileSize - -<<<<<<< HEAD -======= - *Drawing\Bounds\NorthWest\x = tx-nx-1 - *Drawing\Bounds\NorthWest\y = ty-ny-1 - *Drawing\Bounds\SouthEast\x = tx+nx+1 - *Drawing\Bounds\SouthEast\y = ty+ny+1 - VectorFont(FontID(PBMap\Font), 10) VectorSourceColor(RGBA(0, 0, 0,alpha)) ->>>>>>> refs/remotes/origin/idle XY2LatLon(*Drawing\Bounds\NorthWest, @Degrees1) XY2LatLon(*Drawing\Bounds\SouthEast, @Degrees2) @@ -750,27 +728,18 @@ Module PBMap ny1 = Round(Degrees2\Latitude, #PB_Round_Down)-1 GetPixelCoordFromLocation(@Degrees2, @pos2) -<<<<<<< HEAD - -======= - ->>>>>>> refs/remotes/origin/idle + x = nx For y = ny1 To ny Degrees1\Longitude = x Degrees1\Latitude = y GetPixelCoordFromLocation(@Degrees1, @pos1) MovePathCursor(pos1\x, pos1\y) -<<<<<<< HEAD - AddPathLine( pos2\x, pos1\y) - Next -======= AddPathLine( pos2\x, pos1\y) MovePathCursor(10,pos1\y) - DrawVectorText(StrD(0-(90-Mod((y+90),180)),1)) + DrawVectorText(StrD(y, 1)) Next ->>>>>>> refs/remotes/origin/idle y = ny For x = nx To nx1 Degrees1\Longitude = x @@ -778,13 +747,9 @@ Module PBMap GetPixelCoordFromLocation(@Degrees1, @pos1) MovePathCursor(pos1\x, pos1\y) AddPathLine( pos1\x, pos2\y) -<<<<<<< HEAD - Next -======= MovePathCursor(pos1\x,10) - DrawVectorText(StrD(0-(180-Mod((x+180),360)),1)) + DrawVectorText(StrD(x, 1)) Next ->>>>>>> refs/remotes/origin/idle StrokePath(1) EndProcedure @@ -815,7 +780,7 @@ Module PBMap If ListSize(PBMap\track())>0 ;Trace Track ForEach PBMap\track() - If *Drawing\TargetLocation\Latitude<>0 And *Drawing\TargetLocation\Longitude<>0 + If *Drawing\TargetLocation\Latitude<>0 And *Drawing\TargetLocation\Longitude<>0 GetPixelCoordFromLocation(@PBMap\track(),@Pixel) If ListIndex(PBMap\track())=0 MovePathCursor(Pixel\X, Pixel\Y) @@ -851,11 +816,11 @@ Module PBMap EndProcedure ; Add a Marker To the Map - Procedure AddMarker(Latitude.d,Longitude.d,color.l=-1, CallBackPointer.i = -1) + Procedure AddMarker(Latitude.d, Longitude.d, color.l=-1, CallBackPointer.i = -1) AddElement(PBMap\Marker()) - PBMap\Marker()\Location\Latitude=Latitude - PBMap\Marker()\Location\Longitude=Longitude - PBMap\Marker()\color=color + PBMap\Marker()\Location\Latitude = Latitude + PBMap\Marker()\Location\Longitude = Longitude + PBMap\Marker()\color = color PBMap\Marker()\CallBackPointer = CallBackPointer EndProcedure @@ -869,7 +834,9 @@ Module PBMap If PBMap\Marker()\CallBackPointer > 0 CallFunctionFast(PBMap\Marker()\CallBackPointer, Pixel\X, Pixel\Y) Else + Debug 1 DrawPointer(*Drawing) + EndIf EndIf EndIf @@ -922,15 +889,6 @@ Module PBMap DrawScale(*Drawing,10,GadgetHeight(PBMAP\Gadget)-20,192) ;EndIf StopVectorDrawing() -<<<<<<< HEAD - ;If there was a problem while drawing, redraw - ; If PBMap\Dirty - ; PBMap\Redraw = #True - ; ;PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_REDRAW) - ; EndIf -======= - ->>>>>>> refs/remotes/origin/idle EndProcedure Procedure Refresh() @@ -999,7 +957,7 @@ Module PBMap Protected lat.d = centerY; SetLocation(lat,lon, Round(zoom,#PB_Round_Down)) Else - SetLocation(PBMap\TargetLocation\Latitude,PBMap\TargetLocation\Longitude, 15) + SetLocation(PBMap\TargetLocation\Latitude, PBMap\TargetLocation\Longitude, 15) EndIf EndProcedure @@ -1072,10 +1030,12 @@ Module PBMap Procedure.d GetLatitude() ProcedureReturn 0-(90-Mod((PBMap\TargetLocation\Latitude+90),180)) +; ProcedureReturn PBMap\TargetLocation\Latitude EndProcedure Procedure.d GetLongitude() ProcedureReturn 0-(180-Mod((PBMap\TargetLocation\Longitude+180),360)) +; ProcedureReturn PBMap\TargetLocation\Longitude EndProcedure Procedure.i GetZoom() @@ -1193,7 +1153,7 @@ Module PBMap EndModule -;-Exemple +;-Example CompilerIf #PB_Compiler_IsMainFile InitNetwork() @@ -1224,8 +1184,10 @@ CompilerIf #PB_Compiler_IsMainFile EndStructure Procedure UpdateLocation(*Location.Location) - SetGadgetText(#String_0, StrD(0-(90-Mod((*Location\Latitude+90),180)))) - SetGadgetText(#String_1, StrD(0-(180-Mod((*Location\Longitude+180),360)))) +; SetGadgetText(#String_0, StrD(0-(90-Mod((*Location\Latitude+90),180)))) +; SetGadgetText(#String_1, StrD(0-(180-Mod((*Location\Longitude+180),360)))) + SetGadgetText(#String_0, StrD(*Location\Latitude)) + SetGadgetText(#String_1, StrD(*Location\Longitude)) ProcedureReturn 0 EndProcedure @@ -1330,20 +1292,14 @@ CompilerIf #PB_Compiler_IsMainFile Until Quit = #True PBMap::Quit() -<<<<<<< HEAD EndIf CompilerEndIf + + ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 751 -; FirstLine = 719 -======= - EndIf - CompilerEndIf -; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1263 -; FirstLine = 1250 ->>>>>>> refs/remotes/origin/idle +; CursorPosition = 836 +; FirstLine = 821 ; Folding = --------- ; EnableThread ; EnableXP From c7fe9344d2333e4b2a80e853fa10c787a48ca3c8 Mon Sep 17 00:00:00 2001 From: djes Date: Wed, 31 Aug 2016 12:12:31 +0200 Subject: [PATCH 28/49] wip --- PBMap.pb | 99 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 32 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 7e2d542..1e248ea 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -175,6 +175,7 @@ Module PBMap EditMarkerIndex.l ImgLoading.i ;Image Loading Tile + ImgNothing.i ;Image Nothing Tile Options.option ; EndStructure @@ -260,10 +261,11 @@ Module PBMap EndProcedure ;- *** - Procedure LoadingImageCreation() + Procedure TechnicalImagesCreation() + ;"Loading" image Protected Text$ = "Loading" PBmap\ImgLoading = CreateImage(#PB_Any, 256, 256) - If PBmap\ImgLoading + If PBmap\ImgLoading StartVectorDrawing(ImageVectorOutput(PBMap\Imgloading)) BeginVectorLayer() VectorSourceColor(RGBA(255, 255, 255, 128)) @@ -277,6 +279,15 @@ Module PBMap EndVectorLayer() StopVectorDrawing() EndIf + ;"Nothing" tile + PBmap\ImgNothing = CreateImage(#PB_Any, 256, 256) + If PBmap\ImgNothing + StartVectorDrawing(ImageVectorOutput(PBMap\Imgloading)) + VectorSourceColor(RGBA(255, 255, 255, 128)) + AddPathBox(0, 0, 256, 256) + FillPath() + StopVectorDrawing() + EndIf EndProcedure Procedure InitPBMap(Window) @@ -323,7 +334,7 @@ Module PBMap EndIf ClosePreferences() curl_global_init(#CURL_GLOBAL_WIN32) - LoadingImageCreation() + TechnicalImagesCreation() EndProcedure Procedure SetMapServer(ServerURL.s = "http://tile.openstreetmap.org/", TileSize = 256, ZoomMin = 0, ZoomMax = 18) @@ -385,8 +396,8 @@ Module PBMap MyDebug("Latitude : " + StrD(*Location\Latitude) + " ; Longitude : " + StrD(*Location\Longitude)) MyDebug("Coords X : " + Str(*Coords\x) + " ; Y : " + Str(*Coords\y)) EndProcedure - - ;*** Converts tile.decimal to coords + + ;*** Converts tile.decimal to coords ;Warning, structures used in parameters are not tested Procedure XY2LatLon(*Coords.Position, *Location.Location) Protected n.d = Pow(2.0, PBMap\Zoom) @@ -611,25 +622,36 @@ Module PBMap Protected nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point Protected ny = *Drawing\CenterY / PBMap\TileSize Protected px, py, img, tilex,tiley, key.s, CacheFile.s + Protected tilemax = 1< -1 - MovePathCursor(px, py) - DrawVectorImage(ImageID(img), alpha) - Else - MovePathCursor(px, py) - DrawVectorImage(ImageID(PBMap\ImgLoading), alpha) + tilex = (tx + x) % tilemax + Debug "tx " + Str(tx) + Debug "x " + Str(x) + Debug "tilemax " + Str(tilemax) + Debug "tilex " + Str(tilex) + tiley = ty + y + If tiley >= 0 And tiley < tilemax + kq = Layer | (pbmap\zoom << 8) | (tilex << 16) | (tiley << 36) + key = Str(kq) + CacheFile = PBMap\HDDCachePath + key + ".png" + + img = GetTile(key, CacheFile, px, py, tilex, tiley, Layer) + If img <> -1 + MovePathCursor(px, py) + DrawVectorImage(ImageID(img), alpha) + Else + MovePathCursor(px, py) + DrawVectorImage(ImageID(PBMap\ImgLoading), alpha) + EndIf + Else + If Layer = 0 + MovePathCursor(px, py) + DrawVectorImage(ImageID(PBMap\ImgNothing)) + EndIf EndIf Next Next @@ -815,6 +837,20 @@ Module PBMap EndIf EndProcedure + Procedure DrawMarker(x.i, y.i, color.l = 0) + VectorSourceColor(color) + MovePathCursor(x, y) + AddPathLine(-8, -16, #PB_Path_Relative) + AddPathCircle(8, 0, 8, 180, 0, #PB_Path_Relative) + AddPathLine(-8, 16, #PB_Path_Relative) + ;FillPath(#PB_Path_Preserve) + ;ClipPath(#PB_Path_Preserve) + AddPathCircle(0, -16, 5, 0, 360, #PB_Path_Relative) + VectorSourceColor(color) + FillPath(#PB_Path_Preserve):VectorSourceColor(color);RGBA(0, 0, 0, 255)) + StrokePath(1) + EndProcedure + ; Add a Marker To the Map Procedure AddMarker(Latitude.d, Longitude.d, color.l=-1, CallBackPointer.i = -1) AddElement(PBMap\Marker()) @@ -822,8 +858,9 @@ Module PBMap PBMap\Marker()\Location\Longitude = Longitude PBMap\Marker()\color = color PBMap\Marker()\CallBackPointer = CallBackPointer + PBMap\Redraw = #True EndProcedure - + ; Draw all markers on the screen ! Procedure DrawMarkers(*Drawing.DrawingParameters) Protected Pixel.PixelPosition @@ -834,9 +871,7 @@ Module PBMap If PBMap\Marker()\CallBackPointer > 0 CallFunctionFast(PBMap\Marker()\CallBackPointer, Pixel\X, Pixel\Y) Else - Debug 1 - DrawPointer(*Drawing) - + DrawMarker(Pixel\X, Pixel\Y, PBMap\Marker()\color) EndIf EndIf EndIf @@ -1029,13 +1064,13 @@ Module PBMap EndProcedure Procedure.d GetLatitude() - ProcedureReturn 0-(90-Mod((PBMap\TargetLocation\Latitude+90),180)) -; ProcedureReturn PBMap\TargetLocation\Latitude +; ProcedureReturn 0-(90-Mod((PBMap\TargetLocation\Latitude+90),180)) + ProcedureReturn PBMap\TargetLocation\Latitude EndProcedure Procedure.d GetLongitude() - ProcedureReturn 0-(180-Mod((PBMap\TargetLocation\Longitude+180),360)) -; ProcedureReturn PBMap\TargetLocation\Longitude +; ProcedureReturn 0-(180-Mod((PBMap\TargetLocation\Longitude+180),360)) + ProcedureReturn PBMap\TargetLocation\Longitude EndProcedure Procedure.i GetZoom() @@ -1191,7 +1226,7 @@ CompilerIf #PB_Compiler_IsMainFile ProcedureReturn 0 EndProcedure - Procedure MyPointer(x.i, y.i) + Procedure MyMarker(x.i, y.i) Protected color.l color=RGBA(0, 255, 0, 255) VectorSourceColor(color) @@ -1259,7 +1294,7 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::SetLocation(-36.81148, 175.08634,12) PBMap::SetMapServer("http://t1.openseamap.org/seamark/") ;add a special osm overlay map PBMAP::SetMapScaleUnit(PBMAP::#SCALE_NAUTICAL) - PBMap::AddMarker(49.0446828398, 2.0349812508, -1, @MyPointer()) + PBMap::AddMarker(49.0446828398, 2.0349812508, -1, @MyMarker()) Repeat Event = WaitWindowEvent() @@ -1298,9 +1333,9 @@ CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 836 -; FirstLine = 821 -; Folding = --------- +; CursorPosition = 633 +; FirstLine = 612 +; Folding = ---------- ; EnableThread ; EnableXP ; EnableUnicode \ No newline at end of file From 6c06a85b70433d1848c26a4a15f3b04f347dc207 Mon Sep 17 00:00:00 2001 From: djes Date: Wed, 31 Aug 2016 12:54:43 +0200 Subject: [PATCH 29/49] neg modulo bugfix --- PBMap.pb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 1e248ea..9f9b553 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -402,7 +402,11 @@ Module PBMap Procedure XY2LatLon(*Coords.Position, *Location.Location) Protected n.d = Pow(2.0, PBMap\Zoom) Protected LatitudeRad.d - *Location\Longitude = *Coords\x / n * 360.0 - 180.0 + *Location\Longitude = Mod(*Coords\x / n * 360.0, 360.0) + If *Location\Longitude < 0 + *Location\Longitude + 360 + EndIf + *Location\Longitude - 180.0 LatitudeRad = ATan(SinH(#PI * (1.0 - 2.0 * *Coords\y / n))) *Location\Latitude = Degree(LatitudeRad) EndProcedure @@ -629,10 +633,9 @@ Module PBMap px = *Drawing\CenterX + x * PBMap\TileSize - *Drawing\DeltaX py = *Drawing\CenterY + y * PBMap\TileSize - *Drawing\DeltaY tilex = (tx + x) % tilemax - Debug "tx " + Str(tx) - Debug "x " + Str(x) - Debug "tilemax " + Str(tilemax) - Debug "tilex " + Str(tilex) + If tilex < 0 + tilex + tilemax + EndIf tiley = ty + y If tiley >= 0 And tiley < tilemax kq = Layer | (pbmap\zoom << 8) | (tilex << 16) | (tiley << 36) @@ -749,6 +752,8 @@ Module PBMap nx1 = Round(Degrees2\Longitude, #PB_Round_Up) +1 ny1 = Round(Degrees2\Latitude, #PB_Round_Down)-1 + Debug nx + Debug nx1 GetPixelCoordFromLocation(@Degrees2, @pos2) x = nx @@ -1333,8 +1338,8 @@ CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 633 -; FirstLine = 612 +; CursorPosition = 755 +; FirstLine = 732 ; Folding = ---------- ; EnableThread ; EnableXP From 90bd9ef731c10dd9197a24387fa28a8fedbc26c1 Mon Sep 17 00:00:00 2001 From: djes Date: Wed, 31 Aug 2016 14:55:22 +0200 Subject: [PATCH 30/49] wip --- PBMap.pb | 101 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 59 insertions(+), 42 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 9f9b553..276591a 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -29,7 +29,7 @@ DeclareModule PBMap #Red = 255 ;-Show debug infos Global Verbose = 0 - Global MyDebugLevel = 3 + Global MyDebugLevel = 5 ;-Proxy ON/OFF Global Proxy = #False @@ -393,8 +393,8 @@ Module PBMap Protected LatRad.d = Radian(*Location\Latitude) *Coords\x = n * ( (*Location\Longitude + 180.0) / 360.0) *Coords\y = n * ( 1.0 - Log(Tan(LatRad) + 1.0/Cos(LatRad)) / #PI ) / 2.0 - MyDebug("Latitude : " + StrD(*Location\Latitude) + " ; Longitude : " + StrD(*Location\Longitude)) - MyDebug("Coords X : " + Str(*Coords\x) + " ; Y : " + Str(*Coords\y)) + MyDebug("Latitude : " + StrD(*Location\Latitude) + " ; Longitude : " + StrD(*Location\Longitude), 5) + MyDebug("Coords X : " + Str(*Coords\x) + " ; Y : " + Str(*Coords\y), 5) EndProcedure ;*** Converts tile.decimal to coords @@ -407,10 +407,19 @@ Module PBMap *Location\Longitude + 360 EndIf *Location\Longitude - 180.0 + LatitudeRad = ATan(SinH(#PI * (1.0 - 2.0 * *Coords\y / n))) *Location\Latitude = Degree(LatitudeRad) EndProcedure + Procedure LatLon2Pixel(*Location.Location, *Pixel.PixelPosition) + ;Return the position of the coordinates relatively to the canvas center + Protected Pos.Position + LatLon2XY(*Location, @Pos) + *Pixel\x = PBMap\Drawing\CenterX + (Pos\x - PBMap\Drawing\Position\x) * PBMap\TileSize + *Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\Position\y) * PBMap\TileSize + EndProcedure + ; HaversineAlgorithm ; http://andrew.hedges.name/experiments/haversine/ Procedure.d HaversineInKM(*posA.Location, *posB.Location) @@ -429,27 +438,33 @@ Module PBMap ProcedureReturn (1000 * HaversineInKM(@*posA,@*posB)); EndProcedure - Procedure GetPixelCoordFromLocation(*Location.Location, *Pixel.PixelPosition) ; TODO to Optimize - Protected mapWidth.l = Pow(2, PBMap\Zoom + 8) - Protected mapHeight.l = Pow(2, PBMap\Zoom + 8) - Protected x1.l,y1.l - ; get x value - x1 = (*Location\Longitude+180)*(mapWidth/360) - ; convert from degrees To radians - Protected latRad.d = *Location\Latitude*#PI/180; - Protected mercN.d = Log(Tan((#PI/4)+(latRad/2))); - y1 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)) ; - Protected x2.l, y2.l - ; get x value - x2 = (PBMap\TargetLocation\Longitude+180)*(mapWidth/360) - ; convert from degrees To radians - latRad = PBMap\TargetLocation\Latitude*#PI/180; - ; get y value - mercN = Log(Tan((#PI/4)+(latRad/2))) - y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)); - *Pixel\x=GadgetWidth(PBMap\Gadget)/2 - (x2-x1) - *Pixel\y=GadgetHeight(PBMap\Gadget)/2 - (y2-y1) - EndProcedure +; Procedure GetPixelCoordFromLocation(*Location.Location, *Pixel.PixelPosition) ; TODO to Optimize +; Protected mapWidth.l = Pow(2, PBMap\Zoom + 8) +; Protected mapHeight.l = Pow(2, PBMap\Zoom + 8) +; Protected x1.l,y1.l +; ; get x value +; x1 = (*Location\Longitude+180)*(mapWidth/360) +; ; convert from degrees To radians +; Protected latRad.d = *Location\Latitude*#PI/180; +; Protected mercN.d = Log(Tan((#PI/4)+(latRad/2))); +; y1 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)) ; +; ; Debug "location" +; ; Debug x1 +; ; Debug y1 +; Protected x2.l, y2.l +; ; get x value +; x2 = (PBMap\TargetLocation\Longitude+180)*(mapWidth/360) +; ; convert from degrees To radians +; latRad = PBMap\TargetLocation\Latitude*#PI/180; +; ; get y value +; mercN = Log(Tan((#PI/4)+(latRad/2))) +; y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)); +; ; Debug "targetlocation" +; ; Debug x1 +; ; Debug y1 +; *Pixel\x=GadgetWidth(PBMap\Gadget)/2 - (x2-x1) +; *Pixel\y=GadgetHeight(PBMap\Gadget)/2 - (y2-y1) +; EndProcedure Procedure LoadGpxFile(file.s) If LoadXML(0, file.s) @@ -743,7 +758,7 @@ Module PBMap VectorFont(FontID(PBMap\Font), 10) VectorSourceColor(RGBA(0, 0, 0,alpha)) - + XY2LatLon(*Drawing\Bounds\NorthWest, @Degrees1) XY2LatLon(*Drawing\Bounds\SouthEast, @Degrees2) @@ -752,15 +767,14 @@ Module PBMap nx1 = Round(Degrees2\Longitude, #PB_Round_Up) +1 ny1 = Round(Degrees2\Latitude, #PB_Round_Down)-1 - Debug nx - Debug nx1 - GetPixelCoordFromLocation(@Degrees2, @pos2) + LatLon2Pixel(@Degrees2, @pos2) + x = nx For y = ny1 To ny Degrees1\Longitude = x Degrees1\Latitude = y - GetPixelCoordFromLocation(@Degrees1, @pos1) + LatLon2Pixel(@Degrees1, @pos1) MovePathCursor(pos1\x, pos1\y) AddPathLine( pos2\x, pos1\y) MovePathCursor(10,pos1\y) @@ -769,14 +783,14 @@ Module PBMap y = ny For x = nx To nx1 - Degrees1\Longitude = x - Degrees1\Latitude = y - GetPixelCoordFromLocation(@Degrees1, @pos1) - MovePathCursor(pos1\x, pos1\y) - AddPathLine( pos1\x, pos2\y) - MovePathCursor(pos1\x,10) - DrawVectorText(StrD(x, 1)) - Next + Degrees1\Longitude = x + Degrees1\Latitude = y + LatLon2Pixel(@Degrees1, @pos1) + MovePathCursor(pos1\x, pos1\y) + AddPathLine( pos1\x, pos2\y) + MovePathCursor(pos1\x,10) + DrawVectorText(StrD(x, 1)) + Next StrokePath(1) EndProcedure @@ -808,7 +822,8 @@ Module PBMap ;Trace Track ForEach PBMap\track() If *Drawing\TargetLocation\Latitude<>0 And *Drawing\TargetLocation\Longitude<>0 - GetPixelCoordFromLocation(@PBMap\track(),@Pixel) + ;GetPixelCoordFromLocation(@PBMap\track(),@Pixel) + LatLon2Pixel(@PBMap\track(),@Pixel) If ListIndex(PBMap\track())=0 MovePathCursor(Pixel\X, Pixel\Y) Else @@ -829,7 +844,8 @@ Module PBMap Location\Latitude=PBMap\track()\Latitude Location\Longitude=PBMap\track()\Longitude EndIf - GetPixelCoordFromLocation(@PBMap\track(),@Pixel) + ;GetPixelCoordFromLocation(@PBMap\track(),@Pixel) + LatLon2Pixel(@PBMap\track(),@Pixel) If Int(km)<>memKm memKm=Int(km) If PBMap\Zoom>10 @@ -871,7 +887,8 @@ Module PBMap Protected Pixel.PixelPosition ForEach PBMap\Marker() If PBMap\Marker()\Location\Latitude <> 0 And PBMap\Marker()\Location\Longitude <> 0 - GetPixelCoordFromLocation(PBMap\Marker()\Location, @Pixel) + ;GetPixelCoordFromLocation(PBMap\Marker()\Location, @Pixel) + LatLon2Pixel(PBMap\Marker()\Location, @Pixel) If Pixel\X >= 0 And Pixel\Y >= 0 And Pixel\X < GadgetWidth(PBMap\Gadget) And Pixel\Y < GadgetHeight(PBMap\Gadget) ; Only if visible ^_^ If PBMap\Marker()\CallBackPointer > 0 CallFunctionFast(PBMap\Marker()\CallBackPointer, Pixel\X, Pixel\Y) @@ -1338,8 +1355,8 @@ CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 755 -; FirstLine = 732 +; CursorPosition = 892 +; FirstLine = 873 ; Folding = ---------- ; EnableThread ; EnableXP From d7f7a2442bfae78b27b4b68c23cdfb8b9a38ad8f Mon Sep 17 00:00:00 2001 From: djes Date: Wed, 31 Aug 2016 17:26:52 +0200 Subject: [PATCH 31/49] Tracking this damn degrees bug --- PBMap.pb | 123 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 82 insertions(+), 41 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 276591a..26c3288 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -407,7 +407,6 @@ Module PBMap *Location\Longitude + 360 EndIf *Location\Longitude - 180.0 - LatitudeRad = ATan(SinH(#PI * (1.0 - 2.0 * *Coords\y / n))) *Location\Latitude = Degree(LatitudeRad) EndProcedure @@ -415,7 +414,7 @@ Module PBMap Procedure LatLon2Pixel(*Location.Location, *Pixel.PixelPosition) ;Return the position of the coordinates relatively to the canvas center Protected Pos.Position - LatLon2XY(*Location, @Pos) + LatLon2XY(*Location, @Pos) *Pixel\x = PBMap\Drawing\CenterX + (Pos\x - PBMap\Drawing\Position\x) * PBMap\TileSize *Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\Position\y) * PBMap\TileSize EndProcedure @@ -438,33 +437,33 @@ Module PBMap ProcedureReturn (1000 * HaversineInKM(@*posA,@*posB)); EndProcedure -; Procedure GetPixelCoordFromLocation(*Location.Location, *Pixel.PixelPosition) ; TODO to Optimize -; Protected mapWidth.l = Pow(2, PBMap\Zoom + 8) -; Protected mapHeight.l = Pow(2, PBMap\Zoom + 8) -; Protected x1.l,y1.l -; ; get x value -; x1 = (*Location\Longitude+180)*(mapWidth/360) -; ; convert from degrees To radians -; Protected latRad.d = *Location\Latitude*#PI/180; -; Protected mercN.d = Log(Tan((#PI/4)+(latRad/2))); -; y1 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)) ; -; ; Debug "location" -; ; Debug x1 -; ; Debug y1 -; Protected x2.l, y2.l -; ; get x value -; x2 = (PBMap\TargetLocation\Longitude+180)*(mapWidth/360) -; ; convert from degrees To radians -; latRad = PBMap\TargetLocation\Latitude*#PI/180; -; ; get y value -; mercN = Log(Tan((#PI/4)+(latRad/2))) -; y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)); -; ; Debug "targetlocation" -; ; Debug x1 -; ; Debug y1 -; *Pixel\x=GadgetWidth(PBMap\Gadget)/2 - (x2-x1) -; *Pixel\y=GadgetHeight(PBMap\Gadget)/2 - (y2-y1) -; EndProcedure + Procedure GetPixelCoordFromLocation(*Location.Location, *Pixel.PixelPosition) ; TODO to Optimize + Protected mapWidth.l = Pow(2, PBMap\Zoom + 8) + Protected mapHeight.l = Pow(2, PBMap\Zoom + 8) + Protected x1.l,y1.l + ; get x value + x1 = (*Location\Longitude+180)*(mapWidth/360) + ; convert from degrees To radians + Protected latRad.d = *Location\Latitude*#PI/180; + Protected mercN.d = Log(Tan((#PI/4)+(latRad/2))); + y1 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)) ; +; Debug "location" +; Debug x1 +; Debug y1 + Protected x2.l, y2.l + ; get x value + x2 = (PBMap\TargetLocation\Longitude+180)*(mapWidth/360) + ; convert from degrees To radians + latRad = PBMap\TargetLocation\Latitude*#PI/180; + ; get y value + mercN = Log(Tan((#PI/4)+(latRad/2))) + y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)); +; Debug "targetlocation" +; Debug x1 +; Debug y1 + *Pixel\x=GadgetWidth(PBMap\Gadget)/2 - (x2-x1) + *Pixel\y=GadgetHeight(PBMap\Gadget)/2 - (y2-y1) + EndProcedure Procedure LoadGpxFile(file.s) If LoadXML(0, file.s) @@ -756,8 +755,11 @@ Module PBMap *Drawing\Bounds\SouthEast\x = tx+nx+1 *Drawing\Bounds\SouthEast\y = ty+ny+1 - VectorFont(FontID(PBMap\Font), 10) - VectorSourceColor(RGBA(0, 0, 0,alpha)) +; Debug "bounds" +; Debug *Drawing\Bounds\NorthWest\x +; Debug *Drawing\Bounds\NorthWest\y +; Debug *Drawing\Bounds\SouthEast\x +; Debug *Drawing\Bounds\SouthEast\y XY2LatLon(*Drawing\Bounds\NorthWest, @Degrees1) XY2LatLon(*Drawing\Bounds\SouthEast, @Degrees2) @@ -767,21 +769,55 @@ Module PBMap nx1 = Round(Degrees2\Longitude, #PB_Round_Up) +1 ny1 = Round(Degrees2\Latitude, #PB_Round_Down)-1 - LatLon2Pixel(@Degrees2, @pos2) + Degrees1\Longitude = nx + Degrees1\Latitude = ny + Degrees2\Longitude = nx1 + Degrees2\Latitude = ny1 + +; Debug "lon lat center" +; Debug *Drawing\TargetLocation\Longitude +; Debug *Drawing\TargetLocation\Latitude +; Debug "lon lat 1" +; Debug Degrees1\Longitude +; Debug Degrees1\Latitude +; Debug "lon lat 2" +; Debug Degrees2\Longitude +; Debug Degrees2\Latitude + + LatLon2Pixel(@Degrees1, @pos1) +; Debug "pos1" +; Debug pos1\x +; Debug pos1\y + + LatLon2Pixel(@Degrees2, @pos2) +; Debug "pos2" +; Debug pos2\x +; Debug pos2\y + + VectorFont(FontID(PBMap\Font), 10) + VectorSourceColor(RGBA(0, 0, 0,alpha)) x = nx + If ny < ny1 + Swap ny, ny1 + x = nx1 + EndIf For y = ny1 To ny Degrees1\Longitude = x Degrees1\Latitude = y LatLon2Pixel(@Degrees1, @pos1) MovePathCursor(pos1\x, pos1\y) AddPathLine( pos2\x, pos1\y) - MovePathCursor(10,pos1\y) + MovePathCursor(10, pos1\y) DrawVectorText(StrD(y, 1)) Next y = ny + If nx > nx1 + Swap nx, nx1 + y = ny1 + EndIf For x = nx To nx1 Degrees1\Longitude = x Degrees1\Latitude = y @@ -949,7 +985,8 @@ Module PBMap EndProcedure Procedure Refresh() - Drawing() + PBMap\Redraw = #True + ;Drawing() EndProcedure Procedure SetLocation(latitude.d, longitude.d, zoom = 15, Mode.i = #PB_Absolute) @@ -970,7 +1007,8 @@ Module PBMap PBMap\Position\x = PBMap\Drawing\Position\x * PBMap\TileSize PBMap\Position\y = PBMap\Drawing\Position\y * PBMap\TileSize PBMap\Drawing\PassNb = 1 - Drawing() + PBMap\Redraw = #True + ;Drawing() If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) EndIf @@ -1030,9 +1068,10 @@ Module PBMap LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing) ;Convert X, Y in tile.decimal into real pixels PBMap\Position\X = PBMap\Drawing\Position\x * PBMap\TileSize - PBMap\Position\Y = PBMap\Drawing\Position\y * PBMap\TileSize + PBMap\Position\Y = PBMap\Drawing\Position\y * PBMap\TileSize ;First drawing - Drawing() + PBMap\Redraw = #True + ;Drawing() If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) EndIf @@ -1048,7 +1087,8 @@ Module PBMap Procedure SetMapScaleUnit(ScaleUnit.i = PBMAP::#SCALE_KM) PBMap\Options\ScaleUnit = ScaleUnit - Drawing() + PBMap\Redraw = #True + ;Drawing() EndProcedure ;Zoom on x, y position relative to the canvas gadget @@ -1078,7 +1118,8 @@ Module PBMap PBMap\Drawing\PassNb = 1 XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) ;Start drawing - Drawing() + PBMap\Redraw = #True + ;Drawing() ;If CallBackLocation send Location to function If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) @@ -1355,8 +1396,8 @@ CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 892 -; FirstLine = 873 +; CursorPosition = 416 +; FirstLine = 401 ; Folding = ---------- ; EnableThread ; EnableXP From 37197745c54d7ed3b9bfb969771e3e2b9a378b82 Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 1 Sep 2016 15:24:35 +0200 Subject: [PATCH 32/49] wip --- PBMap.pb | 88 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 38 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 26c3288..0527995 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -391,7 +391,7 @@ Module PBMap Procedure LatLon2XY(*Location.Location, *Coords.Position) Protected n.d = Pow(2.0, PBMap\Zoom) Protected LatRad.d = Radian(*Location\Latitude) - *Coords\x = n * ( (*Location\Longitude + 180.0) / 360.0) + *Coords\x = n * (Mod( *Location\Longitude + 180.0, 360) / 360.0 ) *Coords\y = n * ( 1.0 - Log(Tan(LatRad) + 1.0/Cos(LatRad)) / #PI ) / 2.0 MyDebug("Latitude : " + StrD(*Location\Latitude) + " ; Longitude : " + StrD(*Location\Longitude), 5) MyDebug("Coords X : " + Str(*Coords\x) + " ; Y : " + Str(*Coords\y), 5) @@ -406,17 +406,32 @@ Module PBMap If *Location\Longitude < 0 *Location\Longitude + 360 EndIf - *Location\Longitude - 180.0 + *Location\Longitude = Mod(*Location\Longitude, 360.0) - 180 LatitudeRad = ATan(SinH(#PI * (1.0 - 2.0 * *Coords\y / n))) *Location\Latitude = Degree(LatitudeRad) EndProcedure Procedure LatLon2Pixel(*Location.Location, *Pixel.PixelPosition) - ;Return the position of the coordinates relatively to the canvas center Protected Pos.Position LatLon2XY(*Location, @Pos) + Debug "Longitude : " + StrD(*Location\Longitude) + " ; Pos : " + StrD(Pos\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\Position\x) + "/" + Str(Pow(2.0, PBMap\Zoom)) *Pixel\x = PBMap\Drawing\CenterX + (Pos\x - PBMap\Drawing\Position\x) * PBMap\TileSize *Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\Position\y) * PBMap\TileSize + +; Protected Pos.Position +; Protected Center.Location, Pnt.Location +; ;Return the position of the coordinates relatively to the canvas center +; If Abs(PBMap\Drawing\TargetLocation\Longitude - *Location\Longitude) > 180 And Sign(PBMap\Drawing\TargetLocation\Longitude) <> Sign(*Location\Longitude) +; Pnt\Longitude = PBMap\Drawing\TargetLocation\Longitude - (360 - (PBMap\Drawing\TargetLocation\Longitude - *Location\Longitude)) +; Pnt\Latitude = *Location\Latitude +; LatLon2XY(@Pnt, @Pos) +; *Pixel\x = PBMap\Drawing\CenterX + (Pos\x - PBMap\Drawing\Position\x) * PBMap\TileSize +; *Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\Position\y) * PBMap\TileSize +; Else +; LatLon2XY(*Location, @Pos) +; *Pixel\x = PBMap\Drawing\CenterX + (Pos\x - PBMap\Drawing\Position\x) * PBMap\TileSize +; *Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\Position\y) * PBMap\TileSize +; EndIf EndProcedure ; HaversineAlgorithm @@ -744,6 +759,7 @@ Module PBMap Procedure DrawDegrees(*Drawing.DrawingParameters,alpha=192) Protected tx, ty, nx,ny,nx1,ny1,x,y,n,cx,dperpixel.d Protected pos1.PixelPosition,pos2.PixelPosition,Degrees1.Location,degrees2.Location + Protected realx tx = Int(*Drawing\Position\x) ty = Int(*Drawing\Position\y) @@ -769,40 +785,42 @@ Module PBMap nx1 = Round(Degrees2\Longitude, #PB_Round_Up) +1 ny1 = Round(Degrees2\Latitude, #PB_Round_Down)-1 + ;ensure we stay positive for the drawing + realx = nx + If nx < 0 + nx + 360 + EndIf + If nx1 < 0 + nx1 + 360 + EndIf + nx % 360 + nx1 % 360 + + Debug "---" + Debug nx + Debug nx1 + Debug ny1 + Debug ny + Degrees1\Longitude = nx Degrees1\Latitude = ny Degrees2\Longitude = nx1 Degrees2\Latitude = ny1 -; Debug "lon lat center" -; Debug *Drawing\TargetLocation\Longitude -; Debug *Drawing\TargetLocation\Latitude -; Debug "lon lat 1" -; Debug Degrees1\Longitude -; Debug Degrees1\Latitude -; Debug "lon lat 2" -; Debug Degrees2\Longitude -; Debug Degrees2\Latitude - LatLon2Pixel(@Degrees1, @pos1) -; Debug "pos1" -; Debug pos1\x -; Debug pos1\y - LatLon2Pixel(@Degrees2, @pos2) -; Debug "pos2" -; Debug pos2\x -; Debug pos2\y + + Debug "---" + Debug pos1\x + Debug pos1\y + Debug pos2\x + Debug pos2\y VectorFont(FontID(PBMap\Font), 10) VectorSourceColor(RGBA(0, 0, 0,alpha)) - x = nx - If ny < ny1 - Swap ny, ny1 - x = nx1 - EndIf + ;draw latitudes For y = ny1 To ny Degrees1\Longitude = x Degrees1\Latitude = y @@ -812,12 +830,8 @@ Module PBMap MovePathCursor(10, pos1\y) DrawVectorText(StrD(y, 1)) Next - - y = ny - If nx > nx1 - Swap nx, nx1 - y = ny1 - EndIf + + ;draw longitudes For x = nx To nx1 Degrees1\Longitude = x Degrees1\Latitude = y @@ -825,7 +839,7 @@ Module PBMap MovePathCursor(pos1\x, pos1\y) AddPathLine( pos1\x, pos2\y) MovePathCursor(pos1\x,10) - DrawVectorText(StrD(x, 1)) + DrawVectorText(Str(x)) Next StrokePath(1) @@ -1105,10 +1119,8 @@ Module PBMap ;Centered Zoom LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing) ;Convert X, Y in tile.decimal into real pixels - PBMap\Position\x = PBMap\Drawing\Position\x * PBMap\TileSize - PBMap\Position\y = PBMap\Drawing\Position\y * PBMap\TileSize - MouseX = PBMap\Position\x + GadgetWidth(PBMap\Gadget) / 2 - x - MouseY = PBMap\Position\y + GadgetHeight(PBMap\Gadget) / 2 - y + MouseX = PBMap\Drawing\Position\x * PBMap\TileSize + GadgetWidth(PBMap\Gadget) / 2 - x + MouseY = PBMap\Drawing\Position\y * PBMap\TileSize + GadgetHeight(PBMap\Gadget) / 2 - y ;Cross-multiply to get the new center PBMap\Position\x = (OldPx * MouseX) / OldMx PBMap\Position\y = (OldPy * MouseY) / OldMy @@ -1396,8 +1408,8 @@ CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 416 -; FirstLine = 401 +; CursorPosition = 1020 +; FirstLine = 999 ; Folding = ---------- ; EnableThread ; EnableXP From 3f36ae9799affb984f9fe5192897a60d4ad87ae6 Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 1 Sep 2016 17:30:54 +0200 Subject: [PATCH 33/49] Working Degrees (phew!), now working on marker select (doh!) marker select now doesn't work as the coordinates are in a clipped range... --- PBMap.pb | 85 ++++++++++++++++++++------------------------------------ 1 file changed, 30 insertions(+), 55 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 0527995..2951b93 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -413,25 +413,19 @@ Module PBMap Procedure LatLon2Pixel(*Location.Location, *Pixel.PixelPosition) Protected Pos.Position + Protected tilemax = Pow(2.0, PBMap\Zoom) LatLon2XY(*Location, @Pos) - Debug "Longitude : " + StrD(*Location\Longitude) + " ; Pos : " + StrD(Pos\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\Position\x) + "/" + Str(Pow(2.0, PBMap\Zoom)) - *Pixel\x = PBMap\Drawing\CenterX + (Pos\x - PBMap\Drawing\Position\x) * PBMap\TileSize + ;check the x boundaries of the map to adjust the position + If PBMap\Drawing\Position\x - Pos\x > tilemax / 2 + *Pixel\x = PBMap\Drawing\CenterX + (Pos\x - PBMap\Drawing\Position\x + tilemax) * PBMap\TileSize + ElseIf Pos\x - PBMap\Drawing\Position\x > tilemax / 2 + *Pixel\x = PBMap\Drawing\CenterX + (Pos\x - PBMap\Drawing\Position\x - tilemax) * PBMap\TileSize + Else + *Pixel\x = PBMap\Drawing\CenterX + (Pos\x - PBMap\Drawing\Position\x) * PBMap\TileSize + EndIf *Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\Position\y) * PBMap\TileSize - -; Protected Pos.Position -; Protected Center.Location, Pnt.Location -; ;Return the position of the coordinates relatively to the canvas center -; If Abs(PBMap\Drawing\TargetLocation\Longitude - *Location\Longitude) > 180 And Sign(PBMap\Drawing\TargetLocation\Longitude) <> Sign(*Location\Longitude) -; Pnt\Longitude = PBMap\Drawing\TargetLocation\Longitude - (360 - (PBMap\Drawing\TargetLocation\Longitude - *Location\Longitude)) -; Pnt\Latitude = *Location\Latitude -; LatLon2XY(@Pnt, @Pos) -; *Pixel\x = PBMap\Drawing\CenterX + (Pos\x - PBMap\Drawing\Position\x) * PBMap\TileSize -; *Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\Position\y) * PBMap\TileSize -; Else -; LatLon2XY(*Location, @Pos) -; *Pixel\x = PBMap\Drawing\CenterX + (Pos\x - PBMap\Drawing\Position\x) * PBMap\TileSize -; *Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\Position\y) * PBMap\TileSize -; EndIf +; Debug "Longitude : " + StrD(*Location\Longitude) + " ; Pos : " + StrD(Pos\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\Position\x) + "/" + Str(tilemax) + +; " ; XY : " + Str(*Pixel\x) + "," + Str(*Pixel\y) EndProcedure ; HaversineAlgorithm @@ -760,31 +754,20 @@ Module PBMap Protected tx, ty, nx,ny,nx1,ny1,x,y,n,cx,dperpixel.d Protected pos1.PixelPosition,pos2.PixelPosition,Degrees1.Location,degrees2.Location Protected realx - tx = Int(*Drawing\Position\x) ty = Int(*Drawing\Position\y) nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point ny = *Drawing\CenterY / PBMap\TileSize - *Drawing\Bounds\NorthWest\x = tx-nx-1 *Drawing\Bounds\NorthWest\y = ty-ny-1 *Drawing\Bounds\SouthEast\x = tx+nx+1 *Drawing\Bounds\SouthEast\y = ty+ny+1 - -; Debug "bounds" -; Debug *Drawing\Bounds\NorthWest\x -; Debug *Drawing\Bounds\NorthWest\y -; Debug *Drawing\Bounds\SouthEast\x -; Debug *Drawing\Bounds\SouthEast\y - XY2LatLon(*Drawing\Bounds\NorthWest, @Degrees1) XY2LatLon(*Drawing\Bounds\SouthEast, @Degrees2) - nx = Round(Degrees1\Longitude, #PB_Round_Down)-1 ny = Round(Degrees1\Latitude, #PB_Round_Up) +1 nx1 = Round(Degrees2\Longitude, #PB_Round_Up) +1 ny1 = Round(Degrees2\Latitude, #PB_Round_Down)-1 - ;ensure we stay positive for the drawing realx = nx If nx < 0 @@ -795,34 +778,17 @@ Module PBMap EndIf nx % 360 nx1 % 360 - - Debug "---" - Debug nx - Debug nx1 - Debug ny1 - Debug ny - Degrees1\Longitude = nx Degrees1\Latitude = ny - Degrees2\Longitude = nx1 Degrees2\Latitude = ny1 - LatLon2Pixel(@Degrees1, @pos1) LatLon2Pixel(@Degrees2, @pos2) - - Debug "---" - Debug pos1\x - Debug pos1\y - Debug pos2\x - Debug pos2\y - VectorFont(FontID(PBMap\Font), 10) VectorSourceColor(RGBA(0, 0, 0,alpha)) - ;draw latitudes For y = ny1 To ny - Degrees1\Longitude = x + Degrees1\Longitude = nx Degrees1\Latitude = y LatLon2Pixel(@Degrees1, @pos1) MovePathCursor(pos1\x, pos1\y) @@ -830,19 +796,21 @@ Module PBMap MovePathCursor(10, pos1\y) DrawVectorText(StrD(y, 1)) Next - ;draw longitudes For x = nx To nx1 Degrees1\Longitude = x - Degrees1\Latitude = y + Degrees1\Latitude = ny LatLon2Pixel(@Degrees1, @pos1) MovePathCursor(pos1\x, pos1\y) AddPathLine( pos1\x, pos2\y) MovePathCursor(pos1\x,10) - DrawVectorText(Str(x)) + DrawVectorText(StrD(realx, 1)) + realx + 1 + If realx > 180 + realx - 360 + EndIf Next StrokePath(1) - EndProcedure Procedure TrackPointer(x.i, y.i,dist.l) @@ -965,7 +933,6 @@ Module PBMap *Drawing\DeltaY = Py * PBMap\TileSize - (Int(Py) * PBMap\TileSize) *Drawing\TargetLocation\Latitude = PBMap\TargetLocation\Latitude *Drawing\TargetLocation\Longitude = PBMap\TargetLocation\Longitude - ;Main drawing stuff StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget)) ;TODO add in layers of tiles ;this way we can cache them as 0 base 1.n layers @@ -1172,7 +1139,9 @@ Module PBMap MouseX = PBMap\Position\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) MouseY = PBMap\Position\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) ForEach PBMap\Marker() - LatLon2XY(@PBMap\Marker()\Location, @Marker) + LatLon2XY(@PBMap\Marker()\Location, @Marker) + Debug "Pos : " + StrD(Marker\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\Position\x) + Marker\x * PBMap\TileSize Marker\y * PBMap\TileSize If Distance(Marker\x, Marker\y, MouseX, MouseY) < 8 @@ -1197,7 +1166,13 @@ Module PBMap XY2LatLon(@Marker, @PBMap\Marker()\Location) Else ;New move values - PBMap\Position\x - MouseX + ;PBMap\Position\x - MouseX + ;Ensures that pixel position stay in the range [0..2^Zoom*PBMap\TileSize[ + PBMap\Position\x = PBMap\Position\x - MouseX + If PBMap\Position\x < 0 + PBMap\Position\x + Pow(2, PBMap\Zoom) * PBMap\TileSize + EndIf + PBMap\Position\x = Mod(PBMap\Position\x, Pow(2, PBMap\Zoom) * PBMap\TileSize) PBMap\Position\y - MouseY ;PBMap tile position in tile.decimal PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize @@ -1408,8 +1383,8 @@ CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1020 -; FirstLine = 999 +; CursorPosition = 940 +; FirstLine = 932 ; Folding = ---------- ; EnableThread ; EnableXP From c1a6dda16a4fcc0a6f08c178669ad93101a78c21 Mon Sep 17 00:00:00 2001 From: djes Date: Thu, 1 Sep 2016 17:43:40 +0200 Subject: [PATCH 34/49] Marker select fixed --- PBMap.pb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 2951b93..23eca67 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -1138,12 +1138,18 @@ Module PBMap ;Check if we select a marker MouseX = PBMap\Position\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) MouseY = PBMap\Position\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) + ;Clip MouseX to the map range (in X, the map is infinite) + If MouseX < 0 + MouseX + Pow(2, PBMap\Zoom) * PBMap\TileSize + EndIf + MouseX = Mod(MouseX, Pow(2, PBMap\Zoom) * PBMap\TileSize) +; Debug "---" +; Debug "mx : " + Str(MouseX) ForEach PBMap\Marker() LatLon2XY(@PBMap\Marker()\Location, @Marker) - Debug "Pos : " + StrD(Marker\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\Position\x) - Marker\x * PBMap\TileSize Marker\y * PBMap\TileSize +; Debug "Pos : " + StrD(Marker\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\Position\x) If Distance(Marker\x, Marker\y, MouseX, MouseY) < 8 PBMap\EditMarkerIndex = ListIndex(PBMap\Marker()) Break @@ -1383,8 +1389,8 @@ CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 940 -; FirstLine = 932 +; CursorPosition = 1149 +; FirstLine = 1122 ; Folding = ---------- ; EnableThread ; EnableXP From 3ab69529980bc0ec72b1fad4c730d443a394d2ce Mon Sep 17 00:00:00 2001 From: djes Date: Fri, 2 Sep 2016 08:51:40 +0200 Subject: [PATCH 35/49] idle's fix --- PBMap.pb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 23eca67..8ef2574 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -392,7 +392,7 @@ Module PBMap Protected n.d = Pow(2.0, PBMap\Zoom) Protected LatRad.d = Radian(*Location\Latitude) *Coords\x = n * (Mod( *Location\Longitude + 180.0, 360) / 360.0 ) - *Coords\y = n * ( 1.0 - Log(Tan(LatRad) + 1.0/Cos(LatRad)) / #PI ) / 2.0 + *Coords\y = n * ( 1.0 - Log(Tan(LatRad) + (1.0/Cos(LatRad))) / #PI ) / 2.0 MyDebug("Latitude : " + StrD(*Location\Latitude) + " ; Longitude : " + StrD(*Location\Longitude), 5) MyDebug("Coords X : " + Str(*Coords\x) + " ; Y : " + Str(*Coords\y), 5) EndProcedure @@ -894,7 +894,7 @@ Module PBMap Procedure AddMarker(Latitude.d, Longitude.d, color.l=-1, CallBackPointer.i = -1) AddElement(PBMap\Marker()) PBMap\Marker()\Location\Latitude = Latitude - PBMap\Marker()\Location\Longitude = Longitude + PBMap\Marker()\Location\Longitude = Mod(Longitude + 360, 360) PBMap\Marker()\color = color PBMap\Marker()\CallBackPointer = CallBackPointer PBMap\Redraw = #True @@ -1375,7 +1375,7 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::LoadGpxFile(OpenFileRequester("Choose a file to load", "", "*.gpx", 0)) PBMap::ZoomToArea() ; <-To center the view, and zoom on the tracks Case #Gdt_AddMarker - PBMap:: AddMarker(ValD(GetGadgetText(#String_0)), ValD(GetGadgetText(#String_1)), RGBA(Random(255), Random(255), Random(255),255)) + PBMap:: AddMarker(ValD(GetGadgetText(#String_0)), ValD(GetGadgetText(#String_1)), RGBA(Random(255), Random(255), Random(255), 255), @MyMarker()) EndSelect Case #PB_Event_SizeWindow ResizeAll() @@ -1386,11 +1386,9 @@ CompilerIf #PB_Compiler_IsMainFile EndIf CompilerEndIf - - ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1149 -; FirstLine = 1122 +; CursorPosition = 1353 +; FirstLine = 1344 ; Folding = ---------- ; EnableThread ; EnableXP From cfc43dd86c9c5b5aa2118be1a00bd13a8148db74 Mon Sep 17 00:00:00 2001 From: djes Date: Fri, 2 Sep 2016 12:32:10 +0200 Subject: [PATCH 36/49] degrees bugfix --- PBMap.pb | 93 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 54 insertions(+), 39 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 8ef2574..b882be4 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -402,11 +402,14 @@ Module PBMap Procedure XY2LatLon(*Coords.Position, *Location.Location) Protected n.d = Pow(2.0, PBMap\Zoom) Protected LatitudeRad.d - *Location\Longitude = Mod(*Coords\x / n * 360.0, 360.0) - If *Location\Longitude < 0 - *Location\Longitude + 360 - EndIf - *Location\Longitude = Mod(*Location\Longitude, 360.0) - 180 + ;Ensures the longitude to be in the range [-180;180[ + *Location\Longitude = Mod(Mod(*Coords\x / n * 360.0, 360.0) + 360.0, 360.0) + *Location\Longitude - 180 +; *Location\Longitude = Mod(*Coords\x / n * 360.0, 360.0) +; If *Location\Longitude < 0 +; *Location\Longitude + 360 +; EndIf +; *Location\Longitude = Mod(*Location\Longitude, 360.0) - 180 LatitudeRad = ATan(SinH(#PI * (1.0 - 2.0 * *Coords\y / n))) *Location\Latitude = Degree(LatitudeRad) EndProcedure @@ -414,18 +417,29 @@ Module PBMap Procedure LatLon2Pixel(*Location.Location, *Pixel.PixelPosition) Protected Pos.Position Protected tilemax = Pow(2.0, PBMap\Zoom) + Protected cx.d = PBMap\Drawing\CenterX, dpx.d = PBMap\Drawing\Position\x LatLon2XY(*Location, @Pos) - ;check the x boundaries of the map to adjust the position - If PBMap\Drawing\Position\x - Pos\x > tilemax / 2 - *Pixel\x = PBMap\Drawing\CenterX + (Pos\x - PBMap\Drawing\Position\x + tilemax) * PBMap\TileSize - ElseIf Pos\x - PBMap\Drawing\Position\x > tilemax / 2 - *Pixel\x = PBMap\Drawing\CenterX + (Pos\x - PBMap\Drawing\Position\x - tilemax) * PBMap\TileSize + Protected px.d = Pos\x + ;check the x boundaries of the map to adjust the position (coz of the longitude wrapping) + If dpx - px > tilemax / 2 + ;Debug "c1" + *Pixel\x = cx + (px - dpx + tilemax) * PBMap\TileSize + ElseIf px - dpx > tilemax / 2 + ;Debug "c2" + *Pixel\x = cx + (px - dpx - tilemax) * PBMap\TileSize + ElseIf dpx - px < 0 + ;Debug "c3" + *Pixel\x = cx + (px - dpx) * PBMap\TileSize + ElseIf px - dpx < 0 + ;Debug "c4" + *Pixel\x = cx - (dpx - px) * PBMap\TileSize Else - *Pixel\x = PBMap\Drawing\CenterX + (Pos\x - PBMap\Drawing\Position\x) * PBMap\TileSize + ;Debug "c0" + *Pixel\x = cx + (px - dpx) * PBMap\TileSize EndIf *Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\Position\y) * PBMap\TileSize -; Debug "Longitude : " + StrD(*Location\Longitude) + " ; Pos : " + StrD(Pos\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\Position\x) + "/" + Str(tilemax) + -; " ; XY : " + Str(*Pixel\x) + "," + Str(*Pixel\y) + ;Debug "Longitude : " + StrD(*Location\Longitude) + " ; Pos : " + StrD(Pos\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\Position\x) + "/" + Str(tilemax) + + ; " ; XY : " + Str(*Pixel\x) + "," + Str(*Pixel\y) EndProcedure ; HaversineAlgorithm @@ -762,30 +776,32 @@ Module PBMap *Drawing\Bounds\NorthWest\y = ty-ny-1 *Drawing\Bounds\SouthEast\x = tx+nx+1 *Drawing\Bounds\SouthEast\y = ty+ny+1 +; Debug "------------------" XY2LatLon(*Drawing\Bounds\NorthWest, @Degrees1) XY2LatLon(*Drawing\Bounds\SouthEast, @Degrees2) - nx = Round(Degrees1\Longitude, #PB_Round_Down)-1 - ny = Round(Degrees1\Latitude, #PB_Round_Up) +1 - nx1 = Round(Degrees2\Longitude, #PB_Round_Up) +1 - ny1 = Round(Degrees2\Latitude, #PB_Round_Down)-1 ;ensure we stay positive for the drawing + nx = Mod(Mod(Round(Degrees1\Longitude, #PB_Round_Down)-1, 360) + 360, 360) + ny = Round(Degrees1\Latitude, #PB_Round_Up) +1 + nx1 = Mod(Mod(Round(Degrees2\Longitude, #PB_Round_Up) +1, 360) + 360, 360) + ny1 = Round(Degrees2\Latitude, #PB_Round_Down)-1 realx = nx - If nx < 0 - nx + 360 - EndIf - If nx1 < 0 - nx1 + 360 - EndIf - nx % 360 - nx1 % 360 +; If nx < 0 +; nx + 360 +; EndIf +; If nx1 < 0 +; nx1 + 360 +; EndIf +; nx % 360 +; nx1 % 360 Degrees1\Longitude = nx Degrees1\Latitude = ny Degrees2\Longitude = nx1 Degrees2\Latitude = ny1 +; Debug "NW : " + StrD(Degrees1\Longitude) + " ; NE : " + StrD(Degrees2\Longitude) LatLon2Pixel(@Degrees1, @pos1) LatLon2Pixel(@Degrees2, @pos2) VectorFont(FontID(PBMap\Font), 10) - VectorSourceColor(RGBA(0, 0, 0,alpha)) + VectorSourceColor(RGBA(0, 0, 0, alpha)) ;draw latitudes For y = ny1 To ny Degrees1\Longitude = nx @@ -797,7 +813,8 @@ Module PBMap DrawVectorText(StrD(y, 1)) Next ;draw longitudes - For x = nx To nx1 + x = nx + Repeat Degrees1\Longitude = x Degrees1\Latitude = ny LatLon2Pixel(@Degrees1, @pos1) @@ -809,7 +826,8 @@ Module PBMap If realx > 180 realx - 360 EndIf - Next + x = (x + 1)%360 + Until x = nx1 StrokePath(1) EndProcedure @@ -894,7 +912,7 @@ Module PBMap Procedure AddMarker(Latitude.d, Longitude.d, color.l=-1, CallBackPointer.i = -1) AddElement(PBMap\Marker()) PBMap\Marker()\Location\Latitude = Latitude - PBMap\Marker()\Location\Longitude = Mod(Longitude + 360, 360) + PBMap\Marker()\Location\Longitude = Mod(Mod(Longitude, 360) + 360, 360) PBMap\Marker()\color = color PBMap\Marker()\CallBackPointer = CallBackPointer PBMap\Redraw = #True @@ -1088,6 +1106,7 @@ Module PBMap ;Convert X, Y in tile.decimal into real pixels MouseX = PBMap\Drawing\Position\x * PBMap\TileSize + GadgetWidth(PBMap\Gadget) / 2 - x MouseY = PBMap\Drawing\Position\y * PBMap\TileSize + GadgetHeight(PBMap\Gadget) / 2 - y + Debug PBMap\Position\x ;Cross-multiply to get the new center PBMap\Position\x = (OldPx * MouseX) / OldMx PBMap\Position\y = (OldPy * MouseY) / OldMy @@ -1123,7 +1142,7 @@ Module PBMap Procedure CanvasEvents() Protected MouseX.i, MouseY.i - Protected Marker.Position, *Tile.Tile + Protected Marker.Position, *Tile.Tile, MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize PBMap\Moving = #False Select EventType() Case #PB_EventType_MouseWheel @@ -1139,12 +1158,7 @@ Module PBMap MouseX = PBMap\Position\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) MouseY = PBMap\Position\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) ;Clip MouseX to the map range (in X, the map is infinite) - If MouseX < 0 - MouseX + Pow(2, PBMap\Zoom) * PBMap\TileSize - EndIf - MouseX = Mod(MouseX, Pow(2, PBMap\Zoom) * PBMap\TileSize) -; Debug "---" -; Debug "mx : " + Str(MouseX) + MouseX = Mod(Mod(MouseX, MapWidth) + MapWidth, MapWidth) ForEach PBMap\Marker() LatLon2XY(@PBMap\Marker()\Location, @Marker) Marker\x * PBMap\TileSize @@ -1173,7 +1187,7 @@ Module PBMap Else ;New move values ;PBMap\Position\x - MouseX - ;Ensures that pixel position stay in the range [0..2^Zoom*PBMap\TileSize[ + ;Ensures that pixel position stay in the range [0..2^Zoom*PBMap\TileSize[ coz of the wrapping of the map PBMap\Position\x = PBMap\Position\x - MouseX If PBMap\Position\x < 0 PBMap\Position\x + Pow(2, PBMap\Zoom) * PBMap\TileSize @@ -1348,6 +1362,7 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::SetCallBackMainPointer(@MainPointer()) ;To change the Main Pointer PBMap::SetCallBackLocation(@UpdateLocation()) PBMap::SetLocation(-36.81148, 175.08634,12) +; PBMap::SetLocation(0, 0) PBMap::SetMapServer("http://t1.openseamap.org/seamark/") ;add a special osm overlay map PBMAP::SetMapScaleUnit(PBMAP::#SCALE_NAUTICAL) PBMap::AddMarker(49.0446828398, 2.0349812508, -1, @MyMarker()) @@ -1387,8 +1402,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1353 -; FirstLine = 1344 +; CursorPosition = 1364 +; FirstLine = 1352 ; Folding = ---------- ; EnableThread ; EnableXP From f6f149e84ec81d4e46c7d5fd03ffaf617143db35 Mon Sep 17 00:00:00 2001 From: djes Date: Fri, 2 Sep 2016 12:48:46 +0200 Subject: [PATCH 37/49] cleanup --- PBMap.pb | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index b882be4..c66bdbf 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -403,8 +403,7 @@ Module PBMap Protected n.d = Pow(2.0, PBMap\Zoom) Protected LatitudeRad.d ;Ensures the longitude to be in the range [-180;180[ - *Location\Longitude = Mod(Mod(*Coords\x / n * 360.0, 360.0) + 360.0, 360.0) - *Location\Longitude - 180 + *Location\Longitude = Mod(Mod(*Coords\x / n * 360.0, 360.0) + 360.0, 360.0) - 180 ; *Location\Longitude = Mod(*Coords\x / n * 360.0, 360.0) ; If *Location\Longitude < 0 ; *Location\Longitude + 360 @@ -782,17 +781,8 @@ Module PBMap ;ensure we stay positive for the drawing nx = Mod(Mod(Round(Degrees1\Longitude, #PB_Round_Down)-1, 360) + 360, 360) ny = Round(Degrees1\Latitude, #PB_Round_Up) +1 - nx1 = Mod(Mod(Round(Degrees2\Longitude, #PB_Round_Up) +1, 360) + 360, 360) + nx1 = Mod(Mod(Round(Degrees2\Longitude, #PB_Round_Up) +1, 360) + 360, 360) ny1 = Round(Degrees2\Latitude, #PB_Round_Down)-1 - realx = nx -; If nx < 0 -; nx + 360 -; EndIf -; If nx1 < 0 -; nx1 + 360 -; EndIf -; nx % 360 -; nx1 % 360 Degrees1\Longitude = nx Degrees1\Latitude = ny Degrees2\Longitude = nx1 @@ -821,13 +811,9 @@ Module PBMap MovePathCursor(pos1\x, pos1\y) AddPathLine( pos1\x, pos2\y) MovePathCursor(pos1\x,10) - DrawVectorText(StrD(realx, 1)) - realx + 1 - If realx > 180 - realx - 360 - EndIf + DrawVectorText(StrD(Mod(x + 180, 360) - 180, 1)) x = (x + 1)%360 - Until x = nx1 + Until x = nx1 StrokePath(1) EndProcedure @@ -1402,8 +1388,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1364 -; FirstLine = 1352 +; CursorPosition = 413 +; FirstLine = 467 ; Folding = ---------- ; EnableThread ; EnableXP From 93c64758ac8a64f786b454b4515e54fe84ea7d64 Mon Sep 17 00:00:00 2001 From: djes Date: Fri, 2 Sep 2016 14:00:25 +0200 Subject: [PATCH 38/49] cleanup --- PBMap.pb | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index c66bdbf..c6cdef0 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -421,19 +421,16 @@ Module PBMap Protected px.d = Pos\x ;check the x boundaries of the map to adjust the position (coz of the longitude wrapping) If dpx - px > tilemax / 2 - ;Debug "c1" + Debug "c1" *Pixel\x = cx + (px - dpx + tilemax) * PBMap\TileSize ElseIf px - dpx > tilemax / 2 - ;Debug "c2" + Debug "c2" *Pixel\x = cx + (px - dpx - tilemax) * PBMap\TileSize - ElseIf dpx - px < 0 - ;Debug "c3" - *Pixel\x = cx + (px - dpx) * PBMap\TileSize - ElseIf px - dpx < 0 - ;Debug "c4" + ElseIf px - dpx < 0 + Debug "c3" *Pixel\x = cx - (dpx - px) * PBMap\TileSize Else - ;Debug "c0" + Debug "c0" *Pixel\x = cx + (px - dpx) * PBMap\TileSize EndIf *Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\Position\y) * PBMap\TileSize @@ -1092,6 +1089,7 @@ Module PBMap ;Convert X, Y in tile.decimal into real pixels MouseX = PBMap\Drawing\Position\x * PBMap\TileSize + GadgetWidth(PBMap\Gadget) / 2 - x MouseY = PBMap\Drawing\Position\y * PBMap\TileSize + GadgetHeight(PBMap\Gadget) / 2 - y + Debug "------" ;TODO bug when zoom near the wrap Debug PBMap\Position\x ;Cross-multiply to get the new center PBMap\Position\x = (OldPx * MouseX) / OldMx @@ -1348,7 +1346,7 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::SetCallBackMainPointer(@MainPointer()) ;To change the Main Pointer PBMap::SetCallBackLocation(@UpdateLocation()) PBMap::SetLocation(-36.81148, 175.08634,12) -; PBMap::SetLocation(0, 0) + ;PBMap::SetLocation(0, 0) PBMap::SetMapServer("http://t1.openseamap.org/seamark/") ;add a special osm overlay map PBMAP::SetMapScaleUnit(PBMAP::#SCALE_NAUTICAL) PBMap::AddMarker(49.0446828398, 2.0349812508, -1, @MyMarker()) @@ -1388,8 +1386,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 413 -; FirstLine = 467 +; CursorPosition = 1091 +; FirstLine = 1070 ; Folding = ---------- ; EnableThread ; EnableXP From 4e48cda38a4d3e55eeade13e4bc063eef3160875 Mon Sep 17 00:00:00 2001 From: djes Date: Fri, 2 Sep 2016 15:42:38 +0200 Subject: [PATCH 39/49] bug hunt --- PBMap.pb | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index c6cdef0..6fafee5 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -421,16 +421,16 @@ Module PBMap Protected px.d = Pos\x ;check the x boundaries of the map to adjust the position (coz of the longitude wrapping) If dpx - px > tilemax / 2 - Debug "c1" + ;Debug "c1" *Pixel\x = cx + (px - dpx + tilemax) * PBMap\TileSize ElseIf px - dpx > tilemax / 2 - Debug "c2" + ;Debug "c2" *Pixel\x = cx + (px - dpx - tilemax) * PBMap\TileSize ElseIf px - dpx < 0 - Debug "c3" + ;Debug "c3" *Pixel\x = cx - (dpx - px) * PBMap\TileSize Else - Debug "c0" + ;Debug "c0" *Pixel\x = cx + (px - dpx) * PBMap\TileSize EndIf *Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\Position\y) * PBMap\TileSize @@ -756,7 +756,7 @@ Module PBMap MovePathCursor(x,y) DrawVectorText(StrD(Scale,3)+sunit) MovePathCursor(x,y+12) - AddPathLine(x+128,y+10) + AddPathLine(x+128,y+12) StrokePath(1) EndProcedure @@ -768,10 +768,10 @@ Module PBMap ty = Int(*Drawing\Position\y) nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point ny = *Drawing\CenterY / PBMap\TileSize - *Drawing\Bounds\NorthWest\x = tx-nx-1 - *Drawing\Bounds\NorthWest\y = ty-ny-1 - *Drawing\Bounds\SouthEast\x = tx+nx+1 - *Drawing\Bounds\SouthEast\y = ty+ny+1 + *Drawing\Bounds\NorthWest\x = tx-nx-1 + *Drawing\Bounds\NorthWest\y = ty-ny-1 + *Drawing\Bounds\SouthEast\x = tx+nx+2 + *Drawing\Bounds\SouthEast\y = ty+ny+2 ; Debug "------------------" XY2LatLon(*Drawing\Bounds\NorthWest, @Degrees1) XY2LatLon(*Drawing\Bounds\SouthEast, @Degrees2) @@ -922,7 +922,7 @@ Module PBMap ;-*** Main drawing Procedure Drawing() Protected *Drawing.DrawingParameters = @PBMap\Drawing - Protected Px.d, Py.d,a + Protected Px.d, Py.d,a, ts = PBMap\TileSize PBMap\Dirty = #False PBMap\Redraw = #False ;Precalc some values @@ -930,8 +930,8 @@ Module PBMap *Drawing\CenterY = GadgetHeight(PBMap\Gadget) / 2 ;Pixel shift, aka position in the tile Px = *Drawing\Position\x : Py = *Drawing\Position\y - *Drawing\DeltaX = Px * PBMap\TileSize - (Int(Px) * PBMap\TileSize) ;Don't forget the Int() ! - *Drawing\DeltaY = Py * PBMap\TileSize - (Int(Py) * PBMap\TileSize) + *Drawing\DeltaX = Px * ts - (Int(Px) * ts) ;Don't forget the Int() ! + *Drawing\DeltaY = Py * ts - (Int(Py) * ts) *Drawing\TargetLocation\Latitude = PBMap\TargetLocation\Latitude *Drawing\TargetLocation\Longitude = PBMap\TargetLocation\Longitude ;Main drawing stuff @@ -1386,8 +1386,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1091 -; FirstLine = 1070 +; CursorPosition = 427 +; FirstLine = 410 ; Folding = ---------- ; EnableThread ; EnableXP From 3a889a9bdd255ce54e5fbca3249113e91acd9543 Mon Sep 17 00:00:00 2001 From: djes Date: Fri, 2 Sep 2016 17:23:11 +0200 Subject: [PATCH 40/49] wip --- PBMap.pb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 6fafee5..6d1960f 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -1047,7 +1047,7 @@ Module PBMap EndSelect If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf - LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing) + LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing\Position) ;Convert X, Y in tile.decimal into real pixels PBMap\Position\X = PBMap\Drawing\Position\x * PBMap\TileSize PBMap\Position\Y = PBMap\Drawing\Position\y * PBMap\TileSize @@ -1077,6 +1077,7 @@ Module PBMap Procedure SetZoomOnPosition(x, y, zoom) Protected MouseX.d, MouseY.d Protected OldPx.d, OldPy.d, OldMx.d, OldMy.d + Protected MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize ;Fast and dirty code OldPx = PBMap\Position\x : OldPy = PBMap\Position\y OldMx = OldPx + GadgetWidth(PBMap\Gadget) / 2 - x @@ -1085,11 +1086,15 @@ Module PBMap If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf ;Centered Zoom - LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing) + LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing\Position) ;Convert X, Y in tile.decimal into real pixels MouseX = PBMap\Drawing\Position\x * PBMap\TileSize + GadgetWidth(PBMap\Gadget) / 2 - x - MouseY = PBMap\Drawing\Position\y * PBMap\TileSize + GadgetHeight(PBMap\Gadget) / 2 - y + MouseY = PBMap\Drawing\Position\y * PBMap\TileSize + GadgetHeight(PBMap\Gadget) / 2 - y + If MouseX > MapWidth + Debug "kaboum" + EndIf Debug "------" ;TODO bug when zoom near the wrap + Debug "Mx : " +StrD(MouseX) Debug PBMap\Position\x ;Cross-multiply to get the new center PBMap\Position\x = (OldPx * MouseX) / OldMx @@ -1386,8 +1391,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 427 -; FirstLine = 410 +; CursorPosition = 1094 +; FirstLine = 1076 ; Folding = ---------- ; EnableThread ; EnableXP From f5a382ff59eb8fde296f22a682e0c8c8f0f15156 Mon Sep 17 00:00:00 2001 From: djes Date: Mon, 5 Sep 2016 16:42:00 +0200 Subject: [PATCH 41/49] wip --- PBMap.pb | 209 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 115 insertions(+), 94 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 6d1960f..e511b9d 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -60,12 +60,12 @@ Module PBMap EnableExplicit - Structure Location + Structure GeographicCoordinates Longitude.d Latitude.d EndStructure - Structure Position + Structure Coordinates x.d y.d EndStructure @@ -77,7 +77,7 @@ Module PBMap ;- Tile Structure Structure Tile - Position.Position + Position.Coordinates PBMapTileX.i PBMapTileY.i PBMapZoom.i @@ -90,18 +90,18 @@ Module PBMap EndStructure Structure TileBounds - NorthWest.Position - SouthEast.Position + NorthWest.Coordinates + SouthEast.Coordinates EndStructure Structure DrawingParameters - Position.Position + TilePosition.Coordinates Bounds.TileBounds Canvas.i PBMapTileX.i PBMapTileY.i PBMapZoom.i - TargetLocation.Location + TargetLocation.GeographicCoordinates CenterX.i CenterY.i DeltaX.i @@ -128,7 +128,7 @@ Module PBMap EndStructure Structure Marker - Location.Location ; Marker latitude and longitude + Location.GeographicCoordinates ; Marker latitude and longitude color.l ; Marker color CallBackPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) EndStructure @@ -144,13 +144,13 @@ Module PBMap Gadget.i ; Canvas Gadget Id Font.i ; Font to uses when write on the map Timer.i - TargetLocation.Location ; Latitude and Longitude from focus point + TargetLocation.GeographicCoordinates ; Latitude and Longitude from focus point Drawing.DrawingParameters ; Drawing parameters based on focus point ; CallBackLocation.i ; @Procedure(latitude.d,lontitude.d) CallBackMainPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) ; - Position.PixelPosition ; Actual focus point coords in pixels (global) + PixelPosition.PixelPosition ; Actual focus point coords in pixels (global) MoveStartingPoint.PixelPosition ; Start mouse position coords when dragging the map ; Array ServerURL.s(0) ; Web URL ex: http://tile.openstreetmap.org/ @@ -170,7 +170,7 @@ Module PBMap ; MainDrawingThread.i TileThreadMutex.i; ;Mutex to protect resources - List track.Location() ; To display a GPX track + List track.GeographicCoordinates() ; To display a GPX track List Marker.Marker() ; To diplay marker EditMarkerIndex.l @@ -388,8 +388,8 @@ Module PBMap ;*** Converts coords to tile.decimal ;Warning, structures used in parameters are not tested - Procedure LatLon2XY(*Location.Location, *Coords.Position) - Protected n.d = Pow(2.0, PBMap\Zoom) + Procedure LatLon2XY(*Location.GeographicCoordinates, *Coords.Coordinates, Zoom) + Protected n.d = Pow(2.0, Zoom) Protected LatRad.d = Radian(*Location\Latitude) *Coords\x = n * (Mod( *Location\Longitude + 180.0, 360) / 360.0 ) *Coords\y = n * ( 1.0 - Log(Tan(LatRad) + (1.0/Cos(LatRad))) / #PI ) / 2.0 @@ -399,8 +399,8 @@ Module PBMap ;*** Converts tile.decimal to coords ;Warning, structures used in parameters are not tested - Procedure XY2LatLon(*Coords.Position, *Location.Location) - Protected n.d = Pow(2.0, PBMap\Zoom) + Procedure XY2LatLon(*Coords.Coordinates, *Location.GeographicCoordinates, Zoom) + Protected n.d = Pow(2.0, Zoom) Protected LatitudeRad.d ;Ensures the longitude to be in the range [-180;180[ *Location\Longitude = Mod(Mod(*Coords\x / n * 360.0, 360.0) + 360.0, 360.0) - 180 @@ -413,11 +413,11 @@ Module PBMap *Location\Latitude = Degree(LatitudeRad) EndProcedure - Procedure LatLon2Pixel(*Location.Location, *Pixel.PixelPosition) - Protected Pos.Position - Protected tilemax = Pow(2.0, PBMap\Zoom) - Protected cx.d = PBMap\Drawing\CenterX, dpx.d = PBMap\Drawing\Position\x - LatLon2XY(*Location, @Pos) + Procedure LatLon2Pixel(*Location.GeographicCoordinates, *Pixel.PixelPosition, Zoom) + Protected Pos.Coordinates + Protected tilemax = Pow(2.0, Zoom) + Protected cx.d = PBMap\Drawing\CenterX, dpx.d = PBMap\Drawing\TilePosition\x + LatLon2XY(*Location, @Pos, Zoom) Protected px.d = Pos\x ;check the x boundaries of the map to adjust the position (coz of the longitude wrapping) If dpx - px > tilemax / 2 @@ -433,14 +433,14 @@ Module PBMap ;Debug "c0" *Pixel\x = cx + (px - dpx) * PBMap\TileSize EndIf - *Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\Position\y) * PBMap\TileSize + *Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\TilePosition\y) * PBMap\TileSize ;Debug "Longitude : " + StrD(*Location\Longitude) + " ; Pos : " + StrD(Pos\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\Position\x) + "/" + Str(tilemax) + ; " ; XY : " + Str(*Pixel\x) + "," + Str(*Pixel\y) EndProcedure ; HaversineAlgorithm ; http://andrew.hedges.name/experiments/haversine/ - Procedure.d HaversineInKM(*posA.Location, *posB.Location) + Procedure.d HaversineInKM(*posA.GeographicCoordinates, *posB.GeographicCoordinates) Protected eQuatorialEarthRadius.d = 6378.1370;6372.795477598; Protected dlong.d = (*posB\Longitude - *posA\Longitude); Protected dlat.d = (*posB\Latitude - *posA\Latitude) ; @@ -452,13 +452,13 @@ Module PBMap ProcedureReturn distance ; EndProcedure - Procedure.d HaversineInM(*posA.Location, *posB.Location) + Procedure.d HaversineInM(*posA.GeographicCoordinates, *posB.GeographicCoordinates) ProcedureReturn (1000 * HaversineInKM(@*posA,@*posB)); EndProcedure - Procedure GetPixelCoordFromLocation(*Location.Location, *Pixel.PixelPosition) ; TODO to Optimize - Protected mapWidth.l = Pow(2, PBMap\Zoom + 8) - Protected mapHeight.l = Pow(2, PBMap\Zoom + 8) + Procedure GetPixelCoordFromLocation(*Location.GeographicCoordinates, *Pixel.PixelPosition, Zoom) ; TODO to Optimize + Protected mapWidth.l = Pow(2, Zoom + 8) + Protected mapHeight.l = Pow(2, Zoom + 8) Protected x1.l,y1.l ; get x value x1 = (*Location\Longitude+180)*(mapWidth/360) @@ -654,8 +654,8 @@ Module PBMap Procedure DrawTiles(*Drawing.DrawingParameters, Layer.i, alpha.i=255) ;DisableDebugger Protected x.i, y.i,kq.q - Protected tx = Int(*Drawing\Position\x) ;Don't forget the Int() ! - Protected ty = Int(*Drawing\Position\y) + Protected tx = Int(*Drawing\TilePosition\x) ;Don't forget the Int() ! + Protected ty = Int(*Drawing\TilePosition\y) Protected nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point Protected ny = *Drawing\CenterY / PBMap\TileSize Protected px, py, img, tilex,tiley, key.s, CacheFile.s @@ -760,12 +760,12 @@ Module PBMap StrokePath(1) EndProcedure - Procedure DrawDegrees(*Drawing.DrawingParameters,alpha=192) + Procedure DrawDegrees(*Drawing.DrawingParameters, alpha=192) Protected tx, ty, nx,ny,nx1,ny1,x,y,n,cx,dperpixel.d - Protected pos1.PixelPosition,pos2.PixelPosition,Degrees1.Location,degrees2.Location + Protected pos1.PixelPosition,pos2.PixelPosition,Degrees1.GeographicCoordinates,degrees2.GeographicCoordinates Protected realx - tx = Int(*Drawing\Position\x) - ty = Int(*Drawing\Position\y) + tx = Int(*Drawing\TilePosition\x) + ty = Int(*Drawing\TilePosition\y) nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point ny = *Drawing\CenterY / PBMap\TileSize *Drawing\Bounds\NorthWest\x = tx-nx-1 @@ -773,8 +773,8 @@ Module PBMap *Drawing\Bounds\SouthEast\x = tx+nx+2 *Drawing\Bounds\SouthEast\y = ty+ny+2 ; Debug "------------------" - XY2LatLon(*Drawing\Bounds\NorthWest, @Degrees1) - XY2LatLon(*Drawing\Bounds\SouthEast, @Degrees2) + XY2LatLon(*Drawing\Bounds\NorthWest, @Degrees1, PBMap\Zoom) + XY2LatLon(*Drawing\Bounds\SouthEast, @Degrees2, PBMap\Zoom) ;ensure we stay positive for the drawing nx = Mod(Mod(Round(Degrees1\Longitude, #PB_Round_Down)-1, 360) + 360, 360) ny = Round(Degrees1\Latitude, #PB_Round_Up) +1 @@ -785,15 +785,15 @@ Module PBMap Degrees2\Longitude = nx1 Degrees2\Latitude = ny1 ; Debug "NW : " + StrD(Degrees1\Longitude) + " ; NE : " + StrD(Degrees2\Longitude) - LatLon2Pixel(@Degrees1, @pos1) - LatLon2Pixel(@Degrees2, @pos2) + LatLon2Pixel(@Degrees1, @pos1, PBMap\Zoom) + LatLon2Pixel(@Degrees2, @pos2, PBMap\Zoom) VectorFont(FontID(PBMap\Font), 10) VectorSourceColor(RGBA(0, 0, 0, alpha)) ;draw latitudes For y = ny1 To ny Degrees1\Longitude = nx Degrees1\Latitude = y - LatLon2Pixel(@Degrees1, @pos1) + LatLon2Pixel(@Degrees1, @pos1, PBMap\Zoom) MovePathCursor(pos1\x, pos1\y) AddPathLine( pos2\x, pos1\y) MovePathCursor(10, pos1\y) @@ -804,7 +804,7 @@ Module PBMap Repeat Degrees1\Longitude = x Degrees1\Latitude = ny - LatLon2Pixel(@Degrees1, @pos1) + LatLon2Pixel(@Degrees1, @pos1, PBMap\Zoom) MovePathCursor(pos1\x, pos1\y) AddPathLine( pos1\x, pos2\y) MovePathCursor(pos1\x,10) @@ -835,14 +835,14 @@ Module PBMap Procedure DrawTrack(*Drawing.DrawingParameters) Protected Pixel.PixelPosition - Protected Location.Location + Protected Location.GeographicCoordinates Protected km.f, memKm.i If ListSize(PBMap\track())>0 ;Trace Track ForEach PBMap\track() If *Drawing\TargetLocation\Latitude<>0 And *Drawing\TargetLocation\Longitude<>0 ;GetPixelCoordFromLocation(@PBMap\track(),@Pixel) - LatLon2Pixel(@PBMap\track(),@Pixel) + LatLon2Pixel(@PBMap\track(), @Pixel, PBMap\Zoom) If ListIndex(PBMap\track())=0 MovePathCursor(Pixel\X, Pixel\Y) Else @@ -864,7 +864,7 @@ Module PBMap Location\Longitude=PBMap\track()\Longitude EndIf ;GetPixelCoordFromLocation(@PBMap\track(),@Pixel) - LatLon2Pixel(@PBMap\track(),@Pixel) + LatLon2Pixel(@PBMap\track(),@Pixel, PBMap\Zoom) If Int(km)<>memKm memKm=Int(km) If PBMap\Zoom>10 @@ -907,7 +907,7 @@ Module PBMap ForEach PBMap\Marker() If PBMap\Marker()\Location\Latitude <> 0 And PBMap\Marker()\Location\Longitude <> 0 ;GetPixelCoordFromLocation(PBMap\Marker()\Location, @Pixel) - LatLon2Pixel(PBMap\Marker()\Location, @Pixel) + LatLon2Pixel(PBMap\Marker()\Location, @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 PBMap\Marker()\CallBackPointer > 0 CallFunctionFast(PBMap\Marker()\CallBackPointer, Pixel\X, Pixel\Y) @@ -929,7 +929,7 @@ Module PBMap *Drawing\CenterX = GadgetWidth(PBMap\Gadget) / 2 *Drawing\CenterY = GadgetHeight(PBMap\Gadget) / 2 ;Pixel shift, aka position in the tile - Px = *Drawing\Position\x : Py = *Drawing\Position\y + Px = *Drawing\TilePosition\x : Py = *Drawing\TilePosition\y *Drawing\DeltaX = Px * ts - (Int(Px) * ts) ;Don't forget the Int() ! *Drawing\DeltaY = Py * ts - (Int(Py) * ts) *Drawing\TargetLocation\Latitude = PBMap\TargetLocation\Latitude @@ -971,6 +971,19 @@ Module PBMap ;Drawing() EndProcedure + Procedure.d MouseLongitude() + Protected MouseX.d = (PBMap\PixelPosition\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX)) / PBMap\TileSize + Protected n.d = Pow(2.0, PBMap\Zoom) + ;double mod is to ensure the longitude to be in the range [-180;180[ + ProcedureReturn Mod(Mod(MouseX / n * 360.0, 360.0) + 360.0, 360.0) - 180 + EndProcedure + + Procedure.d MouseLatitude() + Protected MouseY.d = (PBMap\PixelPosition\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY)) / PBMap\TileSize + Protected n.d = Pow(2.0, PBMap\Zoom) + ProcedureReturn Degree(ATan(SinH(#PI * (1.0 - 2.0 * MouseY / n)))) + EndProcedure + Procedure SetLocation(latitude.d, longitude.d, zoom = 15, Mode.i = #PB_Absolute) Select Mode Case #PB_Absolute @@ -984,10 +997,10 @@ Module PBMap EndSelect If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf - LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing) + LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing, PBMap\Zoom) ;Convert X, Y in tile.decimal into real pixels - PBMap\Position\x = PBMap\Drawing\Position\x * PBMap\TileSize - PBMap\Position\y = PBMap\Drawing\Position\y * PBMap\TileSize + PBMap\PixelPosition\x = PBMap\Drawing\TilePosition\x * PBMap\TileSize + PBMap\PixelPosition\y = PBMap\Drawing\TilePosition\y * PBMap\TileSize PBMap\Drawing\PassNb = 1 PBMap\Redraw = #True ;Drawing() @@ -1047,10 +1060,10 @@ Module PBMap EndSelect If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf - LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing\Position) + LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing\TilePosition, PBMap\Zoom) ;Convert X, Y in tile.decimal into real pixels - PBMap\Position\X = PBMap\Drawing\Position\x * PBMap\TileSize - PBMap\Position\Y = PBMap\Drawing\Position\y * PBMap\TileSize + PBMap\PixelPosition\X = PBMap\Drawing\TilePosition\x * PBMap\TileSize + PBMap\PixelPosition\Y = PBMap\Drawing\TilePosition\y * PBMap\TileSize ;First drawing PBMap\Redraw = #True ;Drawing() @@ -1075,35 +1088,46 @@ Module PBMap ;Zoom on x, y position relative to the canvas gadget Procedure SetZoomOnPosition(x, y, zoom) + Debug "----" + Debug MouseLatitude() + Debug MouseLongitude() + Protected MouseX.d, MouseY.d - Protected OldPx.d, OldPy.d, OldMx.d, OldMy.d - Protected MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize + Protected OldPx.d, OldPy.d, OldMx.d, OldMy.d, Px.d, Py.d + Protected CenterX = GadgetWidth(PBMap\Gadget) / 2, CenterY = GadgetHeight(PBMap\Gadget) / 2 + ;Fast and dirty code - OldPx = PBMap\Position\x : OldPy = PBMap\Position\y - OldMx = OldPx + GadgetWidth(PBMap\Gadget) / 2 - x - OldMy = OldPy + GadgetHeight(PBMap\Gadget) / 2 - y + OldPx = PBMap\PixelPosition\x : OldPy = PBMap\PixelPosition\y + OldMx = OldPx + CenterX - x + OldMy = OldPy + CenterY - y PBMap\Zoom = PBMap\Zoom + zoom If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf + Protected MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize + ;Centered Zoom - LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing\Position) + LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing\TilePosition, PBMap\Zoom) ;Convert X, Y in tile.decimal into real pixels - MouseX = PBMap\Drawing\Position\x * PBMap\TileSize + GadgetWidth(PBMap\Gadget) / 2 - x - MouseY = PBMap\Drawing\Position\y * PBMap\TileSize + GadgetHeight(PBMap\Gadget) / 2 - y - If MouseX > MapWidth - Debug "kaboum" - EndIf - Debug "------" ;TODO bug when zoom near the wrap - Debug "Mx : " +StrD(MouseX) - Debug PBMap\Position\x - ;Cross-multiply to get the new center - PBMap\Position\x = (OldPx * MouseX) / OldMx - PBMap\Position\y = (OldPy * MouseY) / OldMy - ;PBMap tile position in tile.decimal - PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize - PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize - PBMap\Drawing\PassNb = 1 - XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) +; Px = PBMap\Drawing\TilePosition\x * PBMap\TileSize +; Py = PBMap\Drawing\TilePosition\y * PBMap\TileSize +; MouseX = Px + CenterX - x +; MouseY = Py + CenterY - y +; +; If MouseX > MapWidth +; Debug "kaboum" +; EndIf +; ; Debug "------" ;TODO bug when zoom near the wrap +; ; Debug "Mx : " +StrD(MouseX) +; ; Debug PBMap\TilePosition\x +; ;Cross-multiply to get the new center +; PBMap\PixelPosition\x = (OldPx * MouseX) / OldMx +; PBMap\PixelPosition\y = (OldPy * MouseY) / OldMy +; ;PBMap tile position in tile.decimal +; PBMap\Drawing\TilePosition\x = PBMap\PixelPosition\x / PBMap\TileSize +; PBMap\Drawing\TilePosition\y = PBMap\PixelPosition\y / PBMap\TileSize +; PBMap\Drawing\PassNb = 1 + XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation, PBMap\Zoom) + LatLon2Pixel(@PBMap\TargetLocation, @PBMap\PixelPosition, PBMap\Zoom) ;Start drawing PBMap\Redraw = #True ;Drawing() @@ -1131,7 +1155,7 @@ Module PBMap Procedure CanvasEvents() Protected MouseX.i, MouseY.i - Protected Marker.Position, *Tile.Tile, MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize + Protected Marker.Coordinates, *Tile.Tile, MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize PBMap\Moving = #False Select EventType() Case #PB_EventType_MouseWheel @@ -1141,18 +1165,18 @@ Module PBMap Else ;Absolute zoom (centered on the center of the map) SetZoom(GetGadgetAttribute(PBMap\Gadget,#PB_Canvas_WheelDelta), #PB_Relative) - EndIf + EndIf Case #PB_EventType_LeftButtonDown ;Check if we select a marker - MouseX = PBMap\Position\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) - MouseY = PBMap\Position\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) + MouseX = PBMap\PixelPosition\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) + MouseY = PBMap\PixelPosition\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) ;Clip MouseX to the map range (in X, the map is infinite) MouseX = Mod(Mod(MouseX, MapWidth) + MapWidth, MapWidth) ForEach PBMap\Marker() - LatLon2XY(@PBMap\Marker()\Location, @Marker) + LatLon2XY(@PBMap\Marker()\Location, @Marker, PBMap\Zoom) Marker\x * PBMap\TileSize Marker\y * PBMap\TileSize -; Debug "Pos : " + StrD(Marker\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\Position\x) + ;Debug "Pos : " + StrD(Marker\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\TilePosition\x) If Distance(Marker\x, Marker\y, MouseX, MouseY) < 8 PBMap\EditMarkerIndex = ListIndex(PBMap\Marker()) Break @@ -1169,25 +1193,22 @@ Module PBMap ;Move marker If PBMap\EditMarkerIndex > -1 SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) - LatLon2XY(@PBMap\Marker()\Location, @Marker) + LatLon2XY(@PBMap\Marker()\Location, @Marker, PBMap\Zoom) Marker\x + MouseX / PBMap\TileSize Marker\y + MouseY / PBMap\TileSize - XY2LatLon(@Marker, @PBMap\Marker()\Location) + XY2LatLon(@Marker, @PBMap\Marker()\Location, PBMap\Zoom) Else ;New move values ;PBMap\Position\x - MouseX ;Ensures that pixel position stay in the range [0..2^Zoom*PBMap\TileSize[ coz of the wrapping of the map - PBMap\Position\x = PBMap\Position\x - MouseX - If PBMap\Position\x < 0 - PBMap\Position\x + Pow(2, PBMap\Zoom) * PBMap\TileSize - EndIf - PBMap\Position\x = Mod(PBMap\Position\x, Pow(2, PBMap\Zoom) * PBMap\TileSize) - PBMap\Position\y - MouseY + PBMap\PixelPosition\x - MouseX + PBMap\PixelPosition\x = Mod(Mod(PBMap\PixelPosition\x, MapWidth) + MapWidth, MapWidth) + PBMap\PixelPosition\y - MouseY ;PBMap tile position in tile.decimal - PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize - PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize + PBMap\Drawing\TilePosition\x = PBMap\PixelPosition\x / PBMap\TileSize + PBMap\Drawing\TilePosition\y = PBMap\PixelPosition\y / PBMap\TileSize PBMap\Drawing\PassNb = 1 - XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) + XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation, PBMap\Zoom) ;If CallBackLocation send Location to function If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) @@ -1202,10 +1223,10 @@ Module PBMap If PBMap\EditMarkerIndex > -1 PBMap\EditMarkerIndex = -1 Else ;Move Map - PBMap\Drawing\Position\x = PBMap\Position\x / PBMap\TileSize - PBMap\Drawing\Position\y = PBMap\Position\y / PBMap\TileSize - MyDebug("PBMap\Drawing\Position\x " + Str(PBMap\Drawing\Position\x) + " ; PBMap\Drawing\Position\y " + Str(PBMap\Drawing\Position\y) ) - XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation) + PBMap\Drawing\TilePosition\x = PBMap\PixelPosition\x / PBMap\TileSize + PBMap\Drawing\TilePosition\y = PBMap\PixelPosition\y / PBMap\TileSize + MyDebug("PBMap\Drawing\TilePosition\x " + Str(PBMap\Drawing\TilePosition\x) + " ; PBMap\Drawing\TilePosition\y " + Str(PBMap\Drawing\TilePosition\y) ) + XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation, PBMap\Zoom) PBMap\Redraw = #True EndIf Case #PB_MAP_REDRAW @@ -1391,8 +1412,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1094 -; FirstLine = 1076 +; CursorPosition = 343 +; FirstLine = 314 ; Folding = ---------- ; EnableThread ; EnableXP From 17f999f11c58fd6754c15314bcf917d8dff474bd Mon Sep 17 00:00:00 2001 From: djes Date: Mon, 5 Sep 2016 17:06:54 +0200 Subject: [PATCH 42/49] wip --- PBMap.pb | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index e511b9d..8e9a313 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -388,7 +388,7 @@ Module PBMap ;*** Converts coords to tile.decimal ;Warning, structures used in parameters are not tested - Procedure LatLon2XY(*Location.GeographicCoordinates, *Coords.Coordinates, Zoom) + Procedure LatLon2TileXY(*Location.GeographicCoordinates, *Coords.Coordinates, Zoom) Protected n.d = Pow(2.0, Zoom) Protected LatRad.d = Radian(*Location\Latitude) *Coords\x = n * (Mod( *Location\Longitude + 180.0, 360) / 360.0 ) @@ -399,7 +399,7 @@ Module PBMap ;*** Converts tile.decimal to coords ;Warning, structures used in parameters are not tested - Procedure XY2LatLon(*Coords.Coordinates, *Location.GeographicCoordinates, Zoom) + Procedure TileXY2LatLon(*Coords.Coordinates, *Location.GeographicCoordinates, Zoom) Protected n.d = Pow(2.0, Zoom) Protected LatitudeRad.d ;Ensures the longitude to be in the range [-180;180[ @@ -417,7 +417,7 @@ Module PBMap Protected Pos.Coordinates Protected tilemax = Pow(2.0, Zoom) Protected cx.d = PBMap\Drawing\CenterX, dpx.d = PBMap\Drawing\TilePosition\x - LatLon2XY(*Location, @Pos, Zoom) + LatLon2TileXY(*Location, @Pos, Zoom) Protected px.d = Pos\x ;check the x boundaries of the map to adjust the position (coz of the longitude wrapping) If dpx - px > tilemax / 2 @@ -773,8 +773,8 @@ Module PBMap *Drawing\Bounds\SouthEast\x = tx+nx+2 *Drawing\Bounds\SouthEast\y = ty+ny+2 ; Debug "------------------" - XY2LatLon(*Drawing\Bounds\NorthWest, @Degrees1, PBMap\Zoom) - XY2LatLon(*Drawing\Bounds\SouthEast, @Degrees2, PBMap\Zoom) + TileXY2LatLon(*Drawing\Bounds\NorthWest, @Degrees1, PBMap\Zoom) + TileXY2LatLon(*Drawing\Bounds\SouthEast, @Degrees2, PBMap\Zoom) ;ensure we stay positive for the drawing nx = Mod(Mod(Round(Degrees1\Longitude, #PB_Round_Down)-1, 360) + 360, 360) ny = Round(Degrees1\Latitude, #PB_Round_Up) +1 @@ -971,6 +971,19 @@ Module PBMap ;Drawing() EndProcedure + Procedure.d Longitude(x) + Protected NewX.d = (PBMap\PixelPosition\x - GadgetWidth(PBMap\Gadget) / 2 + x) / PBMap\TileSize + Protected n.d = Pow(2.0, PBMap\Zoom) + ;double mod is to ensure the longitude to be in the range [-180;180[ + ProcedureReturn Mod(Mod(NewX / n * 360.0, 360.0) + 360.0, 360.0) - 180 + EndProcedure + + Procedure.d Latitude(y) + Protected NewY.d = (PBMap\PixelPosition\y - GadgetHeight(PBMap\Gadget) / 2 + y) / PBMap\TileSize + Protected n.d = Pow(2.0, PBMap\Zoom) + ProcedureReturn Degree(ATan(SinH(#PI * (1.0 - 2.0 * NewY / n)))) + EndProcedure + Procedure.d MouseLongitude() Protected MouseX.d = (PBMap\PixelPosition\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX)) / PBMap\TileSize Protected n.d = Pow(2.0, PBMap\Zoom) @@ -997,7 +1010,7 @@ Module PBMap EndSelect If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf - LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing, PBMap\Zoom) + LatLon2TileXY(@PBMap\TargetLocation, @PBMap\Drawing, PBMap\Zoom) ;Convert X, Y in tile.decimal into real pixels PBMap\PixelPosition\x = PBMap\Drawing\TilePosition\x * PBMap\TileSize PBMap\PixelPosition\y = PBMap\Drawing\TilePosition\y * PBMap\TileSize @@ -1060,7 +1073,7 @@ Module PBMap EndSelect If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf - LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing\TilePosition, PBMap\Zoom) + LatLon2TileXY(@PBMap\TargetLocation, @PBMap\Drawing\TilePosition, PBMap\Zoom) ;Convert X, Y in tile.decimal into real pixels PBMap\PixelPosition\X = PBMap\Drawing\TilePosition\x * PBMap\TileSize PBMap\PixelPosition\Y = PBMap\Drawing\TilePosition\y * PBMap\TileSize @@ -1089,8 +1102,8 @@ Module PBMap ;Zoom on x, y position relative to the canvas gadget Procedure SetZoomOnPosition(x, y, zoom) Debug "----" - Debug MouseLatitude() - Debug MouseLongitude() + Debug Latitude(x) + Debug Longitude(y) Protected MouseX.d, MouseY.d Protected OldPx.d, OldPy.d, OldMx.d, OldMy.d, Px.d, Py.d @@ -1106,7 +1119,7 @@ Module PBMap Protected MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize ;Centered Zoom - LatLon2XY(@PBMap\TargetLocation, @PBMap\Drawing\TilePosition, PBMap\Zoom) + LatLon2TileXY(@PBMap\TargetLocation, @PBMap\Drawing\TilePosition, PBMap\Zoom) ;Convert X, Y in tile.decimal into real pixels ; Px = PBMap\Drawing\TilePosition\x * PBMap\TileSize ; Py = PBMap\Drawing\TilePosition\y * PBMap\TileSize @@ -1126,7 +1139,7 @@ Module PBMap ; PBMap\Drawing\TilePosition\x = PBMap\PixelPosition\x / PBMap\TileSize ; PBMap\Drawing\TilePosition\y = PBMap\PixelPosition\y / PBMap\TileSize ; PBMap\Drawing\PassNb = 1 - XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation, PBMap\Zoom) + ;TileXY2LatLon(@PBMap\Drawing\TilePosition, @PBMap\TargetLocation, PBMap\Zoom) LatLon2Pixel(@PBMap\TargetLocation, @PBMap\PixelPosition, PBMap\Zoom) ;Start drawing PBMap\Redraw = #True @@ -1173,7 +1186,7 @@ Module PBMap ;Clip MouseX to the map range (in X, the map is infinite) MouseX = Mod(Mod(MouseX, MapWidth) + MapWidth, MapWidth) ForEach PBMap\Marker() - LatLon2XY(@PBMap\Marker()\Location, @Marker, PBMap\Zoom) + LatLon2TileXY(@PBMap\Marker()\Location, @Marker, PBMap\Zoom) Marker\x * PBMap\TileSize Marker\y * PBMap\TileSize ;Debug "Pos : " + StrD(Marker\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\TilePosition\x) @@ -1193,10 +1206,10 @@ Module PBMap ;Move marker If PBMap\EditMarkerIndex > -1 SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) - LatLon2XY(@PBMap\Marker()\Location, @Marker, PBMap\Zoom) + LatLon2TileXY(@PBMap\Marker()\Location, @Marker, PBMap\Zoom) Marker\x + MouseX / PBMap\TileSize Marker\y + MouseY / PBMap\TileSize - XY2LatLon(@Marker, @PBMap\Marker()\Location, PBMap\Zoom) + TileXY2LatLon(@Marker, @PBMap\Marker()\Location, PBMap\Zoom) Else ;New move values ;PBMap\Position\x - MouseX @@ -1208,7 +1221,7 @@ Module PBMap PBMap\Drawing\TilePosition\x = PBMap\PixelPosition\x / PBMap\TileSize PBMap\Drawing\TilePosition\y = PBMap\PixelPosition\y / PBMap\TileSize PBMap\Drawing\PassNb = 1 - XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation, PBMap\Zoom) + TileXY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation, PBMap\Zoom) ;If CallBackLocation send Location to function If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) @@ -1226,7 +1239,7 @@ Module PBMap PBMap\Drawing\TilePosition\x = PBMap\PixelPosition\x / PBMap\TileSize PBMap\Drawing\TilePosition\y = PBMap\PixelPosition\y / PBMap\TileSize MyDebug("PBMap\Drawing\TilePosition\x " + Str(PBMap\Drawing\TilePosition\x) + " ; PBMap\Drawing\TilePosition\y " + Str(PBMap\Drawing\TilePosition\y) ) - XY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation, PBMap\Zoom) + TileXY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation, PBMap\Zoom) PBMap\Redraw = #True EndIf Case #PB_MAP_REDRAW @@ -1412,8 +1425,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 343 -; FirstLine = 314 +; CursorPosition = 430 +; FirstLine = 415 ; Folding = ---------- ; EnableThread ; EnableXP From b2b6a48d8908fce07ca3d1a82bb93fd61c8d7e35 Mon Sep 17 00:00:00 2001 From: djes Date: Tue, 6 Sep 2016 12:46:50 +0200 Subject: [PATCH 43/49] Cleanup --- PBMap.pb | 212 +++++++++++++++++++++++++++---------------------------- 1 file changed, 105 insertions(+), 107 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 8e9a313..40f910b 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -65,16 +65,16 @@ Module PBMap Latitude.d EndStructure + Structure PixelCoordinates + x.i + y.i + EndStructure + Structure Coordinates x.d y.d EndStructure - Structure PixelPosition - x.i - y.i - EndStructure - ;- Tile Structure Structure Tile Position.Coordinates @@ -95,13 +95,13 @@ Module PBMap EndStructure Structure DrawingParameters - TilePosition.Coordinates + TileCoordinates.Coordinates Bounds.TileBounds Canvas.i PBMapTileX.i PBMapTileY.i PBMapZoom.i - TargetLocation.GeographicCoordinates + GeographicCoordinates.GeographicCoordinates CenterX.i CenterY.i DeltaX.i @@ -128,7 +128,7 @@ Module PBMap EndStructure Structure Marker - Location.GeographicCoordinates ; Marker latitude and longitude + GeographicCoordinates.GeographicCoordinates ; Marker latitude and longitude color.l ; Marker color CallBackPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) EndStructure @@ -144,14 +144,14 @@ Module PBMap Gadget.i ; Canvas Gadget Id Font.i ; Font to uses when write on the map Timer.i - TargetLocation.GeographicCoordinates ; Latitude and Longitude from focus point + GeographicCoordinates.GeographicCoordinates ; Latitude and Longitude from focus point Drawing.DrawingParameters ; Drawing parameters based on focus point ; CallBackLocation.i ; @Procedure(latitude.d,lontitude.d) CallBackMainPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) ; - PixelPosition.PixelPosition ; Actual focus point coords in pixels (global) - MoveStartingPoint.PixelPosition ; Start mouse position coords when dragging the map + PixelCoordinates.PixelCoordinates ; Actual focus point coords in pixels (global) + MoveStartingPoint.PixelCoordinates ; Start mouse position coords when dragging the map ; Array ServerURL.s(0) ; Web URL ex: http://tile.openstreetmap.org/ NumberOfMapLayers.i ; The number of map tile layers; @@ -401,22 +401,22 @@ Module PBMap ;Warning, structures used in parameters are not tested Procedure TileXY2LatLon(*Coords.Coordinates, *Location.GeographicCoordinates, Zoom) Protected n.d = Pow(2.0, Zoom) - Protected LatitudeRad.d ;Ensures the longitude to be in the range [-180;180[ *Location\Longitude = Mod(Mod(*Coords\x / n * 360.0, 360.0) + 360.0, 360.0) - 180 -; *Location\Longitude = Mod(*Coords\x / n * 360.0, 360.0) -; If *Location\Longitude < 0 -; *Location\Longitude + 360 -; EndIf -; *Location\Longitude = Mod(*Location\Longitude, 360.0) - 180 - LatitudeRad = ATan(SinH(#PI * (1.0 - 2.0 * *Coords\y / n))) - *Location\Latitude = Degree(LatitudeRad) + *Location\Latitude = Degree(ATan(SinH(#PI * (1.0 - 2.0 * *Coords\y / n)))) EndProcedure - Procedure LatLon2Pixel(*Location.GeographicCoordinates, *Pixel.PixelPosition, Zoom) + Procedure Pixel2LatLon(*Coords.PixelCoordinates, *Location.GeographicCoordinates, Zoom) + Protected n.d = PBMap\TileSize * Pow(2.0, Zoom) + ;Ensures the longitude to be in the range [-180;180[ + *Location\Longitude = Mod(Mod(*Coords\x / n * 360.0, 360.0) + 360.0, 360.0) - 180 + *Location\Latitude = Degree(ATan(SinH(#PI * (1.0 - 2.0 * *Coords\y / n)))) + EndProcedure + + Procedure LatLon2Pixel(*Location.GeographicCoordinates, *Pixel.PixelCoordinates, Zoom) Protected Pos.Coordinates Protected tilemax = Pow(2.0, Zoom) - Protected cx.d = PBMap\Drawing\CenterX, dpx.d = PBMap\Drawing\TilePosition\x + Protected cx.d = PBMap\Drawing\CenterX, dpx.d = PBMap\Drawing\TileCoordinates\x LatLon2TileXY(*Location, @Pos, Zoom) Protected px.d = Pos\x ;check the x boundaries of the map to adjust the position (coz of the longitude wrapping) @@ -433,7 +433,7 @@ Module PBMap ;Debug "c0" *Pixel\x = cx + (px - dpx) * PBMap\TileSize EndIf - *Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\TilePosition\y) * PBMap\TileSize + *Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\TileCoordinates\y) * PBMap\TileSize ;Debug "Longitude : " + StrD(*Location\Longitude) + " ; Pos : " + StrD(Pos\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\Position\x) + "/" + Str(tilemax) + ; " ; XY : " + Str(*Pixel\x) + "," + Str(*Pixel\y) EndProcedure @@ -456,7 +456,7 @@ Module PBMap ProcedureReturn (1000 * HaversineInKM(@*posA,@*posB)); EndProcedure - Procedure GetPixelCoordFromLocation(*Location.GeographicCoordinates, *Pixel.PixelPosition, Zoom) ; TODO to Optimize + Procedure GetPixelCoordFromLocation(*Location.GeographicCoordinates, *Pixel.PixelCoordinates, Zoom) ; TODO to Optimize Protected mapWidth.l = Pow(2, Zoom + 8) Protected mapHeight.l = Pow(2, Zoom + 8) Protected x1.l,y1.l @@ -471,13 +471,13 @@ Module PBMap ; Debug y1 Protected x2.l, y2.l ; get x value - x2 = (PBMap\TargetLocation\Longitude+180)*(mapWidth/360) + x2 = (PBMap\GeographicCoordinates\Longitude+180)*(mapWidth/360) ; convert from degrees To radians - latRad = PBMap\TargetLocation\Latitude*#PI/180; + latRad = PBMap\GeographicCoordinates\Latitude*#PI/180; ; get y value mercN = Log(Tan((#PI/4)+(latRad/2))) y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)); -; Debug "targetlocation" +; Debug "GeographicCoordinates" ; Debug x1 ; Debug y1 *Pixel\x=GadgetWidth(PBMap\Gadget)/2 - (x2-x1) @@ -654,8 +654,8 @@ Module PBMap Procedure DrawTiles(*Drawing.DrawingParameters, Layer.i, alpha.i=255) ;DisableDebugger Protected x.i, y.i,kq.q - Protected tx = Int(*Drawing\TilePosition\x) ;Don't forget the Int() ! - Protected ty = Int(*Drawing\TilePosition\y) + Protected tx = Int(*Drawing\TileCoordinates\x) ;Don't forget the Int() ! + Protected ty = Int(*Drawing\TileCoordinates\y) Protected nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point Protected ny = *Drawing\CenterY / PBMap\TileSize Protected px, py, img, tilex,tiley, key.s, CacheFile.s @@ -702,11 +702,11 @@ Module PBMap ; Else ; MaxNbTile=GadgetHeight(PBMap\Gadget)/PBMap\TileSize ; EndIf - ; Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) + ; Protected Scale.d= 40075*Cos(Radian(PBMap\GeographicCoordinates\Latitude))/Pow(2,PBMap\Zoom) ; Protected Limit.d=Scale*(MaxNbTile)*1.5 ; Debug "Cache cleaning" ; ForEach PBMap\MemCache\Images() - ; Protected Distance.d = HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\TargetLocation) + ; Protected Distance.d = HaversineInKM(@PBMap\MemCache\Images()\Location, @PBMap\GeographicCoordinates) ; Debug "Limit:"+StrD(Limit)+" Distance:"+StrD(Distance) ; If Distance>Limit And IsImage(PBMap\MemCache\Images()\nImage) ; LockMutex(PBMap\MemCache\Images()\Mutex) @@ -741,7 +741,7 @@ Module PBMap Procedure DrawScale(*Drawing.DrawingParameters,x,y,alpha=80) ;TODO Add Option and function to display Scale on Map Protected sunit.s - Protected Scale.d= 40075*Cos(Radian(PBMap\TargetLocation\Latitude))/Pow(2,PBMap\Zoom) / 2 + Protected Scale.d= 40075*Cos(Radian(PBMap\GeographicCoordinates\Latitude))/Pow(2,PBMap\Zoom) / 2 Select PBMap\Options\ScaleUnit Case #SCALE_Nautical @@ -762,10 +762,10 @@ Module PBMap Procedure DrawDegrees(*Drawing.DrawingParameters, alpha=192) Protected tx, ty, nx,ny,nx1,ny1,x,y,n,cx,dperpixel.d - Protected pos1.PixelPosition,pos2.PixelPosition,Degrees1.GeographicCoordinates,degrees2.GeographicCoordinates + Protected pos1.PixelCoordinates,pos2.PixelCoordinates,Degrees1.GeographicCoordinates,degrees2.GeographicCoordinates Protected realx - tx = Int(*Drawing\TilePosition\x) - ty = Int(*Drawing\TilePosition\y) + tx = Int(*Drawing\TileCoordinates\x) + ty = Int(*Drawing\TileCoordinates\y) nx = *Drawing\CenterX / PBMap\TileSize ;How many tiles around the point ny = *Drawing\CenterY / PBMap\TileSize *Drawing\Bounds\NorthWest\x = tx-nx-1 @@ -834,13 +834,13 @@ Module PBMap EndProcedure Procedure DrawTrack(*Drawing.DrawingParameters) - Protected Pixel.PixelPosition + Protected Pixel.PixelCoordinates Protected Location.GeographicCoordinates Protected km.f, memKm.i If ListSize(PBMap\track())>0 ;Trace Track ForEach PBMap\track() - If *Drawing\TargetLocation\Latitude<>0 And *Drawing\TargetLocation\Longitude<>0 + If *Drawing\GeographicCoordinates\Latitude<>0 And *Drawing\GeographicCoordinates\Longitude<>0 ;GetPixelCoordFromLocation(@PBMap\track(),@Pixel) LatLon2Pixel(@PBMap\track(), @Pixel, PBMap\Zoom) If ListIndex(PBMap\track())=0 @@ -894,8 +894,8 @@ Module PBMap ; Add a Marker To the Map Procedure AddMarker(Latitude.d, Longitude.d, color.l=-1, CallBackPointer.i = -1) AddElement(PBMap\Marker()) - PBMap\Marker()\Location\Latitude = Latitude - PBMap\Marker()\Location\Longitude = Mod(Mod(Longitude, 360) + 360, 360) + PBMap\Marker()\GeographicCoordinates\Latitude = Latitude + PBMap\Marker()\GeographicCoordinates\Longitude = Mod(Mod(Longitude, 360) + 360, 360) PBMap\Marker()\color = color PBMap\Marker()\CallBackPointer = CallBackPointer PBMap\Redraw = #True @@ -903,11 +903,11 @@ Module PBMap ; Draw all markers on the screen ! Procedure DrawMarkers(*Drawing.DrawingParameters) - Protected Pixel.PixelPosition + Protected Pixel.PixelCoordinates ForEach PBMap\Marker() - If PBMap\Marker()\Location\Latitude <> 0 And PBMap\Marker()\Location\Longitude <> 0 - ;GetPixelCoordFromLocation(PBMap\Marker()\Location, @Pixel) - LatLon2Pixel(PBMap\Marker()\Location, @Pixel, PBMap\Zoom) + If PBMap\Marker()\GeographicCoordinates\Latitude <> 0 And PBMap\Marker()\GeographicCoordinates\Longitude <> 0 + ;GetPixelCoordFromLocation(PBMap\Marker()\GeographicCoordinates, @Pixel) + LatLon2Pixel(PBMap\Marker()\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 PBMap\Marker()\CallBackPointer > 0 CallFunctionFast(PBMap\Marker()\CallBackPointer, Pixel\X, Pixel\Y) @@ -928,12 +928,13 @@ Module PBMap ;Precalc some values *Drawing\CenterX = GadgetWidth(PBMap\Gadget) / 2 *Drawing\CenterY = GadgetHeight(PBMap\Gadget) / 2 + *Drawing\GeographicCoordinates\Latitude = PBMap\GeographicCoordinates\Latitude + *Drawing\GeographicCoordinates\Longitude = PBMap\GeographicCoordinates\Longitude + LatLon2TileXY(*Drawing\GeographicCoordinates, *Drawing\TileCoordinates, PBMap\Zoom) ;Pixel shift, aka position in the tile - Px = *Drawing\TilePosition\x : Py = *Drawing\TilePosition\y + 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\TargetLocation\Latitude = PBMap\TargetLocation\Latitude - *Drawing\TargetLocation\Longitude = PBMap\TargetLocation\Longitude ;Main drawing stuff StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget)) ;TODO add in layers of tiles ;this way we can cache them as 0 base 1.n layers @@ -972,27 +973,27 @@ Module PBMap EndProcedure Procedure.d Longitude(x) - Protected NewX.d = (PBMap\PixelPosition\x - GadgetWidth(PBMap\Gadget) / 2 + x) / PBMap\TileSize + Protected NewX.d = (PBMap\PixelCoordinates\x - GadgetWidth(PBMap\Gadget) / 2 + x) / PBMap\TileSize Protected n.d = Pow(2.0, PBMap\Zoom) ;double mod is to ensure the longitude to be in the range [-180;180[ ProcedureReturn Mod(Mod(NewX / n * 360.0, 360.0) + 360.0, 360.0) - 180 EndProcedure Procedure.d Latitude(y) - Protected NewY.d = (PBMap\PixelPosition\y - GadgetHeight(PBMap\Gadget) / 2 + y) / PBMap\TileSize + Protected NewY.d = (PBMap\PixelCoordinates\y - GadgetHeight(PBMap\Gadget) / 2 + y) / PBMap\TileSize Protected n.d = Pow(2.0, PBMap\Zoom) ProcedureReturn Degree(ATan(SinH(#PI * (1.0 - 2.0 * NewY / n)))) EndProcedure Procedure.d MouseLongitude() - Protected MouseX.d = (PBMap\PixelPosition\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX)) / PBMap\TileSize + Protected MouseX.d = (PBMap\PixelCoordinates\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX)) / PBMap\TileSize Protected n.d = Pow(2.0, PBMap\Zoom) ;double mod is to ensure the longitude to be in the range [-180;180[ ProcedureReturn Mod(Mod(MouseX / n * 360.0, 360.0) + 360.0, 360.0) - 180 EndProcedure Procedure.d MouseLatitude() - Protected MouseY.d = (PBMap\PixelPosition\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY)) / PBMap\TileSize + Protected MouseY.d = (PBMap\PixelCoordinates\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY)) / PBMap\TileSize Protected n.d = Pow(2.0, PBMap\Zoom) ProcedureReturn Degree(ATan(SinH(#PI * (1.0 - 2.0 * MouseY / n)))) EndProcedure @@ -1000,25 +1001,25 @@ Module PBMap Procedure SetLocation(latitude.d, longitude.d, zoom = 15, Mode.i = #PB_Absolute) Select Mode Case #PB_Absolute - PBMap\TargetLocation\Latitude = latitude - PBMap\TargetLocation\Longitude = longitude + PBMap\GeographicCoordinates\Latitude = latitude + PBMap\GeographicCoordinates\Longitude = longitude PBMap\Zoom = zoom Case #PB_Relative - PBMap\TargetLocation\Latitude + latitude - PBMap\TargetLocation\Longitude + longitude + PBMap\GeographicCoordinates\Latitude + latitude + PBMap\GeographicCoordinates\Longitude + longitude PBMap\Zoom + zoom EndSelect If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf - LatLon2TileXY(@PBMap\TargetLocation, @PBMap\Drawing, PBMap\Zoom) + LatLon2TileXY(@PBMap\GeographicCoordinates, @PBMap\Drawing\TileCoordinates, PBMap\Zoom) ;Convert X, Y in tile.decimal into real pixels - PBMap\PixelPosition\x = PBMap\Drawing\TilePosition\x * PBMap\TileSize - PBMap\PixelPosition\y = PBMap\Drawing\TilePosition\y * PBMap\TileSize + PBMap\PixelCoordinates\x = PBMap\Drawing\TileCoordinates\x * PBMap\TileSize + PBMap\PixelCoordinates\y = PBMap\Drawing\TileCoordinates\y * PBMap\TileSize PBMap\Drawing\PassNb = 1 PBMap\Redraw = #True ;Drawing() If PBMap\CallBackLocation > 0 - CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) + CallFunctionFast(PBMap\CallBackLocation, @PBMap\GeographicCoordinates) EndIf EndProcedure @@ -1060,7 +1061,7 @@ Module PBMap Protected lat.d = centerY; SetLocation(lat,lon, Round(zoom,#PB_Round_Down)) Else - SetLocation(PBMap\TargetLocation\Latitude, PBMap\TargetLocation\Longitude, 15) + SetLocation(PBMap\GeographicCoordinates\Latitude, PBMap\GeographicCoordinates\Longitude, 15) EndIf EndProcedure @@ -1073,15 +1074,15 @@ Module PBMap EndSelect If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf - LatLon2TileXY(@PBMap\TargetLocation, @PBMap\Drawing\TilePosition, PBMap\Zoom) + LatLon2TileXY(@PBMap\GeographicCoordinates, @PBMap\Drawing\TileCoordinates, PBMap\Zoom) ;Convert X, Y in tile.decimal into real pixels - PBMap\PixelPosition\X = PBMap\Drawing\TilePosition\x * PBMap\TileSize - PBMap\PixelPosition\Y = PBMap\Drawing\TilePosition\y * PBMap\TileSize + PBMap\PixelCoordinates\X = PBMap\Drawing\TileCoordinates\x * PBMap\TileSize + PBMap\PixelCoordinates\Y = PBMap\Drawing\TileCoordinates\y * PBMap\TileSize ;First drawing PBMap\Redraw = #True ;Drawing() If PBMap\CallBackLocation > 0 - CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) + CallFunctionFast(PBMap\CallBackLocation, @PBMap\GeographicCoordinates) EndIf EndProcedure @@ -1110,7 +1111,7 @@ Module PBMap Protected CenterX = GadgetWidth(PBMap\Gadget) / 2, CenterY = GadgetHeight(PBMap\Gadget) / 2 ;Fast and dirty code - OldPx = PBMap\PixelPosition\x : OldPy = PBMap\PixelPosition\y + OldPx = PBMap\PixelCoordinates\x : OldPy = PBMap\PixelCoordinates\y OldMx = OldPx + CenterX - x OldMy = OldPy + CenterY - y PBMap\Zoom = PBMap\Zoom + zoom @@ -1119,10 +1120,10 @@ Module PBMap Protected MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize ;Centered Zoom - LatLon2TileXY(@PBMap\TargetLocation, @PBMap\Drawing\TilePosition, PBMap\Zoom) + LatLon2TileXY(@PBMap\GeographicCoordinates, @PBMap\Drawing\TileCoordinates, PBMap\Zoom) ;Convert X, Y in tile.decimal into real pixels -; Px = PBMap\Drawing\TilePosition\x * PBMap\TileSize -; Py = PBMap\Drawing\TilePosition\y * PBMap\TileSize +; Px = PBMap\Drawing\TileCoordinates\x * PBMap\TileSize +; Py = PBMap\Drawing\TileCoordinates\y * PBMap\TileSize ; MouseX = Px + CenterX - x ; MouseY = Py + CenterY - y ; @@ -1131,33 +1132,33 @@ Module PBMap ; EndIf ; ; Debug "------" ;TODO bug when zoom near the wrap ; ; Debug "Mx : " +StrD(MouseX) -; ; Debug PBMap\TilePosition\x +; ; Debug PBMap\TileCoordinates\x ; ;Cross-multiply to get the new center -; PBMap\PixelPosition\x = (OldPx * MouseX) / OldMx -; PBMap\PixelPosition\y = (OldPy * MouseY) / OldMy +; PBMap\PixelCoordinates\x = (OldPx * MouseX) / OldMx +; PBMap\PixelCoordinates\y = (OldPy * MouseY) / OldMy ; ;PBMap tile position in tile.decimal -; PBMap\Drawing\TilePosition\x = PBMap\PixelPosition\x / PBMap\TileSize -; PBMap\Drawing\TilePosition\y = PBMap\PixelPosition\y / PBMap\TileSize +; PBMap\Drawing\TileCoordinates\x = PBMap\PixelCoordinates\x / PBMap\TileSize +; PBMap\Drawing\TileCoordinates\y = PBMap\PixelCoordinates\y / PBMap\TileSize ; PBMap\Drawing\PassNb = 1 - ;TileXY2LatLon(@PBMap\Drawing\TilePosition, @PBMap\TargetLocation, PBMap\Zoom) - LatLon2Pixel(@PBMap\TargetLocation, @PBMap\PixelPosition, PBMap\Zoom) + ;TileXY2LatLon(@PBMap\Drawing\TileCoordinates, @PBMap\GeographicCoordinates, PBMap\Zoom) + LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom) ;Start drawing PBMap\Redraw = #True ;Drawing() ;If CallBackLocation send Location to function If PBMap\CallBackLocation > 0 - CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) + CallFunctionFast(PBMap\CallBackLocation, @PBMap\GeographicCoordinates) EndIf EndProcedure Procedure.d GetLatitude() -; ProcedureReturn 0-(90-Mod((PBMap\TargetLocation\Latitude+90),180)) - ProcedureReturn PBMap\TargetLocation\Latitude +; ProcedureReturn 0-(90-Mod((PBMap\GeographicCoordinates\Latitude+90),180)) + ProcedureReturn PBMap\GeographicCoordinates\Latitude EndProcedure Procedure.d GetLongitude() -; ProcedureReturn 0-(180-Mod((PBMap\TargetLocation\Longitude+180),360)) - ProcedureReturn PBMap\TargetLocation\Longitude +; ProcedureReturn 0-(180-Mod((PBMap\GeographicCoordinates\Longitude+180),360)) + ProcedureReturn PBMap\GeographicCoordinates\Longitude EndProcedure Procedure.i GetZoom() @@ -1168,7 +1169,7 @@ Module PBMap Procedure CanvasEvents() Protected MouseX.i, MouseY.i - Protected Marker.Coordinates, *Tile.Tile, MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize + Protected MarkerCoords.Coordinates, *Tile.Tile, MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize PBMap\Moving = #False Select EventType() Case #PB_EventType_MouseWheel @@ -1177,20 +1178,20 @@ Module PBMap SetZoomOnPosition(GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX), GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY), 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) + SetZoom(GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_WheelDelta), #PB_Relative) EndIf Case #PB_EventType_LeftButtonDown ;Check if we select a marker - MouseX = PBMap\PixelPosition\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) - MouseY = PBMap\PixelPosition\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) + 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) ;Clip MouseX to the map range (in X, the map is infinite) MouseX = Mod(Mod(MouseX, MapWidth) + MapWidth, MapWidth) ForEach PBMap\Marker() - LatLon2TileXY(@PBMap\Marker()\Location, @Marker, PBMap\Zoom) - Marker\x * PBMap\TileSize - Marker\y * PBMap\TileSize - ;Debug "Pos : " + StrD(Marker\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\TilePosition\x) - If Distance(Marker\x, Marker\y, MouseX, MouseY) < 8 + LatLon2TileXY(@PBMap\Marker()\GeographicCoordinates, @MarkerCoords, PBMap\Zoom) + MarkerCoords\x * PBMap\TileSize + MarkerCoords\y * PBMap\TileSize + ;Debug "Pos : " + StrD(Marker\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\TileCoordinates\x) + If Distance(MarkerCoords\x, MarkerCoords\y, MouseX, MouseY) < 8 PBMap\EditMarkerIndex = ListIndex(PBMap\Marker()) Break EndIf @@ -1206,27 +1207,24 @@ Module PBMap ;Move marker If PBMap\EditMarkerIndex > -1 SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) - LatLon2TileXY(@PBMap\Marker()\Location, @Marker, PBMap\Zoom) - Marker\x + MouseX / PBMap\TileSize - Marker\y + MouseY / PBMap\TileSize - TileXY2LatLon(@Marker, @PBMap\Marker()\Location, PBMap\Zoom) + LatLon2TileXY(@PBMap\Marker()\GeographicCoordinates, @MarkerCoords, PBMap\Zoom) + MarkerCoords\x + MouseX / PBMap\TileSize + MarkerCoords\y + MouseY / PBMap\TileSize + TileXY2LatLon(@MarkerCoords, @PBMap\Marker()\GeographicCoordinates, PBMap\Zoom) Else ;New move values ;PBMap\Position\x - MouseX ;Ensures that pixel position stay in the range [0..2^Zoom*PBMap\TileSize[ coz of the wrapping of the map - PBMap\PixelPosition\x - MouseX - PBMap\PixelPosition\x = Mod(Mod(PBMap\PixelPosition\x, MapWidth) + MapWidth, MapWidth) - PBMap\PixelPosition\y - MouseY - ;PBMap tile position in tile.decimal - PBMap\Drawing\TilePosition\x = PBMap\PixelPosition\x / PBMap\TileSize - PBMap\Drawing\TilePosition\y = PBMap\PixelPosition\y / PBMap\TileSize - PBMap\Drawing\PassNb = 1 - TileXY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation, PBMap\Zoom) + PBMap\PixelCoordinates\x - MouseX + PBMap\PixelCoordinates\x = Mod(Mod(PBMap\PixelCoordinates\x, MapWidth) + MapWidth, MapWidth) + PBMap\PixelCoordinates\y - MouseY + Pixel2LatLon(@PBMap\PixelCoordinates, @PBMap\GeographicCoordinates, PBMap\Zoom) ;If CallBackLocation send Location to function If PBMap\CallBackLocation > 0 - CallFunctionFast(PBMap\CallBackLocation, @PBMap\TargetLocation) + CallFunctionFast(PBMap\CallBackLocation, @PBMap\GeographicCoordinates) EndIf EndIf + PBMap\Drawing\PassNb = 1 PBMap\Redraw = #True PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) @@ -1236,10 +1234,10 @@ Module PBMap If PBMap\EditMarkerIndex > -1 PBMap\EditMarkerIndex = -1 Else ;Move Map - PBMap\Drawing\TilePosition\x = PBMap\PixelPosition\x / PBMap\TileSize - PBMap\Drawing\TilePosition\y = PBMap\PixelPosition\y / PBMap\TileSize - MyDebug("PBMap\Drawing\TilePosition\x " + Str(PBMap\Drawing\TilePosition\x) + " ; PBMap\Drawing\TilePosition\y " + Str(PBMap\Drawing\TilePosition\y) ) - TileXY2LatLon(@PBMap\Drawing, @PBMap\TargetLocation, PBMap\Zoom) +; PBMap\Drawing\TileCoordinates\x = PBMap\PixelCoordinates\x / PBMap\TileSize +; PBMap\Drawing\TileCoordinates\y = PBMap\PixelCoordinates\y / PBMap\TileSize +; MyDebug("PBMap\Drawing\TileCoordinates\x " + Str(PBMap\Drawing\TileCoordinates\x) + " ; PBMap\Drawing\TileCoordinates\y " + Str(PBMap\Drawing\TileCoordinates\y) ) +; TileXY2LatLon(@PBMap\Drawing\TileCoordinates, @PBMap\GeographicCoordinates, PBMap\Zoom) PBMap\Redraw = #True EndIf Case #PB_MAP_REDRAW @@ -1425,9 +1423,9 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 430 -; FirstLine = 415 -; Folding = ---------- +; CursorPosition = 408 +; FirstLine = 397 +; Folding = ----------- ; EnableThread ; EnableXP ; EnableUnicode \ No newline at end of file From 9665398eec8d62a36615eca79c187f9d8b15c31d Mon Sep 17 00:00:00 2001 From: djes Date: Tue, 6 Sep 2016 17:17:56 +0200 Subject: [PATCH 44/49] Better LatLon2Pixel and bughunt Wheelmouse zoom still not working. --- PBMap.pb | 75 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 40f910b..696155b 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -415,25 +415,29 @@ Module PBMap Procedure LatLon2Pixel(*Location.GeographicCoordinates, *Pixel.PixelCoordinates, Zoom) Protected Pos.Coordinates - Protected tilemax = Pow(2.0, Zoom) - Protected cx.d = PBMap\Drawing\CenterX, dpx.d = PBMap\Drawing\TileCoordinates\x - LatLon2TileXY(*Location, @Pos, Zoom) - Protected px.d = Pos\x + Protected tilemax = Pow(2.0, Zoom) * PBMap\TileSize + 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 + ;check the x boundaries of the map to adjust the position (coz of the longitude wrapping) If dpx - px > tilemax / 2 ;Debug "c1" - *Pixel\x = cx + (px - dpx + tilemax) * PBMap\TileSize + *Pixel\x = cx + (px - dpx + tilemax) ElseIf px - dpx > tilemax / 2 ;Debug "c2" - *Pixel\x = cx + (px - dpx - tilemax) * PBMap\TileSize + *Pixel\x = cx + (px - dpx - tilemax) ElseIf px - dpx < 0 ;Debug "c3" - *Pixel\x = cx - (dpx - px) * PBMap\TileSize + *Pixel\x = cx - (dpx - px) Else ;Debug "c0" - *Pixel\x = cx + (px - dpx) * PBMap\TileSize + *Pixel\x = cx + (px - dpx) EndIf - *Pixel\y = PBMap\Drawing\CenterY + (Pos\y - PBMap\Drawing\TileCoordinates\y) * PBMap\TileSize + *Pixel\y = PBMap\Drawing\CenterY + (py - PBMap\PixelCoordinates\y) ;Debug "Longitude : " + StrD(*Location\Longitude) + " ; Pos : " + StrD(Pos\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\Position\x) + "/" + Str(tilemax) + ; " ; XY : " + Str(*Pixel\x) + "," + Str(*Pixel\y) EndProcedure @@ -972,14 +976,14 @@ Module PBMap ;Drawing() EndProcedure - Procedure.d Longitude(x) + Procedure.d Pixel2Lon(x) Protected NewX.d = (PBMap\PixelCoordinates\x - GadgetWidth(PBMap\Gadget) / 2 + x) / PBMap\TileSize Protected n.d = Pow(2.0, PBMap\Zoom) ;double mod is to ensure the longitude to be in the range [-180;180[ ProcedureReturn Mod(Mod(NewX / n * 360.0, 360.0) + 360.0, 360.0) - 180 EndProcedure - Procedure.d Latitude(y) + Procedure.d Pixel2Lat(y) Protected NewY.d = (PBMap\PixelCoordinates\y - GadgetHeight(PBMap\Gadget) / 2 + y) / PBMap\TileSize Protected n.d = Pow(2.0, PBMap\Zoom) ProcedureReturn Degree(ATan(SinH(#PI * (1.0 - 2.0 * NewY / n)))) @@ -1102,25 +1106,30 @@ Module PBMap ;Zoom on x, y position relative to the canvas gadget Procedure SetZoomOnPosition(x, y, zoom) - Debug "----" - Debug Latitude(x) - Debug Longitude(y) - - Protected MouseX.d, MouseY.d - Protected OldPx.d, OldPy.d, OldMx.d, OldMy.d, Px.d, Py.d - Protected CenterX = GadgetWidth(PBMap\Gadget) / 2, CenterY = GadgetHeight(PBMap\Gadget) / 2 - - ;Fast and dirty code - OldPx = PBMap\PixelCoordinates\x : OldPy = PBMap\PixelCoordinates\y - OldMx = OldPx + CenterX - x - OldMy = OldPy + CenterY - y - PBMap\Zoom = PBMap\Zoom + zoom - If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf - If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf - Protected MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize +; Debug "----" +; Debug Latitude(x) +; Debug Longitude(y) +; +; Protected MouseX.d, MouseY.d +; Protected OldPx.d, OldPy.d, OldMx.d, OldMy.d, Px.d, Py.d +; Protected CenterX = GadgetWidth(PBMap\Gadget) / 2, CenterY = GadgetHeight(PBMap\Gadget) / 2 +; +; ;Fast and dirty code +; OldPx = PBMap\PixelCoordinates\x : OldPy = PBMap\PixelCoordinates\y +; OldMx = OldPx + CenterX - x +; OldMy = OldPy + CenterY - y + PBMap\Zoom = PBMap\Zoom + zoom + If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf + If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf + LatLon2TileXY(@PBMap\GeographicCoordinates, @PBMap\Drawing\TileCoordinates, PBMap\Zoom) + ;Convert X, Y in tile.decimal into real pixels + PBMap\PixelCoordinates\X = PBMap\Drawing\TileCoordinates\x * PBMap\TileSize + PBMap\PixelCoordinates\Y = PBMap\Drawing\TileCoordinates\y * PBMap\TileSize + ;First drawing + ;Protected MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize ;Centered Zoom - LatLon2TileXY(@PBMap\GeographicCoordinates, @PBMap\Drawing\TileCoordinates, PBMap\Zoom) + ;LatLon2TileXY(@PBMap\GeographicCoordinates, @PBMap\Drawing\TileCoordinates, PBMap\Zoom) ;Convert X, Y in tile.decimal into real pixels ; Px = PBMap\Drawing\TileCoordinates\x * PBMap\TileSize ; Py = PBMap\Drawing\TileCoordinates\y * PBMap\TileSize @@ -1141,7 +1150,7 @@ Module PBMap ; PBMap\Drawing\TileCoordinates\y = PBMap\PixelCoordinates\y / PBMap\TileSize ; PBMap\Drawing\PassNb = 1 ;TileXY2LatLon(@PBMap\Drawing\TileCoordinates, @PBMap\GeographicCoordinates, PBMap\Zoom) - LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom) + ;LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom) ;Start drawing PBMap\Redraw = #True ;Drawing() @@ -1181,11 +1190,12 @@ Module PBMap SetZoom(GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_WheelDelta), #PB_Relative) EndIf Case #PB_EventType_LeftButtonDown - ;Check if we select a marker + ;LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom) ;TODO 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) ;Clip MouseX to the map range (in X, the map is infinite) MouseX = Mod(Mod(MouseX, MapWidth) + MapWidth, MapWidth) + ;Check if we select a marker ForEach PBMap\Marker() LatLon2TileXY(@PBMap\Marker()\GeographicCoordinates, @MarkerCoords, PBMap\Zoom) MarkerCoords\x * PBMap\TileSize @@ -1213,7 +1223,6 @@ Module PBMap TileXY2LatLon(@MarkerCoords, @PBMap\Marker()\GeographicCoordinates, PBMap\Zoom) Else ;New move values - ;PBMap\Position\x - MouseX ;Ensures that pixel position stay in the range [0..2^Zoom*PBMap\TileSize[ coz of the wrapping of the map PBMap\PixelCoordinates\x - MouseX PBMap\PixelCoordinates\x = Mod(Mod(PBMap\PixelCoordinates\x, MapWidth) + MapWidth, MapWidth) @@ -1423,8 +1432,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 408 -; FirstLine = 397 +; CursorPosition = 430 +; FirstLine = 415 ; Folding = ----------- ; EnableThread ; EnableXP From 26265c21a00e5a04d729d318717296b8550abb8b Mon Sep 17 00:00:00 2001 From: djes Date: Wed, 7 Sep 2016 11:55:53 +0200 Subject: [PATCH 45/49] Clarification on LatLon2PixelRel/LatLon2Pixel --- PBMap.pb | 111 +++++++++++++++++++++++++------------------------------ 1 file changed, 51 insertions(+), 60 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 696155b..02179ef 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -413,16 +413,22 @@ Module PBMap *Location\Latitude = Degree(ATan(SinH(#PI * (1.0 - 2.0 * *Coords\y / n)))) EndProcedure + ;Lat Lon coordinates 2 pixel absolute [0 to 2^Zoom * TileSize [ Procedure LatLon2Pixel(*Location.GeographicCoordinates, *Pixel.PixelCoordinates, Zoom) - Protected Pos.Coordinates + Protected tilemax = Pow(2.0, Zoom) * PBMap\TileSize + Protected LatRad.d = Radian(*Location\Latitude) + *Pixel\x = tilemax * (Mod( *Location\Longitude + 180.0, 360) / 360.0 ) + *Pixel\y = tilemax * ( 1.0 - Log(Tan(LatRad) + (1.0/Cos(LatRad))) / #PI ) / 2.0 + EndProcedure + + ;Lat Lon coordinates 2 pixel relative to the center of view + Procedure LatLon2PixelRel(*Location.GeographicCoordinates, *Pixel.PixelCoordinates, Zoom) Protected tilemax = Pow(2.0, Zoom) * PBMap\TileSize 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 - ;check the x boundaries of the map to adjust the position (coz of the longitude wrapping) If dpx - px > tilemax / 2 ;Debug "c1" @@ -438,10 +444,10 @@ Module PBMap *Pixel\x = cx + (px - dpx) EndIf *Pixel\y = PBMap\Drawing\CenterY + (py - PBMap\PixelCoordinates\y) - ;Debug "Longitude : " + StrD(*Location\Longitude) + " ; Pos : " + StrD(Pos\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\Position\x) + "/" + Str(tilemax) + - ; " ; XY : " + Str(*Pixel\x) + "," + Str(*Pixel\y) EndProcedure + + ; HaversineAlgorithm ; http://andrew.hedges.name/experiments/haversine/ Procedure.d HaversineInKM(*posA.GeographicCoordinates, *posB.GeographicCoordinates) @@ -789,15 +795,15 @@ Module PBMap Degrees2\Longitude = nx1 Degrees2\Latitude = ny1 ; Debug "NW : " + StrD(Degrees1\Longitude) + " ; NE : " + StrD(Degrees2\Longitude) - LatLon2Pixel(@Degrees1, @pos1, PBMap\Zoom) - LatLon2Pixel(@Degrees2, @pos2, PBMap\Zoom) + LatLon2PixelRel(@Degrees1, @pos1, PBMap\Zoom) + LatLon2PixelRel(@Degrees2, @pos2, PBMap\Zoom) VectorFont(FontID(PBMap\Font), 10) VectorSourceColor(RGBA(0, 0, 0, alpha)) ;draw latitudes For y = ny1 To ny Degrees1\Longitude = nx Degrees1\Latitude = y - LatLon2Pixel(@Degrees1, @pos1, PBMap\Zoom) + LatLon2PixelRel(@Degrees1, @pos1, PBMap\Zoom) MovePathCursor(pos1\x, pos1\y) AddPathLine( pos2\x, pos1\y) MovePathCursor(10, pos1\y) @@ -808,7 +814,7 @@ Module PBMap Repeat Degrees1\Longitude = x Degrees1\Latitude = ny - LatLon2Pixel(@Degrees1, @pos1, PBMap\Zoom) + LatLon2PixelRel(@Degrees1, @pos1, PBMap\Zoom) MovePathCursor(pos1\x, pos1\y) AddPathLine( pos1\x, pos2\y) MovePathCursor(pos1\x,10) @@ -846,7 +852,7 @@ Module PBMap ForEach PBMap\track() If *Drawing\GeographicCoordinates\Latitude<>0 And *Drawing\GeographicCoordinates\Longitude<>0 ;GetPixelCoordFromLocation(@PBMap\track(),@Pixel) - LatLon2Pixel(@PBMap\track(), @Pixel, PBMap\Zoom) + LatLon2PixelRel(@PBMap\track(), @Pixel, PBMap\Zoom) If ListIndex(PBMap\track())=0 MovePathCursor(Pixel\X, Pixel\Y) Else @@ -868,7 +874,7 @@ Module PBMap Location\Longitude=PBMap\track()\Longitude EndIf ;GetPixelCoordFromLocation(@PBMap\track(),@Pixel) - LatLon2Pixel(@PBMap\track(),@Pixel, PBMap\Zoom) + LatLon2PixelRel(@PBMap\track(),@Pixel, PBMap\Zoom) If Int(km)<>memKm memKm=Int(km) If PBMap\Zoom>10 @@ -911,7 +917,7 @@ Module PBMap ForEach PBMap\Marker() If PBMap\Marker()\GeographicCoordinates\Latitude <> 0 And PBMap\Marker()\GeographicCoordinates\Longitude <> 0 ;GetPixelCoordFromLocation(PBMap\Marker()\GeographicCoordinates, @Pixel) - LatLon2Pixel(PBMap\Marker()\GeographicCoordinates, @Pixel, PBMap\Zoom) + LatLon2PixelRel(PBMap\Marker()\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 PBMap\Marker()\CallBackPointer > 0 CallFunctionFast(PBMap\Marker()\CallBackPointer, Pixel\X, Pixel\Y) @@ -1105,52 +1111,36 @@ Module PBMap EndProcedure ;Zoom on x, y position relative to the canvas gadget + ;TODO Procedure SetZoomOnPosition(x, y, zoom) -; Debug "----" -; Debug Latitude(x) -; Debug Longitude(y) -; -; Protected MouseX.d, MouseY.d -; Protected OldPx.d, OldPy.d, OldMx.d, OldMy.d, Px.d, Py.d -; Protected CenterX = GadgetWidth(PBMap\Gadget) / 2, CenterY = GadgetHeight(PBMap\Gadget) / 2 -; -; ;Fast and dirty code -; OldPx = PBMap\PixelCoordinates\x : OldPy = PBMap\PixelCoordinates\y -; OldMx = OldPx + CenterX - x -; OldMy = OldPy + CenterY - y - PBMap\Zoom = PBMap\Zoom + zoom - If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf - If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf - LatLon2TileXY(@PBMap\GeographicCoordinates, @PBMap\Drawing\TileCoordinates, PBMap\Zoom) - ;Convert X, Y in tile.decimal into real pixels + Protected MouseX.d, MouseY.d + Protected OldPx.d, OldPy.d, OldMx.d, OldMy.d, Px.d, Py.d + Protected CenterX = GadgetWidth(PBMap\Gadget) / 2, CenterY = GadgetHeight(PBMap\Gadget) / 2 + ;Fast and dirty code + OldPx = PBMap\PixelCoordinates\x : OldPy = PBMap\PixelCoordinates\y + OldMx = OldPx + CenterX - x + OldMy = OldPy + CenterY - y + PBMap\Zoom = PBMap\Zoom + zoom + If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf + If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf + LatLon2TileXY(@PBMap\GeographicCoordinates, @PBMap\Drawing\TileCoordinates, PBMap\Zoom) + ;Convert X, Y in tile.decimal into real pixels PBMap\PixelCoordinates\X = PBMap\Drawing\TileCoordinates\x * PBMap\TileSize PBMap\PixelCoordinates\Y = PBMap\Drawing\TileCoordinates\y * PBMap\TileSize - ;First drawing - ;Protected MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize - - ;Centered Zoom - ;LatLon2TileXY(@PBMap\GeographicCoordinates, @PBMap\Drawing\TileCoordinates, PBMap\Zoom) ;Convert X, Y in tile.decimal into real pixels -; Px = PBMap\Drawing\TileCoordinates\x * PBMap\TileSize -; Py = PBMap\Drawing\TileCoordinates\y * PBMap\TileSize -; MouseX = Px + CenterX - x -; MouseY = Py + CenterY - y -; -; If MouseX > MapWidth -; Debug "kaboum" -; EndIf -; ; Debug "------" ;TODO bug when zoom near the wrap -; ; Debug "Mx : " +StrD(MouseX) -; ; Debug PBMap\TileCoordinates\x -; ;Cross-multiply to get the new center -; PBMap\PixelCoordinates\x = (OldPx * MouseX) / OldMx -; PBMap\PixelCoordinates\y = (OldPy * MouseY) / OldMy -; ;PBMap tile position in tile.decimal -; PBMap\Drawing\TileCoordinates\x = PBMap\PixelCoordinates\x / PBMap\TileSize -; PBMap\Drawing\TileCoordinates\y = PBMap\PixelCoordinates\y / PBMap\TileSize -; PBMap\Drawing\PassNb = 1 - ;TileXY2LatLon(@PBMap\Drawing\TileCoordinates, @PBMap\GeographicCoordinates, PBMap\Zoom) - ;LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom) + Px = PBMap\Drawing\TileCoordinates\x * PBMap\TileSize + Py = PBMap\Drawing\TileCoordinates\y * PBMap\TileSize + MouseX = Px + CenterX - x + MouseY = Py + CenterY - y + ;Cross-multiply to get the new center + PBMap\PixelCoordinates\x = (OldPx * MouseX) / OldMx + PBMap\PixelCoordinates\y = (OldPy * MouseY) / OldMy + ;PBMap tile position in tile.decimal + PBMap\Drawing\TileCoordinates\x = PBMap\PixelCoordinates\x / PBMap\TileSize + PBMap\Drawing\TileCoordinates\y = PBMap\PixelCoordinates\y / PBMap\TileSize + PBMap\Drawing\PassNb = 1 + TileXY2LatLon(@PBMap\Drawing\TileCoordinates, @PBMap\GeographicCoordinates, PBMap\Zoom) + ;LatLon2PixelRel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom) ;Start drawing PBMap\Redraw = #True ;Drawing() @@ -1190,14 +1180,14 @@ Module PBMap SetZoom(GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_WheelDelta), #PB_Relative) EndIf Case #PB_EventType_LeftButtonDown - ;LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom) ;TODO + 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) ;Clip MouseX to the map range (in X, the map is infinite) MouseX = Mod(Mod(MouseX, MapWidth) + MapWidth, MapWidth) ;Check if we select a marker ForEach PBMap\Marker() - LatLon2TileXY(@PBMap\Marker()\GeographicCoordinates, @MarkerCoords, PBMap\Zoom) + LatLon2TileXY(@PBMap\Marker()\GeographicCoordinates, @MarkerCoords, PBMap\Zoom) ;This line could be removed as the coordinates don't have to change but I want to be sure we rely only on geographic coordinates MarkerCoords\x * PBMap\TileSize MarkerCoords\y * PBMap\TileSize ;Debug "Pos : " + StrD(Marker\x) + " ; Drawing pos : " + StrD(PBMap\Drawing\TileCoordinates\x) @@ -1207,8 +1197,8 @@ Module PBMap EndIf Next ;Mem cursor Coord - PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) - PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) + 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 If PBMap\MoveStartingPoint\x <> - 1 @@ -1223,6 +1213,7 @@ Module PBMap TileXY2LatLon(@MarkerCoords, @PBMap\Marker()\GeographicCoordinates, PBMap\Zoom) Else ;New move values + LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom) ;This line could be removed as the coordinates don't have to change but I want to be sure we rely only on geographic coordinates ;Ensures that pixel position stay in the range [0..2^Zoom*PBMap\TileSize[ coz of the wrapping of the map PBMap\PixelCoordinates\x - MouseX PBMap\PixelCoordinates\x = Mod(Mod(PBMap\PixelCoordinates\x, MapWidth) + MapWidth, MapWidth) @@ -1432,8 +1423,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 430 -; FirstLine = 415 +; CursorPosition = 1117 +; FirstLine = 1108 ; Folding = ----------- ; EnableThread ; EnableXP From 5aba6510b558d49213c158d79843a0e76de57828 Mon Sep 17 00:00:00 2001 From: djes Date: Wed, 7 Sep 2016 12:35:38 +0200 Subject: [PATCH 46/49] wip --- PBMap.pb | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index 02179ef..ae97e9e 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -419,7 +419,7 @@ Module PBMap Protected LatRad.d = Radian(*Location\Latitude) *Pixel\x = tilemax * (Mod( *Location\Longitude + 180.0, 360) / 360.0 ) *Pixel\y = tilemax * ( 1.0 - Log(Tan(LatRad) + (1.0/Cos(LatRad))) / #PI ) / 2.0 - EndProcedure + EndProcedure ;Lat Lon coordinates 2 pixel relative to the center of view Procedure LatLon2PixelRel(*Location.GeographicCoordinates, *Pixel.PixelCoordinates, Zoom) @@ -430,7 +430,7 @@ Module PBMap 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 ;check the x boundaries of the map to adjust the position (coz of the longitude wrapping) - If dpx - px > tilemax / 2 + If dpx - px >= tilemax / 2 ;Debug "c1" *Pixel\x = cx + (px - dpx + tilemax) ElseIf px - dpx > tilemax / 2 @@ -1116,34 +1116,27 @@ Module PBMap Protected MouseX.d, MouseY.d Protected OldPx.d, OldPy.d, OldMx.d, OldMy.d, Px.d, Py.d Protected CenterX = GadgetWidth(PBMap\Gadget) / 2, CenterY = GadgetHeight(PBMap\Gadget) / 2 - ;Fast and dirty code + Protected MapWidth = Pow(2.0, PBMap\Zoom) * PBMap\TileSize + LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom) OldPx = PBMap\PixelCoordinates\x : OldPy = PBMap\PixelCoordinates\y OldMx = OldPx + CenterX - x OldMy = OldPy + CenterY - y + ;*** First : Zoom PBMap\Zoom = PBMap\Zoom + zoom If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf - LatLon2TileXY(@PBMap\GeographicCoordinates, @PBMap\Drawing\TileCoordinates, PBMap\Zoom) - ;Convert X, Y in tile.decimal into real pixels - PBMap\PixelCoordinates\X = PBMap\Drawing\TileCoordinates\x * PBMap\TileSize - PBMap\PixelCoordinates\Y = PBMap\Drawing\TileCoordinates\y * PBMap\TileSize - ;Convert X, Y in tile.decimal into real pixels - Px = PBMap\Drawing\TileCoordinates\x * PBMap\TileSize - Py = PBMap\Drawing\TileCoordinates\y * PBMap\TileSize - MouseX = Px + CenterX - x - MouseY = Py + CenterY - y - ;Cross-multiply to get the new center - PBMap\PixelCoordinates\x = (OldPx * MouseX) / OldMx - PBMap\PixelCoordinates\y = (OldPy * MouseY) / OldMy - ;PBMap tile position in tile.decimal - PBMap\Drawing\TileCoordinates\x = PBMap\PixelCoordinates\x / PBMap\TileSize - PBMap\Drawing\TileCoordinates\y = PBMap\PixelCoordinates\y / PBMap\TileSize - PBMap\Drawing\PassNb = 1 - TileXY2LatLon(@PBMap\Drawing\TileCoordinates, @PBMap\GeographicCoordinates, PBMap\Zoom) - ;LatLon2PixelRel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom) + LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom) + ;*** + Debug OldMx - OldPx + MouseX = PBMap\PixelCoordinates\X + CenterX - x + MouseY = PBMap\PixelCoordinates\Y + CenterY - y + ;Cross-multiply to get the new view center + PBMap\PixelCoordinates\x = (OldPx * MouseX) / OldMx + PBMap\PixelCoordinates\y = (OldPy * MouseY) / OldMy + Pixel2LatLon(@PBMap\PixelCoordinates, @PBMap\GeographicCoordinates, PBMap\Zoom) ;Start drawing + PBMap\Drawing\PassNb = 1 PBMap\Redraw = #True - ;Drawing() ;If CallBackLocation send Location to function If PBMap\CallBackLocation > 0 CallFunctionFast(PBMap\CallBackLocation, @PBMap\GeographicCoordinates) @@ -1423,8 +1416,8 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1117 -; FirstLine = 1108 +; CursorPosition = 1129 +; FirstLine = 1115 ; Folding = ----------- ; EnableThread ; EnableXP From c698760d7d58881bdbc5933446b2bad0f6ea82d4 Mon Sep 17 00:00:00 2001 From: djes Date: Fri, 9 Sep 2016 12:33:46 +0200 Subject: [PATCH 47/49] SetZoomOnPosition bugfixed and double clic fast go --- PBMap.pb | 54 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index ae97e9e..abd0159 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -107,7 +107,6 @@ Module PBMap DeltaX.i DeltaY.i Dirty.i - PassNB.i End.i EndStructure @@ -1025,7 +1024,6 @@ Module PBMap ;Convert X, Y in tile.decimal into real pixels PBMap\PixelCoordinates\x = PBMap\Drawing\TileCoordinates\x * PBMap\TileSize PBMap\PixelCoordinates\y = PBMap\Drawing\TileCoordinates\y * PBMap\TileSize - PBMap\Drawing\PassNb = 1 PBMap\Redraw = #True ;Drawing() If PBMap\CallBackLocation > 0 @@ -1111,31 +1109,45 @@ Module PBMap EndProcedure ;Zoom on x, y position relative to the canvas gadget - ;TODO 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, CenterY = GadgetHeight(PBMap\Gadget) / 2 - Protected MapWidth = Pow(2.0, PBMap\Zoom) * PBMap\TileSize - LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom) - OldPx = PBMap\PixelCoordinates\x : OldPy = PBMap\PixelCoordinates\y - OldMx = OldPx + CenterX - x - OldMy = OldPy + CenterY - y + Protected CenterX = GadgetWidth(PBMap\Gadget) / 2 + Protected CenterY = GadgetHeight(PBMap\Gadget) / 2 + x - CenterX + y - CenterY ;*** First : Zoom - PBMap\Zoom = PBMap\Zoom + zoom + PBMap\Zoom + zoom If PBMap\Zoom > PBMap\ZoomMax : PBMap\Zoom = PBMap\ZoomMax : EndIf If PBMap\Zoom < PBMap\ZoomMin : PBMap\Zoom = PBMap\ZoomMin : EndIf LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom) - ;*** - Debug OldMx - OldPx - MouseX = PBMap\PixelCoordinates\X + CenterX - x - MouseY = PBMap\PixelCoordinates\Y + CenterY - y - ;Cross-multiply to get the new view center - PBMap\PixelCoordinates\x = (OldPx * MouseX) / OldMx - PBMap\PixelCoordinates\y = (OldPy * MouseY) / OldMy + If Zoom = 1 + PBMap\PixelCoordinates\x + x + PBMap\PixelCoordinates\y + y + ElseIf zoom = -1 + PBMap\PixelCoordinates\x - x/2 + PBMap\PixelCoordinates\y - y/2 + EndIf + 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 + + ;Go to x, y position relative to the canvas gadget + Procedure GotoPixelRel(x, y) + Protected CenterX = GadgetWidth(PBMap\Gadget) / 2 + Protected CenterY = GadgetHeight(PBMap\Gadget) / 2 + x - CenterX + y - CenterY + LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom) + PBMap\PixelCoordinates\x + x + PBMap\PixelCoordinates\y + y Pixel2LatLon(@PBMap\PixelCoordinates, @PBMap\GeographicCoordinates, PBMap\Zoom) ;Start drawing - PBMap\Drawing\PassNb = 1 PBMap\Redraw = #True ;If CallBackLocation send Location to function If PBMap\CallBackLocation > 0 @@ -1164,6 +1176,8 @@ Module PBMap Protected MarkerCoords.Coordinates, *Tile.Tile, MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize PBMap\Moving = #False Select EventType() + Case #PB_EventType_LeftDoubleClick + GotoPixelRel(GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX), GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY)) Case #PB_EventType_MouseWheel If PBMap\Options\WheelMouseRelative ;Relative zoom (centered on the mouse) @@ -1217,7 +1231,6 @@ Module PBMap CallFunctionFast(PBMap\CallBackLocation, @PBMap\GeographicCoordinates) EndIf EndIf - PBMap\Drawing\PassNb = 1 PBMap\Redraw = #True PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) @@ -1416,8 +1429,7 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 1129 -; FirstLine = 1115 +; CursorPosition = 8 ; Folding = ----------- ; EnableThread ; EnableXP From efdbcc8e4920493c608916a631c8645015dff09d Mon Sep 17 00:00:00 2001 From: djes Date: Fri, 9 Sep 2016 16:02:40 +0200 Subject: [PATCH 48/49] Cleanup + layer update --- PBMap.pb | 208 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 118 insertions(+), 90 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index abd0159..be6e992 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -37,7 +37,8 @@ DeclareModule PBMap #SCALE_KM = 0 Declare InitPBMap(window) - Declare SetMapServer(ServerURL.s = "http://tile.openstreetmap.org/", TileSize = 256, ZoomMin = 0, ZoomMax = 18) + Declare.i AddMapServerLayer(LayerName.s, Order.i, ServerURL.s = "http://tile.openstreetmap.org/", TileSize = 256, ZoomMin = 0, ZoomMax = 18) + Declare DeleteLayer(Nb.i) Declare MapGadget(Gadget.i, X.i, Y.i, Width.i, Height.i) Declare SetLocation(latitude.d, longitude.d, zoom = 15, mode.i = #PB_Absolute) Declare Drawing() @@ -86,10 +87,10 @@ Module PBMap CacheFile.s GetImageThread.i RetryNb.i - Layer.i + ServerURL.s EndStructure - Structure TileBounds + Structure TileBounds NorthWest.Coordinates SouthEast.Coordinates EndStructure @@ -127,56 +128,61 @@ Module PBMap EndStructure Structure Marker - GeographicCoordinates.GeographicCoordinates ; Marker latitude and longitude - color.l ; Marker color - CallBackPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) + GeographicCoordinates.GeographicCoordinates ; Marker latitude and longitude + color.l ; Marker color + CallBackPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) EndStructure Structure Option WheelMouseRelative.i - ScaleUnit.i ; Scale unit to use for measurements + ScaleUnit.i ; Scale unit to use for measurements EndStructure + Structure Layer + Order.i ; Layer nb + Name.s + ServerURL.s ; Web URL ex: http://tile.openstreetmap.org/ + EndStructure + ;-PBMap Structure Structure PBMap - Window.i ; Parent Window - Gadget.i ; Canvas Gadget Id - Font.i ; Font to uses when write on the map + Window.i ; Parent Window + Gadget.i ; Canvas Gadget Id + Font.i ; Font to uses when write on the map Timer.i GeographicCoordinates.GeographicCoordinates ; Latitude and Longitude from focus point - Drawing.DrawingParameters ; Drawing parameters based on focus point - ; - CallBackLocation.i ; @Procedure(latitude.d,lontitude.d) - CallBackMainPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) - ; - PixelCoordinates.PixelCoordinates ; Actual focus point coords in pixels (global) - MoveStartingPoint.PixelCoordinates ; Start mouse position coords when dragging the map - ; - Array ServerURL.s(0) ; Web URL ex: http://tile.openstreetmap.org/ - NumberOfMapLayers.i ; The number of map tile layers; + Drawing.DrawingParameters ; Drawing parameters based on focus point + ; + CallBackLocation.i ; @Procedure(latitude.d,lontitude.d) + CallBackMainPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) + ; + PixelCoordinates.PixelCoordinates ; Actual focus point coords in pixels (global) + MoveStartingPoint.PixelCoordinates ; Start mouse position coords when dragging the map + ; + List Layers.Layer() - ZoomMin.i ; Min Zoom supported by server - ZoomMax.i ; Max Zoom supported by server - Zoom.i ; Current zoom - TileSize.i ; Tile size downloaded on the server ex : 256 - ; - HDDCachePath.S ; Path where to load and save tiles downloaded from server - MemCache.TileMemCach ; Images in memory cache - ; + ZoomMin.i ; Min Zoom supported by server + ZoomMax.i ; Max Zoom supported by server + Zoom.i ; Current zoom + TileSize.i ; Tile size downloaded on the server ex : 256 + ; + HDDCachePath.S ; Path where to load and save tiles downloaded from server + MemCache.TileMemCach ; Images in memory cache + ; Redraw.i Moving.i - Dirty.i ; To signal that drawing need a refresh - ; + Dirty.i ; To signal that drawing need a refresh + ; MainDrawingThread.i - TileThreadMutex.i; ;Mutex to protect resources - List track.GeographicCoordinates() ; To display a GPX track - List Marker.Marker() ; To diplay marker + TileThreadMutex.i; ;Mutex to protect resources + List track.GeographicCoordinates() ; To display a GPX track + List Marker.Marker() ; To diplay marker EditMarkerIndex.l - ImgLoading.i ;Image Loading Tile - ImgNothing.i ;Image Nothing Tile + ImgLoading.i ;Image Loading Tile + ImgNothing.i ;Image Nothing Tile - Options.option ; + Options.option ; EndStructure #PB_MAP_REDRAW = #PB_EventType_FirstCustomValue + 1 @@ -306,8 +312,7 @@ Module PBMap PBMap\Window = Window PBMap\Timer = 1 PBMap\Options\WheelMouseRelative = #True - SetMapServer("http://tile.openstreetmap.org/") - + AddMapServerLayer("OSM", 1, "http://tile.openstreetmap.org/") ;-Preferences ;Use this to create and customize your preferences file for the first time ; CreatePreferences(GetHomeDirectory() + "PBMap.prefs") @@ -336,13 +341,24 @@ Module PBMap TechnicalImagesCreation() EndProcedure - Procedure SetMapServer(ServerURL.s = "http://tile.openstreetmap.org/", TileSize = 256, ZoomMin = 0, ZoomMax = 18) - PBMAP\NumberOfMapLayers + 1 - ReDim PBMap\ServerURL(PBMAP\NumberOfMapLayers) - PBMap\ServerURL(PBMAP\NumberOfMapLayers-1) = ServerURL - PBMap\ZoomMin = ZoomMin - PBMap\ZoomMax = ZoomMax - PBMap\TileSize = TileSize + Procedure.i AddMapServerLayer(LayerName.s, Order.i, ServerURL.s = "http://tile.openstreetmap.org/", TileSize = 256, ZoomMin = 0, ZoomMax = 18) + Protected *Ptr = AddElement(PBMap\Layers()) + If *Ptr + PBMap\Layers()\Name = LayerName + PBMap\Layers()\Order = Order + PBMap\Layers()\ServerURL = ServerURL + SortStructuredList(PBMap\Layers(), #PB_Sort_Ascending, OffsetOf(Layer\Order),TypeOf(Layer\Order)) + ProcedureReturn *Ptr + Else + ProcedureReturn #False + EndIf + EndProcedure + + Procedure DeleteLayer(*Ptr) + ChangeCurrentElement(PBMap\Layers(), *Ptr) + DeleteElement(PBMap\Layers()) + FirstElement(PBMap\Layers()) + SortStructuredList(PBMap\Layers(), #PB_Sort_Ascending, OffsetOf(Layer\Order),TypeOf(Layer\Order)) EndProcedure Procedure Quit() @@ -395,8 +411,8 @@ Module PBMap MyDebug("Latitude : " + StrD(*Location\Latitude) + " ; Longitude : " + StrD(*Location\Longitude), 5) MyDebug("Coords X : " + Str(*Coords\x) + " ; Y : " + Str(*Coords\y), 5) EndProcedure - - ;*** Converts tile.decimal to coords + + ;*** Converts tile.decimal to coords ;Warning, structures used in parameters are not tested Procedure TileXY2LatLon(*Coords.Coordinates, *Location.GeographicCoordinates, Zoom) Protected n.d = Pow(2.0, Zoom) @@ -436,8 +452,8 @@ Module PBMap ;Debug "c2" *Pixel\x = cx + (px - dpx - tilemax) ElseIf px - dpx < 0 - ;Debug "c3" - *Pixel\x = cx - (dpx - px) + ;Debug "c3" + *Pixel\x = cx - (dpx - px) Else ;Debug "c0" *Pixel\x = cx + (px - dpx) @@ -445,7 +461,7 @@ Module PBMap *Pixel\y = PBMap\Drawing\CenterY + (py - PBMap\PixelCoordinates\y) EndProcedure - + ; HaversineAlgorithm ; http://andrew.hedges.name/experiments/haversine/ @@ -465,7 +481,7 @@ Module PBMap ProcedureReturn (1000 * HaversineInKM(@*posA,@*posB)); EndProcedure - Procedure GetPixelCoordFromLocation(*Location.GeographicCoordinates, *Pixel.PixelCoordinates, Zoom) ; TODO to Optimize + Procedure GetPixelCoordFromLocation(*Location.GeographicCoordinates, *Pixel.PixelCoordinates, Zoom) ; TODO to Optimize Protected mapWidth.l = Pow(2, Zoom + 8) Protected mapHeight.l = Pow(2, Zoom + 8) Protected x1.l,y1.l @@ -475,20 +491,20 @@ Module PBMap Protected latRad.d = *Location\Latitude*#PI/180; Protected mercN.d = Log(Tan((#PI/4)+(latRad/2))); y1 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)) ; -; Debug "location" -; Debug x1 -; Debug y1 + ; Debug "location" + ; Debug x1 + ; Debug y1 Protected x2.l, y2.l ; get x value x2 = (PBMap\GeographicCoordinates\Longitude+180)*(mapWidth/360) ; convert from degrees To radians latRad = PBMap\GeographicCoordinates\Latitude*#PI/180; - ; get y value + ; get y value mercN = Log(Tan((#PI/4)+(latRad/2))) y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)); -; Debug "GeographicCoordinates" -; Debug x1 -; Debug y1 + ; Debug "GeographicCoordinates" + ; Debug x1 + ; Debug y1 *Pixel\x=GadgetWidth(PBMap\Gadget)/2 - (x2-x1) *Pixel\y=GadgetHeight(PBMap\Gadget)/2 - (y2-y1) EndProcedure @@ -546,11 +562,10 @@ Module PBMap ProcedureReturn -1 EndProcedure - Procedure.i GetTileFromWeb(Zoom.i, XTile.i, YTile.i, CacheFile.s, Layer.i) + Procedure.i GetTileFromWeb(TileURL.s, CacheFile.s) Protected *Buffer Protected nImage.i = -1 Protected FileSize.i, timg - Protected TileURL.s = PBMap\ServerURL(Layer) + Str(Zoom) + "/" + Str(XTile) + "/" + Str(YTile) + ".png" If Proxy FileSize = CurlReceiveHTTPToFile(TileURL, CacheFile, ProxyURL$, ProxyPort$, ProxyUser$, ProxyPassword$) If FileSize > 0 @@ -595,8 +610,9 @@ Module PBMap Procedure GetImageThread(*Tile.Tile) Protected nImage.i = -1 + Protected TileURL.s = *Tile\ServerURL + Str(*Tile\PBMapZoom) + "/" + Str(*Tile\PBMapTileX) + "/" + Str(*Tile\PBMapTileY) + ".png" Repeat - nImage = GetTileFromWeb(*Tile\PBMapZoom, *Tile\PBMapTileX, *Tile\PBMapTileY, *Tile\CacheFile, *Tile\Layer) + nImage = GetTileFromWeb(TileURL, *Tile\CacheFile) If nImage <> -1 MyDebug("Image key : " + *Tile\key + " web image loaded", 3) *Tile\RetryNb = 0 @@ -611,7 +627,7 @@ Module PBMap PostEvent(#PB_Event_Gadget, PBMap\Window, PBmap\Gadget, #PB_MAP_TILE_CLEANUP, *Tile) ;To free memory outside the thread EndProcedure - Procedure.i GetTile(key.s, CacheFile.s, px.i, py.i, tilex.i, tiley.i, Layer.i) + Procedure.i GetTile(key.s, CacheFile.s, px.i, py.i, tilex.i, tiley.i, ServerURL.s) Protected timg = -1 If FindMapElement(PBMap\MemCache\Images(), key) MyDebug("Key : " + key + " found in memory cache!", 3) @@ -647,7 +663,7 @@ Module PBMap \PBMapZoom = PBMap\Zoom \key = key \CacheFile = CacheFile - \Layer = Layer + \ServerURL = ServerURL \RetryNb = 5 \nImage = -1 MyDebug(" Creating get image thread nb " + Str(\GetImageThread) + " to get " + CacheFile, 3) @@ -660,7 +676,7 @@ Module PBMap ProcedureReturn timg EndProcedure - Procedure DrawTiles(*Drawing.DrawingParameters, Layer.i, alpha.i=255) + Procedure DrawTiles(*Drawing.DrawingParameters, Layer, alpha.i=255) ;DisableDebugger Protected x.i, y.i,kq.q Protected tx = Int(*Drawing\TileCoordinates\x) ;Don't forget the Int() ! @@ -669,6 +685,7 @@ Module PBMap Protected ny = *Drawing\CenterY / PBMap\TileSize Protected px, py, img, tilex,tiley, key.s, CacheFile.s Protected tilemax = 1<= 0 And tiley < tilemax - kq = Layer | (pbmap\zoom << 8) | (tilex << 16) | (tiley << 36) - key = Str(kq) + kq = (PBMap\zoom << 8) | (tilex << 16) | (tiley << 36) + key = PBMap\Layers()\Name + Str(kq) CacheFile = PBMap\HDDCachePath + key + ".png" - - img = GetTile(key, CacheFile, px, py, tilex, tiley, Layer) + img = GetTile(key, CacheFile, px, py, tilex, tiley, PBMap\Layers()\ServerURL) If img <> -1 MovePathCursor(px, py) DrawVectorImage(ImageID(img), alpha) @@ -693,7 +709,7 @@ Module PBMap DrawVectorImage(ImageID(PBMap\ImgLoading), alpha) EndIf Else - If Layer = 0 + If PBMap\Layers()\Name = "" MovePathCursor(px, py) DrawVectorImage(ImageID(PBMap\ImgNothing)) EndIf @@ -768,7 +784,7 @@ Module PBMap AddPathLine(x+128,y+12) StrokePath(1) EndProcedure - + Procedure DrawDegrees(*Drawing.DrawingParameters, alpha=192) Protected tx, ty, nx,ny,nx1,ny1,x,y,n,cx,dperpixel.d Protected pos1.PixelCoordinates,pos2.PixelCoordinates,Degrees1.GeographicCoordinates,degrees2.GeographicCoordinates @@ -781,7 +797,7 @@ Module PBMap *Drawing\Bounds\NorthWest\y = ty-ny-1 *Drawing\Bounds\SouthEast\x = tx+nx+2 *Drawing\Bounds\SouthEast\y = ty+ny+2 -; Debug "------------------" + ; Debug "------------------" TileXY2LatLon(*Drawing\Bounds\NorthWest, @Degrees1, PBMap\Zoom) TileXY2LatLon(*Drawing\Bounds\SouthEast, @Degrees2, PBMap\Zoom) ;ensure we stay positive for the drawing @@ -793,7 +809,7 @@ Module PBMap Degrees1\Latitude = ny Degrees2\Longitude = nx1 Degrees2\Latitude = ny1 -; Debug "NW : " + StrD(Degrees1\Longitude) + " ; NE : " + StrD(Degrees2\Longitude) + ; Debug "NW : " + StrD(Degrees1\Longitude) + " ; NE : " + StrD(Degrees2\Longitude) LatLon2PixelRel(@Degrees1, @pos1, PBMap\Zoom) LatLon2PixelRel(@Degrees2, @pos2, PBMap\Zoom) VectorFont(FontID(PBMap\Font), 10) @@ -886,7 +902,7 @@ Module PBMap EndIf EndProcedure - Procedure DrawMarker(x.i, y.i, color.l = 0) + Procedure DrawMarker(x.i, y.i, color.l = 0) VectorSourceColor(color) MovePathCursor(x, y) AddPathLine(-8, -16, #PB_Path_Relative) @@ -909,7 +925,7 @@ Module PBMap PBMap\Marker()\CallBackPointer = CallBackPointer PBMap\Redraw = #True EndProcedure - + ; Draw all markers on the screen ! Procedure DrawMarkers(*Drawing.DrawingParameters) Protected Pixel.PixelCoordinates @@ -948,8 +964,8 @@ Module PBMap StartVectorDrawing(CanvasVectorOutput(PBMap\Gadget)) ;TODO add in layers of tiles ;this way we can cache them as 0 base 1.n layers ;such as for openseamap tiles which are overlaid. not that efficent from here though. - For a = 0 To PBMap\NumberOfMapLayers - 1 - DrawTiles(*Drawing, a) + ForEach PBMap\Layers() + DrawTiles(*Drawing, ListIndex(PBMap\Layers())) Next DrawTrack(*Drawing) DrawMarkers(*Drawing) @@ -1156,12 +1172,12 @@ Module PBMap EndProcedure Procedure.d GetLatitude() -; ProcedureReturn 0-(90-Mod((PBMap\GeographicCoordinates\Latitude+90),180)) + ; ProcedureReturn 0-(90-Mod((PBMap\GeographicCoordinates\Latitude+90),180)) ProcedureReturn PBMap\GeographicCoordinates\Latitude EndProcedure Procedure.d GetLongitude() -; ProcedureReturn 0-(180-Mod((PBMap\GeographicCoordinates\Longitude+180),360)) + ; ProcedureReturn 0-(180-Mod((PBMap\GeographicCoordinates\Longitude+180),360)) ProcedureReturn PBMap\GeographicCoordinates\Longitude EndProcedure @@ -1221,7 +1237,7 @@ Module PBMap Else ;New move values LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom) ;This line could be removed as the coordinates don't have to change but I want to be sure we rely only on geographic coordinates - ;Ensures that pixel position stay in the range [0..2^Zoom*PBMap\TileSize[ coz of the wrapping of the map + ;Ensures that pixel position stay in the range [0..2^Zoom*PBMap\TileSize[ coz of the wrapping of the map PBMap\PixelCoordinates\x - MouseX PBMap\PixelCoordinates\x = Mod(Mod(PBMap\PixelCoordinates\x, MapWidth) + MapWidth, MapWidth) PBMap\PixelCoordinates\y - MouseY @@ -1240,10 +1256,10 @@ Module PBMap If PBMap\EditMarkerIndex > -1 PBMap\EditMarkerIndex = -1 Else ;Move Map -; PBMap\Drawing\TileCoordinates\x = PBMap\PixelCoordinates\x / PBMap\TileSize -; PBMap\Drawing\TileCoordinates\y = PBMap\PixelCoordinates\y / PBMap\TileSize -; MyDebug("PBMap\Drawing\TileCoordinates\x " + Str(PBMap\Drawing\TileCoordinates\x) + " ; PBMap\Drawing\TileCoordinates\y " + Str(PBMap\Drawing\TileCoordinates\y) ) -; TileXY2LatLon(@PBMap\Drawing\TileCoordinates, @PBMap\GeographicCoordinates, PBMap\Zoom) + ; PBMap\Drawing\TileCoordinates\x = PBMap\PixelCoordinates\x / PBMap\TileSize + ; PBMap\Drawing\TileCoordinates\y = PBMap\PixelCoordinates\y / PBMap\TileSize + ; MyDebug("PBMap\Drawing\TileCoordinates\x " + Str(PBMap\Drawing\TileCoordinates\x) + " ; PBMap\Drawing\TileCoordinates\y " + Str(PBMap\Drawing\TileCoordinates\y) ) + ; TileXY2LatLon(@PBMap\Drawing\TileCoordinates, @PBMap\GeographicCoordinates, PBMap\Zoom) PBMap\Redraw = #True EndIf Case #PB_MAP_REDRAW @@ -1308,6 +1324,7 @@ CompilerIf #PB_Compiler_IsMainFile #String_1 #Gdt_LoadGpx #Gdt_AddMarker + #Gdt_AddOpenseaMap EndEnumeration Structure Location @@ -1316,8 +1333,8 @@ CompilerIf #PB_Compiler_IsMainFile EndStructure Procedure UpdateLocation(*Location.Location) -; SetGadgetText(#String_0, StrD(0-(90-Mod((*Location\Latitude+90),180)))) -; SetGadgetText(#String_1, StrD(0-(180-Mod((*Location\Longitude+180),360)))) + ; SetGadgetText(#String_0, StrD(0-(90-Mod((*Location\Latitude+90),180)))) + ; SetGadgetText(#String_1, StrD(0-(180-Mod((*Location\Longitude+180),360)))) SetGadgetText(#String_0, StrD(*Location\Latitude)) SetGadgetText(#String_1, StrD(*Location\Longitude)) ProcedureReturn 0 @@ -1356,6 +1373,7 @@ CompilerIf #PB_Compiler_IsMainFile ResizeGadget(#Text_4,WindowWidth(#Window_0)-170,#PB_Ignore,#PB_Ignore,#PB_Ignore) ResizeGadget(#Gdt_AddMarker,WindowWidth(#Window_0)-170,#PB_Ignore,#PB_Ignore,#PB_Ignore) ResizeGadget(#Gdt_LoadGpx,WindowWidth(#Window_0)-170,#PB_Ignore,#PB_Ignore,#PB_Ignore) + ResizeGadget(#Gdt_AddOpenseaMap,WindowWidth(#Window_0)-170,#PB_Ignore,#PB_Ignore,#PB_Ignore) PBMap::Refresh() EndProcedure @@ -1378,10 +1396,12 @@ CompilerIf #PB_Compiler_IsMainFile TextGadget(#Text_4, 530, 250, 60, 15, "Longitude : ") StringGadget(#String_1, 600, 250, 90, 20, "") ButtonGadget(#Gdt_AddMarker, 530, 280, 150, 30, "Add Marker") - ButtonGadget(#Gdt_LoadGpx, 530, 310, 150, 30, "Load GPX") + ButtonGadget(#Gdt_LoadGpx, 530, 310, 150, 30, "Load GPX") + ButtonGadget(#Gdt_AddOpenseaMap, 530, 340, 150, 30, "OpenSeaMap") Define Event.i, Gadget.i, Quit.b = #False Define pfValue.d + Define OpenSeaMap = 0 ;Our main gadget PBMap::InitPBMap(#Window_0) @@ -1390,7 +1410,6 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::SetCallBackLocation(@UpdateLocation()) PBMap::SetLocation(-36.81148, 175.08634,12) ;PBMap::SetLocation(0, 0) - PBMap::SetMapServer("http://t1.openseamap.org/seamark/") ;add a special osm overlay map PBMAP::SetMapScaleUnit(PBMAP::#SCALE_NAUTICAL) PBMap::AddMarker(49.0446828398, 2.0349812508, -1, @MyMarker()) @@ -1417,7 +1436,15 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::LoadGpxFile(OpenFileRequester("Choose a file to load", "", "*.gpx", 0)) PBMap::ZoomToArea() ; <-To center the view, and zoom on the tracks Case #Gdt_AddMarker - PBMap:: AddMarker(ValD(GetGadgetText(#String_0)), ValD(GetGadgetText(#String_1)), RGBA(Random(255), Random(255), Random(255), 255), @MyMarker()) + PBMap::AddMarker(ValD(GetGadgetText(#String_0)), ValD(GetGadgetText(#String_1)), RGBA(Random(255), Random(255), Random(255), 255), @MyMarker()) + Case #Gdt_AddOpenseaMap + If OpenSeaMap = 0 + OpenSeaMap = PBMap::AddMapServerLayer("OpenSeaMap", 2, "http://t1.openseamap.org/seamark/") ;add a special osm overlay map + Else + PBMap::DeleteLayer(OpenSeaMap) + OpenSeaMap = 0 + EndIf + PBMAP::Refresh() EndSelect Case #PB_Event_SizeWindow ResizeAll() @@ -1426,10 +1453,11 @@ CompilerIf #PB_Compiler_IsMainFile PBMap::Quit() EndIf - + CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 8 +; CursorPosition = 310 +; FirstLine = 291 ; Folding = ----------- ; EnableThread ; EnableXP From 0dc468fbbb1138b6960d8324914c10468a3ef78b Mon Sep 17 00:00:00 2001 From: djes Date: Fri, 9 Sep 2016 16:06:49 +0200 Subject: [PATCH 49/49] Cleanup --- PBMap.pb | 41 +++++------------------------------------ 1 file changed, 5 insertions(+), 36 deletions(-) diff --git a/PBMap.pb b/PBMap.pb index be6e992..113588d 100644 --- a/PBMap.pb +++ b/PBMap.pb @@ -460,9 +460,7 @@ Module PBMap EndIf *Pixel\y = PBMap\Drawing\CenterY + (py - PBMap\PixelCoordinates\y) EndProcedure - - - + ; HaversineAlgorithm ; http://andrew.hedges.name/experiments/haversine/ Procedure.d HaversineInKM(*posA.GeographicCoordinates, *posB.GeographicCoordinates) @@ -485,26 +483,17 @@ Module PBMap Protected mapWidth.l = Pow(2, Zoom + 8) Protected mapHeight.l = Pow(2, Zoom + 8) Protected x1.l,y1.l - ; get x value x1 = (*Location\Longitude+180)*(mapWidth/360) ; convert from degrees To radians Protected latRad.d = *Location\Latitude*#PI/180; Protected mercN.d = Log(Tan((#PI/4)+(latRad/2))); y1 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)) ; - ; Debug "location" - ; Debug x1 - ; Debug y1 Protected x2.l, y2.l - ; get x value x2 = (PBMap\GeographicCoordinates\Longitude+180)*(mapWidth/360) ; convert from degrees To radians latRad = PBMap\GeographicCoordinates\Latitude*#PI/180; - ; get y value mercN = Log(Tan((#PI/4)+(latRad/2))) y2 = (mapHeight/2)-(mapWidth*mercN/(2*#PI)); - ; Debug "GeographicCoordinates" - ; Debug x1 - ; Debug y1 *Pixel\x=GadgetWidth(PBMap\Gadget)/2 - (x2-x1) *Pixel\y=GadgetHeight(PBMap\Gadget)/2 - (y2-y1) EndProcedure @@ -538,11 +527,7 @@ Module PBMap Next EndIf EndProcedure - - ; Procedure LoadErrorHandler() - ; MessageRequester("Error", "") - ; EndProcedure - + Procedure.i GetTileFromHDD(CacheFile.s) Protected nImage.i If FileSize(CacheFile) > 0 @@ -579,16 +564,6 @@ Module PBMap If *Buffer nImage = CatchImage(#PB_Any, *Buffer, MemorySize(*Buffer)) If IsImage(nImage) - ; Debug "url: " + TileURL - ; Debug "cache file: " + CacheFile - ; timg = LoadImage(#PB_Any,CacheFile) - ; If timg - ; StartDrawing(ImageOutput(timg)) - ; DrawImage(ImageID(nimage)) - ; StopDrawing() - ; SaveImage(timg, CacheFile, #PB_ImagePlugin_PNG) - ; FreeImage(timg) - ; Else If SaveImage(nImage, CacheFile, #PB_ImagePlugin_PNG, 0, 32) MyDebug("Loaded from web " + TileURL + " as CacheFile " + CacheFile, 3) Else @@ -764,18 +739,15 @@ Module PBMap EndProcedure Procedure DrawScale(*Drawing.DrawingParameters,x,y,alpha=80) - ;TODO Add Option and function to display Scale on Map Protected sunit.s - Protected Scale.d= 40075*Cos(Radian(PBMap\GeographicCoordinates\Latitude))/Pow(2,PBMap\Zoom) / 2 - + Protected Scale.d= 40075*Cos(Radian(PBMap\GeographicCoordinates\Latitude))/Pow(2,PBMap\Zoom) / 2 Select PBMap\Options\ScaleUnit Case #SCALE_Nautical Scale * 0.539957 sunit = " Nm" Case #SCALE_KM; sunit = " Km" - EndSelect - + EndSelect VectorFont(FontID(PBMap\Font), 10) VectorSourceColor(RGBA(0, 0, 0,alpha)) MovePathCursor(x,y) @@ -1333,8 +1305,6 @@ CompilerIf #PB_Compiler_IsMainFile EndStructure Procedure UpdateLocation(*Location.Location) - ; SetGadgetText(#String_0, StrD(0-(90-Mod((*Location\Latitude+90),180)))) - ; SetGadgetText(#String_1, StrD(0-(180-Mod((*Location\Longitude+180),360)))) SetGadgetText(#String_0, StrD(*Location\Latitude)) SetGadgetText(#String_1, StrD(*Location\Longitude)) ProcedureReturn 0 @@ -1456,8 +1426,7 @@ CompilerIf #PB_Compiler_IsMainFile CompilerEndIf ; IDE Options = PureBasic 5.50 (Windows - x64) -; CursorPosition = 310 -; FirstLine = 291 +; CursorPosition = 1411 ; Folding = ----------- ; EnableThread ; EnableXP