Browse Source

One last proxy table, rocket.contexts

Made a type LuaRocket, and a global variable rocket in Lua where the functions and parameters will be called from, so it has the same syntax as the rest of the API.
Nate Starkey 13 years ago
parent
commit
9a137588e7

+ 8 - 0
Build/RocketCoreLua.vcproj

@@ -231,6 +231,14 @@
 				RelativePath="..\Source\Core\Lua\Rocket.h"
 				>
 			</File>
+			<File
+				RelativePath="..\Source\Core\Lua\RocketContextsProxy.cpp"
+				>
+			</File>
+			<File
+				RelativePath="..\Source\Core\Lua\RocketContextsProxy.h"
+				>
+			</File>
 			<File
 				RelativePath="..\Source\Core\Lua\Utilities.cpp"
 				>

+ 1 - 1
Samples/luainvaders/lua/start.lua

@@ -32,7 +32,7 @@ Formatters["ship"] = DataFormatter.new("ship",SecondFormatData)
 
 
 function Startup()
-	maincontext = rocket.contexts()["main"]
+	maincontext = rocket.contexts["main"]
 	maincontext:LoadDocument("data/background.rml"):Show()
 	maincontext:LoadDocument("data/main_menu.rml"):Show()
 end

+ 1 - 0
Source/Core/Lua/EventParametersProxy.cpp

@@ -68,6 +68,7 @@ int EventParametersProxy__index(lua_State* L)
 int EventParametersProxy__pairs(lua_State* L)
 {
     EventParametersProxy* obj = LuaType<EventParametersProxy>::check(L,1);
+    LUACHECKOBJ(obj);
     int* pindex = (int*)lua_touserdata(L,3);
     if((*pindex) == -1)
         *pindex = 0;

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

@@ -54,6 +54,7 @@
 #include "ElementChildNodesProxy.h"
 #include "ElementText.h"
 #include "GlobalLuaFunctions.h"
+#include "RocketContextsProxy.h"
 
 namespace Rocket {
 namespace Core {
@@ -61,6 +62,8 @@ namespace Lua {
 lua_State* Interpreter::_L = NULL;
 //typedefs for nicer Lua names
 typedef Rocket::Core::ElementDocument Document;
+//global variable 
+LuaRocket rocket = LuaRocket();
 
 void Interpreter::Startup()
 {
@@ -86,14 +89,18 @@ void Interpreter::RegisterCoreTypes(lua_State* L)
         LuaType<ElementText>::Register(L);
     LuaType<Event>::Register(L);
     LuaType<Context>::Register(L);
-    LuaType<rocket>::Register(L);
+    LuaType<LuaRocket>::Register(L);
     LuaType<ElementInstancer>::Register(L);
     //Proxy tables
     LuaType<ContextDocumentsProxy>::Register(L);
     LuaType<EventParametersProxy>::Register(L);
     LuaType<ElementAttributesProxy>::Register(L);
     LuaType<ElementChildNodesProxy>::Register(L);
+    LuaType<RocketContextsProxy>::Register(L);
     OverrideLuaGlobalFunctions(L);
+    //push the global variable "rocket" to use the "Rocket" methods
+    LuaType<LuaRocket>::push(L,&rocket,false);
+    lua_setglobal(L,"rocket");
 }
 
 

+ 18 - 40
Source/Core/Lua/Rocket.cpp

@@ -31,36 +31,23 @@
 #include <Rocket/Core/Input.h>
 #include "ElementInstancer.h"
 #include "LuaElementInstancer.h"
+#include "RocketContextsProxy.h"
 
 namespace Rocket {
 namespace Core {
 namespace Lua {
 
-template<> void ExtraInit<rocket>(lua_State* L, int metatable_index)
+template<> void ExtraInit<LuaRocket>(lua_State* L, int metatable_index)
 {
     //because of the way LuaType::Register is done, we know that the methods table is directly
     //before the metatable 
     int method_index = metatable_index - 1;
-
-    lua_pushcfunction(L,rocketCreateContext);
-    lua_setfield(L,method_index,"CreateContext");
-
-    lua_pushcfunction(L,rocketLoadFontFace);
-    lua_setfield(L,method_index,"LoadFontFace");
-
-    lua_pushcfunction(L,rocketRegisterTag);
-    lua_setfield(L,method_index,"RegisterTag");
-
-    rocketEnumkey_identifier(L);
+    LuaRocketEnumkey_identifier(L);
     lua_setfield(L,method_index,"key_identifier");
-
-    lua_pushcfunction(L,rocketGetAttrcontexts);
-    lua_setfield(L,method_index,"contexts");
-
     return;
 }
 
-int rocketCreateContext(lua_State* L)
+int LuaRocketCreateContext(lua_State* L, LuaRocket* obj)
 {
     const char* name = luaL_checkstring(L,1);
     Vector2i* dimensions = LuaType<Vector2i>::check(L,2);
@@ -76,14 +63,14 @@ int rocketCreateContext(lua_State* L)
     return 1;
 }
 
-int rocketLoadFontFace(lua_State* L)
+int LuaRocketLoadFontFace(lua_State* L, LuaRocket* obj)
 {
     const char* file = luaL_checkstring(L,1);
     lua_pushboolean(L,FontDatabase::LoadFontFace(file));
     return 1;
 }
 
-int rocketRegisterTag(lua_State* L)
+int LuaRocketRegisterTag(lua_State* L, LuaRocket* obj)
 {
     const char* tag = luaL_checkstring(L,1);
     LuaElementInstancer* lei = (LuaElementInstancer*)LuaType<ElementInstancer>::check(L,2);
@@ -92,27 +79,14 @@ int rocketRegisterTag(lua_State* L)
     return 0;
 }
 
-int rocketGetAttrcontexts(lua_State* L)
+int LuaRocketGetAttrcontexts(lua_State* L)
 {
-    lua_newtable(L);
-    int tbl = lua_gettop(L);
-    int numcontexts = GetNumContexts();
-    Context* cont;
-    for(int i = 0; i < numcontexts; i++)
-    {
-        cont = GetContext(i);
-        if(cont != NULL)
-        {
-            LuaType<Context>::push(L,cont,false);
-            lua_pushvalue(L,-1); //duplicate the top of the stack, because we are indexing by string and integral key so we need two
-            lua_rawseti(L,tbl,i);
-            lua_setfield(L,tbl,cont->GetName().CString());
-        }
-    }
+    RocketContextsProxy* proxy = new RocketContextsProxy();
+    LuaType<RocketContextsProxy>::push(L,proxy,true);
     return 1;
 }
 
-void rocketEnumkey_identifier(lua_State* L)
+void LuaRocketEnumkey_identifier(lua_State* L)
 {
     lua_newtable(L);
     int tbl = lua_gettop(L);
@@ -294,22 +268,26 @@ void rocketEnumkey_identifier(lua_State* L)
 }
 
 
-RegType<rocket> rocketMethods[] = 
+RegType<LuaRocket> LuaRocketMethods[] = 
 {
+    LUAMETHOD(LuaRocket,CreateContext)
+    LUAMETHOD(LuaRocket,LoadFontFace)
+    LUAMETHOD(LuaRocket,RegisterTag)
     { NULL, NULL },
 };
 
-luaL_reg rocketGetters[] = 
+luaL_reg LuaRocketGetters[] = 
 {
+    LUAGETTER(LuaRocket,contexts)
     { NULL, NULL },
 };
 
-luaL_reg rocketSetters[] = 
+luaL_reg LuaRocketSetters[] = 
 {
     { NULL, NULL },
 };
 
-LUATYPEDEFINE(rocket,false)
+LUATYPEDEFINE(LuaRocket,false)
 }
 }
 }

+ 13 - 12
Source/Core/Lua/Rocket.h

@@ -36,22 +36,23 @@ namespace Core {
 namespace Lua {
 #define ROCKETLUA_INPUTENUM(keyident,tbl) lua_pushinteger(L,Input::KI_##keyident); lua_setfield(L,(tbl),#keyident);
 
-//just need a class to take up a type name
-class rocket { int to_remove_warning; };
+//just need a class to take up a type name, and a single object to be able to be pushed to Lua
+class LuaRocket { int to_remove_warning; }; //instance of object defined in Interpreter.cpp
 
-template<> void ExtraInit<rocket>(lua_State* L, int metatable_index);
-int rocketCreateContext(lua_State* L);
-int rocketLoadFontFace(lua_State* L);
-int rocketRegisterTag(lua_State* L);
-int rocketGetAttrcontexts(lua_State* L);
+template<> void ExtraInit<LuaRocket>(lua_State* L, int metatable_index);
+int LuaRocketCreateContext(lua_State* L, LuaRocket* obj);
+int LuaRocketLoadFontFace(lua_State* L, LuaRocket* obj);
+int LuaRocketRegisterTag(lua_State* L, LuaRocket* obj);
 
-void rocketEnumkey_identifier(lua_State* L);
+int LuaRocketGetAttrcontexts(lua_State* L);
 
-RegType<rocket> rocketMethods[];
-luaL_reg rocketGetters[];
-luaL_reg rocketSetters[];
+void LuaRocketEnumkey_identifier(lua_State* L);
 
-LUATYPEDECLARE(rocket)
+RegType<LuaRocket> LuaRocketMethods[];
+luaL_reg LuaRocketGetters[];
+luaL_reg LuaRocketSetters[];
+
+LUATYPEDECLARE(LuaRocket)
 }
 }
 }

+ 141 - 0
Source/Core/Lua/RocketContextsProxy.cpp

@@ -0,0 +1,141 @@
+/*
+ * This source file is part of libRocket, the HTML/CSS Interface Middleware
+ *
+ * For the latest information, see http://www.librocket.com
+ *
+ * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+ 
+#include "precompiled.h"
+#include "RocketContextsProxy.h"
+#include <Rocket/Core/Context.h>
+#include <Rocket/Core/Core.h>
+
+
+namespace Rocket {
+namespace Core {
+namespace Lua {
+
+template<> void ExtraInit<RocketContextsProxy>(lua_State* L, int metatable_index)
+{
+    lua_pushcfunction(L,RocketContextsProxy__index);
+    lua_setfield(L,metatable_index,"__index");
+    lua_pushcfunction(L,RocketContextsProxy__pairs);
+    lua_setfield(L,metatable_index,"__pairs");
+    lua_pushcfunction(L,RocketContextsProxy__ipairs);
+    lua_setfield(L,metatable_index,"__ipairs");
+}
+
+int RocketContextsProxy__index(lua_State* L)
+{
+    /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
+    int keytype = lua_type(L,2);
+    if(keytype == LUA_TSTRING || keytype == LUA_TNUMBER) //only valid key types
+    {
+        RocketContextsProxy* obj = LuaType<RocketContextsProxy>::check(L,1);
+        LUACHECKOBJ(obj);
+        if(keytype == LUA_TSTRING)
+        {
+            const char* key = lua_tostring(L,2);
+            LuaType<Context>::push(L,GetContext(key));
+        }
+        else
+        {
+            int key = luaL_checkint(L,2);
+            LuaType<Context>::push(L,GetContext(key));
+        }
+        return 1;
+    }
+    else
+        return LuaType<RocketContextsProxy>::index(L);
+}
+
+
+//[1] is the object, [2] is the last used key, [3] is the userdata
+int RocketContextsProxy__pairs(lua_State* L)
+{
+    RocketContextsProxy* obj = LuaType<RocketContextsProxy>::check(L,1);
+    LUACHECKOBJ(obj);
+    int* pindex = (int*)lua_touserdata(L,3);
+    if((*pindex) == -1)
+        *pindex = 0;
+    Context* value = NULL;
+    if((*pindex)++ < GetNumContexts())
+    {
+        value = GetContext(*pindex);
+    }
+    if(value == NULL)
+    {
+        lua_pushnil(L);
+        lua_pushnil(L);
+    }
+    else
+    {
+        lua_pushstring(L,value->GetName().CString());
+        LuaType<Context>::push(L,value);
+    }
+    return 2;
+}
+
+//[1] is the object, [2] is the last used key, [3] is the userdata
+int RocketContextsProxy__ipairs(lua_State* L)
+{
+    RocketContextsProxy* obj = LuaType<RocketContextsProxy>::check(L,1);
+    LUACHECKOBJ(obj);
+    int* pindex = (int*)lua_touserdata(L,3);
+    if((*pindex) == -1)
+        *pindex = 0;
+    Context* value = NULL;
+    if((*pindex)++ < GetNumContexts())
+    {
+        value = GetContext(*pindex);
+    }
+    if(value == NULL)
+    {
+        lua_pushnil(L);
+        lua_pushnil(L);
+    }
+    else
+    {
+        lua_pushinteger(L,(*pindex)-1);
+        LuaType<Context>::push(L,value);
+    }
+    return 2;
+}
+
+RegType<RocketContextsProxy> RocketContextsProxyMethods[] =
+{
+    { NULL, NULL },
+};
+luaL_reg RocketContextsProxyGetters[] =
+{
+    { NULL, NULL },
+};
+luaL_reg RocketContextsProxySetters[] =
+{
+    { NULL, NULL },
+};
+
+LUATYPEDEFINE(RocketContextsProxy,false)
+}
+}
+}

+ 54 - 0
Source/Core/Lua/RocketContextsProxy.h

@@ -0,0 +1,54 @@
+/*
+ * This source file is part of libRocket, the HTML/CSS Interface Middleware
+ *
+ * For the latest information, see http://www.librocket.com
+ *
+ * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+ 
+#ifndef ROCKETCORELUAROCKETCONTEXTSPROXY_H
+#define ROCKETCORELUAROCKETCONTEXTSPROXY_H
+
+#include <Rocket/Core/Lua/lua.hpp>
+#include <Rocket/Core/Lua/LuaType.h>
+
+
+namespace Rocket {
+namespace Core {
+namespace Lua {
+//where owner is the Element that we should look up information from
+struct RocketContextsProxy { void* nothing;  };
+
+template<> void ExtraInit<RocketContextsProxy>(lua_State* L, int metatable_index);
+int RocketContextsProxy__index(lua_State* L);
+int RocketContextsProxy__pairs(lua_State* L);
+int RocketContextsProxy__ipairs(lua_State* L);
+
+RegType<RocketContextsProxy> RocketContextsProxyMethods[];
+luaL_reg RocketContextsProxyGetters[];
+luaL_reg RocketContextsProxySetters[];
+
+LUATYPEDECLARE(RocketContextsProxy)
+}
+}
+}
+#endif