This repository has been archived by the owner on Nov 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathvscrollpanel.cpp
138 lines (114 loc) · 4.47 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
/*
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 <wenzel.jakob@epfl.ch>.
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>
#include <nanogui/serializer/core.h>
NAMESPACE_BEGIN(nanogui)
VScrollPanel::VScrollPanel(Widget *parent)
: Widget(parent), mChildPreferredHeight(0), mScroll(0.0f), mUpdateLayout(false) { }
void VScrollPanel::performLayout(NVGcontext *ctx) {
Widget::performLayout(ctx);
if (mChildren.empty())
return;
if (mChildren.size() > 1)
throw std::runtime_error("VScrollPanel should have one child.");
Widget *child = mChildren[0];
mChildPreferredHeight = child->preferredSize(ctx).y();
if (mChildPreferredHeight > mSize.y()) {
child->setPosition(Vector2i(0, -mScroll*(mChildPreferredHeight - mSize.y())));
child->setSize(Vector2i(mSize.x()-12, mChildPreferredHeight));
} else {
child->setPosition(Vector2i::Zero());
child->setSize(mSize);
mScroll = 0;
}
child->performLayout(ctx);
}
Vector2i VScrollPanel::preferredSize(NVGcontext *ctx) const {
if (mChildren.empty())
return Vector2i::Zero();
return mChildren[0]->preferredSize(ctx) + Vector2i(12, 0);
}
bool VScrollPanel::mouseDragEvent(const Vector2i &p, const Vector2i &rel,
int button, int modifiers) {
if (!mChildren.empty() && mChildPreferredHeight > mSize.y()) {
float scrollh = height() *
std::min(1.0f, height() / (float)mChildPreferredHeight);
mScroll = std::max((float) 0.0f, std::min((float) 1.0f,
mScroll + rel.y() / (float)(mSize.y() - 8 - scrollh)));
mUpdateLayout = true;
return true;
} else {
return Widget::mouseDragEvent(p, rel, button, modifiers);
}
}
bool VScrollPanel::scrollEvent(const Vector2i &p, const Vector2f &rel) {
if (!mChildren.empty() && mChildPreferredHeight > mSize.y()) {
float scrollAmount = rel.y() * (mSize.y() / 20.0f);
float scrollh = height() *
std::min(1.0f, height() / (float)mChildPreferredHeight);
mScroll = std::max((float) 0.0f, std::min((float) 1.0f,
mScroll - scrollAmount / (float)(mSize.y() - 8 - scrollh)));
mUpdateLayout = true;
return true;
} else {
return Widget::scrollEvent(p, rel);
}
}
void VScrollPanel::draw(NVGcontext *ctx) {
if (mChildren.empty())
return;
Widget *child = mChildren[0];
child->setPosition(Vector2i(0, -mScroll*(mChildPreferredHeight - mSize.y())));
mChildPreferredHeight = child->preferredSize(ctx).y();
float scrollh = height() *
std::min(1.0f, height() / (float) mChildPreferredHeight);
if (mUpdateLayout)
child->performLayout(ctx);
nvgSave(ctx);
nvgTranslate(ctx, mPos.x(), mPos.y());
nvgIntersectScissor(ctx, 0, 0, mSize.x(), mSize.y());
if (child->visible())
child->draw(ctx);
nvgRestore(ctx);
if (mChildPreferredHeight <= mSize.y())
return;
NVGpaint paint = nvgBoxGradient(
ctx, mPos.x() + mSize.x() - 12 + 1, mPos.y() + 4 + 1, 8,
mSize.y() - 8, 3, 4, Color(0, 32), Color(0, 92));
nvgBeginPath(ctx);
nvgRoundedRect(ctx, mPos.x() + mSize.x() - 12, mPos.y() + 4, 8,
mSize.y() - 8, 3);
nvgFillPaint(ctx, paint);
nvgFill(ctx);
paint = nvgBoxGradient(
ctx, mPos.x() + mSize.x() - 12 - 1,
mPos.y() + 4 + (mSize.y() - 8 - scrollh) * mScroll - 1, 8, scrollh,
3, 4, Color(220, 100), Color(128, 100));
nvgBeginPath(ctx);
nvgRoundedRect(ctx, mPos.x() + mSize.x() - 12 + 1,
mPos.y() + 4 + 1 + (mSize.y() - 8 - scrollh) * mScroll, 8 - 2,
scrollh - 2, 2);
nvgFillPaint(ctx, paint);
nvgFill(ctx);
}
void VScrollPanel::save(Serializer &s) const {
Widget::save(s);
s.set("childPreferredHeight", mChildPreferredHeight);
s.set("scroll", mScroll);
}
bool VScrollPanel::load(Serializer &s) {
if (!Widget::load(s)) return false;
if (!s.get("childPreferredHeight", mChildPreferredHeight)) return false;
if (!s.get("scroll", mScroll)) return false;
return true;
}
NAMESPACE_END(nanogui)