[MAXGUI] How to make a "Voting Box"?

BlitzMax Forums/BlitzMax Beginners Area/[MAXGUI] How to make a "Voting Box"?

boo!

methods are self-explaining I think .. 'ave fun

EventSource() contains the unitbar
EventID() contains one of two possible ID's
EventData() contains the value

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
			
			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'     )o_O) 'O RLY?'",200,200,600,400)
Local unitbar1:TUnitbar=CreateUnitbar(20,16,5,64,32,window)
Local unitbar2:TUnitbar=CreateUnitbar(20,64,16,16,16,window)
Local unitbar3:TUnitbar=CreateUnitbar(20,128,3,128,24,window)

unitbar1.SetGradient 0,-128,255,4,4,0
'unitbar2.SetGradient 128,255,-255,-16,-8,64
unitbar2.Color 4,255,255,255
unitbar3.SetGradient 128,64,32,4,2,1

unitbar3.Set 25

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