1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-03 00:46:55 +02:00

Basic timer ui is almost complete

This commit is contained in:
nordsoft
2023-08-25 20:20:26 +04:00
parent c7ded69edc
commit d758727c23
8 changed files with 196 additions and 28 deletions

View File

@ -54,6 +54,7 @@ InterfaceObjectConfigurable::InterfaceObjectConfigurable(int used, Point offset)
REGISTER_BUILDER("slider", &InterfaceObjectConfigurable::buildSlider);
REGISTER_BUILDER("layout", &InterfaceObjectConfigurable::buildLayout);
REGISTER_BUILDER("comboBox", &InterfaceObjectConfigurable::buildComboBox);
REGISTER_BUILDER("textInput", &InterfaceObjectConfigurable::buildTextInput);
}
void InterfaceObjectConfigurable::registerBuilder(const std::string & type, BuilderFunction f)
@ -63,9 +64,15 @@ void InterfaceObjectConfigurable::registerBuilder(const std::string & type, Buil
void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function<void(int)> callback)
{
callbacks[callbackName] = callback;
callbacks_int[callbackName] = callback;
}
void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function<void(std::string)> callback)
{
callbacks_string[callbackName] = callback;
}
void InterfaceObjectConfigurable::deleteWidget(const std::string & name)
{
auto iter = widgets.find(name);
@ -340,7 +347,7 @@ std::shared_ptr<CToggleGroup> InterfaceObjectConfigurable::buildToggleGroup(cons
if(!config["selected"].isNull())
group->setSelected(config["selected"].Integer());
if(!config["callback"].isNull())
group->addCallback(callbacks.at(config["callback"].String()));
group->addCallback(callbacks_int.at(config["callback"].String()));
return group;
}
@ -413,8 +420,8 @@ void InterfaceObjectConfigurable::loadToggleButtonCallback(std::shared_ptr<CTogg
std::string callbackName = config.String();
if (callbacks.count(callbackName) > 0)
button->addCallback(callbacks.at(callbackName));
if (callbacks_int.count(callbackName) > 0)
button->addCallback(callbacks_int.at(callbackName));
else
logGlobal->error("Invalid callback '%s' in widget", callbackName );
}
@ -426,8 +433,8 @@ void InterfaceObjectConfigurable::loadButtonCallback(std::shared_ptr<CButton> bu
std::string callbackName = config.String();
if (callbacks.count(callbackName) > 0)
button->addCallback(std::bind(callbacks.at(callbackName), 0));
if (callbacks_int.count(callbackName) > 0)
button->addCallback(std::bind(callbacks_int.at(callbackName), 0));
else
logGlobal->error("Invalid callback '%s' in widget", callbackName );
}
@ -483,7 +490,7 @@ std::shared_ptr<CSlider> InterfaceObjectConfigurable::buildSlider(const JsonNode
auto value = config["selected"].Integer();
bool horizontal = config["orientation"].String() == "horizontal";
const auto & result =
std::make_shared<CSlider>(position, length, callbacks.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal ? Orientation::HORIZONTAL : Orientation::VERTICAL, style);
std::make_shared<CSlider>(position, length, callbacks_int.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal ? Orientation::HORIZONTAL : Orientation::VERTICAL, style);
if(!config["scrollBounds"].isNull())
{
@ -541,6 +548,26 @@ std::shared_ptr<ComboBox> InterfaceObjectConfigurable::buildComboBox(const JsonN
return result;
}
std::shared_ptr<CTextInput> InterfaceObjectConfigurable::buildTextInput(const JsonNode & config) const
{
logGlobal->debug("Building widget CTextInput");
auto rect = readRect(config["rect"]);
auto offset = readPosition(config["backgroundOffset"]);
auto bgName = config["background"].String();
auto result = std::make_shared<CTextInput>(rect, offset, bgName, 0);
if(!config["alignment"].isNull())
result->alignment = readTextAlignment(config["alignment"]);
if(!config["font"].isNull())
result->font = readFont(config["font"]);
if(!config["color"].isNull())
result->setColor(readColor(config["color"]));
if(!config["text"].isNull())
result->setText(readText(config["text"]));
if(!config["callback"].isNull())
result->cb += callbacks_string.at(config["callback"].String());
return result;
}
/// Small helper class that provides ownership for shared_ptr's of child elements
class InterfaceLayoutWidget : public CIntObject
{
@ -625,7 +652,7 @@ std::shared_ptr<CShowableAnim> InterfaceObjectConfigurable::buildAnimation(const
if(!config["alpha"].isNull())
anim->setAlpha(config["alpha"].Integer());
if(!config["callback"].isNull())
anim->callback = std::bind(callbacks.at(config["callback"].String()), 0);
anim->callback = std::bind(callbacks_int.at(config["callback"].String()), 0);
if(!config["frames"].isNull())
{
auto b = config["frames"]["start"].Integer();