1 | /* |
2 | src/imagepanel.cpp -- Image panel widget which shows a number of |
3 | square-shaped icons |
4 | |
5 | NanoGUI was developed by Wenzel Jakob <wenzel.jakob@epfl.ch>. |
6 | The widget drawing code is based on the NanoVG demo application |
7 | by Mikko Mononen. |
8 | |
9 | All rights reserved. Use of this source code is governed by a |
10 | BSD-style license that can be found in the LICENSE.txt file. |
11 | */ |
12 | |
13 | #include <nanogui/imagepanel.h> |
14 | #include <nanogui/opengl.h> |
15 | |
16 | NAMESPACE_BEGIN(nanogui) |
17 | |
18 | ImagePanel::ImagePanel(Widget *parent) |
19 | : Widget(parent), mThumbSize(64), mSpacing(10), mMargin(10), |
20 | mMouseIndex(-1) {} |
21 | |
22 | Vector2i ImagePanel::gridSize() const { |
23 | int nCols = 1 + std::max(0, |
24 | (int) ((mSize.x() - 2 * mMargin - mThumbSize) / |
25 | (float) (mThumbSize + mSpacing))); |
26 | int nRows = ((int) mImages.size() + nCols - 1) / nCols; |
27 | return Vector2i(nCols, nRows); |
28 | } |
29 | |
30 | int ImagePanel::indexForPosition(const Vector2i &p) const { |
31 | Vector2f pp = ((p - mPos).cast<float>() - Vector2f::Constant(mMargin)) / |
32 | (float)(mThumbSize + mSpacing); |
33 | float iconRegion = mThumbSize / (float)(mThumbSize + mSpacing); |
34 | bool overImage = pp.x() - std::floor(pp.x()) < iconRegion && |
35 | pp.y() - std::floor(pp.y()) < iconRegion; |
36 | Vector2i gridPos = pp.cast<int>(), grid = gridSize(); |
37 | overImage &= ((gridPos.array() >= 0).all() && |
38 | (gridPos.array() < grid.array()).all()); |
39 | return overImage ? (gridPos.x() + gridPos.y() * grid.x()) : -1; |
40 | } |
41 | |
42 | bool ImagePanel::mouseMotionEvent(const Vector2i &p, const Vector2i & /* rel */, |
43 | int /* button */, int /* modifiers */) { |
44 | mMouseIndex = indexForPosition(p); |
45 | return true; |
46 | } |
47 | |
48 | bool ImagePanel::mouseButtonEvent(const Vector2i &p, int /* button */, bool down, |
49 | int /* modifiers */) { |
50 | int index = indexForPosition(p); |
51 | if (index >= 0 && index < (int) mImages.size() && mCallback && down) |
52 | mCallback(index); |
53 | return true; |
54 | } |
55 | |
56 | Vector2i ImagePanel::preferredSize(NVGcontext *) const { |
57 | Vector2i grid = gridSize(); |
58 | return Vector2i( |
59 | grid.x() * mThumbSize + (grid.x() - 1) * mSpacing + 2*mMargin, |
60 | grid.y() * mThumbSize + (grid.y() - 1) * mSpacing + 2*mMargin |
61 | ); |
62 | } |
63 | |
64 | void ImagePanel::draw(NVGcontext* ctx) { |
65 | Vector2i grid = gridSize(); |
66 | |
67 | for (size_t i=0; i<mImages.size(); ++i) { |
68 | Vector2i p = mPos + Vector2i::Constant(mMargin) + |
69 | Vector2i((int) i % grid.x(), (int) i / grid.x()) * (mThumbSize + mSpacing); |
70 | int imgw, imgh; |
71 | |
72 | nvgImageSize(ctx, mImages[i].first, &imgw, &imgh); |
73 | float iw, ih, ix, iy; |
74 | if (imgw < imgh) { |
75 | iw = mThumbSize; |
76 | ih = iw * (float)imgh / (float)imgw; |
77 | ix = 0; |
78 | iy = -(ih - mThumbSize) * 0.5f; |
79 | } else { |
80 | ih = mThumbSize; |
81 | iw = ih * (float)imgw / (float)imgh; |
82 | ix = -(iw - mThumbSize) * 0.5f; |
83 | iy = 0; |
84 | } |
85 | |
86 | NVGpaint imgPaint = nvgImagePattern( |
87 | ctx, p.x() + ix, p.y()+ iy, iw, ih, 0, mImages[i].first, |
88 | mMouseIndex == (int)i ? 1.0 : 0.7); |
89 | |
90 | nvgBeginPath(ctx); |
91 | nvgRoundedRect(ctx, p.x(), p.y(), mThumbSize, mThumbSize, 5); |
92 | nvgFillPaint(ctx, imgPaint); |
93 | nvgFill(ctx); |
94 | |
95 | NVGpaint shadowPaint = |
96 | nvgBoxGradient(ctx, p.x() - 1, p.y(), mThumbSize + 2, mThumbSize + 2, 5, 3, |
97 | nvgRGBA(0, 0, 0, 128), nvgRGBA(0, 0, 0, 0)); |
98 | nvgBeginPath(ctx); |
99 | nvgRect(ctx, p.x()-5,p.y()-5, mThumbSize+10,mThumbSize+10); |
100 | nvgRoundedRect(ctx, p.x(),p.y(), mThumbSize,mThumbSize, 6); |
101 | nvgPathWinding(ctx, NVG_HOLE); |
102 | nvgFillPaint(ctx, shadowPaint); |
103 | nvgFill(ctx); |
104 | |
105 | nvgBeginPath(ctx); |
106 | nvgRoundedRect(ctx, p.x()+0.5f,p.y()+0.5f, mThumbSize-1,mThumbSize-1, 4-0.5f); |
107 | nvgStrokeWidth(ctx, 1.0f); |
108 | nvgStrokeColor(ctx, nvgRGBA(255,255,255,80)); |
109 | nvgStroke(ctx); |
110 | } |
111 | } |
112 | |
113 | NAMESPACE_END(nanogui) |
114 | |