1 | // SuperTux |
---|---|
2 | // Copyright (C) 2016 Hume2 <teratux.mail@gmail.com> |
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_script.hpp" |
18 | |
19 | #include "gui/item_script_line.hpp" |
20 | #include "util/gettext.hpp" |
21 | |
22 | ScriptMenu::ScriptMenu(std::string* script_) : |
23 | base_script(script_), |
24 | script_strings() |
25 | { |
26 | script_strings.clear(); |
27 | |
28 | add_label(_("Edit script")); |
29 | add_hl(); |
30 | |
31 | // Split the script to the lines. |
32 | std::string script = *base_script; |
33 | std::string line_break = "\n"; |
34 | std::string new_line; |
35 | size_t endl_pos = script.find(line_break); |
36 | while (endl_pos != std::string::npos) { |
37 | new_line = script.substr(0, endl_pos); |
38 | script = script.substr(endl_pos + line_break.length()); |
39 | push_string(new_line); |
40 | endl_pos = script.find(line_break); |
41 | } |
42 | push_string(script); |
43 | |
44 | //add_script_line(base_script); |
45 | |
46 | add_hl(); |
47 | add_back(_("OK")); |
48 | } |
49 | |
50 | ScriptMenu::~ScriptMenu() |
51 | { |
52 | *base_script = *(script_strings[0]); |
53 | for (auto i = script_strings.begin()+1; i != script_strings.end(); ++i) { |
54 | *base_script += "\n"+ **i; |
55 | } |
56 | } |
57 | |
58 | void |
59 | ScriptMenu::push_string(const std::string& new_line) |
60 | { |
61 | script_strings.push_back(std::make_unique<std::string>(new_line)); |
62 | add_script_line( (script_strings.end()-1)->get() ); |
63 | } |
64 | |
65 | void |
66 | ScriptMenu::remove_line() { |
67 | // The script should have at least one line. |
68 | if (script_strings.size() <= 1) { |
69 | return; |
70 | } |
71 | |
72 | script_strings.erase(script_strings.begin() + (m_active_item - 2)); |
73 | delete_item(m_active_item); |
74 | } |
75 | |
76 | void |
77 | ScriptMenu::add_line() { |
78 | auto new_line = std::make_unique<std::string>(); |
79 | script_strings.insert(script_strings.begin() + (m_active_item - 1), move(new_line)); |
80 | |
81 | auto line_item = std::unique_ptr<ItemScriptLine>( |
82 | new ItemScriptLine( (script_strings.begin()+(m_active_item-1))->get() )); |
83 | add_item(std::move(line_item), m_active_item+1); |
84 | m_active_item++; |
85 | } |
86 | |
87 | void |
88 | ScriptMenu::menu_action(MenuItem& item) |
89 | { |
90 | |
91 | } |
92 | |
93 | bool |
94 | ScriptMenu::is_sensitive() const { |
95 | return true; |
96 | } |
97 | |
98 | /* EOF */ |
99 |