GlobalLobbyInviteWindow.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * GlobalLobbyInviteWindow.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 "GlobalLobbyInviteWindow.h"
  12. #include "GlobalLobbyClient.h"
  13. #include "../CServerHandler.h"
  14. #include "../GameEngine.h"
  15. #include "../GameInstance.h"
  16. #include "../gui/Shortcut.h"
  17. #include "../widgets/Buttons.h"
  18. #include "../widgets/GraphicalPrimitiveCanvas.h"
  19. #include "../widgets/Images.h"
  20. #include "../widgets/ObjectLists.h"
  21. #include "../widgets/TextControls.h"
  22. #include "../../lib/json/JsonNode.h"
  23. #include "../../lib/texts/MetaString.h"
  24. GlobalLobbyInviteAccountCard::GlobalLobbyInviteAccountCard(const GlobalLobbyAccount & accountDescription)
  25. : accountID(accountDescription.accountID)
  26. {
  27. pos.w = 200;
  28. pos.h = 40;
  29. addUsedEvents(LCLICK);
  30. bool thisAccountInvited = false;
  31. const auto & myRoomID = GAME->server().getGlobalLobby().getCurrentGameRoomID();
  32. if (!myRoomID.empty())
  33. {
  34. const auto & myRoom = GAME->server().getGlobalLobby().getActiveRoomByName(myRoomID);
  35. for (auto const & invited : myRoom.invited)
  36. {
  37. if (invited.accountID == accountID)
  38. {
  39. thisAccountInvited = true;
  40. break;
  41. }
  42. }
  43. }
  44. OBJECT_CONSTRUCTION;
  45. if (thisAccountInvited)
  46. backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0, 0, 0, 128), Colors::WHITE, 1);
  47. else
  48. backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0, 0, 0, 128), ColorRGBA(64, 64, 64, 64), 1);
  49. labelName = std::make_shared<CLabel>(5, 10, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, accountDescription.displayName);
  50. if (thisAccountInvited)
  51. labelStatus = std::make_shared<CLabel>(5, 30, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::YELLOW, MetaString::createFromTextID("vcmi.lobby.room.state.invited").toString());
  52. else
  53. labelStatus = std::make_shared<CLabel>(5, 30, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::YELLOW, accountDescription.status);
  54. }
  55. void GlobalLobbyInviteAccountCard::clickPressed(const Point & cursorPosition)
  56. {
  57. JsonNode message;
  58. message["type"].String() = "sendInvite";
  59. message["accountID"].String() = accountID;
  60. GAME->server().getGlobalLobby().sendMessage(message);
  61. }
  62. GlobalLobbyInviteWindow::GlobalLobbyInviteWindow()
  63. : CWindowObject(BORDERED)
  64. {
  65. OBJECT_CONSTRUCTION;
  66. pos.w = 236;
  67. pos.h = 420;
  68. filledBackground = std::make_shared<FilledTexturePlayerColored>(Rect(0, 0, pos.w, pos.h));
  69. filledBackground->setPlayerColor(PlayerColor(1));
  70. labelTitle = std::make_shared<CLabel>(
  71. pos.w / 2, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, MetaString::createFromTextID("vcmi.lobby.invite.header").toString()
  72. );
  73. const auto & createAccountCardCallback = [](size_t index) -> std::shared_ptr<CIntObject>
  74. {
  75. const auto & accounts = GAME->server().getGlobalLobby().getActiveAccounts();
  76. if(index < accounts.size())
  77. return std::make_shared<GlobalLobbyInviteAccountCard>(accounts[index]);
  78. return std::make_shared<CIntObject>();
  79. };
  80. listBackground = std::make_shared<TransparentFilledRectangle>(Rect(8, 48, 220, 324), ColorRGBA(0, 0, 0, 64), ColorRGBA(64, 80, 128, 255), 1);
  81. accountList = std::make_shared<CListBox>(createAccountCardCallback, Point(10, 50), Point(0, 40), 8, GAME->server().getGlobalLobby().getActiveAccounts().size(), 0, 1 | 4, Rect(200, 0, 320, 320));
  82. buttonClose = std::make_shared<CButton>(Point(86, 384), AnimationPath::builtin("MuBchck"), CButton::tooltip(), [this]() { close(); }, EShortcut::GLOBAL_RETURN );
  83. center();
  84. }
  85. void GlobalLobbyInviteWindow::onActiveGameRooms(const std::vector<GlobalLobbyRoom> & rooms)
  86. {
  87. accountList->reset();
  88. redraw();
  89. }
  90. void GlobalLobbyInviteWindow::onActiveAccounts(const std::vector<GlobalLobbyAccount> & accounts)
  91. {
  92. if (accountList->size() == accounts.size())
  93. accountList->reset();
  94. else
  95. accountList->resize(accounts.size());
  96. redraw();
  97. }