Skip to content

Commit

Permalink
Provide ScriptMenu reference to ItemScriptLine
Browse files Browse the repository at this point in the history
Fixes #3113.
  • Loading branch information
Vankata453 committed Nov 27, 2024
1 parent 725a20a commit 4a14433
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 27 deletions.
21 changes: 7 additions & 14 deletions src/gui/item_script_line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "gui/item_script_line.hpp"

#include "control/input_manager.hpp"
#include "gui/menu_manager.hpp"
#include "gui/menu_script.hpp"
#include "supertux/colorscheme.hpp"
#include "supertux/console.hpp"
Expand All @@ -27,9 +26,9 @@
#include "util/string_util.hpp"
#include "video/drawing_context.hpp"

ItemScriptLine::ItemScriptLine(std::string* input_, int id_) :
ItemScriptLine::ItemScriptLine(ScriptMenu& script_menu, std::string* input_, int id_) :
ItemTextField("", input_, id_),
m_script_menu(dynamic_cast<ScriptMenu*>(MenuManager::instance().current_menu()))
m_script_menu(script_menu)
{
m_cursor_width = Resources::console_font->get_text_width(m_cursor);
}
Expand Down Expand Up @@ -96,8 +95,7 @@ ItemScriptLine::process_action(const MenuAction& action)
void
ItemScriptLine::invalid_remove()
{
if (m_script_menu)
m_script_menu->remove_line();
m_script_menu.remove_line();
}

// Text manipulation and navigation functions
Expand All @@ -116,27 +114,22 @@ ItemScriptLine::paste() // Paste with mutli-line support
if (paste_lines.empty()) return;
insert_text(paste_lines[0], m_cursor_left_offset);

if (m_script_menu)
{
for (std::size_t i = 1; i < paste_lines.size(); i++)
m_script_menu->add_line()->change_input(paste_lines[i]);
}
for (std::size_t i = 1; i < paste_lines.size(); i++)
m_script_menu.add_line()->change_input(paste_lines[i]);

on_input_update();
}

void
ItemScriptLine::new_line()
{
if (m_script_menu)
m_script_menu->add_line();
m_script_menu.add_line();
}

void
ItemScriptLine::duplicate_line()
{
if (m_script_menu)
m_script_menu->add_line()->change_input(*input);
m_script_menu.add_line()->change_input(*input);
}

/* EOF */
4 changes: 2 additions & 2 deletions src/gui/item_script_line.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ScriptMenu;
class ItemScriptLine final : public ItemTextField
{
public:
ItemScriptLine(std::string* input_, int id_ = -1);
ItemScriptLine(ScriptMenu& script_menu, std::string* input_, int id_ = -1);

/** Draws the menu item. */
virtual void draw(DrawingContext&, const Vector& pos, int menu_width, bool active) override;
Expand All @@ -49,7 +49,7 @@ class ItemScriptLine final : public ItemTextField
virtual void duplicate_line();

private:
ScriptMenu* const m_script_menu;
ScriptMenu& m_script_menu;

private:
ItemScriptLine(const ItemScriptLine&) = delete;
Expand Down
6 changes: 0 additions & 6 deletions src/gui/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,6 @@ Menu::add_script(const std::string& text, std::string* script, int id)
return add_item<ItemScript>(text, script, id);
}

ItemScriptLine&
Menu::add_script_line(std::string* input, int id)
{
return add_item<ItemScriptLine>(input, id);
}

ItemIntField&
Menu::add_intfield(const std::string& text, int* input, int id, bool positive)
{
Expand Down
2 changes: 0 additions & 2 deletions src/gui/menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class ItemIntField;
class ItemLabel;
class ItemPaths;
class ItemScript;
class ItemScriptLine;
class ItemList;
class ItemStringSelect;
class ItemTextField;
Expand Down Expand Up @@ -91,7 +90,6 @@ class Menu
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);
ItemIntField& add_intfield(const std::string& text, int* input, int id = -1, bool positive = false);
ItemFloatField& add_floatfield(const std::string& text, float* input, int id = -1, bool positive = false);
ItemAction& add_file(const std::string& text, std::string* input, const std::vector<std::string>& extensions,
Expand Down
5 changes: 2 additions & 3 deletions src/gui/menu_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void
ScriptMenu::push_string(const std::string& new_line)
{
script_strings.push_back(std::make_unique<std::string>(new_line));
add_script_line( (script_strings.end()-1)->get() );
add_item(std::make_unique<ItemScriptLine>(*this, (script_strings.end()-1)->get()));
}

void
Expand All @@ -79,8 +79,7 @@ ScriptMenu::add_line()
auto new_line = std::make_unique<std::string>();
script_strings.insert(script_strings.begin() + (m_active_item - 1), std::move(new_line));

auto line_item = std::unique_ptr<ItemScriptLine>(
new ItemScriptLine( (script_strings.begin()+(m_active_item-1))->get() ));
auto line_item = std::make_unique<ItemScriptLine>(*this, (script_strings.begin()+(m_active_item-1))->get());
add_item(std::move(line_item), m_active_item+1);
m_active_item++;

Expand Down
2 changes: 2 additions & 0 deletions src/gui/menu_script.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "gui/menu.hpp"

class ItemScriptLine;

class ScriptMenu final : public Menu
{
public:
Expand Down

0 comments on commit 4a14433

Please sign in to comment.