New markers select/move

This commit is contained in:
djes
2016-09-13 12:08:08 +02:00
parent 970959bd51
commit f89476609f

152
PBMap.pb
View File

@@ -119,7 +119,7 @@ Module PBMap
Dirty.i Dirty.i
End.i End.i
EndStructure EndStructure
Structure ImgMemCach Structure ImgMemCach
nImage.i nImage.i
*Tile.Tile *Tile.Tile
@@ -137,7 +137,8 @@ Module PBMap
Structure Marker Structure Marker
GeographicCoordinates.GeographicCoordinates ; Marker latitude and longitude GeographicCoordinates.GeographicCoordinates ; Marker latitude and longitude
color.l ; Marker color Color.l ; Marker color
Selected.i ; Is the marker selected ?
CallBackPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) CallBackPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib)
EndStructure EndStructure
@@ -163,7 +164,7 @@ Module PBMap
Name.s Name.s
ServerURL.s ; Web URL ex: http://tile.openstreetmap.org/ ServerURL.s ; Web URL ex: http://tile.openstreetmap.org/
EndStructure EndStructure
;-PBMap Structure ;-PBMap Structure
Structure PBMap Structure PBMap
Window.i ; Parent Window Window.i ; Parent Window
@@ -173,34 +174,34 @@ Module PBMap
GeographicCoordinates.GeographicCoordinates ; Latitude and Longitude from focus point GeographicCoordinates.GeographicCoordinates ; Latitude and Longitude from focus point
Drawing.DrawingParameters ; Drawing parameters based on focus point Drawing.DrawingParameters ; Drawing parameters based on focus point
CallBackLocation.i ; @Procedure(latitude.d,lontitude.d) CallBackLocation.i ; @Procedure(latitude.d,lontitude.d)
CallBackMainPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib) CallBackMainPointer.i ; @Procedure(X.i, Y.i) to DrawPointer (you must use VectorDrawing lib)
PixelCoordinates.PixelCoordinates ; Actual focus point coords in pixels (global) PixelCoordinates.PixelCoordinates ; Actual focus point coords in pixels (global)
MoveStartingPoint.PixelCoordinates ; Start mouse position coords when dragging the map MoveStartingPoint.PixelCoordinates ; Start mouse position coords when dragging the map
List Layers.Layer() ; List Layers.Layer() ;
ZoomMin.i ; Min Zoom supported by server ZoomMin.i ; Min Zoom supported by server
ZoomMax.i ; Max Zoom supported by server ZoomMax.i ; Max Zoom supported by server
Zoom.i ; Current zoom Zoom.i ; Current zoom
TileSize.i ; Tile size downloaded on the server ex : 256 TileSize.i ; Tile size downloaded on the server ex : 256
MemCache.TileMemCach ; Images in memory cache MemCache.TileMemCach ; Images in memory cache
Mode.i ; User mode : 0 (default)->hand (moving map) and select markers, 1->hand, 2->select only (moving objects), 3->drawing (todo) Mode.i ; User mode : 0 (default)->hand (moving map) and select markers, 1->hand, 2->select only (moving objects), 3->drawing (todo)
Redraw.i Redraw.i
Moving.i Moving.i
Dirty.i ; To signal that drawing need a refresh Dirty.i ; To signal that drawing need a refresh
List track.GeographicCoordinates() ; To display a GPX track List track.GeographicCoordinates() ; To display a GPX track
List Marker.Marker() ; To diplay marker List Marker.Marker() ; To diplay marker
EditMarkerIndex.l EditMarker.l
ImgLoading.i ; Image Loading Tile ImgLoading.i ; Image Loading Tile
ImgNothing.i ; Image Nothing Tile ImgNothing.i ; Image Nothing Tile
Options.option ; Options Options.option ; Options
EndStructure EndStructure
@@ -315,18 +316,18 @@ Module PBMap
EndIf EndIf
EndProcedure EndProcedure
;TODO : best cleaning of the string from bad behaviour ;TODO : best cleaning of the string from bad behaviour
Procedure.s StringCheck(String.s) Procedure.s StringCheck(String.s)
ProcedureReturn Trim(RemoveString(RemoveString(RemoveString(RemoveString(RemoveString(RemoveString(RemoveString(RemoveString(RemoveString(RemoveString(String, Chr(0)), Chr(32)), Chr(39)), Chr(33)), Chr(34)), "@"), "/"), "\"), "$"), "%")) ProcedureReturn Trim(RemoveString(RemoveString(RemoveString(RemoveString(RemoveString(RemoveString(RemoveString(RemoveString(RemoveString(RemoveString(String, Chr(0)), Chr(32)), Chr(39)), Chr(33)), Chr(34)), "@"), "/"), "\"), "$"), "%"))
EndProcedure EndProcedure
Macro SelBool(Name) Macro SelBool(Name)
Select UCase(Value) Select UCase(Value)
Case "0", "FALSE", "DISABLE" Case "0", "FALSE", "DISABLE"
PBMap\Options\Name = #False PBMap\Options\Name = #False
Default Default
PBMap\Options\Name = #True PBMap\Options\Name = #True
EndSelect EndSelect
EndMacro EndMacro
Procedure SetOption(Option.s, Value.s) Procedure SetOption(Option.s, Value.s)
@@ -409,7 +410,7 @@ Module PBMap
EndIf EndIf
PreferenceGroup("URL") PreferenceGroup("URL")
PBMap\Options\DefaultOSMServer = ReadPreferenceString("DefaultOSMServer", "http://tile.openstreetmap.org/") PBMap\Options\DefaultOSMServer = ReadPreferenceString("DefaultOSMServer", "http://tile.openstreetmap.org/")
PreferenceGroup("PATHS") PreferenceGroup("PATHS")
PBMap\Options\HDDCachePath = ReadPreferenceString("TilesCachePath", GetTemporaryDirectory()) PBMap\Options\HDDCachePath = ReadPreferenceString("TilesCachePath", GetTemporaryDirectory())
PreferenceGroup("OPTIONS") PreferenceGroup("OPTIONS")
@@ -418,7 +419,7 @@ Module PBMap
PBMap\Options\TimerInterval = 20 PBMap\Options\TimerInterval = 20
ClosePreferences() ClosePreferences()
EndProcedure EndProcedure
Procedure InitPBMap(Window) Procedure InitPBMap(Window)
Protected Result.i Protected Result.i
If Verbose If Verbose
@@ -429,8 +430,7 @@ Module PBMap
PBMap\MoveStartingPoint\x = - 1 PBMap\MoveStartingPoint\x = - 1
PBMap\TileSize = 256 PBMap\TileSize = 256
PBMap\Dirty = #False PBMap\Dirty = #False
;PBMap\TileThreadMutex = CreateMutex() PBMap\EditMarker = #False
PBMap\EditMarkerIndex = -1 ;Initialised with "no marker selected"
PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold) PBMap\Font = LoadFont(#PB_Any, "Arial", 20, #PB_Font_Bold)
PBMap\Window = Window PBMap\Window = Window
PBMap\Timer = 1 PBMap\Timer = 1
@@ -571,7 +571,7 @@ Module PBMap
EndIf EndIf
*Pixel\y = PBMap\Drawing\CenterY + (py - PBMap\PixelCoordinates\y) *Pixel\y = PBMap\Drawing\CenterY + (py - PBMap\PixelCoordinates\y)
EndProcedure EndProcedure
; HaversineAlgorithm ; HaversineAlgorithm
; http://andrew.hedges.name/experiments/haversine/ ; http://andrew.hedges.name/experiments/haversine/
Procedure.d HaversineInKM(*posA.GeographicCoordinates, *posB.GeographicCoordinates) Procedure.d HaversineInKM(*posA.GeographicCoordinates, *posB.GeographicCoordinates)
@@ -1030,7 +1030,7 @@ Module PBMap
EndIf EndIf
EndProcedure EndProcedure
Procedure DrawMarker(x.i, y.i, color.l = 0) Procedure DrawMarker(x.i, y.i, color.l = 0, Selected = #False)
VectorSourceColor(color) VectorSourceColor(color)
MovePathCursor(x, y) MovePathCursor(x, y)
AddPathLine(-8, -16, #PB_Path_Relative) AddPathLine(-8, -16, #PB_Path_Relative)
@@ -1040,8 +1040,14 @@ Module PBMap
;ClipPath(#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) VectorSourceColor(color)
FillPath(#PB_Path_Preserve):VectorSourceColor(color);RGBA(0, 0, 0, 255)) FillPath(#PB_Path_Preserve)
StrokePath(1) If Selected
VectorSourceColor(RGBA(255, 255, 0, 255))
StrokePath(3)
Else
VectorSourceColor(color)
StrokePath(1)
EndIf
EndProcedure EndProcedure
; Add a Marker To the Map ; Add a Marker To the Map
@@ -1055,7 +1061,7 @@ Module PBMap
EndProcedure EndProcedure
; Draw all markers on the screen ! ; Draw all markers on the screen !
Procedure DrawMarkers(*Drawing.DrawingParameters) Procedure DrawMarkers(*Drawing.DrawingParameters)
Protected Pixel.PixelCoordinates Protected Pixel.PixelCoordinates
ForEach PBMap\Marker() ForEach PBMap\Marker()
If PBMap\Marker()\GeographicCoordinates\Latitude <> 0 And PBMap\Marker()\GeographicCoordinates\Longitude <> 0 If PBMap\Marker()\GeographicCoordinates\Latitude <> 0 And PBMap\Marker()\GeographicCoordinates\Longitude <> 0
@@ -1063,9 +1069,9 @@ Module PBMap
LatLon2PixelRel(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 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 If PBMap\Marker()\CallBackPointer > 0
CallFunctionFast(PBMap\Marker()\CallBackPointer, Pixel\X, Pixel\Y) CallFunctionFast(PBMap\Marker()\CallBackPointer, Pixel\X, Pixel\Y, PBMap\Marker()\Selected)
Else Else
DrawMarker(Pixel\X, Pixel\Y, PBMap\Marker()\color) DrawMarker(Pixel\X, Pixel\Y, PBMap\Marker()\Color, PBMap\Marker()\Selected)
EndIf EndIf
EndIf EndIf
EndIf EndIf
@@ -1330,7 +1336,7 @@ Module PBMap
Procedure CanvasEvents() Procedure CanvasEvents()
Protected MouseX.i, MouseY.i Protected MouseX.i, MouseY.i
Protected MarkerCoords.Coordinates, *Tile.Tile, MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize Protected MarkerCoords.PixelCoordinates, *Tile.Tile, MapWidth = Pow(2, PBMap\Zoom) * PBMap\TileSize
Protected key.s Protected key.s
PBMap\Moving = #False PBMap\Moving = #False
Select EventType() Select EventType()
@@ -1353,12 +1359,8 @@ Module PBMap
If PBMap\Mode = #MODE_DEFAULT Or PBMap\Mode = #MODE_SELECT If PBMap\Mode = #MODE_DEFAULT Or PBMap\Mode = #MODE_SELECT
;Check if we select a marker ;Check if we select a marker
ForEach PBMap\Marker() ForEach PBMap\Marker()
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 If PBMap\Marker()\Selected
MarkerCoords\x * PBMap\TileSize PBMap\EditMarker = #True;ListIndex(PBMap\Marker())
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 Break
EndIf EndIf
Next Next
@@ -1371,15 +1373,19 @@ Module PBMap
If PBMap\MoveStartingPoint\x <> - 1 If PBMap\MoveStartingPoint\x <> - 1
MouseX = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) - PBMap\MoveStartingPoint\x MouseX = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) - PBMap\MoveStartingPoint\x
MouseY = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) - PBMap\MoveStartingPoint\y MouseY = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) - PBMap\MoveStartingPoint\y
;Move marker ;Move selected markers
If PBMap\EditMarkerIndex > -1 And (PBMap\Mode = #MODE_DEFAULT Or PBMap\Mode = #MODE_SELECT) If PBMap\EditMarker And (PBMap\Mode = #MODE_DEFAULT Or PBMap\Mode = #MODE_SELECT)
SelectElement(PBMap\Marker(), PBMap\EditMarkerIndex) ForEach PBMap\Marker()
LatLon2TileXY(@PBMap\Marker()\GeographicCoordinates, @MarkerCoords, PBMap\Zoom) If PBMap\Marker()\Selected
MarkerCoords\x + MouseX / PBMap\TileSize LatLon2Pixel(@PBMap\Marker()\GeographicCoordinates, @MarkerCoords, PBMap\Zoom)
MarkerCoords\y + MouseY / PBMap\TileSize MarkerCoords\x + MouseX
TileXY2LatLon(@MarkerCoords, @PBMap\Marker()\GeographicCoordinates, PBMap\Zoom) MarkerCoords\y + MouseY
Pixel2LatLon(@MarkerCoords, @PBMap\Marker()\GeographicCoordinates, PBMap\Zoom)
Break ;Only one marker moved at once
EndIf
Next
ElseIf PBMap\Mode = #MODE_DEFAULT Or PBMap\Mode = #MODE_HAND ElseIf PBMap\Mode = #MODE_DEFAULT Or PBMap\Mode = #MODE_HAND
;New move values ;Move map only
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 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 - MouseX
@@ -1394,18 +1400,35 @@ Module PBMap
PBMap\Redraw = #True PBMap\Redraw = #True
PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX) PBMap\MoveStartingPoint\x = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX)
PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY) PBMap\MoveStartingPoint\y = GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY)
EndIf Else
Case #PB_EventType_LeftButtonUp LatLon2Pixel(@PBMap\GeographicCoordinates, @PBMap\PixelCoordinates, PBMap\Zoom)
PBMap\MoveStartingPoint\x = - 1 MouseX = PBMap\PixelCoordinates\x - GadgetWidth(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseX)
If PBMap\EditMarkerIndex > -1 MouseY = PBMap\PixelCoordinates\y - GadgetHeight(PBMap\Gadget) / 2 + GetGadgetAttribute(PBMap\Gadget, #PB_Canvas_MouseY)
PBMap\EditMarkerIndex = -1 ;Clip MouseX to the map range (in X, the map is infinite)
Else ;Move Map MouseX = Mod(Mod(MouseX, MapWidth) + MapWidth, MapWidth)
; PBMap\Drawing\TileCoordinates\x = PBMap\PixelCoordinates\x / PBMap\TileSize If PBMap\Mode = #MODE_DEFAULT Or PBMap\Mode = #MODE_SELECT
; PBMap\Drawing\TileCoordinates\y = PBMap\PixelCoordinates\y / PBMap\TileSize ;Check if mouse touch markers
; MyDebug("PBMap\Drawing\TileCoordinates\x " + Str(PBMap\Drawing\TileCoordinates\x) + " ; PBMap\Drawing\TileCoordinates\y " + Str(PBMap\Drawing\TileCoordinates\y) ) ForEach PBMap\Marker()
; TileXY2LatLon(@PBMap\Drawing\TileCoordinates, @PBMap\GeographicCoordinates, PBMap\Zoom) LatLon2Pixel(@PBMap\Marker()\GeographicCoordinates, @MarkerCoords, PBMap\Zoom)
If Distance(MarkerCoords\x, MarkerCoords\y, MouseX, MouseY) < 8
PBMap\Marker()\Selected = #True
Else
PBMap\Marker()\Selected = #False
EndIf
Next
EndIf
PBMap\Redraw = #True PBMap\Redraw = #True
EndIf EndIf
Case #PB_EventType_LeftButtonUp
PBMap\MoveStartingPoint\x = - 1
If PBMap\EditMarker
PBMap\EditMarker = #False
;Deselect markers
ForEach PBMap\Marker()
PBMap\Marker()\Selected = #False
Next
EndIf
PBMap\Redraw = #True
Case #PB_MAP_REDRAW Case #PB_MAP_REDRAW
Debug "Redraw" Debug "Redraw"
PBMap\Redraw = #True PBMap\Redraw = #True
@@ -1484,16 +1507,21 @@ CompilerIf #PB_Compiler_IsMainFile
ProcedureReturn 0 ProcedureReturn 0
EndProcedure EndProcedure
Procedure MyMarker(x.i, y.i) Procedure MyMarker(x.i, y.i, Selected = #False)
Protected color.l Protected color = RGBA(0, 255, 0, 255)
color=RGBA(0, 255, 0, 255)
VectorSourceColor(color)
MovePathCursor(x, y) MovePathCursor(x, y)
AddPathLine(-16,-32,#PB_Path_Relative) AddPathLine(-16,-32,#PB_Path_Relative)
AddPathCircle(16,0,16,180,0,#PB_Path_Relative) AddPathCircle(16,0,16,180,0,#PB_Path_Relative)
AddPathLine(-16,32,#PB_Path_Relative) AddPathLine(-16,32,#PB_Path_Relative)
VectorSourceColor(color) VectorSourceColor(color)
FillPath(#PB_Path_Preserve):VectorSourceColor(RGBA(0, 0, 0, 255)):StrokePath(1) FillPath(#PB_Path_Preserve)
If Selected
VectorSourceColor(RGBA($FF, $FF, 0, $FF))
StrokePath(2)
Else
VectorSourceColor(RGBA(0, 0, 0, 255))
StrokePath(1)
EndIf
EndProcedure EndProcedure
Procedure MainPointer(x.i, y.i) Procedure MainPointer(x.i, y.i)
@@ -1559,7 +1587,7 @@ CompilerIf #PB_Compiler_IsMainFile
PBMap::SetCallBackLocation(@UpdateLocation()) ; To obtain realtime coordinates PBMap::SetCallBackLocation(@UpdateLocation()) ; To obtain realtime coordinates
PBMap::SetLocation(-36.81148, 175.08634,12) ; Change the PBMap coordinates PBMap::SetLocation(-36.81148, 175.08634,12) ; Change the PBMap coordinates
PBMAP::SetMapScaleUnit(PBMAP::#SCALE_NAUTICAL) ; To change the scale unit PBMAP::SetMapScaleUnit(PBMAP::#SCALE_NAUTICAL) ; To change the scale unit
PBMap::AddMarker(49.0446828398, 2.0349812508, -1, @MyMarker()) ; To add a marker PBMap::AddMarker(49.0446828398, 2.0349812508, -1, @MyMarker()) ; To add a marker with a customised GFX
Repeat Repeat
Event = WaitWindowEvent() Event = WaitWindowEvent()
@@ -1608,8 +1636,8 @@ CompilerIf #PB_Compiler_IsMainFile
CompilerEndIf CompilerEndIf
; IDE Options = PureBasic 5.50 (Windows - x64) ; IDE Options = PureBasic 5.50 (Windows - x64)
; CursorPosition = 134 ; CursorPosition = 438
; FirstLine = 113 ; FirstLine = 422
; Folding = ------------ ; Folding = ------------
; EnableThread ; EnableThread
; EnableXP ; EnableXP