Scaled bitmaps paint fix

This commit is contained in:
Bertrand Songis
2020-04-06 16:51:22 +02:00
parent 2b551260f3
commit 7c0d033059
3 changed files with 71 additions and 56 deletions
+49 -33
View File
@@ -389,7 +389,7 @@ class BitmapBuffer: public BitmapBufferBase<uint16_t>
coord_t drawNumber(coord_t x, coord_t y, int32_t val, LcdFlags flags = 0, uint8_t len = 0, const char * prefix = nullptr, const char * suffix = nullptr); coord_t drawNumber(coord_t x, coord_t y, int32_t val, LcdFlags flags = 0, uint8_t len = 0, const char * prefix = nullptr, const char * suffix = nullptr);
template<class T> template<class T>
void drawBitmap(coord_t x, coord_t y, const T * bmp, coord_t srcx = 0, coord_t srcy = 0, coord_t w = 0, coord_t h = 0, float scale = 0) void drawBitmap(coord_t x, coord_t y, const T * bmp, coord_t srcx = 0, coord_t srcy = 0, coord_t srcw = 0, coord_t srch = 0, float scale = 0)
{ {
if (!data || !bmp) if (!data || !bmp)
return; return;
@@ -399,42 +399,58 @@ class BitmapBuffer: public BitmapBufferBase<uint16_t>
if (x >= xmax || y >= ymax) if (x >= xmax || y >= ymax)
return; return;
coord_t srcw = bmp->width(); coord_t bmpw = bmp->width();
coord_t srch = bmp->height(); coord_t bmph = bmp->height();
if (w == 0) if (srcw == 0)
w = srcw; srcw = bmpw;
if (h == 0) if (srch == 0)
h = srch; srch = bmph;
if (srcx + w > srcw) if (srcx + srcw > bmpw)
w = srcw - srcx; srcw = bmpw - srcx;
if (srcy + h > srch) if (srcy + srch > bmph)
h = srch - srcy; srch = bmph - srcy;
if (x < xmin) {
w += x - xmin;
srcx -= x - xmin;
x = xmin;
}
if (y < ymin) {
h += y - ymin;
srcy -= y - ymin;
y = ymin;
}
if (scale == 0) { if (scale == 0) {
if (x + w > xmax) { if (x < xmin) {
w = xmax - x; srcw += x - xmin;
srcx -= x - xmin;
x = xmin;
} }
if (y < ymin) {
if (y + h > ymax) { srch += y - ymin;
h = ymax - y; srcy -= y - ymin;
y = ymin;
}
if (x + srcw > xmax) {
srcw = xmax - x;
}
if (y + srch > ymax) {
srch = ymax - y;
}
}
else {
if (x < xmin) {
srcw += (x - xmin) / scale;
srcx -= (x - xmin) / scale;
x = xmin;
}
if (y < ymin) {
srch += (y - ymin) / scale;
srcy -= (y - ymin) / scale;
y = ymin;
}
if (x + srcw * scale > xmax) {
srcw = (xmax - x) / scale;
}
if (y + srch * scale > ymax) {
srch = (ymax - y) / scale;
} }
} }
if (w <= 0 || h <= 0) if (srcw <= 0 || srch <= 0) {
return; return;
}
// TRACE("x=%d xmin=%d xmax=%d w=%d", x, xmin, xmax, w); // TRACE("x=%d xmin=%d xmax=%d w=%d", x, xmin, xmax, w);
@@ -446,15 +462,15 @@ class BitmapBuffer: public BitmapBufferBase<uint16_t>
h = height - y; h = height - y;
}*/ }*/
if (bmp->getFormat() == BMP_ARGB4444) { if (bmp->getFormat() == BMP_ARGB4444) {
DMACopyAlphaBitmap(data, _width, _height, x, y, bmp->getData(), srcw, srch, srcx, srcy, w, h); DMACopyAlphaBitmap(data, _width, _height, x, y, bmp->getData(), bmpw, bmph, srcx, srcy, srcw, srch);
} }
else { else {
DMACopyBitmap(data, _width, _height, x, y, bmp->getData(), srcw, srch, srcx, srcy, w, h); DMACopyBitmap(data, _width, _height, x, y, bmp->getData(), bmpw, bmph, srcx, srcy, srcw, srch);
} }
} }
else { else {
int scaledw = w * scale; int scaledw = srcw * scale;
int scaledh = h * scale; int scaledh = srch * scale;
if (x + scaledw > _width) if (x + scaledw > _width)
scaledw = _width - x; scaledw = _width - x;
+17 -6
View File
@@ -25,16 +25,27 @@ void CarouselWindow::update()
first = max(0, first); first = max(0, first);
coord_t lastPosition = 0; coord_t lastPosition = 0;
detachItems(); int index = 0;
for (auto & item: items) {
for (uint8_t index = first; lastPosition < width() && index < items.size(); index++) { Window * window;
auto & item = items[index]; if (index == selection) {
auto window = (index == selection ? item.front : item.back); item.back->detach();
window = item.front;
}
else {
item.front->detach();
window = item.back;
}
window->attach(this); window->attach(this);
window->setLeft(lastPosition); window->setLeft(lastPosition);
window->setTop((height() - window->height()) / 2); window->setTop((height() - window->height()) / 2);
if (index == first)
setScrollPositionX(lastPosition);
lastPosition += window->width() + CAROUSEL_SPACING; lastPosition += window->width() + CAROUSEL_SPACING;
index += 1;
} }
setInnerWidth(lastPosition);
} }
void Carousel::onEvent(event_t event) void Carousel::onEvent(event_t event)
@@ -42,7 +53,7 @@ void Carousel::onEvent(event_t event)
TRACE_WINDOWS("%s received event 0x%X", getWindowDebugString().c_str(), event); TRACE_WINDOWS("%s received event 0x%X", getWindowDebugString().c_str(), event);
if (event == EVT_ROTARY_RIGHT) { if (event == EVT_ROTARY_RIGHT) {
if (body->selection < body->items.size() - 1) if (body->selection < (int)body->items.size() - 1)
select(body->selection + 1); select(body->selection + 1);
} }
else if (event == EVT_ROTARY_LEFT) { else if (event == EVT_ROTARY_LEFT) {
+5 -17
View File
@@ -49,12 +49,10 @@ class CarouselWindow: public Window {
~CarouselWindow() override ~CarouselWindow() override
{ {
detachItems(); for (auto & item: items) {
// TODO memory leak here item.front->deleteLater();
// for (auto & item: items) { item.back->deleteLater();
// item.front->deleteLater(); }
// item.back->deleteLater();
// }
} }
void addItem(CarouselItem item) void addItem(CarouselItem item)
@@ -75,19 +73,9 @@ class CarouselWindow: public Window {
update(); update();
} }
int selection = 0;
protected: protected:
std::vector<CarouselItem> items; std::vector<CarouselItem> items;
int selection = 0;
void detachItems()
{
for (auto & item: items) {
item.front->detach();
item.back->detach();
}
}
void update(); void update();
}; };