Button.hxx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #pragma once
  2. #ifndef SNAKE_BUTTON_HXX
  3. #define SNAKE_BUTTON_HXX
  4. #include <SDL.h>
  5. #include <SDL_ttf.h>
  6. #include <functional>
  7. #include <string>
  8. #include <boost/noncopyable.hpp>
  9. #include "UiColor.hxx"
  10. #include "../AssetManager.hxx"
  11. class Button final : private boost::noncopyable {
  12. public:
  13. static int constexpr MIN_WIDTH = 12;
  14. static int constexpr MIN_HEIGHT = 14;
  15. Button(std::string title, int x, int y, int w, int h, UiColor color = UiColor::Grey);
  16. void set_pressed(bool pressed);
  17. [[nodiscard]] bool is_pressed() const;
  18. void set_visible(bool visible);
  19. [[nodiscard]] bool is_visible() const;
  20. void update();
  21. void render(SDLRenderer& renderer);
  22. void trigger();
  23. void set_on_click(std::function<void()> handler);
  24. void move(int x, int y);
  25. void resize(int w, int h);
  26. [[nodiscard]] SDL_Rect get_bounding_box() const;
  27. private:
  28. std::string title_;
  29. int x_, y_, w_, h_;
  30. bool pressed_;
  31. bool visible_;
  32. std::function<void()> on_click_;
  33. Asset<SDL_Texture*> up_;
  34. Asset<SDL_Texture*> down_;
  35. Asset<TTF_Font*> font_;
  36. };
  37. #endif // SNAKE_BUTTON_HXX