I have a requirement to be able to re-organise a number of "tiles" on a canvas, while my routine works well horizontally, vertically is swaps the blocks and really I would want to insert the block and push the tiles along.
I can't seem to think of a way to do this, any ideas? Example here (its ripped from my code so may be a couple of redundant lines that can be ignored) :
Code: Select all
Global Window_1
Global Canvas_TileDisplay
Global Dim imagetiles(256)
Procedure OpenWindow_1(x = 0, y = 0, width = 600, height = 400)
Window_1 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_WindowCentered)
Canvas_TileDisplay = CanvasGadget(#PB_Any, 10, 10, 580, 190)
EndProcedure
Procedure CheckTileDisplayEvents(Type)
Static startx, starty, firsttile, moveddone, movex, movey, stayx, stayy
offya = GetGadgetAttribute(Canvas_TileDisplay, #PB_Canvas_MouseY) /16 ; get mouse position
offxa = GetGadgetAttribute(Canvas_TileDisplay, #PB_Canvas_MouseX) /16
If offxa <32 And offxa >=0 And offya >= 0 And offya < 8
If Type=#PB_EventType_LeftButtonDown
startx = offxa : starty = offya
StartDrawing(CanvasOutput(Canvas_TileDisplay))
DrawText(520,10,"Tile : "+RSet(Str(CurrentEditTile),3,"0")+" / $"+RSet(Hex((CurrentEditTile),#PB_Byte),2,"0")+" ")
StopDrawing()
firsttile = (starty*32+startx)
EndIf
If (Type = #PB_EventType_MouseMove And GetGadgetAttribute(Canvas_TileDisplay, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
If (startx<>offxa) Or (starty <> offya)
Debug Str(startx)+" "+Str(starty)
Debug Str(offxa)+" "+Str(offya)
firsttile = (starty*32+startx)
secondtile = (offya*32+offxa)
tmp = imagetiles(firsttile) ; get the first tile id
imagetiles(firsttile)=imagetiles(secondtile) ; set the first id to secondtile
imagetiles(secondtile)=tmp ; put first tile into second
StartDrawing(CanvasOutput(Canvas_TileDisplay))
x = 0 : y = 0 : tile = 0
For tiles = 0 To 255
DrawImage(ImageID(imagetiles(tiles)),x,y,16,16)
x=x+16 : If x=512 : x = 0 : y+16 : EndIf
Next tiles
StopDrawing()
startx = offxa : starty = offya
EndIf
EndIf
EndIf
EndProcedure
OpenWindow_1()
; make some test images
For x = 0 To 255
imagetiles(x)=CreateImage(#PB_Any,16,16)
StartDrawing(ImageOutput(imagetiles(x)))
Box(0,0,16,16,RGB(Random(255,0),Random(255,0),Random(255,0)))
StopDrawing()
Next
StartDrawing(CanvasOutput(Canvas_TileDisplay))
x = 0 : y = 0 : tile = 0
For tiles = 0 To 255
DrawImage(ImageID(imagetiles(tiles)),x,y,16,16)
x=x+16 : If x=512 : x = 0 : y+16 : EndIf
Next tiles
StopDrawing()
Repeat
event=WaitWindowEvent()
Select event
Case #PB_Event_CloseWindow
quit=1
Case #PB_Event_Menu
Select EventMenu()
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
Case Canvas_TileDisplay
Type=EventType()
CheckTileDisplayEvents(Type)
EndSelect
EndSelect
Until quit=1