Skip to content

Commit

Permalink
Use DirectionOption to manage custom directions, code fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vankata453 committed Aug 7, 2023
1 parent 8d97dca commit 26b5821
Show file tree
Hide file tree
Showing 24 changed files with 161 additions and 183 deletions.
32 changes: 2 additions & 30 deletions src/badguy/badguy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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<int>(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_,
Expand All @@ -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),
Expand Down Expand Up @@ -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<int>(allowed_directions.size()); ++i)
{
if (allowed_directions[i] == m_start_dir)
{
m_dir_in_allowed = i;
break;
}
}
}

void
Expand Down Expand Up @@ -332,7 +308,7 @@ BadGuy::deactivate()
std::vector<Direction>
BadGuy::get_allowed_directions() const
{
return {Direction::AUTO, Direction::LEFT, Direction::RIGHT};
return { Direction::AUTO, Direction::LEFT, Direction::RIGHT };
}

void
Expand Down Expand Up @@ -1069,7 +1045,7 @@ BadGuy::get_settings()
ObjectSettings result = MovingSprite::get_settings();

if (!get_allowed_directions().empty())
result.add_direction(_("Direction"), reinterpret_cast<Direction*>(&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"});
Expand All @@ -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")) {
Expand Down
6 changes: 1 addition & 5 deletions src/badguy/badguy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Direction> get_allowed_directions() const;

void kill_squished(GameObject& object);
Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/dart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Dart::set_flip(Flip flip)
std::vector<Direction>
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
Expand Down
13 changes: 1 addition & 12 deletions src/badguy/darttrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(allowed_directions.size()); ++i)
{
if (allowed_directions[i] == m_start_dir)
{
m_dir_in_allowed = i;
break;
}
}
}

void
Expand Down Expand Up @@ -167,7 +156,7 @@ DartTrap::get_settings()
std::vector<Direction>
DartTrap::get_allowed_directions() const
{
return {Direction::LEFT, Direction::RIGHT, Direction::UP, Direction::DOWN};
return { Direction::LEFT, Direction::RIGHT, Direction::UP, Direction::DOWN };
}

void
Expand Down
50 changes: 50 additions & 0 deletions src/editor/object_option.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
#include <sstream>

#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"
Expand Down Expand Up @@ -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<Direction> 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<std::string> 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<int>(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 */
23 changes: 22 additions & 1 deletion src/editor/object_option.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Direction> 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<Direction> m_possible_directions;

private:
DirectionOption(const DirectionOption&) = delete;
DirectionOption& operator=(const DirectionOption&) = delete;
};

#endif

/* EOF */
42 changes: 5 additions & 37 deletions src/editor/object_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <assert.h>
#include <sexp/value.hpp>

#include "supertux/direction.hpp"
#include "util/gettext.hpp"
#include "video/color.hpp"

Expand Down Expand Up @@ -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<Direction> default_value,
const std::string& key, unsigned int flags,
std::vector<Direction> possible_directions)
{
std::vector<std::string> 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<int>(direction_symbols.size()); ++i)
{
if (string_to_dir(direction_symbols[i]) == *default_value)
{
new_default_value = i;
break;
}
}
}

add_enum(text, reinterpret_cast<int*>(value_ptr),
direction_labels, direction_symbols,
default_value ? new_default_value : std::optional<int>(),
key, flags);
std::vector<Direction> possible_directions,
const std::string& key, unsigned int flags)
{
add_option(std::make_unique<DirectionOption>(text, value_ptr, std::move(possible_directions),
key, flags));
}

void
Expand Down
5 changes: 2 additions & 3 deletions src/editor/object_settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ class ObjectSettings final
std::optional<worldmap::Direction> default_value = {},
const std::string& key = {}, unsigned int flags = 0);
void add_direction(const std::string& text, Direction* value_ptr,
std::optional<Direction> default_value = {},
const std::string& key = {}, unsigned int flags = 0,
std::vector<Direction> possible_directions = {});
std::vector<Direction> possible_directions = {},
const std::string& key = {}, unsigned int flags = 0);
void add_walk_mode(const std::string& text, WalkMode* value_ptr,
const std::optional<WalkMode>& default_value = {},
const std::string& key = {}, unsigned int flags = 0);
Expand Down
17 changes: 17 additions & 0 deletions src/gui/item_stringselect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,28 @@ ItemStringSelect::ItemStringSelect(const std::string& text, std::vector<std::str
MenuItem(text, id),
m_items(std::move(items)),
m_selected(std::move(selected)),
m_pointer_provided(true),
m_callback(),
m_width(calculate_width())
{
}

ItemStringSelect::ItemStringSelect(const std::string& text, std::vector<std::string> 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)
{
Expand Down
4 changes: 4 additions & 0 deletions src/gui/item_stringselect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class ItemStringSelect final : public MenuItem
{
public:
ItemStringSelect(const std::string& text, std::vector<std::string> items, int* selected, int id = -1);
ItemStringSelect(const std::string& text, std::vector<std::string> 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;
Expand All @@ -49,6 +51,8 @@ class ItemStringSelect final : public MenuItem
private:
std::vector<std::string> 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<void(int)> m_callback;
float m_width;

Expand Down
9 changes: 9 additions & 0 deletions src/gui/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& strings)
{
auto item = std::make_unique<ItemStringSelect>(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<std::string>& extensions,
const std::string& basedir, bool path_relative_to_basedir, int id)
Expand Down
1 change: 1 addition & 0 deletions src/gui/menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& strings);
ItemStringSelect& add_string_select(int id, const std::string& text, int default_item, const std::vector<std::string>& 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);
Expand Down
Loading

0 comments on commit 26b5821

Please sign in to comment.