1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Making team alignments widget customizable

This commit is contained in:
nordsoft 2022-12-17 20:38:16 +04:00
parent b590e4a0e4
commit 9b76a8000f
4 changed files with 82 additions and 62 deletions

View File

@ -45,9 +45,14 @@ void InterfaceObjectConfigurable::addCallback(const std::string & callbackName,
void InterfaceObjectConfigurable::init(const JsonNode &config)
{
OBJ_CONSTRUCTION;
for(auto & item : config["variables"].Struct())
{
variables[item.first] = item.second;
}
int unnamedObjectId = 0;
const std::string unnamedObjectPrefix = "__widget_";
for(const auto & item : config["items"].Vector())
{
std::string name = item["name"].isNull()
@ -57,11 +62,6 @@ void InterfaceObjectConfigurable::init(const JsonNode &config)
}
}
const JsonNode & InterfaceObjectConfigurable::variable(const std::string & name) const
{
return variables[name];
}
std::string InterfaceObjectConfigurable::readText(const JsonNode & config) const
{
if(config.isNull())
@ -296,7 +296,7 @@ std::shared_ptr<CAnimImage> InterfaceObjectConfigurable::buildImage(const JsonNo
std::shared_ptr<CFilledTexture> InterfaceObjectConfigurable::buildTexture(const JsonNode & config) const
{
auto image = config["image"].String();
auto rect = readRect(config);
auto rect = readRect(config["rect"]);
return std::make_shared<CFilledTexture>(image, rect);
}
@ -323,9 +323,15 @@ std::shared_ptr<CShowableAnim> InterfaceObjectConfigurable::buildAnimation(const
return anim;
}
std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(const JsonNode & config) const
std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(JsonNode config) const
{
assert(!config.isNull());
//overrides from variables
for(auto & item : config["overrides"].Struct())
{
config[item.first] = variables[item.second.String()];
}
auto type = config["type"].String();
if(type == "picture")
{

View File

@ -36,6 +36,7 @@ protected:
void init(const JsonNode & config);
void addCallback(const std::string & callbackName, std::function<void(int)> callback);
JsonNode variables;
template<class T>
const std::shared_ptr<T> widget(const std::string & name) const
@ -45,9 +46,7 @@ protected:
return nullptr;
return std::dynamic_pointer_cast<T>(iter->second);
}
const JsonNode & variable(const std::string & name) const;
//basic serializers
Point readPosition(const JsonNode &) const;
Rect readRect(const JsonNode &) const;
@ -72,11 +71,10 @@ protected:
//composite widgets
virtual std::shared_ptr<CIntObject> buildCustomWidget(const JsonNode & config);
std::shared_ptr<CIntObject> buildWidget(const JsonNode & config) const;
std::shared_ptr<CIntObject> buildWidget(JsonNode config) const;
private:
std::map<std::string, std::shared_ptr<CIntObject>> widgets;
std::map<std::string, std::function<void(int)>> callbacks;
JsonNode variables;
};

View File

@ -480,10 +480,11 @@ void TemplatesDropBox::setTemplate(const CRmgTemplate * tmpl)
}
TeamAlignmentsWidget::TeamAlignmentsWidget(RandomMapTab & randomMapTab):
CIntObject(),
InterfaceObjectConfigurable(),
randomMapTab(randomMapTab)
{
OBJ_CONSTRUCTION;
const JsonNode config(ResourceID("config/widgets/randomMapTeamsWidget.json"));
variables = config["variables"];
int humanPlayers = randomMapTab.obtainMapGenOptions().getPlayerCount();
int cpuPlayers = randomMapTab.obtainMapGenOptions().getCompOnlyPlayerCount();
@ -491,51 +492,20 @@ TeamAlignmentsWidget::TeamAlignmentsWidget(RandomMapTab & randomMapTab):
? PlayerColor::PLAYER_LIMIT_I : humanPlayers + cpuPlayers;
assert(totalPlayers <= PlayerColor::PLAYER_LIMIT_I);
auto settings = randomMapTab.obtainMapGenOptions().getPlayersSettings();
variables["totalPlayers"].Integer() = totalPlayers;
pos.w = 80 + totalPlayers * 32;
pos.h = 80 + totalPlayers * 32;
background = std::make_shared<CFilledTexture>("Bl3DCvex", pos);
center(pos);
pos.w = variables["windowSize"]["x"].Integer() + totalPlayers * variables["cellMargin"]["x"].Integer();
pos.h = variables["windowSize"]["y"].Integer() + totalPlayers * variables["cellMargin"]["y"].Integer();
variables["backgroundRect"]["x"].Integer() = pos.x;
variables["backgroundRect"]["y"].Integer() = pos.y;
variables["backgroundRect"]["w"].Integer() = pos.w;
variables["backgroundRect"]["h"].Integer() = pos.h;
variables["okButtonPosition"]["x"].Integer() = variables["buttonsOffset"]["ok"]["x"].Integer();
variables["okButtonPosition"]["y"].Integer() = variables["buttonsOffset"]["ok"]["y"].Integer() + totalPlayers * variables["cellMargin"]["y"].Integer();
variables["cancelButtonPosition"]["x"].Integer() = variables["buttonsOffset"]["cancel"]["x"].Integer();
variables["cancelButtonPosition"]["y"].Integer() = variables["buttonsOffset"]["cancel"]["y"].Integer() + totalPlayers * variables["cellMargin"]["y"].Integer();
for(int plId = 0; plId < totalPlayers; ++plId)
{
players.push_back(std::make_shared<CToggleGroup>([&, totalPlayers, plId](int sel)
{
SObjectConstruction obj__i(players[plId].get());
for(int teamId = 0; teamId < totalPlayers; ++teamId)
{
auto button = std::dynamic_pointer_cast<CToggleButton>(players[plId]->buttons[teamId]);
assert(button);
if(sel == teamId)
{
button->addOverlay(std::make_shared<CAnimImage>("ITGFLAGS", plId, 0, 8, 8));
}
else
{
button->addOverlay(std::make_shared<CPicture>("TeamPlSl"));
}
}
}));
SObjectConstruction obj__i(players.back().get());
for(int teamId = 0; teamId < totalPlayers; ++teamId)
{
Point p(40 + plId * 32, 20 + teamId * 32);
placeholders.push_back(std::make_shared<CPicture>("TeamPlSl", p.x, p.y));
auto button = std::make_shared<CToggleButton>(p, "TeamPlSl", std::pair<std::string, std::string>{"", ""});
button->pos.w = 32;
button->pos.h = 32;
players.back()->addToggle(teamId, button);
}
auto team = settings.at(PlayerColor(plId)).getTeam();
if(team == TeamID::NO_TEAM)
players.back()->setSelected(plId);
else
players.back()->setSelected(team.getNum());
}
buttonOk = std::make_shared<CButton>(Point(40, 40 + totalPlayers * 32), "MUBCHCK.DEF", CGI->generaltexth->zelp[560], [&]()
addCallback("ok", [&](int)
{
for(int plId = 0; plId < players.size(); ++plId)
{
@ -545,11 +515,57 @@ TeamAlignmentsWidget::TeamAlignmentsWidget(RandomMapTab & randomMapTab):
assert(GH.topInt().get() == this);
GH.popInt(GH.topInt());
});
buttonCancel = std::make_shared<CButton>(Point(120, 40 + totalPlayers * 32), "MUBCANC.DEF", CGI->generaltexth->zelp[561], [&]()
addCallback("cancel", [&](int)
{
assert(GH.topInt().get() == this);
GH.popInt(GH.topInt());
}, SDLK_ESCAPE);
});
init(config);
center(pos);
OBJ_CONSTRUCTION;
for(int plId = 0; plId < totalPlayers; ++plId)
{
players.push_back(std::make_shared<CToggleGroup>([&, totalPlayers, plId](int sel)
{
variables["player_id"].Integer() = plId;
SObjectConstruction obj__i(players[plId].get());
for(int teamId = 0; teamId < totalPlayers; ++teamId)
{
auto button = std::dynamic_pointer_cast<CToggleButton>(players[plId]->buttons[teamId]);
assert(button);
if(sel == teamId)
{
button->addOverlay(buildWidget(variables["flagsAnimation"]));
}
else
{
button->addOverlay(buildWidget(variables["unchecked"]));
}
}
}));
SObjectConstruction obj__i(players.back().get());
for(int teamId = 0; teamId < totalPlayers; ++teamId)
{
variables["point"]["x"].Integer() = variables["cellOffset"]["x"].Integer() + plId * variables["cellMargin"]["x"].Integer();
variables["point"]["y"].Integer() = variables["cellOffset"]["y"].Integer() + teamId * variables["cellMargin"]["y"].Integer();
//Point p(40 + plId * 32, 20 + teamId * 32);
placeholders.push_back(buildWidget(variables["placeholder"]));
auto button = buildWidget(variables["button"]);
button->pos.w = variables["cellMargin"]["x"].Integer();
button->pos.h = variables["cellMargin"]["y"].Integer();
players.back()->addToggle(teamId, std::dynamic_pointer_cast<CToggleBase>(button));
}
auto team = settings.at(PlayerColor(plId)).getTeam();
if(team == TeamID::NO_TEAM)
players.back()->setSelected(plId);
else
players.back()->setSelected(team.getNum());
}
}

View File

@ -87,7 +87,7 @@ private:
};
class TeamAlignmentsWidget: public CIntObject
class TeamAlignmentsWidget: public InterfaceObjectConfigurable
{
public:
TeamAlignmentsWidget(RandomMapTab & randomMapTab);
@ -100,5 +100,5 @@ private:
std::shared_ptr<CLabelGroup> labels;
std::shared_ptr<CButton> buttonOk, buttonCancel;
std::vector<std::shared_ptr<CToggleGroup>> players;
std::vector<std::shared_ptr<CPicture>> placeholders;
std::vector<std::shared_ptr<CIntObject>> placeholders;
};