ElementStyleProxy.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #include "precompiled.h"
  28. #include <Rocket/Core/Lua/LuaType.h>
  29. #include <Rocket/Core/Lua/lua.hpp>
  30. #include "ElementStyleProxy.h"
  31. namespace Rocket {
  32. namespace Core {
  33. namespace Lua {
  34. template<> void ExtraInit<ElementStyleProxy>(lua_State* L, int metatable_index)
  35. {
  36. lua_pushcfunction(L,ElementStyleProxy__index);
  37. lua_setfield(L,metatable_index,"__index");
  38. lua_pushcfunction(L,ElementStyleProxy__newindex);
  39. lua_setfield(L,metatable_index,"__newindex");
  40. lua_pushcfunction(L,ElementStyleProxy__pairs);
  41. lua_setfield(L,metatable_index,"__pairs");
  42. lua_pushcfunction(L,ElementStyleProxy__ipairs);
  43. lua_setfield(L,metatable_index,"__ipairs");
  44. }
  45. int ElementStyleProxy__index(lua_State* L)
  46. {
  47. /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
  48. int keytype = lua_type(L,2);
  49. if(keytype == LUA_TSTRING) //if we are trying to access a string, then we will assume that it is a property
  50. {
  51. ElementStyleProxy* es = LuaType<ElementStyleProxy>::check(L,1);
  52. LUACHECKOBJ(es);
  53. const Property* prop = es->owner->GetProperty(lua_tostring(L,2));
  54. LUACHECKOBJ(prop)
  55. lua_pushstring(L,prop->ToString().c_str());
  56. return 1;
  57. }
  58. else //if it wasn't trying to get a string
  59. {
  60. lua_settop(L,2);
  61. return LuaType<ElementStyleProxy>::index(L);
  62. }
  63. }
  64. int ElementStyleProxy__newindex(lua_State* L)
  65. {
  66. //[1] = obj, [2] = key, [3] = value
  67. ElementStyleProxy* es = LuaType<ElementStyleProxy>::check(L,1);
  68. LUACHECKOBJ(es);
  69. int keytype = lua_type(L,2);
  70. int valuetype = lua_type(L,3);
  71. if(keytype == LUA_TSTRING )
  72. {
  73. const char* key = lua_tostring(L,2);
  74. if(valuetype == LUA_TSTRING)
  75. {
  76. const char* value = lua_tostring(L,3);
  77. lua_pushboolean(L,es->owner->SetProperty(key,value));
  78. return 1;
  79. }
  80. else if (valuetype == LUA_TNIL)
  81. {
  82. es->owner->RemoveProperty(key);
  83. return 0;
  84. }
  85. }
  86. //everything else returns when it needs to, so we are safe to pass it
  87. //on if needed
  88. lua_settop(L,3);
  89. return LuaType<ElementStyleProxy>::newindex(L);
  90. }
  91. //[1] is the object, [2] is the last used key, [3] is the userdata
  92. int ElementStyleProxy__pairs(lua_State* L)
  93. {
  94. ElementStyleProxy* obj = LuaType<ElementStyleProxy>::check(L,1);
  95. LUACHECKOBJ(obj);
  96. int* pindex = (int*)lua_touserdata(L,3);
  97. if((*pindex) == -1)
  98. *pindex = 0;
  99. //iterate variables
  100. String key,val;
  101. const Property* prop;
  102. if(obj->owner->IterateProperties((*pindex),key,prop))
  103. {
  104. prop->definition->GetValue(val,*prop);
  105. lua_pushstring(L,key.c_str());
  106. lua_pushstring(L,val.c_str());
  107. }
  108. else
  109. {
  110. lua_pushnil(L);
  111. lua_pushnil(L);
  112. }
  113. return 2;
  114. }
  115. //only indexed by string
  116. int ElementStyleProxy__ipairs(lua_State* L)
  117. {
  118. lua_pushnil(L);
  119. lua_pushnil(L);
  120. return 2;
  121. }
  122. RegType<ElementStyleProxy> ElementStyleProxyMethods[] =
  123. {
  124. { NULL, NULL },
  125. };
  126. luaL_Reg ElementStyleProxyGetters[] =
  127. {
  128. { NULL, NULL },
  129. };
  130. luaL_Reg ElementStyleProxySetters[] =
  131. {
  132. { NULL, NULL },
  133. };
  134. LUACORETYPEDEFINE(ElementStyleProxy,false)
  135. }
  136. }
  137. }