diff --git a/src/badguy/badguy.cpp b/src/badguy/badguy.cpp index 84cdeb216af..2b6ba4bc81c 100644 --- a/src/badguy/badguy.cpp +++ b/src/badguy/badguy.cpp @@ -57,7 +57,6 @@ BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite m_start_position(m_col.m_bbox.p1()), m_dir(direction), m_start_dir(direction), - m_dir_in_allowed(0), m_frozen(false), m_ignited(false), m_in_water(false), @@ -82,17 +81,6 @@ BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite m_dir = (m_start_dir == Direction::AUTO) ? Direction::LEFT : m_start_dir; m_lightsprite->set_blend(Blend::ADD); - - auto allowed_directions = get_allowed_directions(); - - for (int i = 0; i < static_cast(allowed_directions.size()); ++i) - { - if (allowed_directions[i] == m_start_dir) - { - m_dir_in_allowed = i; - break; - } - } } BadGuy::BadGuy(const ReaderMapping& reader, const std::string& sprite_name_, int layer_, @@ -105,7 +93,6 @@ BadGuy::BadGuy(const ReaderMapping& reader, const std::string& sprite_name_, int m_start_position(m_col.m_bbox.p1()), m_dir(Direction::LEFT), m_start_dir(Direction::AUTO), - m_dir_in_allowed(0), m_frozen(false), m_ignited(false), m_in_water(false), @@ -137,17 +124,6 @@ BadGuy::BadGuy(const ReaderMapping& reader, const std::string& sprite_name_, int m_dir = (m_start_dir == Direction::AUTO) ? Direction::LEFT : m_start_dir; m_lightsprite->set_blend(Blend::ADD); - - auto allowed_directions = get_allowed_directions(); - - for (int i = 0; i < static_cast(allowed_directions.size()); ++i) - { - if (allowed_directions[i] == m_start_dir) - { - m_dir_in_allowed = i; - break; - } - } } void @@ -332,7 +308,7 @@ BadGuy::deactivate() std::vector BadGuy::get_allowed_directions() const { - return {Direction::AUTO, Direction::LEFT, Direction::RIGHT}; + return { Direction::AUTO, Direction::LEFT, Direction::RIGHT }; } void @@ -1069,7 +1045,7 @@ BadGuy::get_settings() ObjectSettings result = MovingSprite::get_settings(); if (!get_allowed_directions().empty()) - result.add_direction(_("Direction"), reinterpret_cast(&m_dir_in_allowed), Direction::AUTO, "direction", 0, get_allowed_directions()); + result.add_direction(_("Direction"), &m_start_dir, get_allowed_directions(), "direction"); result.add_script(_("Death script"), &m_dead_script, "dead-script"); result.reorder({"direction", "sprite", "x", "y"}); @@ -1082,10 +1058,6 @@ BadGuy::after_editor_set() { MovingSprite::after_editor_set(); - if (!get_allowed_directions().empty()) - m_start_dir = get_allowed_directions()[m_dir_in_allowed]; - else m_start_dir = Direction::AUTO; - if (m_dir == Direction::AUTO) { if (m_sprite->has_action("editor-left")) { diff --git a/src/badguy/badguy.hpp b/src/badguy/badguy.hpp index b267edb32e1..acbed144992 100644 --- a/src/badguy/badguy.hpp +++ b/src/badguy/badguy.hpp @@ -184,7 +184,7 @@ class BadGuy : public MovingSprite, /** called when the badguy has been deactivated */ virtual void deactivate(); - /** returns a vector of dorections the BadGuy can be set to */ + /** Returns a vector of directions the badguy can be set to. */ virtual std::vector get_allowed_directions() const; void kill_squished(GameObject& object); @@ -253,10 +253,6 @@ class BadGuy : public MovingSprite, /** The direction we initially faced in */ Direction m_start_dir; - /** The order of the direction in the allowed directions, - does not correspond to the actual direction!*/ - int m_dir_in_allowed; - bool m_frozen; bool m_ignited; /**< true if this badguy is currently on fire */ bool m_in_water; /** < true if the badguy is currently in water */ diff --git a/src/badguy/dart.cpp b/src/badguy/dart.cpp index a9d42b96ac5..d59e5e2c76e 100644 --- a/src/badguy/dart.cpp +++ b/src/badguy/dart.cpp @@ -158,7 +158,7 @@ Dart::set_flip(Flip flip) std::vector Dart::get_allowed_directions() const { - return {Direction::AUTO, Direction::LEFT, Direction::RIGHT, Direction::UP, Direction::DOWN}; + return { Direction::AUTO, Direction::LEFT, Direction::RIGHT, Direction::UP, Direction::DOWN }; } void diff --git a/src/badguy/darttrap.cpp b/src/badguy/darttrap.cpp index 892cfcb35d3..47f2819f470 100644 --- a/src/badguy/darttrap.cpp +++ b/src/badguy/darttrap.cpp @@ -50,17 +50,6 @@ DartTrap::DartTrap(const ReaderMapping& reader) : if (!Editor::is_active()) { if (m_initial_delay == 0) m_initial_delay = 0.1f; } - - auto allowed_directions = get_allowed_directions(); - - for (int i = 0; i < static_cast(allowed_directions.size()); ++i) - { - if (allowed_directions[i] == m_start_dir) - { - m_dir_in_allowed = i; - break; - } - } } void @@ -167,7 +156,7 @@ DartTrap::get_settings() std::vector DartTrap::get_allowed_directions() const { - return {Direction::LEFT, Direction::RIGHT, Direction::UP, Direction::DOWN}; + return { Direction::LEFT, Direction::RIGHT, Direction::UP, Direction::DOWN }; } void diff --git a/src/editor/object_option.cpp b/src/editor/object_option.cpp index f4b4d48900a..c1799423385 100644 --- a/src/editor/object_option.cpp +++ b/src/editor/object_option.cpp @@ -23,10 +23,12 @@ #include #include "editor/object_menu.hpp" +#include "gui/item_stringselect.hpp" #include "gui/menu.hpp" #include "gui/menu_manager.hpp" #include "gui/menu_object_select.hpp" #include "object/tilemap.hpp" +#include "supertux/direction.hpp" #include "supertux/moving_object.hpp" #include "util/gettext.hpp" #include "util/writer.hpp" @@ -769,4 +771,52 @@ ListOption::add_to_menu(Menu& menu) const { menu.add_list(get_text(), m_items, m_value_ptr); } + +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), + m_possible_directions(std::move(possible_directions)) +{ + if (m_possible_directions.empty()) + m_possible_directions = { Direction::AUTO, Direction::NONE, Direction::LEFT, + Direction::RIGHT, Direction::UP, Direction::DOWN }; +} + +void +DirectionOption::save(Writer& writer) const +{ + if (*m_value_ptr == m_possible_directions.at(0)) + return; + + writer.write(get_key(), dir_to_string(*m_value_ptr)); +} + +std::string +DirectionOption::to_string() const +{ + return dir_to_translated_string(*m_value_ptr); +} + +void +DirectionOption::add_to_menu(Menu& menu) const +{ + int selected = 0; + std::vector labels; + for (size_t i = 0; i < m_possible_directions.size(); i++) + { + const auto& dir = m_possible_directions.at(i); + labels.push_back(dir_to_translated_string(dir)); + + if (dir == *m_value_ptr) + 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 selected) { + *value_ptr = possible_directions.at(selected); + }); +} + /* EOF */ diff --git a/src/editor/object_option.hpp b/src/editor/object_option.hpp index 5258588fe44..756163b357b 100644 --- a/src/editor/object_option.hpp +++ b/src/editor/object_option.hpp @@ -40,8 +40,9 @@ namespace sexp { class Value; } // namespace sexp class Color; -class Menu; +enum class Direction; class GameObject; +class Menu; class Path; class PathObject; class Rectf; @@ -521,6 +522,26 @@ class ListOption : public ObjectOption ListOption& operator=(const ListOption&) = delete; }; +class DirectionOption : public ObjectOption +{ +public: + DirectionOption(const std::string& text, Direction* value_ptr, + std::vector possible_directions, + const std::string& key, unsigned int flags); + + virtual void save(Writer& write) const override; + virtual std::string to_string() const override; + virtual void add_to_menu(Menu& menu) const override; + +private: + Direction* m_value_ptr; + std::vector m_possible_directions; + +private: + DirectionOption(const DirectionOption&) = delete; + DirectionOption& operator=(const DirectionOption&) = delete; +}; + #endif /* EOF */ diff --git a/src/editor/object_settings.cpp b/src/editor/object_settings.cpp index c2622ce09b3..73a8f1462a4 100644 --- a/src/editor/object_settings.cpp +++ b/src/editor/object_settings.cpp @@ -19,7 +19,6 @@ #include #include -#include "supertux/direction.hpp" #include "util/gettext.hpp" #include "video/color.hpp" @@ -113,42 +112,11 @@ ObjectSettings::add_rectf(const std::string& text, Rectf* value_ptr, void ObjectSettings::add_direction(const std::string& text, Direction* value_ptr, - std::optional default_value, - const std::string& key, unsigned int flags, - std::vector possible_directions) -{ - std::vector direction_labels, direction_symbols; - if (!possible_directions.empty()) - { - for (Direction direction : possible_directions) - { - direction_labels.push_back(_(dir_to_string(direction))); - direction_symbols.push_back(dir_to_string(direction)); - } - } - else - { - direction_labels = {_("auto"), _("none"), _("left"), _("right"), _("up"), _("down")}; - direction_symbols = {"auto", "none", "left", "right", "up", "down"}; - } - - int new_default_value = -1; - if (default_value) - { - for (int i = 0; i < static_cast(direction_symbols.size()); ++i) - { - if (string_to_dir(direction_symbols[i]) == *default_value) - { - new_default_value = i; - break; - } - } - } - - add_enum(text, reinterpret_cast(value_ptr), - direction_labels, direction_symbols, - default_value ? new_default_value : std::optional(), - key, flags); + std::vector possible_directions, + const std::string& key, unsigned int flags) +{ + add_option(std::make_unique(text, value_ptr, std::move(possible_directions), + key, flags)); } void diff --git a/src/editor/object_settings.hpp b/src/editor/object_settings.hpp index 91917f3fa18..b8f60845993 100644 --- a/src/editor/object_settings.hpp +++ b/src/editor/object_settings.hpp @@ -63,9 +63,8 @@ class ObjectSettings final std::optional default_value = {}, const std::string& key = {}, unsigned int flags = 0); void add_direction(const std::string& text, Direction* value_ptr, - std::optional default_value = {}, - const std::string& key = {}, unsigned int flags = 0, - std::vector possible_directions = {}); + std::vector possible_directions = {}, + const std::string& key = {}, unsigned int flags = 0); void add_walk_mode(const std::string& text, WalkMode* value_ptr, const std::optional& default_value = {}, const std::string& key = {}, unsigned int flags = 0); diff --git a/src/gui/item_stringselect.cpp b/src/gui/item_stringselect.cpp index f81784c5ae3..288a80c21ad 100644 --- a/src/gui/item_stringselect.cpp +++ b/src/gui/item_stringselect.cpp @@ -28,11 +28,28 @@ ItemStringSelect::ItemStringSelect(const std::string& text, std::vector items, int default_item, int id) : + MenuItem(text, id), + m_items(std::move(items)), + m_selected(new int(default_item)), + m_pointer_provided(false), + m_callback(), + m_width(calculate_width()) +{ +} + +ItemStringSelect::~ItemStringSelect() +{ + if (!m_pointer_provided) + delete m_selected; +} + void ItemStringSelect::draw(DrawingContext& context, const Vector& pos, int menu_width, bool active) { diff --git a/src/gui/item_stringselect.hpp b/src/gui/item_stringselect.hpp index e4478515f70..46504274507 100644 --- a/src/gui/item_stringselect.hpp +++ b/src/gui/item_stringselect.hpp @@ -25,6 +25,8 @@ class ItemStringSelect final : public MenuItem { public: ItemStringSelect(const std::string& text, std::vector items, int* selected, int id = -1); + ItemStringSelect(const std::string& text, std::vector items, int default_item, int id = -1); + ~ItemStringSelect() override; /** Draws the menu item. */ virtual void draw(DrawingContext&, const Vector& pos, int menu_width, bool active) override; @@ -49,6 +51,8 @@ class ItemStringSelect final : public MenuItem private: std::vector m_items; // list of values for a STRINGSELECT item int* m_selected; // currently selected item + const bool m_pointer_provided; // Indicates whether a pointer has been provided to the item. + std::function m_callback; float m_width; diff --git a/src/gui/menu.cpp b/src/gui/menu.cpp index e51eb8606d4..347b9413b53 100644 --- a/src/gui/menu.cpp +++ b/src/gui/menu.cpp @@ -272,6 +272,15 @@ Menu::add_string_select(int id, const std::string& text, int* selected, const st return *item_ptr; } +ItemStringSelect& +Menu::add_string_select(int id, const std::string& text, int default_item, const std::vector& strings) +{ + auto item = std::make_unique(text, strings, default_item, id); + auto item_ptr = item.get(); + add_item(std::move(item)); + return *item_ptr; +} + ItemFile& Menu::add_file(const std::string& text, std::string* input, const std::vector& extensions, const std::string& basedir, bool path_relative_to_basedir, int id) diff --git a/src/gui/menu.hpp b/src/gui/menu.hpp index 448562b661b..0f55350b43e 100644 --- a/src/gui/menu.hpp +++ b/src/gui/menu.hpp @@ -90,6 +90,7 @@ class Menu ItemGoTo& add_submenu(const std::string& text, int submenu, int id = -1); ItemControlField& add_controlfield(int id, const std::string& text, const std::string& mapping = ""); ItemStringSelect& add_string_select(int id, const std::string& text, int* selected, const std::vector& strings); + ItemStringSelect& add_string_select(int id, const std::string& text, int default_item, const std::vector& strings); ItemTextField& add_textfield(const std::string& text, std::string* input, int id = -1); ItemScript& add_script(const std::string& text, std::string* script, int id = -1); ItemScriptLine& add_script_line(std::string* input, int id = -1); diff --git a/src/object/bumper.cpp b/src/object/bumper.cpp index b7567a890f8..f2c59aea1f6 100644 --- a/src/object/bumper.cpp +++ b/src/object/bumper.cpp @@ -32,8 +32,7 @@ const float BOUNCE_X = 700.0f; Bumper::Bumper(const ReaderMapping& reader) : MovingSprite(reader, "images/objects/trampoline/bumper.sprite", LAYER_OBJECTS, COLGROUP_MOVING), m_physic(), - m_dir(Direction::RIGHT), - m_dir_in_allowed(0) + m_dir(Direction::RIGHT) { std::string dir_str; bool old_facing_left = false; @@ -44,17 +43,6 @@ Bumper::Bumper(const ReaderMapping& reader) : m_dir = Direction::LEFT; set_action("normal", m_dir); m_physic.enable_gravity(false); - - auto allowed_directions = get_allowed_directions(); - - for (int i = 0; i < static_cast(allowed_directions.size()); ++i) - { - if (allowed_directions[i] == m_dir) - { - m_dir_in_allowed = i; - break; - } - } } ObjectSettings @@ -62,7 +50,7 @@ Bumper::get_settings() { ObjectSettings result = MovingSprite::get_settings(); - result.add_direction(_("Direction"), reinterpret_cast(&m_dir_in_allowed), Direction::RIGHT, "direction", 0, get_allowed_directions()); + result.add_direction(_("Direction"), &m_dir, get_allowed_directions(), "direction"); result.reorder({"direction", "sprite", "x", "y"}); return result; } @@ -105,7 +93,7 @@ Bumper::get_physic() std::vector Bumper::get_allowed_directions() const { - return {Direction::LEFT, Direction::RIGHT}; + return { Direction::LEFT, Direction::RIGHT }; } void @@ -113,7 +101,6 @@ Bumper::after_editor_set() { MovingSprite::after_editor_set(); - m_dir = get_allowed_directions()[m_dir_in_allowed]; set_action("normal", m_dir); } diff --git a/src/object/bumper.hpp b/src/object/bumper.hpp index c5da8ffaad0..854d0d63cff 100644 --- a/src/object/bumper.hpp +++ b/src/object/bumper.hpp @@ -44,13 +44,12 @@ class Bumper final : public MovingSprite Physic& get_physic(); private: - virtual std::vector get_allowed_directions() const; + std::vector get_allowed_directions() const; private: Physic m_physic; Direction m_dir; - int m_dir_in_allowed; private: Bumper(const Bumper&) = delete; diff --git a/src/object/conveyor_belt.cpp b/src/object/conveyor_belt.cpp index 6498ba04b05..d832fc500da 100644 --- a/src/object/conveyor_belt.cpp +++ b/src/object/conveyor_belt.cpp @@ -28,7 +28,6 @@ ConveyorBelt::ConveyorBelt(const ReaderMapping &reader) : ExposedObject(this), m_running(true), m_dir(Direction::LEFT), - m_dir_in_allowed(0), m_length(1), m_speed(1.0f), m_frame(0.0f), @@ -51,17 +50,6 @@ ConveyorBelt::ConveyorBelt(const ReaderMapping &reader) : set_action("stopped"); else set_action(m_dir); - - auto allowed_directions = get_allowed_directions(); - - for (int i = 0; i < static_cast(allowed_directions.size()); ++i) - { - if (allowed_directions[i] == m_dir) - { - m_dir_in_allowed = i; - break; - } - } } ObjectSettings @@ -69,7 +57,7 @@ ConveyorBelt::get_settings() { ObjectSettings result = MovingSprite::get_settings(); - result.add_direction(_("Direction"), reinterpret_cast(&m_dir_in_allowed), Direction::LEFT, "direction", 0, get_allowed_directions()); + result.add_direction(_("Direction"), &m_dir, get_allowed_directions(), "direction"); result.add_float(_("Speed"), &m_speed, "speed", 1.0f); result.add_bool(_("Running"), &m_running, "running", true); result.add_int(_("Length"), &m_length, "length", 3); @@ -138,8 +126,6 @@ ConveyorBelt::after_editor_set() { MovingSprite::after_editor_set(); - m_dir = get_allowed_directions()[m_dir_in_allowed]; - if (m_length <= 0) m_length = 1; set_action(dir_to_string(m_dir)); @@ -185,7 +171,7 @@ ConveyorBelt::set_speed(float target_speed) std::vector ConveyorBelt::get_allowed_directions() const { - return {Direction::LEFT, Direction::RIGHT}; + return { Direction::LEFT, Direction::RIGHT }; } /* EOF */ diff --git a/src/object/conveyor_belt.hpp b/src/object/conveyor_belt.hpp index ad240022498..f0a42c4fc94 100644 --- a/src/object/conveyor_belt.hpp +++ b/src/object/conveyor_belt.hpp @@ -65,12 +65,12 @@ class ConveyorBelt final : public MovingSprite, private: void update_hitbox() override; - virtual std::vector get_allowed_directions() const; + + std::vector get_allowed_directions() const; private: bool m_running; Direction m_dir; - int m_dir_in_allowed; int m_length; float m_speed; diff --git a/src/object/ispy.cpp b/src/object/ispy.cpp index 4b91ea69d3d..62d755cedb5 100644 --- a/src/object/ispy.cpp +++ b/src/object/ispy.cpp @@ -29,8 +29,7 @@ Ispy::Ispy(const ReaderMapping& reader) : MovingSprite(reader, "images/objects/ispy/ispy.sprite", LAYER_TILES + 5, COLGROUP_DISABLED), m_state(ISPYSTATE_IDLE), m_script(), - m_dir(Direction::LEFT), - m_dir_in_allowed(0) + m_dir(Direction::LEFT) { reader.get("script", m_script); @@ -43,17 +42,6 @@ Ispy::Ispy(const ReaderMapping& reader) : if (m_dir == Direction::AUTO) log_warning << "Setting an Ispy's direction to AUTO is no good idea." << std::endl; - auto allowed_directions = get_allowed_directions(); - - for (int i = 0; i < static_cast(allowed_directions.size()); ++i) - { - if (allowed_directions[i] == m_dir) - { - m_dir_in_allowed = i; - break; - } - } - set_action("idle", m_dir); } @@ -63,7 +51,7 @@ Ispy::get_settings() ObjectSettings result = MovingSprite::get_settings(); result.add_script(_("Script"), &m_script, "script"); - result.add_direction(_("Direction"), reinterpret_cast(&m_dir_in_allowed), Direction::LEFT, "direction", 0, get_allowed_directions()); + result.add_direction(_("Direction"), &m_dir, get_allowed_directions(), "direction"); result.reorder({"script", "facing-down", "direction", "x", "y"}); @@ -74,7 +62,6 @@ void Ispy::after_editor_set() { MovingSprite::after_editor_set(); - m_dir = get_allowed_directions()[m_dir_in_allowed]; set_action("idle", m_dir); } @@ -139,7 +126,7 @@ Ispy::update(float dt_sec) std::vector Ispy::get_allowed_directions() const { - return {Direction::LEFT, Direction::RIGHT, Direction::UP, Direction::DOWN}; + return { Direction::LEFT, Direction::RIGHT, Direction::UP, Direction::DOWN }; } void diff --git a/src/object/ispy.hpp b/src/object/ispy.hpp index 2474c3f9f8f..8d635fb0516 100644 --- a/src/object/ispy.hpp +++ b/src/object/ispy.hpp @@ -41,7 +41,7 @@ class Ispy final : public MovingSprite virtual void on_flip(float height) override; private: - virtual std::vector get_allowed_directions() const; + std::vector get_allowed_directions() const; private: enum IspyState { @@ -56,7 +56,6 @@ class Ispy final : public MovingSprite std::string m_script; /**< script to execute when Tux is spotted */ Direction m_dir; - int m_dir_in_allowed; private: Ispy(const Ispy&) = delete; diff --git a/src/object/pushbutton.cpp b/src/object/pushbutton.cpp index d691a8dbfa6..52bd1659537 100644 --- a/src/object/pushbutton.cpp +++ b/src/object/pushbutton.cpp @@ -20,7 +20,6 @@ #include "object/player.hpp" #include "object/rock.hpp" #include "sprite/sprite.hpp" -#include "supertux/direction.hpp" #include "supertux/flip_level_transformer.hpp" #include "supertux/sector.hpp" #include "util/reader_mapping.hpp" @@ -34,8 +33,7 @@ PushButton::PushButton(const ReaderMapping& mapping) : MovingSprite(mapping, "images/objects/pushbutton/pushbutton.sprite", LAYER_BACKGROUNDTILES+1, COLGROUP_MOVING), m_script(), m_state(OFF), - m_dir(Direction::UP), - m_dir_in_allowed(0) + m_dir(Direction::UP) { SoundManager::current()->preload(BUTTON_SOUND); @@ -52,17 +50,6 @@ PushButton::PushButton(const ReaderMapping& mapping) : else if (mapping.get("upside-down", old_upside_down) && old_upside_down) m_dir = Direction::DOWN; - auto allowed_directions = get_allowed_directions(); - - for (int i = 0; i < static_cast(allowed_directions.size()); ++i) - { - if (allowed_directions[i] == m_dir) - { - m_dir_in_allowed = i; - break; - } - } - set_action("off", m_dir, -1); } @@ -71,7 +58,7 @@ PushButton::get_settings() { ObjectSettings result = MovingSprite::get_settings(); - result.add_direction(_("Direction"), reinterpret_cast(&m_dir_in_allowed), Direction::UP, "direction", 0, get_allowed_directions()); + result.add_direction(_("Direction"), &m_dir, get_allowed_directions(), "direction"); result.add_script(_("Script"), &m_script, "script"); result.reorder({"direction", "script", "x", "y"}); @@ -83,7 +70,6 @@ void PushButton::after_editor_set() { MovingSprite::after_editor_set(); - m_dir = get_allowed_directions()[m_dir_in_allowed]; set_action("off", m_dir); } @@ -147,7 +133,7 @@ PushButton::collision(GameObject& other, const CollisionHit& hit) std::vector PushButton::get_allowed_directions() const { - return {Direction::UP, Direction::DOWN}; + return { Direction::UP, Direction::DOWN }; } void diff --git a/src/object/pushbutton.hpp b/src/object/pushbutton.hpp index b0800dac96a..6ef027efacb 100644 --- a/src/object/pushbutton.hpp +++ b/src/object/pushbutton.hpp @@ -19,8 +19,6 @@ #include "object/moving_sprite.hpp" -enum class Direction; - /** PushButton - jump on it to run a script */ class PushButton final : public MovingSprite { @@ -40,7 +38,7 @@ class PushButton final : public MovingSprite virtual void on_flip(float height) override; private: - virtual std::vector get_allowed_directions() const; + std::vector get_allowed_directions() const; private: enum PushButtonState { @@ -52,7 +50,6 @@ class PushButton final : public MovingSprite PushButtonState m_state; Direction m_dir; - int m_dir_in_allowed; private: PushButton(const PushButton&) = delete; diff --git a/src/supertux/direction.cpp b/src/supertux/direction.cpp index 08441cedbbf..a8248b449cf 100644 --- a/src/supertux/direction.cpp +++ b/src/supertux/direction.cpp @@ -52,6 +52,31 @@ dir_to_string(const Direction& dir) } } +std::string +dir_to_translated_string(const Direction& dir) +{ + switch (dir) + { + case Direction::NONE: + return _("none"); + case Direction::LEFT: + return _("left"); + case Direction::RIGHT: + return _("right"); + case Direction::UP: + return _("up"); + case Direction::DOWN: + return _("down"); + default: + if (dir != Direction::AUTO) + { + // Display a warning when an invalid direction has been provided. + log_warning << "Unknown direction \"" << dir << "\". Switching to \"auto\"." << std::endl; + } + return _("auto"); + } +} + Direction string_to_dir(const std::string& dir_str) { diff --git a/src/supertux/direction.hpp b/src/supertux/direction.hpp index 6bac98d2e84..3477756156f 100644 --- a/src/supertux/direction.hpp +++ b/src/supertux/direction.hpp @@ -26,6 +26,7 @@ enum class Direction { AUTO, NONE, LEFT, RIGHT, UP, DOWN }; std::ostream& operator<<(std::ostream& o, const Direction& dir); std::string dir_to_string(const Direction& dir); +std::string dir_to_translated_string(const Direction& dir); Direction string_to_dir(const std::string& dir_str); #endif diff --git a/src/trigger/switch.cpp b/src/trigger/switch.cpp index dd164e673e1..60825acb810 100644 --- a/src/trigger/switch.cpp +++ b/src/trigger/switch.cpp @@ -34,8 +34,7 @@ Switch::Switch(const ReaderMapping& reader) : m_off_script(), m_state(OFF), m_bistable(), - m_dir(Direction::NONE), - m_dir_in_allowed(0) + m_dir(Direction::NONE) { std::string dir_str; if (reader.get("direction", dir_str)) @@ -43,17 +42,6 @@ Switch::Switch(const ReaderMapping& reader) : else m_dir = Direction::NONE; - auto allowed_directions = get_allowed_directions(); - - for (int i = 0; i < static_cast(allowed_directions.size()); ++i) - { - if (allowed_directions[i] == m_dir) - { - m_dir_in_allowed = i; - break; - } - } - set_action("off", m_dir); reader.get("script", m_script); @@ -71,7 +59,7 @@ Switch::get_settings() { ObjectSettings result = SpritedTrigger::get_settings(); - result.add_direction(_("Direction"), reinterpret_cast(&m_dir_in_allowed), Direction::NONE, "direction", 0, get_allowed_directions()); + result.add_direction(_("Direction"), &m_dir, get_allowed_directions(), "direction"); result.add_script(_("Turn on script"), &m_script, "script"); result.add_script(_("Turn off script"), &m_off_script, "off-script"); @@ -146,7 +134,7 @@ Switch::event(Player& , EventType type) std::vector Switch::get_allowed_directions() const { - return {Direction::NONE, Direction::LEFT, Direction::RIGHT, Direction::UP, Direction::DOWN}; + return { Direction::NONE, Direction::LEFT, Direction::RIGHT, Direction::UP, Direction::DOWN }; } void @@ -154,7 +142,6 @@ Switch::after_editor_set() { SpritedTrigger::after_editor_set(); - m_dir = get_allowed_directions()[m_dir_in_allowed]; set_action("off", m_dir); } diff --git a/src/trigger/switch.hpp b/src/trigger/switch.hpp index 267032d629d..3e92928e416 100644 --- a/src/trigger/switch.hpp +++ b/src/trigger/switch.hpp @@ -18,7 +18,6 @@ #define HEADER_SUPERTUX_TRIGGER_SWITCH_HPP #include "trigger/trigger_base.hpp" -#include "supertux/direction.hpp" class Switch final : public SpritedTrigger { @@ -40,7 +39,7 @@ class Switch final : public SpritedTrigger virtual void on_flip(float height) override; private: - virtual std::vector get_allowed_directions() const; + std::vector get_allowed_directions() const; private: enum SwitchState { @@ -56,7 +55,6 @@ class Switch final : public SpritedTrigger SwitchState m_state; bool m_bistable; Direction m_dir; - int m_dir_in_allowed; private: Switch(const Switch&) = delete;