From f1d08f096f3ba35022dd0924aa1c6c636db1e27b Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Mon, 29 Dec 2014 12:28:14 -0800 Subject: [PATCH] Add Lime mouse cursor support on SDL2 --- lime/ui/Mouse.hx | 37 +++++++++++++ project/Build.xml | 1 + project/include/ui/Mouse.h | 27 ++++++++++ project/include/ui/MouseCursor.h | 21 ++++++++ project/src/ExternalInterface.cpp | 29 +++++++++++ project/src/backend/sdl/SDLMouse.cpp | 77 ++++++++++++++++++++++++++++ project/src/backend/sdl/SDLMouse.h | 27 ++++++++++ 7 files changed, 219 insertions(+) create mode 100644 project/include/ui/Mouse.h create mode 100644 project/include/ui/MouseCursor.h create mode 100644 project/src/backend/sdl/SDLMouse.cpp create mode 100644 project/src/backend/sdl/SDLMouse.h diff --git a/lime/ui/Mouse.hx b/lime/ui/Mouse.hx index 8d976c755..8682a78e2 100644 --- a/lime/ui/Mouse.hx +++ b/lime/ui/Mouse.hx @@ -2,6 +2,7 @@ package lime.ui; import lime.app.Application; +import lime.system.System; @:access(lime.app.Application) @@ -28,6 +29,10 @@ class Mouse { } + #elseif (cpp || neko || nodejs) + + lime_mouse_hide (); + #end __hidden = true; @@ -47,6 +52,10 @@ class Mouse { __cursor = null; cursor = cacheValue; + #elseif (cpp || neko || nodejs) + + lime_mouse_show (); + #end __hidden = false; @@ -91,6 +100,18 @@ class Mouse { } + #elseif (cpp || neko || nodejs) + + var type = switch (value) { + + case POINTER: MouseCursorType.POINTER; + case TEXT: MouseCursorType.TEXT; + default: MouseCursorType.DEFAULT; + + } + + lime_mouse_set_cursor (type); + #end } @@ -104,4 +125,20 @@ class Mouse { } + #if (cpp || neko || nodejs) + private static var lime_mouse_hide = System.load ("lime", "lime_mouse_hide", 0); + private static var lime_mouse_set_cursor = System.load ("lime", "lime_mouse_set_cursor", 1); + private static var lime_mouse_show = System.load ("lime", "lime_mouse_show", 0); + #end + + +} + + +@:enum private abstract MouseCursorType(Int) { + + var DEFAULT = 0; + var POINTER = 1; + var TEXT = 2; + } \ No newline at end of file diff --git a/project/Build.xml b/project/Build.xml index dcaca2103..6bee43798 100644 --- a/project/Build.xml +++ b/project/Build.xml @@ -132,6 +132,7 @@ + diff --git a/project/include/ui/Mouse.h b/project/include/ui/Mouse.h new file mode 100644 index 000000000..f444b6b89 --- /dev/null +++ b/project/include/ui/Mouse.h @@ -0,0 +1,27 @@ +#ifndef LIME_UI_MOUSE_H +#define LIME_UI_MOUSE_H + + +#include + + +namespace lime { + + + class Mouse { + + public: + + static MouseCursor currentCursor; + + static void Hide (); + static void SetCursor (MouseCursor cursor); + static void Show (); + + }; + + +} + + +#endif \ No newline at end of file diff --git a/project/include/ui/MouseCursor.h b/project/include/ui/MouseCursor.h new file mode 100644 index 000000000..e8f93d7d8 --- /dev/null +++ b/project/include/ui/MouseCursor.h @@ -0,0 +1,21 @@ +#ifndef LIME_UI_MOUSE_CURSOR_H +#define LIME_UI_MOUSE_CURSOR_H + + +namespace lime { + + + enum MouseCursor { + + DEFAULT, + POINTER, + TEXT, + CUSTOM + + }; + + +} + + +#endif \ No newline at end of file diff --git a/project/src/ExternalInterface.cpp b/project/src/ExternalInterface.cpp index 68d3eb2ef..5b20108a0 100644 --- a/project/src/ExternalInterface.cpp +++ b/project/src/ExternalInterface.cpp @@ -24,6 +24,8 @@ #include #include #include +#include +#include #include #include #include @@ -329,6 +331,30 @@ namespace lime { } + value lime_mouse_hide () { + + Mouse::Hide (); + return alloc_null (); + + } + + + value lime_mouse_set_cursor (value cursor) { + + Mouse::SetCursor ((MouseCursor)val_int (cursor)); + return alloc_null (); + + } + + + value lime_mouse_show () { + + Mouse::Show (); + return alloc_null (); + + } + + value lime_neko_execute (value module) { #ifdef LIME_NEKO @@ -490,6 +516,9 @@ namespace lime { DEFINE_PRIM (lime_key_event_manager_register, 2); DEFINE_PRIM (lime_lzma_encode, 1); DEFINE_PRIM (lime_lzma_decode, 1); + DEFINE_PRIM (lime_mouse_hide, 0); + DEFINE_PRIM (lime_mouse_set_cursor, 1); + DEFINE_PRIM (lime_mouse_show, 0); DEFINE_PRIM (lime_mouse_event_manager_register, 2); DEFINE_PRIM (lime_neko_execute, 1); DEFINE_PRIM (lime_renderer_create, 1); diff --git a/project/src/backend/sdl/SDLMouse.cpp b/project/src/backend/sdl/SDLMouse.cpp new file mode 100644 index 000000000..b8f9536d3 --- /dev/null +++ b/project/src/backend/sdl/SDLMouse.cpp @@ -0,0 +1,77 @@ +#include "SDLMouse.h" + + +namespace lime { + + + MouseCursor Mouse::currentCursor = DEFAULT; + SDL_Cursor* SDLMouse::defaultCursor = 0; + SDL_Cursor* SDLMouse::pointerCursor = 0; + SDL_Cursor* SDLMouse::textCursor = 0; + + + void Mouse::Hide () { + + SDL_ShowCursor (SDL_DISABLE); + + } + + + void Mouse::SetCursor (MouseCursor cursor) { + + if (cursor != Mouse::currentCursor) { + + switch (cursor) { + + case POINTER: + + if (!SDLMouse::pointerCursor) { + + SDLMouse::pointerCursor = SDL_CreateSystemCursor (SDL_SYSTEM_CURSOR_HAND); + + } + + SDL_SetCursor (SDLMouse::pointerCursor); + break; + + case TEXT: + + if (!SDLMouse::textCursor) { + + SDLMouse::textCursor = SDL_CreateSystemCursor (SDL_SYSTEM_CURSOR_IBEAM); + + } + + SDL_SetCursor (SDLMouse::textCursor); + break; + + default: + + if (!SDLMouse::defaultCursor) { + + SDLMouse::defaultCursor = SDL_CreateSystemCursor (SDL_SYSTEM_CURSOR_ARROW); + + } + + SDL_SetCursor (SDLMouse::defaultCursor); + break; + + } + + Mouse::currentCursor = cursor; + + } + + + + } + + + void Mouse::Show () { + + SDL_ShowCursor (SDL_ENABLE); + + } + + +} \ No newline at end of file diff --git a/project/src/backend/sdl/SDLMouse.h b/project/src/backend/sdl/SDLMouse.h new file mode 100644 index 000000000..a294d0c7f --- /dev/null +++ b/project/src/backend/sdl/SDLMouse.h @@ -0,0 +1,27 @@ +#ifndef LIME_SDL_MOUSE_H +#define LIME_SDL_MOUSE_H + + +#include +#include +#include + + +namespace lime { + + + class SDLMouse { + + public: + + static SDL_Cursor* defaultCursor; + static SDL_Cursor* pointerCursor; + static SDL_Cursor* textCursor; + + }; + + +} + + +#endif \ No newline at end of file