InfoWindow.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * api/netpacks/InfoWindow.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "InfoWindow.h"
  12. #include "../../LuaStack.h"
  13. #include "../Registry.h"
  14. using scripting::api::netpacks::InfoWindowProxy;
  15. using scripting::api::RegisterAPI;
  16. VCMI_REGISTER_SCRIPT_API(InfoWindowProxy, "netpacks.InfoWindow")
  17. namespace scripting
  18. {
  19. namespace api
  20. {
  21. namespace netpacks
  22. {
  23. const std::vector<InfoWindowProxy::RegType> InfoWindowProxy::REGISTER =
  24. {
  25. {
  26. "addReplacement",
  27. &InfoWindowProxy::addReplacement
  28. },
  29. {
  30. "addText",
  31. &InfoWindowProxy::addText
  32. },
  33. {
  34. "setPlayer",
  35. &InfoWindowProxy::setPlayer
  36. },
  37. {
  38. "toNetpackLight",
  39. &PackForClientProxy<InfoWindowProxy>::toNetpackLight
  40. },
  41. };
  42. const std::vector<InfoWindowProxy::CustomRegType> InfoWindowProxy::REGISTER_CUSTOM =
  43. {
  44. {"new", &Wrapper::constructor, true}
  45. };
  46. int InfoWindowProxy::addReplacement(lua_State * L, std::shared_ptr<InfoWindow> object)
  47. {
  48. int top = lua_gettop(L);
  49. if(top == 1)
  50. {
  51. if(lua_isstring(L, 1))
  52. {
  53. size_t len = 0;
  54. auto raw = lua_tolstring(L, 1, &len);
  55. std::string text(raw, len);
  56. object->text.addReplacement(text);
  57. }
  58. else if(lua_isnumber(L, 1))
  59. {
  60. object->text.addReplacement(lua_tointeger(L, 1));
  61. }
  62. }
  63. else if(top >= 2)
  64. {
  65. if(lua_isnumber(L, 1) && lua_isnumber(L, 2))
  66. object->text.addReplacement(lua_tointeger(L, 1), lua_tointeger(L, 2));
  67. }
  68. lua_settop(L, 0);
  69. return 0;
  70. }
  71. int InfoWindowProxy::addText(lua_State * L, std::shared_ptr<InfoWindow> object)
  72. {
  73. int top = lua_gettop(L);
  74. if(top == 1)
  75. {
  76. if(lua_isstring(L, 1))
  77. {
  78. size_t len = 0;
  79. auto raw = lua_tolstring(L, 1, &len);
  80. std::string text(raw, len);
  81. object->text << text;
  82. }
  83. else if(lua_isnumber(L, 1))
  84. {
  85. object->text << (lua_tointeger(L, 1));
  86. }
  87. }
  88. if(top >= 2)
  89. {
  90. if(lua_isnumber(L, 1) && lua_isnumber(L, 2))
  91. object->text.addTxt(lua_tointeger(L, 1), lua_tointeger(L, 2));
  92. }
  93. lua_settop(L, 0);
  94. return 0;
  95. }
  96. int InfoWindowProxy::setPlayer(lua_State * L, std::shared_ptr<InfoWindow> object)
  97. {
  98. LuaStack S(L);
  99. PlayerColor value;
  100. if(S.tryGet(1, value))
  101. object->player = value;
  102. return S.retVoid();
  103. }
  104. }
  105. }
  106. }