Some work on multiple inputs...

- Corrected bug and tested new gamepad system
- Reviewed Android key inputs system, unified with desktop
- Reorganize mouse functions on core
This commit is contained in:
raysan5 2016-10-17 18:18:13 +02:00
parent d5e0f4e84e
commit 0ce7f0c409
2 changed files with 110 additions and 125 deletions

View File

@ -131,7 +131,7 @@
#endif
#define MAX_GAMEPADS 4 // Max number of gamepads supported
#define MAX_GAMEPAD_BUTTONS 11 // Max bumber of buttons supported (per gamepad)
#define MAX_GAMEPAD_BUTTONS 32 // Max bumber of buttons supported (per gamepad)
#define MAX_GAMEPAD_AXIS 8 // Max number of axis supported (per gamepad)
#define RL_LOAD_DEFAULT_FONT // Load default font on window initialization (module: text)
@ -158,9 +158,6 @@ static const char *internalDataPath; // Android internal data path to
static bool windowReady = false; // Used to detect display initialization
static bool appEnabled = true; // Used to detec if app is active
static bool contextRebindRequired = false; // Used to know context rebind required
static int previousButtonState[128] = { 1 }; // Required to check if button pressed/released once
static int currentButtonState[128] = { 1 }; // Required to check if button pressed/released once
#endif
#if defined(PLATFORM_RPI)
@ -203,10 +200,6 @@ static Matrix downscaleView; // Matrix to downscale view (in case
static const char *windowTitle; // Window text title...
static bool cursorOnScreen = false; // Tracks if cursor is inside client area
// Register keyboard states
static char previousKeyState[512] = { 0 }; // Registers previous frame key state
static char currentKeyState[512] = { 0 }; // Registers current frame key state
// Register mouse states
static char previousMouseState[3] = { 0 }; // Registers previous mouse button state
static char currentMouseState[3] = { 0 }; // Registers current mouse button state
@ -221,11 +214,16 @@ static char currentGamepadState[MAX_GAMEPADS][MAX_GAMEPAD_BUTTONS] = { 0 };
// Keyboard configuration
static int exitKey = KEY_ESCAPE; // Default exit key (ESC)
static int lastKeyPressed = -1; // Register last key pressed
static bool cursorHidden; // Track if cursor is hidden
#endif
// Register keyboard states
static char previousKeyState[512] = { 0 }; // Registers previous frame key state
static char currentKeyState[512] = { 0 }; // Registers current frame key state
static int lastKeyPressed = -1; // Register last key pressed
static Vector2 mousePosition; // Mouse position on screen
static Vector2 touchPosition[MAX_TOUCH_POINTS]; // Touch position on screen
@ -526,6 +524,63 @@ int GetScreenHeight(void)
return screenHeight;
}
// Show mouse cursor
void ShowCursor()
{
#if defined(PLATFORM_DESKTOP)
#ifdef __linux
XUndefineCursor(glfwGetX11Display(), glfwGetX11Window(window));
#else
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
#endif
#endif
cursorHidden = false;
}
// Hide mouse cursor
void HideCursor()
{
#if defined(PLATFORM_DESKTOP)
#ifdef __linux
XColor Col;
const char Nil[] = {0};
Pixmap Pix = XCreateBitmapFromData(glfwGetX11Display(), glfwGetX11Window(window), Nil, 1, 1);
Cursor Cur = XCreatePixmapCursor(glfwGetX11Display(), Pix, Pix, &Col, &Col, 0, 0);
XDefineCursor(glfwGetX11Display(), glfwGetX11Window(window), Cur);
XFreeCursor(glfwGetX11Display(), Cur);
#else
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
#endif
#endif
cursorHidden = true;
}
// Check if mouse cursor is hidden
bool IsCursorHidden()
{
return cursorHidden;
}
// Enable mouse cursor
void EnableCursor()
{
#if defined(PLATFORM_DESKTOP)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
#endif
cursorHidden = false;
}
// Disable mouse cursor
void DisableCursor()
{
#if defined(PLATFORM_DESKTOP)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
#endif
cursorHidden = true;
}
// Sets Background Color
void ClearBackground(Color color)
{
@ -1050,8 +1105,13 @@ bool IsKeyPressed(int key)
{
bool pressed = false;
#if defined(PLATFORM_ANDROID)
if ((currentButtonState[key] != previousButtonState[key]) && (currentButtonState[key] == 0)) pressed = true;
else pressed = false;
#else
if ((currentKeyState[key] != previousKeyState[key]) && (currentKeyState[key] == 1)) pressed = true;
else pressed = false;
#endif
return pressed;
}
@ -1067,9 +1127,14 @@ bool IsKeyDown(int key)
bool IsKeyReleased(int key)
{
bool released = false;
#if defined(PLATFORM_ANDROID)
if ((currentButtonState[button] != previousButtonState[button]) && (currentButtonState[button] == 1)) released = true;
else released = false;
#else
if ((currentKeyState[key] != previousKeyState[key]) && (currentKeyState[key] == 0)) released = true;
else released = false;
#endif
return released;
}
@ -1091,64 +1156,9 @@ int GetKeyPressed(void)
// NOTE: default exitKey is ESCAPE
void SetExitKey(int key)
{
#if !defined(PLATFORM_ANDROID)
exitKey = key;
}
// Hide mouse cursor
void HideCursor()
{
#if defined(PLATFORM_DESKTOP)
#ifdef __linux
XColor Col;
const char Nil[] = {0};
Pixmap Pix = XCreateBitmapFromData(glfwGetX11Display(), glfwGetX11Window(window), Nil, 1, 1);
Cursor Cur = XCreatePixmapCursor(glfwGetX11Display(), Pix, Pix, &Col, &Col, 0, 0);
XDefineCursor(glfwGetX11Display(), glfwGetX11Window(window), Cur);
XFreeCursor(glfwGetX11Display(), Cur);
#else
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
#endif
#endif
cursorHidden = true;
}
// Show mouse cursor
void ShowCursor()
{
#if defined(PLATFORM_DESKTOP)
#ifdef __linux
XUndefineCursor(glfwGetX11Display(), glfwGetX11Window(window));
#else
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
#endif
#endif
cursorHidden = false;
}
// Disable mouse cursor
void DisableCursor()
{
#if defined(PLATFORM_DESKTOP)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
#endif
cursorHidden = true;
}
// Enable mouse cursor
void EnableCursor()
{
#if defined(PLATFORM_DESKTOP)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
#endif
cursorHidden = false;
}
// Check if mouse cursor is hidden
bool IsCursorHidden()
{
return cursorHidden;
}
// NOTE: Gamepad support not implemented in emscripten GLFW3 (PLATFORM_WEB)
@ -1204,7 +1214,7 @@ bool IsGamepadButtonDown(int gamepad, int button)
{
bool result = false;
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad] && (button < MAX_GAMEPAD_BUTTONS) &&
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad] && (button < MAX_GAMEPAD_BUTTONS) &&
(currentGamepadState[gamepad][button] == 1)) result = true;
return result;
@ -1387,37 +1397,6 @@ Vector2 GetTouchPosition(int index)
return position;
}
#if defined(PLATFORM_ANDROID)
// Detect if a button has been pressed once
bool IsButtonPressed(int button)
{
bool pressed = false;
if ((currentButtonState[button] != previousButtonState[button]) && (currentButtonState[button] == 0)) pressed = true;
else pressed = false;
return pressed;
}
// Detect if a button is being pressed (button held down)
bool IsButtonDown(int button)
{
if (currentButtonState[button] == 0) return true;
else return false;
}
// Detect if a button has been released once
bool IsButtonReleased(int button)
{
bool released = false;
if ((currentButtonState[button] != previousButtonState[button]) && (currentButtonState[button] == 1)) released = true;
else released = false;
return released;
}
#endif
//----------------------------------------------------------------------------------
// Module specific Functions Definition
//----------------------------------------------------------------------------------
@ -1900,8 +1879,9 @@ static bool GetKeyStatus(int key)
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
return glfwGetKey(window, key);
#elif defined(PLATFORM_ANDROID)
// TODO: Check for virtual keyboard
return false;
// NOTE: Android supports up to 260 keys
if (key < 0 || key > 260) return false;
else return currentKeyState[key];
#elif defined(PLATFORM_RPI)
// NOTE: Keys states are filled in PollInputEvents()
if (key < 0 || key > 511) return false;
@ -1929,6 +1909,9 @@ static void PollInputEvents(void)
// NOTE: Gestures update must be called every frame to reset gestures correctly
// because ProcessGestureEvent() is just called on an event, not every frame
UpdateGestures();
// Reset last key pressed registered
lastKeyPressed = -1;
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
// Mouse input polling
@ -1939,9 +1922,8 @@ static void PollInputEvents(void)
mousePosition.x = (float)mouseX;
mousePosition.y = (float)mouseY;
// Keyboard input polling (automatically managed by GLFW3 through callback)
lastKeyPressed = -1;
// Register previous keys states
for (int i = 0; i < 512; i++) previousKeyState[i] = currentKeyState[i];
@ -1971,7 +1953,7 @@ static void PollInputEvents(void)
for (int k = 0; (buttons != NULL) && (k < buttonsCount) && (buttonsCount < MAX_GAMEPAD_BUTTONS); k++)
{
if (buttons[i] == GLFW_PRESS) currentGamepadState[i][k] = 1;
if (buttons[k] == GLFW_PRESS) currentGamepadState[i][k] = 1;
else currentGamepadState[i][k] = 0;
}
@ -1994,7 +1976,8 @@ static void PollInputEvents(void)
#if defined(PLATFORM_ANDROID)
// Register previous keys states
for (int i = 0; i < 128; i++) previousButtonState[i] = currentButtonState[i];
// NOTE: Android supports up to 260 keys
for (int i = 0; i < 260; i++) previousKeyState[i] = currentKeyState[i];
// Poll Events (registered events)
// NOTE: Activity is paused if not enabled (appEnabled)
@ -2371,7 +2354,13 @@ static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event)
//int32_t AKeyEvent_getMetaState(event);
// Save current button and its state
currentButtonState[keycode] = AKeyEvent_getAction(event); // Down = 0, Up = 1
// NOTE: Android key action is 0 for down and 1 for up
if (AKeyEvent_getAction(event) == 0)
{
currentKeyState[keycode] = 1; // Key down
lastKeyPressed = keycode;
}
else currentKeyState[keycode] = 0; // Key up
if (keycode == AKEYCODE_POWER)
{

View File

@ -174,6 +174,14 @@
#define KEY_Y 89
#define KEY_Z 90
#if defined(PLATFORM_ANDROID)
// Android Physical Buttons
#define KEY_BACK 4
#define KEY_MENU 82
#define KEY_VOLUME_UP 24
#define KEY_VOLUME_DOWN 25
#endif
// Mouse Buttons
#define MOUSE_LEFT_BUTTON 0
#define MOUSE_RIGHT_BUTTON 1
@ -213,6 +221,13 @@
#define GAMEPAD_XBOX_BUTTON_RB 5
#define GAMEPAD_XBOX_BUTTON_SELECT 6
#define GAMEPAD_XBOX_BUTTON_START 7
#define GAMEPAD_XBOX_BUTTON_UP 10
#define GAMEPAD_XBOX_BUTTON_RIGHT 11
#define GAMEPAD_XBOX_BUTTON_DOWN 12
#define GAMEPAD_XBOX_BUTTON_LEFT 13
#define GAMEPAD_XBOX_AXIS_LEFT_X 0
#define GAMEPAD_XBOX_AXIS_LEFT_Y 1
#if defined(PLATFORM_RPI)
#define GAMEPAD_XBOX_AXIS_DPAD_X 7
@ -222,24 +237,11 @@
#define GAMEPAD_XBOX_AXIS_LT 2
#define GAMEPAD_XBOX_AXIS_RT 5
#else
#define GAMEPAD_XBOX_BUTTON_UP 10
#define GAMEPAD_XBOX_BUTTON_DOWN 12
#define GAMEPAD_XBOX_BUTTON_LEFT 13
#define GAMEPAD_XBOX_BUTTON_RIGHT 11
#define GAMEPAD_XBOX_AXIS_RIGHT_X 4
#define GAMEPAD_XBOX_AXIS_RIGHT_Y 3
#define GAMEPAD_XBOX_AXIS_LT_RT 2
#endif
#define GAMEPAD_XBOX_AXIS_LEFT_X 0
#define GAMEPAD_XBOX_AXIS_LEFT_Y 1
// Android Physic Buttons
#define ANDROID_BACK 4
#define ANDROID_MENU 82
#define ANDROID_VOLUME_UP 24
#define ANDROID_VOLUME_DOWN 25
// NOTE: MSC C++ compiler does not support compound literals (C99 feature)
// Plain structures in C++ (without constructors) can be initialized from { } initializers.
#ifdef __cplusplus
@ -648,7 +650,6 @@ RLAPI int StorageLoadValue(int position); // Storage loa
//------------------------------------------------------------------------------------
// Input Handling Functions (Module: core)
//------------------------------------------------------------------------------------
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB)
RLAPI bool IsKeyPressed(int key); // Detect if a key has been pressed once
RLAPI bool IsKeyDown(int key); // Detect if a key is being pressed
RLAPI bool IsKeyReleased(int key); // Detect if a key has been released once
@ -656,6 +657,7 @@ RLAPI bool IsKeyUp(int key); // Detect if a key
RLAPI int GetKeyPressed(void); // Get latest key pressed
RLAPI void SetExitKey(int key); // Set a custom key to exit program (default is ESC)
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB)
RLAPI bool IsGamepadAvailable(int gamepad); // Detect if a gamepad is available
RLAPI const char *GetGamepadName(int gamepad); // Return gamepad internal name id
RLAPI float GetGamepadAxisMovement(int gamepad, int axis); // Return axis movement value for a gamepad axis
@ -679,12 +681,6 @@ RLAPI int GetTouchX(void); // Returns touch p
RLAPI int GetTouchY(void); // Returns touch position Y for touch point 0 (relative to screen size)
RLAPI Vector2 GetTouchPosition(int index); // Returns touch position XY for a touch point index (relative to screen size)
#if defined(PLATFORM_ANDROID)
bool IsButtonPressed(int button); // Detect if an android physic button has been pressed
bool IsButtonDown(int button); // Detect if an android physic button is being pressed
bool IsButtonReleased(int button); // Detect if an android physic button has been released
#endif
//------------------------------------------------------------------------------------
// Gestures and Touch Handling Functions (Module: gestures)
//------------------------------------------------------------------------------------