GlobalLobbyWidget.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "../CServerHandler.h"
  15. #include "../gui/CGuiHandler.h"
  16. #include "../gui/WindowHandler.h"
  17. #include "../widgets/Buttons.h"
  18. #include "../widgets/MiscWidgets.h"
  19. #include "../widgets/ObjectLists.h"
  20. #include "../widgets/TextControls.h"
  21. #include "../../lib/MetaString.h"
  22. GlobalLobbyWidget::GlobalLobbyWidget(GlobalLobbyWindow * window)
  23. : window(window)
  24. {
  25. addCallback("closeWindow", [](int) { GH.windows().popWindows(1); });
  26. addCallback("sendMessage", [this](int) { this->window->doSendChatMessage(); });
  27. addCallback("createGameRoom", [this](int) { this->window->doCreateGameRoom(); });
  28. REGISTER_BUILDER("accountList", &GlobalLobbyWidget::buildAccountList);
  29. REGISTER_BUILDER("roomList", &GlobalLobbyWidget::buildRoomList);
  30. const JsonNode config(JsonPath::builtin("config/widgets/lobbyWindow.json"));
  31. build(config);
  32. }
  33. std::shared_ptr<CIntObject> GlobalLobbyWidget::buildAccountList(const JsonNode & config) const
  34. {
  35. const auto & createCallback = [this](size_t index) -> std::shared_ptr<CIntObject>
  36. {
  37. const auto & accounts = CSH->getGlobalLobby().getActiveAccounts();
  38. if(index < accounts.size())
  39. return std::make_shared<GlobalLobbyAccountCard>(this->window, accounts[index]);
  40. return std::make_shared<CIntObject>();
  41. };
  42. auto position = readPosition(config["position"]);
  43. auto itemOffset = readPosition(config["itemOffset"]);
  44. auto sliderPosition = readPosition(config["sliderPosition"]);
  45. auto sliderSize = readPosition(config["sliderSize"]);
  46. size_t visibleSize = 4; // FIXME: how many items can fit into UI?
  47. size_t totalSize = 4; //FIXME: how many items are there in total
  48. int sliderMode = 1 | 4; // present, vertical, blue
  49. int initialPos = 0;
  50. return std::make_shared<CListBox>(createCallback, position, itemOffset, visibleSize, totalSize, initialPos, sliderMode, Rect(sliderPosition, sliderSize) );
  51. }
  52. std::shared_ptr<CIntObject> GlobalLobbyWidget::buildRoomList(const JsonNode & config) const
  53. {
  54. const auto & createCallback = [this](size_t index) -> std::shared_ptr<CIntObject>
  55. {
  56. const auto & rooms = CSH->getGlobalLobby().getActiveRooms();
  57. if(index < rooms.size())
  58. return std::make_shared<GlobalLobbyRoomCard>(this->window, rooms[index]);
  59. return std::make_shared<CIntObject>();
  60. };
  61. auto position = readPosition(config["position"]);
  62. auto itemOffset = readPosition(config["itemOffset"]);
  63. auto sliderPosition = readPosition(config["sliderPosition"]);
  64. auto sliderSize = readPosition(config["sliderSize"]);
  65. size_t visibleSize = 4; // FIXME: how many items can fit into UI?
  66. size_t totalSize = 4; //FIXME: how many items are there in total
  67. int sliderMode = 1 | 4; // present, vertical, blue
  68. int initialPos = 0;
  69. return std::make_shared<CListBox>(createCallback, position, itemOffset, visibleSize, totalSize, initialPos, sliderMode, Rect(sliderPosition, sliderSize) );
  70. }
  71. std::shared_ptr<CLabel> GlobalLobbyWidget::getAccountNameLabel()
  72. {
  73. return widget<CLabel>("accountNameLabel");
  74. }
  75. std::shared_ptr<CTextInput> GlobalLobbyWidget::getMessageInput()
  76. {
  77. return widget<CTextInput>("messageInput");
  78. }
  79. std::shared_ptr<CTextBox> GlobalLobbyWidget::getGameChat()
  80. {
  81. return widget<CTextBox>("gameChat");
  82. }
  83. std::shared_ptr<CListBox> GlobalLobbyWidget::getAccountList()
  84. {
  85. return widget<CListBox>("accountList");
  86. }
  87. std::shared_ptr<CListBox> GlobalLobbyWidget::getRoomList()
  88. {
  89. return widget<CListBox>("roomList");
  90. }
  91. GlobalLobbyAccountCard::GlobalLobbyAccountCard(GlobalLobbyWindow * window, const GlobalLobbyAccount & accountDescription)
  92. {
  93. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  94. const auto & onInviteClicked = [window, accountID=accountDescription.accountID]()
  95. {
  96. window->doInviteAccount(accountID);
  97. };
  98. pos.w = 130;
  99. pos.h = 40;
  100. backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0,0,0,128), ColorRGBA(64,64,64,64));
  101. labelName = std::make_shared<CLabel>( 5, 2, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, accountDescription.displayName);
  102. labelStatus = std::make_shared<CLabel>( 5, 20, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, accountDescription.status);
  103. buttonInvite = std::make_shared<CButton>(Point(95, 8), AnimationPath::builtin("settingsWindow/button32"), CButton::tooltip(), onInviteClicked);
  104. }
  105. GlobalLobbyRoomCard::GlobalLobbyRoomCard(GlobalLobbyWindow * window, const GlobalLobbyRoom & roomDescription)
  106. {
  107. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  108. const auto & onJoinClicked = [window, roomID=roomDescription.gameRoomID]()
  109. {
  110. window->doJoinRoom(roomID);
  111. };
  112. auto roomSizeText = MetaString::createFromRawString("%d/%d");
  113. roomSizeText.replaceNumber(roomDescription.playersCount);
  114. roomSizeText.replaceNumber(roomDescription.playersLimit);
  115. pos.w = 230;
  116. pos.h = 40;
  117. backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0,0,0,128), ColorRGBA(64,64,64,64));
  118. labelName = std::make_shared<CLabel>( 5, 2, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, roomDescription.hostAccountDisplayName);
  119. labelStatus = std::make_shared<CLabel>( 5, 20, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, roomDescription.description);
  120. labelRoomSize = std::make_shared<CLabel>( 160, 2, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, roomSizeText.toString());
  121. buttonJoin = std::make_shared<CButton>(Point(195, 8), AnimationPath::builtin("settingsWindow/button32"), CButton::tooltip(), onJoinClicked);
  122. }