Skip to content

Commit 85cec8a

Browse files
committed
Allow LVGL widget to auto-scale (OpenGL only)
Signed-off-by: falkTX <falktx@falktx.com>
1 parent b544113 commit 85cec8a

1 file changed

Lines changed: 54 additions & 14 deletions

File tree

generic/LVGL.cpp

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* LVGL for DPF
3-
* Copyright (C) 2024 Filipe Coelho <falktx@falktx.com>
3+
* Copyright (C) 2024-2025 Filipe Coelho <falktx@falktx.com>
44
*
55
* Permission to use, copy, modify, and/or distribute this software for any purpose with
66
* or without fee is hereby granted, provided that the above copyright notice and this
@@ -26,6 +26,9 @@
2626
#include "../distrho/extra/Sleep.hpp"
2727
#include "../distrho/extra/Time.hpp"
2828

29+
// NOTE only valid for OpenGL
30+
// #define DPF_LVGL_AUTO_SCALING
31+
2932
START_NAMESPACE_DGL
3033

3134
// --------------------------------------------------------------------------------------------------------------------
@@ -81,7 +84,11 @@ struct LVGLWidget<BaseWidget>::PrivateData {
8184
lv_delay_set_cb(msleep);
8285
lv_tick_set_cb(gettime_ms);
8386

87+
#ifdef DPF_LVGL_AUTO_SCALING
88+
static constexpr const int scaleFactor = 1;
89+
#else
8490
const double scaleFactor = self->getTopLevelWidget()->getScaleFactor();
91+
#endif
8592
const uint width = self->getWidth() ?: 640 * scaleFactor;
8693
const uint height = self->getHeight() ?: 480 * scaleFactor;
8794

@@ -320,10 +327,18 @@ void LVGLWidget<BaseWidget>::idleCallback()
320327
template <class BaseWidget>
321328
void LVGLWidget<BaseWidget>::onDisplay()
322329
{
330+
#ifdef DPF_LVGL_AUTO_SCALING
331+
const double scaleFactor = BaseWidget::getTopLevelWidget()->getScaleFactor();
332+
DISTRHO_SAFE_ASSERT_RETURN(BaseWidget::getSize() == lvglData->textureSize * scaleFactor,);
333+
#else
334+
static constexpr const int scaleFactor = 1;
323335
DISTRHO_SAFE_ASSERT_RETURN(BaseWidget::getSize() == lvglData->textureSize,);
336+
#endif
324337

325-
const int32_t width = static_cast<int32_t>(BaseWidget::getWidth());
326-
const int32_t height = static_cast<int32_t>(BaseWidget::getHeight());
338+
const int32_t fullwidth = static_cast<int32_t>(BaseWidget::getWidth());
339+
const int32_t fullheight = static_cast<int32_t>(BaseWidget::getHeight());
340+
const int32_t width = fullwidth / scaleFactor;
341+
const int32_t height = fullheight / scaleFactor;
327342

328343
#if 0
329344
// TODO see what is really needed here..
@@ -417,13 +432,13 @@ void LVGLWidget<BaseWidget>::onDisplay()
417432
glVertex2d(0, 0);
418433

419434
glTexCoord2f(1.f, 0.f);
420-
glVertex2d(width, 0);
435+
glVertex2d(fullwidth, 0);
421436

422437
glTexCoord2f(1.f, 1.f);
423-
glVertex2d(width, height);
438+
glVertex2d(fullwidth, fullheight);
424439

425440
glTexCoord2f(0.f, 1.f);
426-
glVertex2d(0, height);
441+
glVertex2d(0, fullheight);
427442
}
428443
glEnd();
429444

@@ -549,8 +564,13 @@ bool LVGLWidget<BaseWidget>::onMotion(const Widget::MotionEvent& event)
549564
if (BaseWidget::onMotion(event))
550565
return true;
551566

552-
lvglData->mousePos.x = std::max(0, std::min<int>(BaseWidget::getWidth() - 1, event.pos.getX()));
553-
lvglData->mousePos.y = std::max(0, std::min<int>(BaseWidget::getHeight() - 1, event.pos.getY()));
567+
#ifdef DPF_LVGL_AUTO_SCALING
568+
const double scaleFactor = BaseWidget::getTopLevelWidget()->getScaleFactor();
569+
#else
570+
static constexpr const int scaleFactor = 1;
571+
#endif
572+
lvglData->mousePos.x = std::max<int>(0, std::min<int>(BaseWidget::getWidth() - 1, event.pos.getX()) / scaleFactor);
573+
lvglData->mousePos.y = std::max<int>(0, std::min<int>(BaseWidget::getHeight() - 1, event.pos.getY()) / scaleFactor);
554574
return true;
555575
}
556576

@@ -563,8 +583,13 @@ bool LVGLWidget<SubWidget>::onMotion(const Widget::MotionEvent& event)
563583
if (!getAbsoluteArea().contains(event.absolutePos))
564584
return false;
565585

566-
lvglData->mousePos.x = std::max(0, std::min<int>(getWidth() - 1, event.pos.getX()));
567-
lvglData->mousePos.y = std::max(0, std::min<int>(getHeight() - 1, event.pos.getY()));
586+
#ifdef DPF_LVGL_AUTO_SCALING
587+
const double scaleFactor = getTopLevelWidget()->getScaleFactor();
588+
#else
589+
static constexpr const int scaleFactor = 1;
590+
#endif
591+
lvglData->mousePos.x = std::max<int>(0, std::min<int>(getWidth() - 1, event.pos.getX()) / scaleFactor);
592+
lvglData->mousePos.y = std::max<int>(0, std::min<int>(getHeight() - 1, event.pos.getY()) / scaleFactor);
568593
return true;
569594
}
570595

@@ -574,7 +599,12 @@ bool LVGLWidget<BaseWidget>::onScroll(const Widget::ScrollEvent& event)
574599
if (BaseWidget::onScroll(event))
575600
return true;
576601

577-
lvglData->mouseWheelDelta -= event.delta.getY();
602+
#ifdef DPF_LVGL_AUTO_SCALING
603+
const double scaleFactor = BaseWidget::getTopLevelWidget()->getScaleFactor();
604+
#else
605+
static constexpr const int scaleFactor = 1;
606+
#endif
607+
lvglData->mouseWheelDelta -= event.delta.getY() / scaleFactor;
578608
return false;
579609
}
580610

@@ -587,7 +617,12 @@ bool LVGLWidget<SubWidget>::onScroll(const Widget::ScrollEvent& event)
587617
if (!getAbsoluteArea().contains(event.absolutePos))
588618
return false;
589619

590-
lvglData->mouseWheelDelta -= event.delta.getY();
620+
#ifdef DPF_LVGL_AUTO_SCALING
621+
const double scaleFactor = getTopLevelWidget()->getScaleFactor();
622+
#else
623+
static constexpr const int scaleFactor = 1;
624+
#endif
625+
lvglData->mouseWheelDelta -= event.delta.getY() / scaleFactor;
591626
return false;
592627
}
593628

@@ -599,8 +634,13 @@ void LVGLWidget<BaseWidget>::onResize(const Widget::ResizeEvent& event)
599634
if (lvglData->display == nullptr)
600635
return;
601636

602-
const uint width = event.size.getWidth();
603-
const uint height = event.size.getHeight();
637+
#ifdef DPF_LVGL_AUTO_SCALING
638+
const double scaleFactor = BaseWidget::getTopLevelWidget()->getScaleFactor();
639+
#else
640+
static constexpr const int scaleFactor = 1;
641+
#endif
642+
const uint width = event.size.getWidth() / scaleFactor;
643+
const uint height = event.size.getHeight() / scaleFactor;
604644
lv_area_set(&lvglData->updatedArea, 0, 0, width, height);
605645

606646
lv_global = lvglData->global;

0 commit comments

Comments
 (0)