RmlUiContextsProxy.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "RmlUiContextsProxy.h"
  29. #include <RmlUi/Core/Context.h>
  30. #include <RmlUi/Core/Core.h>
  31. namespace Rml {
  32. namespace Lua {
  33. template<> void ExtraInit<RmlUiContextsProxy>(lua_State* L, int metatable_index)
  34. {
  35. lua_pushcfunction(L,RmlUiContextsProxy__index);
  36. lua_setfield(L,metatable_index,"__index");
  37. lua_pushcfunction(L,RmlUiContextsProxy__pairs);
  38. lua_setfield(L,metatable_index,"__pairs");
  39. lua_pushcfunction(L,RmlUiContextsProxy__ipairs);
  40. lua_setfield(L,metatable_index,"__ipairs");
  41. }
  42. int RmlUiContextsProxy__index(lua_State* L)
  43. {
  44. /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
  45. int keytype = lua_type(L,2);
  46. if(keytype == LUA_TSTRING || keytype == LUA_TNUMBER) //only valid key types
  47. {
  48. RmlUiContextsProxy* obj = LuaType<RmlUiContextsProxy>::check(L,1);
  49. RMLUI_CHECK_OBJ(obj);
  50. if(keytype == LUA_TSTRING)
  51. {
  52. const char* key = lua_tostring(L,2);
  53. LuaType<Context>::push(L,GetContext(key));
  54. }
  55. else
  56. {
  57. int key = (int)luaL_checkinteger(L,2);
  58. LuaType<Context>::push(L,GetContext(key));
  59. }
  60. return 1;
  61. }
  62. else
  63. return LuaType<RmlUiContextsProxy>::index(L);
  64. }
  65. //[1] is the object, [2] is the last used key, [3] is the userdata
  66. int RmlUiContextsProxy__pairs(lua_State* L)
  67. {
  68. RmlUiContextsProxy* obj = LuaType<RmlUiContextsProxy>::check(L,1);
  69. RMLUI_CHECK_OBJ(obj);
  70. int* pindex = (int*)lua_touserdata(L,3);
  71. if((*pindex) == -1)
  72. *pindex = 0;
  73. Context* value = nullptr;
  74. if((*pindex)++ < GetNumContexts())
  75. {
  76. value = GetContext(*pindex);
  77. }
  78. if(value == nullptr)
  79. {
  80. lua_pushnil(L);
  81. lua_pushnil(L);
  82. }
  83. else
  84. {
  85. lua_pushstring(L,value->GetName().c_str());
  86. LuaType<Context>::push(L,value);
  87. }
  88. return 2;
  89. }
  90. //[1] is the object, [2] is the last used key, [3] is the userdata
  91. int RmlUiContextsProxy__ipairs(lua_State* L)
  92. {
  93. RmlUiContextsProxy* obj = LuaType<RmlUiContextsProxy>::check(L,1);
  94. RMLUI_CHECK_OBJ(obj);
  95. int* pindex = (int*)lua_touserdata(L,3);
  96. if((*pindex) == -1)
  97. *pindex = 0;
  98. Context* value = nullptr;
  99. if((*pindex)++ < GetNumContexts())
  100. {
  101. value = GetContext(*pindex);
  102. }
  103. if(value == nullptr)
  104. {
  105. lua_pushnil(L);
  106. lua_pushnil(L);
  107. }
  108. else
  109. {
  110. lua_pushinteger(L,(*pindex)-1);
  111. LuaType<Context>::push(L,value);
  112. }
  113. return 2;
  114. }
  115. RegType<RmlUiContextsProxy> RmlUiContextsProxyMethods[] =
  116. {
  117. { nullptr, nullptr },
  118. };
  119. luaL_Reg RmlUiContextsProxyGetters[] =
  120. {
  121. { nullptr, nullptr },
  122. };
  123. luaL_Reg RmlUiContextsProxySetters[] =
  124. {
  125. { nullptr, nullptr },
  126. };
  127. RMLUI_LUATYPE_DEFINE(RmlUiContextsProxy)
  128. } // namespace Lua
  129. } // namespace Rml