1 /*
2 * This source file is part of libRocket, the HTML/CSS Interface Middleware
3 *
4 * For the latest information, see http://www.librocket.com
5 *
6 * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 *
26 */
27
28 #include "precompiled.h"
29 #include "RocketContextsProxy.h"
30 #include <Rocket/Core/Context.h>
31 #include <Rocket/Core/Core.h>
32
33
34 namespace Rocket {
35 namespace Core {
36 namespace Lua {
37
ExtraInit(lua_State * L,int metatable_index)38 template<> void ExtraInit<RocketContextsProxy>(lua_State* L, int metatable_index)
39 {
40 lua_pushcfunction(L,RocketContextsProxy__index);
41 lua_setfield(L,metatable_index,"__index");
42 lua_pushcfunction(L,RocketContextsProxy__pairs);
43 lua_setfield(L,metatable_index,"__pairs");
44 lua_pushcfunction(L,RocketContextsProxy__ipairs);
45 lua_setfield(L,metatable_index,"__ipairs");
46 }
47
RocketContextsProxy__index(lua_State * L)48 int RocketContextsProxy__index(lua_State* L)
49 {
50 /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
51 int keytype = lua_type(L,2);
52 if(keytype == LUA_TSTRING || keytype == LUA_TNUMBER) //only valid key types
53 {
54 RocketContextsProxy* obj = LuaType<RocketContextsProxy>::check(L,1);
55 LUACHECKOBJ(obj);
56 if(keytype == LUA_TSTRING)
57 {
58 const char* key = lua_tostring(L,2);
59 LuaType<Context>::push(L,GetContext(key));
60 }
61 else
62 {
63 int key = luaL_checkinteger(L,2);
64 LuaType<Context>::push(L,GetContext(key));
65 }
66 return 1;
67 }
68 else
69 return LuaType<RocketContextsProxy>::index(L);
70 }
71
72
73 //[1] is the object, [2] is the last used key, [3] is the userdata
RocketContextsProxy__pairs(lua_State * L)74 int RocketContextsProxy__pairs(lua_State* L)
75 {
76 RocketContextsProxy* obj = LuaType<RocketContextsProxy>::check(L,1);
77 LUACHECKOBJ(obj);
78 int* pindex = (int*)lua_touserdata(L,3);
79 if((*pindex) == -1)
80 *pindex = 0;
81 Context* value = NULL;
82 if((*pindex)++ < GetNumContexts())
83 {
84 value = GetContext(*pindex);
85 }
86 if(value == NULL)
87 {
88 lua_pushnil(L);
89 lua_pushnil(L);
90 }
91 else
92 {
93 lua_pushstring(L,value->GetName().CString());
94 LuaType<Context>::push(L,value);
95 }
96 return 2;
97 }
98
99 //[1] is the object, [2] is the last used key, [3] is the userdata
RocketContextsProxy__ipairs(lua_State * L)100 int RocketContextsProxy__ipairs(lua_State* L)
101 {
102 RocketContextsProxy* obj = LuaType<RocketContextsProxy>::check(L,1);
103 LUACHECKOBJ(obj);
104 int* pindex = (int*)lua_touserdata(L,3);
105 if((*pindex) == -1)
106 *pindex = 0;
107 Context* value = NULL;
108 if((*pindex)++ < GetNumContexts())
109 {
110 value = GetContext(*pindex);
111 }
112 if(value == NULL)
113 {
114 lua_pushnil(L);
115 lua_pushnil(L);
116 }
117 else
118 {
119 lua_pushinteger(L,(*pindex)-1);
120 LuaType<Context>::push(L,value);
121 }
122 return 2;
123 }
124
125 RegType<RocketContextsProxy> RocketContextsProxyMethods[] =
126 {
127 { NULL, NULL },
128 };
129 luaL_Reg RocketContextsProxyGetters[] =
130 {
131 { NULL, NULL },
132 };
133 luaL_Reg RocketContextsProxySetters[] =
134 {
135 { NULL, NULL },
136 };
137
138 LUACORETYPEDEFINE(RocketContextsProxy,false)
139 }
140 }
141 }