Button.hxx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 "../../NonCopyable.hxx"
  9. #include "UiColor.hxx"
  10. #include "../AssetManager.hxx"
  11. class Button final : private NonCopyable {
  12. public:
  13. static int constexpr MIN_WIDTH = 12;
  14. static int constexpr MIN_HEIGHT = 14;
  15. Button(int x, int y, int w, int h, UiColor color = UiColor::Grey);
  16. void set_title(std::string const& title);
  17. [[nodiscard]] std::string const& title() const;
  18. void set_pressed(bool pressed);
  19. [[nodiscard]] bool is_pressed() const;
  20. void set_visible(bool visible);
  21. [[nodiscard]] bool is_visible() const;
  22. void update();
  23. void render(SDLRenderer& renderer);
  24. void trigger();
  25. void set_on_click(std::function<void()> handler);
  26. void move(int x, int y);
  27. void resize(int w, int h);
  28. [[nodiscard]] SDL_Rect get_bounding_box() const;
  29. private:
  30. std::string title_{};
  31. int x_, y_, w_, h_;
  32. bool pressed_;
  33. bool visible_;
  34. std::function<void()> on_click_;
  35. Asset<SDL_Texture*> up_;
  36. Asset<SDL_Texture*> down_;
  37. Asset<TTF_Font*> font_;
  38. };
  39. #endif // SNAKE_BUTTON_HXX