ElementAttributesProxy.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "ElementAttributesProxy.h"
  2. #include "Pairs.h"
  3. #include <RmlUi/Core/Variant.h>
  4. #include <RmlUi/Lua/Utilities.h>
  5. namespace Rml {
  6. namespace Lua {
  7. template <>
  8. void ExtraInit<ElementAttributesProxy>(lua_State* L, int metatable_index)
  9. {
  10. lua_pushcfunction(L, ElementAttributesProxy__index);
  11. lua_setfield(L, metatable_index, "__index");
  12. lua_pushcfunction(L, ElementAttributesProxy__pairs);
  13. lua_setfield(L, metatable_index, "__pairs");
  14. }
  15. int ElementAttributesProxy__index(lua_State* L)
  16. {
  17. /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
  18. int keytype = lua_type(L, 2);
  19. if (keytype == LUA_TSTRING) // only valid key types
  20. {
  21. ElementAttributesProxy* obj = LuaType<ElementAttributesProxy>::check(L, 1);
  22. RMLUI_CHECK_OBJ(obj);
  23. const char* key = lua_tostring(L, 2);
  24. Variant* attr = obj->owner->GetAttribute(key);
  25. PushVariant(L, attr); // Utilities.h
  26. return 1;
  27. }
  28. else
  29. return LuaType<ElementAttributesProxy>::index(L);
  30. }
  31. int ElementAttributesProxy__pairs(lua_State* L)
  32. {
  33. ElementAttributesProxy* obj = LuaType<ElementAttributesProxy>::check(L, 1);
  34. RMLUI_CHECK_OBJ(obj);
  35. return MakePairs(L, obj->owner->GetAttributes());
  36. }
  37. RegType<ElementAttributesProxy> ElementAttributesProxyMethods[] = {
  38. {nullptr, nullptr},
  39. };
  40. luaL_Reg ElementAttributesProxyGetters[] = {
  41. {nullptr, nullptr},
  42. };
  43. luaL_Reg ElementAttributesProxySetters[] = {
  44. {nullptr, nullptr},
  45. };
  46. RMLUI_LUATYPE_DEFINE(ElementAttributesProxy)
  47. } // namespace Lua
  48. } // namespace Rml