From 2f94c8019d9ce0ed9caf63a0b5c5c90e9a410fdd Mon Sep 17 00:00:00 2001 From: hogliux Date: Mon, 15 Jun 2015 18:08:12 +0100 Subject: [PATCH] Implement proper android scaling support --- .../native/juce_android_Windowing.cpp | 34 +++++++++++++------ .../juce_opengl/native/juce_OpenGL_android.h | 7 ++-- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/modules/juce_gui_basics/native/juce_android_Windowing.cpp b/modules/juce_gui_basics/native/juce_android_Windowing.cpp index 0a7df049b4..369dce94ed 100644 --- a/modules/juce_gui_basics/native/juce_android_Windowing.cpp +++ b/modules/juce_gui_basics/native/juce_android_Windowing.cpp @@ -112,7 +112,8 @@ public: : ComponentPeer (comp, windowStyleFlags), usingAndroidGraphics (false), fullScreen (false), - sizeAllocated (0) + sizeAllocated (0), + scale ((float) Desktop::getInstance().getDisplays().getMainDisplay().scale) { // NB: must not put this in the initialiser list, as it invokes a callback, // which will fail if the peer is only half-constructed. @@ -189,8 +190,10 @@ public: view.callVoidMethod (ComponentPeerView.setViewName, javaString (title).get()); } - void setBounds (const Rectangle& r, bool isNowFullScreen) override + void setBounds (const Rectangle& userRect, bool isNowFullScreen) override { + Rectangle r = userRect * scale; + if (MessageManager::getInstance()->isThisTheMessageThread()) { fullScreen = isNowFullScreen; @@ -224,7 +227,7 @@ public: return Rectangle (view.callIntMethod (ComponentPeerView.getLeft), view.callIntMethod (ComponentPeerView.getTop), view.callIntMethod (ComponentPeerView.getWidth), - view.callIntMethod (ComponentPeerView.getHeight)); + view.callIntMethod (ComponentPeerView.getHeight)) / scale; } void handleScreenSizeChange() @@ -238,7 +241,7 @@ public: Point getScreenPosition() const { return Point (view.callIntMethod (ComponentPeerView.getLeft), - view.callIntMethod (ComponentPeerView.getTop)); + view.callIntMethod (ComponentPeerView.getTop)) / scale; } Point localToGlobal (Point relativePosition) override @@ -291,7 +294,8 @@ public: return isPositiveAndBelow (localPos.x, component.getWidth()) && isPositiveAndBelow (localPos.y, component.getHeight()) && ((! trueIfInAChildWindow) || view.callBooleanMethod (ComponentPeerView.containsPoint, - localPos.x, localPos.y)); + localPos.x * scale, + localPos.y * scale)); } BorderSize getFrameSize() const override @@ -322,19 +326,21 @@ public: } //============================================================================== - void handleMouseDownCallback (int index, Point pos, int64 time) + void handleMouseDownCallback (int index, Point sysPos, int64 time) { + Point pos = sysPos / scale; lastMousePos = pos; // this forces a mouse-enter/up event, in case for some reason we didn't get a mouse-up before. handleMouseEvent (index, pos, currentModifiers.withoutMouseButtons(), time); if (isValidPeer (this)) - handleMouseDragCallback (index, pos, time); + handleMouseDragCallback (index, sysPos, time); } void handleMouseDragCallback (int index, Point pos, int64 time) { + pos /= scale; lastMousePos = pos; jassert (index < 64); @@ -346,6 +352,7 @@ public: void handleMouseUpCallback (int index, Point pos, int64 time) { + pos /= scale; lastMousePos = pos; jassert (index < 64); @@ -441,6 +448,7 @@ public: { LowLevelGraphicsSoftwareRenderer g (temp); g.setOrigin (-clip.getPosition()); + g.addTransform (AffineTransform::scale (scale)); handlePaint (g); } } @@ -453,8 +461,10 @@ public: } } - void repaint (const Rectangle& area) override + void repaint (const Rectangle& userArea) override { + Rectangle area = userArea * scale; + if (MessageManager::getInstance()->isThisTheMessageThread()) { view.callVoidMethod (ComponentPeerView.invalidate, area.getX(), area.getY(), area.getRight(), area.getBottom()); @@ -507,6 +517,7 @@ private: GlobalRef buffer; bool usingAndroidGraphics, fullScreen; int sizeAllocated; + float scale; class PreallocatedImage : public ImagePixelData { @@ -723,11 +734,12 @@ bool juce_areThereAnyAlwaysOnTopWindows() void Desktop::Displays::findDisplays (float masterScale) { Display d; - d.userArea = d.totalArea = Rectangle (android.screenWidth, - android.screenHeight) / masterScale; + d.isMain = true; - d.scale = masterScale; d.dpi = android.dpi; + d.scale = masterScale * (d.dpi / 150.); + d.userArea = d.totalArea = Rectangle (android.screenWidth, + android.screenHeight) / d.scale; displays.add (d); } diff --git a/modules/juce_opengl/native/juce_OpenGL_android.h b/modules/juce_opengl/native/juce_OpenGL_android.h index 082d0ea788..7fc4d53516 100644 --- a/modules/juce_opengl/native/juce_OpenGL_android.h +++ b/modules/juce_opengl/native/juce_OpenGL_android.h @@ -88,9 +88,12 @@ public: if (lastBounds != bounds) { lastBounds = bounds; + + Rectangle r = bounds * Desktop::getInstance().getDisplays().getMainDisplay().scale; + glView.callVoidMethod (OpenGLView.layout, - bounds.getX(), bounds.getY(), - bounds.getRight(), bounds.getBottom()); + r.getX(), r.getY(), + r.getRight(), r.getBottom()); } }