I've never considered myself a fan of writing GUI code, but now that I have a small job I need to get it done for, I'm actually having a lot of fun with it. I've been tinkering around with Lua GUI code for two days, and I came up with a really neat system after lots of trial and error.
Controls
A "control" is the name for a button, label, or other widget. A control is a Lua table.
The programmer creates the controls they want and then inputs mouse coordinates and the left mouse button state in the Update() function.
There are only three real events that occur; mouse movement, mouse button down, and mouse button up.
When the mouse is pressed, the system has a fast hierarchical search to find the selected control, and this control is made active.
Since the programmer supplies the mouse coordinates and button state, the GUI could be displayed on a 3D surface and interacted with in the game, like in Doom 3.
Frames
Frames are a lua table with a drawing method. The simplest frame just draws a rectangle on the screen. An image frame draws an image within the specified coordinates. A more sophisticated frame uses nine images to draw image-based edges around a rectangle of any size.
Controls use frames to draw different parts of the control, instead of using a lot of hard-coded GUI drawing or images.
You can replace a control's frame easily in Lua: button.frame=CreateImageFrame(image)
Methods
There are some predefined methods you can add to a control. Right now I have the following:
CursorDown
CursorUp
CursorMove
Activate
Deactivate
HideUpdate
ShowUpdate
Lua makes it really easy to expand a basic control to look or act differently. You can just overwrite a function, and you have your new control. For example:
function CreateMyButton(text,x,y,width,height,group,style)
local button=CreateButton(text,x,y,width,height,group,style)
function button:CursorMove()
--Your code here
end
function button:Draw()
--Your own drawing code goes here
end
return button
end