ScriptableObject.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "ScriptableObject.h"
  2. #include "ScriptEngine.h"
  3. #include "WallpaperEngine/Data/Model/Effect.h"
  4. #include "WallpaperEngine/Data/Model/Object.h"
  5. #include "WallpaperEngine/Data/Utils/ScopeGuard.h"
  6. #include <cstdint>
  7. #include <ranges>
  8. using namespace WallpaperEngine::Render;
  9. using namespace WallpaperEngine::Scripting;
  10. ScriptableObject::ScriptableObject (Wallpapers::CScene& scene, const Object& object) : CObject (scene, object) {
  11. this->registerProperty ("origin", *object.origin->value);
  12. this->registerProperty ("scale", *object.groupScale->value);
  13. this->registerProperty ("angles", *object.groupAngles->value);
  14. this->registerProperty ("visible", *object.groupVisible->value);
  15. this->registerProperty ("solid", *object.solid->value);
  16. this->registerProperty ("disablepropagation", *object.disablePropagation->value);
  17. }
  18. DynamicValue& ScriptableObject::getProperty (const std::string& name) {
  19. const auto it = this->m_properties.find (name);
  20. if (it == this->m_properties.end ()) {
  21. sLog.exception ("Property '" + name + "' not found on object '" + this->getObject ().name + "'");
  22. }
  23. return it->second.value;
  24. }
  25. DynamicValue* ScriptableObject::tryGetProperty (const std::string& name) {
  26. const auto it = this->m_properties.find (name);
  27. return it == this->m_properties.end () ? nullptr : &it->second.value;
  28. }
  29. const std::map<std::string, ScriptableObject::PropertyEntry>& ScriptableObject::getProperties () const {
  30. return this->m_properties;
  31. }
  32. void ScriptableObject::registerEffectConstants (const std::vector<ImageEffectUniquePtr>& effects) {
  33. for (size_t effectIndex = 0; effectIndex < effects.size (); effectIndex++) {
  34. const auto& passes = effects[effectIndex]->passOverrides;
  35. for (size_t passIndex = 0; passIndex < passes.size (); passIndex++) {
  36. const std::string prefix = "fx" + std::to_string (effectIndex) + ".p" + std::to_string (passIndex) + ".";
  37. for (const auto& [constant, setting] : passes[passIndex]->constants) {
  38. if (!setting->value->getScriptSource ().has_value () && setting->value->getAnimation () == nullptr) {
  39. continue;
  40. }
  41. this->registerProperty (
  42. prefix + constant, *setting->value, "obj" + std::to_string (this->getId ()) + "/" + prefix, constant
  43. );
  44. }
  45. }
  46. }
  47. }
  48. void ScriptableObject::registerProperty (
  49. const std::string& name, DynamicValue& value, const std::string& animationGroup, const std::string& animationKey
  50. ) {
  51. if (const auto existing = this->m_properties.find (name); existing != this->m_properties.end ()) {
  52. if (&existing->second.value == &value) {
  53. return;
  54. }
  55. this->getScene ().getScriptEngine ().getAnimations ().remove (existing->second.value);
  56. // A derived class's own field (e.g. CImage's "scale") is overriding the generic
  57. // groupScale/groupAngles/groupVisible fallback registered by the base constructor under the
  58. // the base and derived classes parse the same JSON key, so this can be a redundant queue of the same script;
  59. // rebind the running module in place when identical so init() does not run twice
  60. if (this->getScene ().getScriptEngine ().rebindScript (existing->second.key, value)) {
  61. const std::string key = existing->second.key;
  62. this->m_properties.erase (existing);
  63. this->m_properties.emplace (name, PropertyEntry { .key = key, .value = value });
  64. this->getScene ().getScriptEngine ().getAnimations ().add (
  65. animationGroup.empty () ? "obj" + std::to_string (this->getId ()) : animationGroup,
  66. animationKey.empty () ? name : animationKey, value
  67. );
  68. return;
  69. }
  70. // different script, nothing to rebind - retire the stale lookup
  71. this->getScene ().getScriptEngine ().retireScript (existing->second.key);
  72. this->m_properties.erase (existing);
  73. }
  74. // Includes the DynamicValue's own address so two different registrations under the same
  75. // name (see above) never end up sharing a script engine key/module filename.
  76. const std::string key
  77. = name + "_" + std::to_string (this->getId ()) + "_" + std::to_string (reinterpret_cast<uintptr_t> (&value));
  78. const auto inserted = this->m_properties.emplace (name, PropertyEntry { .key = key, .value = value });
  79. this->getScene ().getScriptEngine ().queueScript (
  80. inserted.first->second.key, inserted.first->second.value, *this, animationKey.empty () ? name : animationKey
  81. );
  82. this->getScene ().getScriptEngine ().getAnimations ().add (
  83. animationGroup.empty () ? "obj" + std::to_string (this->getId ()) : animationGroup,
  84. animationKey.empty () ? name : animationKey, value
  85. );
  86. }