Skip to content

Commit

Permalink
Plaftorm: Add cursor management support
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Melorio committed Oct 17, 2019
1 parent 6ebe264 commit cbefac5
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Magnum/Platform/GlfwApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,42 @@ void GlfwApplication::setWindowTitle(const std::string& title) {
glfwSetWindowTitle(_window, title.data());
}

void GlfwApplication::createCursor(Cursor cursor) {
if(!_cursors[static_cast<UnsignedInt>(cursor)]) {
int cursor_type;

switch(cursor) {
case Cursor::Arrow:
cursor_type = GLFW_ARROW_CURSOR;
break;
case Cursor::IBeam:
cursor_type = GLFW_IBEAM_CURSOR;
break;
case Cursor::Crosshair:
cursor_type = GLFW_CROSSHAIR_CURSOR;
break;
case Cursor::ResizeWE:
cursor_type = GLFW_HRESIZE_CURSOR;
break;
case Cursor::ResizeNS:
cursor_type = GLFW_VRESIZE_CURSOR;
break;
case Cursor::Hand:
cursor_type = GLFW_HAND_CURSOR;
break;
default:
return;
}

_cursors[static_cast<UnsignedInt>(cursor)] = glfwCreateStandardCursor(cursor_type);
}
}

void GlfwApplication::setCursor(Cursor cursor) {
if(_cursors[static_cast<UnsignedInt>(cursor)])
glfwSetCursor(_window, _cursors[static_cast<UnsignedInt>(cursor)]);
}

bool GlfwApplication::tryCreate(const Configuration& configuration) {
#ifdef MAGNUM_TARGET_GL
#ifdef GLFW_NO_API
Expand Down Expand Up @@ -549,6 +585,8 @@ void GlfwApplication::setupCallbacks() {

GlfwApplication::~GlfwApplication() {
glfwDestroyWindow(_window);
for(auto& cursor: _cursors)
glfwDestroyCursor(cursor);
glfwTerminate();
}

Expand Down
30 changes: 30 additions & 0 deletions src/Magnum/Platform/GlfwApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*/

#include <string>
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/ArrayView.h>
#include <Corrade/Containers/Optional.h>
#include <Corrade/Containers/Pointer.h>
Expand Down Expand Up @@ -436,6 +437,33 @@ class GlfwApplication {
/** @copydoc Sdl2Application::redraw() */
void redraw() { _flags |= Flag::Redraw; }

public:
enum class Cursor : UnsignedInt {
Arrow, /**< Arrow */
IBeam, /**< I-Beam */
Crosshair, /**< Crosshair */
ResizeWE, /**< Double arrow pointing west and east */
ResizeNS, /**< Double arrow pointing north and south */
Hand /**< Hand */
};

public:
/**
* @brief Create a cursor that can then be used
*
* @see @ref setCursor()
*/
void createCursor(Cursor cursor);

/**
* @brief Set cursor
*
* The @p cursor is expected to be created from the @ref Configuration or
* with @ref createCursor().
* @see @ref createCursor()
*/
void setCursor(Cursor cursor);

private:
/**
* @brief Viewport event
Expand Down Expand Up @@ -577,6 +605,8 @@ class GlfwApplication {

void setupCallbacks();

Containers::Array<GLFWcursor*> _cursors{static_cast<std::size_t>(Cursor::Hand)};

/* These are saved from command-line arguments */
bool _verboseLog{};
Implementation::GlfwDpiScalingPolicy _commandLineDpiScalingPolicy{};
Expand Down
57 changes: 57 additions & 0 deletions src/Magnum/Platform/Sdl2Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,60 @@ void Sdl2Application::setWindowTitle(const std::string& title) {
#endif
}

void Sdl2Application::createCursor(Cursor cursor) {
if(!_cursors[static_cast<UnsignedInt>(cursor)]) {
SDL_SystemCursor cursor_type;

switch(cursor) {
case Cursor::Arrow:
cursor_type = SDL_SYSTEM_CURSOR_ARROW;
break;
case Cursor::IBeam:
cursor_type = SDL_SYSTEM_CURSOR_IBEAM;
break;
case Cursor::Wait:
cursor_type = SDL_SYSTEM_CURSOR_WAIT;
break;
case Cursor::Crosshair:
cursor_type = SDL_SYSTEM_CURSOR_CROSSHAIR;
break;
case Cursor::WaitArrow:
cursor_type = SDL_SYSTEM_CURSOR_WAITARROW;
break;
case Cursor::ResizeNWSE:
cursor_type = SDL_SYSTEM_CURSOR_SIZENWSE;
break;
case Cursor::ResizeNESW:
cursor_type = SDL_SYSTEM_CURSOR_SIZENESW;
break;
case Cursor::ResizeWE:
cursor_type = SDL_SYSTEM_CURSOR_SIZEWE;
break;
case Cursor::ResizeNS:
cursor_type = SDL_SYSTEM_CURSOR_SIZENS;
break;
case Cursor::ResizeAll:
cursor_type = SDL_SYSTEM_CURSOR_SIZEALL;
break;
case Cursor::No:
cursor_type = SDL_SYSTEM_CURSOR_NO;
break;
case Cursor::Hand:
cursor_type = SDL_SYSTEM_CURSOR_HAND;
break;
default:
return;
}

_cursors[static_cast<UnsignedInt>(cursor)] = SDL_CreateSystemCursor(cursor_type);
}
}

void Sdl2Application::setCursor(Cursor cursor) {
if(_cursors[static_cast<UnsignedInt>(cursor)])
SDL_SetCursor(_cursors[static_cast<UnsignedInt>(cursor)]);
}

bool Sdl2Application::tryCreate(const Configuration& configuration) {
#ifdef MAGNUM_TARGET_GL
if(!(configuration.windowFlags() & Configuration::WindowFlag::Contextless))
Expand Down Expand Up @@ -684,6 +738,9 @@ Sdl2Application::~Sdl2Application() {
#endif
#endif

for(auto& cursor: _cursors)
SDL_FreeCursor(cursor);

#ifndef CORRADE_TARGET_EMSCRIPTEN
SDL_DestroyWindow(_window);
#endif
Expand Down
36 changes: 36 additions & 0 deletions src/Magnum/Platform/Sdl2Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/

#include <string>
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/ArrayView.h>
#include <Corrade/Containers/EnumSet.h>
#include <Corrade/Containers/Pointer.h>
Expand Down Expand Up @@ -761,6 +762,39 @@ class Sdl2Application {
*/
void redraw() { _flags |= Flag::Redraw; }

public:
enum class Cursor : UnsignedInt {
Arrow, /**< Arrow */
IBeam, /**< I-Beam */
Wait, /**< Wait */
Crosshair, /**< Crosshair */
WaitArrow, /**< Small wait cursor */
ResizeNWSE, /**< Double arrow pointing northwest and southeast */
ResizeNESW, /**< Double arrow pointing northeast and southwest */
ResizeWE, /**< Double arrow pointing west and east */
ResizeNS, /**< Double arrow pointing north and south */
ResizeAll, /**< Four pointed arrow pointing north, south, east, and west */
No, /**< Slashed circle or crossbones */
Hand /**< Hand */
};

public:
/**
* @brief Create a cursor that can then be used
*
* @see @ref setCursor()
*/
void createCursor(Cursor cursor);

/**
* @brief Set cursor
*
* The @p cursor is expected to be created from the @ref Configuration or
* with @ref createCursor().
* @see @ref createCursor()
*/
void setCursor(Cursor cursor);

private:
/**
* @brief Viewport event
Expand Down Expand Up @@ -1015,6 +1049,8 @@ class Sdl2Application {
typedef Containers::EnumSet<Flag> Flags;
CORRADE_ENUMSET_FRIEND_OPERATORS(Flags)

Containers::Array<SDL_Cursor*> _cursors{static_cast<std::size_t>(Cursor::Hand)};

/* These are saved from command-line arguments */
bool _verboseLog{};
Implementation::Sdl2DpiScalingPolicy _commandLineDpiScalingPolicy{};
Expand Down

0 comments on commit cbefac5

Please sign in to comment.