1 | /* |
2 | src/progressbar.cpp -- Standard widget for visualizing progress |
3 | |
4 | NanoGUI was developed by Wenzel Jakob <wenzel.jakob@epfl.ch>. |
5 | The widget drawing code is based on the NanoVG demo application |
6 | by Mikko Mononen. |
7 | |
8 | All rights reserved. Use of this source code is governed by a |
9 | BSD-style license that can be found in the LICENSE.txt file. |
10 | */ |
11 | |
12 | #include <nanogui/progressbar.h> |
13 | #include <nanogui/opengl.h> |
14 | #include <nanogui/serializer/core.h> |
15 | |
16 | NAMESPACE_BEGIN(nanogui) |
17 | |
18 | ProgressBar::ProgressBar(Widget *parent) |
19 | : Widget(parent), mValue(0.0f) {} |
20 | |
21 | Vector2i ProgressBar::preferredSize(NVGcontext *) const { |
22 | return Vector2i(70, 12); |
23 | } |
24 | |
25 | void ProgressBar::draw(NVGcontext* ctx) { |
26 | Widget::draw(ctx); |
27 | |
28 | NVGpaint paint = nvgBoxGradient( |
29 | ctx, mPos.x() + 1, mPos.y() + 1, |
30 | mSize.x()-2, mSize.y(), 3, 4, Color(0, 32), Color(0, 92)); |
31 | nvgBeginPath(ctx); |
32 | nvgRoundedRect(ctx, mPos.x(), mPos.y(), mSize.x(), mSize.y(), 3); |
33 | nvgFillPaint(ctx, paint); |
34 | nvgFill(ctx); |
35 | |
36 | float value = std::min(std::max(0.0f, mValue), 1.0f); |
37 | int barPos = (int) std::round((mSize.x() - 2) * value); |
38 | |
39 | paint = nvgBoxGradient( |
40 | ctx, mPos.x(), mPos.y(), |
41 | barPos+1.5f, mSize.y()-1, 3, 4, |
42 | Color(220, 100), Color(128, 100)); |
43 | |
44 | nvgBeginPath(ctx); |
45 | nvgRoundedRect( |
46 | ctx, mPos.x()+1, mPos.y()+1, |
47 | barPos, mSize.y()-2, 3); |
48 | nvgFillPaint(ctx, paint); |
49 | nvgFill(ctx); |
50 | } |
51 | |
52 | void ProgressBar::save(Serializer &s) const { |
53 | Widget::save(s); |
54 | s.set("value" , mValue); |
55 | } |
56 | |
57 | bool ProgressBar::load(Serializer &s) { |
58 | if (!Widget::load(s)) |
59 | return false; |
60 | if (!s.get("value" , mValue)) |
61 | return false; |
62 | return true; |
63 | } |
64 | |
65 | NAMESPACE_END(nanogui) |
66 | |