Difference between revisions of "Writing CEGUI scripts"

From CEGUI Wiki - Crazy Eddie's GUI System (Open Source)
Jump to: navigation, search
m (Bot: Automated text replacement (-\[\[(.*?)\|.*?\]\] +\1))
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
The code on this page is Lua script using the CEGUILua bindings available in CEGUI 0.3.
+
{{VersionBadge|0.4}}
 +
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.
 
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 :)
 
Off we go :)
  
--[[User:Lindquist|Lindquist]] 20:56, 9 Jun 2005 (BST)
+
--[[User:Lindquist]] 20:56, 9 Jun 2005 (BST)
  
  
Line 33: Line 34:
 
   
 
   
 
  -- set its size and position
 
  -- set its size and position
  local sz = CEGUI.Size:new_local(0.5,0.5)
+
  local sz = CEGUI.Size(0.5,0.5)
  local pos = CEGUI.Point:new_local(0.2,0.1)
+
  local pos = CEGUI.Point(0.2,0.1)
 
  fw:setSize(sz)
 
  fw:setSize(sz)
 
  fw:setPosition(pos)
 
  fw:setPosition(pos)
Line 67: Line 68:
  
 
== Menubar with popup ==
 
== Menubar with popup ==
 +
-- we'll be using the window manager quite alot
 +
local wmgr = CEGUI.WindowManager:getSingleton()
 +
 
  -- do the menubar
 
  -- do the menubar
  local bar = CEGUI.WindowManager:getSingleton():createWindow("WindowsLook/Menubar","the_menu_bar")
+
  local bar = wmgr:createWindow("WindowsLook/Menubar","the_menu_bar")
  bar:setSize(CEGUI.Size:new_local(1,0.1))
+
  bar:setSize(CEGUI.Size(1,0.1))
 
  CEGUI.System:getSingleton():getGUISheet():addChildWindow(bar)
 
  CEGUI.System:getSingleton():getGUISheet():addChildWindow(bar)
 
   
 
   
 
  -- add a menuitem to the bar
 
  -- add a menuitem to the bar
  local item = CEGUI.WindowManager:getSingleton():createWindow("WindowsLook/MenubarItem","the_menu_bar_item")
+
  local item = wmgr:createWindow("WindowsLook/MenubarItem","the_menu_bar_item")
 
  item:setText("Bar item")
 
  item:setText("Bar item")
 
  bar:addChildWindow(item)
 
  bar:addChildWindow(item)
 
   
 
   
 
  -- add a popupmenu to the bar's menuitem
 
  -- add a popupmenu to the bar's menuitem
  local pop = CEGUI.WindowManager:getSingleton():createWindow("WindowsLook/PopupMenu","the_popup_menu")
+
  local pop = wmgr:createWindow("WindowsLook/PopupMenu","the_popup_menu")
 
  item:addChildWindow(pop)
 
  item:addChildWindow(pop)
 
   
 
   
 
  -- add a few menuitems to the popup
 
  -- add a few menuitems to the popup
  item = CEGUI.WindowManager:getSingleton():createWindow("WindowsLook/PopupMenuItem","the_popup_menu_item_1")
+
  item = wmgr:createWindow("WindowsLook/PopupMenuItem","the_popup_menu_item_1")
 
  item:setText("Popup item 1")
 
  item:setText("Popup item 1")
 
  pop:addChildWindow(item)
 
  pop:addChildWindow(item)
 
   
 
   
  item = CEGUI.WindowManager:getSingleton():createWindow("WindowsLook/PopupMenuItem","the_popup_menu_item_2")
+
  item = wmgr:createWindow("WindowsLook/PopupMenuItem","the_popup_menu_item_2")
 
  item:setText("Popup item 2")
 
  item:setText("Popup item 2")
 
  pop:addChildWindow(item)
 
  pop:addChildWindow(item)
 
Creates a simple menubar and adds a popupmenu with two items to it.
 
Creates a simple menubar and adds a popupmenu with two items to it.
 +
 +
[[Category:HowTo]]

Latest revision as of 23:53, 3 March 2011

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.