GUI Helper functions...

BlitzMax Forums/MaxGUI Module/GUI Helper functions...

_Skully(Posted 2009) [#1]
Apart from getting a GUI layout program I found creating panels, buttons etc tedious from a programming perspective so I took something I had built into my original GUI system and added a few functions to make things easier...


Const TGADGET_ALIGN:Int=-1000000


Function RelativeToLast(Parent:TGadget,x:Int Var,y:Int Var,width:Int Var,height:Int Var)
	' if x or y<0 then it spaces abs(x) or abs(y) from last gadget in kids list
	' if width or height is <0 then it consumes the entire width of the parent - abs(width)

	Local tx:Int,ty:Int
	If parent.kids.Count()>0
		If x<0
			Local t:TGadget=TGadget(parent.kids.Last())
			x=t.xpos+t.width-x+1
			If y=TGADGET_ALIGN
				y=t.ypos
			End If
		End If
		
		If y<0
			Local t:TGadget=TGadget(parent.kids.Last())
			y=t.ypos+t.height-y+1
			If x=TGADGET_ALIGN
				x=t.xpos
			End If
		End If
	EndIf
	
	If width<0
		width=parent.ClientWidth()+width+1-x
	End If
	If height<0
		height=parent.ClientHeight()+height+1-y
	End If

End Function

Function AutoCreateCanvas:TGadget(x:Int=0,y:Int=0,width:Int=-1,height:Int=-1,parent:tgadget,style:Int=0)
	RelativeToLast(Parent,x,y,width,height)
	Return CreateCanvas(x,y,width,height,parent,style)
End Function

Function AutoCreateButton:Tgadget(label:String,x:Int=0,y:Int=0,width:Int=-1,height:Int=16,Parent:Tgadget,style:Int=BUTTON_PUSH)
	RelativeToLast(Parent,x,y,width,height)
	Return CreateButton(label,x,y,width,height,parent,style)
End Function

Function AutoCreateLabel:Tgadget(label:String,x:Int=0,y:Int=0,width:Int=-1,height:Int=16,parent:TGadget,style:Int=LABEL_LEFT)
	RelativeToLast(Parent,x,y,width,height)
	Return CreateLabel(label,x,y,width,height,parent,style)
End Function

Function AutoCreateSlider:TGadget(x:Int=0,y:Int=0,width:Int=-1,height:Int=16,Parent:TGadget,style:Int=0)
	RelativeToLast(Parent,x,y,width,height)
	Return CreateSlider(x,y,width,height,parent,style)
End Function

Function AutoCreatePanel:TGadget(x:Int=0,y:Int=0,width:Int=-1,height:Int=-1,Parent:TGadget,style:Int=0,title:String="")
	RelativeToLast(Parent,x,y,width,height)
	Return CreatePanel(x,y,width,height,Parent,style,title)
End Function


These allow you to place items relative to the last item added by using negative values in place of X,Y, Width, and Height

-x will place the next item abs(x) pixels from the last one... if Y=TGADGET_ALIGN then the same y is used from the last TGadget...

-y will place the next item abs(y) pixels from the last one... if x=TGADGET_ALIGN then the same x is used from the last TGadget

-width will use the parent TGadget Clientwidth-width-x+1
-height will use the parent TGadget Clientheight-height-y+1

Just add "Auto" to the beginning of your create statements... only panel, button, label, and slider are done but its easy to expand.

Cheers

{EDIT} added TGADGET_ALIGN to simplify use, added Canvas, added overloads suggested by Leadworks


GaryV(Posted 2009) [#2]
Nice. Would you consider sticking it in the code archives, so it will always be easy to find? Posts like this tend to get lost very easily.


_Skully(Posted 2009) [#3]
I will but I generally post here first just in case its buggy or gets expanded by someone with more time ;)


_Skully(Posted 2009) [#4]
Updated...

Added a global TGADGET_ALIGN to eliminate some weirdnesses and add clarity


JoshK(Posted 2009) [#5]
I think all gadget create functions should have default arguments like this:

CreatePanel(x=0,y=0,width=-1,height=-1,group:TGadget)

And they should act like this:

If width=-1 width=group.ClientWidth()
if height=-1 height=group.ClientHeight()

So you can do this:

CreatePanel(,,,,group)


_Skully(Posted 2009) [#6]
I added overloads to the functions above but whomever maintains the MaxGUI could certainly incorporate some of this into the mod

With the - values being relative to the last gadget you can't simply make a value of -1 = ClientWidth/ClientHeight... it has to be the remaining space available... the nice thing about using the relative values is that you can move items around by moving the line in the code and dont have to reprogram all your fixed values.

Good Suggestion about the overloads.. I incorporated it into the autofunctions.

Cheers