Skip to content

Commit

Permalink
[Color] Prevent ModelBmp from reloading the bitmap when not needed
Browse files Browse the repository at this point in the history
Resolves #214
  • Loading branch information
raphaelcoeffic committed Jun 16, 2021
1 parent 8cc5e77 commit a0090f4
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions radio/src/gui/colorlcd/widgets/modelbmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,56 @@ class ModelBitmapWidget: public Widget
ModelBitmapWidget(const WidgetFactory * factory, FormGroup * parent, const rect_t & rect, Widget::PersistentData * persistentData):
Widget(factory, parent, rect, persistentData)
{
loadBitmap();
}

void refresh(BitmapBuffer * dc) override
{
if (buffer && ((buffer->width() != width()) || (buffer->height() != height()))) {
if (buffer &&
((buffer->width() != width()) || (buffer->height() != height()) ||
(deps_hash != getHash()))) {
loadBitmap();
}

// big space to draw
if (rect.h >= 96 && rect.w >= 120) {

dc->drawFilledRect(0, 0, rect.w, rect.h, SOLID, MAINVIEW_PANES_COLOR, OPACITY(5));

if (buffer) {
dc->drawBitmap(0, 0, buffer.get());
}

auto iconMask = theme->getIconMask(ICON_MODEL);
if (iconMask) {
dc->drawMask(6, 4, iconMask, MAINVIEW_GRAPHICS_COLOR);
dc->drawMask(6, 4, iconMask, HEADER_COLOR);
}

dc->drawSizedText(45, 10, g_model.header.name, LEN_MODEL_NAME, FONT(XS));
dc->drawSolidFilledRect(39, 27, rect.w - 48, 2, MAINVIEW_GRAPHICS_COLOR);

if (buffer) {
dc->drawBitmap(0, 38, buffer.get());
}
dc->drawSizedText(45, 10, g_model.header.name, LEN_MODEL_NAME,
FONT(XS) | DEFAULT_COLOR);
dc->drawSolidFilledRect(39, 27, rect.w - 48, 2, HEADER_COLOR);
}
// smaller space to draw
else if (buffer) {
dc->drawBitmap(0, 38, buffer.get());
}
dc->drawBitmap(0, 0, buffer.get());
}
}

void checkEvents() override
{
Widget::checkEvents();

uint32_t new_hash = hash(g_model.header.bitmap, sizeof(g_model.header.bitmap));
new_hash ^= hash(g_model.header.name, sizeof(g_model.header.name));
new_hash ^= hash(g_eeGeneral.themeName, sizeof(g_eeGeneral.themeName));

if (new_hash != deps_hash) {
deps_hash = new_hash;
loadBitmap();
if (getHash() != deps_hash) {
invalidate();
}
}

protected:
std::unique_ptr<BitmapBuffer> buffer;
uint32_t deps_hash = 0;

uint32_t getHash()
{
return hash(g_model.header.bitmap, sizeof(g_model.header.bitmap));
}

void loadBitmap()
{
std::string filename = std::string(g_model.header.bitmap);
Expand All @@ -94,7 +95,7 @@ class ModelBitmapWidget: public Widget
}

buffer->clear(DEFAULT_BGCOLOR);
buffer->drawScaledBitmap(bitmap.get(), 0, 0, width(), height() - 38);
buffer->drawScaledBitmap(bitmap.get(), 0, 38, width(), height() - 38);
}
};

Expand Down

0 comments on commit a0090f4

Please sign in to comment.