RmlUiContextsProxy.cpp 4.1 KB

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