Pages

Sunday, May 22, 2011

Gui for Game Dev Tools - IupLua

While designing tools for Elpis, I found the need for a more robust set of GUI controls than my game-tuned HUDish controls. There were several choices that I had to chose from:

  1. Use straight up, hand crafted, Win32 code
  2. Enhance Elpis' GUI to support tools
  3. Wrap Elpis with COM and load it up in C# or similar
  4. Use an existing library that interfaces through Lua
Naturally, I opted for #4. Interfacing through Lua means that the Elpis core doesn't have to know about a GUI at all, and there's no need to recompile.

Library, The Chosen 
Now came time to find, try, and chose a library. I was hoping that I would get a chance to play around with a bunch of new and cool libraries, though the ones I wanted to try had license limitations, did not use native controls (a finicky requirement), or did not interface with Lua.

The two libraries that were left of my choices were wxWidgets and IUP, and both of these shipped with Lua for Windows binaries. Of these, I chose IUP with IupLua binding for subjective reasons, in that I preferred the interface and IUP is focused on GUI functionality alone.

Usage Example
Assuming iup51.dll and iuplua51.dll are available to Lua's CPath, a simple require("iuplua") in Lua, and IUP will be accessible to you through the global iup object.

IUP comes with many many samples to show it's use, but one thing I struggled with was to use Elpis' existing Win32 Window as the parent for new controls. I shot a quick question to the user-list to find out if this was possible, and as it turns out, this is very easy for IUP dialogs...


Here is an example of a dialog with a menu used with Elpis:

local item_exit = iup.item{title = "E&xit", action=function() return iup.CLOSE end}
local item_open = iup.item{title = "&Open", action=OpenFile_OnClick}
local item_save = iup.item{title = "&Save", action=nil, active="NO"}
local function InitializeUI()

 menu = iup.menu
 {
  iup.submenu
  {
   iup.menu
   {
    item_open,
    item_save,
    {},
    item_exit
   };
   title = "File"
  };
 }

 mainDlg = iup.dialog
 {
  iup.vbox
  {

  };
  title="Volumetric Marker Generator", font="Helvetica, Bold 14", menu = menu,
  size="100x15",
  toolbox="YES",
  maxbox="NO",
  minbox="NO",
  resize="NO"
 }
 iup.SetAttribute(mainDlg, "NATIVEPARENT", gSystem.HWND);
 mainDlg:showxy(250,90)
end

The line to note is line 34: iup.SetAttribute(mainDlg, "NATIVEPARENT", gSystem.HWND);
This tells IUP to use gSystem.HWND (which returns Elpis' main hWnd) as the parent for the dialog mainDlg.
Retrieving the HWND is as simple as lua_pushlightuserdata(L, mainHwnd) from C++.

No comments:

Post a Comment