/*
This file is part of Magnum .
Copyright © 2010 , 2011 , 2012 , 2013 , 2014 , 2015 , 2016 , 2017 , 2018 , 2019 ,
2020 , 2021 , 2022 , 2023 , 2024
Vladimír Vondruš < mosra @ centrum . cz >
Permission is hereby granted , free of charge , to any person obtaining a
copy of this software and associated documentation files ( the " Software " ) ,
to deal in the Software without restriction , including without limitation
the rights to use , copy , modify , merge , publish , distribute , sublicense ,
and / or sell copies of the Software , and to permit persons to whom the
Software is furnished to do so , subject to the following conditions :
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software .
THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING
FROM , OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE .
*/
# include <Corrade/Containers/EnumSet.hpp>
# include <Corrade/Containers/Optional.h>
# include <Corrade/PluginManager/Manager.h>
# include <Corrade/Utility/Arguments.h>
# include <Corrade/Utility/Resource.h>
# include "Magnum/ImageView.h"
# include "Magnum/Math/ConfigurationValue.h"
# include "Magnum/Math/Time.h"
# include "Magnum/Platform/Sdl2Application.h"
# include "Magnum/Trade/AbstractImporter.h"
# include "Magnum/Trade/ImageData.h"
# ifdef CORRADE_TARGET_CLANG_CL
/* SDL does #pragma pack(push,8) and #pragma pack(pop,8) in different headers
( begin_code . h and end_code . h ) and clang - cl doesn ' t like that , even though it
is completely fine . Silence the warning . */
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wpragma-pack"
# endif
# include <SDL_events.h>
# ifdef CORRADE_TARGET_CLANG_CL
# pragma clang diagnostic pop
# endif
# include <SDL_timer.h>
# ifdef MAGNUM_TARGET_GL
# include "Magnum/GL/DefaultFramebuffer.h"
# endif
namespace Magnum { namespace Platform {
/* These cannot be in an anonymous namespace as enumSetDebugOutput() below
wouldn ' t be able to pick them up */
static Debug & operator < < ( Debug & debug , Application : : InputEvent : : Modifier value ) {
debug < < " Modifier " < < Debug : : nospace ;
switch ( value ) {
# define _c(value) case Application::InputEvent::Modifier::value: return debug << "::" #value;
_c ( Shift )
_c ( Ctrl )
_c ( Alt )
_c ( Super )
_c ( AltGr )
_c ( CapsLock )
_c ( NumLock )
# undef _c
}
return debug < < " ( " < < Debug : : nospace < < UnsignedInt ( value ) < < Debug : : nospace < < " ) " ;
}
Platform: replace mouse events with pointer events in Sdl2Application.
Pointer events are an unified abstraction over mouse, touch, pen and
potential other yet-to-be-invented pointer-like input methods. Their goal
is to expose all such input methods under a single interface so the
application side doesn't need to explicitly make sure that it's
touch-aware or pen-aware. This abstraction is already present in HTML5,
in Qt6 and in WINAPI as well, and is also what I adopted for the new UI
library because it *just makes sense*.
Unfortunately not even SDL3 took the opportunity to introduce that and
instead added a *third* separate event type for pen input in SDL3. At
first I thought that I wouldn't introduce any extra abstractions in the
Application classes (because that's what they are designed to be, as
lightweight as possible), but midway through introducing TouchEvent
classes and fighting SDL's touch->mouse and mouse->touch compatibility
translation (yes, it's both ways, depending on the platform) I realized
that a much simpler solution that doesn't require any event translation
or the users duplicating their event handling logic for several possible
input types is to introduce a single new event type that covers all.
Which is what this commit does -- it doesn't introduce anything
touch-related so far, just creates a new PointerEvent and
PointerMoveEvent class and corresponding virtual functions. Additionally,
I took this as an opportunity to make the position floating-point, since
that's what SDL3 does now as well, and GLFW did so since ever.
Plus, the Pointer and Pointers enums are directly on the Sdl2Application
class, to allow me to *finally* introduce pointer state queries. Which
weren't possible until now, because there were mutually incompatible
MouseEvent::Button and MouseMoveEvent::Button enums and putting them on
the base class would mean one would have to be translated and the other
not. With Pointer it's translated always, because there isn't any similar
enumeration in SDL that would cover mouse, touch and pen at the same
time.
2 years ago
static Debug & operator < < ( Debug & debug , Application : : Pointer value ) {
debug < < " Pointer " < < Debug : : nospace ;
switch ( value ) {
# define _c(value) case Application::Pointer::value: return debug << "::" #value;
_c ( MouseLeft )
_c ( MouseMiddle )
_c ( MouseRight )
_c ( MouseButton4 )
_c ( MouseButton5 )
# undef _c
}
return debug < < " ( " < < Debug : : nospace < < UnsignedInt ( value ) < < Debug : : nospace < < " ) " ;
}
# ifdef MAGNUM_BUILD_DEPRECATED
CORRADE_IGNORE_DEPRECATED_PUSH
CORRADE_UNUSED static Debug & operator < < ( Debug & debug , Application : : MouseMoveEvent : : Button value ) {
debug < < " Button " < < Debug : : nospace ;
switch ( value ) {
# define _c(value) case Application::MouseMoveEvent::Button::value: return debug << "::" #value;
_c ( Left )
_c ( Middle )
_c ( Right )
_c ( X1 )
_c ( X2 )
# undef _c
}
return debug < < " ( " < < Debug : : nospace < < UnsignedInt ( value ) < < Debug : : nospace < < " ) " ;
}
Platform: replace mouse events with pointer events in Sdl2Application.
Pointer events are an unified abstraction over mouse, touch, pen and
potential other yet-to-be-invented pointer-like input methods. Their goal
is to expose all such input methods under a single interface so the
application side doesn't need to explicitly make sure that it's
touch-aware or pen-aware. This abstraction is already present in HTML5,
in Qt6 and in WINAPI as well, and is also what I adopted for the new UI
library because it *just makes sense*.
Unfortunately not even SDL3 took the opportunity to introduce that and
instead added a *third* separate event type for pen input in SDL3. At
first I thought that I wouldn't introduce any extra abstractions in the
Application classes (because that's what they are designed to be, as
lightweight as possible), but midway through introducing TouchEvent
classes and fighting SDL's touch->mouse and mouse->touch compatibility
translation (yes, it's both ways, depending on the platform) I realized
that a much simpler solution that doesn't require any event translation
or the users duplicating their event handling logic for several possible
input types is to introduce a single new event type that covers all.
Which is what this commit does -- it doesn't introduce anything
touch-related so far, just creates a new PointerEvent and
PointerMoveEvent class and corresponding virtual functions. Additionally,
I took this as an opportunity to make the position floating-point, since
that's what SDL3 does now as well, and GLFW did so since ever.
Plus, the Pointer and Pointers enums are directly on the Sdl2Application
class, to allow me to *finally* introduce pointer state queries. Which
weren't possible until now, because there were mutually incompatible
MouseEvent::Button and MouseMoveEvent::Button enums and putting them on
the base class would mean one would have to be translated and the other
not. With Pointer it's translated always, because there isn't any similar
enumeration in SDL that would cover mouse, touch and pen at the same
time.
2 years ago
CORRADE_IGNORE_DEPRECATED_POP
# endif
namespace Test { namespace {
Debug & operator < < ( Debug & debug , Application : : InputEvent : : Modifiers value ) {
return Containers : : enumSetDebugOutput ( debug , value , " Modifiers{} " , {
Application : : InputEvent : : Modifier : : Shift ,
Application : : InputEvent : : Modifier : : Ctrl ,
Application : : InputEvent : : Modifier : : Alt ,
Application : : InputEvent : : Modifier : : Super ,
Application : : InputEvent : : Modifier : : AltGr ,
Application : : InputEvent : : Modifier : : CapsLock ,
Application : : InputEvent : : Modifier : : NumLock ,
} ) ;
}
Platform: replace mouse events with pointer events in Sdl2Application.
Pointer events are an unified abstraction over mouse, touch, pen and
potential other yet-to-be-invented pointer-like input methods. Their goal
is to expose all such input methods under a single interface so the
application side doesn't need to explicitly make sure that it's
touch-aware or pen-aware. This abstraction is already present in HTML5,
in Qt6 and in WINAPI as well, and is also what I adopted for the new UI
library because it *just makes sense*.
Unfortunately not even SDL3 took the opportunity to introduce that and
instead added a *third* separate event type for pen input in SDL3. At
first I thought that I wouldn't introduce any extra abstractions in the
Application classes (because that's what they are designed to be, as
lightweight as possible), but midway through introducing TouchEvent
classes and fighting SDL's touch->mouse and mouse->touch compatibility
translation (yes, it's both ways, depending on the platform) I realized
that a much simpler solution that doesn't require any event translation
or the users duplicating their event handling logic for several possible
input types is to introduce a single new event type that covers all.
Which is what this commit does -- it doesn't introduce anything
touch-related so far, just creates a new PointerEvent and
PointerMoveEvent class and corresponding virtual functions. Additionally,
I took this as an opportunity to make the position floating-point, since
that's what SDL3 does now as well, and GLFW did so since ever.
Plus, the Pointer and Pointers enums are directly on the Sdl2Application
class, to allow me to *finally* introduce pointer state queries. Which
weren't possible until now, because there were mutually incompatible
MouseEvent::Button and MouseMoveEvent::Button enums and putting them on
the base class would mean one would have to be translated and the other
not. With Pointer it's translated always, because there isn't any similar
enumeration in SDL that would cover mouse, touch and pen at the same
time.
2 years ago
Debug & operator < < ( Debug & debug , Application : : Pointers value ) {
return Containers : : enumSetDebugOutput ( debug , value , " Pointers{} " , {
Application : : Pointer : : MouseLeft ,
Application : : Pointer : : MouseMiddle ,
Application : : Pointer : : MouseRight ,
Application : : Pointer : : MouseButton4 ,
Application : : Pointer : : MouseButton5 ,
} ) ;
}
# ifdef MAGNUM_BUILD_DEPRECATED
CORRADE_IGNORE_DEPRECATED_PUSH
CORRADE_UNUSED Debug & operator < < ( Debug & debug , Application : : MouseEvent : : Button value ) {
debug < < " Button " < < Debug : : nospace ;
switch ( value ) {
# define _c(value) case Application::MouseEvent::Button::value: return debug << "::" #value;
_c ( Left )
_c ( Middle )
_c ( Right )
_c ( X1 )
_c ( X2 )
# undef _c
}
return debug < < " ( " < < Debug : : nospace < < UnsignedInt ( value ) < < Debug : : nospace < < " ) " ;
}
Platform: replace mouse events with pointer events in Sdl2Application.
Pointer events are an unified abstraction over mouse, touch, pen and
potential other yet-to-be-invented pointer-like input methods. Their goal
is to expose all such input methods under a single interface so the
application side doesn't need to explicitly make sure that it's
touch-aware or pen-aware. This abstraction is already present in HTML5,
in Qt6 and in WINAPI as well, and is also what I adopted for the new UI
library because it *just makes sense*.
Unfortunately not even SDL3 took the opportunity to introduce that and
instead added a *third* separate event type for pen input in SDL3. At
first I thought that I wouldn't introduce any extra abstractions in the
Application classes (because that's what they are designed to be, as
lightweight as possible), but midway through introducing TouchEvent
classes and fighting SDL's touch->mouse and mouse->touch compatibility
translation (yes, it's both ways, depending on the platform) I realized
that a much simpler solution that doesn't require any event translation
or the users duplicating their event handling logic for several possible
input types is to introduce a single new event type that covers all.
Which is what this commit does -- it doesn't introduce anything
touch-related so far, just creates a new PointerEvent and
PointerMoveEvent class and corresponding virtual functions. Additionally,
I took this as an opportunity to make the position floating-point, since
that's what SDL3 does now as well, and GLFW did so since ever.
Plus, the Pointer and Pointers enums are directly on the Sdl2Application
class, to allow me to *finally* introduce pointer state queries. Which
weren't possible until now, because there were mutually incompatible
MouseEvent::Button and MouseMoveEvent::Button enums and putting them on
the base class would mean one would have to be translated and the other
not. With Pointer it's translated always, because there isn't any similar
enumeration in SDL that would cover mouse, touch and pen at the same
time.
2 years ago
CORRADE_UNUSED Debug & operator < < ( Debug & debug , Application : : MouseMoveEvent : : Buttons value ) {
return Containers : : enumSetDebugOutput ( debug , value , " Buttons{} " , {
Application : : MouseMoveEvent : : Button : : Left ,
Application : : MouseMoveEvent : : Button : : Middle ,
Application : : MouseMoveEvent : : Button : : Right ,
Application : : MouseMoveEvent : : Button : : X1 ,
Application : : MouseMoveEvent : : Button : : X2 ,
} ) ;
}
Platform: replace mouse events with pointer events in Sdl2Application.
Pointer events are an unified abstraction over mouse, touch, pen and
potential other yet-to-be-invented pointer-like input methods. Their goal
is to expose all such input methods under a single interface so the
application side doesn't need to explicitly make sure that it's
touch-aware or pen-aware. This abstraction is already present in HTML5,
in Qt6 and in WINAPI as well, and is also what I adopted for the new UI
library because it *just makes sense*.
Unfortunately not even SDL3 took the opportunity to introduce that and
instead added a *third* separate event type for pen input in SDL3. At
first I thought that I wouldn't introduce any extra abstractions in the
Application classes (because that's what they are designed to be, as
lightweight as possible), but midway through introducing TouchEvent
classes and fighting SDL's touch->mouse and mouse->touch compatibility
translation (yes, it's both ways, depending on the platform) I realized
that a much simpler solution that doesn't require any event translation
or the users duplicating their event handling logic for several possible
input types is to introduce a single new event type that covers all.
Which is what this commit does -- it doesn't introduce anything
touch-related so far, just creates a new PointerEvent and
PointerMoveEvent class and corresponding virtual functions. Additionally,
I took this as an opportunity to make the position floating-point, since
that's what SDL3 does now as well, and GLFW did so since ever.
Plus, the Pointer and Pointers enums are directly on the Sdl2Application
class, to allow me to *finally* introduce pointer state queries. Which
weren't possible until now, because there were mutually incompatible
MouseEvent::Button and MouseMoveEvent::Button enums and putting them on
the base class would mean one would have to be translated and the other
not. With Pointer it's translated always, because there isn't any similar
enumeration in SDL that would cover mouse, touch and pen at the same
time.
2 years ago
CORRADE_IGNORE_DEPRECATED_POP
# endif
Debug & operator < < ( Debug & debug , Application : : KeyEvent : : Key value ) {
debug < < " Key " < < Debug : : nospace ;
switch ( value ) {
# define _c(value) case Application::KeyEvent::Key::value: return debug << "::" #value;
_c ( Unknown )
_c ( LeftShift )
_c ( RightShift )
_c ( LeftCtrl )
_c ( RightCtrl )
_c ( LeftAlt )
_c ( RightAlt )
_c ( LeftSuper )
_c ( RightSuper )
_c ( AltGr )
_c ( Enter )
_c ( Esc )
_c ( Up )
_c ( Down )
_c ( Left )
_c ( Right )
_c ( Home )
_c ( End )
_c ( PageUp )
_c ( PageDown )
_c ( Backspace )
_c ( Insert )
_c ( Delete )
_c ( F1 )
_c ( F2 )
_c ( F3 )
_c ( F4 )
_c ( F5 )
_c ( F6 )
_c ( F7 )
_c ( F8 )
_c ( F9 )
_c ( F10 )
_c ( F11 )
_c ( F12 )
_c ( Space )
_c ( Tab )
_c ( Quote )
_c ( Comma )
_c ( Period )
_c ( Minus )
_c ( Plus )
_c ( Slash )
_c ( Percent )
_c ( Semicolon )
_c ( Equal )
_c ( LeftBracket )
_c ( RightBracket )
_c ( Backslash )
_c ( Backquote )
_c ( Zero )
_c ( One )
_c ( Two )
_c ( Three )
_c ( Four )
_c ( Five )
_c ( Six )
_c ( Seven )
_c ( Eight )
_c ( Nine )
_c ( A )
_c ( B )
_c ( C )
_c ( D )
_c ( E )
_c ( F )
_c ( G )
_c ( H )
_c ( I )
_c ( J )
_c ( K )
_c ( L )
_c ( M )
_c ( N )
_c ( O )
_c ( P )
_c ( Q )
_c ( R )
_c ( S )
_c ( T )
_c ( U )
_c ( V )
_c ( W )
_c ( X )
_c ( Y )
_c ( Z )
_c ( CapsLock )
_c ( ScrollLock )
_c ( NumLock )
_c ( PrintScreen )
_c ( Pause )
_c ( Menu )
_c ( NumZero )
_c ( NumOne )
_c ( NumTwo )
_c ( NumThree )
_c ( NumFour )
_c ( NumFive )
_c ( NumSix )
_c ( NumSeven )
_c ( NumEight )
_c ( NumNine )
_c ( NumDecimal )
_c ( NumDivide )
_c ( NumMultiply )
_c ( NumSubtract )
_c ( NumAdd )
_c ( NumEnter )
_c ( NumEqual )
# undef _c
}
return debug < < " ( " < < Debug : : nospace < < UnsignedInt ( value ) < < Debug : : nospace < < " ) " ;
}
using namespace Containers : : Literals ;
using namespace Math : : Literals ;
struct Sdl2ApplicationTest : Platform : : Application {
explicit Sdl2ApplicationTest ( const Arguments & arguments ) ;
void exitEvent ( ExitEvent & event ) override {
Debug { } < < " application exiting " ;
event . setAccepted ( ) ; /* Comment-out to test app exit suppression */
}
/* For testing HiDPI resize events */
void viewportEvent ( ViewportEvent & event ) override {
Debug { } < < " viewport event " < < event . windowSize ( )
# ifdef MAGNUM_TARGET_GL
< < event . framebufferSize ( )
# endif
< < event . dpiScaling ( ) ;
}
void drawEvent ( ) override {
Debug { } < < " draw event " ;
# ifdef MAGNUM_TARGET_GL
GL : : defaultFramebuffer . clear ( GL : : FramebufferClear : : Color ) ;
# endif
swapBuffers ( ) ;
if ( _redraw )
redraw ( ) ;
}
Platform: replace mouse events with pointer events in Sdl2Application.
Pointer events are an unified abstraction over mouse, touch, pen and
potential other yet-to-be-invented pointer-like input methods. Their goal
is to expose all such input methods under a single interface so the
application side doesn't need to explicitly make sure that it's
touch-aware or pen-aware. This abstraction is already present in HTML5,
in Qt6 and in WINAPI as well, and is also what I adopted for the new UI
library because it *just makes sense*.
Unfortunately not even SDL3 took the opportunity to introduce that and
instead added a *third* separate event type for pen input in SDL3. At
first I thought that I wouldn't introduce any extra abstractions in the
Application classes (because that's what they are designed to be, as
lightweight as possible), but midway through introducing TouchEvent
classes and fighting SDL's touch->mouse and mouse->touch compatibility
translation (yes, it's both ways, depending on the platform) I realized
that a much simpler solution that doesn't require any event translation
or the users duplicating their event handling logic for several possible
input types is to introduce a single new event type that covers all.
Which is what this commit does -- it doesn't introduce anything
touch-related so far, just creates a new PointerEvent and
PointerMoveEvent class and corresponding virtual functions. Additionally,
I took this as an opportunity to make the position floating-point, since
that's what SDL3 does now as well, and GLFW did so since ever.
Plus, the Pointer and Pointers enums are directly on the Sdl2Application
class, to allow me to *finally* introduce pointer state queries. Which
weren't possible until now, because there were mutually incompatible
MouseEvent::Button and MouseMoveEvent::Button enums and putting them on
the base class would mean one would have to be translated and the other
not. With Pointer it's translated always, because there isn't any similar
enumeration in SDL that would cover mouse, touch and pen at the same
time.
2 years ago
/* Set to 0 to test the deprecated mouse events instead */
# if 1
void pointerPressEvent ( PointerEvent & event ) override {
Debug { } < < " pointer press: " < < event . pointer ( ) < < event . modifiers ( ) < < Debug : : packed < < event . position ( ) ;
_gestureDistance = { } ;
_gestureRotation = { } ;
}
void pointerReleaseEvent ( PointerEvent & event ) override {
Debug { } < < " pointer release: " < < event . pointer ( ) < < event . modifiers ( ) < < Debug : : packed < < event . position ( ) ;
_gestureDistance = { } ;
_gestureRotation = { } ;
}
void pointerMoveEvent ( PointerMoveEvent & event ) override {
Debug { } < < " pointer move: " < < event . pointer ( ) < < event . pointers ( ) < < event . modifiers ( ) < < Debug : : packed < < event . position ( ) < < Debug : : packed < < event . relativePosition ( ) ;
}
# else
CORRADE_IGNORE_DEPRECATED_PUSH
void mousePressEvent ( MouseEvent & event ) override {
Debug { } < < " mouse press: " < < event . button ( ) < < Debug : : packed < < event . position ( ) < < event . modifiers ( ) ;
Platform: test the Sdl2Application::multiGestureEvent() as well.
The distance reported by it is useless for any practical purpose because
it doesn't report a ratio of the current and previous radius between all
points, but rather the distance. Which, well, have fun using for any sort
of zooming.
(And yeah, given that the MultiGestureEvent is gone in SDL3, I spent
quite some time looking at what it actually did in order to reimplement
that functionality on my end, and it felt *extremely weird* to me that it
always considered just that single point, never the others in order to
calculate any sort of radius. This is why, because it never considered
any sort of radius between the points, so the "Multi" in there is highly
questionable!)
So this test is just to make the uselessness easy to verify, nothing
more.
2 years ago
_gestureDistance = { } ;
_gestureRotation = { } ;
}
void mouseReleaseEvent ( MouseEvent & event ) override {
Debug { } < < " mouse release: " < < event . button ( ) < < Debug : : packed < < event . position ( ) < < event . modifiers ( ) ;
Platform: test the Sdl2Application::multiGestureEvent() as well.
The distance reported by it is useless for any practical purpose because
it doesn't report a ratio of the current and previous radius between all
points, but rather the distance. Which, well, have fun using for any sort
of zooming.
(And yeah, given that the MultiGestureEvent is gone in SDL3, I spent
quite some time looking at what it actually did in order to reimplement
that functionality on my end, and it felt *extremely weird* to me that it
always considered just that single point, never the others in order to
calculate any sort of radius. This is why, because it never considered
any sort of radius between the points, so the "Multi" in there is highly
questionable!)
So this test is just to make the uselessness easy to verify, nothing
more.
2 years ago
_gestureDistance = { } ;
_gestureRotation = { } ;
}
void mouseMoveEvent ( MouseMoveEvent & event ) override {
Debug { } < < " mouse move: " < < event . buttons ( ) < < Debug : : packed < < event . position ( ) < < Debug : : packed < < event . relativePosition ( ) < < event . modifiers ( ) ;
}
Platform: replace mouse events with pointer events in Sdl2Application.
Pointer events are an unified abstraction over mouse, touch, pen and
potential other yet-to-be-invented pointer-like input methods. Their goal
is to expose all such input methods under a single interface so the
application side doesn't need to explicitly make sure that it's
touch-aware or pen-aware. This abstraction is already present in HTML5,
in Qt6 and in WINAPI as well, and is also what I adopted for the new UI
library because it *just makes sense*.
Unfortunately not even SDL3 took the opportunity to introduce that and
instead added a *third* separate event type for pen input in SDL3. At
first I thought that I wouldn't introduce any extra abstractions in the
Application classes (because that's what they are designed to be, as
lightweight as possible), but midway through introducing TouchEvent
classes and fighting SDL's touch->mouse and mouse->touch compatibility
translation (yes, it's both ways, depending on the platform) I realized
that a much simpler solution that doesn't require any event translation
or the users duplicating their event handling logic for several possible
input types is to introduce a single new event type that covers all.
Which is what this commit does -- it doesn't introduce anything
touch-related so far, just creates a new PointerEvent and
PointerMoveEvent class and corresponding virtual functions. Additionally,
I took this as an opportunity to make the position floating-point, since
that's what SDL3 does now as well, and GLFW did so since ever.
Plus, the Pointer and Pointers enums are directly on the Sdl2Application
class, to allow me to *finally* introduce pointer state queries. Which
weren't possible until now, because there were mutually incompatible
MouseEvent::Button and MouseMoveEvent::Button enums and putting them on
the base class would mean one would have to be translated and the other
not. With Pointer it's translated always, because there isn't any similar
enumeration in SDL that would cover mouse, touch and pen at the same
time.
2 years ago
CORRADE_IGNORE_DEPRECATED_POP
# endif
void mouseScrollEvent ( MouseScrollEvent & event ) override {
Debug { } < < " mouse scroll: " < < event . modifiers ( ) < < Debug : : packed < < event . offset ( ) < < Debug : : packed < < event . position ( ) ;
}
Platform: test the Sdl2Application::multiGestureEvent() as well.
The distance reported by it is useless for any practical purpose because
it doesn't report a ratio of the current and previous radius between all
points, but rather the distance. Which, well, have fun using for any sort
of zooming.
(And yeah, given that the MultiGestureEvent is gone in SDL3, I spent
quite some time looking at what it actually did in order to reimplement
that functionality on my end, and it felt *extremely weird* to me that it
always considered just that single point, never the others in order to
calculate any sort of radius. This is why, because it never considered
any sort of radius between the points, so the "Multi" in there is highly
questionable!)
So this test is just to make the uselessness easy to verify, nothing
more.
2 years ago
void multiGestureEvent ( MultiGestureEvent & event ) override {
Debug { } < < " multi gesture: " < < event . center ( ) < < " rotation: " < < Deg { _gestureRotation } < < Debug : : nospace < < " , relative " < < Deg { event . relativeRotation ( ) } < < Debug : : nospace < < " , distance " < < _gestureDistance < < Debug : : nospace < < " , relative " < < event . relativeDistance ( ) < < Debug : : nospace < < " , fingers " < < event . fingerCount ( ) ;
_gestureDistance + = event . relativeDistance ( ) ;
_gestureRotation + = Rad ( event . relativeRotation ( ) ) ;
}
void keyPressEvent ( KeyEvent & event ) override {
Debug { } < < " key press: " < < event . key ( ) < < " keycode: " < < SDL_Keycode ( event . key ( ) ) < < event . keyName ( ) < < " scancode: " < < event . event ( ) . key . keysym . scancode
# ifndef CORRADE_TARGET_EMSCRIPTEN
< < SDL_GetScancodeName ( event . event ( ) . key . keysym . scancode )
# endif
< < event . modifiers ( ) ;
if ( event . key ( ) = = KeyEvent : : Key : : F1 ) {
Debug { } < < " starting text input " ;
startTextInput ( ) ;
} else if ( event . key ( ) = = KeyEvent : : Key : : F2 ) {
_redraw = ! _redraw ;
Debug { } < < " redrawing " < < ( _redraw ? " enabled " : " disabled " ) ;
if ( _redraw ) redraw ( ) ;
}
# ifndef CORRADE_TARGET_EMSCRIPTEN
else if ( event . key ( ) = = KeyEvent : : Key : : V ) {
_vsync = ! _vsync ;
Debug { } < < " vsync " < < ( _vsync ? " on " : " off " ) ;
setSwapInterval ( _vsync ? 1 : 0 ) ;
}
# endif
else if ( event . key ( ) = = KeyEvent : : Key : : Esc ) {
Debug { } < < " stopping text input " ;
stopTextInput ( ) ;
} else if ( event . key ( ) = = KeyEvent : : Key : : T ) {
Debug { } < < " setting window title " ;
setWindowTitle ( " This is a UTF-8 Window Title™ and it should have no exclamation mark!! " _s . exceptSuffix ( 2 ) ) ;
}
# ifndef CORRADE_TARGET_EMSCRIPTEN
else if ( event . key ( ) = = KeyEvent : : Key : : S ) {
Debug { } < < " setting window size, which should trigger a viewport event " ;
setWindowSize ( Vector2i { 300 , 200 } ) ;
} else if ( event . key ( ) = = KeyEvent : : Key : : W ) {
Debug { } < < " setting max window size, which should trigger a viewport event " ;
setMaxWindowSize ( Vector2i { 700 , 500 } ) ;
}
# endif
else if ( event . key ( ) = = KeyEvent : : Key : : H ) {
Debug { } < < " toggling hand cursor " ;
setCursor ( cursor ( ) = = Cursor : : Arrow ? Cursor : : Hand : Cursor : : Arrow ) ;
}
# ifndef CORRADE_TARGET_EMSCRIPTEN
else if ( event . key ( ) = = KeyEvent : : Key : : L ) {
Debug { } < < " toggling locked mouse " ;
setCursor ( cursor ( ) = = Cursor : : Arrow ? Cursor : : HiddenLocked : Cursor : : Arrow ) ;
}
# else
else if ( event . key ( ) = = KeyEvent : : Key : : F ) {
Debug { } < < " toggling fullscreen " ;
setContainerCssClass ( ( _fullscreen ^ = true ) ? " mn-fullsize " : " " ) ;
}
# endif
else if ( event . key ( ) = = KeyEvent : : Key : : X ) {
Debug { } < < " requesting an exit with code 5 " ;
exit ( 5 ) ;
}
/* With EmscriptenApplication, this makes the event stop from
propagating further to the page ( such as when pressing F1 ) .
Unfortunately on SDL this doesn ' t as there ' s no API to actually mark
events as accepted . */
event . setAccepted ( ) ;
}
void keyReleaseEvent ( KeyEvent & event ) override {
Debug { } < < " key release: " < < event . key ( ) < < " keycode: " < < SDL_Keycode ( event . key ( ) ) < < event . keyName ( ) < < " scancode: " < < event . event ( ) . key . keysym . scancode
# ifndef CORRADE_TARGET_EMSCRIPTEN
< < SDL_GetScancodeName ( event . event ( ) . key . keysym . scancode )
# endif
< < event . modifiers ( ) ;
/* With EmscriptenApplication, this makes the event stop from
propagating further to the page ( such as when pressing F1 ) .
Unfortunately on SDL this doesn ' t as there ' s no API to actually mark
events as accepted . */
event . setAccepted ( ) ;
}
void textInputEvent ( TextInputEvent & event ) override {
Debug { } < < " text input event: " < < event . text ( ) ;
/* With EmscriptenApplication, this makes the event stop from
propagating further to the page ( such as when pressing F1 ) .
Unfortunately on SDL this doesn ' t as there ' s no API to actually mark
events as accepted . */
event . setAccepted ( ) ;
}
/* Should fire on currently not handled events, such as minimize/maximize
or window focus / blur . Comment out to verify correct behavior with the
override not present . */
void anyEvent ( SDL_Event & event ) override {
Debug d ;
d < < " any event: " < < event . type ;
if ( event . type = = SDL_WINDOWEVENT ) d < < event . window . event ;
}
/* Uncomment to test the tick event. It should run at given minimal loop
period even if not redrawing , it should not run at a different period
when redrawing constantly . */
#if 0
void tickEvent ( ) override {
setMinimalLoopPeriod ( 250.0 _msec ) ;
Debug { } < < " tick event: " < < Seconds { SDL_GetTicks ( ) * 1.0 _msec } ;
}
# endif
private :
# ifdef CORRADE_TARGET_EMSCRIPTEN
bool _fullscreen = false ;
# endif
bool _redraw = false ;
# ifndef CORRADE_TARGET_EMSCRIPTEN
bool _vsync = false ;
# endif
Platform: test the Sdl2Application::multiGestureEvent() as well.
The distance reported by it is useless for any practical purpose because
it doesn't report a ratio of the current and previous radius between all
points, but rather the distance. Which, well, have fun using for any sort
of zooming.
(And yeah, given that the MultiGestureEvent is gone in SDL3, I spent
quite some time looking at what it actually did in order to reimplement
that functionality on my end, and it felt *extremely weird* to me that it
always considered just that single point, never the others in order to
calculate any sort of radius. This is why, because it never considered
any sort of radius between the points, so the "Multi" in there is highly
questionable!)
So this test is just to make the uselessness easy to verify, nothing
more.
2 years ago
Float _gestureDistance { } ;
Rad _gestureRotation { } ;
} ;
Sdl2ApplicationTest : : Sdl2ApplicationTest ( const Arguments & arguments ) : Platform : : Application { arguments , NoCreate } {
Utility : : Arguments args ;
args . addOption ( " dpi-scaling " ) . setHelp ( " dpi-scaling " , " DPI scaled passed via Configuration instead of --magnum-dpi-scaling, to test app overrides " )
. addSkippedPrefix ( " magnum " , " engine-specific options " )
. addBooleanOption ( " exit-immediately " ) . setHelp ( " exit-immediately " , " exit the application immediately from the constructor, to test that the app doesn't run any event handlers after " )
# ifndef CORRADE_TARGET_EMSCRIPTEN
. addBooleanOption ( " borderless " ) . setHelp ( " borderless " , " no window decoration " )
# if SDL_MAJOR_VERSION*1000 + SDL_MINOR_VERSION*100 + SDL_PATCHLEVEL >= 2005
. addBooleanOption ( " always-on-top " ) . setHelp ( " always-on-top " , " always on top " )
# endif
# endif
# ifdef MAGNUM_TARGET_GL
. addBooleanOption ( " quiet " ) . setHelp ( " quiet " , " like --magnum-log quiet, but specified via a Context::Configuration instead " )
. addBooleanOption ( " gpu-validation " ) . setHelp ( " gpu-validation " , " like --magnum-gpu-validation, but specified via a Context::Configuration instead " )
# endif
. parse ( arguments . argc , arguments . argv ) ;
if ( args . isSet ( " exit-immediately " ) ) {
exit ( ) ;
return ;
}
Configuration conf ;
conf . setTitle ( " Window title that should have no exclamation mark!! " _s . exceptSuffix ( 2 ) )
. setWindowFlags ( Configuration : : WindowFlag : : Resizable ) ;
if ( ! args . value ( " dpi-scaling " ) . empty ( ) )
conf . setSize ( { 800 , 600 } , args . value < Vector2 > ( " dpi-scaling " ) ) ;
# ifndef CORRADE_TARGET_EMSCRIPTEN
if ( args . isSet ( " borderless " ) )
conf . addWindowFlags ( Configuration : : WindowFlag : : Borderless ) ;
# if SDL_MAJOR_VERSION*1000 + SDL_MINOR_VERSION*100 + SDL_PATCHLEVEL >= 2005
if ( args . isSet ( " always-on-top " ) )
conf . addWindowFlags ( Configuration : : WindowFlag : : AlwaysOnTop ) ;
# endif
# endif
# ifdef MAGNUM_TARGET_GL
GLConfiguration glConf ;
if ( args . isSet ( " quiet " ) )
glConf . addFlags ( GLConfiguration : : Flag : : QuietLog ) ;
/* No GL-specific verbose log in Sdl2Application that we'd need to handle
explicitly */
if ( args . isSet ( " gpu-validation " ) )
glConf . addFlags ( GLConfiguration : : Flag : : GpuValidation ) ;
create ( conf , glConf ) ;
# else
create ( conf ) ;
# endif
# if defined(MAGNUM_TARGET_GL) && !defined(MAGNUM_TARGET_WEBGL)
Debug { } < < " GL context flags: " < < GL : : Context : : current ( ) . flags ( ) ;
# endif
/* For testing resize events */
Debug { } < < " window size " < < windowSize ( )
# ifdef MAGNUM_TARGET_GL
< < framebufferSize ( )
# endif
< < dpiScaling ( ) ;
# ifndef CORRADE_TARGET_EMSCRIPTEN
# if SDL_MAJOR_VERSION*1000 + SDL_MINOR_VERSION*100 + SDL_PATCHLEVEL >= 2005
Utility : : Resource rs { " icons " } ;
PluginManager : : Manager < Trade : : AbstractImporter > manager ;
Containers : : Pointer < Trade : : AbstractImporter > importer ;
Containers : : Optional < Trade : : ImageData2D > image ;
if ( ( importer = manager . loadAndInstantiate ( " AnyImageImporter " ) ) & &
importer - > openData ( rs . getRaw ( " icon-64.tga " ) ) & &
( image = importer - > image2D ( 0 ) ) ) setWindowIcon ( * image ) ;
else Warning { } < < " Can't load the plugin / file, not setting window icon " ;
# else
Debug { } < < " SDL too old, can't set window icon " ;
# endif
# endif
}
} } } }
MAGNUM_APPLICATION_MAIN ( Magnum : : Platform : : Test : : Sdl2ApplicationTest )