menu_script.cpp 2.6 KB

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