Utilities.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <RmlUi/Lua/Utilities.h>
  29. #include <RmlUi/Core/Variant.h>
  30. namespace Rml {
  31. namespace Lua {
  32. void PushVariant(lua_State* L, const Variant* var)
  33. {
  34. if (!var)
  35. {
  36. lua_pushnil(L);
  37. return;
  38. }
  39. switch (var->GetType())
  40. {
  41. case Variant::BOOL:
  42. lua_pushboolean(L, var->Get<bool>());
  43. break;
  44. case Variant::BYTE:
  45. case Variant::CHAR:
  46. case Variant::INT:
  47. lua_pushinteger(L, var->Get<int>());
  48. break;
  49. case Variant::INT64:
  50. lua_pushinteger(L, var->Get<int64_t>());
  51. break;
  52. case Variant::UINT:
  53. lua_pushinteger(L, var->Get<unsigned int>());
  54. break;
  55. case Variant::UINT64:
  56. lua_pushinteger(L, var->Get<uint64_t>());
  57. break;
  58. case Variant::FLOAT:
  59. case Variant::DOUBLE:
  60. lua_pushnumber(L, var->Get<double>());
  61. break;
  62. case Variant::COLOURB:
  63. LuaType<Colourb>::push(L, new Colourb(var->Get<Colourb>()), true);
  64. break;
  65. case Variant::COLOURF:
  66. LuaType<Colourf>::push(L, new Colourf(var->Get<Colourf>()), true);
  67. break;
  68. case Variant::STRING:
  69. {
  70. const String& s = var->GetReference<Rml::String>();
  71. lua_pushlstring(L, s.c_str(), s.length());
  72. }
  73. break;
  74. case Variant::VECTOR2:
  75. //according to Variant.inl, it is going to be a Vector2f
  76. LuaType<Vector2f>::push(L, new Vector2f(var->Get<Vector2f>()), true);
  77. break;
  78. case Variant::VOIDPTR:
  79. lua_pushlightuserdata(L, var->Get<void*>());
  80. break;
  81. default:
  82. lua_pushnil(L);
  83. break;
  84. }
  85. }
  86. void GetVariant(lua_State* L, int index, Variant* variant)
  87. {
  88. if (!variant)
  89. return;
  90. switch (lua_type(L, index))
  91. {
  92. case LUA_TBOOLEAN:
  93. *variant = (bool)lua_toboolean(L, index);
  94. break;
  95. case LUA_TNUMBER:
  96. *variant = lua_tonumber(L, index);
  97. break;
  98. case LUA_TSTRING:
  99. *variant = Rml::String(lua_tostring(L, index));
  100. break;
  101. case LUA_TLIGHTUSERDATA:
  102. *variant = lua_touserdata(L, index);
  103. break;
  104. case LUA_TNIL:
  105. default: // todo: support other types
  106. *variant = Variant();
  107. break;
  108. }
  109. }
  110. } // namespace Lua
  111. } // namespace Rml