GlobalLobbyWidget.cpp 12 KB

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