Ok, I spend some hour or so on this thingy.. I think it's way more than you ever need, but it's here anyway.
The only thing you need to do is copy/paste the CreateLine function and the 3 small functions at the bottom to some include file and there you go! That's all folks..
Notice that I don't read out personal windows GUI colors. (too lazy :)
Have fun!
; Line Gadget v1.0
;
; by CS^TBL - March 2004
;
; CreateLine(x, y, length, direction, thickness, parent, [contrast#], [r], [g],[b])
;
; default values: contrast# 1.2 (range 0..2) (1=no relief, 0 and 2 = opposite maximum relief)
; r 192 (range 0..255)
; g 192 (range 0..255)
; b 192 (range 0..255)
app=CreateWindow("line gadget",0,0,640,480)
MyLine1=CreateLine(32,32,128,1,2,app,1.2,192,192,192)
MyLine2=CreateLine(40,32,160,0,3,app,0.8)
; ------------------------------------------------------------------------------
; examples:
; read out the line lengths
Notify GetLineLength(MyLine1)
Notify GetLineLength(MyLine2)
; read out a canvas from a line and make it red
c=GetLineCanvas(MyLine1)
SetBuffer CanvasBuffer(c)
ClsColor 255,0,0:Cls
FlipCanvas c
; ------------------------------------------------------------------------------
; main loop
quit=False
Repeat
WaitEvent()
If EventID()=$803 quit=True
Until quit
; and clean up your mess again..
FreeBank MyLine1
FreeBank MyLine2
FreeGadget app
End ; bye!
; ------------------------------------------------------------------------------
; typical examples to read a property from the line gadget
;
; notice that the only check is whether it 'is' a bank.. it doesn't check whether it's the correct bank!
; if you read out the wrong bank (esp. non-line 'gadgets' :) then really odd things might happen..
; If the bank is not big enough then it'll crash probably :)
;
; you can solve this by expanding the whole bank structure with 4 bytes, move all data 4 bytes forward,
; and use the first 4 bytes to add an ID .. "Line" or something, in each Get or Set function you should check
; on this ID, if it doesn't match, then use the Break command .
;
; also, you might want to split the CreateLine function in a DefineLine and DrawLine function, since you might have
; to draw to the canvas again. (for example when you make typical 'Set' functions!)
;
; For beginners, this function should be enough anyway, and it's prolly overkill already :)
Function GetLineLength(bank)
If bank
Return PeekShort (bank,08)
EndIf
Break"Bank error"
End Function
Function GetLineCanvas(bank)
If bank
Return PeekInt (bank,00)
EndIf
Break"Bank error"
End Function
; ------------------------------------------------------------------------------
; the actual function..
Function CreateLine(x,y,length,direction,thickness,parent,contrast#=1.2,r=192,g=192,b=192)
; 00 canvas int (4 bytes)
; 04 x short (2 bytes)
; 06 y short (2 bytes)
; 08 length short (2 bytes)
; 10 direction byte (1 byte)
; 11 thickness byte (1 byte)
; 12 parent int (4 bytes)
; 16 contrast float (8 bytes)
; 24 r byte (1 byte)
; 25 g byte (1 byte)
; 26 b byte (1 byte)
; checks:
If Not parent Break"Wrong parent!"
If Not thickness Break"Thickness must be larger than 0"
If direction<0
direction=0
Notify"Warning: direction must be 0 or 1, value is clipped"
EndIf
If direction>1
direction=1
Notify"Warning: direction must be 0 or 1, value is clipped"
EndIf
r=Limit(r,0,255)
g=Limit(g,0,255)
b=Limit(b,0,255)
; create:
bank=CreateBank(27)
If direction=0 ; horizontal
canvas=CreateCanvas(x,y,length,thickness,parent)
Else ; vertical
canvas=CreateCanvas(x,y,thickness,length,parent)
EndIf
SetBuffer CanvasBuffer(canvas)
ClsColor r,g,b:Cls
r1#=r*contrast#
r2#=r*(1-(contrast#-1))
g1#=g*contrast#
g2#=g*(1-(contrast#-1))
b1#=b*contrast#
b2#=b*(1-(contrast#-1))
r1#=LimitF(r1#,0,255)
g1#=LimitF(g1#,0,255)
b1#=LimitF(b1#,0,255)
r2#=LimitF(r2#,0,255)
g2#=LimitF(g2#,0,255)
b2#=LimitF(b2#,0,255)
; notice: if the thickness is 1 then it's obvious that only the '2nd' line will be shown!
If direction=0
Color r1#,g1#,b1#
Line 0,0,length,0
Color r2#,g2#,b2#
Line 0,thickness-1,length-1,thickness-1
Else
Color r1#,g1#,b1#
Line 0,0,0,length
Color r2#,g2#,b2#
Line thickness-1,0,thickness-1,length-1
EndIf
FlipCanvas canvas
; poke stuff in the bank
PokeInt bank,00,canvas
PokeShort bank,04,x
PokeShort bank,06,y
PokeShort bank,08,length
PokeByte bank,10,direction
PokeByte bank,11,thickness
PokeInt bank,12,parent
PokeFloat bank,16,contrast#
PokeByte bank,24,r
PokeByte bank,25,g
PokeByte bank,26,b
; return the bankhandle
Return bank
End Function ; byebye!
; ------------------------------------------------------------------------------
; Additional handy stuff for the lazy coder :)
;
; TIP:
; add these lines to your usual .decls file
;
; CreateLine(x,y,length,direction,thickness,parent,contrast#,r,g,b)
; Limit(value,Min,Max)
; LimitF(value#,Min#,Max#)
; Break(msg$)
;
; additionally you could add the Get commands, but I don't think you'll ever need them..
Function Limit(value,minvalue,maxvalue)
If value<minvalue Return minvalue
If value>maxvalue Return maxvalue
Return value
End Function
Function LimitF(value#,minvalue#,maxvalue#)
If value#<minvalue# Return minvalue#
If value#>maxvalue# Return maxvalue#
Return value#
End Function
Function Break(s$)
Notify s$
End
End Function