GlobalLobbyWidget.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * GlobalLobbyWidget.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 "GlobalLobbyWidget.h"
  12. #include "GlobalLobbyClient.h"
  13. #include "GlobalLobbyWindow.h"
  14. #include "../CServerHandler.h"
  15. #include "../gui/CGuiHandler.h"
  16. #include "../gui/WindowHandler.h"
  17. #include "../widgets/Buttons.h"
  18. #include "../widgets/GraphicalPrimitiveCanvas.h"
  19. #include "../widgets/Images.h"
  20. #include "../widgets/MiscWidgets.h"
  21. #include "../widgets/ObjectLists.h"
  22. #include "../widgets/TextControls.h"
  23. #include "../../lib/MetaString.h"
  24. GlobalLobbyWidget::GlobalLobbyWidget(GlobalLobbyWindow * window)
  25. : window(window)
  26. {
  27. addCallback("closeWindow", [](int) { GH.windows().popWindows(1); });
  28. addCallback("sendMessage", [this](int) { this->window->doSendChatMessage(); });
  29. addCallback("createGameRoom", [this](int) { this->window->doCreateGameRoom(); });
  30. REGISTER_BUILDER("accountList", &GlobalLobbyWidget::buildAccountList);
  31. REGISTER_BUILDER("roomList", &GlobalLobbyWidget::buildRoomList);
  32. const JsonNode config(JsonPath::builtin("config/widgets/lobbyWindow.json"));
  33. build(config);
  34. }
  35. std::shared_ptr<CIntObject> GlobalLobbyWidget::buildAccountList(const JsonNode & config) const
  36. {
  37. const auto & createCallback = [this](size_t index) -> std::shared_ptr<CIntObject>
  38. {
  39. const auto & accounts = CSH->getGlobalLobby().getActiveAccounts();
  40. if(index < accounts.size())
  41. return std::make_shared<GlobalLobbyAccountCard>(this->window, accounts[index]);
  42. return std::make_shared<CIntObject>();
  43. };
  44. auto position = readPosition(config["position"]);
  45. auto itemOffset = readPosition(config["itemOffset"]);
  46. auto sliderPosition = readPosition(config["sliderPosition"]);
  47. auto sliderSize = readPosition(config["sliderSize"]);
  48. size_t visibleAmount = config["visibleAmount"].Integer();
  49. size_t totalAmount = 0; // Will be set later, on server netpack
  50. int sliderMode = 1 | 4; // present, vertical, blue
  51. int initialPos = 0;
  52. return std::make_shared<CListBox>(createCallback, position, itemOffset, visibleAmount, totalAmount, initialPos, sliderMode, Rect(sliderPosition, sliderSize));
  53. }
  54. std::shared_ptr<CIntObject> GlobalLobbyWidget::buildRoomList(const JsonNode & config) const
  55. {
  56. const auto & createCallback = [this](size_t index) -> std::shared_ptr<CIntObject>
  57. {
  58. const auto & rooms = CSH->getGlobalLobby().getActiveRooms();
  59. if(index < rooms.size())
  60. return std::make_shared<GlobalLobbyRoomCard>(this->window, rooms[index]);
  61. return std::make_shared<CIntObject>();
  62. };
  63. auto position = readPosition(config["position"]);
  64. auto itemOffset = readPosition(config["itemOffset"]);
  65. auto sliderPosition = readPosition(config["sliderPosition"]);
  66. auto sliderSize = readPosition(config["sliderSize"]);
  67. size_t visibleAmount = config["visibleAmount"].Integer();
  68. size_t totalAmount = 0; // Will be set later, on server netpack
  69. int sliderMode = 1 | 4; // present, vertical, blue
  70. int initialPos = 0;
  71. return std::make_shared<CListBox>(createCallback, position, itemOffset, visibleAmount, totalAmount, initialPos, sliderMode, Rect(sliderPosition, sliderSize));
  72. }
  73. std::shared_ptr<CLabel> GlobalLobbyWidget::getAccountNameLabel()
  74. {
  75. return widget<CLabel>("accountNameLabel");
  76. }
  77. std::shared_ptr<CTextInput> GlobalLobbyWidget::getMessageInput()
  78. {
  79. return widget<CTextInput>("messageInput");
  80. }
  81. std::shared_ptr<CTextBox> GlobalLobbyWidget::getGameChat()
  82. {
  83. return widget<CTextBox>("gameChat");
  84. }
  85. std::shared_ptr<CListBox> GlobalLobbyWidget::getAccountList()
  86. {
  87. return widget<CListBox>("accountList");
  88. }
  89. std::shared_ptr<CListBox> GlobalLobbyWidget::getRoomList()
  90. {
  91. return widget<CListBox>("roomList");
  92. }
  93. GlobalLobbyAccountCard::GlobalLobbyAccountCard(GlobalLobbyWindow * window, const GlobalLobbyAccount & accountDescription)
  94. {
  95. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  96. //const auto & onInviteClicked = [window, accountID=accountDescription.accountID]()
  97. //{
  98. // window->doInviteAccount(accountID);
  99. //};
  100. pos.w = 130;
  101. pos.h = 40;
  102. backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0, 0, 0, 128), ColorRGBA(64, 64, 64, 64));
  103. labelName = std::make_shared<CLabel>(5, 2, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, accountDescription.displayName);
  104. labelStatus = std::make_shared<CLabel>(5, 20, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, accountDescription.status);
  105. //if (CSH->inLobbyRoom())
  106. // buttonInvite = std::make_shared<CButton>(Point(95, 8), AnimationPath::builtin("settingsWindow/button32"), CButton::tooltip(), onInviteClicked);
  107. }
  108. GlobalLobbyRoomCard::GlobalLobbyRoomCard(GlobalLobbyWindow * window, const GlobalLobbyRoom & roomDescription)
  109. {
  110. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  111. const auto & onJoinClicked = [window, roomID = roomDescription.gameRoomID]()
  112. {
  113. window->doJoinRoom(roomID);
  114. };
  115. auto roomSizeText = MetaString::createFromRawString("%d/%d");
  116. roomSizeText.replaceNumber(roomDescription.playersCount);
  117. roomSizeText.replaceNumber(roomDescription.playersLimit);
  118. pos.w = 230;
  119. pos.h = 40;
  120. backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0, 0, 0, 128), ColorRGBA(64, 64, 64, 64));
  121. labelName = std::make_shared<CLabel>(5, 2, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, roomDescription.hostAccountDisplayName);
  122. labelDescription = std::make_shared<CLabel>(5, 20, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, roomDescription.description);
  123. labelRoomSize = std::make_shared<CLabel>(160, 2, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, roomSizeText.toString());
  124. iconRoomSize = std::make_shared<CPicture>(ImagePath::builtin("lobby/iconPlayer"), Point(145, 5));
  125. if(!CSH->inGame())
  126. {
  127. buttonJoin = std::make_shared<CButton>(Point(194, 4), AnimationPath::builtin("lobbyJoinRoom"), CButton::tooltip(), onJoinClicked);
  128. buttonJoin->setOverlay(std::make_shared<CPicture>(ImagePath::builtin("lobby/iconEnter")));
  129. }
  130. }