ShishGL  1.0
A simple modular cross-platform graphics library
Slidable.hpp
Go to the documentation of this file.
1 /*============================================================================*/
2 #ifndef SHISHGL_UI_SLIDER_HPP
3 #define SHISHGL_UI_SLIDER_HPP
4 /*============================================================================*/
5 #include "Draggable.hpp"
6 #include "Segment2.hpp"
7 #include "Event.hpp"
8 /*============================================================================*/
9 namespace Sh {
10 
11  class Slidable : public Draggable {
12  public:
13 
15  const Frame& slide_frame)
16  : Draggable(target)
17  , frame(slide_frame)
18  , parent_offset({})
19  { }
20 
21  void onTargetUpdate() override {
22  auto parent = target<UIWindow>()->getParent();
23  if (parent) {
25  parent_offset = parent->getPos();
27  }
28  }
29 
30  bool onMouseButton(MouseButtonEvent& event) override {
31 
32  if (target<UIWindow>()->contains(event.where()) ||
33  event.state() == Mouse::UP) {
34  return Draggable::onMouseButton(event);
35  }
36 
37  if (frame.contains(event.where())) {
38 
39  slide(
40  event.where() -
41  0.5 * target<UIWindow>()->getSize() -
42  target<UIWindow>()->getPos()
43  );
44 
45  return true;
46  }
47 
48  return false;
49  }
50 
51  bool onMouseMove(MouseEvent& event) override {
52 
53  Holdable::onMouseMove(event);
54 
55  if (isHeld()) {
56  slide(event.where() - dragPoint());
57  }
58 
59  return true;
60  }
61 
62  void onDrag(const Vector2<double>&) final {
63 
64  Vector2<double> pos = target<UIWindow>()->getPos();
65  Vector2<double> size = target<UIWindow>()->getSize();
66 
68  (pos.x - frame.pos.x) / (frame.size.x - size.x),
69  (pos.y - frame.pos.y) / (frame.size.y - size.y)
70  });
71  }
72 
73  virtual void onSlide(const Vector2<double>&) {}
74 
75  protected:
76 
77  void slide(const Vector2<double>& delta) {
78 
79  if (!isHeld()) {
80  ++n_held;
81  }
82 
83  Vector2<double> new_pos = target<UIWindow>()->getPos() + delta;
84  Vector2<double> size = target<UIWindow>()->getSize();
85 
86  new_pos.x = std::max(
87  frame.pos.x,
88  std::min(frame.pos.x + frame.size.x - size.x, new_pos.x)
89  );
90  new_pos.y = std::max(
91  frame.pos.y,
92  std::min(frame.pos.y + frame.size.y - size.y, new_pos.y)
93  );
94 
95  Draggable::drag(new_pos - target<UIWindow>()->getPos());
96  }
97 
100  };
101 
102 
103 
104  /*
105  class Slidable : public Draggable {
106  public:
107 
108  explicit Slidable(UIWindow* target,
109  const Segment2<double>& slide);
110 
111  bool onMouseMove(MouseEvent& event) override;
112 
113  void slide(const Vector2<double>& delta, Mouse::Button button);
114 
115  void slide(double delta);
116 
117  void setSegment(const Segment2<double>& segment) {
118  slide_seg = segment;
119  }
120 
121  virtual void onSlide(double value) { }
122 
123  protected:
124 
125  Segment2<double> slide_seg;
126  };
127 
128  class SlideListener : public Behavior {
129  public:
130 
131  explicit SlideListener(UIWindow* target)
132  : Behavior(target)
133  { }
134 
135  virtual bool onSlide(class SlideEvent& event) = 0;
136 
137  };
138 
139  class SlideEvent : public Event {
140  public:
141 
142  explicit SlideEvent(const Vector2<double>& delta)
143  : Event()
144  , slide_delta(delta)
145  { }
146 
147  bool happen(Listener* listener) override {
148  return dynamic_cast<SlideListener*>(listener)->onSlide(*this);
149  }
150 
151  EventMask mask() override {
152  return SLIDE;
153  }
154 
155  [[nodiscard]]
156  const Vector2<double>& delta() const {
157  return slide_delta;
158  }
159 
160  private:
161 
162  Vector2<double> slide_delta;
163 
164  };
165  */
166 }
167 /*============================================================================*/
168 #endif //SHISHGL_UI_SLIDER_HPP
169 /*============================================================================*/
Sh::Slidable
Definition: Slidable.hpp:11
Sh::MouseEvent
Definition: MouseEvent.hpp:12
Sh::Draggable::onMouseButton
bool onMouseButton(MouseButtonEvent &event) override
Definition: Draggable.cpp:18
Sh::Vector2::x
T x
Definition: Vector2.hpp:10
Sh::Frame
Definition: Frame.hpp:9
Sh::Holdable::isHeld
bool isHeld() const
Definition: Clickable.cpp:57
Sh::Draggable
Definition: Draggable.hpp:10
Sh::Draggable::dragPoint
const Vector2< double > & dragPoint() const
Definition: Draggable.cpp:12
Sh::Vector2< double >
Sh::Slidable::onSlide
virtual void onSlide(const Vector2< double > &)
Definition: Slidable.hpp:73
Sh::Slidable::onTargetUpdate
void onTargetUpdate() override
Definition: Slidable.hpp:21
Sh::Vector2::y
T y
Definition: Vector2.hpp:10
Sh::Behavior::target
SomeWindow * target() const
Definition: Behavior.hpp:17
Sh::MouseButtonEvent::state
Mouse::ButtonState state() const
Definition: MouseEvent.cpp:47
Sh::Slidable::onMouseMove
bool onMouseMove(MouseEvent &event) override
Definition: Slidable.hpp:51
Event.hpp
Segment2.hpp
Sh::Frame::contains
bool contains(const Vector2< double > &point) const
Definition: Frame.hpp:20
Sh::UIWindow
Definition: UIWindow.hpp:16
Sh::Draggable::drag
void drag(const Vector2< double > &delta)
Definition: Draggable.cpp:43
Sh::Slidable::onMouseButton
bool onMouseButton(MouseButtonEvent &event) override
Definition: Slidable.hpp:30
Sh
Definition: CoreApplication.hpp:10
Sh::Slidable::onDrag
void onDrag(const Vector2< double > &) final
Definition: Slidable.hpp:62
Sh::MouseEvent::where
const Vector2< double > & where() const
Definition: MouseEvent.cpp:13
Draggable.hpp
Sh::Holdable::n_held
uint8_t n_held
Definition: Clickable.hpp:45
Sh::Slidable::parent_offset
Vector2< double > parent_offset
Definition: Slidable.hpp:99
Sh::Slidable::Slidable
Slidable(UIWindow *target, const Frame &slide_frame)
Definition: Slidable.hpp:14
Sh::Mouse::UP
@ UP
Definition: Mouse.hpp:12
Sh::Frame::size
Vector2< double > size
Definition: Frame.hpp:12
Sh::Slidable::slide
void slide(const Vector2< double > &delta)
Definition: Slidable.hpp:77
Sh::Frame::pos
Vector2< double > pos
Definition: Frame.hpp:11
Sh::Slidable::frame
Frame frame
Definition: Slidable.hpp:98
Sh::MouseButtonEvent
Definition: MouseEvent.hpp:38
Sh::DefaultBehavior::onMouseMove
bool onMouseMove(MouseEvent &event) override
Definition: DefaultBehavior.cpp:23