From aa6ada4eceda538197edea224922e19303cdd112 Mon Sep 17 00:00:00 2001 From: falkTX Date: Sun, 30 Sep 2018 12:30:02 +0200 Subject: [PATCH] Add get/set scaling to Window --- dgl/Window.hpp | 3 +++ dgl/src/WidgetPrivateData.hpp | 35 ++++++++++++++------------ dgl/src/Window.cpp | 47 ++++++++++++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 20 deletions(-) diff --git a/dgl/Window.hpp b/dgl/Window.hpp index b202f041..fab14725 100644 --- a/dgl/Window.hpp +++ b/dgl/Window.hpp @@ -111,6 +111,9 @@ public: void setTransientWinId(uintptr_t winId); + double getScaling() const noexcept; + void setScaling(double scaling) noexcept; + Application& getApp() const noexcept; intptr_t getWindowId() const noexcept; diff --git a/dgl/src/WidgetPrivateData.hpp b/dgl/src/WidgetPrivateData.hpp index bf836b7b..5dbe2ace 100644 --- a/dgl/src/WidgetPrivateData.hpp +++ b/dgl/src/WidgetPrivateData.hpp @@ -63,7 +63,7 @@ struct Widget::PrivateData { subWidgets.clear(); } - void display(const uint width, const uint height, const bool renderingSubWidget) + void display(const uint width, const uint height, const double scaling, const bool renderingSubWidget) { if ((skipDisplay && ! renderingSubWidget) || size.isInvalid() || ! visible) return; @@ -76,29 +76,32 @@ struct Widget::PrivateData { if (needsFullViewport || (absolutePos.isZero() && size == Size(width, height))) { // full viewport size - glViewport(0, 0, static_cast(width), static_cast(height)); + glViewport(0, + -(height * scaling - height), + width * scaling, + height * scaling); } else if (needsScaling) { // limit viewport to widget bounds glViewport(absolutePos.getX(), - static_cast(height - self->getHeight()) - absolutePos.getY(), - static_cast(self->getWidth()), - static_cast(self->getHeight())); + height - self->getHeight() - absolutePos.getY(), + self->getWidth(), + self->getHeight()); } else { // only set viewport pos - glViewport(absolutePos.getX(), - /*static_cast(height - self->getHeight())*/ - absolutePos.getY(), - static_cast(width), - static_cast(height)); + glViewport(absolutePos.getX() * scaling, + -std::round((height * scaling - height) + (absolutePos.getY() * scaling)), + std::round(width * scaling), + std::round(height * scaling)); // then cut the outer bounds - glScissor(absolutePos.getX(), - static_cast(height - self->getHeight()) - absolutePos.getY(), - static_cast(self->getWidth()), - static_cast(self->getHeight())); + glScissor(absolutePos.getX() * scaling, + height - std::round((self->getHeight() + absolutePos.getY()) * scaling), + std::round(self->getWidth() * scaling), + std::round(self->getHeight() * scaling)); glEnable(GL_SCISSOR_TEST); needsDisableScissor = true; @@ -113,17 +116,17 @@ struct Widget::PrivateData { needsDisableScissor = false; } - displaySubWidgets(width, height); + displaySubWidgets(width, height, scaling); } - void displaySubWidgets(const uint width, const uint height) + void displaySubWidgets(const uint width, const uint height, const double scaling) { for (std::vector::iterator it = subWidgets.begin(); it != subWidgets.end(); ++it) { Widget* const widget(*it); DISTRHO_SAFE_ASSERT_CONTINUE(widget->pData != this); - widget->pData->display(width, height, true); + widget->pData->display(width, height, scaling, true); } } diff --git a/dgl/src/Window.cpp b/dgl/src/Window.cpp index 8396a954..610196fd 100644 --- a/dgl/src/Window.cpp +++ b/dgl/src/Window.cpp @@ -83,6 +83,7 @@ struct Window::PrivateData { fUsingEmbed(false), fWidth(1), fHeight(1), + fScaling(1.0), fTitle(nullptr), fWidgets(), fModal(), @@ -113,6 +114,7 @@ struct Window::PrivateData { fUsingEmbed(false), fWidth(1), fHeight(1), + fScaling(1.0), fTitle(nullptr), fWidgets(), fModal(parent.pData), @@ -155,6 +157,7 @@ struct Window::PrivateData { fUsingEmbed(parentId != 0), fWidth(1), fHeight(1), + fScaling(1.0), fTitle(nullptr), fWidgets(), fModal(), @@ -689,6 +692,20 @@ struct Window::PrivateData { // ------------------------------------------------------------------- + double getScaling() const noexcept + { + return fScaling; + } + + void setScaling(double scaling) noexcept + { + DISTRHO_SAFE_ASSERT_RETURN(scaling > 0.0,); + + fScaling = scaling; + } + + // ------------------------------------------------------------------- + void addWidget(Widget* const widget) { fWidgets.push_back(widget); @@ -740,7 +757,7 @@ struct Window::PrivateData { FOR_EACH_WIDGET(it) { Widget* const widget(*it); - widget->pData->display(fWidth, fHeight, false); + widget->pData->display(fWidth, fHeight, fScaling, false); } fSelf->onDisplayAfter(); @@ -800,7 +817,7 @@ struct Window::PrivateData { return 1; } - void onPuglMouse(const int button, const bool press, const int x, const int y) + void onPuglMouse(const int button, const bool press, int x, int y) { DBGp("PUGL: onMouse : %i %i %i %i\n", button, press, x, y); @@ -810,6 +827,9 @@ struct Window::PrivateData { if (fModal.childFocus != nullptr) return fModal.childFocus->focus(); + x /= fScaling; + y /= fScaling; + Widget::MouseEvent ev; ev.button = button; ev.press = press; @@ -827,13 +847,16 @@ struct Window::PrivateData { } } - void onPuglMotion(const int x, const int y) + void onPuglMotion(int x, int y) { DBGp("PUGL: onMotion : %i %i\n", x, y); if (fModal.childFocus != nullptr) return; + x /= fScaling; + y /= fScaling; + Widget::MotionEvent ev; ev.mod = static_cast(puglGetModifiers(fView)); ev.time = puglGetEventTimestamp(fView); @@ -849,13 +872,18 @@ struct Window::PrivateData { } } - void onPuglScroll(const int x, const int y, const float dx, const float dy) + void onPuglScroll(int x, int y, float dx, float dy) { DBGp("PUGL: onScroll : %i %i %f %f\n", x, y, dx, dy); if (fModal.childFocus != nullptr) return; + x /= fScaling; + y /= fScaling; + dx /= fScaling; + dy /= fScaling; + Widget::ScrollEvent ev; ev.delta = Point(dx, dy); ev.mod = static_cast(puglGetModifiers(fView)); @@ -1004,6 +1032,7 @@ struct Window::PrivateData { bool fUsingEmbed; uint fWidth; uint fHeight; + double fScaling; char* fTitle; std::list fWidgets; @@ -1290,6 +1319,16 @@ void Window::setTransientWinId(uintptr_t winId) pData->setTransientWinId(winId); } +double Window::getScaling() const noexcept +{ + return pData->getScaling(); +} + +void Window::setScaling(double scaling) noexcept +{ + pData->setScaling(scaling); +} + Application& Window::getApp() const noexcept { return pData->fApp;