GlobalLobbyWidget.cpp 11 KB

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