From 04827d374376688924846d0b61f85660b5477a35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 5 Nov 2019 19:41:44 +0100 Subject: [PATCH] Platform: Emscripten support for cursors in Sdl2Application. --- src/Magnum/Platform/Sdl2Application.cpp | 34 +++++++++++++++++++++++-- src/Magnum/Platform/Sdl2Application.h | 16 ++++++++---- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/Magnum/Platform/Sdl2Application.cpp b/src/Magnum/Platform/Sdl2Application.cpp index 1a11b6bfa..84b260731 100644 --- a/src/Magnum/Platform/Sdl2Application.cpp +++ b/src/Magnum/Platform/Sdl2Application.cpp @@ -885,9 +885,9 @@ bool Sdl2Application::mainLoopIteration() { return !(_flags & Flag::Exit); } -#ifndef CORRADE_TARGET_EMSCRIPTEN namespace { +#ifndef CORRADE_TARGET_EMSCRIPTEN constexpr SDL_SystemCursor CursorMap[] { SDL_SYSTEM_CURSOR_ARROW, SDL_SYSTEM_CURSOR_IBEAM, @@ -902,10 +902,29 @@ constexpr SDL_SystemCursor CursorMap[] { SDL_SYSTEM_CURSOR_NO, SDL_SYSTEM_CURSOR_HAND }; +#else +constexpr const char* CursorMap[] { + "default", + "text", + "wait", + "crosshair", + "progress", + "nwse-resize", + "nesw-resize", + "ew-resize", + "ns-resize", + "move", + "not-allowed", + "pointer", + "none" + /* Hidden & locked not supported yet */ +}; +#endif } void Sdl2Application::setCursor(Cursor cursor) { + #ifndef CORRADE_TARGET_EMSCRIPTEN CORRADE_INTERNAL_ASSERT(UnsignedInt(cursor) < Containers::arraySize(_cursors)); if(cursor == Cursor::Hidden) { @@ -927,9 +946,18 @@ void Sdl2Application::setCursor(Cursor cursor) { _cursors[UnsignedInt(cursor)] = SDL_CreateSystemCursor(CursorMap[UnsignedInt(cursor)]); SDL_SetCursor(_cursors[UnsignedInt(cursor)]); + #else + _cursor = cursor; + CORRADE_INTERNAL_ASSERT(UnsignedInt(cursor) < Containers::arraySize(CursorMap)); + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wdollar-in-identifier-extension" + EM_ASM_({document.getElementById('canvas').style.cursor = AsciiToString($0);}, CursorMap[UnsignedInt(cursor)]); + #pragma GCC diagnostic pop + #endif } Sdl2Application::Cursor Sdl2Application::cursor() { + #ifndef CORRADE_TARGET_EMSCRIPTEN if(SDL_GetRelativeMouseMode()) return Cursor::HiddenLocked; else if(!SDL_ShowCursor(SDL_QUERY)) @@ -941,8 +969,10 @@ Sdl2Application::Cursor Sdl2Application::cursor() { if(_cursors[i] == cursor) return Cursor(i); return Cursor::Arrow; + #else + return _cursor; + #endif } -#endif void Sdl2Application::setMouseLocked(bool enabled) { /** @todo Implement this in Emscripten */ diff --git a/src/Magnum/Platform/Sdl2Application.h b/src/Magnum/Platform/Sdl2Application.h index dcde9550c..1d402bf52 100644 --- a/src/Magnum/Platform/Sdl2Application.h +++ b/src/Magnum/Platform/Sdl2Application.h @@ -832,7 +832,6 @@ class Sdl2Application { /** @{ @name Mouse handling */ public: - #ifndef CORRADE_TARGET_EMSCRIPTEN /** * @brief Cursor type * @@ -852,11 +851,17 @@ class Sdl2Application { No, /**< Slashed circle or crossbones */ Hand, /**< Hand */ Hidden, /**< Hidden */ - HiddenLocked /**< Hidden and locked */ + + #ifndef CORRADE_TARGET_EMSCRIPTEN + /** + * Hidden and locked. + * + * @note Not available in @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten". + */ + HiddenLocked + #endif }; - #endif - #ifndef CORRADE_TARGET_EMSCRIPTEN /** * @brief Set cursor type * @@ -866,7 +871,6 @@ class Sdl2Application { /** @brief Get current cursor type */ Cursor cursor(); - #endif #ifndef CORRADE_TARGET_EMSCRIPTEN /** @@ -1064,6 +1068,8 @@ class Sdl2Application { #ifndef CORRADE_TARGET_EMSCRIPTEN SDL_Cursor* _cursors[14]{}; + #else + Cursor _cursor; #endif /* These are saved from command-line arguments */