2022-12-12 09:48:39 +02:00
|
|
|
/*
|
|
|
|
* InterfaceBuilder.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 "InterfaceObjectConfigurable.h"
|
|
|
|
|
|
|
|
#include "../CGameInfo.h"
|
2023-01-04 16:40:15 +02:00
|
|
|
#include "../CPlayerInterface.h"
|
2022-12-12 09:48:39 +02:00
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../widgets/CComponent.h"
|
|
|
|
#include "../widgets/Buttons.h"
|
|
|
|
#include "../widgets/MiscWidgets.h"
|
|
|
|
#include "../widgets/ObjectLists.h"
|
|
|
|
#include "../widgets/TextControls.h"
|
|
|
|
#include "../windows/GUIClasses.h"
|
|
|
|
#include "../windows/InfoWindows.h"
|
|
|
|
|
|
|
|
#include "../../lib/CGeneralTextHandler.h"
|
|
|
|
|
2022-12-25 12:22:07 +02:00
|
|
|
static std::map<std::string, int> KeycodeMap{
|
|
|
|
{"up", SDLK_UP},
|
|
|
|
{"down", SDLK_DOWN},
|
|
|
|
{"left", SDLK_LEFT},
|
|
|
|
{"right", SDLK_RIGHT},
|
|
|
|
{"space", SDLK_SPACE},
|
|
|
|
{"enter", SDLK_RETURN}
|
|
|
|
};
|
|
|
|
|
2022-12-12 09:48:39 +02:00
|
|
|
|
2022-12-16 00:15:53 +02:00
|
|
|
InterfaceObjectConfigurable::InterfaceObjectConfigurable(const JsonNode & config, int used, Point offset):
|
2022-12-21 22:54:06 +02:00
|
|
|
InterfaceObjectConfigurable(used, offset)
|
2022-12-12 09:48:39 +02:00
|
|
|
{
|
2022-12-25 12:22:07 +02:00
|
|
|
build(config);
|
2022-12-12 09:48:39 +02:00
|
|
|
}
|
|
|
|
|
2022-12-16 00:15:53 +02:00
|
|
|
InterfaceObjectConfigurable::InterfaceObjectConfigurable(int used, Point offset):
|
|
|
|
CIntObject(used, offset)
|
2022-12-12 09:48:39 +02:00
|
|
|
{
|
2022-12-22 00:05:29 +02:00
|
|
|
REGISTER_BUILDER("picture", &InterfaceObjectConfigurable::buildPicture);
|
|
|
|
REGISTER_BUILDER("image", &InterfaceObjectConfigurable::buildImage);
|
|
|
|
REGISTER_BUILDER("texture", &InterfaceObjectConfigurable::buildTexture);
|
|
|
|
REGISTER_BUILDER("animation", &InterfaceObjectConfigurable::buildAnimation);
|
|
|
|
REGISTER_BUILDER("label", &InterfaceObjectConfigurable::buildLabel);
|
|
|
|
REGISTER_BUILDER("toggleGroup", &InterfaceObjectConfigurable::buildToggleGroup);
|
|
|
|
REGISTER_BUILDER("toggleButton", &InterfaceObjectConfigurable::buildToggleButton);
|
|
|
|
REGISTER_BUILDER("button", &InterfaceObjectConfigurable::buildButton);
|
|
|
|
REGISTER_BUILDER("labelGroup", &InterfaceObjectConfigurable::buildLabelGroup);
|
|
|
|
REGISTER_BUILDER("slider", &InterfaceObjectConfigurable::buildSlider);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterfaceObjectConfigurable::registerBuilder(const std::string & type, BuilderFunction f)
|
|
|
|
{
|
|
|
|
builders[type] = f;
|
2022-12-12 09:48:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function<void(int)> callback)
|
|
|
|
{
|
|
|
|
callbacks[callbackName] = callback;
|
|
|
|
}
|
|
|
|
|
2022-12-25 12:33:10 +02:00
|
|
|
void InterfaceObjectConfigurable::deleteWidget(const std::string & name)
|
|
|
|
{
|
|
|
|
auto iter = widgets.find(name);
|
|
|
|
if(iter != widgets.end())
|
|
|
|
widgets.erase(iter);
|
|
|
|
}
|
|
|
|
|
2022-12-25 12:22:07 +02:00
|
|
|
void InterfaceObjectConfigurable::build(const JsonNode &config)
|
2022-12-12 09:48:39 +02:00
|
|
|
{
|
|
|
|
OBJ_CONSTRUCTION;
|
2022-12-22 00:08:26 +02:00
|
|
|
logGlobal->debug("Building configurable interface object");
|
2022-12-25 12:22:07 +02:00
|
|
|
auto * items = &config;
|
|
|
|
|
|
|
|
if(config.getType() == JsonNode::JsonType::DATA_STRUCT)
|
2022-12-17 18:38:16 +02:00
|
|
|
{
|
2022-12-25 12:22:07 +02:00
|
|
|
for(auto & item : config["variables"].Struct())
|
|
|
|
{
|
|
|
|
logGlobal->debug("Read variable named %s", item.first);
|
|
|
|
variables[item.first] = item.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
items = &config["items"];
|
2022-12-17 18:38:16 +02:00
|
|
|
}
|
|
|
|
|
2022-12-12 09:48:39 +02:00
|
|
|
const std::string unnamedObjectPrefix = "__widget_";
|
2022-12-25 12:22:07 +02:00
|
|
|
for(const auto & item : items->Vector())
|
2022-12-12 09:48:39 +02:00
|
|
|
{
|
|
|
|
std::string name = item["name"].isNull()
|
|
|
|
? unnamedObjectPrefix + std::to_string(unnamedObjectId++)
|
|
|
|
: item["name"].String();
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Building widget with name %s", name);
|
2022-12-12 09:48:39 +02:00
|
|
|
widgets[name] = buildWidget(item);
|
|
|
|
}
|
2022-12-16 00:15:53 +02:00
|
|
|
}
|
|
|
|
|
2022-12-15 22:46:36 +02:00
|
|
|
std::string InterfaceObjectConfigurable::readText(const JsonNode & config) const
|
2022-12-12 09:48:39 +02:00
|
|
|
{
|
|
|
|
if(config.isNull())
|
|
|
|
return "";
|
|
|
|
|
2022-12-16 00:48:07 +02:00
|
|
|
std::string s = config.String();
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Reading text from translations by key: %s", s);
|
2022-12-27 22:19:05 +02:00
|
|
|
return CGI->generaltexth->translate(s);
|
2022-12-12 09:48:39 +02:00
|
|
|
}
|
|
|
|
|
2022-12-15 22:46:36 +02:00
|
|
|
Point InterfaceObjectConfigurable::readPosition(const JsonNode & config) const
|
2022-12-12 09:48:39 +02:00
|
|
|
{
|
2022-12-15 22:46:36 +02:00
|
|
|
Point p;
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Reading point");
|
2022-12-15 22:46:36 +02:00
|
|
|
p.x = config["x"].Integer();
|
|
|
|
p.y = config["y"].Integer();
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2022-12-17 06:19:16 +02:00
|
|
|
Rect InterfaceObjectConfigurable::readRect(const JsonNode & config) const
|
|
|
|
{
|
|
|
|
Rect p;
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Reading rect");
|
2022-12-17 06:19:16 +02:00
|
|
|
p.x = config["x"].Integer();
|
|
|
|
p.y = config["y"].Integer();
|
|
|
|
p.w = config["w"].Integer();
|
|
|
|
p.h = config["h"].Integer();
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2022-12-15 22:46:36 +02:00
|
|
|
ETextAlignment InterfaceObjectConfigurable::readTextAlignment(const JsonNode & config) const
|
|
|
|
{
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Reading text alignment");
|
2022-12-15 22:46:36 +02:00
|
|
|
if(!config.isNull())
|
2022-12-12 09:48:39 +02:00
|
|
|
{
|
2022-12-15 22:46:36 +02:00
|
|
|
if(config.String() == "center")
|
|
|
|
return ETextAlignment::CENTER;
|
|
|
|
if(config.String() == "left")
|
|
|
|
return ETextAlignment::TOPLEFT;
|
|
|
|
if(config.String() == "right")
|
|
|
|
return ETextAlignment::BOTTOMRIGHT;
|
2022-12-12 09:48:39 +02:00
|
|
|
}
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Uknown text alignment attribute");
|
2022-12-15 22:46:36 +02:00
|
|
|
return ETextAlignment::CENTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Color InterfaceObjectConfigurable::readColor(const JsonNode & config) const
|
|
|
|
{
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Reading color");
|
2022-12-15 22:46:36 +02:00
|
|
|
if(!config.isNull())
|
|
|
|
{
|
|
|
|
if(config.String() == "yellow")
|
|
|
|
return Colors::YELLOW;
|
|
|
|
if(config.String() == "white")
|
|
|
|
return Colors::WHITE;
|
|
|
|
if(config.String() == "gold")
|
|
|
|
return Colors::METALLIC_GOLD;
|
|
|
|
if(config.String() == "green")
|
|
|
|
return Colors::GREEN;
|
|
|
|
if(config.String() == "orange")
|
|
|
|
return Colors::ORANGE;
|
|
|
|
if(config.String() == "bright-yellow")
|
|
|
|
return Colors::BRIGHT_YELLOW;
|
2022-12-12 09:48:39 +02:00
|
|
|
}
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Uknown color attribute");
|
2022-12-15 22:46:36 +02:00
|
|
|
return Colors::DEFAULT_KEY_COLOR;
|
2022-12-12 09:48:39 +02:00
|
|
|
|
2022-12-15 22:46:36 +02:00
|
|
|
}
|
|
|
|
EFonts InterfaceObjectConfigurable::readFont(const JsonNode & config) const
|
|
|
|
{
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Reading font");
|
2022-12-15 22:46:36 +02:00
|
|
|
if(!config.isNull())
|
2022-12-12 09:48:39 +02:00
|
|
|
{
|
2022-12-15 22:46:36 +02:00
|
|
|
if(config.String() == "big")
|
|
|
|
return EFonts::FONT_BIG;
|
|
|
|
if(config.String() == "medium")
|
|
|
|
return EFonts::FONT_MEDIUM;
|
|
|
|
if(config.String() == "small")
|
|
|
|
return EFonts::FONT_SMALL;
|
|
|
|
if(config.String() == "tiny")
|
|
|
|
return EFonts::FONT_TINY;
|
2022-12-12 09:48:39 +02:00
|
|
|
}
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Uknown font attribute");
|
2022-12-15 22:46:36 +02:00
|
|
|
return EFonts::FONT_TIMES;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<std::string, std::string> InterfaceObjectConfigurable::readHintText(const JsonNode & config) const
|
|
|
|
{
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Reading hint text");
|
2022-12-15 22:46:36 +02:00
|
|
|
std::pair<std::string, std::string> result;
|
|
|
|
if(!config.isNull())
|
2022-12-12 09:48:39 +02:00
|
|
|
{
|
2022-12-15 22:46:36 +02:00
|
|
|
if(config.getType() == JsonNode::JsonType::DATA_STRUCT)
|
|
|
|
{
|
2022-12-16 00:49:52 +02:00
|
|
|
result.first = readText(config["hover"]);
|
|
|
|
result.second = readText(config["help"]);
|
2022-12-15 22:46:36 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if(config.getType() == JsonNode::JsonType::DATA_STRING)
|
|
|
|
{
|
2022-12-28 23:23:11 +02:00
|
|
|
logGlobal->debug("Reading hint text (help) from generaltext handler:%sd", config.String());
|
2022-12-30 19:09:19 +02:00
|
|
|
result.first = CGI->generaltexth->translate( config.String(), "hover");
|
|
|
|
result.second = CGI->generaltexth->translate( config.String(), "help");
|
2022-12-15 22:46:36 +02:00
|
|
|
}
|
2022-12-12 09:48:39 +02:00
|
|
|
}
|
2022-12-15 22:46:36 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-12-25 12:22:07 +02:00
|
|
|
int InterfaceObjectConfigurable::readKeycode(const JsonNode & config) const
|
|
|
|
{
|
|
|
|
logGlobal->debug("Reading keycode");
|
|
|
|
if(config.getType() == JsonNode::JsonType::DATA_INTEGER)
|
|
|
|
return config.Integer();
|
|
|
|
|
|
|
|
if(config.getType() == JsonNode::JsonType::DATA_STRING)
|
|
|
|
{
|
|
|
|
auto s = config.String();
|
|
|
|
if(s.size() == 1) //keyboard symbol
|
|
|
|
return s[0];
|
|
|
|
return KeycodeMap[s];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-15 22:46:36 +02:00
|
|
|
std::shared_ptr<CPicture> InterfaceObjectConfigurable::buildPicture(const JsonNode & config) const
|
|
|
|
{
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Building widget CPicture");
|
2022-12-16 00:48:07 +02:00
|
|
|
auto image = config["image"].String();
|
2022-12-15 22:46:36 +02:00
|
|
|
auto position = readPosition(config["position"]);
|
2022-12-16 00:15:53 +02:00
|
|
|
auto pic = std::make_shared<CPicture>(image, position.x, position.y);
|
|
|
|
if(!config["visible"].isNull())
|
|
|
|
pic->visible = config["visible"].Bool();
|
2023-01-04 16:40:15 +02:00
|
|
|
|
|
|
|
if ( config["playerColored"].Bool() && LOCPLINT)
|
|
|
|
pic->colorize(LOCPLINT->playerID);
|
2022-12-16 00:15:53 +02:00
|
|
|
return pic;
|
2022-12-15 22:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CLabel> InterfaceObjectConfigurable::buildLabel(const JsonNode & config) const
|
|
|
|
{
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Building widget CLabel");
|
2022-12-15 22:46:36 +02:00
|
|
|
auto font = readFont(config["font"]);
|
|
|
|
auto alignment = readTextAlignment(config["alignment"]);
|
|
|
|
auto color = readColor(config["color"]);
|
|
|
|
auto text = readText(config["text"]);
|
|
|
|
auto position = readPosition(config["position"]);
|
|
|
|
return std::make_shared<CLabel>(position.x, position.y, font, alignment, color, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CToggleGroup> InterfaceObjectConfigurable::buildToggleGroup(const JsonNode & config) const
|
|
|
|
{
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Building widget CToggleGroup");
|
2022-12-15 22:46:36 +02:00
|
|
|
auto position = readPosition(config["position"]);
|
|
|
|
auto group = std::make_shared<CToggleGroup>(0);
|
|
|
|
group->pos += position;
|
|
|
|
if(!config["items"].isNull())
|
2022-12-12 09:48:39 +02:00
|
|
|
{
|
2022-12-21 22:54:06 +02:00
|
|
|
OBJ_CONSTRUCTION_TARGETED(group.get());
|
2022-12-15 22:46:36 +02:00
|
|
|
int itemIdx = -1;
|
|
|
|
for(const auto & item : config["items"].Vector())
|
2022-12-12 09:48:39 +02:00
|
|
|
{
|
2022-12-15 22:46:36 +02:00
|
|
|
itemIdx = item["index"].isNull() ? itemIdx + 1 : item["index"].Integer();
|
|
|
|
group->addToggle(itemIdx, std::dynamic_pointer_cast<CToggleBase>(buildWidget(item)));
|
2022-12-12 09:48:39 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-15 22:46:36 +02:00
|
|
|
if(!config["selected"].isNull())
|
|
|
|
group->setSelected(config["selected"].Integer());
|
|
|
|
if(!config["callback"].isNull())
|
|
|
|
group->addCallback(callbacks.at(config["callback"].String()));
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CToggleButton> InterfaceObjectConfigurable::buildToggleButton(const JsonNode & config) const
|
|
|
|
{
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Building widget CToggleButton");
|
2022-12-15 22:46:36 +02:00
|
|
|
auto position = readPosition(config["position"]);
|
|
|
|
auto image = config["image"].String();
|
2022-12-28 23:23:11 +02:00
|
|
|
auto help = readHintText(config["help"]);
|
|
|
|
auto button = std::make_shared<CToggleButton>(position, image, help);
|
2023-02-14 23:22:11 +02:00
|
|
|
if(!config["items"].isNull())
|
|
|
|
{
|
|
|
|
for(const auto & item : config["items"].Vector())
|
|
|
|
{
|
|
|
|
button->addOverlay(buildWidget(item));
|
|
|
|
}
|
|
|
|
}
|
2022-12-15 22:46:36 +02:00
|
|
|
if(!config["selected"].isNull())
|
|
|
|
button->setSelected(config["selected"].Bool());
|
|
|
|
if(!config["imageOrder"].isNull())
|
2022-12-12 09:48:39 +02:00
|
|
|
{
|
2022-12-15 22:46:36 +02:00
|
|
|
auto imgOrder = config["imageOrder"].Vector();
|
|
|
|
assert(imgOrder.size() >= 4);
|
|
|
|
button->setImageOrder(imgOrder[0].Integer(), imgOrder[1].Integer(), imgOrder[2].Integer(), imgOrder[3].Integer());
|
|
|
|
}
|
|
|
|
if(!config["callback"].isNull())
|
|
|
|
button->addCallback(callbacks.at(config["callback"].String()));
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CButton> InterfaceObjectConfigurable::buildButton(const JsonNode & config) const
|
|
|
|
{
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Building widget CButton");
|
2022-12-15 22:46:36 +02:00
|
|
|
auto position = readPosition(config["position"]);
|
|
|
|
auto image = config["image"].String();
|
2022-12-28 23:23:11 +02:00
|
|
|
auto help = readHintText(config["help"]);
|
|
|
|
auto button = std::make_shared<CButton>(position, image, help);
|
2022-12-15 22:46:36 +02:00
|
|
|
if(!config["items"].isNull())
|
|
|
|
{
|
|
|
|
for(const auto & item : config["items"].Vector())
|
2022-12-12 09:48:39 +02:00
|
|
|
{
|
2022-12-15 22:46:36 +02:00
|
|
|
button->addOverlay(buildWidget(item));
|
2022-12-12 09:48:39 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-25 12:22:07 +02:00
|
|
|
if(!config["imageOrder"].isNull())
|
|
|
|
{
|
|
|
|
auto imgOrder = config["imageOrder"].Vector();
|
|
|
|
assert(imgOrder.size() >= 4);
|
|
|
|
button->setImageOrder(imgOrder[0].Integer(), imgOrder[1].Integer(), imgOrder[2].Integer(), imgOrder[3].Integer());
|
|
|
|
}
|
2022-12-15 22:46:36 +02:00
|
|
|
if(!config["callback"].isNull())
|
|
|
|
button->addCallback(std::bind(callbacks.at(config["callback"].String()), 0));
|
2022-12-25 12:22:07 +02:00
|
|
|
if(!config["hotkey"].isNull())
|
|
|
|
{
|
|
|
|
if(config["hotkey"].getType() == JsonNode::JsonType::DATA_VECTOR)
|
|
|
|
{
|
|
|
|
for(auto k : config["hotkey"].Vector())
|
|
|
|
button->assignedKeys.insert(readKeycode(k));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
button->assignedKeys.insert(readKeycode(config["hotkey"]));
|
|
|
|
}
|
2022-12-15 22:46:36 +02:00
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CLabelGroup> InterfaceObjectConfigurable::buildLabelGroup(const JsonNode & config) const
|
|
|
|
{
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Building widget CLabelGroup");
|
2022-12-15 22:46:36 +02:00
|
|
|
auto font = readFont(config["font"]);
|
|
|
|
auto alignment = readTextAlignment(config["alignment"]);
|
|
|
|
auto color = readColor(config["color"]);
|
|
|
|
auto group = std::make_shared<CLabelGroup>(font, alignment, color);
|
|
|
|
if(!config["items"].isNull())
|
2022-12-12 09:48:39 +02:00
|
|
|
{
|
2022-12-15 22:46:36 +02:00
|
|
|
for(const auto & item : config["items"].Vector())
|
2022-12-13 01:47:29 +02:00
|
|
|
{
|
2022-12-15 22:46:36 +02:00
|
|
|
auto position = readPosition(item["position"]);
|
|
|
|
auto text = readText(item["text"]);
|
|
|
|
group->add(position.x, position.y, text);
|
2022-12-13 01:47:29 +02:00
|
|
|
}
|
2022-12-15 22:46:36 +02:00
|
|
|
}
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
2022-12-16 00:15:53 +02:00
|
|
|
std::shared_ptr<CSlider> InterfaceObjectConfigurable::buildSlider(const JsonNode & config) const
|
|
|
|
{
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Building widget CSlider");
|
2022-12-16 00:15:53 +02:00
|
|
|
auto position = readPosition(config["position"]);
|
|
|
|
int length = config["size"].Integer();
|
|
|
|
auto style = config["style"].String() == "brown" ? CSlider::BROWN : CSlider::BLUE;
|
|
|
|
auto itemsVisible = config["itemsVisible"].Integer();
|
|
|
|
auto itemsTotal = config["itemsTotal"].Integer();
|
|
|
|
auto value = config["selected"].Integer();
|
|
|
|
bool horizontal = config["orientation"].String() == "horizontal";
|
|
|
|
return std::make_shared<CSlider>(position, length, callbacks.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal, style);
|
|
|
|
}
|
|
|
|
|
2022-12-17 06:19:16 +02:00
|
|
|
std::shared_ptr<CAnimImage> InterfaceObjectConfigurable::buildImage(const JsonNode & config) const
|
|
|
|
{
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Building widget CAnimImage");
|
2022-12-17 06:19:16 +02:00
|
|
|
auto position = readPosition(config["position"]);
|
|
|
|
auto image = config["image"].String();
|
|
|
|
int group = config["group"].isNull() ? 0 : config["group"].Integer();
|
|
|
|
int frame = config["frame"].isNull() ? 0 : config["frame"].Integer();
|
|
|
|
return std::make_shared<CAnimImage>(image, frame, group, position.x, position.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CFilledTexture> InterfaceObjectConfigurable::buildTexture(const JsonNode & config) const
|
|
|
|
{
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Building widget CFilledTexture");
|
2022-12-17 06:19:16 +02:00
|
|
|
auto image = config["image"].String();
|
2022-12-17 18:38:16 +02:00
|
|
|
auto rect = readRect(config["rect"]);
|
2022-12-17 06:19:16 +02:00
|
|
|
return std::make_shared<CFilledTexture>(image, rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CShowableAnim> InterfaceObjectConfigurable::buildAnimation(const JsonNode & config) const
|
|
|
|
{
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Building widget CShowableAnim");
|
2022-12-17 06:19:16 +02:00
|
|
|
auto position = readPosition(config["position"]);
|
|
|
|
auto image = config["image"].String();
|
|
|
|
ui8 flags = 0;
|
|
|
|
if(!config["repeat"].Bool())
|
|
|
|
flags |= CShowableAnim::EFlags::PLAY_ONCE;
|
|
|
|
|
|
|
|
int group = config["group"].isNull() ? 0 : config["group"].Integer();
|
|
|
|
auto anim = std::make_shared<CShowableAnim>(position.x, position.y, image, flags, 4, group);
|
|
|
|
if(!config["alpha"].isNull())
|
|
|
|
anim->setAlpha(config["alpha"].Integer());
|
|
|
|
if(!config["callback"].isNull())
|
|
|
|
anim->callback = std::bind(callbacks.at(config["callback"].String()), 0);
|
|
|
|
if(!config["frames"].isNull())
|
|
|
|
{
|
|
|
|
auto b = config["frames"]["start"].Integer();
|
|
|
|
auto e = config["frames"]["end"].Integer();
|
|
|
|
anim->set(group, b, e);
|
|
|
|
}
|
|
|
|
return anim;
|
|
|
|
}
|
|
|
|
|
2022-12-17 18:38:16 +02:00
|
|
|
std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(JsonNode config) const
|
2022-12-15 22:46:36 +02:00
|
|
|
{
|
|
|
|
assert(!config.isNull());
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Building widget from config");
|
2022-12-17 18:38:16 +02:00
|
|
|
//overrides from variables
|
|
|
|
for(auto & item : config["overrides"].Struct())
|
|
|
|
{
|
2022-12-21 23:13:32 +02:00
|
|
|
logGlobal->debug("Config attribute %s was overriden by variable %s", item.first, item.second.String());
|
2022-12-17 18:38:16 +02:00
|
|
|
config[item.first] = variables[item.second.String()];
|
|
|
|
}
|
|
|
|
|
2022-12-15 22:46:36 +02:00
|
|
|
auto type = config["type"].String();
|
2022-12-22 00:05:29 +02:00
|
|
|
auto buildIterator = builders.find(type);
|
|
|
|
if(buildIterator != builders.end())
|
|
|
|
return (buildIterator->second)(config);
|
2022-12-21 23:37:33 +02:00
|
|
|
|
2022-12-22 00:05:29 +02:00
|
|
|
logGlobal->error("Builder with type %s is not registered", type);
|
2022-12-15 22:57:10 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|