interface.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #ifndef INTERFACE_HPP
  2. #define INTERFACE_HPP
  3. #include "simple.hpp"
  4. template <typename T, typename Enabled = std::nullptr_t>
  5. struct has_offset_type : std::false_type {};
  6. template <typename T>
  7. struct has_offset_type<T, decltype(std::declval<T::offset_type>(), nullptr)> :
  8. std::true_type {};
  9. template <typename Offset>
  10. class i_movable
  11. {
  12. protected:
  13. ~i_movable() = default;
  14. public:
  15. using offset_type = Offset;
  16. virtual i_movable& operator+=(const Offset&) = 0;
  17. };
  18. template <typename Offset>
  19. i_movable<Offset>& operator+=(i_movable<Offset>& self, const Offset& offset)
  20. {
  21. return self += offset;
  22. }
  23. template <typename Bound>
  24. class i_bounds
  25. {
  26. protected:
  27. ~i_bounds() = default;
  28. public:
  29. using bound_type = Bound;
  30. virtual Bound lower() const = 0;
  31. virtual Bound upper() const = 0;
  32. };
  33. template <typename Bound>
  34. Bound lower(const i_bounds<Bound>& self)
  35. { return self.lower(); }
  36. template <typename Bound>
  37. Bound upper(const i_bounds<Bound>& self)
  38. { return self.upper(); }
  39. template <typename T>
  40. class i_movable_bounds : public i_bounds<T>, public i_movable<T>
  41. {
  42. protected:
  43. ~i_movable_bounds() = default;
  44. };
  45. class i_graphic
  46. {
  47. protected:
  48. ~i_graphic() = default;
  49. public:
  50. virtual void draw(const graphical::surface&) = 0;
  51. };
  52. class i_interactive
  53. {
  54. protected:
  55. ~i_interactive() = default;
  56. public:
  57. virtual void update(const interactive::event&) = 0;
  58. };
  59. class i_focusable
  60. {
  61. protected:
  62. ~i_focusable() = default;
  63. public:
  64. enum direction
  65. {
  66. prev = -1,
  67. self = 0,
  68. next = 1
  69. };
  70. virtual bool focus() const = 0;
  71. virtual void drop_focus() = 0;
  72. virtual bool focus(direction) = 0;
  73. virtual bool focus_on(const i_focusable&) = 0;
  74. };
  75. // free function alternatives... double the work double the fun??
  76. // or should these be the only ones
  77. // initial rationale: interface to work with reference wrapper, that implicitly converts to reference
  78. inline bool focus(const i_focusable& self) {return self.focus(); }
  79. inline void drop_focus(i_focusable& self) { self.drop_focus(); }
  80. inline bool focus(i_focusable& self, i_focusable::direction direction)
  81. { return self.focus(direction); }
  82. inline bool focus_on(i_focusable& self, const i_focusable& target) { return self.focus_on(target); }
  83. class i_ui_object
  84. {
  85. public:
  86. virtual ~i_ui_object() = default;
  87. };
  88. #endif /* end of include guard */