GlobalLobbyWidget.cpp 11 KB

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