Browse Source

Fixed most of the Lua side of options.rml.

Added a new Element method, Element:AsType that is pretty much a static cast operator. See Source/Core/Lua/Element.h for the documentation.
Nate Starkey 13 years ago
parent
commit
010536abb7

+ 40 - 13
Samples/luainvaders/data/options.rml

@@ -27,15 +27,43 @@
 		</style>
 		<script>
 Options = Options or {}
+
+function Options.Serialize(filename,options)
+    local file,message = io.open(filename,'w+') --w+ will erase the previous data
+    if file == nil then Log.Message(Log.logtype.error, "Error saving options in options.rml: " .. message) return end
+    --cache the function
+    local format = string.format
+    
+    for key,value in pairs(options) do
+        file:write(tostring(key)..'='..tostring(value)..'\n')
+    end
+    file:close()
+end
+
+function Options.Deserialize(filename)
+    local ret = {} --table to return
+    --cache functions that are used a lot for faster lookup
+    local lines = io.lines
+    local gmatch = string.gmatch
+    
+    for line in lines(filename) do
+        for k,v in gmatch(line, '(%w+)=(%w+)') do ret[k] = v end
+    end 
+    --io.lines automatically closes the file when it get to the end
+    
+    return ret
+end
+        
         
 function Options.LoadOptions(document)
-    local f = io.open('options.dat', 'r')
-    --create the function below
-    local options = nil --cPickle.loads(''.join(f:lines()))
+    --TODO: check for a non-existing file so we don't get warnings
+    local options = Options.Deserialize('options.dat')
+    local type = Element.etype
     
-    document:GetElementById(options['graphics']).checked = true
-    document:GetElementById('reverb').checked = options['reverb']
-    document:GetElementById('3d').checked = options['3d']
+    document:GetElementById(options['graphics']):AsType(type.input).checked = true
+    --because everything is loaded as a string, we have to fool around with the boolean variables
+    document:GetElementById('reverb'):AsType(type.input).checked = (options['reverb'] == 'true')
+    document:GetElementById('3d'):AsType(type.input).checked = (options['3d'] == 'true')
 end
 
 function Options.SaveOptions(event)
@@ -46,13 +74,12 @@ function Options.SaveOptions(event)
     local options = {}
     local params = event.parameters
     options['graphics'] = params['graphics']
-    options['reverb'] = params['reverb']
-    options['3d'] = params['3d']
+    --because of how checkboxes won't be in the event params if they aren't checked,
+    --we return false if they don't exist. This is Lua's ternary operator.
+    options['reverb'] = params['reverb'] and true or false
+    options['3d'] = params['3d'] and true or false
     
-    local f = io.open('options.dat', 'w+')
-    --need the below function
-    --f:write(cPickle.dumps(options))
-    f:close()
+    Options.Serialize('options.dat',options)
 end
 		
 function Options.DisplayBadGraphics(document, display)
@@ -78,7 +105,7 @@ end
 				<p>
 					Audio:<br />
 					<input id="reverb" type="checkbox" name="reverb" value="true" checked="true" /> Reverb<br />
-					<input id="3d" type="checkbox" name="3d" value="true" /> 3D Spatialisation
+					<input id="3d" type="checkbox" name="3d" value="false" checked="false" /> 3D Spatialisation
 				</p>
 			</div>
 			<input type="submit" name="button" value="accept">Accept</input>

+ 2 - 0
Samples/luainvaders/lua/start.lua

@@ -1,4 +1,6 @@
 
+
+
 function Startup()
 	maincontext = rocket.contexts()["main"]
 	maincontext:LoadDocument("data/background.rml"):Show()

+ 40 - 7
Source/Core/Lua/Element.cpp

@@ -235,6 +235,38 @@ int ElementSetClass(lua_State* L, Element* obj)
     return 0;
 }
 
+int ElementAsType(lua_State* L, Element* obj)
+{
+    Elementetype type = Elementetype(luaL_checkint(L,1));
+    switch(type)
+    {
+    case TDATAGRID:
+        LuaType<Rocket::Controls::ElementDataGrid>::push(L,(Rocket::Controls::ElementDataGrid*)obj,false);
+        break;
+    case TDATASELECT:
+        LuaType<Rocket::Controls::ElementFormControlDataSelect>::push(L,(Rocket::Controls::ElementFormControlDataSelect*)obj,false);
+        break;
+    case TFORM:
+        LuaType<Rocket::Controls::ElementForm>::push(L,(Rocket::Controls::ElementForm*)obj,false);
+        break;
+    case TINPUT:
+        LuaType<Rocket::Controls::ElementFormControlInput>::push(L,(Rocket::Controls::ElementFormControlInput*)obj,false);
+        break;
+    case TSELECT:
+        LuaType<Rocket::Controls::ElementFormControlSelect>::push(L,(Rocket::Controls::ElementFormControlSelect*)obj,false);
+        break;
+    case TTABSET:
+        LuaType<Rocket::Controls::ElementTabSet>::push(L,(Rocket::Controls::ElementTabSet*)obj,false);
+        break;
+    case TTEXTAREA:
+        LuaType<Rocket::Controls::ElementFormControlTextArea>::push(L,(Rocket::Controls::ElementFormControlTextArea*)obj,false);
+    default:
+        LuaType<Element>::push(L,obj,false);
+        break;
+    }
+    return 1;
+}
+
 
 //getters
 int ElementGetAttrattributes(lua_State* L)
@@ -257,26 +289,26 @@ int ElementGetAttrattributes(lua_State* L)
         case Variant::BYTE:
         case Variant::CHAR:
         case Variant::INT:
-            lua_pushinteger(L,*(int*)var);
+            lua_pushinteger(L,var->Get<int>());
             break;
         case Variant::FLOAT:
-            lua_pushnumber(L,*(float*)var);
+            lua_pushnumber(L,var->Get<float>());
             break;
         case Variant::COLOURB:
-            LuaType<Colourb>::push(L,(Colourb*)var,false);
+            LuaType<Colourb>::push(L,&var->Get<Colourb>(),false);
             break;
         case Variant::COLOURF:
-            LuaType<Colourf>::push(L,(Colourf*)var,false);
+            LuaType<Colourf>::push(L,&var->Get<Colourf>(),false);
             break;
         case Variant::STRING:
-            lua_pushstring(L,((String*)var)->CString());
+            lua_pushstring(L,var->Get<String>().CString());
             break;
         case Variant::VECTOR2:
             //according to Variant.inl, it is going to be a Vector2f
-            LuaType<Vector2f>::push(L,((Vector2f*)var),false);
+            LuaType<Vector2f>::push(L,&var->Get<Vector2f>(),false);
             break;
         case Variant::VOIDPTR:
-            lua_pushlightuserdata(L,(void*)var);
+            lua_pushlightuserdata(L,var->Get<void*>());
             break;
         default:
             lua_pushnil(L);
@@ -583,6 +615,7 @@ RegType<Element> ElementMethods[] =
     LUAMETHOD(Element,ScrollIntoView)
     LUAMETHOD(Element,SetAttribute)
     LUAMETHOD(Element,SetClass)
+    LUAMETHOD(Element,AsType)
     { NULL, NULL },
 };
 

+ 13 - 0
Source/Core/Lua/Element.h

@@ -31,6 +31,7 @@
     noreturn Element:ScrollIntoView(bool align_with_top)
     noreturn Element:SetAttribute(string name,string value)
     noreturn Element:SetClass(string name, bool activate)
+    type_specified Element:AsType(Element.etype) --see footnote 2. Yes, that 'e' is supposed to be there.
 
 
     //getters accessed by the period syntax from an element object
@@ -81,6 +82,12 @@
     it is in a specific order. The order is event,element,document. So:
     function foo(l,q,e) end element:AddEventListener("click",foo,true) is the correct syntax, and puts l=event,q=element,e=document
     They are terrible names, but it is to make a point.
+
+
+    footnote 2: For Element:AsType(Element.etype)
+    Element.etype is an enum that maps strings to integers. It can be one of the following:
+    datagrid,dataselect,element,form,input,select,tabset,textarea
+    If you give it a bad parameter, it will return an Element type
 */
 #include "LuaType.h"
 #include "lua.hpp"
@@ -89,7 +96,12 @@
 namespace Rocket {
 namespace Core {
 namespace Lua {
+enum Elementetype
+{
+    TDATAGRID = 0, TDATASELECT, TELEMENT, TFORM, TINPUT, TSELECT, TTABSET, TTEXTAREA
+};
 template<> bool LuaType<Element>::is_reference_counted();
+template<> void LuaType<Element>::extra_init(lua_State* L, int metatable_index);
 
 //methods
 int ElementAddEventListener(lua_State* L, Element* obj);
@@ -111,6 +123,7 @@ int ElementReplaceChild(lua_State* L, Element* obj);
 int ElementScrollIntoView(lua_State* L, Element* obj);
 int ElementSetAttribute(lua_State* L, Element* obj);
 int ElementSetClass(lua_State* L, Element* obj);
+int ElementAsType(lua_State* L, Element* obj);
 
 //getters
 int ElementGetAttrattributes(lua_State* L);

+ 1 - 1
Source/Core/Lua/Interpreter.cpp

@@ -31,10 +31,10 @@ void Interpreter::RegisterEverything(lua_State* L)
     LuaType<Colourf>::Register(L);
     LuaType<Colourb>::Register(L);
     LuaType<Log>::Register(L);
+    LuaType<ElementStyle>::Register(L);
     LuaType<Element>::Register(L);
         //things that inherit from Element
         LuaType<Document>::Register(L);
-        LuaType<ElementStyle>::Register(L);
         //controls that inherit from Element
         LuaType<Rocket::Controls::ElementTabSet>::Register(L);
         LuaType<Rocket::Controls::ElementDataGrid>::Register(L);

+ 31 - 1
Source/Core/Lua/LuaTypeTemplateSpec.inl

@@ -214,11 +214,36 @@ void LuaType<Vector2i>::extra_init(lua_State* L, int metatable_index)
     Elements
 
 */
-
+template<> void LuaType<Element>::extra_init(lua_State* L, int metatable_index)
+{
+    int top = lua_gettop(L);
+    //build the Element.etype table
+    lua_newtable(L);
+    lua_pushinteger(L,TDATAGRID);
+    lua_setfield(L,-2,"datagrid");
+    lua_pushinteger(L,TDATASELECT);
+    lua_setfield(L,-2,"dataselect");
+    lua_pushinteger(L,TELEMENT);
+    lua_setfield(L,-2,"element");
+    lua_pushinteger(L,TFORM);
+    lua_setfield(L,-2,"form");
+    lua_pushinteger(L,TINPUT);
+    lua_setfield(L,-2,"input");
+    lua_pushinteger(L,TSELECT);
+    lua_setfield(L,-2,"select");
+    lua_pushinteger(L,TTABSET);
+    lua_setfield(L,-2,"tabset");
+    lua_pushinteger(L,TTEXTAREA);
+    lua_setfield(L,-2,"textarea");
+    
+    lua_setfield(L,metatable_index-1,"etype");
+    lua_settop(L,top);
+}
 
 template<> void LuaType<Document>::extra_init(lua_State* L, int metatable_index)
 {
     //we will inherit from Element
+    LuaType<Element>::extra_init(L,metatable_index);
     LuaType<Element>::_regfunctions(L,metatable_index,metatable_index - 1);
 }
 
@@ -235,11 +260,13 @@ template<> void LuaType<ElementStyle>::extra_init(lua_State* L, int metatable_in
 template<> void LuaType<ElementForm>::extra_init(lua_State* L, int metatable_index)
 {
     //inherit from Element
+    LuaType<Element>::extra_init(L,metatable_index);
     LuaType<Element>::_regfunctions(L,metatable_index,metatable_index-1);
 }
 
 template<> void LuaType<ElementFormControl>::extra_init(lua_State* L, int metatable_index)
 {
+    LuaType<Element>::extra_init(L,metatable_index);
     LuaType<Element>::_regfunctions(L,metatable_index,metatable_index-1);
 }
 
@@ -273,16 +300,19 @@ template<> void LuaType<ElementFormControlTextArea>::extra_init(lua_State* L, in
 
 template<> void LuaType<ElementDataGrid>::extra_init(lua_State* L, int metatable_index)
 {
+    LuaType<Element>::extra_init(L,metatable_index);
     LuaType<Element>::_regfunctions(L,metatable_index,metatable_index-1);
 }
 
 template<> void LuaType<ElementDataGridRow>::extra_init(lua_State* L, int metatable_index)
 {
+    LuaType<Element>::extra_init(L,metatable_index);
     LuaType<Element>::_regfunctions(L,metatable_index,metatable_index-1);
 }
 
 template<> void LuaType<ElementTabSet>::extra_init(lua_State* L, int metatable_index)
 {
+    LuaType<Element>::extra_init(L,metatable_index);
     LuaType<Element>::_regfunctions(L,metatable_index,metatable_index-1);
 }