I'd say that the code could be organised somewhat better ..
Anyway.. try to study this map-editor example below.. I think I broke my record in fast-map-editor-coding :)
It doesn't save the maps, but that's easy to implement..
; miniminiminiminiminiminiminiminiminiminiminiminiminiminimap-editor - by CS^TBL
app=CreateWindow("blah",0,0,640,480)
Global c1=CreateCanvas(8,8,32,256,app)
Global c2=CreateCanvas(56,8,512,256,app)
Global img,currentpalette=0,maplmd=False
Dim map(32,16)
loadimg=CreateButton("IMG",8,8+256+4,32,24,app)
UpdatePalette(True)
quit=False
;------------------------------------------------------------------------
Repeat
WaitEvent()
If EventID()=$803 quit=True ; window-close / alt-f4
If EventID()=$401 ; generic gadget action.. like a buttonpress
If EventSource()=loadimg
; request a file
whichfile$=RequestFile$("load image..","jpg")
If whichfile$<>"" ; did we succeed?
If img FreeImage img ; first get rid of the old image
img=LoadImage(whichfile$)
UpdatePalette(True)
EndIf
EndIf
EndIf
If EventSource()=c1 ; the palette? ------------------------------------------------
If EventID()=$203 ; mousemove
x=EventX()/16
y=EventY()/16
UpdatePalette(False)
Color 255,255,255:Rect x*16,y*16,16,16,False
Color 0,0,0:Rect 1+x*16,1+y*16,14,14,False
FlipCanvas c1
EndIf
If EventID()=$206 ; mouseleave
UpdatePalette(True)
EndIf
If EventID()=$201 ; mouseclick
x=EventX()/16
y=EventY()/16
currentpalette=(x Mod 2)+y*2
UpdatePalette(True)
EndIf
EndIf
If EventSource()=c2 ; the map? ------------------------------------------------
If EventID()=$203 ; mousemove
x=EventX()/16
y=EventY()/16
UpdateMap(False)
Color 255,255,255:Rect x*16,y*16,16,16,False
Color 0,0,0:Rect 1+x*16,1+y*16,14,14,False
FlipCanvas c2
EndIf
If EventID()=$206 ; mouseleave
UpdateMap(True)
EndIf
If EventID()=$201 ; mouseclick
maplmd=True
EndIf
If EventID()=$202 ; mouseclick
maplmd=False
EndIf
If maplmd
x=EventX()/16
y=EventY()/16
If x<0 x=0 ; make sure you clip! otherwise you'll get errors!
If y<0 y=0
If x>31 x=31
If y>15 y=15
map(x,y)=currentpalette
UpdateMap(False)
Color 255,255,255:Rect x*16,y*16,16,16,False
Color 0,0,0:Rect 1+x*16,1+y*16,14,14,False
FlipCanvas c2
EndIf
EndIf
Until quit
FreeImage img
End
;------------------------------------------------------------------------
Function UpdatePalette(swap)
SetBuffer CanvasBuffer(c1)
Cls
If img DrawBlock img,0,0
x=currentpalette Mod 2
y=currentpalette /2
Color 255,255,255:Rect x*16,y*16,16,16,False
Color 0,0,0:Rect 1+x*16,1+y*16,14,14,False
If swap FlipCanvas c1
End Function
;------------------------------------------------------------------------
Function UpdateMap(swap)
SetBuffer CanvasBuffer(c2)
Cls
For y=0 To 15
For x=0 To 31
tile=map(x,y)
px=tile Mod 2
py=tile / 2
If img DrawBlockRect img,x*16,y*16,px*16,py*16,16,16
Next
Next
If swap FlipCanvas c2
End Function