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);
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)
return;
@@ -399,42 +399,58 @@ class BitmapBuffer: public BitmapBufferBase<uint16_t>
if (x >= xmax || y >= ymax)
return;
coord_t srcw = bmp->width();
coord_t srch = bmp->height();
coord_t bmpw = bmp->width();
coord_t bmph = bmp->height();
if (w == 0)
w = srcw;
if (h == 0)
h = srch;
if (srcx + w > srcw)
w = srcw - srcx;
if (srcy + h > srch)
h = srch - srcy;
if (x < xmin) {
w += x - xmin;
srcx -= x - xmin;
x = xmin;
}
if (y < ymin) {
h += y - ymin;
srcy -= y - ymin;
y = ymin;
}
if (srcw == 0)
srcw = bmpw;
if (srch == 0)
srch = bmph;
if (srcx + srcw > bmpw)
srcw = bmpw - srcx;
if (srcy + srch > bmph)
srch = bmph - srcy;
if (scale == 0) {
if (x + w > xmax) {
w = xmax - x;
if (x < xmin) {
srcw += x - xmin;
srcx -= x - xmin;
x = xmin;
}
if (y + h > ymax) {
h = ymax - y;
if (y < ymin) {
srch += y - ymin;
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;
}
// 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;
}*/
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 {
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 {
int scaledw = w * scale;
int scaledh = h * scale;
int scaledw = srcw * scale;
int scaledh = srch * scale;
if (x + scaledw > _width)
scaledw = _width - x;
+17 -6
View File
@@ -25,16 +25,27 @@ void CarouselWindow::update()
first = max(0, first);
coord_t lastPosition = 0;
detachItems();
for (uint8_t index = first; lastPosition < width() && index < items.size(); index++) {
auto & item = items[index];
auto window = (index == selection ? item.front : item.back);
int index = 0;
for (auto & item: items) {
Window * window;
if (index == selection) {
item.back->detach();
window = item.front;
}
else {
item.front->detach();
window = item.back;
}
window->attach(this);
window->setLeft(lastPosition);
window->setTop((height() - window->height()) / 2);
if (index == first)
setScrollPositionX(lastPosition);
lastPosition += window->width() + CAROUSEL_SPACING;
index += 1;
}
setInnerWidth(lastPosition);
}
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);
if (event == EVT_ROTARY_RIGHT) {
if (body->selection < body->items.size() - 1)
if (body->selection < (int)body->items.size() - 1)
select(body->selection + 1);
}
else if (event == EVT_ROTARY_LEFT) {
+5 -17
View File
@@ -49,12 +49,10 @@ class CarouselWindow: public Window {
~CarouselWindow() override
{
detachItems();
// TODO memory leak here
// for (auto & item: items) {
// item.front->deleteLater();
// item.back->deleteLater();
// }
for (auto & item: items) {
item.front->deleteLater();
item.back->deleteLater();
}
}
void addItem(CarouselItem item)
@@ -75,19 +73,9 @@ class CarouselWindow: public Window {
update();
}
int selection = 0;
protected:
std::vector<CarouselItem> items;
void detachItems()
{
for (auto & item: items) {
item.front->detach();
item.back->detach();
}
}
int selection = 0;
void update();
};