2023-12-27 19:07:49 +02:00
|
|
|
/*
|
|
|
|
* GlobalLobbyWidget.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "GlobalLobbyWidget.h"
|
2024-01-21 16:48:36 +02:00
|
|
|
|
|
|
|
#include "GlobalLobbyClient.h"
|
2023-12-27 19:07:49 +02:00
|
|
|
#include "GlobalLobbyWindow.h"
|
2024-04-16 15:30:22 +02:00
|
|
|
#include "GlobalLobbyRoomWindow.h"
|
2023-12-27 19:07:49 +02:00
|
|
|
|
2024-03-25 18:04:44 +02:00
|
|
|
#include "../CGameInfo.h"
|
|
|
|
#include "../CMusicHandler.h"
|
2024-01-21 16:48:36 +02:00
|
|
|
#include "../CServerHandler.h"
|
2023-12-27 19:07:49 +02:00
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../gui/WindowHandler.h"
|
2024-01-21 16:48:36 +02:00
|
|
|
#include "../widgets/Buttons.h"
|
2024-01-18 16:23:12 +02:00
|
|
|
#include "../widgets/GraphicalPrimitiveCanvas.h"
|
2024-03-11 17:52:33 +02:00
|
|
|
#include "../widgets/Images.h"
|
2024-01-21 16:48:36 +02:00
|
|
|
#include "../widgets/MiscWidgets.h"
|
|
|
|
#include "../widgets/ObjectLists.h"
|
2023-12-27 19:07:49 +02:00
|
|
|
#include "../widgets/TextControls.h"
|
|
|
|
|
2024-03-20 19:49:28 +02:00
|
|
|
#include "../../lib/CConfigHandler.h"
|
2024-03-13 23:02:53 +02:00
|
|
|
#include "../../lib/Languages.h"
|
2024-01-21 16:48:36 +02:00
|
|
|
#include "../../lib/MetaString.h"
|
2024-03-11 17:51:11 +02:00
|
|
|
|
2023-12-27 19:07:49 +02:00
|
|
|
GlobalLobbyWidget::GlobalLobbyWidget(GlobalLobbyWindow * window)
|
|
|
|
: window(window)
|
|
|
|
{
|
|
|
|
addCallback("closeWindow", [](int) { GH.windows().popWindows(1); });
|
|
|
|
addCallback("sendMessage", [this](int) { this->window->doSendChatMessage(); });
|
2024-03-13 23:02:53 +02:00
|
|
|
addCallback("createGameRoom", [this](int) { if (!CSH->inGame()) this->window->doCreateGameRoom(); });//TODO: button should be blocked instead
|
2023-12-27 19:07:49 +02:00
|
|
|
|
2024-03-13 23:02:53 +02:00
|
|
|
REGISTER_BUILDER("lobbyItemList", &GlobalLobbyWidget::buildItemList);
|
2024-01-21 16:48:36 +02:00
|
|
|
|
2023-12-27 19:07:49 +02:00
|
|
|
const JsonNode config(JsonPath::builtin("config/widgets/lobbyWindow.json"));
|
|
|
|
build(config);
|
|
|
|
}
|
|
|
|
|
2024-03-13 23:02:53 +02:00
|
|
|
GlobalLobbyWidget::CreateFunc GlobalLobbyWidget::getItemListConstructorFunc(const std::string & callbackName) const
|
2024-01-21 16:48:36 +02:00
|
|
|
{
|
2024-03-13 23:02:53 +02:00
|
|
|
const auto & createAccountCardCallback = [this](size_t index) -> std::shared_ptr<CIntObject>
|
2024-01-21 16:48:36 +02:00
|
|
|
{
|
|
|
|
const auto & accounts = CSH->getGlobalLobby().getActiveAccounts();
|
|
|
|
|
|
|
|
if(index < accounts.size())
|
|
|
|
return std::make_shared<GlobalLobbyAccountCard>(this->window, accounts[index]);
|
|
|
|
return std::make_shared<CIntObject>();
|
|
|
|
};
|
|
|
|
|
2024-03-13 23:02:53 +02:00
|
|
|
const auto & createRoomCardCallback = [this](size_t index) -> std::shared_ptr<CIntObject>
|
2024-01-21 16:48:36 +02:00
|
|
|
{
|
|
|
|
const auto & rooms = CSH->getGlobalLobby().getActiveRooms();
|
|
|
|
|
|
|
|
if(index < rooms.size())
|
|
|
|
return std::make_shared<GlobalLobbyRoomCard>(this->window, rooms[index]);
|
|
|
|
return std::make_shared<CIntObject>();
|
|
|
|
};
|
|
|
|
|
2024-03-13 23:02:53 +02:00
|
|
|
const auto & createChannelCardCallback = [this](size_t index) -> std::shared_ptr<CIntObject>
|
|
|
|
{
|
|
|
|
const auto & channels = CSH->getGlobalLobby().getActiveChannels();
|
|
|
|
|
|
|
|
if(index < channels.size())
|
|
|
|
return std::make_shared<GlobalLobbyChannelCard>(this->window, channels[index]);
|
|
|
|
return std::make_shared<CIntObject>();
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto & createMatchCardCallback = [this](size_t index) -> std::shared_ptr<CIntObject>
|
|
|
|
{
|
|
|
|
const auto & matches = CSH->getGlobalLobby().getMatchesHistory();
|
|
|
|
|
|
|
|
if(index < matches.size())
|
|
|
|
return std::make_shared<GlobalLobbyMatchCard>(this->window, matches[index]);
|
|
|
|
return std::make_shared<CIntObject>();
|
|
|
|
};
|
|
|
|
|
|
|
|
if (callbackName == "room")
|
|
|
|
return createRoomCardCallback;
|
|
|
|
|
|
|
|
if (callbackName == "account")
|
|
|
|
return createAccountCardCallback;
|
|
|
|
|
|
|
|
if (callbackName == "channel")
|
|
|
|
return createChannelCardCallback;
|
|
|
|
|
|
|
|
if (callbackName == "match")
|
|
|
|
return createMatchCardCallback;
|
|
|
|
|
|
|
|
throw std::runtime_error("Unknown item type in lobby widget constructor: " + callbackName);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CIntObject> GlobalLobbyWidget::buildItemList(const JsonNode & config) const
|
|
|
|
{
|
|
|
|
auto callback = getItemListConstructorFunc(config["itemType"].String());
|
2024-01-21 16:48:36 +02:00
|
|
|
auto position = readPosition(config["position"]);
|
|
|
|
auto itemOffset = readPosition(config["itemOffset"]);
|
|
|
|
auto sliderPosition = readPosition(config["sliderPosition"]);
|
|
|
|
auto sliderSize = readPosition(config["sliderSize"]);
|
2024-03-11 17:51:11 +02:00
|
|
|
size_t visibleAmount = config["visibleAmount"].Integer();
|
|
|
|
size_t totalAmount = 0; // Will be set later, on server netpack
|
2024-03-13 23:02:53 +02:00
|
|
|
int sliderMode = config["sliderSize"].isNull() ? 0 : (1 | 4); // present, vertical, blue
|
2024-01-21 16:48:36 +02:00
|
|
|
int initialPos = 0;
|
|
|
|
|
2024-03-23 21:12:10 +02:00
|
|
|
auto result = std::make_shared<CListBox>(callback, position, itemOffset, visibleAmount, totalAmount, initialPos, sliderMode, Rect(sliderPosition, sliderSize));
|
|
|
|
|
|
|
|
result->setRedrawParent(true);
|
|
|
|
return result;
|
2024-01-21 16:48:36 +02:00
|
|
|
}
|
|
|
|
|
2023-12-27 19:07:49 +02:00
|
|
|
std::shared_ptr<CLabel> GlobalLobbyWidget::getAccountNameLabel()
|
|
|
|
{
|
|
|
|
return widget<CLabel>("accountNameLabel");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CTextInput> GlobalLobbyWidget::getMessageInput()
|
|
|
|
{
|
|
|
|
return widget<CTextInput>("messageInput");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CTextBox> GlobalLobbyWidget::getGameChat()
|
|
|
|
{
|
|
|
|
return widget<CTextBox>("gameChat");
|
|
|
|
}
|
2024-01-21 16:48:36 +02:00
|
|
|
|
|
|
|
std::shared_ptr<CListBox> GlobalLobbyWidget::getAccountList()
|
|
|
|
{
|
|
|
|
return widget<CListBox>("accountList");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CListBox> GlobalLobbyWidget::getRoomList()
|
|
|
|
{
|
|
|
|
return widget<CListBox>("roomList");
|
|
|
|
}
|
|
|
|
|
2024-03-21 18:00:05 +02:00
|
|
|
std::shared_ptr<CListBox> GlobalLobbyWidget::getChannelList()
|
|
|
|
{
|
|
|
|
return widget<CListBox>("channelList");
|
|
|
|
}
|
|
|
|
|
2024-03-20 18:40:37 +02:00
|
|
|
std::shared_ptr<CListBox> GlobalLobbyWidget::getMatchList()
|
|
|
|
{
|
|
|
|
return widget<CListBox>("matchList");
|
|
|
|
}
|
|
|
|
|
2024-03-21 18:00:05 +02:00
|
|
|
std::shared_ptr<CLabel> GlobalLobbyWidget::getGameChatHeader()
|
|
|
|
{
|
|
|
|
return widget<CLabel>("headerGameChat");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CLabel> GlobalLobbyWidget::getAccountListHeader()
|
|
|
|
{
|
|
|
|
return widget<CLabel>("headerAccountList");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CLabel> GlobalLobbyWidget::getRoomListHeader()
|
|
|
|
{
|
|
|
|
return widget<CLabel>("headerRoomList");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CLabel> GlobalLobbyWidget::getChannelListHeader()
|
|
|
|
{
|
|
|
|
return widget<CLabel>("headerChannelList");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CLabel> GlobalLobbyWidget::getMatchListHeader()
|
|
|
|
{
|
|
|
|
return widget<CLabel>("headerMatchList");
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalLobbyChannelCardBase::GlobalLobbyChannelCardBase(GlobalLobbyWindow * window, const Point & dimensions, const std::string & channelType, const std::string & channelName, const std::string & channelDescription)
|
2024-03-13 23:02:53 +02:00
|
|
|
: window(window)
|
|
|
|
, channelType(channelType)
|
|
|
|
, channelName(channelName)
|
2024-03-21 18:00:05 +02:00
|
|
|
, channelDescription(channelDescription)
|
2024-03-13 23:02:53 +02:00
|
|
|
{
|
2024-03-21 18:00:05 +02:00
|
|
|
pos.w = dimensions.x;
|
|
|
|
pos.h = dimensions.y;
|
2024-03-13 23:02:53 +02:00
|
|
|
addUsedEvents(LCLICK);
|
2024-03-21 18:00:05 +02:00
|
|
|
|
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
|
|
|
|
|
|
|
|
if (window->isChannelOpen(channelType, channelName))
|
|
|
|
backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0, 0, 0, 128), Colors::YELLOW, 2);
|
2024-03-25 18:04:44 +02:00
|
|
|
else if (window->isChannelUnread(channelType, channelName))
|
|
|
|
backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0, 0, 0, 128), Colors::WHITE, 1);
|
2024-03-21 18:00:05 +02:00
|
|
|
else
|
|
|
|
backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0, 0, 0, 128), ColorRGBA(64, 64, 64, 64), 1);
|
2024-03-13 23:02:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyChannelCardBase::clickPressed(const Point & cursorPosition)
|
|
|
|
{
|
2024-03-25 18:04:44 +02:00
|
|
|
CCS->soundh->playSound(soundBase::button);
|
2024-03-21 18:00:05 +02:00
|
|
|
window->doOpenChannel(channelType, channelName, channelDescription);
|
2024-03-13 23:02:53 +02:00
|
|
|
}
|
|
|
|
|
2024-01-21 16:48:36 +02:00
|
|
|
GlobalLobbyAccountCard::GlobalLobbyAccountCard(GlobalLobbyWindow * window, const GlobalLobbyAccount & accountDescription)
|
2024-03-21 18:00:05 +02:00
|
|
|
: GlobalLobbyChannelCardBase(window, Point(130, 40), "player", accountDescription.accountID, accountDescription.displayName)
|
2024-01-21 16:48:36 +02:00
|
|
|
{
|
2024-03-25 18:04:44 +02:00
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
|
2024-03-13 23:02:53 +02:00
|
|
|
labelName = std::make_shared<CLabel>(5, 10, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, accountDescription.displayName);
|
|
|
|
labelStatus = std::make_shared<CLabel>(5, 30, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::YELLOW, accountDescription.status);
|
2024-01-21 16:48:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GlobalLobbyRoomCard::GlobalLobbyRoomCard(GlobalLobbyWindow * window, const GlobalLobbyRoom & roomDescription)
|
2024-04-16 15:30:22 +02:00
|
|
|
: roomUUID(roomDescription.gameRoomID)
|
|
|
|
, window(window)
|
2024-01-21 16:48:36 +02:00
|
|
|
{
|
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
|
2024-04-16 15:30:22 +02:00
|
|
|
addUsedEvents(LCLICK);
|
2024-01-21 16:48:36 +02:00
|
|
|
|
2024-03-25 20:23:57 +02:00
|
|
|
bool hasInvite = CSH->getGlobalLobby().isInvitedToRoom(roomDescription.gameRoomID);
|
|
|
|
|
2024-01-21 16:48:36 +02:00
|
|
|
auto roomSizeText = MetaString::createFromRawString("%d/%d");
|
2024-03-20 18:40:37 +02:00
|
|
|
roomSizeText.replaceNumber(roomDescription.participants.size());
|
2024-03-11 20:03:33 +02:00
|
|
|
roomSizeText.replaceNumber(roomDescription.playerLimit);
|
2024-01-21 16:48:36 +02:00
|
|
|
|
2024-03-25 20:23:57 +02:00
|
|
|
MetaString roomStatusText;
|
|
|
|
if (roomDescription.statusID == "private" && hasInvite)
|
|
|
|
roomStatusText.appendTextID("vcmi.lobby.room.state.invited");
|
|
|
|
else
|
|
|
|
roomStatusText.appendTextID("vcmi.lobby.room.state." + roomDescription.statusID);
|
2024-03-12 19:52:42 +02:00
|
|
|
|
2024-01-21 16:48:36 +02:00
|
|
|
pos.w = 230;
|
|
|
|
pos.h = 40;
|
|
|
|
|
2024-03-25 20:23:57 +02:00
|
|
|
if (window->isInviteUnread(roomDescription.gameRoomID))
|
|
|
|
backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0, 0, 0, 128), Colors::WHITE, 1);
|
|
|
|
else
|
|
|
|
backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0, 0, 0, 128), ColorRGBA(64, 64, 64, 64), 1);
|
|
|
|
|
2024-03-13 23:02:53 +02:00
|
|
|
labelName = std::make_shared<CLabel>(5, 10, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, roomDescription.hostAccountDisplayName);
|
|
|
|
labelDescription = std::make_shared<CLabel>(5, 30, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::YELLOW, roomDescription.description);
|
2024-04-19 16:50:05 +02:00
|
|
|
labelRoomSize = std::make_shared<CLabel>(212, 10, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::YELLOW, roomSizeText.toString());
|
|
|
|
labelRoomStatus = std::make_shared<CLabel>(225, 30, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::YELLOW, roomStatusText.toString());
|
|
|
|
iconRoomSize = std::make_shared<CPicture>(ImagePath::builtin("lobby/iconPlayer"), Point(214, 5));
|
2024-04-16 15:30:22 +02:00
|
|
|
}
|
2024-02-10 19:02:25 +02:00
|
|
|
|
2024-04-16 15:30:22 +02:00
|
|
|
void GlobalLobbyRoomCard::clickPressed(const Point & cursorPosition)
|
|
|
|
{
|
|
|
|
GH.windows().createAndPushWindow<GlobalLobbyRoomWindow>(window, roomUUID);
|
2024-01-21 16:48:36 +02:00
|
|
|
}
|
2024-03-13 23:02:53 +02:00
|
|
|
|
|
|
|
GlobalLobbyChannelCard::GlobalLobbyChannelCard(GlobalLobbyWindow * window, const std::string & channelName)
|
2024-03-21 18:00:05 +02:00
|
|
|
: GlobalLobbyChannelCardBase(window, Point(146, 40), "global", channelName, Languages::getLanguageOptions(channelName).nameNative)
|
2024-03-13 23:02:53 +02:00
|
|
|
{
|
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
|
|
|
|
labelName = std::make_shared<CLabel>(5, 20, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, Languages::getLanguageOptions(channelName).nameNative);
|
|
|
|
}
|
|
|
|
|
2024-03-20 18:40:37 +02:00
|
|
|
GlobalLobbyMatchCard::GlobalLobbyMatchCard(GlobalLobbyWindow * window, const GlobalLobbyRoom & matchDescription)
|
2024-03-21 18:00:05 +02:00
|
|
|
: GlobalLobbyChannelCardBase(window, Point(130, 40), "match", matchDescription.gameRoomID, matchDescription.startDateFormatted)
|
2024-03-13 23:02:53 +02:00
|
|
|
{
|
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
|
|
|
|
labelMatchDate = std::make_shared<CLabel>(5, 10, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::WHITE, matchDescription.startDateFormatted);
|
|
|
|
|
|
|
|
MetaString opponentDescription;
|
|
|
|
|
2024-03-20 18:40:37 +02:00
|
|
|
if (matchDescription.participants.size() == 1)
|
2024-03-20 19:49:28 +02:00
|
|
|
opponentDescription.appendTextID("vcmi.lobby.match.solo");
|
2024-03-13 23:02:53 +02:00
|
|
|
|
2024-03-20 18:40:37 +02:00
|
|
|
if (matchDescription.participants.size() == 2)
|
2024-03-20 19:49:28 +02:00
|
|
|
{
|
2024-03-29 17:04:52 +02:00
|
|
|
std::string ourAccountID = CSH->getGlobalLobby().getAccountID();
|
2024-03-20 19:49:28 +02:00
|
|
|
|
|
|
|
opponentDescription.appendTextID("vcmi.lobby.match.duel");
|
|
|
|
// Find display name of our one and only opponent in this game
|
|
|
|
if (matchDescription.participants[0].accountID == ourAccountID)
|
|
|
|
opponentDescription.replaceRawString(matchDescription.participants[1].displayName);
|
|
|
|
else
|
|
|
|
opponentDescription.replaceRawString(matchDescription.participants[0].displayName);
|
|
|
|
}
|
2024-03-13 23:02:53 +02:00
|
|
|
|
2024-03-20 18:40:37 +02:00
|
|
|
if (matchDescription.participants.size() > 2)
|
2024-03-13 23:02:53 +02:00
|
|
|
{
|
2024-03-20 19:49:28 +02:00
|
|
|
opponentDescription.appendTextID("vcmi.lobby.match.multi");
|
2024-03-20 18:40:37 +02:00
|
|
|
opponentDescription.replaceNumber(matchDescription.participants.size());
|
2024-03-13 23:02:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
labelMatchOpponent = std::make_shared<CLabel>(5, 30, FONT_SMALL, ETextAlignment::CENTERLEFT, Colors::YELLOW, opponentDescription.toString());
|
|
|
|
}
|