| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- #include "DynamicValue.h"
- #include "UserSetting.h"
- #include "WallpaperEngine/Data/Utils/ScopeGuard.h"
- #include "WallpaperEngine/Logging/Log.h"
- #include "WallpaperEngine/Scripting/ScriptEngine.h"
- using namespace WallpaperEngine::Data::Model;
- DynamicValue::DynamicValue (const DynamicValue& other) {
- this->DynamicValue::update (other, UpdateSource::Initialization);
- }
- DynamicValue::DynamicValue (const glm::vec4& value) {
- this->DynamicValue::update (value, UpdateSource::Initialization);
- }
- DynamicValue::DynamicValue (const glm::vec3& value) {
- this->DynamicValue::update (value, UpdateSource::Initialization);
- }
- DynamicValue::DynamicValue (const glm::vec2& value) {
- this->DynamicValue::update (value, UpdateSource::Initialization);
- }
- DynamicValue::DynamicValue (float value) { this->DynamicValue::update (value, UpdateSource::Initialization); }
- DynamicValue::DynamicValue (int value) { this->DynamicValue::update (value, UpdateSource::Initialization); }
- DynamicValue::DynamicValue (bool value) { this->DynamicValue::update (value, UpdateSource::Initialization); }
- DynamicValue::DynamicValue (const std::string& value) {
- this->DynamicValue::update (value, UpdateSource::Initialization);
- }
- DynamicValue::DynamicValue (const Model::Color& value) {
- this->DynamicValue::update (value, UpdateSource::Initialization);
- }
- DynamicValue::~DynamicValue () {
- // TODO: properly fix this lifetime handling
- if (this->m_aliveFlag) {
- *this->m_aliveFlag = false;
- }
- this->disconnect ();
- this->m_listeners.clear ();
- }
- const glm::vec4& DynamicValue::getVec4 () const { return this->m_vec4; }
- const glm::vec3& DynamicValue::getVec3 () const { return this->m_vec3; }
- const glm::vec2& DynamicValue::getVec2 () const { return this->m_vec2; }
- const float& DynamicValue::getFloat () const { return this->m_float; }
- const int& DynamicValue::getInt () const { return this->m_int; }
- const bool& DynamicValue::getBool () const { return this->m_bool; }
- const std::string& DynamicValue::getString () const { return this->m_string; }
- DynamicValue::UnderlyingType DynamicValue::getType () const { return this->m_type; }
- std::string DynamicValue::toString () const {
- switch (this->m_type) {
- case UnderlyingType::Float:
- return std::to_string (this->m_float);
- case UnderlyingType::Int:
- return std::to_string (this->m_int);
- case UnderlyingType::Boolean:
- return std::to_string (this->m_bool);
- case UnderlyingType::Vec2:
- return std::to_string (this->m_vec2.x) + ", " + std::to_string (this->m_vec2.y);
- case UnderlyingType::Vec3:
- return std::to_string (this->m_vec3.x) + ", " + std::to_string (this->m_vec3.y) + ", "
- + std::to_string (this->m_vec3.z);
- case UnderlyingType::Vec4:
- return std::to_string (this->m_vec4.x) + ", " + std::to_string (this->m_vec4.y) + ", "
- + std::to_string (this->m_vec4.z) + ", " + std::to_string (this->m_vec4.w);
- case UnderlyingType::String:
- return this->m_string;
- default:
- return "Unknown conversion for dynamic value of type: " + std::to_string (static_cast<int> (this->m_type));
- }
- }
- void DynamicValue::update (const float newValue, UpdateSource source) {
- this->m_vec4 = glm::vec4 (newValue);
- this->m_vec3 = glm::vec3 (newValue);
- this->m_vec2 = glm::vec2 (newValue);
- this->m_float = newValue;
- this->m_int = static_cast<int> (newValue);
- this->m_bool = static_cast<int> (newValue) != 0;
- this->m_string = "";
- this->m_type = UnderlyingType::Float;
- this->propagate (source);
- }
- void DynamicValue::update (const int newValue, UpdateSource source) {
- this->m_vec4 = glm::vec4 (static_cast<float> (newValue));
- this->m_vec3 = glm::vec3 (static_cast<float> (newValue));
- this->m_vec2 = glm::vec2 (static_cast<float> (newValue));
- this->m_float = static_cast<float> (newValue);
- this->m_int = newValue;
- this->m_bool = newValue != 0;
- this->m_string = "";
- this->m_type = UnderlyingType::Int;
- this->propagate (source);
- }
- void DynamicValue::update (const bool newValue, UpdateSource source) {
- this->m_vec4 = glm::vec4 (newValue);
- this->m_vec3 = glm::vec3 (newValue);
- this->m_vec2 = glm::vec2 (newValue);
- this->m_float = newValue;
- this->m_int = newValue;
- this->m_bool = newValue;
- this->m_string = "";
- this->m_type = UnderlyingType::Boolean;
- this->propagate (source);
- }
- void DynamicValue::update (const glm::vec2& newValue, UpdateSource source) {
- this->m_vec2 = newValue;
- this->m_vec3 = glm::vec3 (newValue, 0.0f);
- this->m_vec4 = glm::vec4 (newValue, 0.0f, 0.0f);
- this->m_float = newValue.x;
- this->m_int = static_cast<int> (newValue.x);
- this->m_bool = newValue.x != 0.0f;
- this->m_string = "";
- this->m_type = UnderlyingType::Vec2;
- this->propagate (source);
- }
- void DynamicValue::update (const glm::vec3& newValue, UpdateSource source) {
- this->m_vec2 = glm::vec2 (newValue);
- this->m_vec3 = newValue;
- this->m_vec4 = glm::vec4 (newValue, 0.0f);
- this->m_float = newValue.x;
- this->m_int = static_cast<int> (newValue.x);
- this->m_bool = newValue.x != 0.0f;
- this->m_string = "";
- this->m_type = UnderlyingType::Vec3;
- this->propagate (source);
- }
- void DynamicValue::update (const glm::vec4& newValue, UpdateSource source) {
- this->m_vec2 = glm::vec2 (newValue);
- this->m_vec3 = glm::vec3 (newValue);
- this->m_vec4 = newValue;
- this->m_float = newValue.x;
- this->m_int = static_cast<int> (newValue.x);
- this->m_bool = newValue.x != 0.0f;
- this->m_string = "";
- this->m_type = UnderlyingType::Vec4;
- this->propagate (source);
- }
- void DynamicValue::update (const std::string& newValue, UpdateSource source) {
- this->m_vec2 = glm::vec2 (0.0f);
- this->m_vec3 = glm::vec3 (0.0f);
- this->m_vec4 = glm::vec4 (0.0f);
- this->m_float = 0.0f;
- this->m_int = 0;
- this->m_bool = false;
- this->m_string = newValue;
- this->m_type = UnderlyingType::String;
- if (this->m_condition.has_value ()) {
- const bool boolValue = this->m_condition.value ().condition == newValue;
- this->m_vec2 = glm::vec2 (boolValue ? 1.0f : 0.0f);
- this->m_vec3 = glm::vec3 (boolValue ? 1.0f : 0.0f);
- this->m_vec4 = glm::vec4 (boolValue ? 1.0f : 0.0f);
- this->m_float = boolValue ? 1.0f : 0.0f;
- this->m_int = boolValue ? 1 : 0;
- this->m_bool = boolValue;
- this->m_type = UnderlyingType::Boolean;
- }
- this->propagate (source);
- }
- void DynamicValue::update (const Model::Color& newValue, UpdateSource source) {
- this->m_vec2 = glm::vec2 (newValue.r, newValue.g);
- this->m_vec3 = glm::vec3 (newValue.r, newValue.g, newValue.b);
- this->m_vec4 = glm::vec4 (newValue.r, newValue.g, newValue.b, newValue.a);
- this->m_float = newValue.r;
- this->m_int = 255 * newValue.r;
- this->m_bool = newValue.a != 0.0f;
- this->m_string = "";
- this->m_type = UnderlyingType::Vec4;
- this->propagate (source);
- }
- void DynamicValue::update (const DynamicValue& other, UpdateSource source) {
- // WE's bool properties only take JSON bools (sub_1401E1A90), a combo's string without a condition
- // leaves the scene's own value in place (3401903509's "visible": {"user": "diff", "value": true})
- if (this->m_type == UnderlyingType::Boolean && !this->m_condition.has_value ()
- && other.getType () == UnderlyingType::String) {
- return;
- }
- // a slider bound to a vector property sets every component, but the property stays a vector
- const bool isVector = this->m_type == UnderlyingType::Vec2 || this->m_type == UnderlyingType::Vec3
- || this->m_type == UnderlyingType::Vec4;
- const bool isScalar = other.getType () == UnderlyingType::Float || other.getType () == UnderlyingType::Int;
- const auto keptType = this->m_type;
- this->m_vec2 = other.getVec2 ();
- this->m_vec3 = other.getVec3 ();
- this->m_vec4 = other.getVec4 ();
- this->m_float = other.getFloat ();
- this->m_int = other.getInt ();
- this->m_bool = other.getBool ();
- this->m_string = other.getString ();
- this->m_type = isVector && isScalar ? keptType : other.getType ();
- if (this->m_condition.has_value () && other.getType () == UnderlyingType::String) {
- const bool boolValue = this->m_condition.value ().condition == other.getString ();
- this->m_vec2 = glm::vec2 (boolValue ? 1.0f : 0.0f);
- this->m_vec3 = glm::vec3 (boolValue ? 1.0f : 0.0f);
- this->m_vec4 = glm::vec4 (boolValue ? 1.0f : 0.0f);
- this->m_float = boolValue ? 1.0f : 0.0f;
- this->m_int = boolValue ? 1 : 0;
- this->m_bool = boolValue;
- this->m_type = UnderlyingType::Boolean;
- }
- this->propagate (source);
- }
- void DynamicValue::update (UpdateSource source) {
- this->m_vec2 = glm::vec2 (0.0f);
- this->m_vec3 = glm::vec3 (0.0f);
- this->m_vec4 = glm::vec4 (0.0f);
- this->m_float = 0.0f;
- this->m_int = 0;
- this->m_bool = false;
- this->m_type = UnderlyingType::Null;
- this->propagate (source);
- }
- std::function<void ()> DynamicValue::listen (const std::function<void (const DynamicValue&, UpdateSource)>& callback) {
- const auto it = this->m_listeners.insert (this->m_listeners.end (), callback);
- auto alive = this->m_aliveFlag;
- return [this, it, alive] {
- if (!alive || !*alive) {
- return;
- }
- this->m_listeners.erase (it);
- };
- }
- void DynamicValue::connect (DynamicValue* other) {
- const auto lambda = [this] (const DynamicValue& other, UpdateSource source) {
- // null needs the no-arg update() - copying everything to 0 is a different meaning
- if (other.getType () == UnderlyingType::Null) {
- this->update (source);
- } else {
- this->update (other, source);
- }
- };
- const auto deregisterFunction = other->listen (lambda);
- // trigger the same update cycle immediately for the initial value
- lambda (*other, UpdateSource::Initialization);
- this->m_connections.push_back (deregisterFunction);
- }
- void DynamicValue::disconnect () {
- for (const auto& deregister : this->m_connections) {
- if (!deregister) {
- continue;
- }
- try {
- deregister ();
- } catch (...) {
- sLog.error ("Exception during listener deregistration");
- }
- }
- this->m_connections.clear ();
- }
- void DynamicValue::attachCondition (const ConditionInfo& condition) { this->m_condition = condition; }
- void DynamicValue::setScriptSource (const std::string& source) { this->m_scriptSource = source; }
- void DynamicValue::clearScriptSource () { this->m_scriptSource = std::nullopt; }
- const std::optional<std::string>& DynamicValue::getScriptSource () const { return this->m_scriptSource; }
- void DynamicValue::setAnimation (std::shared_ptr<const PropertyAnimation> animation) {
- this->m_animation = std::move (animation);
- }
- const std::shared_ptr<const PropertyAnimation>& DynamicValue::getAnimation () const { return this->m_animation; }
- std::map<std::string, UserSettingUniquePtr>& DynamicValue::getProperties () { return this->m_properties; }
- void DynamicValue::setProperties (std::map<std::string, UserSettingUniquePtr> properties) {
- this->m_properties = std::move (properties);
- }
- void DynamicValue::propagate (UpdateSource source) const {
- for (const auto& callback : this->m_listeners) {
- callback (*this, source);
- }
- }
|