Utilities.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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::FLOAT:
  53. case Variant::DOUBLE:
  54. lua_pushnumber(L, var->Get<double>());
  55. break;
  56. case Variant::COLOURB:
  57. LuaType<Colourb>::push(L, new Colourb(var->Get<Colourb>()), true);
  58. break;
  59. case Variant::COLOURF:
  60. LuaType<Colourf>::push(L, new Colourf(var->Get<Colourf>()), true);
  61. break;
  62. case Variant::STRING:
  63. {
  64. const String& s = var->GetReference<Rml::String>();
  65. lua_pushlstring(L, s.c_str(), s.length());
  66. }
  67. break;
  68. case Variant::VECTOR2:
  69. //according to Variant.inl, it is going to be a Vector2f
  70. LuaType<Vector2f>::push(L, new Vector2f(var->Get<Vector2f>()), true);
  71. break;
  72. case Variant::VOIDPTR:
  73. lua_pushlightuserdata(L, var->Get<void*>());
  74. break;
  75. default:
  76. lua_pushnil(L);
  77. break;
  78. }
  79. }
  80. void GetVariant(lua_State* L, int index, Variant* variant)
  81. {
  82. if (!variant)
  83. return;
  84. switch (lua_type(L, index))
  85. {
  86. case LUA_TBOOLEAN:
  87. *variant = (bool)lua_toboolean(L, index);
  88. break;
  89. case LUA_TNUMBER:
  90. *variant = lua_tonumber(L, index);
  91. break;
  92. case LUA_TSTRING:
  93. *variant = Rml::String(lua_tostring(L, index));
  94. break;
  95. case LUA_TLIGHTUSERDATA:
  96. *variant = lua_touserdata(L, index);
  97. break;
  98. case LUA_TNIL:
  99. default: // todo: support other types
  100. *variant = Variant();
  101. break;
  102. }
  103. }
  104. } // namespace Lua
  105. } // namespace Rml