forked from mitsuba-renderer/nanogui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvscrollpanel.cpp
166 lines (135 loc) · 5.55 KB
/
vscrollpanel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
src/vscrollpanel.cpp -- Adds a vertical scrollbar around a widget
that is too big to fit into a certain area
NanoGUI was developed by Wenzel Jakob <[email protected]>.
The widget drawing code is based on the NanoVG demo application
by Mikko Mononen.
All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE.txt file.
*/
#include <nanogui/vscrollpanel.h>
#include <nanogui/theme.h>
#include <nanogui/opengl.h>
NAMESPACE_BEGIN(nanogui)
VScrollPanel::VScrollPanel(Widget *parent)
: Widget(parent), m_child_preferred_height(0),
m_scroll(0.f), m_update_layout(false) { }
void VScrollPanel::perform_layout(NVGcontext *ctx) {
Widget::perform_layout(ctx);
if (m_children.empty())
return;
if (m_children.size() > 1)
throw std::runtime_error("VScrollPanel should have one child.");
Widget *child = m_children[0];
m_child_preferred_height = child->preferred_size(ctx).y();
if (m_child_preferred_height > m_size.y()) {
child->set_position(Vector2i(0, -m_scroll * (m_child_preferred_height - m_size.y())));
child->set_size(Vector2i(m_size.x() - 12, m_child_preferred_height));
} else {
child->set_position(Vector2i(0));
child->set_size(m_size);
m_scroll = 0;
}
child->perform_layout(ctx);
}
Vector2i VScrollPanel::preferred_size(NVGcontext *ctx) const {
if (m_children.empty())
return Vector2i(0);
return m_children[0]->preferred_size(ctx) + Vector2i(12, 0);
}
bool VScrollPanel::mouse_drag_event(const Vector2i &p, const Vector2i &rel,
int button, int modifiers) {
if (!m_children.empty() && m_child_preferred_height > m_size.y()) {
float scrollh = height() *
std::min(1.f, height() / (float) m_child_preferred_height);
m_scroll = std::max(0.f, std::min(1.f,
m_scroll + rel.y() / (m_size.y() - 8.f - scrollh)));
m_update_layout = true;
return true;
} else {
return Widget::mouse_drag_event(p, rel, button, modifiers);
}
}
bool VScrollPanel::mouse_button_event(const Vector2i &p, int button, bool down,
int modifiers) {
if (Widget::mouse_button_event(p, button, down, modifiers))
return true;
if (down && button == GLFW_MOUSE_BUTTON_1 && !m_children.empty() &&
m_child_preferred_height > m_size.y() &&
p.x() > m_pos.x() + m_size.x() - 13 &&
p.x() < m_pos.x() + m_size.x() - 4) {
int scrollh = (int) (height() *
std::min(1.f, height() / (float) m_child_preferred_height));
int start = (int) (m_pos.y() + 4 + 1 + (m_size.y() - 8 - scrollh) * m_scroll);
float delta = 0.f;
if (p.y() < start)
delta = -m_size.y() / (float) m_child_preferred_height;
else if (p.y() > start + scrollh)
delta = m_size.y() / (float) m_child_preferred_height;
m_scroll = std::max(0.f, std::min(1.f, m_scroll + delta*0.98f));
m_children[0]->set_position(
Vector2i(0, -m_scroll * (m_child_preferred_height - m_size.y())));
m_update_layout = true;
return true;
}
return false;
}
bool VScrollPanel::scroll_event(const Vector2i &p, const Vector2f &rel) {
if (!m_children.empty() && m_child_preferred_height > m_size.y()) {
auto child = m_children[0];
float scroll_amount = rel.y() * m_size.y() * .25f;
m_scroll = std::max(0.f, std::min(1.f,
m_scroll - scroll_amount / m_child_preferred_height));
Vector2i old_pos = child->position();
child->set_position(Vector2i(0, -m_scroll*(m_child_preferred_height - m_size.y())));
Vector2i new_pos = child->position();
m_update_layout = true;
child->mouse_motion_event(p-m_pos, old_pos - new_pos, 0, 0);
return true;
} else {
return Widget::scroll_event(p, rel);
}
}
void VScrollPanel::draw(NVGcontext *ctx) {
if (m_children.empty())
return;
Widget *child = m_children[0];
int yoffset = 0;
if (m_child_preferred_height > m_size.y())
yoffset = -m_scroll*(m_child_preferred_height - m_size.y());
child->set_position(Vector2i(0, yoffset));
m_child_preferred_height = child->preferred_size(ctx).y();
float scrollh = height() *
std::min(1.f, height() / (float) m_child_preferred_height);
if (m_update_layout) {
m_update_layout = false;
child->perform_layout(ctx);
}
nvgSave(ctx);
nvgTranslate(ctx, m_pos.x(), m_pos.y());
nvgIntersectScissor(ctx, 0, 0, m_size.x(), m_size.y());
if (child->visible())
child->draw(ctx);
nvgRestore(ctx);
if (m_child_preferred_height <= m_size.y())
return;
NVGpaint paint = nvgBoxGradient(
ctx, m_pos.x() + m_size.x() - 12 + 1, m_pos.y() + 4 + 1, 8,
m_size.y() - 8, 3, 4, Color(0, 32), Color(0, 92));
nvgBeginPath(ctx);
nvgRoundedRect(ctx, m_pos.x() + m_size.x() - 12, m_pos.y() + 4, 8,
m_size.y() - 8, 3);
nvgFillPaint(ctx, paint);
nvgFill(ctx);
paint = nvgBoxGradient(
ctx, m_pos.x() + m_size.x() - 12 - 1,
m_pos.y() + 4 + (m_size.y() - 8 - scrollh) * m_scroll - 1, 8, scrollh,
3, 4, Color(220, 100), Color(128, 100));
nvgBeginPath(ctx);
nvgRoundedRect(ctx, m_pos.x() + m_size.x() - 12 + 1,
m_pos.y() + 4 + 1 + (m_size.y() - 8 - scrollh) * m_scroll, 8 - 2,
scrollh - 2, 2);
nvgFillPaint(ctx, paint);
nvgFill(ctx);
}
NAMESPACE_END(nanogui)