Writing CEGUI scripts

From CEGUI Wiki - Crazy Eddie's GUI System (Open Source)
Jump to: navigation, search

Written for CEGUI 0.4


Works with versions 0.4.x (obsolete)

The code on this page is Lua script using the CEGUILua bindings available in CEGUI 0.4. The code snippets might not be specifically useful, but they should show off some of the posibilities with CEGUI and Lua used together. And give an idea of how to write these scripts.

Off we go :)

--User:Lindquist 20:56, 9 Jun 2005 (BST)


Change logging level

local logger = CEGUI.Logger:getSingleton()	-- get the logger
local lvl = logger:getLoggingLevel()		-- get logging level

if lvl < CEGUI.Insane then			-- if logging level is less than insane
	logger:setLoggingLevel(lvl+1)		-- then increase it
end

Bumps up the logging level a notch unless we're already at Insane.


Load a scheme

CEGUI.SchemeManager:getSingleton():loadScheme("../datafiles/schemes/TaharezLook.scheme")

Loads the TaharezLook scheme.


Simple Interface

-- create the GUI sheet
local sheet = CEGUI.WindowManager:getSingleton():createWindow("DefaultGUISheet","root");
CEGUI.System:getSingleton():setGUISheet(sheet) -- and attach it to the system

-- create a FrameWindow
local fw = CEGUI.WindowManager:getSingleton():createWindow("TaharezLook/FrameWindow","framewnd");
-- add it to the sheet
sheet:addChildWindow(fw)

-- set its size and position
local sz = CEGUI.Size(0.5,0.5)
local pos = CEGUI.Point(0.2,0.1)
fw:setSize(sz)
fw:setPosition(pos)
-- disable user sizing
fw:setProperty("SizingEnabled","False")

-- make the close button work
fw:subscribeEvent("CloseClicked","fwCloseClicked")

-- the CloseClicked event handler
function fwCloseClicked(eventArgs)
	local we = CEGUI.toWindowEventArgs(eventArgs)
	CEGUI.WindowManager:getSingleton():destroyWindow(we.window) -- destroy the frame window
end

Creates a GUISheet and attaches it to the System. Then creates a FrameWindow, sets its size and position. Disables the sizing feature and subscribes a scripted event handler to destroy the window when the close button is clicked.


Alternative casting

-- the CloseClicked event handler
function fwCloseClicked(eventArgs)
	local we = tolua.cast(eventArgs,"CEGUI::WindowEventArgs")
	CEGUI.WindowManager:getSingleton():destroyWindow(we.window) -- destroy the frame window
end

A modified close button click handler (from the previous snippet) showing an alternative way to cast EventArgs to WindowEventArgs


Load a layout

local w = CEGUI.WindowManager:getSingleton():loadWindowLayout("../datafiles/layouts/test.layout")
CEGUI.System:getSingleton():getGUISheet():addChildWindow(w)

Loads a XML layout and adds the returned window to the active GUISheet.


Menubar with popup

-- we'll be using the window manager quite alot
local wmgr = CEGUI.WindowManager:getSingleton()

-- do the menubar
local bar = wmgr:createWindow("WindowsLook/Menubar","the_menu_bar")
bar:setSize(CEGUI.Size(1,0.1))
CEGUI.System:getSingleton():getGUISheet():addChildWindow(bar)

-- add a menuitem to the bar
local item = wmgr:createWindow("WindowsLook/MenubarItem","the_menu_bar_item")
item:setText("Bar item")
bar:addChildWindow(item)

-- add a popupmenu to the bar's menuitem
local pop = wmgr:createWindow("WindowsLook/PopupMenu","the_popup_menu")
item:addChildWindow(pop)

-- add a few menuitems to the popup
item = wmgr:createWindow("WindowsLook/PopupMenuItem","the_popup_menu_item_1")
item:setText("Popup item 1")
pop:addChildWindow(item)

item = wmgr:createWindow("WindowsLook/PopupMenuItem","the_popup_menu_item_2")
item:setText("Popup item 2")
pop:addChildWindow(item)

Creates a simple menubar and adds a popupmenu with two items to it.