Skip to content

Commit 21ab026

Browse files
committed
Fix array rendering inaccuracies, fix array width message
1 parent 60e42c4 commit 21ab026

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

Libraries/nanovg

Source/Objects/ArrayObject.h

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class GraphicalArray final : public Component
3232
bool visible = true;
3333
bool arrayNeedsUpdate = true;
3434
Path arrayPath;
35+
NVGCachedPath cachedPath;
3536

3637
std::function<void()> reloadGraphs = [] { };
3738

@@ -64,7 +65,7 @@ class GraphicalArray final : public Component
6465
pd->unregisterMessageListener(this);
6566
}
6667

67-
static Path createArrayPath(HeapArray<float> points, DrawType style, StackArray<float, 2> scale, float const width, float const height)
68+
static Path createArrayPath(HeapArray<float> points, DrawType style, StackArray<float, 2> scale, float const width, float const height, float const lineWidth)
6869
{
6970
bool invert = false;
7071
if (scale[0] >= scale[1]) {
@@ -76,8 +77,8 @@ class GraphicalArray final : public Component
7677
if (points.size() <= 4 && style == Curve)
7778
style = Polygon;
7879

79-
float const dh = (height - 2) / (scale[1] - scale[0]);
80-
float const invh = invert ? 0 : (height - 2);
80+
float const dh = (height - (lineWidth + 1)) / (scale[1] - scale[0]);
81+
float const invh = invert ? 0 : (height - (lineWidth + 1));
8182
float const yscale = invert ? -1.0f : 1.0f;
8283

8384
auto yToCoords = [dh, invh, scale, yscale](float y){
@@ -90,11 +91,11 @@ class GraphicalArray final : public Component
9091
StackArray<float, 6> control = {0};
9192
Path result;
9293
if (std::isfinite(pointPtr[0])) {
93-
result.startNewSubPath(0, pointPtr[0]);
94+
result.startNewSubPath(0, yToCoords(pointPtr[0]));
9495
}
9596

9697
int onset = 0;
97-
int lastX = 0;
98+
float lastX = 0;
9899
if(style == Curve)
99100
{
100101
onset = 2;
@@ -109,35 +110,29 @@ class GraphicalArray final : public Component
109110
switch (style) {
110111
case Points: {
111112
float const xIncrement = width / numPoints;
112-
int nextX = std::round(static_cast<float>(i + 1) / numPoints * width);
113+
float nextX = static_cast<float>(i + 1) / numPoints * width;
113114
float y = yToCoords(pointPtr[0]);
114115
minY = std::min(y, minY);
115116
maxY = std::max(y, maxY);
116117

117-
if (i == 0 || i == numPoints-1 || nextX != lastX)
118+
if (i == 0 || i == numPoints-1 || std::abs(nextX - lastX) >= 1.0f)
118119
{
119-
if(xIncrement < 1.0f) {
120-
result.addRectangle(lastX - 0.75f, minY, (nextX - lastX) + 1.5f, std::max((maxY - minY), 1.0f));
121-
}
122-
else {
123-
result.addRectangle(lastX, minY, (nextX - lastX), (maxY - minY) + 1.0f);
124-
}
125-
120+
result.addRectangle(lastX - 0.33f, minY, (nextX - lastX) + 0.33f, std::max((maxY - minY), lineWidth));
126121
lastX = nextX;
127122
minY = 1e20;
128123
maxY = -1e20;
129124
}
130125
break;
131126
}
132127
case Polygon: {
133-
int nextX = std::round(static_cast<float>(i + 1) / (numPoints - 1) * width);
134-
if (i == 0 || i == numPoints-2 || nextX != lastX) {
128+
float nextX = static_cast<float>(i) / (numPoints - 1) * width;
129+
if (i != 0 || i == numPoints-1 || std::abs(nextX - lastX) >= 1.0f) {
135130
float y1 = yToCoords(pointPtr[0]);
136131
if (std::isfinite(y1)) {
137-
result.lineTo(lastX, y1);
132+
result.lineTo(nextX, y1);
138133
}
139134

140-
if (i == numPoints-2) {
135+
if (i == numPoints-1) {
141136
float y2 = yToCoords(pointPtr[1]);
142137
if (std::isfinite(y2)) {
143138
result.lineTo(nextX, y2);
@@ -148,8 +143,8 @@ class GraphicalArray final : public Component
148143
break;
149144
}
150145
case Curve: {
151-
int nextX = std::round(static_cast<float>(i) / (numPoints - 1) * width);
152-
if(nextX == lastX && i != 0 && i != numPoints-1)
146+
float nextX = static_cast<float>(i) / (numPoints - 1) * width;
147+
if(std::abs(nextX - lastX) < 1.0f && i != 0 && i != numPoints-1)
153148
continue;
154149

155150
float y1 = yToCoords(pointPtr[0]);
@@ -200,7 +195,7 @@ class GraphicalArray final : public Component
200195
if(arrayNeedsUpdate)
201196
{
202197
if(vec.not_empty()) {
203-
arrayPath = createArrayPath(vec, static_cast<DrawType>(getValue<int>(drawMode) - 1), getScale(), getWidth(), getHeight());
198+
arrayPath = createArrayPath(vec, static_cast<DrawType>(getValue<int>(drawMode) - 1), getScale(), getWidth(), getHeight(), getLineWidth());
204199
}
205200
arrayNeedsUpdate = false;
206201
}
@@ -217,26 +212,44 @@ class GraphicalArray final : public Component
217212
if(arrayNeedsUpdate)
218213
{
219214
if(vec.not_empty()) {
220-
arrayPath = createArrayPath(vec, arrDrawMode, getScale(), getWidth(), getHeight());
215+
arrayPath = createArrayPath(vec, arrDrawMode, getScale(), getWidth(), getHeight(), getLineWidth());
221216
}
217+
cachedPath.clear();
222218
arrayNeedsUpdate = false;
223219
}
224220
NVGScopedState scopedState(nvg);
225221
auto const arrB = getLocalBounds().reduced(1);
226222
nvgIntersectRoundedScissor(nvg, arrB.getX(), arrB.getY(), arrB.getWidth(), arrB.getHeight(), Corners::objectCornerRadius);
227223

224+
if(cachedPath.isValid())
225+
{
226+
auto const contentColour = getContentColour();
227+
if(arrDrawMode == Points) {
228+
nvgFillColor(nvg, nvgRGBA(contentColour.getRed(), contentColour.getGreen(), contentColour.getBlue(), contentColour.getAlpha()));
229+
cachedPath.fill();
230+
}
231+
else {
232+
nvgStrokeColor(nvg, nvgRGBA(contentColour.getRed(), contentColour.getGreen(), contentColour.getBlue(), contentColour.getAlpha()));
233+
nvgStrokeWidth(nvg, getLineWidth());
234+
cachedPath.stroke();
235+
}
236+
return;
237+
}
238+
228239
if (vec.not_empty()) {
229240
setJUCEPath(nvg, arrayPath);
230241

231242
auto const contentColour = getContentColour();
232243
if(arrDrawMode == Points) {
233244
nvgFillColor(nvg, nvgRGBA(contentColour.getRed(), contentColour.getGreen(), contentColour.getBlue(), contentColour.getAlpha()));
234245
nvgFill(nvg);
246+
cachedPath.save(nvg);
235247
}
236248
else {
237249
nvgStrokeColor(nvg, nvgRGBA(contentColour.getRed(), contentColour.getGreen(), contentColour.getBlue(), contentColour.getAlpha()));
238250
nvgStrokeWidth(nvg, getLineWidth());
239251
nvgStroke(nvg);
252+
cachedPath.save(nvg);
240253
}
241254
}
242255
}
@@ -277,8 +290,9 @@ class GraphicalArray final : public Component
277290
}
278291
case hash("width"): {
279292
MessageManager::callAsync([_this = SafePointer(this)] {
280-
if (_this)
281-
_this->repaint();
293+
if (_this) {
294+
_this->updateArrayPath();
295+
}
282296
});
283297
break;
284298
}

0 commit comments

Comments
 (0)