LineInput.hxx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #pragma once
  2. #ifndef SNAKE_LINEINPUT_HXX
  3. #define SNAKE_LINEINPUT_HXX
  4. #include <chrono>
  5. #include "../../NonCopyable.hxx"
  6. #include "../AssetManager.hxx"
  7. class LineInput final : private NonCopyable {
  8. public:
  9. static int constexpr MIN_WIDTH = 16;
  10. static int constexpr MIN_HEIGHT = 15;
  11. static int constexpr MAX_CHARACTERS = 40;
  12. LineInput(int x, int y, int w, int h, std::string value = "");
  13. void on_event(SDL_Event const& evt);
  14. void update(std::chrono::milliseconds delta_time);
  15. void render(SDLRenderer& renderer);
  16. void move(int x, int y);
  17. void resize(int w, int h);
  18. [[nodiscard]] SDL_Rect get_bounding_box() const;
  19. // this does not remove the focus from other inputs!
  20. void set_focus(bool focus);
  21. [[nodiscard]] bool has_focus() const;
  22. void set_visible(bool visible);
  23. [[nodiscard]] bool is_visible() const;
  24. void set_value(std::string value);
  25. [[nodiscard]]char const* value() const;
  26. private:
  27. std::string value_;
  28. int x_, y_, w_, h_;
  29. bool focus_;
  30. bool visible_;
  31. Asset<SDL_Texture*> texture_;
  32. Asset<TTF_Font*> font_;
  33. std::chrono::milliseconds blink_timer_{0};
  34. };
  35. #endif // SNAKE_LINEINPUT_HXX