Back to Recipes Homepage
recipe picture
Recipe: Options Dialog v1.0 -- Brow42
Created by brow42 118 5892
Your rating: None


Name: Options Dialog v1.0 -- Brow42
ID: 37119
Created on: Fri, 01/06/2012 - 00:07
Updated on: Tue, 03/06/2012 - 15:25

A library of extra dialog functions to automate creating, filling defaults, and reading back values, using tables.

Best For


brow42's picture
User offline. Last seen 2 days 18 hours ago. Offline
Joined: 09/19/2011
Groups: None
Automates a lot of code

Often in a GUI you have a data object sitting behind the graphical object. This lets you set up a table of controls and field names, and a function will add all the controls AND set their values to the current values in the data object. When the user hits a button (other than cancel or reset) another function reads back all the values.

To use this library, you define a table where each entry contains all that you would normally do to make a control, along with the data key name. Then you decide which buttons you want. After you get the data back, you should do some error checking on the inputs. If there's an error, you can just redisplay the dialog with the current values so the user can correct them. If you have text fields, you may need to convert those strings to and from what you want them to mean.

The dialog code works nicely if used correctly, but is a little intolerant of passing nil values (client crashes). I've tried to catch some errors so that you'll get a standard error message. I'm an inexperienced Lua programmer so I can't guarantee the robustness.

The script above includes a demo program with one of each control, and Okay, Cancel, Help, and Reset buttons. The text field expects 2 numbers and makes an error dialog if it doesn't pass the user input check. You'll want to strip out the demo program or just rewrite it for your purposes. I think the demo contains most of what a basic options dialog would need.

Please send me bug reports so I can fix them (or trap the crashers)!

brow42's picture
User offline. Last seen 2 days 18 hours ago. Offline
Joined: 09/19/2011
Groups: None
The Library Alone
  * Option Dialogs
  * Original Author: Brow42
  * Version 1.0 Jan. 6 2012

  * This allows you to define a dialog with a table instead of
  * explicit function calls, set its values with another table,
  * and read its values into yet a third table.
  * A self-contained function for text-only message dialogs is
  * included. This is good for help and error messages.
  * New functions:
  * dialog.AddFromTable(dialog,field table, data table)
  * dialog.ReadFields(dialog,field table, data table)
  * dialog.MessageBox(text or text table,title text,okay button text, cancel button text)
-- ================================== Begin New Dialog Library Functions
  Throws up a dialog containing just text, provided as a string or table
  of strings in the first argument. Title and buttons are optional. Return
  value is 1 if the first button is clicked and 0 if the second button is clicked
  or the window is closed.
function dialog.MessageBox(msg,title,buttontext1, buttontext0)
    title = title or ''
    local d = dialog.CreateDialog(title)
    if type(msg) == 'string' then d['1'] = dialog.AddLabel(msg)
    else for i = 1,#msg do
            d[tostring(i)] = dialog.AddLabel(msg[i])
    buttontext1 = buttontext1 or 'Ok'
    d.button = dialog.AddButton(buttontext1,1)
    if buttontext0 then d.button0 = dialog.AddButton(buttontest0,0) end
    return dialog.Show(d)

  Add items to the dialog from a table in the order given. The each table entry
  is the dialog control function, the control label, the data key name, and the control options.
  If this function fails, it gives an error saying which table item it failed on.
  d = a created dialog, fields = table of dialog controls as described, object = data table
function dialog.AddFromTable(d,fields,object)
    local  func, label, id, value -- renamed field parameters for clarity
    -- function wrappers to catch errors, explicitly named for clarity
    function _AddLabel(args)  d[id] = dialog.AddLabel(label) end
    function _AddCheckbox(args) d[id] = dialog.AddCheckbox(label,value) end
    function _AddSlider(args) d[id] = dialog.AddSlider(label,value,args[4],args[5],args[6]) end
    function _AddTextbox(args) d[id] = dialog.AddTextbox(label,value) end

    for i = 1, #fields do
        local rc = true
        local err
        rc,err = pcall( function() func,label,id = fields[i][1],fields[i][2],fields[i][3] end)
        value = object[id]
        if func == dialog.AddLabel then id='_AL'..tostring(i) rc,err = pcall(_AddLabel,fields[i])
            -- this is a crasher for AddTextbox but not an error for AddLabel!
            if value == nil then error('Missing data field for field #'..tostring(i)..'\n(Did you forget to pass a string?)') end 
            if func == dialog.AddCheckbox then rc,err = pcall(_AddCheckbox,fields[i])
            elseif func == dialog.AddSlider then rc,err = pcall(_AddSlider,fields[i])
            elseif func == dialog.AddTextbox then rc,err = pcall(_AddTextbox,fields[i])
            else error('Unknown control in dialog field #'..tostring(i)) end
        end -- if AddLabel
        if rc == false then
            error('Error setting dialog field #'..tostring(i)..(err and ('\n'..err) or ''))

  This just copies the data from the dialog to the data table.
function dialog.ReadFields(d,fields,object)
    for i = 1,#fields do
        if fields[i][1]~= dialog.AddLabel then
            object[fields[i][3]] = d[fields[i][3]].value
-- ================================== End New Dialog Library Functions
brow42's picture
User offline. Last seen 2 days 18 hours ago. Offline
Joined: 09/19/2011
Groups: None
typo - crasher

Please note the typo in MessageBox activated if you use 2 buttons:
if buttontext0 then d.button0 = dialog.AddButton(buttontest0,0) end

buttontest should be buttontext

Want to try?
Add to Cookbook!
To download recipes to your cookbook, you need to have the game client running.





Developed by: UW Center for Game Science, UW Institute for Protein Design, Northeastern University, Vanderbilt University Meiler Lab, UC Davis
Supported by: DARPA, NSF, NIH, HHMI, Amazon, Microsoft, Adobe, RosettaCommons