Button.hxx 913 B

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