implementation.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifndef IMPLEMENTATION_H
  2. #define IMPLEMENTATION_H
  3. #include <functional>
  4. #include <vector>
  5. #include "interface.hpp"
  6. #include "simple.hpp"
  7. class movable_bounds : public i_movable_bounds<int2>
  8. {
  9. public:
  10. movable_bounds(range2D bounds);
  11. int2 lower() const override;
  12. int2 upper() const override;
  13. movable_bounds& operator+=(const int2& offset) override;
  14. virtual ~movable_bounds() = default;
  15. protected:
  16. movable_bounds(const movable_bounds&) = default;
  17. movable_bounds& operator=(const movable_bounds&) = default;
  18. range2D bounds;
  19. };
  20. class ui_element : public i_interactive, public i_focusable
  21. {
  22. public:
  23. enum class state
  24. {
  25. idle,
  26. pressed,
  27. hover,
  28. disabled
  29. };
  30. ui_element(const ui_element&) = delete; // TODO: remove, when bounds_proxy is replaced with CRTP, meanwhile, copy is dangerous, it should still be explicit though
  31. virtual ~ui_element() = default;
  32. void update(const interactive::event&) noexcept override;
  33. state current_state() const noexcept;
  34. void disable(bool = true) noexcept;
  35. void enable(bool = true) noexcept;
  36. using callback = std::function<void(ui_element&)>;
  37. std::vector<callback> on_click;
  38. std::vector<callback> on_press;
  39. std::vector<callback> on_release;
  40. void drop_focus() noexcept override;
  41. virtual void gain_focus() noexcept;
  42. bool focus() const noexcept override;
  43. bool focus_on(const i_focusable&) noexcept override;
  44. bool focus(direction) noexcept override;
  45. protected:
  46. explicit ui_element(const i_bounds<int2>&);
  47. state current;
  48. private:
  49. // TODO: polymorphic bounds are clearly useless here, can just CRTP this
  50. const i_bounds<int2>& bounds_proxy;
  51. bool _focus;
  52. };
  53. template <typename Range>
  54. class focus_group final : public i_focusable
  55. {
  56. public:
  57. bool focus() const noexcept override;
  58. void drop_focus() noexcept override;
  59. bool focus_on(const i_focusable&) noexcept override;
  60. bool focus(direction) override;
  61. focus_group();
  62. focus_group(Range);
  63. private:
  64. bool focused_in_range() const noexcept;
  65. Range elements;
  66. typename Range::iterator focused_element;
  67. };
  68. template <typename R> focus_group(R) -> focus_group<R>;
  69. using focus_vector = focus_group<std::vector<std::reference_wrapper<i_focusable>>>;
  70. #endif /* end of include guard */