| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- #include "UserSettingParser.h"
- #include "WallpaperEngine/Data/Model/UserSetting.h"
- #include "WallpaperEngine/Data/Model/Property.h"
- using namespace WallpaperEngine::Data::Parsers;
- using namespace WallpaperEngine::Data::Builders;
- UserSettingUniquePtr UserSettingParser::parse (const json& data, const Properties& properties) {
- auto value = std::make_unique <DynamicValue> ();
- PropertySharedPtr property;
- std::optional<ConditionInfo> condition;
- auto valueIt = data;
- std::string content = data.dump ();
- if (data.is_object ()) {
- const auto user = data.optional ("user");
- const auto script = data.optional ("script");
- valueIt = data.require ("value", "User setting must have a value");
- // TODO: PARSE SCRIPT VALUES
- if (script.has_value () && !script->is_null ()) {
- sLog.error ("Found user setting with script value: ", script.value ().dump ());
- }
- if (user.has_value () && !user->is_null ()) {
- std::string source;
- if (const auto& it = *user; it.is_string ()) {
- source = it;
- } else {
- condition = ConditionInfo {
- .name = it.require <std::string> ("name", "Name for conditional setting must be present"),
- .condition = it.require <std::string> ("condition", "Condition for conditional setting must be present"),
- };
- source = condition.value ().name;
- }
- if (const auto propertyIt = properties.find (source); propertyIt != properties.end ()) {
- property = propertyIt->second;
- }
- }
- }
- // actual value parsing
- if (valueIt.is_string ()) {
- std::string str = valueIt;
- // TODO: VALIDATE THIS IS RIGHT?
- if (int size = VectorBuilder::preparseSize (str); size == 2) {
- value->update (static_cast<glm::vec2> (valueIt));
- } else if (size == 3) {
- value->update (static_cast<glm::vec3> (valueIt));
- } else {
- value->update (static_cast<glm::vec4> (valueIt));
- }
- } else if (valueIt.is_number_integer ()) {
- value->update (valueIt.get <int> ());
- } else if (valueIt.is_number_float ()) {
- value->update (valueIt.get <float> ());
- } else if (valueIt.is_boolean ()) {
- value->update (valueIt.get <bool> ());
- } else if (valueIt.is_null ()) {
- // null value with no connection to property
- value->update ();
- }
- // TODO: This might need to be removed if it causes issues with default values
- // Connect to property if one is specified (this allows property overrides to propagate)
- if (property != nullptr) {
- value->connect (property.get());
- }
- return std::make_unique <UserSetting> (UserSetting {
- .value = std::move (value),
- .property = property,
- .condition = condition,
- });
- }
|