ElementStyleProxy.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "ElementStyleProxy.h"
  29. #include <RmlUi/Lua/LuaType.h>
  30. #include <RmlUi/Lua/IncludeLua.h>
  31. #include <RmlUi/Core/Element.h>
  32. #include <RmlUi/Core/Property.h>
  33. #include <RmlUi/Core/PropertyDefinition.h>
  34. #include <RmlUi/Core/PropertiesIteratorView.h>
  35. namespace Rml {
  36. namespace Lua {
  37. template<> void ExtraInit<ElementStyleProxy>(lua_State* L, int metatable_index)
  38. {
  39. lua_pushcfunction(L,ElementStyleProxy__index);
  40. lua_setfield(L,metatable_index,"__index");
  41. lua_pushcfunction(L,ElementStyleProxy__newindex);
  42. lua_setfield(L,metatable_index,"__newindex");
  43. lua_pushcfunction(L,ElementStyleProxy__pairs);
  44. lua_setfield(L,metatable_index,"__pairs");
  45. lua_pushcfunction(L,ElementStyleProxy__ipairs);
  46. lua_setfield(L,metatable_index,"__ipairs");
  47. }
  48. int ElementStyleProxy__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) //if we are trying to access a string, then we will assume that it is a property
  53. {
  54. ElementStyleProxy* es = LuaType<ElementStyleProxy>::check(L,1);
  55. RMLUI_CHECK_OBJ(es);
  56. const Property* prop = es->owner->GetProperty(lua_tostring(L,2));
  57. RMLUI_CHECK_OBJ(prop)
  58. lua_pushstring(L,prop->ToString().c_str());
  59. return 1;
  60. }
  61. else //if it wasn't trying to get a string
  62. {
  63. lua_settop(L,2);
  64. return LuaType<ElementStyleProxy>::index(L);
  65. }
  66. }
  67. int ElementStyleProxy__newindex(lua_State* L)
  68. {
  69. //[1] = obj, [2] = key, [3] = value
  70. ElementStyleProxy* es = LuaType<ElementStyleProxy>::check(L,1);
  71. RMLUI_CHECK_OBJ(es);
  72. int keytype = lua_type(L,2);
  73. int valuetype = lua_type(L,3);
  74. if(keytype == LUA_TSTRING )
  75. {
  76. const char* key = lua_tostring(L,2);
  77. if(valuetype == LUA_TSTRING)
  78. {
  79. const char* value = lua_tostring(L,3);
  80. lua_pushboolean(L,es->owner->SetProperty(key,value));
  81. return 1;
  82. }
  83. else if (valuetype == LUA_TNIL)
  84. {
  85. es->owner->RemoveProperty(key);
  86. return 0;
  87. }
  88. }
  89. //everything else returns when it needs to, so we are safe to pass it
  90. //on if needed
  91. lua_settop(L,3);
  92. return LuaType<ElementStyleProxy>::newindex(L);
  93. }
  94. //[1] is the object, [2] is the last used key, [3] is the userdata
  95. int ElementStyleProxy__pairs(lua_State* L)
  96. {
  97. ElementStyleProxy* obj = LuaType<ElementStyleProxy>::check(L,1);
  98. RMLUI_CHECK_OBJ(obj);
  99. int* pindex = (int*)lua_touserdata(L,3);
  100. if ((*pindex) == -1)
  101. *pindex = 0;
  102. int i = 0;
  103. auto it = obj->owner->IterateLocalProperties();
  104. while (i < (*pindex) && !it.AtEnd())
  105. {
  106. ++it;
  107. ++i;
  108. }
  109. if(!it.AtEnd())
  110. {
  111. const String& key = it.GetName();
  112. const Property& property = it.GetProperty();
  113. String val;
  114. property.definition->GetValue(val, property);
  115. lua_pushstring(L,key.c_str());
  116. lua_pushstring(L,val.c_str());
  117. }
  118. else
  119. {
  120. lua_pushnil(L);
  121. lua_pushnil(L);
  122. }
  123. return 2;
  124. }
  125. //only indexed by string
  126. int ElementStyleProxy__ipairs(lua_State* L)
  127. {
  128. lua_pushnil(L);
  129. lua_pushnil(L);
  130. return 2;
  131. }
  132. RegType<ElementStyleProxy> ElementStyleProxyMethods[] =
  133. {
  134. { nullptr, nullptr },
  135. };
  136. luaL_Reg ElementStyleProxyGetters[] =
  137. {
  138. { nullptr, nullptr },
  139. };
  140. luaL_Reg ElementStyleProxySetters[] =
  141. {
  142. { nullptr, nullptr },
  143. };
  144. RMLUI_LUATYPE_DEFINE(ElementStyleProxy)
  145. } // namespace Lua
  146. } // namespace Rml