Haven't be successfull on the the custom canvas height by now... :(
But I have changed the type to have 2 more states, i.e: disabled and hidden.
But I have changed the type to have 2 more states, i.e: disabled and hidden.
Type TUnitbar Rem TUnitbar CS_TBL, Grisu Last modified: 15.04.06 Usage: MyUnitbar:TUnitbar=CreateUnitbar:TUnitbar(x,y,unitsize, units, height, hidden, disabled, 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 hidden : Byte ' Unitbar is invisible Field disabled: Byte ' Unitbar is diabled 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 Resizeheight(h:Int) SetGadgetShape canvas,GadgetX(canvas),GadgetY(canvas),GadgetWidth(canvas),h update 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) If hidden=False Then ' Unitbar hidden completely? ShowGadget (canvas) SetGraphics CanvasGraphics(canvas) SetClsColor 0,0,0;Cls If disabled=False Then ' Draw an enabled Unitbar only SetColor 100,100,100 For t=value 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 Else ' Draw an empty Unitbar SetColor 100,100,100 For t=1 To units DrawRect (number*12)+t*unitwidth+1,1,unitwidth-2,gh-1-1 Next value=0 SetColor 255,255,255 DrawText value,number*12/2-TextWidth(value)/2,gh/2-TextHeight(value)/2 EndIf ' Disabled Else HideGadget (canvas) EndIf ' Hidden 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, hidden:Byte, disabled:Byte, parent:TGadget) Local a:TUnitbar=New TUnitbar Local tmp$=units a.number=Len(tmp) a.canvas=CreateCanvas(x,y,1+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 a.hidden=hidden a.disabled=disabled Return a End Function