ElementChildNodesProxy.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "ElementChildNodesProxy.h"
  2. #include "Element.h"
  3. #include "Pairs.h"
  4. namespace Rml {
  5. namespace Lua {
  6. template <>
  7. void ExtraInit<ElementChildNodesProxy>(lua_State* L, int metatable_index)
  8. {
  9. lua_pushcfunction(L, ElementChildNodesProxy__index);
  10. lua_setfield(L, metatable_index, "__index");
  11. lua_pushcfunction(L, ElementChildNodesProxy__pairs);
  12. lua_setfield(L, metatable_index, "__pairs");
  13. lua_pushcfunction(L, ElementChildNodesProxy__len);
  14. lua_setfield(L, metatable_index, "__len");
  15. }
  16. int ElementChildNodesProxy__index(lua_State* L)
  17. {
  18. /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
  19. int keytype = lua_type(L, 2);
  20. if (keytype == LUA_TNUMBER) // only valid key types
  21. {
  22. ElementChildNodesProxy* obj = LuaType<ElementChildNodesProxy>::check(L, 1);
  23. RMLUI_CHECK_OBJ(obj);
  24. int key = (int)luaL_checkinteger(L, 2);
  25. Element* child = obj->owner->GetChild(key - 1);
  26. LuaType<Element>::push(L, child, false);
  27. return 1;
  28. }
  29. else
  30. return LuaType<ElementChildNodesProxy>::index(L);
  31. }
  32. int ElementChildNodesProxy__pairs(lua_State* L)
  33. {
  34. return MakeIntPairs(L);
  35. }
  36. int ElementChildNodesProxy__len(lua_State* L)
  37. {
  38. ElementChildNodesProxy* obj = LuaType<ElementChildNodesProxy>::check(L, 1);
  39. RMLUI_CHECK_OBJ(obj);
  40. lua_pushinteger(L, obj->owner->GetNumChildren());
  41. ;
  42. return 1;
  43. }
  44. RegType<ElementChildNodesProxy> ElementChildNodesProxyMethods[] = {
  45. {nullptr, nullptr},
  46. };
  47. luaL_Reg ElementChildNodesProxyGetters[] = {
  48. {nullptr, nullptr},
  49. };
  50. luaL_Reg ElementChildNodesProxySetters[] = {
  51. {nullptr, nullptr},
  52. };
  53. RMLUI_LUATYPE_DEFINE(ElementChildNodesProxy)
  54. } // namespace Lua
  55. } // namespace Rml