GlobalLobbyWidget.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/CConfigHandler.h"
  24. #include "../../lib/Languages.h"
  25. #include "../../lib/MetaString.h"
  26. GlobalLobbyWidget::GlobalLobbyWidget(GlobalLobbyWindow * window)
  27. : window(window)
  28. {
  29. addCallback("closeWindow", [](int) { GH.windows().popWindows(1); });
  30. addCallback("sendMessage", [this](int) { this->window->doSendChatMessage(); });
  31. addCallback("createGameRoom", [this](int) { if (!CSH->inGame()) this->window->doCreateGameRoom(); });//TODO: button should be blocked instead
  32. REGISTER_BUILDER("lobbyItemList", &GlobalLobbyWidget::buildItemList);
  33. const JsonNode config(JsonPath::builtin("config/widgets/lobbyWindow.json"));
  34. build(config);
  35. }
  36. GlobalLobbyWidget::CreateFunc GlobalLobbyWidget::getItemListConstructorFunc(const std::string & callbackName) const
  37. {
  38. const auto & createAccountCardCallback = [this](size_t index) -> std::shared_ptr<CIntObject>
  39. {
  40. const auto & accounts = CSH->getGlobalLobby().getActiveAccounts();
  41. if(index < accounts.size())
  42. return std::make_shared<GlobalLobbyAccountCard>(this->window, accounts[index]);
  43. return std::make_shared<CIntObject>();
  44. };
  45. const auto & createRoomCardCallback = [this](size_t index) -> std::shared_ptr<CIntObject>
  46. {
  47. const auto & rooms = CSH->getGlobalLobby().getActiveRooms();
  48. if(index < rooms.size())
  49. return std::make_shared<GlobalLobbyRoomCard>(this->window, rooms[index]);
  50. return std::make_shared<CIntObject>();
  51. };
  52. const auto & createChannelCardCallback = [this](size_t index) -> std::shared_ptr<CIntObject>
  53. {
  54. const auto & channels = CSH->getGlobalLobby().getActiveChannels();
  55. if(index < channels.size())
  56. return std::make_shared<GlobalLobbyChannelCard>(this->window, channels[index]);
  57. return std::make_shared<CIntObject>();
  58. };
  59. const auto & createMatchCardCallback = [this](size_t index) -> std::shared_ptr<CIntObject>
  60. {
  61. const auto & matches = CSH->getGlobalLobby().getMatchesHistory();
  62. if(index < matches.size())
  63. return std::make_shared<GlobalLobbyMatchCard>(this->window, matches[index]);
  64. return std::make_shared<CIntObject>();
  65. };
  66. if (callbackName == "room")
  67. return createRoomCardCallback;
  68. if (callbackName == "account")
  69. return createAccountCardCallback;
  70. if (callbackName == "channel")
  71. return createChannelCardCallback;
  72. if (callbackName == "match")
  73. return createMatchCardCallback;
  74. throw std::runtime_error("Unknown item type in lobby widget constructor: " + callbackName);
  75. }
  76. std::shared_ptr<CIntObject> GlobalLobbyWidget::buildItemList(const JsonNode & config) const
  77. {
  78. auto callback = getItemListConstructorFunc(config["itemType"].String());
  79. auto position = readPosition(config["position"]);
  80. auto itemOffset = readPosition(config["itemOffset"]);
  81. auto sliderPosition = readPosition(config["sliderPosition"]);
  82. auto sliderSize = readPosition(config["sliderSize"]);
  83. size_t visibleAmount = config["visibleAmount"].Integer();
  84. size_t totalAmount = 0; // Will be set later, on server netpack
  85. int sliderMode = config["sliderSize"].isNull() ? 0 : (1 | 4); // present, vertical, blue
  86. int initialPos = 0;
  87. return std::make_shared<CListBox>(callback, position, itemOffset, visibleAmount, totalAmount, initialPos, sliderMode, Rect(sliderPosition, sliderSize));
  88. }
  89. std::shared_ptr<CLabel> GlobalLobbyWidget::getAccountNameLabel()
  90. {
  91. return widget<CLabel>("accountNameLabel");
  92. }
  93. std::shared_ptr<CTextInput> GlobalLobbyWidget::getMessageInput()
  94. {
  95. return widget<CTextInput>("messageInput");
  96. }
  97. std::shared_ptr<CTextBox> GlobalLobbyWidget::getGameChat()
  98. {
  99. return widget<CTextBox>("gameChat");
  100. }
  101. std::shared_ptr<CListBox> GlobalLobbyWidget::getAccountList()
  102. {
  103. return widget<CListBox>("accountList");
  104. }
  105. std::shared_ptr<CListBox> GlobalLobbyWidget::getRoomList()
  106. {
  107. return widget<CListBox>("roomList");
  108. }
  109. std::shared_ptr<CListBox> GlobalLobbyWidget::getMatchList()
  110. {
  111. return widget<CListBox>("matchList");
  112. }
  113. GlobalLobbyChannelCardBase::GlobalLobbyChannelCardBase(GlobalLobbyWindow * window, const std::string & channelType, const std::string & channelName)
  114. : window(window)
  115. , channelType(channelType)
  116. , channelName(channelName)
  117. {
  118. addUsedEvents(LCLICK);
  119. }
  120. void GlobalLobbyChannelCardBase::clickPressed(const Point & cursorPosition)
  121. {
  122. window->doOpenChannel(channelType, channelName);
  123. }
  124. GlobalLobbyAccountCard::GlobalLobbyAccountCard(GlobalLobbyWindow * window, const GlobalLobbyAccount & accountDescription)
  125. : GlobalLobbyChannelCardBase(window, "player", accountDescription.accountID)
  126. {
  127. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  128. pos.w = 130;
  129. pos.h = 40;
  130. backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0, 0, 0, 128), ColorRGBA(64, 64, 64, 64));
  131. labelName = std::make_shared<CLabel>(5, 10, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, accountDescription.displayName);
  132. labelStatus = std::make_shared<CLabel>(5, 30, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::YELLOW, accountDescription.status);
  133. }
  134. GlobalLobbyRoomCard::GlobalLobbyRoomCard(GlobalLobbyWindow * window, const GlobalLobbyRoom & roomDescription)
  135. {
  136. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  137. const auto & onJoinClicked = [window, roomID = roomDescription.gameRoomID]()
  138. {
  139. window->doJoinRoom(roomID);
  140. };
  141. auto roomSizeText = MetaString::createFromRawString("%d/%d");
  142. roomSizeText.replaceNumber(roomDescription.participants.size());
  143. roomSizeText.replaceNumber(roomDescription.playerLimit);
  144. auto roomStatusText = MetaString::createFromTextID("vcmi.lobby.room.state." + roomDescription.statusID);
  145. pos.w = 230;
  146. pos.h = 40;
  147. backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0, 0, 0, 128), ColorRGBA(64, 64, 64, 64));
  148. labelName = std::make_shared<CLabel>(5, 10, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, roomDescription.hostAccountDisplayName);
  149. labelDescription = std::make_shared<CLabel>(5, 30, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::YELLOW, roomDescription.description);
  150. labelRoomSize = std::make_shared<CLabel>(178, 10, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::YELLOW, roomSizeText.toString());
  151. labelRoomStatus = std::make_shared<CLabel>(190, 30, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::YELLOW, roomStatusText.toString());
  152. iconRoomSize = std::make_shared<CPicture>(ImagePath::builtin("lobby/iconPlayer"), Point(180, 5));
  153. if(!CSH->inGame() && roomDescription.statusID == "public")
  154. {
  155. buttonJoin = std::make_shared<CButton>(Point(194, 4), AnimationPath::builtin("lobbyJoinRoom"), CButton::tooltip(), onJoinClicked);
  156. buttonJoin->setOverlay(std::make_shared<CPicture>(ImagePath::builtin("lobby/iconEnter")));
  157. }
  158. }
  159. GlobalLobbyChannelCard::GlobalLobbyChannelCard(GlobalLobbyWindow * window, const std::string & channelName)
  160. : GlobalLobbyChannelCardBase(window, "global", channelName)
  161. {
  162. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  163. pos.w = 146;
  164. pos.h = 40;
  165. backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0, 0, 0, 128), ColorRGBA(64, 64, 64, 64));
  166. labelName = std::make_shared<CLabel>(5, 20, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, Languages::getLanguageOptions(channelName).nameNative);
  167. }
  168. GlobalLobbyMatchCard::GlobalLobbyMatchCard(GlobalLobbyWindow * window, const GlobalLobbyRoom & matchDescription)
  169. : GlobalLobbyChannelCardBase(window, "match", matchDescription.gameRoomID)
  170. {
  171. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  172. pos.w = 130;
  173. pos.h = 40;
  174. backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0, 0, 0, 128), ColorRGBA(64, 64, 64, 64));
  175. labelMatchDate = std::make_shared<CLabel>(5, 10, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, matchDescription.startDateFormatted);
  176. MetaString opponentDescription;
  177. if (matchDescription.participants.size() == 1)
  178. opponentDescription.appendTextID("vcmi.lobby.match.solo");
  179. if (matchDescription.participants.size() == 2)
  180. {
  181. std::string ourAccountID = settings["lobby"]["accountID"].String();
  182. opponentDescription.appendTextID("vcmi.lobby.match.duel");
  183. // Find display name of our one and only opponent in this game
  184. if (matchDescription.participants[0].accountID == ourAccountID)
  185. opponentDescription.replaceRawString(matchDescription.participants[1].displayName);
  186. else
  187. opponentDescription.replaceRawString(matchDescription.participants[0].displayName);
  188. }
  189. if (matchDescription.participants.size() > 2)
  190. {
  191. opponentDescription.appendTextID("vcmi.lobby.match.multi");
  192. opponentDescription.replaceNumber(matchDescription.participants.size());
  193. }
  194. labelMatchOpponent = std::make_shared<CLabel>(5, 30, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::YELLOW, opponentDescription.toString());
  195. }