commit 13d6c3bfae82eb1aee85476eaa37ef76ea61e8ae
parent 471a00f4f60e6b977311827f3ffebf139f76e22a
Author: falkTX <falktx@falktx.com>
Date: Sun, 30 May 2021 01:48:39 +0100
Allow to automatically scale nanovg viewport, used in blendish
Signed-off-by: falkTX <falktx@falktx.com>
Diffstat:
5 files changed, 31 insertions(+), 16 deletions(-)
diff --git a/dgl/SubWidget.hpp b/dgl/SubWidget.hpp
@@ -129,7 +129,7 @@ public:
/**
Indicate that this subwidget will always draw at its own internal size and needs scaling to fit target size.
*/
- void setNeedsViewportScaling(bool needsViewportScaling = true);
+ void setNeedsViewportScaling(bool needsViewportScaling = true, double autoScaleFactor = 0.0);
/**
Indicate that this subwidget should not be drawn on screen, typically because it is managed by something else.
diff --git a/dgl/src/OpenGL.cpp b/dgl/src/OpenGL.cpp
@@ -575,21 +575,33 @@ void SubWidget::PrivateData::display(const uint width, const uint height, const
bool needsDisableScissor = false;
- if (needsFullViewportForDrawing || (absolutePos.isZero() && self->getSize() == Size<uint>(width, height)))
+ if (needsViewportScaling)
{
- // full viewport size
- glViewport(0,
- -static_cast<int>(height * autoScaleFactor - height + 0.5),
- static_cast<int>(width * autoScaleFactor + 0.5),
- static_cast<int>(height * autoScaleFactor + 0.5));
+ // limit viewport to widget bounds
+ const int x = absolutePos.getX();
+ const int w = static_cast<int>(self->getWidth());
+ const int h = static_cast<int>(self->getHeight());
+
+ if (viewportScaleFactor != 0.0)
+ {
+ glViewport(x,
+ -static_cast<int>(height * viewportScaleFactor - height + absolutePos.getY() + 0.5),
+ static_cast<int>(width * viewportScaleFactor + 0.5),
+ static_cast<int>(height * viewportScaleFactor + 0.5));
+ }
+ else
+ {
+ const int y = static_cast<int>(height - self->getHeight()) - absolutePos.getY();
+ glViewport(x, y, w, h);
+ }
}
- else if (needsViewportScaling)
+ else if (needsFullViewportForDrawing || (absolutePos.isZero() && self->getSize() == Size<uint>(width, height)))
{
- // limit viewport to widget bounds
- glViewport(absolutePos.getX(),
- static_cast<int>(height - self->getHeight()) - absolutePos.getY(),
- static_cast<int>(self->getWidth()),
- static_cast<int>(self->getHeight()));
+ // full viewport size
+ glViewport(0,
+ -static_cast<int>(height - height + 0.5),
+ static_cast<int>(width + 0.5),
+ static_cast<int>(height + 0.5));
}
else
{
diff --git a/dgl/src/SubWidget.cpp b/dgl/src/SubWidget.cpp
@@ -123,9 +123,10 @@ void SubWidget::setNeedsFullViewportDrawing(const bool needsFullViewportForDrawi
pData->needsFullViewportForDrawing = needsFullViewportForDrawing;
}
-void SubWidget::setNeedsViewportScaling(const bool needsViewportScaling)
+void SubWidget::setNeedsViewportScaling(const bool needsViewportScaling, const double autoScaleFactor)
{
pData->needsViewportScaling = needsViewportScaling;
+ pData->viewportScaleFactor = autoScaleFactor;
}
void SubWidget::setSkipDrawing(const bool skipDrawing)
diff --git a/dgl/src/SubWidgetPrivateData.cpp b/dgl/src/SubWidgetPrivateData.cpp
@@ -28,7 +28,8 @@ SubWidget::PrivateData::PrivateData(SubWidget* const s, Widget* const pw)
absolutePos(),
needsFullViewportForDrawing(false),
needsViewportScaling(false),
- skipDrawing(false)
+ skipDrawing(false),
+ viewportScaleFactor(0.0)
{
parentWidget->pData->subWidgets.push_back(self);
}
diff --git a/dgl/src/SubWidgetPrivateData.hpp b/dgl/src/SubWidgetPrivateData.hpp
@@ -30,7 +30,8 @@ struct SubWidget::PrivateData {
Point<int> absolutePos;
bool needsFullViewportForDrawing; // needed for widgets drawing out of bounds
bool needsViewportScaling; // needed for NanoVG
- bool skipDrawing;
+ bool skipDrawing; // for context reuse in NanoVG based guis
+ double viewportScaleFactor; // auto-scaling for NanoVG
explicit PrivateData(SubWidget* const s, Widget* const pw);
~PrivateData();