DynamicValue.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #pragma once
  2. #include "Color.h"
  3. #include "Types.h"
  4. #include <functional>
  5. #include <glm/glm.hpp>
  6. #include <list>
  7. #include <map>
  8. #include <memory>
  9. #include <optional>
  10. #include <string>
  11. namespace WallpaperEngine::Data::Model {
  12. class DynamicValue;
  13. struct ConditionInfo {
  14. std::string name;
  15. std::string condition;
  16. };
  17. struct ScriptContext {
  18. struct Object {
  19. int id;
  20. std::string name;
  21. } object;
  22. };
  23. /**
  24. * Class that represents different types of dynamic values
  25. */
  26. class DynamicValue {
  27. public:
  28. enum UnderlyingType {
  29. Null = 0,
  30. Vec4 = 4,
  31. Vec3 = 5,
  32. Vec2 = 6,
  33. Float = 7,
  34. Int = 8,
  35. Boolean = 9,
  36. String = 10,
  37. };
  38. enum UpdateSource {
  39. Initialization = 0,
  40. Script = 1,
  41. User = 2,
  42. };
  43. DynamicValue () = default;
  44. DynamicValue (const DynamicValue& other);
  45. explicit DynamicValue (const glm::vec4& value);
  46. explicit DynamicValue (const glm::vec3& value);
  47. explicit DynamicValue (const glm::vec2& value);
  48. explicit DynamicValue (float value);
  49. explicit DynamicValue (int value);
  50. explicit DynamicValue (bool value);
  51. explicit DynamicValue (const std::string& value);
  52. explicit DynamicValue (const Model::Color& value);
  53. virtual ~DynamicValue ();
  54. [[nodiscard]] const glm::vec4& getVec4 () const;
  55. [[nodiscard]] const glm::vec3& getVec3 () const;
  56. [[nodiscard]] const glm::vec2& getVec2 () const;
  57. [[nodiscard]] const float& getFloat () const;
  58. [[nodiscard]] const int& getInt () const;
  59. [[nodiscard]] const bool& getBool () const;
  60. [[nodiscard]] const std::string& getString () const;
  61. [[nodiscard]] UnderlyingType getType () const;
  62. [[nodiscard]] virtual std::string toString () const;
  63. virtual void update (float newValue, UpdateSource source);
  64. virtual void update (int newValue, UpdateSource source);
  65. virtual void update (bool newValue, UpdateSource source);
  66. virtual void update (const glm::vec2& newValue, UpdateSource source);
  67. virtual void update (const glm::vec3& newValue, UpdateSource source);
  68. virtual void update (const glm::vec4& newValue, UpdateSource source);
  69. virtual void update (const std::string& newValue, UpdateSource source);
  70. virtual void update (const Model::Color& newValue, UpdateSource source);
  71. virtual void update (const DynamicValue& other, UpdateSource source);
  72. /**
  73. * Sets the current value to null
  74. */
  75. virtual void update (UpdateSource source);
  76. /**
  77. * Registers the given callback to be called when the value changes
  78. *
  79. * @param callback
  80. *
  81. * @return The de-register function to call when the listener is no longer needed
  82. */
  83. std::function<void ()> listen (const std::function<void (const DynamicValue&, UpdateSource)>& callback);
  84. /**
  85. * Connects the current instance to the given instance, updating this instance's value
  86. * based on the given instance's value
  87. *
  88. * @param other
  89. *
  90. * @return The de-register function to call when the listener is no longer needed
  91. */
  92. void connect (DynamicValue* other);
  93. /**
  94. * Disconnects the current instance from all the connections to stop notifying
  95. * new value changes
  96. */
  97. void disconnect ();
  98. /**
  99. * Associates a condition with the dynamic value to apply proper checks
  100. *
  101. * @param condition
  102. */
  103. void attachCondition (const ConditionInfo& condition);
  104. /**
  105. * Associates a script source with the dynamic value to allow for dynamic updates
  106. *
  107. * @param source The script code to associate to this value
  108. */
  109. void setScriptSource (const std::string& source);
  110. /**
  111. * Clears the associated script to this dynamic value
  112. */
  113. void clearScriptSource ();
  114. /**
  115. * @return The current script source associated with this dynamic value
  116. */
  117. [[nodiscard]] const std::optional<std::string>& getScriptSource () const;
  118. /**
  119. * @return The script properties associated with this dynamic value (if any)
  120. */
  121. [[nodiscard]] std::map<std::string, UserSettingUniquePtr>& getProperties ();
  122. /**
  123. * Updates the script properties associated with this dynamic value
  124. */
  125. void setProperties (std::map<std::string, UserSettingUniquePtr> properties);
  126. private:
  127. /**
  128. * Notifies any listeners that the value has changed
  129. */
  130. void propagate (UpdateSource source) const;
  131. std::shared_ptr<bool> m_aliveFlag = std::make_shared<bool> (true);
  132. std::list<std::function<void (const DynamicValue&, UpdateSource)>> m_listeners = {};
  133. std::vector<std::function<void ()>> m_connections = {};
  134. std::optional<std::string> m_scriptSource = std::nullopt;
  135. glm::vec4 m_vec4 = {};
  136. glm::vec3 m_vec3 = {};
  137. glm::vec2 m_vec2 = {};
  138. float m_float = 0.0f;
  139. int m_int = 0;
  140. bool m_bool = false;
  141. std::string m_string;
  142. UnderlyingType m_type = Null;
  143. std::optional<ConditionInfo> m_condition = std::nullopt;
  144. /** All the properties this script takes in */
  145. std::map<std::string, UserSettingUniquePtr> m_properties;
  146. };
  147. }