item_textfield.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SuperTux
  2. // Copyright (C) 2015 Hume2 <teratux.mail@gmail.com>
  3. // 2022 Vankata453
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #ifndef HEADER_SUPERTUX_GUI_ITEM_TEXTFIELD_HPP
  18. #define HEADER_SUPERTUX_GUI_ITEM_TEXTFIELD_HPP
  19. #include "gui/menu_item.hpp"
  20. class ItemTextField : public MenuItem
  21. {
  22. public:
  23. ItemTextField(const std::string& text_, std::string* input_, int id_ = -1);
  24. /** Draws the menu item. */
  25. virtual void draw(DrawingContext&, const Vector& pos, int menu_width, bool active) override;
  26. /** Returns the minimum width of the menu item. */
  27. virtual int get_width() const override;
  28. /** Processes the menu action. */
  29. virtual void process_action(const MenuAction& action) override;
  30. std::string* input;
  31. void change_input(const std::string& input_) {
  32. *input = input_;
  33. m_input_undo.clear();
  34. m_input_redo.clear();
  35. }
  36. /** Calls when the user wants to remove an invalid char. */
  37. virtual void invalid_remove() {}
  38. /** Processes the given event. */
  39. virtual void event(const SDL_Event& ev) override;
  40. /** Indicates that this item changes its width. */
  41. virtual bool changes_width() const override {
  42. return true;
  43. }
  44. /** Updates undo and redo status. */
  45. virtual void update_undo();
  46. /** Calls when the input gets updated. */
  47. virtual void on_input_update() {}
  48. // Text manipulation and navigation functions
  49. virtual void insert_text(const std::string& text, const int left_offset_pos);
  50. virtual void clear();
  51. virtual void go_left();
  52. virtual void go_right();
  53. virtual void go_to_beginning();
  54. virtual void go_to_end();
  55. virtual void delete_front();
  56. virtual void delete_back();
  57. virtual void cut();
  58. virtual void copy();
  59. virtual void paste();
  60. virtual void undo();
  61. virtual void redo();
  62. protected:
  63. std::string m_input_undo;
  64. std::string m_input_redo;
  65. const std::string m_cursor;
  66. float m_cursor_width;
  67. int m_cursor_left_offset;
  68. private:
  69. ItemTextField(const ItemTextField&) = delete;
  70. ItemTextField& operator=(const ItemTextField&) = delete;
  71. };
  72. #endif
  73. /* EOF */