Browse Source

Merge 3ea97a242f into 65743b5647

pull/423/merge
Vladimír Vondruš 6 years ago committed by GitHub
parent
commit
1e58f5a8b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      src/Magnum/Platform/GlfwApplication.cpp
  2. 13
      src/Magnum/Platform/GlfwApplication.h
  3. 27
      src/Magnum/Platform/Sdl2Application.cpp
  4. 13
      src/Magnum/Platform/Sdl2Application.h
  5. 12
      src/Magnum/Platform/Test/GlfwApplicationTest.cpp
  6. 15
      src/Magnum/Platform/Test/Sdl2ApplicationTest.cpp

18
src/Magnum/Platform/GlfwApplication.cpp

@ -584,13 +584,27 @@ void GlfwApplication::setupCallbacks() {
#ifdef MAGNUM_TARGET_GL #ifdef MAGNUM_TARGET_GL
glfwSetFramebufferSizeCallback(_window, [](GLFWwindow* const window, const int w, const int h) { glfwSetFramebufferSizeCallback(_window, [](GLFWwindow* const window, const int w, const int h) {
auto& app = *static_cast<GlfwApplication*>(glfwGetWindowUserPointer(window)); auto& app = *static_cast<GlfwApplication*>(glfwGetWindowUserPointer(window));
ViewportEvent e{app.windowSize(), {w, h}, app.dpiScaling()}; ViewportEvent e{ViewportEvent::Type::Resized, app.windowSize(), {w, h}, app.dpiScaling()};
app.viewportEvent(e); app.viewportEvent(e);
}); });
#else #else
glfwSetWindowSizeCallback(_window, [](GLFWwindow* const window, const int w, const int h) { glfwSetWindowSizeCallback(_window, [](GLFWwindow* const window, const int w, const int h) {
auto& app = *static_cast<GlfwApplication*>(glfwGetWindowUserPointer(window)); auto& app = *static_cast<GlfwApplication*>(glfwGetWindowUserPointer(window));
ViewportEvent e{{w, h}, app.dpiScaling()}; ViewportEvent e{ViewportEvent::Type::Resized, {w, h}, app.dpiScaling()};
app.viewportEvent(e);
});
#endif
#if GLFW_VERSION_MAJOR*100 + GLFW_VERSION_MINOR >= 303
glfwSetWindowContentScaleCallback(_window, [](GLFWwindow* const window, float xScale, float yScale) {
auto& app = *static_cast<GlfwApplication*>(glfwGetWindowUserPointer(window));
ViewportEvent e{ViewportEvent::Type::DpiScalingChanged,
#ifdef MAGNUM_TARGET_GL
app.windowSize(), app.framebufferSize(),
#else
app.windowSize(),
#endif
/* Update the cached DPI scaling value as well */
app._dpiScaling = {xScale, yScale}};
app.viewportEvent(e); app.viewportEvent(e);
}); });
#endif #endif

13
src/Magnum/Platform/GlfwApplication.h

@ -1300,6 +1300,11 @@ class GlfwApplication::ExitEvent {
*/ */
class GlfwApplication::ViewportEvent { class GlfwApplication::ViewportEvent {
public: public:
enum class Type: UnsignedByte {
Resized,
DpiScalingChanged
};
/** @brief Copying is not allowed */ /** @brief Copying is not allowed */
ViewportEvent(const ViewportEvent&) = delete; ViewportEvent(const ViewportEvent&) = delete;
@ -1312,6 +1317,9 @@ class GlfwApplication::ViewportEvent {
/** @brief Moving is not allowed */ /** @brief Moving is not allowed */
ViewportEvent& operator=(ViewportEvent&&) = delete; ViewportEvent& operator=(ViewportEvent&&) = delete;
/** @brief Event type */
Type type() const { return _type; }
/** /**
* @brief Window size * @brief Window size
* *
@ -1353,16 +1361,17 @@ class GlfwApplication::ViewportEvent {
private: private:
friend GlfwApplication; friend GlfwApplication;
explicit ViewportEvent(const Vector2i& windowSize, explicit ViewportEvent(Type type, const Vector2i& windowSize,
#ifdef MAGNUM_TARGET_GL #ifdef MAGNUM_TARGET_GL
const Vector2i& framebufferSize, const Vector2i& framebufferSize,
#endif #endif
const Vector2& dpiScaling): _windowSize{windowSize}, const Vector2& dpiScaling): _type{type}, _windowSize{windowSize},
#ifdef MAGNUM_TARGET_GL #ifdef MAGNUM_TARGET_GL
_framebufferSize{framebufferSize}, _framebufferSize{framebufferSize},
#endif #endif
_dpiScaling{dpiScaling} {} _dpiScaling{dpiScaling} {}
Type _type;
const Vector2i _windowSize; const Vector2i _windowSize;
#ifdef MAGNUM_TARGET_GL #ifdef MAGNUM_TARGET_GL
const Vector2i _framebufferSize; const Vector2i _framebufferSize;

27
src/Magnum/Platform/Sdl2Application.cpp

@ -65,6 +65,8 @@
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#endif #endif
#include <windows.h> #include <windows.h>
/* For capturing the WM_DPICHANGED event */
#include <SDL_syswm.h>
#endif #endif
namespace Magnum { namespace Platform { namespace Magnum { namespace Platform {
@ -155,6 +157,11 @@ Sdl2Application::Sdl2Application(const Arguments& arguments, NoCreateT):
std::exit(1); std::exit(1);
} }
#ifdef CORRADE_TARGET_WINDOWS
/* Enable SysWM events to get the WM_DPICHANGED event on Windows */
SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
#endif
/* Save command-line arguments */ /* Save command-line arguments */
if(args.value("log") == "verbose") _verboseLog = true; if(args.value("log") == "verbose") _verboseLog = true;
const std::string dpiScaling = args.value("dpi-scaling"); const std::string dpiScaling = args.value("dpi-scaling");
@ -834,7 +841,8 @@ bool Sdl2Application::mainLoopIteration() {
_lastKnownCanvasSize = canvasSizei; _lastKnownCanvasSize = canvasSizei;
const Vector2i size = _dpiScaling*canvasSizei; const Vector2i size = _dpiScaling*canvasSizei;
emscripten_set_canvas_element_size("#canvas", size.x(), size.y()); emscripten_set_canvas_element_size("#canvas", size.x(), size.y());
ViewportEvent e{ /** @todo dpi scaling changed? */
ViewportEvent e{ViewportEvent::Type::Resized,
#ifdef MAGNUM_TARGET_GL #ifdef MAGNUM_TARGET_GL
size, size,
#endif #endif
@ -864,7 +872,7 @@ bool Sdl2Application::mainLoopIteration() {
framebuffer size and not window size on macOS, which framebuffer size and not window size on macOS, which
is weird. Query the values directly instead to be is weird. Query the values directly instead to be
really sure. */ really sure. */
ViewportEvent e{event, windowSize(), ViewportEvent e{ViewportEvent::Type::Resized, event, windowSize(),
#ifdef MAGNUM_TARGET_GL #ifdef MAGNUM_TARGET_GL
framebufferSize(), framebufferSize(),
#endif #endif
@ -942,6 +950,21 @@ bool Sdl2Application::mainLoopIteration() {
} }
} break; } break;
#ifdef CORRADE_TARGET_WINDOWS
case SDL_SYSWMEVENT: {
if(event.syswm.msg->msg.win.msg == WM_DPICHANGED) {
DWORD dpi = event.syswm.msg->msg.win.wParam;
ViewportEvent e{ViewportEvent::Type::DpiScalingChanged, event, windowSize(),
#ifdef MAGNUM_TARGET_GL
framebufferSize(),
#endif
/* Update the cached DPI scaling value as well */
_dpiScaling = Vector2{Vector2i{LOWORD(dpi), HIWORD(dpi)}}/96.0f};
viewportEvent(e);
}
} break;
#endif
/* Direct everything else to anyEvent(), so users can implement /* Direct everything else to anyEvent(), so users can implement
event handling for things not present in the Application APIs */ event handling for things not present in the Application APIs */
default: if(!(_flags & Flag::NoAnyEvent)) anyEvent(event); default: if(!(_flags & Flag::NoAnyEvent)) anyEvent(event);

13
src/Magnum/Platform/Sdl2Application.h

@ -1829,6 +1829,11 @@ class Sdl2Application::ExitEvent {
*/ */
class Sdl2Application::ViewportEvent { class Sdl2Application::ViewportEvent {
public: public:
enum class Type: UnsignedByte {
Resized,
DpiScalingChanged
};
/** @brief Copying is not allowed */ /** @brief Copying is not allowed */
ViewportEvent(const ViewportEvent&) = delete; ViewportEvent(const ViewportEvent&) = delete;
@ -1841,6 +1846,9 @@ class Sdl2Application::ViewportEvent {
/** @brief Moving is not allowed */ /** @brief Moving is not allowed */
ViewportEvent& operator=(ViewportEvent&&) = delete; ViewportEvent& operator=(ViewportEvent&&) = delete;
/** @brief Event type */
Type type() const { return _type; }
/** /**
* @brief Window size * @brief Window size
* *
@ -1893,7 +1901,7 @@ class Sdl2Application::ViewportEvent {
private: private:
friend Sdl2Application; friend Sdl2Application;
explicit ViewportEvent( explicit ViewportEvent(Type type,
#ifndef CORRADE_TARGET_EMSCRIPTEN #ifndef CORRADE_TARGET_EMSCRIPTEN
const SDL_Event& event, const SDL_Event& event,
#endif #endif
@ -1901,7 +1909,7 @@ class Sdl2Application::ViewportEvent {
#ifdef MAGNUM_TARGET_GL #ifdef MAGNUM_TARGET_GL
const Vector2i& framebufferSize, const Vector2i& framebufferSize,
#endif #endif
const Vector2& dpiScaling): const Vector2& dpiScaling): _type{type},
#ifndef CORRADE_TARGET_EMSCRIPTEN #ifndef CORRADE_TARGET_EMSCRIPTEN
_event(event), _event(event),
#endif #endif
@ -1911,6 +1919,7 @@ class Sdl2Application::ViewportEvent {
#endif #endif
_dpiScaling{dpiScaling} {} _dpiScaling{dpiScaling} {}
Type _type;
#ifndef CORRADE_TARGET_EMSCRIPTEN #ifndef CORRADE_TARGET_EMSCRIPTEN
const SDL_Event& _event; const SDL_Event& _event;
#endif #endif

12
src/Magnum/Platform/Test/GlfwApplicationTest.cpp

@ -47,6 +47,11 @@ struct GlfwApplicationTest: Platform::Application {
<< event.framebufferSize() << event.framebufferSize()
#endif #endif
<< event.dpiScaling(); << event.dpiScaling();
if(event.type() == ViewportEvent::Type::DpiScalingChanged) {
Debug{} << "DPI scaling changed, resizing to" << _lastUnscaledWindowSize;
setWindowSize(_lastUnscaledWindowSize);
} else _lastUnscaledWindowSize = event.windowSize()/event.dpiScaling();
} }
void keyPressEvent(KeyEvent& event) override { void keyPressEvent(KeyEvent& event) override {
@ -96,6 +101,9 @@ struct GlfwApplicationTest: Platform::Application {
} }
void drawEvent() override {} void drawEvent() override {}
private:
Vector2i _lastUnscaledWindowSize;
}; };
GlfwApplicationTest::GlfwApplicationTest(const Arguments& arguments): Platform::Application{arguments, NoCreate} { GlfwApplicationTest::GlfwApplicationTest(const Arguments& arguments): Platform::Application{arguments, NoCreate} {
@ -116,6 +124,10 @@ GlfwApplicationTest::GlfwApplicationTest(const Arguments& arguments): Platform::
#endif #endif
<< dpiScaling(); << dpiScaling();
/* Save desired window size for resizing when going between differently
dense displays */
_lastUnscaledWindowSize = windowSize()/dpiScaling();
#if GLFW_VERSION_MAJOR*100 + GLFW_VERSION_MINOR >= 302 #if GLFW_VERSION_MAJOR*100 + GLFW_VERSION_MINOR >= 302
Utility::Resource rs{"icons"}; Utility::Resource rs{"icons"};
PluginManager::Manager<Trade::AbstractImporter> manager; PluginManager::Manager<Trade::AbstractImporter> manager;

15
src/Magnum/Platform/Test/Sdl2ApplicationTest.cpp

@ -66,6 +66,11 @@ struct Sdl2ApplicationTest: Platform::Application {
<< event.framebufferSize() << event.framebufferSize()
#endif #endif
<< event.dpiScaling(); << event.dpiScaling();
if(event.type() == ViewportEvent::Type::DpiScalingChanged) {
Debug{} << "DPI scaling changed, resizing to" << _lastUnscaledWindowSize;
setWindowSize(_lastUnscaledWindowSize);
} else _lastUnscaledWindowSize = event.windowSize()/event.dpiScaling();
} }
/* For testing event coordinates */ /* For testing event coordinates */
@ -137,10 +142,12 @@ struct Sdl2ApplicationTest: Platform::Application {
if(event.type == SDL_WINDOWEVENT) d << event.window.event; if(event.type == SDL_WINDOWEVENT) d << event.window.event;
} }
#ifdef CORRADE_TARGET_EMSCRIPTEN
private: private:
#ifdef CORRADE_TARGET_EMSCRIPTEN
bool _fullscreen = false; bool _fullscreen = false;
#endif #else
Vector2i _lastUnscaledWindowSize;
#endif
}; };
Sdl2ApplicationTest::Sdl2ApplicationTest(const Arguments& arguments): Platform::Application{arguments, NoCreate} { Sdl2ApplicationTest::Sdl2ApplicationTest(const Arguments& arguments): Platform::Application{arguments, NoCreate} {
@ -162,6 +169,10 @@ Sdl2ApplicationTest::Sdl2ApplicationTest(const Arguments& arguments): Platform::
#endif #endif
<< dpiScaling(); << dpiScaling();
/* Save desired window size for resizing when going between differently
dense displays */
_lastUnscaledWindowSize = windowSize()/dpiScaling();
#ifndef CORRADE_TARGET_EMSCRIPTEN #ifndef CORRADE_TARGET_EMSCRIPTEN
#if SDL_MAJOR_VERSION*1000 + SDL_MINOR_VERSION*100 + SDL_PATCHLEVEL >= 2005 #if SDL_MAJOR_VERSION*1000 + SDL_MINOR_VERSION*100 + SDL_PATCHLEVEL >= 2005
Utility::Resource rs{"icons"}; Utility::Resource rs{"icons"};

Loading…
Cancel
Save