GlobalLobbyWidget.cpp 11 KB

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