GlobalLobbyWidget.cpp 11 KB

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