Sorry to get on your nerves... :(
1.
If I want to "disable" a unitbar:
So that it only displays a certain vaule, but the user isn't allowed to change that.
How would I do that? The unitbar keeps on getting updated.
2.
*solved*
3.
These hooks only work on windows, right?
Updated code:
' Unitbar Example 2
Const EVENT_UNITBARCHANGE:Int= $13370400
Const EVENT_UNITBARCHANGED:Int= $13370401
Type TUnitbar
Rem
TUnitbar
CS_TBL
Usage:
MyUnitbar:TUnitbar=CreateUnitbar:TUnitbar(x,y,unitsize, units, height, parent)
EndRem
Field canvas:TGadget
Field r:Int[]
Field g:Int[]
Field b:Int[]
Field unitwidth:Int
Field units:Int
Field value:Int
Field number:Int
Field lmd:Int
Field NewEvent:TEvent=New TEvent
Function eventhook:Object(id:Int,data:Object,context:Object)
If TUnitbar(context) TUnitbar(context).ev TEvent(data);Return data
EndFunction
Method New()
AddHook EmitEventHook,eventhook,Self
End Method
Method Free()
RemoveHook EmitEventHook,eventhook
GCCollect()
End Method
Method ev(event:TEvent)
If event.source=canvas
If event.id=EVENT_GADGETPAINT update
If event.id=EVENT_MOUSEDOWN
lmd=1
EndIf
If event.id=EVENT_MOUSEUP
lmd=0
NewEvent.id=EVENT_UNITBARCHANGED
NewEvent.source=Self
NewEvent.data=value
EmitEvent NewEvent
EndIf
If lmd
value=_Limit((EventX()-(number*12))/unitwidth,0,units-1)
update
NewEvent.id=EVENT_UNITBARCHANGE
NewEvent.source=Self
NewEvent.data=value
EmitEvent NewEvent
EndIf
If event.id=EVENT_MOUSEENTER
ActivateGadget canvas
EndIf
EndIf
End Method
Method _Limit(v:Int,l:Int,h:Int)
If v<l Return l
If v>h Return h
Return v
End Method
Method update()
Local t:Int
Local mini:Int
Local gh:Int=GadgetHeight(canvas)
SetGraphics CanvasGraphics(canvas)
SetClsColor 0,0,0;Cls
SetColor 80,80,80
For t=1 To units
DrawRect (number*12)+t*unitwidth+1,1,unitwidth-2,gh-1-1
Next
For t=1 To value
SetColor r[t],g[t],b[t]
DrawRect (number*12)+t*unitwidth+1,1,unitwidth-2,gh-1-1
Next
SetColor 255,255,255
DrawText value,number*12/2-TextWidth(value)/2,gh/2-TextHeight(value)/2
Flip
End Method
Method Set(v:Int)
value=_Limit(v,0,units-1)
update
End Method
Method SetGradient(startR:Int,startG:Int,startB:Int,deltaR:Int,deltaG:Int,deltaB:Int)
Local t:Int
For t=0 To units-1
r[t]=_Limit(startR,0,255)
g[t]=_Limit(startG,0,255)
b[t]=_Limit(startB,0,255)
startR:+deltaR
startG:+deltaG
startB:+deltaB
Next
update
End Method
Method Color(index:Int,_r:Int,_g:Int,_b:Int)
index=_Limit(index,0,units-1)
r[index]=_r
g[index]=_g
b[index]=_b
update
End Method
EndType
Function CreateUnitbar:TUnitbar(x:Int, y:Int, unitwidth:Int, units:Int, h:Int, parent:TGadget)
Local a:TUnitbar=New TUnitbar
Local tmp$=units
a.number=Len(tmp)
a.canvas=CreateCanvas(x,y,unitwidth*units+a.number*12,h,parent)
a.unitwidth=unitwidth
a.units=units
a.r=a.r[..units]
a.g=a.g[..units]
a.b=a.b[..units]
For Local t:Int=0 To units-1
a.r[t]=160
a.g[t]=160
a.b[t]=160
Next
Return a
End Function
' Example from here:
Local window:TGadget=CreateWindow("TUnitbar Example",200,200,600,400)
Local unitbar1:TUnitbar=CreateUnitbar(20,16,5,101,32,window)
Local unitbar2:TUnitbar=CreateUnitbar(20,64,10,11,16,window)
Local unitbar3:TUnitbar=CreateUnitbar(20,128,3,101,24,window)
unitbar1.SetGradient 450,0,0,-3,4,0
unitbar2.SetGradient 400,0,0,-35,4,0
unitbar3.SetGradient 450,0,0,-3,4,0
unitbar1.Set 5
unitbar2.Set 2
unitbar3.Set 35
Repeat
WaitEvent()
If EventID()=EVENT_WINDOWCLOSE End
If EventSource()=unitbar1 Or EventSource()=unitbar2 Or EventSource()=unitbar3
If EventID()=EVENT_UNITBARCHANGE DebugLog "changing.."
If EventID()=EVENT_UNITBARCHANGED DebugLog "changed.."
DebugLog "value: "+EventData()
DebugLog "--------------------------------"
EndIf
Forever