ShishGL  1.0
A simple modular cross-platform graphics library
UIFrame.hpp
Go to the documentation of this file.
1 /*============================================================================*/
2 #ifndef SHISHGL_UI_FRAME_HPP
3 #define SHISHGL_UI_FRAME_HPP
4 /*============================================================================*/
5 #include <cmath>
6 
7 #include "UIWindow.hpp"
8 #include "UIScrollbar.hpp"
9 #include "DefaultBehavior.hpp"
10 /*============================================================================*/
11 namespace Sh {
12 
13  class FrameScroller;
14 
15  class UIFrame : public UIWindow {
16  public:
17 
18  explicit UIFrame(const Frame& frame);
19 
20  void setView(const Vector2<double>& pos) {
21 
22  Vector2<double> new_rel_pos {
23  (!std::isnan(pos.x) ? max_rel_pos.x * pos.x : rel_pos.x),
24  (!std::isnan(pos.y) ? max_rel_pos.y * pos.y : rel_pos.y)
25  };
26 
27  for (auto& child : getChildren()) {
28  if (child == v_scrollbar ||
29  child == h_scrollbar) {
30  continue;
31  }
32  child->translate(rel_pos - new_rel_pos);
33  }
34 
35  rel_pos = new_rel_pos;
36  }
37 
38  void fit() {
39 
40  static constexpr double SB_WIDTH = 30;
41 
42  Vector2<double> min_pos = getPos() - rel_pos;
43  Vector2<double> max_pos = min_pos + max_rel_pos;
44 
45  for (auto& child : getChildren()) {
46  min_pos.x = std::min(min_pos.x, child->getPos().x);
47  min_pos.y = std::min(min_pos.y, child->getPos().y);
48  max_pos.x = std::max(max_pos.x, child->getPos().x + child->getSize().x - getSize().x);
49  max_pos.y = std::max(max_pos.y, child->getPos().y + child->getSize().y - getSize().y);
50  }
51 
52  if (min_pos.x < max_pos.x) {
53 
55 
56  h_scrollbar = attach<UIHorizontalScrollbar<FrameScroller>>(
57  Frame{ {0, getSize().y - SB_WIDTH}, {getSize().x - SB_WIDTH, SB_WIDTH} },
58  getSize().x / (max_pos.x - min_pos.x + getSize().x), this
59  );
60  h_scrollbar->applyStyle<UIWindow::NORMAL>(
61  ColorFill{ Color{40, 40, 40, 100} }
62  );
63  h_scrollbar->left_button->applyStyle<UIWindow::NORMAL>(
64  ColorFill{ Color{40, 40, 40, 140} }
65  );
66  h_scrollbar->right_button->applyStyle<UIWindow::NORMAL>(
67  ColorFill{ Color{40, 40, 40, 140} }
68  );
69  h_scrollbar->slider->slider->applyStyle<UIWindow::NORMAL>(
71  );
72 
73  SubscriptionManager::subscribe(h_scrollbar->slider->slider->getBehavior(),
74  this, MOUSE_SCROLL);
75  }
76  if (min_pos.y < max_pos.y) {
77 
79 
80  v_scrollbar = attach<UIVerticalScrollbar<FrameScroller>>(
81  Frame{ {getSize().x - SB_WIDTH, 0}, {SB_WIDTH, getSize().y} },
82  getSize().y / (max_pos.y - min_pos.y + getSize().y), this
83  );
84  v_scrollbar->applyStyle<UIWindow::NORMAL>(
85  ColorFill{ Color{40, 40, 40, 100} }
86  );
87  v_scrollbar->up_button->applyStyle<UIWindow::NORMAL>(
88  ColorFill{ Color{40, 40, 40, 140} }
89  );
90  v_scrollbar->down_button->applyStyle<UIWindow::NORMAL>(
91  ColorFill{ Color{40, 40, 40, 140} }
92  );
93  v_scrollbar->slider->slider->applyStyle<UIWindow::NORMAL>(
95  );
96 
97  SubscriptionManager::subscribe(v_scrollbar->slider->slider->getBehavior(),
98  this, MOUSE_SCROLL);
99  }
100 
101  rel_pos = getPos() - min_pos;
102  max_rel_pos = max_pos - min_pos;
103  }
104 
107 
110 
111  };
112 
113  /*------------------------------------------------------------------------*/
114 
115  class FrameScroller : public ScrollSlidable {
116  public:
117 
120  , target_frame(target_fr)
121  { }
122 
123  void onSlide(const Vector2<double>& pos) override {
124  target_frame->setView(pos);
125  }
126 
127  private:
128 
129  UIFrame* target_frame;
130  };
131 
133  public:
134 
138  MOUSE_SCROLL);
139  }
140 
141  bool onMouseScroll(MouseScrollEvent& event) override {
142  return EventSystem::sendEvent(target<Window>(), event);
143  }
144 
145  };
146 
147 }
148 /*============================================================================*/
149 #endif //SHISHGL_UI_FRAME_HPP
150 /*============================================================================*/
Sh::Window::getPos
const Vector2< double > & getPos() const
Definition: Window.cpp:31
Sh::UIWindow::NORMAL
@ NORMAL
Definition: UIWindow.hpp:20
UIScrollbar.hpp
Sh::Window::detach
Window * detach(Window *child)
Definition: Window.cpp:74
Sh::Window::getChildren
const std::list< Window * > & getChildren()
Definition: Window.hpp:60
Sh::DefaultBehavior
Definition: DefaultBehavior.hpp:14
Sh::EventSystem::SystemEvents
static constexpr Listener * SystemEvents
Definition: EventSystem.hpp:12
Sh::Vector2::x
T x
Definition: Vector2.hpp:10
Sh::Frame
Definition: Frame.hpp:9
Sh::Vector2< double >
Sh::FrameBehavior::onMouseScroll
bool onMouseScroll(MouseScrollEvent &event) override
Definition: UIFrame.hpp:141
Sh::ScrollSlidable
Definition: UIScrollbar.hpp:12
Sh::Vector2::y
T y
Definition: Vector2.hpp:10
Sh::UIHorizontalScrollbar
Definition: UIScrollbar.hpp:101
Sh::UIFrame::rel_pos
Vector2< double > rel_pos
Definition: UIFrame.hpp:108
Sh::Behavior::target
SomeWindow * target() const
Definition: Behavior.hpp:17
Sh::UIVerticalScrollbar
Definition: UIScrollbar.hpp:57
Sh::UIWindow
Definition: UIWindow.hpp:16
DefaultBehavior.hpp
Sh::EventSystem::sendEvent
static bool sendEvent(Listener *sender, Args &&... args)
Definition: EventSystem.ipp:8
Sh::UIFrame::UIFrame
UIFrame(const Frame &frame)
Definition: UIFrame.cpp:9
Sh::Window::getSize
const Vector2< double > & getSize() const
Definition: Window.cpp:27
Sh
Definition: CoreApplication.hpp:10
Sh::FrameScroller
Definition: UIFrame.hpp:115
Sh::FrameBehavior::FrameBehavior
FrameBehavior(UIWindow *target)
Definition: UIFrame.hpp:135
Sh::Color
Definition: Color.hpp:9
Sh::FrameScroller::onSlide
void onSlide(const Vector2< double > &pos) override
Definition: UIFrame.hpp:123
Sh::WindowManager::destroy
static void destroy(Window *window)
Definition: WindowManager.cpp:29
Sh::UIFrame::h_scrollbar
UIHorizontalScrollbar< FrameScroller > * h_scrollbar
Definition: UIFrame.hpp:106
Sh::FrameBehavior
Definition: UIFrame.hpp:132
Sh::UIFrame::v_scrollbar
UIVerticalScrollbar< FrameScroller > * v_scrollbar
Definition: UIFrame.hpp:105
UIWindow.hpp
Sh::MouseScrollEvent
Definition: MouseEvent.hpp:68
Sh::ColorFill
Definition: ColorFill.hpp:11
Sh::UIFrame::setView
void setView(const Vector2< double > &pos)
Definition: UIFrame.hpp:20
Sh::SubscriptionManager::subscribe
static void subscribe(Listener *receiver, Listener *sender, EventMask mask)
Definition: SubscriptionManager.cpp:18
Sh::UIFrame
Definition: UIFrame.hpp:15
Sh::UIFrame::fit
void fit()
Definition: UIFrame.hpp:38
Sh::Color::BLANCHED_ALMOND
static const Color BLANCHED_ALMOND
Definition: Color.hpp:78
Sh::Slidable::frame
Frame frame
Definition: Slidable.hpp:98
Sh::FrameScroller::FrameScroller
FrameScroller(UIWindow *target, const Frame &frame, UIFrame *target_fr)
Definition: UIFrame.hpp:118
Sh::UIFrame::max_rel_pos
Vector2< double > max_rel_pos
Definition: UIFrame.hpp:109