GlobalLobbyInviteWindow.cpp 3.9 KB

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