<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://cegui.org.uk/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Sarev0k</id>
		<title>CEGUI Wiki - Crazy Eddie's GUI System (Open Source) - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://cegui.org.uk/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Sarev0k"/>
		<link rel="alternate" type="text/html" href="http://cegui.org.uk/wiki/Special:Contributions/Sarev0k"/>
		<updated>2026-04-10T01:19:00Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.24.1</generator>

	<entry>
		<id>http://cegui.org.uk/wiki/index.php?title=Handling_Events_from_Lua&amp;diff=1337</id>
		<title>Handling Events from Lua</title>
		<link rel="alternate" type="text/html" href="http://cegui.org.uk/wiki/index.php?title=Handling_Events_from_Lua&amp;diff=1337"/>
				<updated>2005-06-26T01:19:39Z</updated>
		
		<summary type="html">&lt;p&gt;Sarev0k: /* Registering Events to Lua Functions */  Fixing lua function example&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Moving the handling of GUI events outside of your project code, and into Lua scripts can add a great deal of flexibilty to your interface. Simple GUI related stuff can be easily prototyped, and modified during testing.&lt;br /&gt;
&lt;br /&gt;
Event handlers in Lua are just regular Lua functions that take one parameter. An CEGUI::EventArgs struct.&lt;br /&gt;
The functions you register as event handlers must already be registered with the system, so unless you load them all from your init-script, you will need to load some script files before doing any Lua handled events.&lt;br /&gt;
&lt;br /&gt;
Here we go...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Loading the script files ==&lt;br /&gt;
There are two ways of loading your script files. You can use the C++ interface or you can do it from Lua in your init-script.&lt;br /&gt;
The two approaches are very alike, as the Lua version uses a direct binding to the same C++ function.&lt;br /&gt;
&lt;br /&gt;
 CEGUI::System::executeScriptFile(const CEGUI::String&amp;amp; filename, const CEGUI::String&amp;amp; resourceGroup = &amp;quot;&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
Obviously this function takes two parameters, a filename and the resource group.&lt;br /&gt;
I think most people ignore the last parameter pretty much.&lt;br /&gt;
&lt;br /&gt;
Calling this function with the Lua scripting module correctly attached to CEGUI, will execute the specified Lua script file.&lt;br /&gt;
This means that functions etc. declared within these files will be accessible to your program, except of course if they are declared ''local''.&lt;br /&gt;
&lt;br /&gt;
So. From C++ you would do something like this:&lt;br /&gt;
&lt;br /&gt;
 CEGUI::System::getSingleton().executeScriptFile(&amp;quot;../datafiles/scripts/guiscript.lua&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
If anything goes wrong an exception is thrown.&lt;br /&gt;
&lt;br /&gt;
From Lua you could use this code:&lt;br /&gt;
&lt;br /&gt;
 CEGUI.System:getSingleton():executeScriptFile(&amp;quot;../datafiles/scripts/guiscript.lua&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
any global code in these script files will also be executed so be sure to manage each execution if necessary (with a counter fx).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Registering Events to Lua Functions ==&lt;br /&gt;
Now that we have our scripts loaded and all, we are ready to bind some events to our scripted handlers.&lt;br /&gt;
&lt;br /&gt;
The function to use when binding scripted events is a little different from the regular C++ one.&lt;br /&gt;
&lt;br /&gt;
 Event::Connection subscribeScriptedEvent(const String&amp;amp; name, const String&amp;amp; subscriber_name);&lt;br /&gt;
&lt;br /&gt;
''name'' is the name of the event you want to subscribe. As usual. ''subscriber_name'' is a string holding the name of the Lua function to bind to the event.&lt;br /&gt;
&lt;br /&gt;
After this call the Lua function specified will be registered as the event handler. And works exactly like the C++ version (except being Lua of course)&lt;br /&gt;
&lt;br /&gt;
Here's a little snippet to bind a PushButton Clicked event to a Lua function:&lt;br /&gt;
&lt;br /&gt;
 CEGUI::PushButton* pb = (CEGUI::PushButton*)CEGUI::WindowManager::getSingleton().createWindow(&amp;quot;TaharezLook/Button&amp;quot;,&amp;quot;lua_powered_button&amp;quot;);&lt;br /&gt;
 pb-&amp;gt;setSize(CEGUI::Size(0.1f,0.1f));&lt;br /&gt;
 pb-&amp;gt;setPosition(CEGUI::Point(0.1f,0.1f));&lt;br /&gt;
 pb-&amp;gt;subscribeScriptedEvent(&amp;quot;Clicked&amp;quot;,&amp;quot;luabtn_clicked&amp;quot;);&lt;br /&gt;
 CEGUI::System::getSingleton().getGUISheet()-&amp;gt;addChildWindow(pb);&lt;br /&gt;
&lt;br /&gt;
This code would create a simple TaharezLook button, subscribe the Lua function ''luabtn_clicked'' to its ''Clicked'' event, and finally add it to the current GUI sheet.&lt;br /&gt;
&lt;br /&gt;
Now let's take a look at that Lua event handler:&lt;br /&gt;
&lt;br /&gt;
 function luabtn_clicked(e)&lt;br /&gt;
   local we = CEGUI.toWindowEventArgs(e)&lt;br /&gt;
   we.window:setText(&amp;quot;handled from Lua&amp;quot;);&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Here we make sure the button text is ''handled from Lua'' when the button is clicked.&lt;br /&gt;
We use a utility function:&lt;br /&gt;
&lt;br /&gt;
 CEGUI.toWindowEventArgs(e)&lt;br /&gt;
&lt;br /&gt;
it's obvious what it does, and converters for the other EventArgs class are also available.&lt;br /&gt;
&lt;br /&gt;
== Registering Lua Event Handlers from XML Layouts ==&lt;br /&gt;
It's very easy to bind scripted event handlers to the windows in your XML layouts.&lt;br /&gt;
&lt;br /&gt;
Take a look a this example:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;br /&gt;
 &amp;lt;GUILayout&amp;gt;&lt;br /&gt;
 &amp;lt;Window Type=&amp;quot;TaharezLook/Button&amp;quot; Name=&amp;quot;lua_powered_button&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;Property Name=&amp;quot;Width&amp;quot; Value=&amp;quot;0.1&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;Property Name=&amp;quot;Height&amp;quot; Value=&amp;quot;0.1&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;Property Name=&amp;quot;XPosition&amp;quot; Value=&amp;quot;0.1&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;Property Name=&amp;quot;YPosition&amp;quot; Value=&amp;quot;0.1&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;Event Name=&amp;quot;Clicked&amp;quot; Function=&amp;quot;luabtn_clicked&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;/Window&amp;gt;&lt;br /&gt;
 &amp;lt;/GUILayout&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This simple layout create the same button as described above and even bind the scripted event handler to it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
now you know how to do stuff with your GUI without writing C++ code.&lt;br /&gt;
&lt;br /&gt;
--[[User:Lindquist|Lindquist]] 17:10, 6 May 2005 (BST)&lt;/div&gt;</summary>
		<author><name>Sarev0k</name></author>	</entry>

	<entry>
		<id>http://cegui.org.uk/wiki/index.php?title=Writing_CEGUI_scripts&amp;diff=57</id>
		<title>Writing CEGUI scripts</title>
		<link rel="alternate" type="text/html" href="http://cegui.org.uk/wiki/index.php?title=Writing_CEGUI_scripts&amp;diff=57"/>
				<updated>2005-06-26T01:08:32Z</updated>
		
		<summary type="html">&lt;p&gt;Sarev0k: /* Load a layout */  Changed loadLayout() to loadWindowLayout()&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;All code on this page is Lua script using the CEGUILua bindings available on CVS HEAD.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Off we go :)&lt;br /&gt;
&lt;br /&gt;
--[[User:Lindquist|Lindquist]] 20:56, 9 Jun 2005 (BST)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Change logging level ==&lt;br /&gt;
 local logger = CEGUI.Logger:getSingleton()	-- get the logger&lt;br /&gt;
 local lvl = logger:getLoggingLevel()		-- get logging level&lt;br /&gt;
 &lt;br /&gt;
 if lvl &amp;lt; CEGUI.Insane then			-- if logging level is less than insane&lt;br /&gt;
 	logger:setLoggingLevel(lvl+1)		-- then increase it&lt;br /&gt;
 end&lt;br /&gt;
Bumps up the logging level a notch unless we're already at Insane.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Load a scheme ==&lt;br /&gt;
 CEGUI.SchemeManager:getSingleton():loadScheme(&amp;quot;../datafiles/schemes/TaharezLook.scheme&amp;quot;)&lt;br /&gt;
Loads the TaharezLook scheme.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Simple Interface ==&lt;br /&gt;
 -- create the GUI sheet&lt;br /&gt;
 local sheet = CEGUI.WindowManager:getSingleton():createWindow(&amp;quot;DefaultGUISheet&amp;quot;,&amp;quot;root&amp;quot;);&lt;br /&gt;
 CEGUI.System:getSingleton():setGUISheet(sheet) -- and attach it to the system&lt;br /&gt;
 &lt;br /&gt;
 -- create a FrameWindow&lt;br /&gt;
 local fw = CEGUI.WindowManager:getSingleton():createWindow(&amp;quot;TaharezLook/FrameWindow&amp;quot;,&amp;quot;framewnd&amp;quot;);&lt;br /&gt;
 -- add it to the sheet&lt;br /&gt;
 sheet:addChildWindow(fw)&lt;br /&gt;
 &lt;br /&gt;
 -- set its size and position&lt;br /&gt;
 local sz = CEGUI.Size:new_local(0.5,0.5)&lt;br /&gt;
 local pos = CEGUI.Point:new_local(0.2,0.1)&lt;br /&gt;
 fw:setSize(sz)&lt;br /&gt;
 fw:setPosition(pos)&lt;br /&gt;
 -- disable user sizing&lt;br /&gt;
 fw:setProperty(&amp;quot;SizingEnabled&amp;quot;,&amp;quot;False&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 -- make the close button work&lt;br /&gt;
 fw:subscribeEvent(&amp;quot;CloseClicked&amp;quot;,&amp;quot;fwCloseClicked&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 -- the CloseClicked event handler&lt;br /&gt;
 function fwCloseClicked(eventArgs)&lt;br /&gt;
 	local we = CEGUI.toWindowEventArgs(eventArgs)&lt;br /&gt;
 	CEGUI.WindowManager:getSingleton():destroyWindow(we.window) -- destroy the frame window&lt;br /&gt;
 end&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Load a layout ==&lt;br /&gt;
 local w = CEGUI.WindowManager:getSingleton():loadWindowLayout(&amp;quot;../datafiles/layouts/test.layout&amp;quot;)&lt;br /&gt;
 CEGUI.System:getSingleton():getGUISheet():addChildWindow(w)&lt;br /&gt;
Loads a XML layout and adds the returned window to the active GUISheet.&lt;/div&gt;</summary>
		<author><name>Sarev0k</name></author>	</entry>

	</feed>