Skip to content

Commit 6638806

Browse files
committed
sdl: add cursor management
1 parent ac2b074 commit 6638806

1 file changed

Lines changed: 71 additions & 4 deletions

File tree

src/ruisapp/glue/sdl/glue.cxx

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
#include <atomic>
2323

2424
#include <utki/config.hpp>
25+
#include <utki/enum_array.hpp>
2526
#include <utki/unicode.hpp>
2627

2728
#include "../../application.hpp"
@@ -367,7 +368,74 @@ class window_wrapper : public utki::destructable
367368
sdl_wrapper& operator=(sdl_wrapper&&) = delete;
368369
} sdl;
369370

371+
class sdl_cursor_wrapper
372+
{
373+
SDL_Cursor* sdl_cursor = nullptr;
374+
375+
public:
376+
sdl_cursor_wrapper() = default;
377+
378+
void set(ruis::mouse_cursor cursor)
379+
{
380+
ASSERT(!this->sdl_cursor)
381+
382+
utki::enum_array<SDL_SystemCursor, ruis::mouse_cursor> cursor_mapping = {
383+
SDL_SYSTEM_CURSOR_ARROW, // none
384+
SDL_SYSTEM_CURSOR_ARROW, // arrow
385+
SDL_SYSTEM_CURSOR_SIZEWE, // left_right_arrow
386+
SDL_SYSTEM_CURSOR_SIZENS, // up_down_arrow
387+
SDL_SYSTEM_CURSOR_SIZEALL, // all_directions_arrow
388+
SDL_SYSTEM_CURSOR_SIZEWE, // left_side
389+
SDL_SYSTEM_CURSOR_SIZEWE, // right_side
390+
SDL_SYSTEM_CURSOR_SIZENS, // top_side
391+
SDL_SYSTEM_CURSOR_SIZENS, // bottom_side
392+
SDL_SYSTEM_CURSOR_SIZEALL, // top_left_corner
393+
SDL_SYSTEM_CURSOR_SIZEALL, // top_right_corner
394+
SDL_SYSTEM_CURSOR_SIZEALL, // bottom_left_corner
395+
SDL_SYSTEM_CURSOR_SIZEALL, // bottom_right_corner
396+
SDL_SYSTEM_CURSOR_CROSSHAIR, // index_finger
397+
SDL_SYSTEM_CURSOR_HAND, // grab
398+
SDL_SYSTEM_CURSOR_IBEAM, // caret
399+
// TODO:
400+
// SDL_SYSTEM_CURSOR_WAIT
401+
// SDL_SYSTEM_CURSOR_NO
402+
};
403+
404+
this->sdl_cursor = SDL_CreateSystemCursor(cursor_mapping[cursor]);
405+
}
406+
407+
~sdl_cursor_wrapper()
408+
{
409+
if (this->sdl_cursor) {
410+
SDL_FreeCursor(this->sdl_cursor);
411+
}
412+
}
413+
414+
bool empty() const noexcept
415+
{
416+
return this->sdl_cursor == nullptr;
417+
}
418+
419+
void activate()
420+
{
421+
ASSERT(!this->empty())
422+
423+
SDL_SetCursor(this->sdl_cursor);
424+
}
425+
};
426+
427+
utki::enum_array<sdl_cursor_wrapper, ruis::mouse_cursor> mouse_cursors;
428+
370429
public:
430+
void set_cursor(ruis::mouse_cursor cursor)
431+
{
432+
auto& c = this->mouse_cursors[cursor];
433+
if (c.empty()) {
434+
c.set(cursor);
435+
}
436+
c.activate();
437+
}
438+
371439
class sdl_window_wrapper
372440
{
373441
public:
@@ -645,10 +713,9 @@ application::application(std::string name, const window_params& wp) :
645713
SDL_PushEvent(&e);
646714
},
647715
.set_mouse_cursor_function =
648-
[](ruis::mouse_cursor c) {
649-
// TODO:
650-
// auto& ww = get_impl(*this);
651-
// ww.set_cursor(c);
716+
[this](ruis::mouse_cursor c) {
717+
auto& ww = get_impl(*this);
718+
ww.set_cursor(c);
652719
},
653720
.units = ruis::units(
654721
get_dpi(), //

0 commit comments

Comments
 (0)