From b8559132510e93644cc8b4094ce3ffe586ab2319 Mon Sep 17 00:00:00 2001 From: Tobias Markus Date: Sun, 17 Sep 2023 12:42:12 +0200 Subject: [PATCH] Make object option generic (#2633) * Make object option generic --- src/editor/object_option.cpp | 188 +++++++++++++++------------------ src/editor/object_option.hpp | 98 ++++++++--------- src/editor/object_settings.cpp | 10 +- src/editor/object_settings.hpp | 6 +- src/object/path.hpp | 1 + src/object/path_walker.hpp | 1 + src/supertux/direction.hpp | 2 +- 7 files changed, 144 insertions(+), 162 deletions(-) diff --git a/src/editor/object_option.cpp b/src/editor/object_option.cpp index ace1213382b..28e360df0c3 100644 --- a/src/editor/object_option.cpp +++ b/src/editor/object_option.cpp @@ -46,22 +46,24 @@ std::string fmt_to_string(const T& v) } // namespace -ObjectOption::ObjectOption(const std::string& text, const std::string& key, unsigned int flags) : +BaseObjectOption::BaseObjectOption(const std::string& text, const std::string& key, unsigned int flags) : m_text(text), m_key(key), m_flags(flags) { } -ObjectOption::~ObjectOption() +template +ObjectOption::ObjectOption(const std::string& text, const std::string& key, unsigned int flags, T* pointer) : + BaseObjectOption(text, key, flags), + m_value_pointer(pointer) { } BoolObjectOption::BoolObjectOption(const std::string& text, bool* pointer, const std::string& key, std::optional default_value, unsigned int flags) : - ObjectOption(text, key, flags), - m_pointer(pointer), + ObjectOption(text, key, flags, pointer), m_default_value(std::move(default_value)) { } @@ -69,17 +71,17 @@ BoolObjectOption::BoolObjectOption(const std::string& text, bool* pointer, const void BoolObjectOption::add_to_menu(Menu& menu) const { - menu.add_toggle(-1, get_text(), m_pointer); + menu.add_toggle(-1, get_text(), m_value_pointer); } void BoolObjectOption::save(Writer& writer) const { if (!get_key().empty()) { - if (m_default_value && *m_default_value == *m_pointer) { + if (m_default_value && *m_default_value == *m_value_pointer) { // skip } else { - writer.write(get_key(), *m_pointer); + writer.write(get_key(), *m_value_pointer); } } } @@ -87,14 +89,13 @@ BoolObjectOption::save(Writer& writer) const std::string BoolObjectOption::to_string() const { - return *m_pointer ? _("true") : _("false"); + return *m_value_pointer ? _("true") : _("false"); } IntObjectOption::IntObjectOption(const std::string& text, int* pointer, const std::string& key, std::optional default_value, unsigned int flags) : - ObjectOption(text, key, flags), - m_pointer(pointer), + ObjectOption(text, key, flags, pointer), m_default_value(std::move(default_value)) { } @@ -103,10 +104,10 @@ void IntObjectOption::save(Writer& writer) const { if (!get_key().empty()) { - if (m_default_value && *m_default_value == *m_pointer) { + if (m_default_value && *m_default_value == *m_value_pointer) { // skip } else { - writer.write(get_key(), *m_pointer); + writer.write(get_key(), *m_value_pointer); } } } @@ -114,13 +115,13 @@ IntObjectOption::save(Writer& writer) const std::string IntObjectOption::to_string() const { - return fmt_to_string(*m_pointer); + return fmt_to_string(*m_value_pointer); } void IntObjectOption::add_to_menu(Menu& menu) const { - menu.add_intfield(get_text(), m_pointer); + menu.add_intfield(get_text(), m_value_pointer); } LabelObjectOption::LabelObjectOption(const std::string& text, @@ -148,10 +149,9 @@ LabelObjectOption::add_to_menu(Menu& menu) const RectfObjectOption::RectfObjectOption(const std::string& text, Rectf* pointer, const std::string& key, unsigned int flags) : - ObjectOption(text, key, flags), - m_pointer(pointer), - m_width(m_pointer->get_width()), - m_height(m_pointer->get_height()) + ObjectOption(text, key, flags, pointer), + m_width(m_value_pointer->get_width()), + m_height(m_value_pointer->get_height()) { } @@ -168,7 +168,7 @@ std::string RectfObjectOption::to_string() const { std::ostringstream out; - out << *m_pointer; + out << *m_value_pointer; return out.str(); } @@ -182,8 +182,7 @@ RectfObjectOption::add_to_menu(Menu& menu) const FloatObjectOption::FloatObjectOption(const std::string& text, float* pointer, const std::string& key, std::optional default_value, unsigned int flags) : - ObjectOption(text, key, flags), - m_pointer(pointer), + ObjectOption(text, key, flags, pointer), m_default_value(std::move(default_value)) { } @@ -192,10 +191,10 @@ void FloatObjectOption::save(Writer& writer) const { if (!get_key().empty()) { - if (m_default_value && *m_default_value == *m_pointer) { + if (m_default_value && *m_default_value == *m_value_pointer) { // skip } else { - writer.write(get_key(), *m_pointer); + writer.write(get_key(), *m_value_pointer); } } } @@ -203,20 +202,19 @@ FloatObjectOption::save(Writer& writer) const std::string FloatObjectOption::to_string() const { - return fmt_to_string(*m_pointer); + return fmt_to_string(*m_value_pointer); } void FloatObjectOption::add_to_menu(Menu& menu) const { - menu.add_floatfield(get_text(), m_pointer); + menu.add_floatfield(get_text(), m_value_pointer); } StringObjectOption::StringObjectOption(const std::string& text, std::string* pointer, const std::string& key, std::optional default_value, unsigned int flags) : - ObjectOption(text, key, flags), - m_pointer(pointer), + ObjectOption(text, key, flags, pointer), m_default_value(std::move(default_value)) { } @@ -225,10 +223,10 @@ void StringObjectOption::save(Writer& writer) const { if (!get_key().empty()) { - if ((m_default_value && *m_default_value == *m_pointer) || m_pointer->empty()) { + if ((m_default_value && *m_default_value == *m_value_pointer) || m_value_pointer->empty()) { // skip } else { - writer.write(get_key(), *m_pointer, (get_flags() & OPTION_TRANSLATABLE)); + writer.write(get_key(), *m_value_pointer, (get_flags() & OPTION_TRANSLATABLE)); } } } @@ -236,20 +234,19 @@ StringObjectOption::save(Writer& writer) const std::string StringObjectOption::to_string() const { - return *m_pointer; + return *m_value_pointer; } void StringObjectOption::add_to_menu(Menu& menu) const { - menu.add_textfield(get_text(), m_pointer); + menu.add_textfield(get_text(), m_value_pointer); } StringMultilineObjectOption::StringMultilineObjectOption(const std::string& text, std::string* pointer, const std::string& key, std::optional default_value, unsigned int flags) : - ObjectOption(text, key, flags), - m_pointer(pointer), + ObjectOption(text, key, flags, pointer), m_default_value(std::move(default_value)) { } @@ -258,10 +255,10 @@ void StringMultilineObjectOption::save(Writer& writer) const { if (!get_key().empty()) { - if ((m_default_value && *m_default_value == *m_pointer) || m_pointer->empty()) { + if ((m_default_value && *m_default_value == *m_value_pointer) || m_value_pointer->empty()) { // skip } else { - writer.write(get_key(), *m_pointer, (get_flags() & OPTION_TRANSLATABLE)); + writer.write(get_key(), *m_value_pointer, (get_flags() & OPTION_TRANSLATABLE)); } } } @@ -269,7 +266,7 @@ StringMultilineObjectOption::save(Writer& writer) const std::string StringMultilineObjectOption::to_string() const { - if (!m_pointer->empty()) { + if (!m_value_pointer->empty()) { return "..."; } return ""; @@ -278,15 +275,14 @@ StringMultilineObjectOption::to_string() const void StringMultilineObjectOption::add_to_menu(Menu& menu) const { - menu.add_script(get_text(), m_pointer); + menu.add_script(get_text(), m_value_pointer); } StringSelectObjectOption::StringSelectObjectOption(const std::string& text, int* pointer, const std::vector& select, std::optional default_value, const std::string& key, unsigned int flags) : - ObjectOption(text, key, flags), - m_pointer(pointer), + ObjectOption(text, key, flags, pointer), m_select(select), m_default_value(std::move(default_value)) { @@ -296,10 +292,10 @@ void StringSelectObjectOption::save(Writer& writer) const { if (!get_key().empty()) { - if (m_default_value && *m_default_value == *m_pointer) { + if (m_default_value && *m_default_value == *m_value_pointer) { // skip } else { - writer.write(get_key(), *m_pointer); + writer.write(get_key(), *m_value_pointer); } } } @@ -307,7 +303,7 @@ StringSelectObjectOption::save(Writer& writer) const std::string StringSelectObjectOption::to_string() const { - int* selected_id = static_cast(m_pointer); + int* selected_id = static_cast(m_value_pointer); if (*selected_id >= int(m_select.size()) || *selected_id < 0) { return _("invalid"); //Test whether the selected ID is valid } else { @@ -318,11 +314,11 @@ StringSelectObjectOption::to_string() const void StringSelectObjectOption::add_to_menu(Menu& menu) const { - int& selected_id = *m_pointer; + int& selected_id = *m_value_pointer; if ( selected_id >= static_cast(m_select.size()) || selected_id < 0 ) { selected_id = 0; // Set the option to zero when not selectable } - menu.add_string_select(-1, get_text(), m_pointer, m_select); + menu.add_string_select(-1, get_text(), m_value_pointer, m_select); } EnumObjectOption::EnumObjectOption(const std::string& text, int* pointer, @@ -330,8 +326,7 @@ EnumObjectOption::EnumObjectOption(const std::string& text, int* pointer, const std::vector& symbols, std::optional default_value, const std::string& key, unsigned int flags) : - ObjectOption(text, key, flags), - m_pointer(pointer), + ObjectOption(text, key, flags, pointer), m_labels(labels), m_symbols(symbols), m_default_value(std::move(default_value)) @@ -341,13 +336,13 @@ EnumObjectOption::EnumObjectOption(const std::string& text, int* pointer, void EnumObjectOption::save(Writer& writer) const { - if (0 <= *m_pointer && *m_pointer < int(m_symbols.size()) && + if (0 <= *m_value_pointer && *m_value_pointer < int(m_symbols.size()) && !get_key().empty()) { - if (m_default_value && *m_default_value == *m_pointer) { + if (m_default_value && *m_default_value == *m_value_pointer) { // skip } else { - writer.write(get_key(), m_symbols[*m_pointer]); + writer.write(get_key(), m_symbols[*m_value_pointer]); } } } @@ -355,8 +350,8 @@ EnumObjectOption::save(Writer& writer) const std::string EnumObjectOption::to_string() const { - if (0 <= *m_pointer && *m_pointer < int(m_labels.size())) { - return m_labels[*m_pointer]; + if (0 <= *m_value_pointer && *m_value_pointer < int(m_labels.size())) { + return m_labels[*m_value_pointer]; } else { return _("invalid"); } @@ -365,24 +360,23 @@ EnumObjectOption::to_string() const void EnumObjectOption::add_to_menu(Menu& menu) const { - if (*m_pointer >= static_cast(m_labels.size()) || *m_pointer < 0 ) { - *m_pointer = 0; // Set the option to zero when not selectable + if (*m_value_pointer >= static_cast(m_labels.size()) || *m_value_pointer < 0 ) { + *m_value_pointer = 0; // Set the option to zero when not selectable } - menu.add_string_select(-1, get_text(), m_pointer, m_labels); + menu.add_string_select(-1, get_text(), m_value_pointer, m_labels); } ScriptObjectOption::ScriptObjectOption(const std::string& text, std::string* pointer, const std::string& key, unsigned int flags) : - ObjectOption(text, key, flags), - m_pointer(pointer) + ObjectOption(text, key, flags, pointer) { } void ScriptObjectOption::save(Writer& writer) const { - auto& value = *m_pointer; + auto& value = *m_value_pointer; if (!value.empty()) { if (!get_key().empty()) { @@ -394,7 +388,7 @@ ScriptObjectOption::save(Writer& writer) const std::string ScriptObjectOption::to_string() const { - if (!m_pointer->empty()) { + if (!m_value_pointer->empty()) { return "..."; } return ""; @@ -403,7 +397,7 @@ ScriptObjectOption::to_string() const void ScriptObjectOption::add_to_menu(Menu& menu) const { - menu.add_script(get_text(), m_pointer); + menu.add_script(get_text(), m_value_pointer); } FileObjectOption::FileObjectOption(const std::string& text, std::string* pointer, @@ -413,8 +407,7 @@ FileObjectOption::FileObjectOption(const std::string& text, std::string* pointer const std::string& basedir, bool path_relative_to_basedir, unsigned int flags) : - ObjectOption(text, key, flags), - m_pointer(pointer), + ObjectOption(text, key, flags, pointer), m_default_value(std::move(default_value)), m_filter(std::move(filter)), m_basedir(basedir), @@ -425,10 +418,10 @@ FileObjectOption::FileObjectOption(const std::string& text, std::string* pointer void FileObjectOption::save(Writer& writer) const { - if (m_default_value && *m_default_value == *m_pointer) { + if (m_default_value && *m_default_value == *m_value_pointer) { // skip } else { - auto& value = *m_pointer; + auto& value = *m_value_pointer; if (!value.empty()) { if (!get_key().empty()) { @@ -441,20 +434,19 @@ FileObjectOption::save(Writer& writer) const std::string FileObjectOption::to_string() const { - return *m_pointer; + return *m_value_pointer; } void FileObjectOption::add_to_menu(Menu& menu) const { - menu.add_file(get_text(), m_pointer, m_filter, m_basedir, m_path_relative_to_basedir); + menu.add_file(get_text(), m_value_pointer, m_filter, m_basedir, m_path_relative_to_basedir); } ColorObjectOption::ColorObjectOption(const std::string& text, Color* pointer, const std::string& key, std::optional default_value, bool use_alpha, unsigned int flags) : - ObjectOption(text, key, flags), - m_pointer(pointer), + ObjectOption(text, key, flags, pointer), m_default_value(std::move(default_value)), m_use_alpha(use_alpha) { @@ -464,10 +456,10 @@ void ColorObjectOption::save(Writer& writer) const { if (!get_key().empty()) { - if (m_default_value && *m_default_value == *m_pointer) { + if (m_default_value && *m_default_value == *m_value_pointer) { // skip } else { - auto vec = m_pointer->toVector(); + auto vec = m_value_pointer->toVector(); if (!m_use_alpha || vec.back() == 1.0f) { vec.pop_back(); } @@ -479,19 +471,18 @@ ColorObjectOption::save(Writer& writer) const std::string ColorObjectOption::to_string() const { - return m_pointer->to_string(); + return m_value_pointer->to_string(); } void ColorObjectOption::add_to_menu(Menu& menu) const { - menu.add_color(get_text(), m_pointer); + menu.add_color(get_text(), m_value_pointer); } ObjectSelectObjectOption::ObjectSelectObjectOption(const std::string& text, std::vector>* pointer, GameObject* parent, const std::string& key, unsigned int flags) : - ObjectOption(text, key, flags), - m_pointer(pointer), + ObjectOption(text, key, flags, pointer), m_parent(parent) { } @@ -503,7 +494,7 @@ ObjectSelectObjectOption::save(Writer& writer) const return; writer.start_list(get_key()); - for (auto it = m_pointer->begin(); it != m_pointer->end(); it++) + for (auto it = m_value_pointer->begin(); it != m_value_pointer->end(); it++) { auto& obj = *it; writer.start_list(obj->get_class_name()); @@ -516,13 +507,13 @@ ObjectSelectObjectOption::save(Writer& writer) const std::string ObjectSelectObjectOption::to_string() const { - return fmt_to_string(m_pointer->size()); + return fmt_to_string(m_value_pointer->size()); } void ObjectSelectObjectOption::add_to_menu(Menu& menu) const { - menu.add_entry(get_text(), [pointer = m_pointer, parent = m_parent]() { + menu.add_entry(get_text(), [pointer = m_value_pointer, parent = m_parent]() { MenuManager::instance().push_menu(std::make_unique(*pointer, parent)); }); } @@ -555,15 +546,14 @@ TilesObjectOption::add_to_menu(Menu& menu) const PathObjectOption::PathObjectOption(const std::string& text, Path* path, const std::string& key, unsigned int flags) : - ObjectOption(text, key, flags), - m_path(path) + ObjectOption(text, key, flags, path) { } void PathObjectOption::save(Writer& write) const { - m_path->save(write); + m_value_pointer->save(write); } std::string @@ -579,9 +569,8 @@ PathObjectOption::add_to_menu(Menu& menu) const PathRefObjectOption::PathRefObjectOption(const std::string& text, PathObject& target, const std::string& path_ref, const std::string& key, unsigned int flags) : - ObjectOption(text, key, flags), - m_path_ref(path_ref), - m_target(target) + ObjectOption(text, key, flags, &target), + m_path_ref(path_ref) { } @@ -602,28 +591,27 @@ PathRefObjectOption::to_string() const void PathRefObjectOption::add_to_menu(Menu& menu) const { - menu.add_path_settings(m_text, m_target, m_path_ref); + menu.add_path_settings(m_text, *m_value_pointer, m_path_ref); } SExpObjectOption::SExpObjectOption(const std::string& text, const std::string& key, sexp::Value& value, unsigned int flags) : - ObjectOption(text, key, flags), - m_sx(value) + ObjectOption(text, key, flags, &value) { } void SExpObjectOption::save(Writer& writer) const { - if (!m_sx.is_nil()) { - writer.write(get_key(), m_sx); + if (!m_value_pointer->is_nil()) { + writer.write(get_key(), *m_value_pointer); } } std::string SExpObjectOption::to_string() const { - return m_sx.str(); + return m_value_pointer->str(); } void @@ -632,7 +620,7 @@ SExpObjectOption::add_to_menu(Menu& menu) const } PathHandleOption::PathHandleOption(const std::string& text, PathWalker::Handle& handle, - const std::string& key, unsigned int flags) : + const std::string& key, unsigned int flags) : ObjectOption(text, key, flags), m_target(handle) { @@ -755,28 +743,26 @@ StringArrayOption::add_to_menu(Menu& menu) const } ListOption::ListOption(const std::string& text, const std::string& key, const std::vector& items, std::string* value_ptr) : - ObjectOption(text, key, 0), - m_items(items), - m_value_ptr(value_ptr) + ObjectOption(text, key, 0, value_ptr), + m_items(items) {} void ListOption::save(Writer& writer) const { - writer.write(get_key(), *m_value_ptr); + writer.write(get_key(), *m_value_pointer); } void ListOption::add_to_menu(Menu& menu) const { - menu.add_list(get_text(), m_items, m_value_ptr); + menu.add_list(get_text(), m_items, m_value_pointer); } DirectionOption::DirectionOption(const std::string& text, Direction* value_ptr, std::vector possible_directions, const std::string& key, unsigned int flags) : - ObjectOption(text, key, flags), - m_value_ptr(value_ptr), + ObjectOption(text, key, flags, value_ptr), m_possible_directions(std::move(possible_directions)) { if (m_possible_directions.empty()) @@ -787,16 +773,16 @@ DirectionOption::DirectionOption(const std::string& text, Direction* value_ptr, void DirectionOption::save(Writer& writer) const { - if (*m_value_ptr == m_possible_directions.at(0)) + if (*m_value_pointer == m_possible_directions.at(0)) return; - writer.write(get_key(), dir_to_string(*m_value_ptr)); + writer.write(get_key(), dir_to_string(*m_value_pointer)); } std::string DirectionOption::to_string() const { - return dir_to_translated_string(*m_value_ptr); + return dir_to_translated_string(*m_value_pointer); } void @@ -809,12 +795,12 @@ DirectionOption::add_to_menu(Menu& menu) const const auto& dir = m_possible_directions.at(i); labels.push_back(dir_to_translated_string(dir)); - if (dir == *m_value_ptr) + if (dir == *m_value_pointer) selected = static_cast(i); } menu.add_string_select(-1, get_text(), selected, labels) - .set_callback([value_ptr = m_value_ptr, possible_directions = m_possible_directions](int index) { + .set_callback([value_ptr = m_value_pointer, possible_directions = m_possible_directions](int index) { *value_ptr = possible_directions.at(index); }); } diff --git a/src/editor/object_option.hpp b/src/editor/object_option.hpp index 756163b357b..b481f78350f 100644 --- a/src/editor/object_option.hpp +++ b/src/editor/object_option.hpp @@ -49,11 +49,11 @@ class Rectf; class TileMap; class Writer; -class ObjectOption +class BaseObjectOption { public: - ObjectOption(const std::string& text, const std::string& key, unsigned int flags); - virtual ~ObjectOption(); + BaseObjectOption(const std::string& text, const std::string& key, unsigned int flags); + virtual ~BaseObjectOption() = default; virtual void save(Writer& write) const = 0; virtual std::string to_string() const = 0; @@ -68,12 +68,29 @@ class ObjectOption const std::string m_key; const unsigned int m_flags; +private: + BaseObjectOption(const BaseObjectOption&) = delete; + BaseObjectOption& operator=(const BaseObjectOption&) = delete; +}; + +template +class ObjectOption: public BaseObjectOption +{ +public: + ObjectOption(const std::string& text, const std::string& key, unsigned int flags, T* pointer = nullptr); + virtual ~ObjectOption() override = default; + + virtual T* get_value() const { return m_value_pointer; } + +protected: + T* const m_value_pointer; + private: ObjectOption(const ObjectOption&) = delete; ObjectOption& operator=(const ObjectOption&) = delete; }; -class BoolObjectOption : public ObjectOption +class BoolObjectOption final : public ObjectOption { public: BoolObjectOption(const std::string& text, bool* pointer, const std::string& key, @@ -85,7 +102,6 @@ class BoolObjectOption : public ObjectOption virtual void add_to_menu(Menu& menu) const override; private: - bool* const m_pointer; const std::optional m_default_value; private: @@ -93,7 +109,7 @@ class BoolObjectOption : public ObjectOption BoolObjectOption& operator=(const BoolObjectOption&) = delete; }; -class IntObjectOption : public ObjectOption +class IntObjectOption final : public ObjectOption { public: IntObjectOption(const std::string& text, int* pointer, const std::string& key, @@ -105,7 +121,6 @@ class IntObjectOption : public ObjectOption virtual void add_to_menu(Menu& menu) const override; private: - int* const m_pointer; const std::optional m_default_value; private: @@ -113,7 +128,7 @@ class IntObjectOption : public ObjectOption IntObjectOption& operator=(const IntObjectOption&) = delete; }; -class LabelObjectOption : public ObjectOption +class LabelObjectOption final : public ObjectOption<> { public: LabelObjectOption(const std::string& text, @@ -128,7 +143,7 @@ class LabelObjectOption : public ObjectOption LabelObjectOption& operator=(const LabelObjectOption&) = delete; }; -class RectfObjectOption : public ObjectOption +class RectfObjectOption final : public ObjectOption { public: RectfObjectOption(const std::string& text, Rectf* pointer, const std::string& key, @@ -139,7 +154,6 @@ class RectfObjectOption : public ObjectOption virtual void add_to_menu(Menu& menu) const override; private: - Rectf* const m_pointer; float m_width; float m_height; @@ -148,7 +162,7 @@ class RectfObjectOption : public ObjectOption RectfObjectOption& operator=(const RectfObjectOption&) = delete; }; -class FloatObjectOption : public ObjectOption +class FloatObjectOption final : public ObjectOption { public: FloatObjectOption(const std::string& text, float* pointer, const std::string& key, @@ -160,7 +174,6 @@ class FloatObjectOption : public ObjectOption virtual void add_to_menu(Menu& menu) const override; private: - float* const m_pointer; const std::optional m_default_value; private: @@ -168,7 +181,7 @@ class FloatObjectOption : public ObjectOption FloatObjectOption& operator=(const FloatObjectOption&) = delete; }; -class StringObjectOption : public ObjectOption +class StringObjectOption final : public ObjectOption { public: StringObjectOption(const std::string& text, std::string* pointer, const std::string& key, @@ -180,7 +193,6 @@ class StringObjectOption : public ObjectOption virtual void add_to_menu(Menu& menu) const override; private: - std::string* const m_pointer; std::optional m_default_value; private: @@ -188,7 +200,7 @@ class StringObjectOption : public ObjectOption StringObjectOption& operator=(const StringObjectOption&) = delete; }; -class StringMultilineObjectOption : public ObjectOption +class StringMultilineObjectOption final : public ObjectOption { public: StringMultilineObjectOption(const std::string& text, std::string* pointer, const std::string& key, @@ -200,7 +212,6 @@ class StringMultilineObjectOption : public ObjectOption virtual void add_to_menu(Menu& menu) const override; private: - std::string* const m_pointer; std::optional m_default_value; private: @@ -208,7 +219,7 @@ class StringMultilineObjectOption : public ObjectOption StringMultilineObjectOption& operator=(const StringMultilineObjectOption&) = delete; }; -class StringSelectObjectOption : public ObjectOption +class StringSelectObjectOption final : public ObjectOption { public: StringSelectObjectOption(const std::string& text, int* pointer, const std::vector& select, @@ -220,7 +231,6 @@ class StringSelectObjectOption : public ObjectOption virtual void add_to_menu(Menu& menu) const override; private: - int* const m_pointer; const std::vector m_select; const std::optional m_default_value; @@ -229,7 +239,7 @@ class StringSelectObjectOption : public ObjectOption StringSelectObjectOption& operator=(const StringSelectObjectOption&) = delete; }; -class EnumObjectOption : public ObjectOption +class EnumObjectOption final : public ObjectOption { public: EnumObjectOption(const std::string& text, int* pointer, @@ -243,7 +253,6 @@ class EnumObjectOption : public ObjectOption virtual void add_to_menu(Menu& menu) const override; private: - int* const m_pointer; const std::vector m_labels; const std::vector m_symbols; const std::optional m_default_value; @@ -253,7 +262,7 @@ class EnumObjectOption : public ObjectOption EnumObjectOption& operator=(const EnumObjectOption&) = delete; }; -class ScriptObjectOption : public ObjectOption +class ScriptObjectOption final : public ObjectOption { public: ScriptObjectOption(const std::string& text, std::string* pointer, const std::string& key, @@ -263,15 +272,12 @@ class ScriptObjectOption : public ObjectOption virtual std::string to_string() const override; virtual void add_to_menu(Menu& menu) const override; -private: - std::string* const m_pointer; - private: ScriptObjectOption(const ScriptObjectOption&) = delete; ScriptObjectOption& operator=(const ScriptObjectOption&) = delete; }; -class FileObjectOption : public ObjectOption +class FileObjectOption final : public ObjectOption { public: FileObjectOption(const std::string& text, std::string* pointer, @@ -287,7 +293,6 @@ class FileObjectOption : public ObjectOption virtual void add_to_menu(Menu& menu) const override; private: - std::string* const m_pointer; std::optional m_default_value; const std::vector m_filter; std::string m_basedir; @@ -298,7 +303,7 @@ class FileObjectOption : public ObjectOption FileObjectOption& operator=(const FileObjectOption&) = delete; }; -class ColorObjectOption : public ObjectOption +class ColorObjectOption final : public ObjectOption { public: ColorObjectOption(const std::string& text, Color* pointer, const std::string& key, @@ -310,7 +315,6 @@ class ColorObjectOption : public ObjectOption virtual void add_to_menu(Menu& menu) const override; private: - Color* const m_pointer; const std::optional m_default_value; bool m_use_alpha; @@ -319,7 +323,7 @@ class ColorObjectOption : public ObjectOption ColorObjectOption& operator=(const ColorObjectOption&) = delete; }; -class ObjectSelectObjectOption : public ObjectOption +class ObjectSelectObjectOption final : public ObjectOption>> { public: ObjectSelectObjectOption(const std::string& text, std::vector>* pointer, @@ -330,7 +334,6 @@ class ObjectSelectObjectOption : public ObjectOption virtual void add_to_menu(Menu& menu) const override; private: - std::vector>* const m_pointer; GameObject* m_parent; private: @@ -338,7 +341,7 @@ class ObjectSelectObjectOption : public ObjectOption ObjectSelectObjectOption& operator=(const ObjectSelectObjectOption&) = delete; }; -class TilesObjectOption : public ObjectOption +class TilesObjectOption final : public ObjectOption { public: TilesObjectOption(const std::string& text, TileMap* tilemap, const std::string& key, @@ -356,7 +359,7 @@ class TilesObjectOption : public ObjectOption TilesObjectOption& operator=(const TilesObjectOption&) = delete; }; -class PathObjectOption : public ObjectOption +class PathObjectOption final : public ObjectOption { public: PathObjectOption(const std::string& text, Path* path, const std::string& key, @@ -366,15 +369,12 @@ class PathObjectOption : public ObjectOption virtual std::string to_string() const override; virtual void add_to_menu(Menu& menu) const override; -private: - Path* m_path; - private: PathObjectOption(const PathObjectOption&) = delete; PathObjectOption& operator=(const PathObjectOption&) = delete; }; -class PathRefObjectOption : public ObjectOption +class PathRefObjectOption final : public ObjectOption { public: PathRefObjectOption(const std::string& text, PathObject& target, const std::string& path_ref, @@ -386,14 +386,13 @@ class PathRefObjectOption : public ObjectOption private: std::string m_path_ref; - PathObject& m_target; private: PathRefObjectOption(const PathRefObjectOption&) = delete; PathRefObjectOption& operator=(const PathRefObjectOption&) = delete; }; -class SExpObjectOption : public ObjectOption +class SExpObjectOption final : public ObjectOption { public: SExpObjectOption(const std::string& text, const std::string& key, sexp::Value& value, unsigned int flags); @@ -402,15 +401,12 @@ class SExpObjectOption : public ObjectOption virtual std::string to_string() const override; virtual void add_to_menu(Menu& menu) const override; -private: - sexp::Value m_sx; - private: SExpObjectOption(const SExpObjectOption&) = delete; SExpObjectOption& operator=(const SExpObjectOption&) = delete; }; -class PathHandleOption : public ObjectOption +class PathHandleOption final : public ObjectOption { public: PathHandleOption(const std::string& text, PathWalker::Handle& handle, @@ -428,7 +424,7 @@ class PathHandleOption : public ObjectOption PathHandleOption& operator=(const PathHandleOption&) = delete; }; -class RemoveObjectOption : public ObjectOption +class RemoveObjectOption final : public ObjectOption<> { public: RemoveObjectOption(); @@ -442,7 +438,7 @@ class RemoveObjectOption : public ObjectOption RemoveObjectOption& operator=(const RemoveObjectOption&) = delete; }; -class TestFromHereOption : public ObjectOption +class TestFromHereOption final : public ObjectOption<> { public: TestFromHereOption(); @@ -456,7 +452,7 @@ class TestFromHereOption : public ObjectOption TestFromHereOption& operator=(const TestFromHereOption&) = delete; }; -class ParticleEditorOption : public ObjectOption +class ParticleEditorOption final : public ObjectOption<> { public: ParticleEditorOption(); @@ -470,7 +466,7 @@ class ParticleEditorOption : public ObjectOption ParticleEditorOption& operator=(const ParticleEditorOption&) = delete; }; -class ButtonOption : public ObjectOption +class ButtonOption final : public ObjectOption<> { public: ButtonOption(const std::string& text, std::function callback); @@ -487,7 +483,7 @@ class ButtonOption : public ObjectOption ButtonOption& operator=(const ButtonOption&) = delete; }; -class StringArrayOption : public ObjectOption +class StringArrayOption final : public ObjectOption<> { public: StringArrayOption(const std::string& text, const std::string& key, std::vector& items); @@ -504,25 +500,24 @@ class StringArrayOption : public ObjectOption StringArrayOption& operator=(const StringArrayOption&) = delete; }; -class ListOption : public ObjectOption +class ListOption final : public ObjectOption { public: ListOption(const std::string& text, const std::string& key, const std::vector& items, std::string* value_ptr); virtual void save(Writer& write) const override; - virtual std::string to_string() const override { return *m_value_ptr; } + virtual std::string to_string() const override { return *m_value_pointer; } virtual void add_to_menu(Menu& menu) const override; private: const std::vector& m_items; - std::string* m_value_ptr; private: ListOption(const ListOption&) = delete; ListOption& operator=(const ListOption&) = delete; }; -class DirectionOption : public ObjectOption +class DirectionOption final : public ObjectOption { public: DirectionOption(const std::string& text, Direction* value_ptr, @@ -534,7 +529,6 @@ class DirectionOption : public ObjectOption virtual void add_to_menu(Menu& menu) const override; private: - Direction* m_value_ptr; std::vector m_possible_directions; private: diff --git a/src/editor/object_settings.cpp b/src/editor/object_settings.cpp index 73a8f1462a4..24b7fa19a1d 100644 --- a/src/editor/object_settings.cpp +++ b/src/editor/object_settings.cpp @@ -29,7 +29,7 @@ ObjectSettings::ObjectSettings(const std::string& name) : } void -ObjectSettings::add_option(std::unique_ptr option) +ObjectSettings::add_option(std::unique_ptr option) { m_options.push_back(std::move(option)); } @@ -245,7 +245,7 @@ ObjectSettings::add_path_ref(const std::string& text, PathObject& target, const if (!path_ref.empty()) { m_options.erase(std::remove_if(m_options.begin(), m_options.end(), - [](const std::unique_ptr& obj) { + [](const std::unique_ptr& obj) { return obj->get_key() == "x" || obj->get_key() == "y"; }), m_options.end()); @@ -352,7 +352,7 @@ ObjectSettings::add_list(const std::string& text, const std::string& key, const void ObjectSettings::reorder(const std::vector& order) { - std::vector > new_options; + std::vector > new_options; // put all items not in 'order' into 'new_options' for(auto& option : m_options) { @@ -368,7 +368,7 @@ ObjectSettings::reorder(const std::vector& order) // put all other items in 'order' into 'new_options' in the order of 'order' for(const auto& option_name : order) { auto it = std::find_if(m_options.begin(), m_options.end(), - [option_name](const std::unique_ptr& option){ + [option_name](const std::unique_ptr& option){ return option && option->get_key() == option_name; }); if (it != m_options.end()) { @@ -385,7 +385,7 @@ void ObjectSettings::remove(const std::string& key) { m_options.erase(std::remove_if(m_options.begin(), m_options.end(), - [key](const std::unique_ptr& option){ + [key](const std::unique_ptr& option){ return option->get_key() == key; }), m_options.end()); diff --git a/src/editor/object_settings.hpp b/src/editor/object_settings.hpp index b8f60845993..3742daa9a4b 100644 --- a/src/editor/object_settings.hpp +++ b/src/editor/object_settings.hpp @@ -156,7 +156,7 @@ class ObjectSettings final // VERY UNSTABLE - use with care ~ Semphris (author of that option) void add_button(const std::string& text, const std::function& callback); - const std::vector >& get_options() const { return m_options; } + const std::vector >& get_options() const { return m_options; } /** Reorder the options in the given order, this is a hack to get saving identical to the other editor */ @@ -166,11 +166,11 @@ class ObjectSettings final void remove(const std::string& key); private: - void add_option(std::unique_ptr option); + void add_option(std::unique_ptr option); private: std::string m_name; - std::vector > m_options; + std::vector > m_options; private: ObjectSettings(const ObjectSettings&) = delete; diff --git a/src/object/path.hpp b/src/object/path.hpp index 04bee8ee6dc..ab0e4dd7b05 100644 --- a/src/object/path.hpp +++ b/src/object/path.hpp @@ -27,6 +27,7 @@ #include "math/easing.hpp" #include "util/gettext.hpp" +template class ObjectOption; class PathGameObject; class ReaderMapping; diff --git a/src/object/path_walker.hpp b/src/object/path_walker.hpp index 693f7abc245..ade8e5a5ef3 100644 --- a/src/object/path_walker.hpp +++ b/src/object/path_walker.hpp @@ -24,6 +24,7 @@ #include "object/path.hpp" #include "util/uid.hpp" +template class ObjectOption; /** A walker that travels along a path */ diff --git a/src/supertux/direction.hpp b/src/supertux/direction.hpp index 3477756156f..b4a6776689f 100644 --- a/src/supertux/direction.hpp +++ b/src/supertux/direction.hpp @@ -19,7 +19,7 @@ #include -class ObjectOption; +template class ObjectOption; enum class Direction { AUTO, NONE, LEFT, RIGHT, UP, DOWN };