| 1 | // Aseprite |
| 2 | // Copyright (C) 2019-2022 Igara Studio S.A. |
| 3 | // |
| 4 | // This program is distributed under the terms of |
| 5 | // the End-User License Agreement for Aseprite. |
| 6 | |
| 7 | #ifndef APP_COMMANDS_NEW_PARAMS_H |
| 8 | #define APP_COMMANDS_NEW_PARAMS_H |
| 9 | |
| 10 | #include "app/commands/command.h" |
| 11 | #include "app/commands/params.h" |
| 12 | |
| 13 | #include <map> |
| 14 | #include <string> |
| 15 | #include <utility> |
| 16 | |
| 17 | #ifdef ENABLE_SCRIPTING |
| 18 | extern "C" struct lua_State; |
| 19 | #endif |
| 20 | |
| 21 | namespace app { |
| 22 | |
| 23 | class ParamBase { |
| 24 | public: |
| 25 | virtual ~ParamBase() { } |
| 26 | virtual void resetValue() = 0; |
| 27 | virtual void fromString(const std::string& value) = 0; |
| 28 | #ifdef ENABLE_SCRIPTING |
| 29 | virtual void fromLua(lua_State* L, int index) = 0; |
| 30 | #endif |
| 31 | }; |
| 32 | |
| 33 | class NewParams { |
| 34 | public: |
| 35 | void addParam(const char* id, ParamBase* param) { |
| 36 | m_params[id] = param; |
| 37 | } |
| 38 | |
| 39 | void resetValues() { |
| 40 | for (auto& pair : m_params) |
| 41 | pair.second->resetValue(); |
| 42 | } |
| 43 | |
| 44 | ParamBase* getParam(const std::string& id) { |
| 45 | auto it = m_params.find(id); |
| 46 | if (it != m_params.end()) |
| 47 | return it->second; |
| 48 | else |
| 49 | return nullptr; |
| 50 | } |
| 51 | |
| 52 | private: |
| 53 | std::map<std::string, ParamBase*> m_params; |
| 54 | }; |
| 55 | |
| 56 | template<typename T> |
| 57 | class Param : public ParamBase { |
| 58 | public: |
| 59 | Param(NewParams* params, |
| 60 | const T& defaultValue, |
| 61 | const char* id) |
| 62 | : m_defaultValue(defaultValue) |
| 63 | , m_value(defaultValue) { |
| 64 | if (id) |
| 65 | params->addParam(id, this); |
| 66 | } |
| 67 | |
| 68 | Param(NewParams* params, |
| 69 | const T& defaultValue, |
| 70 | const std::initializer_list<const char*> ids) |
| 71 | : Param(params, defaultValue, nullptr) { |
| 72 | for (const auto& id : ids) |
| 73 | params->addParam(id, this); |
| 74 | } |
| 75 | |
| 76 | bool isSet() const { |
| 77 | return m_isSet; |
| 78 | } |
| 79 | |
| 80 | void resetValue() override { |
| 81 | m_value = m_defaultValue; |
| 82 | m_isSet = false; |
| 83 | } |
| 84 | |
| 85 | void fromString(const std::string& value) override; |
| 86 | #ifdef ENABLE_SCRIPTING |
| 87 | void fromLua(lua_State* L, int index) override; |
| 88 | #endif |
| 89 | |
| 90 | const T& operator()() const { |
| 91 | return m_value; |
| 92 | } |
| 93 | |
| 94 | T& operator()(const T& value) { |
| 95 | m_value = value; |
| 96 | m_isSet = true; |
| 97 | return m_value; |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | void setValue(const T& value) { |
| 102 | operator()(value); |
| 103 | } |
| 104 | |
| 105 | T m_defaultValue; |
| 106 | T m_value; |
| 107 | bool m_isSet = false; |
| 108 | }; |
| 109 | |
| 110 | class CommandWithNewParamsBase : public Command { |
| 111 | public: |
| 112 | template<typename...Args> |
| 113 | CommandWithNewParamsBase(Args&&...args) |
| 114 | : Command(std::forward<Args>(args)...) { } |
| 115 | |
| 116 | #ifdef ENABLE_SCRIPTING |
| 117 | void loadParamsFromLuaTable(lua_State* L, int index); |
| 118 | #endif |
| 119 | |
| 120 | protected: |
| 121 | void onLoadParams(const Params& params) override; |
| 122 | |
| 123 | virtual void onResetValues() = 0; |
| 124 | virtual ParamBase* onGetParam(const std::string& k) = 0; |
| 125 | |
| 126 | private: |
| 127 | #ifdef ENABLE_SCRIPTING |
| 128 | bool m_skipLoadParams = false; |
| 129 | #endif |
| 130 | }; |
| 131 | |
| 132 | template<typename T> |
| 133 | class CommandWithNewParams : public CommandWithNewParamsBase { |
| 134 | public: |
| 135 | template<typename...Args> |
| 136 | CommandWithNewParams(Args&&...args) |
| 137 | : CommandWithNewParamsBase(std::forward<Args>(args)...) { } |
| 138 | |
| 139 | T& params() { return m_params; } |
| 140 | const T& params() const { return m_params; } |
| 141 | |
| 142 | protected: |
| 143 | void onResetValues() override { |
| 144 | m_params.resetValues(); |
| 145 | } |
| 146 | |
| 147 | ParamBase* onGetParam(const std::string& k) override { |
| 148 | return m_params.getParam(k); |
| 149 | } |
| 150 | |
| 151 | private: |
| 152 | T m_params; |
| 153 | }; |
| 154 | |
| 155 | } // namespace app |
| 156 | |
| 157 | #endif |
| 158 | |