Skip to content

Commit 3c8f878

Browse files
authored
Merge pull request #1915 from mrkubax10/add_text_area
Add text area object
2 parents ec9009e + d3f5673 commit 3c8f878

18 files changed

+590
-98
lines changed

data/images/engine/editor/objects.stoi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@
318318
(object
319319
(class "skull_tile")
320320
(icon "images/objects/skull_tile/skull.png"))
321+
(object
322+
(class "text-area")
323+
(icon "images/engine/editor/textarray.png"))
321324
)
322325

323326
(objectgroup

src/editor/object_option.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,4 +684,20 @@ ButtonOption::add_to_menu(Menu& menu) const
684684
menu.add_entry(get_text(), m_callback);
685685
}
686686

687+
StringArrayOption::StringArrayOption(const std::string& text, const std::string& key, std::vector<std::string>& items) :
688+
ObjectOption(text, key, 0),
689+
m_items(items)
690+
{}
691+
692+
void
693+
StringArrayOption::save(Writer& write) const
694+
{
695+
write.write("strings", m_items);
696+
}
697+
698+
void
699+
StringArrayOption::add_to_menu(Menu& menu) const
700+
{
701+
menu.add_string_array(get_text(), m_items);
702+
}
687703
/* EOF */

src/editor/object_option.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,23 @@ class ButtonOption : public ObjectOption
464464
ButtonOption& operator=(const ButtonOption&) = delete;
465465
};
466466

467+
class StringArrayOption : public ObjectOption
468+
{
469+
public:
470+
StringArrayOption(const std::string& text, const std::string& key, std::vector<std::string>& items);
471+
472+
virtual void save(Writer& write) const override;
473+
virtual std::string to_string() const override { return "text-area"; }
474+
virtual void add_to_menu(Menu& menu) const override;
475+
476+
private:
477+
std::vector<std::string>& m_items;
478+
479+
private:
480+
StringArrayOption(const StringArrayOption&) = delete;
481+
StringArrayOption& operator=(const StringArrayOption&) = delete;
482+
};
483+
467484
#endif
468485

469486
/* EOF */

src/editor/object_settings.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ ObjectSettings::add_sexp(const std::string& text, const std::string& key, sexp::
293293
add_option(std::make_unique<SExpObjectOption>(text, key, value, flags));
294294
}
295295

296+
void
297+
ObjectSettings::add_string_array(const std::string& text, const std::string& key, std::vector<std::string>& items)
298+
{
299+
add_option(std::make_unique<StringArrayOption>(text, key, items));
300+
}
301+
296302
void
297303
ObjectSettings::add_test_from_here()
298304
{

src/editor/object_settings.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class ObjectSettings final
139139
unsigned int flags = 0);
140140
void add_sexp(const std::string& text, const std::string& key,
141141
sexp::Value& value, unsigned int flags = 0);
142+
void add_string_array(const std::string& text, const std::string& key, std::vector<std::string>& items);
142143
void add_test_from_here();
143144
void add_particle_editor();
144145
void add_path_handle(const std::string& text, PathWalker::Handle& handle,

src/gui/item_string_array.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SuperTux
2+
// Copyright (C) 2021 mrkubax10 <[email protected]>
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
#include "gui/item_string_array.hpp"
18+
19+
#include "gui/menu_manager.hpp"
20+
#include "gui/menu_string_array.hpp"
21+
22+
ItemStringArray::ItemStringArray(const std::string& text, std::vector<std::string>& items, int id) :
23+
MenuItem(text, id),
24+
m_items(items)
25+
{
26+
}
27+
28+
void
29+
ItemStringArray::process_action(const MenuAction& action)
30+
{
31+
if (action == MenuAction::HIT)
32+
{
33+
MenuManager::instance().push_menu(std::make_unique<StringArrayMenu>(m_items));
34+
}
35+
}
36+
/* EOF */

src/gui/item_string_array.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SuperTux
2+
// Copyright (C) 2021 mrkubax10 <[email protected]>
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
#ifndef HEADER_SUPERTUX_GUI_ITEM_STRING_ARRAY_HPP
18+
#define HEADER_SUPERTUX_GUI_ITEM_STRING_ARRAY_HPP
19+
20+
#include "gui/menu_item.hpp"
21+
#include <vector>
22+
23+
class ItemStringArray final : public MenuItem
24+
{
25+
public:
26+
ItemStringArray(const std::string& text, std::vector<std::string>& items, int id = -1);
27+
28+
virtual void process_action(const MenuAction& action) override;
29+
30+
private:
31+
std::vector<std::string>& m_items;
32+
33+
private:
34+
ItemStringArray(const ItemStringArray&) = delete;
35+
ItemStringArray& operator=(const ItemStringArray&) = delete;
36+
};
37+
38+
#endif
39+
/* EOF */

src/gui/menu.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "gui/item_stringselect.hpp"
3838
#include "gui/item_textfield.hpp"
3939
#include "gui/item_toggle.hpp"
40+
#include "gui/item_string_array.hpp"
4041
#include "gui/menu_item.hpp"
4142
#include "gui/menu_manager.hpp"
4243
#include "gui/mousecursor.hpp"
@@ -341,6 +342,15 @@ Menu::add_badguy_select(const std::string& text, std::vector<std::string>* badgu
341342
return *item_ptr;
342343
}
343344

345+
ItemStringArray&
346+
Menu::add_string_array(const std::string& text, std::vector<std::string>& items, int id)
347+
{
348+
auto item = std::make_unique<ItemStringArray>(text, items, id);
349+
auto item_ptr = item.get();
350+
add_item(std::move(item));
351+
return *item_ptr;
352+
}
353+
344354
void
345355
Menu::clear()
346356
{

src/gui/menu.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class ItemScriptLine;
4848
class ItemStringSelect;
4949
class ItemTextField;
5050
class ItemToggle;
51+
class ItemStringArray;
5152
class MenuItem;
5253
class PathObject;
5354

@@ -96,6 +97,7 @@ class Menu
9697
bool is_linear = false);
9798
ItemColorChannelOKLab& add_color_channel_oklab(Color* color, int channel);
9899
ItemPaths& add_path_settings(const std::string& text, PathObject& target, const std::string& path_ref);
100+
ItemStringArray& add_string_array(const std::string& text, std::vector<std::string>& items, int id = -1);
99101

100102
void process_input(const Controller& controller);
101103

src/gui/menu_string_array.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// SuperTux
2+
// Copyright (C) 2021 mrkubax10 <[email protected]>
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
#include "gui/menu_string_array.hpp"
18+
19+
#include <boost/format.hpp>
20+
21+
#include "gui/menu_item.hpp"
22+
#include "util/gettext.hpp"
23+
24+
StringArrayMenu::StringArrayMenu(std::vector<std::string>& items) :
25+
m_array_items(items),
26+
m_text(),
27+
m_selected_item(-1)
28+
{
29+
reload();
30+
}
31+
32+
void
33+
StringArrayMenu::menu_action(MenuItem& item)
34+
{
35+
int id = item.get_id();
36+
if (id >= 0)
37+
{
38+
m_text = m_array_items[id];
39+
m_selected_item = id;
40+
get_item_by_id(-2).set_text(str(boost::format(_("Selected item: %s")) % (m_selected_item >= 0 ? m_array_items[m_selected_item] : _("None"))));
41+
}
42+
else if (m_text.length() > 0 && id < -2)
43+
{
44+
if (id == -3)
45+
{
46+
m_array_items.push_back(m_text);
47+
}
48+
else if (id == -4 && m_selected_item >= 0)
49+
{
50+
m_array_items.insert(m_array_items.begin() + m_selected_item + 1, m_text);
51+
}
52+
else if (id == -5 && m_selected_item >= 0)
53+
{
54+
m_array_items[m_selected_item] = m_text;
55+
}
56+
else if (id == -6 && m_selected_item >= 0)
57+
{
58+
m_array_items.erase(m_array_items.begin() + m_selected_item);
59+
}
60+
m_text = "";
61+
m_selected_item = -1;
62+
reload();
63+
}
64+
}
65+
66+
void
67+
StringArrayMenu::reload()
68+
{
69+
clear();
70+
add_label(_("Edit string array"));
71+
add_hl();
72+
for (unsigned int i = 0; i < m_array_items.size(); i++)
73+
{
74+
add_entry(i, m_array_items.at(i));
75+
}
76+
add_hl();
77+
add_textfield(_("Text"), &m_text);
78+
add_entry(-2, str(boost::format(_("Selected item: %s")) % (m_selected_item >= 0 ? m_array_items[m_selected_item] : _("None"))));
79+
add_entry(-3, _("Add"));
80+
add_entry(-4, _("Insert"));
81+
add_entry(-5, _("Update"));
82+
add_entry(-6, _("Delete"));
83+
add_hl();
84+
add_back(_("OK"));
85+
}
86+
/* EOF */

0 commit comments

Comments
 (0)