123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- /*
- * 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"
- #include "../gui/CAnimation.h"
- #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"
- InterfaceObjectConfigurable::InterfaceObjectConfigurable(const JsonNode & config):
- CIntObject()
- {
- init(config);
- }
- InterfaceObjectConfigurable::InterfaceObjectConfigurable():
- CIntObject()
- {
- }
- void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function<void(int)> callback)
- {
- callbacks[callbackName] = callback;
- }
- void InterfaceObjectConfigurable::init(const JsonNode &config)
- {
- OBJ_CONSTRUCTION;
- int unnamedObjectId = 0;
- const std::string unnamedObjectPrefix = "__widget_";
-
- for(const auto & item : config["items"].Vector())
- {
- std::string name = item["name"].isNull()
- ? unnamedObjectPrefix + std::to_string(unnamedObjectId++)
- : item["name"].String();
- widgets[name] = buildWidget(item);
- }
- }
- std::string InterfaceObjectConfigurable::readText(const JsonNode & config) const
- {
- if(config.isNull())
- return "";
-
- if(config.isNumber())
- {
- return CGI->generaltexth->allTexts[config.Integer()];
- }
- return config.String();
- }
- Point InterfaceObjectConfigurable::readPosition(const JsonNode & config) const
- {
- Point p;
- p.x = config["x"].Integer();
- p.y = config["y"].Integer();
- return p;
- }
- ETextAlignment InterfaceObjectConfigurable::readTextAlignment(const JsonNode & config) const
- {
- if(!config.isNull())
- {
- if(config.String() == "center")
- return ETextAlignment::CENTER;
- if(config.String() == "left")
- return ETextAlignment::TOPLEFT;
- if(config.String() == "right")
- return ETextAlignment::BOTTOMRIGHT;
- }
- return ETextAlignment::CENTER;
- }
- SDL_Color InterfaceObjectConfigurable::readColor(const JsonNode & config) const
- {
- 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;
- }
- return Colors::DEFAULT_KEY_COLOR;
-
- }
- EFonts InterfaceObjectConfigurable::readFont(const JsonNode & config) const
- {
- if(!config.isNull())
- {
- 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;
- }
- return EFonts::FONT_TIMES;
- }
- std::pair<std::string, std::string> InterfaceObjectConfigurable::readHintText(const JsonNode & config) const
- {
- std::pair<std::string, std::string> result;
- if(!config.isNull())
- {
- if(config.isNumber())
- return CGI->generaltexth->zelp[config.Integer()];
-
- if(config.getType() == JsonNode::JsonType::DATA_STRUCT)
- {
- result.first = config["hover"].String();
- result.second = config["help"].String();
- return result;
- }
- if(config.getType() == JsonNode::JsonType::DATA_STRING)
- {
- result.first = result.second = config.String();
- }
- }
- return result;
- }
- std::shared_ptr<CPicture> InterfaceObjectConfigurable::buildPicture(const JsonNode & config) const
- {
- auto image = readText(config["image"]);
- auto position = readPosition(config["position"]);
- return std::make_shared<CPicture>(image, position.x, position.y);
- }
- std::shared_ptr<CLabel> InterfaceObjectConfigurable::buildLabel(const JsonNode & config) const
- {
- 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
- {
- auto position = readPosition(config["position"]);
- auto group = std::make_shared<CToggleGroup>(0);
- group->pos += position;
- if(!config["items"].isNull())
- {
- SObjectConstruction obj__i(group.get());
- int itemIdx = -1;
- for(const auto & item : config["items"].Vector())
- {
- itemIdx = item["index"].isNull() ? itemIdx + 1 : item["index"].Integer();
- group->addToggle(itemIdx, std::dynamic_pointer_cast<CToggleBase>(buildWidget(item)));
- }
- }
- 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
- {
- auto position = readPosition(config["position"]);
- auto image = config["image"].String();
- auto zelp = readHintText(config["zelp"]);
- auto button = std::make_shared<CToggleButton>(position, image, zelp);
- if(!config["selected"].isNull())
- button->setSelected(config["selected"].Bool());
- 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());
- }
- if(!config["callback"].isNull())
- button->addCallback(callbacks.at(config["callback"].String()));
- return button;
- }
- std::shared_ptr<CButton> InterfaceObjectConfigurable::buildButton(const JsonNode & config) const
- {
- auto position = readPosition(config["position"]);
- auto image = config["image"].String();
- auto zelp = readHintText(config["zelp"]);
- auto button = std::make_shared<CButton>(position, image, zelp);
- if(!config["items"].isNull())
- {
- for(const auto & item : config["items"].Vector())
- {
- button->addOverlay(buildWidget(item));
- }
- }
- if(!config["callback"].isNull())
- button->addCallback(std::bind(callbacks.at(config["callback"].String()), 0));
- return button;
- }
- std::shared_ptr<CLabelGroup> InterfaceObjectConfigurable::buildLabelGroup(const JsonNode & config) const
- {
- 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())
- {
- for(const auto & item : config["items"].Vector())
- {
- auto position = readPosition(item["position"]);
- auto text = readText(item["text"]);
- group->add(position.x, position.y, text);
- }
- }
- return group;
- }
- std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(const JsonNode & config) const
- {
- assert(!config.isNull());
- auto type = config["type"].String();
- if(type == "picture")
- {
- return buildPicture(config);
- }
- if(type == "label")
- {
- return buildLabel(config);
- }
- if(type == "toggleGroup")
- {
- return buildToggleGroup(config);
- }
- if(type == "toggleButton")
- {
- return buildToggleButton(config);
- }
- if(type == "button")
- {
- return buildButton(config);
- }
- if(type == "labelGroup")
- {
- return buildLabelGroup(config);
- }
- if(type == "custom")
- {
- return buildCustomWidget(config);
- }
- return std::shared_ptr<CIntObject>(nullptr);
- }
- std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildCustomWidget(const JsonNode & config) const
- {
- return nullptr;
- }
|