ElementStyleProxy.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "ElementStyleProxy.h"
  2. #include <RmlUi/Core/Element.h>
  3. #include <RmlUi/Core/PropertiesIteratorView.h>
  4. #include <RmlUi/Core/Property.h>
  5. #include <RmlUi/Core/PropertyDefinition.h>
  6. #include <RmlUi/Lua/IncludeLua.h>
  7. #include <RmlUi/Lua/LuaType.h>
  8. namespace Rml {
  9. namespace Lua {
  10. template <>
  11. void ExtraInit<ElementStyleProxy>(lua_State* L, int metatable_index)
  12. {
  13. lua_pushcfunction(L, ElementStyleProxy__index);
  14. lua_setfield(L, metatable_index, "__index");
  15. lua_pushcfunction(L, ElementStyleProxy__newindex);
  16. lua_setfield(L, metatable_index, "__newindex");
  17. lua_pushcfunction(L, ElementStyleProxy__pairs);
  18. lua_setfield(L, metatable_index, "__pairs");
  19. }
  20. int ElementStyleProxy__index(lua_State* L)
  21. {
  22. /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
  23. int keytype = lua_type(L, 2);
  24. if (keytype == LUA_TSTRING) // if we are trying to access a string, then we will assume that it is a property
  25. {
  26. ElementStyleProxy* es = LuaType<ElementStyleProxy>::check(L, 1);
  27. RMLUI_CHECK_OBJ(es);
  28. const Property* prop = es->owner->GetProperty(lua_tostring(L, 2));
  29. RMLUI_CHECK_OBJ(prop)
  30. lua_pushstring(L, prop->ToString().c_str());
  31. return 1;
  32. }
  33. else // if it wasn't trying to get a string
  34. {
  35. lua_settop(L, 2);
  36. return LuaType<ElementStyleProxy>::index(L);
  37. }
  38. }
  39. int ElementStyleProxy__newindex(lua_State* L)
  40. {
  41. //[1] = obj, [2] = key, [3] = value
  42. ElementStyleProxy* es = LuaType<ElementStyleProxy>::check(L, 1);
  43. RMLUI_CHECK_OBJ(es);
  44. int keytype = lua_type(L, 2);
  45. int valuetype = lua_type(L, 3);
  46. if (keytype == LUA_TSTRING)
  47. {
  48. const char* key = lua_tostring(L, 2);
  49. if (valuetype == LUA_TSTRING)
  50. {
  51. const char* value = lua_tostring(L, 3);
  52. lua_pushboolean(L, es->owner->SetProperty(key, value));
  53. return 1;
  54. }
  55. else if (valuetype == LUA_TNIL)
  56. {
  57. es->owner->RemoveProperty(key);
  58. return 0;
  59. }
  60. }
  61. // everything else returns when it needs to, so we are safe to pass it
  62. // on if needed
  63. lua_settop(L, 3);
  64. return LuaType<ElementStyleProxy>::newindex(L);
  65. }
  66. struct ElementStyleProxyPairs {
  67. static int next(lua_State* L)
  68. {
  69. ElementStyleProxyPairs* self = static_cast<ElementStyleProxyPairs*>(lua_touserdata(L, lua_upvalueindex(1)));
  70. if (self->m_view.AtEnd())
  71. {
  72. return 0;
  73. }
  74. const String& key = self->m_view.GetName();
  75. const Property& property = self->m_view.GetProperty();
  76. String val;
  77. property.definition->GetValue(val, property);
  78. lua_pushlstring(L, key.c_str(), key.size());
  79. lua_pushlstring(L, val.c_str(), val.size());
  80. ++self->m_view;
  81. return 2;
  82. }
  83. static int destroy(lua_State* L)
  84. {
  85. static_cast<ElementStyleProxyPairs*>(lua_touserdata(L, 1))->~ElementStyleProxyPairs();
  86. return 0;
  87. }
  88. static int constructor(lua_State* L, ElementStyleProxy* obj)
  89. {
  90. void* storage = lua_newuserdata(L, sizeof(ElementStyleProxyPairs));
  91. if (luaL_newmetatable(L, "RmlUi::Lua::ElementStyleProxyPairs"))
  92. {
  93. static luaL_Reg mt[] = {
  94. {"__gc", destroy},
  95. {NULL, NULL},
  96. };
  97. luaL_setfuncs(L, mt, 0);
  98. }
  99. lua_setmetatable(L, -2);
  100. lua_pushcclosure(L, next, 1);
  101. new (storage) ElementStyleProxyPairs(obj);
  102. return 1;
  103. }
  104. ElementStyleProxyPairs(ElementStyleProxy* obj) : m_view(obj->owner->IterateLocalProperties()) {}
  105. PropertiesIteratorView m_view;
  106. };
  107. int ElementStyleProxy__pairs(lua_State* L)
  108. {
  109. ElementStyleProxy* obj = LuaType<ElementStyleProxy>::check(L, 1);
  110. RMLUI_CHECK_OBJ(obj);
  111. ElementStyleProxyPairs::constructor(L, obj);
  112. return 1;
  113. }
  114. RegType<ElementStyleProxy> ElementStyleProxyMethods[] = {
  115. {nullptr, nullptr},
  116. };
  117. luaL_Reg ElementStyleProxyGetters[] = {
  118. {nullptr, nullptr},
  119. };
  120. luaL_Reg ElementStyleProxySetters[] = {
  121. {nullptr, nullptr},
  122. };
  123. RMLUI_LUATYPE_DEFINE(ElementStyleProxy)
  124. } // namespace Lua
  125. } // namespace Rml