Added desktop cursor getter and setter functions (#1407)
* [core] Added desktop cursor getter and setter functions * Example: Set mouse cursor in text input box * Setup standard cursors _after_ GLFW window initialization * Remove old `int GetMouseWheelMove` declaration
This commit is contained in:
parent
eb3e9e4df5
commit
9833fe45eb
4 changed files with 84 additions and 1 deletions
|
@ -43,6 +43,9 @@ int main(void)
|
||||||
|
|
||||||
if (mouseOnText)
|
if (mouseOnText)
|
||||||
{
|
{
|
||||||
|
// Set the window's cursor to the I-Beam
|
||||||
|
SetMouseCursor(MOUSE_CURSOR_IBEAM);
|
||||||
|
|
||||||
// Get pressed key (character) on the queue
|
// Get pressed key (character) on the queue
|
||||||
int key = GetKeyPressed();
|
int key = GetKeyPressed();
|
||||||
|
|
||||||
|
@ -67,6 +70,10 @@ int main(void)
|
||||||
if (letterCount < 0) letterCount = 0;
|
if (letterCount < 0) letterCount = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (GetMouseCursor() != MOUSE_CURSOR_DEFAULT)
|
||||||
|
{
|
||||||
|
SetMouseCursor(MOUSE_CURSOR_DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
if (mouseOnText) framesCounter++;
|
if (mouseOnText) framesCounter++;
|
||||||
else framesCounter = 0;
|
else framesCounter = 0;
|
||||||
|
|
|
@ -148,6 +148,8 @@ RLAPI void SetMousePosition(int x, int y); // Set mouse posit
|
||||||
RLAPI void SetMouseOffset(int offsetX, int offsetY); // Set mouse offset
|
RLAPI void SetMouseOffset(int offsetX, int offsetY); // Set mouse offset
|
||||||
RLAPI void SetMouseScale(float scaleX, float scaleY); // Set mouse scaling
|
RLAPI void SetMouseScale(float scaleX, float scaleY); // Set mouse scaling
|
||||||
RLAPI float GetMouseWheelMove(void); // Returns mouse wheel movement Y
|
RLAPI float GetMouseWheelMove(void); // Returns mouse wheel movement Y
|
||||||
|
RLAPI MouseCursor GetMouseCursor(void); // Returns mouse cursor
|
||||||
|
RLAPI void SetMouseCursor(MouseCursor cursor); // Set mouse cursor
|
||||||
|
|
||||||
// Input-related functions: touch
|
// Input-related functions: touch
|
||||||
RLAPI int GetTouchX(void); // Returns touch position X for touch point 0 (relative to screen size)
|
RLAPI int GetTouchX(void); // Returns touch position X for touch point 0 (relative to screen size)
|
||||||
|
|
53
src/core.c
53
src/core.c
|
@ -423,6 +423,8 @@ typedef struct CoreData {
|
||||||
Vector2 offset; // Mouse offset
|
Vector2 offset; // Mouse offset
|
||||||
Vector2 scale; // Mouse scaling
|
Vector2 scale; // Mouse scaling
|
||||||
|
|
||||||
|
MouseCursor cursor; // Tracks current mouse cursor
|
||||||
|
void* standardCursors[10]; // Opaque pointers to GLFW cursors
|
||||||
bool cursorHidden; // Track if cursor is hidden
|
bool cursorHidden; // Track if cursor is hidden
|
||||||
bool cursorOnScreen; // Tracks if cursor is inside client area
|
bool cursorOnScreen; // Tracks if cursor is inside client area
|
||||||
|
|
||||||
|
@ -653,8 +655,9 @@ void InitWindow(int width, int height, const char *title)
|
||||||
// Initialize required global values different than 0
|
// Initialize required global values different than 0
|
||||||
CORE.Input.Keyboard.exitKey = KEY_ESCAPE;
|
CORE.Input.Keyboard.exitKey = KEY_ESCAPE;
|
||||||
CORE.Input.Mouse.scale = (Vector2){ 1.0f, 1.0f };
|
CORE.Input.Mouse.scale = (Vector2){ 1.0f, 1.0f };
|
||||||
|
CORE.Input.Mouse.cursor = MOUSE_CURSOR_ARROW;
|
||||||
CORE.Input.Gamepad.lastButtonPressed = -1;
|
CORE.Input.Gamepad.lastButtonPressed = -1;
|
||||||
|
|
||||||
#if defined(PLATFORM_UWP)
|
#if defined(PLATFORM_UWP)
|
||||||
// The axis count is 6 (2 thumbsticks and left and right trigger)
|
// The axis count is 6 (2 thumbsticks and left and right trigger)
|
||||||
CORE.Input.Gamepad.axisCount = 6;
|
CORE.Input.Gamepad.axisCount = 6;
|
||||||
|
@ -753,6 +756,26 @@ void InitWindow(int width, int height, const char *title)
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(PLATFORM_DESKTOP)
|
||||||
|
// Initialize GLFW's standard cursors
|
||||||
|
const int shapes[] = {
|
||||||
|
0x00036001, // MOUSE_CURSOR_ARROW
|
||||||
|
0x00036002, // MOUSE_CURSOR_IBEAM
|
||||||
|
0x00036003, // MOUSE_CURSOR_CROSSHAIR
|
||||||
|
0x00036004, // MOUSE_CURSOR_POINTING_HAND
|
||||||
|
0x00036005, // MOUSE_CURSOR_RESIZE_EW
|
||||||
|
0x00036006, // MOUSE_CURSOR_RESIZE_NS
|
||||||
|
0x00036007, // MOUSE_CURSOR_RESIZE_NWSE
|
||||||
|
0x00036008, // MOUSE_CURSOR_RESIZE_NESW
|
||||||
|
0x00036009, // MOUSE_CURSOR_RESIZE_ALL
|
||||||
|
0x0003600A, // MOUSE_CURSOR_NOT_ALLOWED
|
||||||
|
};
|
||||||
|
for (int i = 0; i < sizeof(CORE.Input.Mouse.standardCursors) / sizeof(CORE.Input.Mouse.standardCursors[0]); i += 1)
|
||||||
|
{
|
||||||
|
CORE.Input.Mouse.standardCursors[i] = glfwCreateStandardCursor(shapes[i]);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(PLATFORM_WEB)
|
#if defined(PLATFORM_WEB)
|
||||||
// Detect fullscreen change events
|
// Detect fullscreen change events
|
||||||
emscripten_set_fullscreenchange_callback("#canvas", NULL, 1, EmscriptenFullscreenChangeCallback);
|
emscripten_set_fullscreenchange_callback("#canvas", NULL, 1, EmscriptenFullscreenChangeCallback);
|
||||||
|
@ -797,6 +820,11 @@ void CloseWindow(void)
|
||||||
|
|
||||||
rlglClose(); // De-init rlgl
|
rlglClose(); // De-init rlgl
|
||||||
|
|
||||||
|
#if defined(PLATFORM_DESKTOP)
|
||||||
|
for (int i = 0; i < sizeof(CORE.Input.Mouse.standardCursors) / sizeof(CORE.Input.Mouse.standardCursors[0]); i += 1)
|
||||||
|
glfwDestroyCursor(CORE.Input.Mouse.standardCursors[i]);
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
||||||
glfwDestroyWindow(CORE.Window.handle);
|
glfwDestroyWindow(CORE.Window.handle);
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
@ -2733,6 +2761,29 @@ float GetMouseWheelMove(void)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns mouse cursor
|
||||||
|
MouseCursor GetMouseCursor(void)
|
||||||
|
{
|
||||||
|
return CORE.Input.Mouse.cursor;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set mouse cursor
|
||||||
|
// NOTE: This is a no-op on platforms other than PLATFORM_DESKTOP
|
||||||
|
void SetMouseCursor(MouseCursor cursor)
|
||||||
|
{
|
||||||
|
#if defined(PLATFORM_DESKTOP)
|
||||||
|
CORE.Input.Mouse.cursor = cursor;
|
||||||
|
if (cursor == MOUSE_CURSOR_DEFAULT)
|
||||||
|
{
|
||||||
|
glfwSetCursor(CORE.Window.handle, NULL);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
glfwSetCursor(CORE.Window.handle, CORE.Input.Mouse.standardCursors[cursor]);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
// Returns touch position X for touch point 0 (relative to screen size)
|
// Returns touch position X for touch point 0 (relative to screen size)
|
||||||
int GetTouchX(void)
|
int GetTouchX(void)
|
||||||
{
|
{
|
||||||
|
|
23
src/raylib.h
23
src/raylib.h
|
@ -617,6 +617,27 @@ typedef enum {
|
||||||
MOUSE_MIDDLE_BUTTON = 2
|
MOUSE_MIDDLE_BUTTON = 2
|
||||||
} MouseButton;
|
} MouseButton;
|
||||||
|
|
||||||
|
// Mouse cursor types
|
||||||
|
typedef enum {
|
||||||
|
MOUSE_CURSOR_DEFAULT = -1,
|
||||||
|
MOUSE_CURSOR_ARROW = 0,
|
||||||
|
MOUSE_CURSOR_IBEAM = 1,
|
||||||
|
MOUSE_CURSOR_CROSSHAIR = 2,
|
||||||
|
MOUSE_CURSOR_POINTING_HAND = 3,
|
||||||
|
// The horizontal resize/move arrow shape.
|
||||||
|
MOUSE_CURSOR_RESIZE_EW = 4,
|
||||||
|
// The vertical resize/move arrow shape.
|
||||||
|
MOUSE_CURSOR_RESIZE_NS = 5,
|
||||||
|
// The top-left to bottom-right diagonal resize/move arrow shape.
|
||||||
|
MOUSE_CURSOR_RESIZE_NWSE = 6,
|
||||||
|
// The top-right to bottom-left diagonal resize/move arrow shape.
|
||||||
|
MOUSE_CURSOR_RESIZE_NESW = 7,
|
||||||
|
// The omni-directional resize/move cursor shape.
|
||||||
|
MOUSE_CURSOR_RESIZE_ALL = 8,
|
||||||
|
// The operation-not-allowed shape.
|
||||||
|
MOUSE_CURSOR_NOT_ALLOWED = 9
|
||||||
|
} MouseCursor;
|
||||||
|
|
||||||
// Gamepad number
|
// Gamepad number
|
||||||
typedef enum {
|
typedef enum {
|
||||||
GAMEPAD_PLAYER1 = 0,
|
GAMEPAD_PLAYER1 = 0,
|
||||||
|
@ -1016,6 +1037,8 @@ RLAPI void SetMousePosition(int x, int y); // Set mouse posit
|
||||||
RLAPI void SetMouseOffset(int offsetX, int offsetY); // Set mouse offset
|
RLAPI void SetMouseOffset(int offsetX, int offsetY); // Set mouse offset
|
||||||
RLAPI void SetMouseScale(float scaleX, float scaleY); // Set mouse scaling
|
RLAPI void SetMouseScale(float scaleX, float scaleY); // Set mouse scaling
|
||||||
RLAPI float GetMouseWheelMove(void); // Returns mouse wheel movement Y
|
RLAPI float GetMouseWheelMove(void); // Returns mouse wheel movement Y
|
||||||
|
RLAPI MouseCursor GetMouseCursor(void); // Returns mouse cursor
|
||||||
|
RLAPI void SetMouseCursor(MouseCursor cursor); // Set mouse cursor
|
||||||
|
|
||||||
// Input-related functions: touch
|
// Input-related functions: touch
|
||||||
RLAPI int GetTouchX(void); // Returns touch position X for touch point 0 (relative to screen size)
|
RLAPI int GetTouchX(void); // Returns touch position X for touch point 0 (relative to screen size)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue