DynamicValue.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include "DynamicValue.h"
  2. #include "UserSetting.h"
  3. #include "WallpaperEngine/Data/Utils/ScopeGuard.h"
  4. #include "WallpaperEngine/Logging/Log.h"
  5. #include "WallpaperEngine/Scripting/ScriptEngine.h"
  6. using namespace WallpaperEngine::Data::Model;
  7. DynamicValue::DynamicValue (const DynamicValue& other) {
  8. this->DynamicValue::update (other, UpdateSource::Initialization);
  9. }
  10. DynamicValue::DynamicValue (const glm::vec4& value) {
  11. this->DynamicValue::update (value, UpdateSource::Initialization);
  12. }
  13. DynamicValue::DynamicValue (const glm::vec3& value) {
  14. this->DynamicValue::update (value, UpdateSource::Initialization);
  15. }
  16. DynamicValue::DynamicValue (const glm::vec2& value) {
  17. this->DynamicValue::update (value, UpdateSource::Initialization);
  18. }
  19. DynamicValue::DynamicValue (float value) { this->DynamicValue::update (value, UpdateSource::Initialization); }
  20. DynamicValue::DynamicValue (int value) { this->DynamicValue::update (value, UpdateSource::Initialization); }
  21. DynamicValue::DynamicValue (bool value) { this->DynamicValue::update (value, UpdateSource::Initialization); }
  22. DynamicValue::DynamicValue (const std::string& value) {
  23. this->DynamicValue::update (value, UpdateSource::Initialization);
  24. }
  25. DynamicValue::DynamicValue (const Model::Color& value) {
  26. this->DynamicValue::update (value, UpdateSource::Initialization);
  27. }
  28. DynamicValue::~DynamicValue () {
  29. // TODO: properly fix this lifetime handling
  30. if (this->m_aliveFlag) {
  31. *this->m_aliveFlag = false;
  32. }
  33. this->disconnect ();
  34. this->m_listeners.clear ();
  35. }
  36. const glm::vec4& DynamicValue::getVec4 () const { return this->m_vec4; }
  37. const glm::vec3& DynamicValue::getVec3 () const { return this->m_vec3; }
  38. const glm::vec2& DynamicValue::getVec2 () const { return this->m_vec2; }
  39. const float& DynamicValue::getFloat () const { return this->m_float; }
  40. const int& DynamicValue::getInt () const { return this->m_int; }
  41. const bool& DynamicValue::getBool () const { return this->m_bool; }
  42. const std::string& DynamicValue::getString () const { return this->m_string; }
  43. DynamicValue::UnderlyingType DynamicValue::getType () const { return this->m_type; }
  44. std::string DynamicValue::toString () const {
  45. switch (this->m_type) {
  46. case UnderlyingType::Float:
  47. return std::to_string (this->m_float);
  48. case UnderlyingType::Int:
  49. return std::to_string (this->m_int);
  50. case UnderlyingType::Boolean:
  51. return std::to_string (this->m_bool);
  52. case UnderlyingType::Vec2:
  53. return std::to_string (this->m_vec2.x) + ", " + std::to_string (this->m_vec2.y);
  54. case UnderlyingType::Vec3:
  55. return std::to_string (this->m_vec3.x) + ", " + std::to_string (this->m_vec3.y) + ", "
  56. + std::to_string (this->m_vec3.z);
  57. case UnderlyingType::Vec4:
  58. return std::to_string (this->m_vec4.x) + ", " + std::to_string (this->m_vec4.y) + ", "
  59. + std::to_string (this->m_vec4.z) + ", " + std::to_string (this->m_vec4.w);
  60. case UnderlyingType::String:
  61. return this->m_string;
  62. default:
  63. return "Unknown conversion for dynamic value of type: " + std::to_string (static_cast<int> (this->m_type));
  64. }
  65. }
  66. void DynamicValue::update (const float newValue, UpdateSource source) {
  67. this->m_vec4 = glm::vec4 (newValue);
  68. this->m_vec3 = glm::vec3 (newValue);
  69. this->m_vec2 = glm::vec2 (newValue);
  70. this->m_float = newValue;
  71. this->m_int = static_cast<int> (newValue);
  72. this->m_bool = static_cast<int> (newValue) != 0;
  73. this->m_string = "";
  74. this->m_type = UnderlyingType::Float;
  75. this->propagate (source);
  76. }
  77. void DynamicValue::update (const int newValue, UpdateSource source) {
  78. this->m_vec4 = glm::vec4 (static_cast<float> (newValue));
  79. this->m_vec3 = glm::vec3 (static_cast<float> (newValue));
  80. this->m_vec2 = glm::vec2 (static_cast<float> (newValue));
  81. this->m_float = static_cast<float> (newValue);
  82. this->m_int = newValue;
  83. this->m_bool = newValue != 0;
  84. this->m_string = "";
  85. this->m_type = UnderlyingType::Int;
  86. this->propagate (source);
  87. }
  88. void DynamicValue::update (const bool newValue, UpdateSource source) {
  89. this->m_vec4 = glm::vec4 (newValue);
  90. this->m_vec3 = glm::vec3 (newValue);
  91. this->m_vec2 = glm::vec2 (newValue);
  92. this->m_float = newValue;
  93. this->m_int = newValue;
  94. this->m_bool = newValue;
  95. this->m_string = "";
  96. this->m_type = UnderlyingType::Boolean;
  97. this->propagate (source);
  98. }
  99. void DynamicValue::update (const glm::vec2& newValue, UpdateSource source) {
  100. this->m_vec2 = newValue;
  101. this->m_vec3 = glm::vec3 (newValue, 0.0f);
  102. this->m_vec4 = glm::vec4 (newValue, 0.0f, 0.0f);
  103. this->m_float = newValue.x;
  104. this->m_int = static_cast<int> (newValue.x);
  105. this->m_bool = newValue.x != 0.0f;
  106. this->m_string = "";
  107. this->m_type = UnderlyingType::Vec2;
  108. this->propagate (source);
  109. }
  110. void DynamicValue::update (const glm::vec3& newValue, UpdateSource source) {
  111. this->m_vec2 = glm::vec2 (newValue);
  112. this->m_vec3 = newValue;
  113. this->m_vec4 = glm::vec4 (newValue, 0.0f);
  114. this->m_float = newValue.x;
  115. this->m_int = static_cast<int> (newValue.x);
  116. this->m_bool = newValue.x != 0.0f;
  117. this->m_string = "";
  118. this->m_type = UnderlyingType::Vec3;
  119. this->propagate (source);
  120. }
  121. void DynamicValue::update (const glm::vec4& newValue, UpdateSource source) {
  122. this->m_vec2 = glm::vec2 (newValue);
  123. this->m_vec3 = glm::vec3 (newValue);
  124. this->m_vec4 = newValue;
  125. this->m_float = newValue.x;
  126. this->m_int = static_cast<int> (newValue.x);
  127. this->m_bool = newValue.x != 0.0f;
  128. this->m_string = "";
  129. this->m_type = UnderlyingType::Vec4;
  130. this->propagate (source);
  131. }
  132. void DynamicValue::update (const std::string& newValue, UpdateSource source) {
  133. this->m_vec2 = glm::vec2 (0.0f);
  134. this->m_vec3 = glm::vec3 (0.0f);
  135. this->m_vec4 = glm::vec4 (0.0f);
  136. this->m_float = 0.0f;
  137. this->m_int = 0;
  138. this->m_bool = false;
  139. this->m_string = newValue;
  140. this->m_type = UnderlyingType::String;
  141. if (this->m_condition.has_value ()) {
  142. const bool boolValue = this->m_condition.value ().condition == newValue;
  143. this->m_vec2 = glm::vec2 (boolValue ? 1.0f : 0.0f);
  144. this->m_vec3 = glm::vec3 (boolValue ? 1.0f : 0.0f);
  145. this->m_vec4 = glm::vec4 (boolValue ? 1.0f : 0.0f);
  146. this->m_float = boolValue ? 1.0f : 0.0f;
  147. this->m_int = boolValue ? 1 : 0;
  148. this->m_bool = boolValue;
  149. this->m_type = UnderlyingType::Boolean;
  150. }
  151. this->propagate (source);
  152. }
  153. void DynamicValue::update (const Model::Color& newValue, UpdateSource source) {
  154. this->m_vec2 = glm::vec2 (newValue.r, newValue.g);
  155. this->m_vec3 = glm::vec3 (newValue.r, newValue.g, newValue.b);
  156. this->m_vec4 = glm::vec4 (newValue.r, newValue.g, newValue.b, newValue.a);
  157. this->m_float = newValue.r;
  158. this->m_int = 255 * newValue.r;
  159. this->m_bool = newValue.a != 0.0f;
  160. this->m_string = "";
  161. this->m_type = UnderlyingType::Vec4;
  162. this->propagate (source);
  163. }
  164. void DynamicValue::update (const DynamicValue& other, UpdateSource source) {
  165. // WE's bool properties only take JSON bools (sub_1401E1A90), a combo's string without a condition
  166. // leaves the scene's own value in place (3401903509's "visible": {"user": "diff", "value": true})
  167. if (this->m_type == UnderlyingType::Boolean && !this->m_condition.has_value ()
  168. && other.getType () == UnderlyingType::String) {
  169. return;
  170. }
  171. // a slider bound to a vector property sets every component, but the property stays a vector
  172. const bool isVector = this->m_type == UnderlyingType::Vec2 || this->m_type == UnderlyingType::Vec3
  173. || this->m_type == UnderlyingType::Vec4;
  174. const bool isScalar = other.getType () == UnderlyingType::Float || other.getType () == UnderlyingType::Int;
  175. const auto keptType = this->m_type;
  176. this->m_vec2 = other.getVec2 ();
  177. this->m_vec3 = other.getVec3 ();
  178. this->m_vec4 = other.getVec4 ();
  179. this->m_float = other.getFloat ();
  180. this->m_int = other.getInt ();
  181. this->m_bool = other.getBool ();
  182. this->m_string = other.getString ();
  183. this->m_type = isVector && isScalar ? keptType : other.getType ();
  184. if (this->m_condition.has_value () && other.getType () == UnderlyingType::String) {
  185. const bool boolValue = this->m_condition.value ().condition == other.getString ();
  186. this->m_vec2 = glm::vec2 (boolValue ? 1.0f : 0.0f);
  187. this->m_vec3 = glm::vec3 (boolValue ? 1.0f : 0.0f);
  188. this->m_vec4 = glm::vec4 (boolValue ? 1.0f : 0.0f);
  189. this->m_float = boolValue ? 1.0f : 0.0f;
  190. this->m_int = boolValue ? 1 : 0;
  191. this->m_bool = boolValue;
  192. this->m_type = UnderlyingType::Boolean;
  193. }
  194. this->propagate (source);
  195. }
  196. void DynamicValue::update (UpdateSource source) {
  197. this->m_vec2 = glm::vec2 (0.0f);
  198. this->m_vec3 = glm::vec3 (0.0f);
  199. this->m_vec4 = glm::vec4 (0.0f);
  200. this->m_float = 0.0f;
  201. this->m_int = 0;
  202. this->m_bool = false;
  203. this->m_type = UnderlyingType::Null;
  204. this->propagate (source);
  205. }
  206. std::function<void ()> DynamicValue::listen (const std::function<void (const DynamicValue&, UpdateSource)>& callback) {
  207. const auto it = this->m_listeners.insert (this->m_listeners.end (), callback);
  208. auto alive = this->m_aliveFlag;
  209. return [this, it, alive] {
  210. if (!alive || !*alive) {
  211. return;
  212. }
  213. this->m_listeners.erase (it);
  214. };
  215. }
  216. void DynamicValue::connect (DynamicValue* other) {
  217. const auto lambda = [this] (const DynamicValue& other, UpdateSource source) {
  218. // null needs the no-arg update() - copying everything to 0 is a different meaning
  219. if (other.getType () == UnderlyingType::Null) {
  220. this->update (source);
  221. } else {
  222. this->update (other, source);
  223. }
  224. };
  225. const auto deregisterFunction = other->listen (lambda);
  226. // trigger the same update cycle immediately for the initial value
  227. lambda (*other, UpdateSource::Initialization);
  228. this->m_connections.push_back (deregisterFunction);
  229. }
  230. void DynamicValue::disconnect () {
  231. for (const auto& deregister : this->m_connections) {
  232. if (!deregister) {
  233. continue;
  234. }
  235. try {
  236. deregister ();
  237. } catch (...) {
  238. sLog.error ("Exception during listener deregistration");
  239. }
  240. }
  241. this->m_connections.clear ();
  242. }
  243. void DynamicValue::attachCondition (const ConditionInfo& condition) { this->m_condition = condition; }
  244. void DynamicValue::setScriptSource (const std::string& source) { this->m_scriptSource = source; }
  245. void DynamicValue::clearScriptSource () { this->m_scriptSource = std::nullopt; }
  246. const std::optional<std::string>& DynamicValue::getScriptSource () const { return this->m_scriptSource; }
  247. void DynamicValue::setAnimation (std::shared_ptr<const PropertyAnimation> animation) {
  248. this->m_animation = std::move (animation);
  249. }
  250. const std::shared_ptr<const PropertyAnimation>& DynamicValue::getAnimation () const { return this->m_animation; }
  251. std::map<std::string, UserSettingUniquePtr>& DynamicValue::getProperties () { return this->m_properties; }
  252. void DynamicValue::setProperties (std::map<std::string, UserSettingUniquePtr> properties) {
  253. this->m_properties = std::move (properties);
  254. }
  255. void DynamicValue::propagate (UpdateSource source) const {
  256. for (const auto& callback : this->m_listeners) {
  257. callback (*this, source);
  258. }
  259. }