1 why can't I get the imagewidth/height in that function?
Since a TImage contains .width and .height fields you can do this:
Print a.image.width
2 can I somehow automate that .ev thing in the mainloop? So that upon creation of a gadget like this, things happen automatically?
See new example here. I've removed width/height fields and replaced them with image.width etc..
Also, I added 'IButton.Ev()'. This calls a function in the IButton type which iterates through a TList (called IButtonList). The list contains references to all the buttons created. Every time you add a new button it is automatically added to the list thanks to the New() method (also present in the IButton type):
'<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
Global IButtonList:TList=New TList
Type IButton
Field canvas:TGadget
Field image:Timage
Field state:Byte
Field ID:String
Rem
state:
0 off, normal
1 off, mousemove
2 on, normal
3 on, mousemove
EndRem
Function ev()
For Local ib:IButton=EachIn IButtonList
If EventSource()=ib.canvas
If EventID()=EVENT_MOUSEENTER
ib.state:+1
ib.update()
EndIf
If EventID()=EVENT_MOUSELEAVE
ib.state:-1
ib.update()
EndIf
If EventID()=EVENT_GADGETPAINT
ib.update()
EndIf
If EventID()=EVENT_MOUSEDOWN
If EventData()=1
If ib.state<2
ib.state:+2
Else
ib.state:-2
EndIf
ib.update()
EndIf
EndIf
EndIf
Next
End Function
Method New()
IButtonList.AddLast Self
End Method
Method update()
SetGraphics CanvasGraphics(canvas)
DrawImage image,-state*(image.width/4),0
Flip
End Method
End Type
Function CreateIButton:IButton(x:Short, y:Short, ID$, img:Timage, parent:Tgadget, state:Byte=0)
Local a:IButton=New IButton
a.canvas=CreateCanvas(x,y,img.width/4,img.height,parent)
a.image=img
a.state=state
Return a
End Function
'<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
'<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
Local testimage:Timage=CreateImage(32*4,32)
Local x,y,r,g,b,i:Byte
Local map:TPixmap=LockImage(testimage)
For i=0 To 3
For y=0 To 31
For x=0 To 31
r=Rnd(0,i*32+31)
g=Rnd(0,i*32+31)
b=Rnd(0,i*32+31)
WritePixel map,i*32+x,y,$ff000000|b+256*g+65536*r
Next
Next
Next
UnlockImage testimage
map=Null
Local window:Tgadget=CreateWindow("^_^",32,32,640,480)
'Local ib.IButton
Local ib1:IButton=CreateIButton(4,4,"dog",testimage,window,0)
Local ib2:IButton=CreateIButton(104,4,"cat",testimage,window,0)
Local ib3:IButton=CreateIButton(50,40,"mouse",testimage,window,0)
OnEnd Quit
Repeat
WaitEvent()
If EventID()=EVENT_WINDOWCLOSE End
IButton.Ev()
Forever
Function Quit()
End Function