add interface wizard: variables can now be used to set property values

see #107
Dieser Commit ist enthalten in:
Reinder Feenstra 2024-04-12 23:39:17 +02:00
Ursprung 558dd7e445
Commit c1f288ea47
2 geänderte Dateien mit 42 neuen und 22 gelöschten Zeilen

Datei anzeigen

@ -53,24 +53,6 @@ static void setTitleAndText(JSONWizard& wizard, TextPage* page, const QJsonObjec
page->setText(wizard.translateAndReplaceVariables(object["text"].toString()));
}
static Properties toProperties(const QJsonObject& object)
{
Properties properties;
for(const auto& key : object.keys())
{
auto value = object[key];
if(value.isBool() || value.isDouble() || value.isString())
{
properties.emplace_back(key, value.toVariant());
}
else
{
assert(false);
}
}
return properties;
}
class PageJSON
{
public:
@ -321,6 +303,7 @@ JSONWizard::JSONWizard(const QString& filename, ObjectPtr world, QWidget* parent
JSONWizard::~JSONWizard() = default;
QString JSONWizard::translateAndReplaceVariables(const QString& text) const
{
auto result = Locale::instance->parse(text);
@ -331,8 +314,9 @@ QString JSONWizard::translateAndReplaceVariables(const QString& text) const
{
if(auto it = m_variables.find(match.captured(1)); it != m_variables.end())
{
result.replace(match.capturedStart(), match.capturedLength(), Locale::instance->parse(it->second));
match = re.match(result, match.capturedStart() + it->second.length());
auto value = it->second.toString();
result.replace(match.capturedStart(), match.capturedLength(), Locale::instance->parse(value));
match = re.match(result, match.capturedStart() + value.length());
}
else
{
@ -431,7 +415,7 @@ void JSONWizard::doActions(const QJsonObject& actions)
{
for(const auto& name : setVariables.keys())
{
m_variables.emplace(name, setVariables[name].toString());
m_variables.emplace(name, setVariables[name].toVariant());
}
}
}
@ -453,3 +437,36 @@ void JSONWizard::undoActions(const QJsonObject& actions)
}
}
}
Properties JSONWizard::toProperties(const QJsonObject& object)
{
Properties properties;
for(const auto& key : object.keys())
{
auto value = object[key];
if(value.isString())
{
static const QRegularExpression re{"^%([a-z_]+)%$"};
auto match = re.match(value.toString());
if(match.hasMatch())
{
if(auto it = m_variables.find(match.captured(1)); it != m_variables.end())
{
properties.emplace_back(key, it->second);
continue;
}
}
}
if(value.isBool() || value.isDouble() || value.isString())
{
properties.emplace_back(key, value.toVariant());
}
else
{
assert(false);
}
}
return properties;
}

Datei anzeigen

@ -28,6 +28,7 @@
#include <QJsonObject>
#include <QFuture>
#include "../network/objectptr.hpp"
#include "../network/create/properties.hpp"
class CreateInterface;
@ -42,7 +43,7 @@ class JSONWizard : public Wizard
ObjectPtr m_world;
std::map<QString, QFuture<ObjectPtr>> m_objects;
std::map<QString, QString> m_variables;
std::map<QString, QVariant> m_variables;
std::map<QString, PageInfo> m_pages;
QVector<int> m_ids;
std::shared_ptr<CreateInterface> m_createInterface;
@ -52,6 +53,8 @@ class JSONWizard : public Wizard
void doActions(const QJsonObject& actions);
void undoActions(const QJsonObject& actions);
Properties toProperties(const QJsonObject& object);
protected:
void initializePage(int id) override;
void cleanupPage(int id) override;