Move some callbacks from core to desktop and web

This commit is contained in:
ubkp 2023-09-23 18:56:59 -03:00
parent 7a656ba477
commit 2ef9274493
3 changed files with 785 additions and 273 deletions

View file

@ -2446,279 +2446,279 @@ static void ScanDirectoryFilesRecursively(const char *basePath, FilePathList *fi
else TRACELOG(LOG_WARNING, "FILEIO: Directory cannot be opened (%s)", basePath); else TRACELOG(LOG_WARNING, "FILEIO: Directory cannot be opened (%s)", basePath);
} }
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) //#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
//
// GLFW3 WindowSize Callback, runs when window is resizedLastFrame //// GLFW3 WindowSize Callback, runs when window is resizedLastFrame
// NOTE: Window resizing not allowed by default //// NOTE: Window resizing not allowed by default
static void WindowSizeCallback(GLFWwindow *window, int width, int height) //static void WindowSizeCallback(GLFWwindow *window, int width, int height)
{ //{
// Reset viewport and projection matrix for new size // // Reset viewport and projection matrix for new size
SetupViewport(width, height); // SetupViewport(width, height);
//
CORE.Window.currentFbo.width = width; // CORE.Window.currentFbo.width = width;
CORE.Window.currentFbo.height = height; // CORE.Window.currentFbo.height = height;
CORE.Window.resizedLastFrame = true; // CORE.Window.resizedLastFrame = true;
//
if (IsWindowFullscreen()) return; // if (IsWindowFullscreen()) return;
//
// Set current screen size // // Set current screen size
#if defined(__APPLE__) //#if defined(__APPLE__)
CORE.Window.screen.width = width; // CORE.Window.screen.width = width;
CORE.Window.screen.height = height; // CORE.Window.screen.height = height;
#else //#else
if ((CORE.Window.flags & FLAG_WINDOW_HIGHDPI) > 0) // if ((CORE.Window.flags & FLAG_WINDOW_HIGHDPI) > 0)
{ // {
Vector2 windowScaleDPI = GetWindowScaleDPI(); // Vector2 windowScaleDPI = GetWindowScaleDPI();
//
CORE.Window.screen.width = (unsigned int)(width/windowScaleDPI.x); // CORE.Window.screen.width = (unsigned int)(width/windowScaleDPI.x);
CORE.Window.screen.height = (unsigned int)(height/windowScaleDPI.y); // CORE.Window.screen.height = (unsigned int)(height/windowScaleDPI.y);
} // }
else // else
{ // {
CORE.Window.screen.width = width; // CORE.Window.screen.width = width;
CORE.Window.screen.height = height; // CORE.Window.screen.height = height;
} // }
#endif //#endif
//
// NOTE: Postprocessing texture is not scaled to new size // // NOTE: Postprocessing texture is not scaled to new size
} //}
//
// GLFW3 WindowIconify Callback, runs when window is minimized/restored //// GLFW3 WindowIconify Callback, runs when window is minimized/restored
static void WindowIconifyCallback(GLFWwindow *window, int iconified) //static void WindowIconifyCallback(GLFWwindow *window, int iconified)
{ //{
if (iconified) CORE.Window.flags |= FLAG_WINDOW_MINIMIZED; // The window was iconified // if (iconified) CORE.Window.flags |= FLAG_WINDOW_MINIMIZED; // The window was iconified
else CORE.Window.flags &= ~FLAG_WINDOW_MINIMIZED; // The window was restored // else CORE.Window.flags &= ~FLAG_WINDOW_MINIMIZED; // The window was restored
} //}
//
// GLFW3 WindowFocus Callback, runs when window get/lose focus //// GLFW3 WindowFocus Callback, runs when window get/lose focus
static void WindowFocusCallback(GLFWwindow *window, int focused) //static void WindowFocusCallback(GLFWwindow *window, int focused)
{ //{
if (focused) CORE.Window.flags &= ~FLAG_WINDOW_UNFOCUSED; // The window was focused // if (focused) CORE.Window.flags &= ~FLAG_WINDOW_UNFOCUSED; // The window was focused
else CORE.Window.flags |= FLAG_WINDOW_UNFOCUSED; // The window lost focus // else CORE.Window.flags |= FLAG_WINDOW_UNFOCUSED; // The window lost focus
} //}
//
// GLFW3 Keyboard Callback, runs on key pressed //// GLFW3 Keyboard Callback, runs on key pressed
static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) //static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
{ //{
if (key < 0) return; // Security check, macOS fn key generates -1 // if (key < 0) return; // Security check, macOS fn key generates -1
//
// WARNING: GLFW could return GLFW_REPEAT, we need to consider it as 1 // // WARNING: GLFW could return GLFW_REPEAT, we need to consider it as 1
// to work properly with our implementation (IsKeyDown/IsKeyUp checks) // // to work properly with our implementation (IsKeyDown/IsKeyUp checks)
if (action == GLFW_RELEASE) CORE.Input.Keyboard.currentKeyState[key] = 0; // if (action == GLFW_RELEASE) CORE.Input.Keyboard.currentKeyState[key] = 0;
else if(action == GLFW_PRESS) CORE.Input.Keyboard.currentKeyState[key] = 1; // else if(action == GLFW_PRESS) CORE.Input.Keyboard.currentKeyState[key] = 1;
else if(action == GLFW_REPEAT) CORE.Input.Keyboard.keyRepeatInFrame[key] = 1; // else if(action == GLFW_REPEAT) CORE.Input.Keyboard.keyRepeatInFrame[key] = 1;
//
#if !defined(PLATFORM_WEB) //#if !defined(PLATFORM_WEB)
// WARNING: Check if CAPS/NUM key modifiers are enabled and force down state for those keys // // WARNING: Check if CAPS/NUM key modifiers are enabled and force down state for those keys
if (((key == KEY_CAPS_LOCK) && ((mods & GLFW_MOD_CAPS_LOCK) > 0)) || // if (((key == KEY_CAPS_LOCK) && ((mods & GLFW_MOD_CAPS_LOCK) > 0)) ||
((key == KEY_NUM_LOCK) && ((mods & GLFW_MOD_NUM_LOCK) > 0))) CORE.Input.Keyboard.currentKeyState[key] = 1; // ((key == KEY_NUM_LOCK) && ((mods & GLFW_MOD_NUM_LOCK) > 0))) CORE.Input.Keyboard.currentKeyState[key] = 1;
#endif //#endif
//
// Check if there is space available in the key queue // // Check if there is space available in the key queue
if ((CORE.Input.Keyboard.keyPressedQueueCount < MAX_KEY_PRESSED_QUEUE) && (action == GLFW_PRESS)) // if ((CORE.Input.Keyboard.keyPressedQueueCount < MAX_KEY_PRESSED_QUEUE) && (action == GLFW_PRESS))
{ // {
// Add character to the queue // // Add character to the queue
CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = key; // CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = key;
CORE.Input.Keyboard.keyPressedQueueCount++; // CORE.Input.Keyboard.keyPressedQueueCount++;
} // }
//
// Check the exit key to set close window // // Check the exit key to set close window
if ((key == CORE.Input.Keyboard.exitKey) && (action == GLFW_PRESS)) glfwSetWindowShouldClose(CORE.Window.handle, GLFW_TRUE); // if ((key == CORE.Input.Keyboard.exitKey) && (action == GLFW_PRESS)) glfwSetWindowShouldClose(CORE.Window.handle, GLFW_TRUE);
//
#if defined(SUPPORT_SCREEN_CAPTURE) //#if defined(SUPPORT_SCREEN_CAPTURE)
if ((key == GLFW_KEY_F12) && (action == GLFW_PRESS)) // if ((key == GLFW_KEY_F12) && (action == GLFW_PRESS))
{ // {
#if defined(SUPPORT_GIF_RECORDING) //#if defined(SUPPORT_GIF_RECORDING)
if (mods & GLFW_MOD_CONTROL) // if (mods & GLFW_MOD_CONTROL)
{ // {
if (gifRecording) // if (gifRecording)
{ // {
gifRecording = false; // gifRecording = false;
//
MsfGifResult result = msf_gif_end(&gifState); // MsfGifResult result = msf_gif_end(&gifState);
//
SaveFileData(TextFormat("%s/screenrec%03i.gif", CORE.Storage.basePath, screenshotCounter), result.data, (unsigned int)result.dataSize); // SaveFileData(TextFormat("%s/screenrec%03i.gif", CORE.Storage.basePath, screenshotCounter), result.data, (unsigned int)result.dataSize);
msf_gif_free(result); // msf_gif_free(result);
//
#if defined(PLATFORM_WEB) // #if defined(PLATFORM_WEB)
// Download file from MEMFS (emscripten memory filesystem) // // Download file from MEMFS (emscripten memory filesystem)
// saveFileFromMEMFSToDisk() function is defined in raylib/templates/web_shel/shell.html // // saveFileFromMEMFSToDisk() function is defined in raylib/templates/web_shel/shell.html
emscripten_run_script(TextFormat("saveFileFromMEMFSToDisk('%s','%s')", TextFormat("screenrec%03i.gif", screenshotCounter - 1), TextFormat("screenrec%03i.gif", screenshotCounter - 1))); // emscripten_run_script(TextFormat("saveFileFromMEMFSToDisk('%s','%s')", TextFormat("screenrec%03i.gif", screenshotCounter - 1), TextFormat("screenrec%03i.gif", screenshotCounter - 1)));
#endif // #endif
//
TRACELOG(LOG_INFO, "SYSTEM: Finish animated GIF recording"); // TRACELOG(LOG_INFO, "SYSTEM: Finish animated GIF recording");
} // }
else // else
{ // {
gifRecording = true; // gifRecording = true;
gifFrameCounter = 0; // gifFrameCounter = 0;
//
Vector2 scale = GetWindowScaleDPI(); // Vector2 scale = GetWindowScaleDPI();
msf_gif_begin(&gifState, (int)((float)CORE.Window.render.width*scale.x), (int)((float)CORE.Window.render.height*scale.y)); // msf_gif_begin(&gifState, (int)((float)CORE.Window.render.width*scale.x), (int)((float)CORE.Window.render.height*scale.y));
screenshotCounter++; // screenshotCounter++;
//
TRACELOG(LOG_INFO, "SYSTEM: Start animated GIF recording: %s", TextFormat("screenrec%03i.gif", screenshotCounter)); // TRACELOG(LOG_INFO, "SYSTEM: Start animated GIF recording: %s", TextFormat("screenrec%03i.gif", screenshotCounter));
} // }
} // }
else // else
#endif // SUPPORT_GIF_RECORDING //#endif // SUPPORT_GIF_RECORDING
{ // {
TakeScreenshot(TextFormat("screenshot%03i.png", screenshotCounter)); // TakeScreenshot(TextFormat("screenshot%03i.png", screenshotCounter));
screenshotCounter++; // screenshotCounter++;
} // }
} // }
#endif // SUPPORT_SCREEN_CAPTURE //#endif // SUPPORT_SCREEN_CAPTURE
//
#if defined(SUPPORT_EVENTS_AUTOMATION) //#if defined(SUPPORT_EVENTS_AUTOMATION)
if ((key == GLFW_KEY_F11) && (action == GLFW_PRESS)) // if ((key == GLFW_KEY_F11) && (action == GLFW_PRESS))
{ // {
eventsRecording = !eventsRecording; // eventsRecording = !eventsRecording;
//
// On finish recording, we export events into a file // // On finish recording, we export events into a file
if (!eventsRecording) ExportAutomationEvents("eventsrec.rep"); // if (!eventsRecording) ExportAutomationEvents("eventsrec.rep");
} // }
else if ((key == GLFW_KEY_F9) && (action == GLFW_PRESS)) // else if ((key == GLFW_KEY_F9) && (action == GLFW_PRESS))
{ // {
LoadAutomationEvents("eventsrec.rep"); // LoadAutomationEvents("eventsrec.rep");
eventsPlaying = true; // eventsPlaying = true;
//
TRACELOG(LOG_WARNING, "eventsPlaying enabled!"); // TRACELOG(LOG_WARNING, "eventsPlaying enabled!");
} // }
#endif //#endif
} //}
//
// GLFW3 Char Key Callback, runs on key down (gets equivalent unicode char value) //// GLFW3 Char Key Callback, runs on key down (gets equivalent unicode char value)
static void CharCallback(GLFWwindow *window, unsigned int key) //static void CharCallback(GLFWwindow *window, unsigned int key)
{ //{
//TRACELOG(LOG_DEBUG, "Char Callback: KEY:%i(%c)", key, key); // //TRACELOG(LOG_DEBUG, "Char Callback: KEY:%i(%c)", key, key);
//
// NOTE: Registers any key down considering OS keyboard layout but // // NOTE: Registers any key down considering OS keyboard layout but
// does not detect action events, those should be managed by user... // // does not detect action events, those should be managed by user...
// Ref: https://github.com/glfw/glfw/issues/668#issuecomment-166794907 // // Ref: https://github.com/glfw/glfw/issues/668#issuecomment-166794907
// Ref: https://www.glfw.org/docs/latest/input_guide.html#input_char // // Ref: https://www.glfw.org/docs/latest/input_guide.html#input_char
//
// Check if there is space available in the queue // // Check if there is space available in the queue
if (CORE.Input.Keyboard.charPressedQueueCount < MAX_CHAR_PRESSED_QUEUE) // if (CORE.Input.Keyboard.charPressedQueueCount < MAX_CHAR_PRESSED_QUEUE)
{ // {
// Add character to the queue // // Add character to the queue
CORE.Input.Keyboard.charPressedQueue[CORE.Input.Keyboard.charPressedQueueCount] = key; // CORE.Input.Keyboard.charPressedQueue[CORE.Input.Keyboard.charPressedQueueCount] = key;
CORE.Input.Keyboard.charPressedQueueCount++; // CORE.Input.Keyboard.charPressedQueueCount++;
} // }
} //}
//
// GLFW3 Mouse Button Callback, runs on mouse button pressed //// GLFW3 Mouse Button Callback, runs on mouse button pressed
static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods) //static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
{ //{
// WARNING: GLFW could only return GLFW_PRESS (1) or GLFW_RELEASE (0) for now, // // WARNING: GLFW could only return GLFW_PRESS (1) or GLFW_RELEASE (0) for now,
// but future releases may add more actions (i.e. GLFW_REPEAT) // // but future releases may add more actions (i.e. GLFW_REPEAT)
CORE.Input.Mouse.currentButtonState[button] = action; // CORE.Input.Mouse.currentButtonState[button] = action;
//
#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES) // PLATFORM_DESKTOP //#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES) // PLATFORM_DESKTOP
// Process mouse events as touches to be able to use mouse-gestures // // Process mouse events as touches to be able to use mouse-gestures
GestureEvent gestureEvent = { 0 }; // GestureEvent gestureEvent = { 0 };
//
// Register touch actions // // Register touch actions
if ((CORE.Input.Mouse.currentButtonState[button] == 1) && (CORE.Input.Mouse.previousButtonState[button] == 0)) gestureEvent.touchAction = TOUCH_ACTION_DOWN; // if ((CORE.Input.Mouse.currentButtonState[button] == 1) && (CORE.Input.Mouse.previousButtonState[button] == 0)) gestureEvent.touchAction = TOUCH_ACTION_DOWN;
else if ((CORE.Input.Mouse.currentButtonState[button] == 0) && (CORE.Input.Mouse.previousButtonState[button] == 1)) gestureEvent.touchAction = TOUCH_ACTION_UP; // else if ((CORE.Input.Mouse.currentButtonState[button] == 0) && (CORE.Input.Mouse.previousButtonState[button] == 1)) gestureEvent.touchAction = TOUCH_ACTION_UP;
//
// NOTE: TOUCH_ACTION_MOVE event is registered in MouseCursorPosCallback() // // NOTE: TOUCH_ACTION_MOVE event is registered in MouseCursorPosCallback()
//
// Assign a pointer ID // // Assign a pointer ID
gestureEvent.pointId[0] = 0; // gestureEvent.pointId[0] = 0;
//
// Register touch points count // // Register touch points count
gestureEvent.pointCount = 1; // gestureEvent.pointCount = 1;
//
// Register touch points position, only one point registered // // Register touch points position, only one point registered
gestureEvent.position[0] = GetMousePosition(); // gestureEvent.position[0] = GetMousePosition();
//
// Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height // // Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
gestureEvent.position[0].x /= (float)GetScreenWidth(); // gestureEvent.position[0].x /= (float)GetScreenWidth();
gestureEvent.position[0].y /= (float)GetScreenHeight(); // gestureEvent.position[0].y /= (float)GetScreenHeight();
//
// Gesture data is sent to gestures-system for processing // // Gesture data is sent to gestures-system for processing
#if defined(PLATFORM_WEB) //#if defined(PLATFORM_WEB)
// Prevent calling ProcessGestureEvent() when Emscripten is present and there's a touch gesture, so EmscriptenTouchCallback() can handle it itself // // Prevent calling ProcessGestureEvent() when Emscripten is present and there's a touch gesture, so EmscriptenTouchCallback() can handle it itself
if (GetMouseX() != 0 || GetMouseY() != 0) ProcessGestureEvent(gestureEvent); // if (GetMouseX() != 0 || GetMouseY() != 0) ProcessGestureEvent(gestureEvent);
#else //#else
ProcessGestureEvent(gestureEvent); // ProcessGestureEvent(gestureEvent);
#endif //#endif
//
#endif //#endif
} //}
//
// GLFW3 Cursor Position Callback, runs on mouse move //// GLFW3 Cursor Position Callback, runs on mouse move
static void MouseCursorPosCallback(GLFWwindow *window, double x, double y) //static void MouseCursorPosCallback(GLFWwindow *window, double x, double y)
{ //{
CORE.Input.Mouse.currentPosition.x = (float)x; // CORE.Input.Mouse.currentPosition.x = (float)x;
CORE.Input.Mouse.currentPosition.y = (float)y; // CORE.Input.Mouse.currentPosition.y = (float)y;
CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition; // CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;
//
#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES) // PLATFORM_DESKTOP //#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES) // PLATFORM_DESKTOP
// Process mouse events as touches to be able to use mouse-gestures // // Process mouse events as touches to be able to use mouse-gestures
GestureEvent gestureEvent = { 0 }; // GestureEvent gestureEvent = { 0 };
//
gestureEvent.touchAction = TOUCH_ACTION_MOVE; // gestureEvent.touchAction = TOUCH_ACTION_MOVE;
//
// Assign a pointer ID // // Assign a pointer ID
gestureEvent.pointId[0] = 0; // gestureEvent.pointId[0] = 0;
//
// Register touch points count // // Register touch points count
gestureEvent.pointCount = 1; // gestureEvent.pointCount = 1;
//
// Register touch points position, only one point registered // // Register touch points position, only one point registered
gestureEvent.position[0] = CORE.Input.Touch.position[0]; // gestureEvent.position[0] = CORE.Input.Touch.position[0];
//
// Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height // // Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
gestureEvent.position[0].x /= (float)GetScreenWidth(); // gestureEvent.position[0].x /= (float)GetScreenWidth();
gestureEvent.position[0].y /= (float)GetScreenHeight(); // gestureEvent.position[0].y /= (float)GetScreenHeight();
//
// Gesture data is sent to gestures-system for processing // // Gesture data is sent to gestures-system for processing
ProcessGestureEvent(gestureEvent); // ProcessGestureEvent(gestureEvent);
#endif //#endif
} //}
//
// GLFW3 Scrolling Callback, runs on mouse wheel //// GLFW3 Scrolling Callback, runs on mouse wheel
static void MouseScrollCallback(GLFWwindow *window, double xoffset, double yoffset) //static void MouseScrollCallback(GLFWwindow *window, double xoffset, double yoffset)
{ //{
CORE.Input.Mouse.currentWheelMove = (Vector2){ (float)xoffset, (float)yoffset }; // CORE.Input.Mouse.currentWheelMove = (Vector2){ (float)xoffset, (float)yoffset };
} //}
//
// GLFW3 CursorEnter Callback, when cursor enters the window //// GLFW3 CursorEnter Callback, when cursor enters the window
static void CursorEnterCallback(GLFWwindow *window, int enter) //static void CursorEnterCallback(GLFWwindow *window, int enter)
{ //{
if (enter == true) CORE.Input.Mouse.cursorOnScreen = true; // if (enter == true) CORE.Input.Mouse.cursorOnScreen = true;
else CORE.Input.Mouse.cursorOnScreen = false; // else CORE.Input.Mouse.cursorOnScreen = false;
} //}
//
// GLFW3 Window Drop Callback, runs when drop files into window //// GLFW3 Window Drop Callback, runs when drop files into window
static void WindowDropCallback(GLFWwindow *window, int count, const char **paths) //static void WindowDropCallback(GLFWwindow *window, int count, const char **paths)
{ //{
if (count > 0) // if (count > 0)
{ // {
// In case previous dropped filepaths have not been freed, we free them // // In case previous dropped filepaths have not been freed, we free them
if (CORE.Window.dropFileCount > 0) // if (CORE.Window.dropFileCount > 0)
{ // {
for (unsigned int i = 0; i < CORE.Window.dropFileCount; i++) RL_FREE(CORE.Window.dropFilepaths[i]); // for (unsigned int i = 0; i < CORE.Window.dropFileCount; i++) RL_FREE(CORE.Window.dropFilepaths[i]);
//
RL_FREE(CORE.Window.dropFilepaths); // RL_FREE(CORE.Window.dropFilepaths);
//
CORE.Window.dropFileCount = 0; // CORE.Window.dropFileCount = 0;
CORE.Window.dropFilepaths = NULL; // CORE.Window.dropFilepaths = NULL;
} // }
//
// WARNING: Paths are freed by GLFW when the callback returns, we must keep an internal copy // // WARNING: Paths are freed by GLFW when the callback returns, we must keep an internal copy
CORE.Window.dropFileCount = count; // CORE.Window.dropFileCount = count;
CORE.Window.dropFilepaths = (char **)RL_CALLOC(CORE.Window.dropFileCount, sizeof(char *)); // CORE.Window.dropFilepaths = (char **)RL_CALLOC(CORE.Window.dropFileCount, sizeof(char *));
//
for (unsigned int i = 0; i < CORE.Window.dropFileCount; i++) // for (unsigned int i = 0; i < CORE.Window.dropFileCount; i++)
{ // {
CORE.Window.dropFilepaths[i] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char)); // CORE.Window.dropFilepaths[i] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char));
strcpy(CORE.Window.dropFilepaths[i], paths[i]); // strcpy(CORE.Window.dropFilepaths[i], paths[i]);
} // }
} // }
} //}
#endif //#endif
#if defined(PLATFORM_DRM) #if defined(PLATFORM_DRM)
// Initialize Keyboard system (using standard input) // Initialize Keyboard system (using standard input)

View file

@ -1758,3 +1758,261 @@ void PollInputEvents(void)
if (CORE.Window.eventWaiting) glfwWaitEvents(); // Wait for in input events before continue (drawing is paused) if (CORE.Window.eventWaiting) glfwWaitEvents(); // Wait for in input events before continue (drawing is paused)
else glfwPollEvents(); // Poll input events: keyboard/mouse/window events (callbacks) else glfwPollEvents(); // Poll input events: keyboard/mouse/window events (callbacks)
} }
// GLFW3 WindowSize Callback, runs when window is resizedLastFrame
// NOTE: Window resizing not allowed by default
static void WindowSizeCallback(GLFWwindow *window, int width, int height)
{
// Reset viewport and projection matrix for new size
SetupViewport(width, height);
CORE.Window.currentFbo.width = width;
CORE.Window.currentFbo.height = height;
CORE.Window.resizedLastFrame = true;
if (IsWindowFullscreen()) return;
// Set current screen size
#if defined(__APPLE__)
CORE.Window.screen.width = width;
CORE.Window.screen.height = height;
#else
if ((CORE.Window.flags & FLAG_WINDOW_HIGHDPI) > 0)
{
Vector2 windowScaleDPI = GetWindowScaleDPI();
CORE.Window.screen.width = (unsigned int)(width/windowScaleDPI.x);
CORE.Window.screen.height = (unsigned int)(height/windowScaleDPI.y);
}
else
{
CORE.Window.screen.width = width;
CORE.Window.screen.height = height;
}
#endif
// NOTE: Postprocessing texture is not scaled to new size
}
// GLFW3 WindowIconify Callback, runs when window is minimized/restored
static void WindowIconifyCallback(GLFWwindow *window, int iconified)
{
if (iconified) CORE.Window.flags |= FLAG_WINDOW_MINIMIZED; // The window was iconified
else CORE.Window.flags &= ~FLAG_WINDOW_MINIMIZED; // The window was restored
}
// GLFW3 WindowFocus Callback, runs when window get/lose focus
static void WindowFocusCallback(GLFWwindow *window, int focused)
{
if (focused) CORE.Window.flags &= ~FLAG_WINDOW_UNFOCUSED; // The window was focused
else CORE.Window.flags |= FLAG_WINDOW_UNFOCUSED; // The window lost focus
}
// GLFW3 Keyboard Callback, runs on key pressed
static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
if (key < 0) return; // Security check, macOS fn key generates -1
// WARNING: GLFW could return GLFW_REPEAT, we need to consider it as 1
// to work properly with our implementation (IsKeyDown/IsKeyUp checks)
if (action == GLFW_RELEASE) CORE.Input.Keyboard.currentKeyState[key] = 0;
else if(action == GLFW_PRESS) CORE.Input.Keyboard.currentKeyState[key] = 1;
else if(action == GLFW_REPEAT) CORE.Input.Keyboard.keyRepeatInFrame[key] = 1;
// WARNING: Check if CAPS/NUM key modifiers are enabled and force down state for those keys
if (((key == KEY_CAPS_LOCK) && ((mods & GLFW_MOD_CAPS_LOCK) > 0)) ||
((key == KEY_NUM_LOCK) && ((mods & GLFW_MOD_NUM_LOCK) > 0))) CORE.Input.Keyboard.currentKeyState[key] = 1;
// Check if there is space available in the key queue
if ((CORE.Input.Keyboard.keyPressedQueueCount < MAX_KEY_PRESSED_QUEUE) && (action == GLFW_PRESS))
{
// Add character to the queue
CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = key;
CORE.Input.Keyboard.keyPressedQueueCount++;
}
// Check the exit key to set close window
if ((key == CORE.Input.Keyboard.exitKey) && (action == GLFW_PRESS)) glfwSetWindowShouldClose(CORE.Window.handle, GLFW_TRUE);
#if defined(SUPPORT_SCREEN_CAPTURE)
if ((key == GLFW_KEY_F12) && (action == GLFW_PRESS))
{
#if defined(SUPPORT_GIF_RECORDING)
if (mods & GLFW_MOD_CONTROL)
{
if (gifRecording)
{
gifRecording = false;
MsfGifResult result = msf_gif_end(&gifState);
SaveFileData(TextFormat("%s/screenrec%03i.gif", CORE.Storage.basePath, screenshotCounter), result.data, (unsigned int)result.dataSize);
msf_gif_free(result);
TRACELOG(LOG_INFO, "SYSTEM: Finish animated GIF recording");
}
else
{
gifRecording = true;
gifFrameCounter = 0;
Vector2 scale = GetWindowScaleDPI();
msf_gif_begin(&gifState, (int)((float)CORE.Window.render.width*scale.x), (int)((float)CORE.Window.render.height*scale.y));
screenshotCounter++;
TRACELOG(LOG_INFO, "SYSTEM: Start animated GIF recording: %s", TextFormat("screenrec%03i.gif", screenshotCounter));
}
}
else
#endif // SUPPORT_GIF_RECORDING
{
TakeScreenshot(TextFormat("screenshot%03i.png", screenshotCounter));
screenshotCounter++;
}
}
#endif // SUPPORT_SCREEN_CAPTURE
#if defined(SUPPORT_EVENTS_AUTOMATION)
if ((key == GLFW_KEY_F11) && (action == GLFW_PRESS))
{
eventsRecording = !eventsRecording;
// On finish recording, we export events into a file
if (!eventsRecording) ExportAutomationEvents("eventsrec.rep");
}
else if ((key == GLFW_KEY_F9) && (action == GLFW_PRESS))
{
LoadAutomationEvents("eventsrec.rep");
eventsPlaying = true;
TRACELOG(LOG_WARNING, "eventsPlaying enabled!");
}
#endif
}
// GLFW3 Char Key Callback, runs on key down (gets equivalent unicode char value)
static void CharCallback(GLFWwindow *window, unsigned int key)
{
//TRACELOG(LOG_DEBUG, "Char Callback: KEY:%i(%c)", key, key);
// NOTE: Registers any key down considering OS keyboard layout but
// does not detect action events, those should be managed by user...
// Ref: https://github.com/glfw/glfw/issues/668#issuecomment-166794907
// Ref: https://www.glfw.org/docs/latest/input_guide.html#input_char
// Check if there is space available in the queue
if (CORE.Input.Keyboard.charPressedQueueCount < MAX_CHAR_PRESSED_QUEUE)
{
// Add character to the queue
CORE.Input.Keyboard.charPressedQueue[CORE.Input.Keyboard.charPressedQueueCount] = key;
CORE.Input.Keyboard.charPressedQueueCount++;
}
}
// GLFW3 Mouse Button Callback, runs on mouse button pressed
static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
{
// WARNING: GLFW could only return GLFW_PRESS (1) or GLFW_RELEASE (0) for now,
// but future releases may add more actions (i.e. GLFW_REPEAT)
CORE.Input.Mouse.currentButtonState[button] = action;
#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES) // PLATFORM_DESKTOP
// Process mouse events as touches to be able to use mouse-gestures
GestureEvent gestureEvent = { 0 };
// Register touch actions
if ((CORE.Input.Mouse.currentButtonState[button] == 1) && (CORE.Input.Mouse.previousButtonState[button] == 0)) gestureEvent.touchAction = TOUCH_ACTION_DOWN;
else if ((CORE.Input.Mouse.currentButtonState[button] == 0) && (CORE.Input.Mouse.previousButtonState[button] == 1)) gestureEvent.touchAction = TOUCH_ACTION_UP;
// NOTE: TOUCH_ACTION_MOVE event is registered in MouseCursorPosCallback()
// Assign a pointer ID
gestureEvent.pointId[0] = 0;
// Register touch points count
gestureEvent.pointCount = 1;
// Register touch points position, only one point registered
gestureEvent.position[0] = GetMousePosition();
// Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
gestureEvent.position[0].x /= (float)GetScreenWidth();
gestureEvent.position[0].y /= (float)GetScreenHeight();
// Gesture data is sent to gestures-system for processing
ProcessGestureEvent(gestureEvent);
#endif
}
// GLFW3 Cursor Position Callback, runs on mouse move
static void MouseCursorPosCallback(GLFWwindow *window, double x, double y)
{
CORE.Input.Mouse.currentPosition.x = (float)x;
CORE.Input.Mouse.currentPosition.y = (float)y;
CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;
#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES) // PLATFORM_DESKTOP
// Process mouse events as touches to be able to use mouse-gestures
GestureEvent gestureEvent = { 0 };
gestureEvent.touchAction = TOUCH_ACTION_MOVE;
// Assign a pointer ID
gestureEvent.pointId[0] = 0;
// Register touch points count
gestureEvent.pointCount = 1;
// Register touch points position, only one point registered
gestureEvent.position[0] = CORE.Input.Touch.position[0];
// Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
gestureEvent.position[0].x /= (float)GetScreenWidth();
gestureEvent.position[0].y /= (float)GetScreenHeight();
// Gesture data is sent to gestures-system for processing
ProcessGestureEvent(gestureEvent);
#endif
}
// GLFW3 Scrolling Callback, runs on mouse wheel
static void MouseScrollCallback(GLFWwindow *window, double xoffset, double yoffset)
{
CORE.Input.Mouse.currentWheelMove = (Vector2){ (float)xoffset, (float)yoffset };
}
// GLFW3 CursorEnter Callback, when cursor enters the window
static void CursorEnterCallback(GLFWwindow *window, int enter)
{
if (enter == true) CORE.Input.Mouse.cursorOnScreen = true;
else CORE.Input.Mouse.cursorOnScreen = false;
}
// GLFW3 Window Drop Callback, runs when drop files into window
static void WindowDropCallback(GLFWwindow *window, int count, const char **paths)
{
if (count > 0)
{
// In case previous dropped filepaths have not been freed, we free them
if (CORE.Window.dropFileCount > 0)
{
for (unsigned int i = 0; i < CORE.Window.dropFileCount; i++) RL_FREE(CORE.Window.dropFilepaths[i]);
RL_FREE(CORE.Window.dropFilepaths);
CORE.Window.dropFileCount = 0;
CORE.Window.dropFilepaths = NULL;
}
// WARNING: Paths are freed by GLFW when the callback returns, we must keep an internal copy
CORE.Window.dropFileCount = count;
CORE.Window.dropFilepaths = (char **)RL_CALLOC(CORE.Window.dropFileCount, sizeof(char *));
for (unsigned int i = 0; i < CORE.Window.dropFileCount; i++)
{
CORE.Window.dropFilepaths[i] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char));
strcpy(CORE.Window.dropFilepaths[i], paths[i]);
}
}
}

View file

@ -1272,3 +1272,257 @@ void PollInputEvents(void)
} }
} }
} }
// GLFW3 WindowSize Callback, runs when window is resizedLastFrame
// NOTE: Window resizing not allowed by default
static void WindowSizeCallback(GLFWwindow *window, int width, int height)
{
// Reset viewport and projection matrix for new size
SetupViewport(width, height);
CORE.Window.currentFbo.width = width;
CORE.Window.currentFbo.height = height;
CORE.Window.resizedLastFrame = true;
if (IsWindowFullscreen()) return;
// Set current screen size
if ((CORE.Window.flags & FLAG_WINDOW_HIGHDPI) > 0)
{
Vector2 windowScaleDPI = GetWindowScaleDPI();
CORE.Window.screen.width = (unsigned int)(width/windowScaleDPI.x);
CORE.Window.screen.height = (unsigned int)(height/windowScaleDPI.y);
}
else
{
CORE.Window.screen.width = width;
CORE.Window.screen.height = height;
}
// NOTE: Postprocessing texture is not scaled to new size
}
// GLFW3 WindowIconify Callback, runs when window is minimized/restored
static void WindowIconifyCallback(GLFWwindow *window, int iconified)
{
if (iconified) CORE.Window.flags |= FLAG_WINDOW_MINIMIZED; // The window was iconified
else CORE.Window.flags &= ~FLAG_WINDOW_MINIMIZED; // The window was restored
}
// GLFW3 WindowFocus Callback, runs when window get/lose focus
static void WindowFocusCallback(GLFWwindow *window, int focused)
{
if (focused) CORE.Window.flags &= ~FLAG_WINDOW_UNFOCUSED; // The window was focused
else CORE.Window.flags |= FLAG_WINDOW_UNFOCUSED; // The window lost focus
}
// GLFW3 Keyboard Callback, runs on key pressed
static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
if (key < 0) return; // Security check, macOS fn key generates -1
// WARNING: GLFW could return GLFW_REPEAT, we need to consider it as 1
// to work properly with our implementation (IsKeyDown/IsKeyUp checks)
if (action == GLFW_RELEASE) CORE.Input.Keyboard.currentKeyState[key] = 0;
else if(action == GLFW_PRESS) CORE.Input.Keyboard.currentKeyState[key] = 1;
else if(action == GLFW_REPEAT) CORE.Input.Keyboard.keyRepeatInFrame[key] = 1;
// Check if there is space available in the key queue
if ((CORE.Input.Keyboard.keyPressedQueueCount < MAX_KEY_PRESSED_QUEUE) && (action == GLFW_PRESS))
{
// Add character to the queue
CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = key;
CORE.Input.Keyboard.keyPressedQueueCount++;
}
// Check the exit key to set close window
if ((key == CORE.Input.Keyboard.exitKey) && (action == GLFW_PRESS)) glfwSetWindowShouldClose(CORE.Window.handle, GLFW_TRUE);
#if defined(SUPPORT_SCREEN_CAPTURE)
if ((key == GLFW_KEY_F12) && (action == GLFW_PRESS))
{
#if defined(SUPPORT_GIF_RECORDING)
if (mods & GLFW_MOD_CONTROL)
{
if (gifRecording)
{
gifRecording = false;
MsfGifResult result = msf_gif_end(&gifState);
SaveFileData(TextFormat("%s/screenrec%03i.gif", CORE.Storage.basePath, screenshotCounter), result.data, (unsigned int)result.dataSize);
msf_gif_free(result);
// Download file from MEMFS (emscripten memory filesystem)
// saveFileFromMEMFSToDisk() function is defined in raylib/templates/web_shel/shell.html
emscripten_run_script(TextFormat("saveFileFromMEMFSToDisk('%s','%s')", TextFormat("screenrec%03i.gif", screenshotCounter - 1), TextFormat("screenrec%03i.gif", screenshotCounter - 1)));
TRACELOG(LOG_INFO, "SYSTEM: Finish animated GIF recording");
}
else
{
gifRecording = true;
gifFrameCounter = 0;
Vector2 scale = GetWindowScaleDPI();
msf_gif_begin(&gifState, (int)((float)CORE.Window.render.width*scale.x), (int)((float)CORE.Window.render.height*scale.y));
screenshotCounter++;
TRACELOG(LOG_INFO, "SYSTEM: Start animated GIF recording: %s", TextFormat("screenrec%03i.gif", screenshotCounter));
}
}
else
#endif // SUPPORT_GIF_RECORDING
{
TakeScreenshot(TextFormat("screenshot%03i.png", screenshotCounter));
screenshotCounter++;
}
}
#endif // SUPPORT_SCREEN_CAPTURE
#if defined(SUPPORT_EVENTS_AUTOMATION)
if ((key == GLFW_KEY_F11) && (action == GLFW_PRESS))
{
eventsRecording = !eventsRecording;
// On finish recording, we export events into a file
if (!eventsRecording) ExportAutomationEvents("eventsrec.rep");
}
else if ((key == GLFW_KEY_F9) && (action == GLFW_PRESS))
{
LoadAutomationEvents("eventsrec.rep");
eventsPlaying = true;
TRACELOG(LOG_WARNING, "eventsPlaying enabled!");
}
#endif
}
// GLFW3 Char Key Callback, runs on key down (gets equivalent unicode char value)
static void CharCallback(GLFWwindow *window, unsigned int key)
{
//TRACELOG(LOG_DEBUG, "Char Callback: KEY:%i(%c)", key, key);
// NOTE: Registers any key down considering OS keyboard layout but
// does not detect action events, those should be managed by user...
// Ref: https://github.com/glfw/glfw/issues/668#issuecomment-166794907
// Ref: https://www.glfw.org/docs/latest/input_guide.html#input_char
// Check if there is space available in the queue
if (CORE.Input.Keyboard.charPressedQueueCount < MAX_CHAR_PRESSED_QUEUE)
{
// Add character to the queue
CORE.Input.Keyboard.charPressedQueue[CORE.Input.Keyboard.charPressedQueueCount] = key;
CORE.Input.Keyboard.charPressedQueueCount++;
}
}
// GLFW3 Mouse Button Callback, runs on mouse button pressed
static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
{
// WARNING: GLFW could only return GLFW_PRESS (1) or GLFW_RELEASE (0) for now,
// but future releases may add more actions (i.e. GLFW_REPEAT)
CORE.Input.Mouse.currentButtonState[button] = action;
#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES) // PLATFORM_DESKTOP
// Process mouse events as touches to be able to use mouse-gestures
GestureEvent gestureEvent = { 0 };
// Register touch actions
if ((CORE.Input.Mouse.currentButtonState[button] == 1) && (CORE.Input.Mouse.previousButtonState[button] == 0)) gestureEvent.touchAction = TOUCH_ACTION_DOWN;
else if ((CORE.Input.Mouse.currentButtonState[button] == 0) && (CORE.Input.Mouse.previousButtonState[button] == 1)) gestureEvent.touchAction = TOUCH_ACTION_UP;
// NOTE: TOUCH_ACTION_MOVE event is registered in MouseCursorPosCallback()
// Assign a pointer ID
gestureEvent.pointId[0] = 0;
// Register touch points count
gestureEvent.pointCount = 1;
// Register touch points position, only one point registered
gestureEvent.position[0] = GetMousePosition();
// Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
gestureEvent.position[0].x /= (float)GetScreenWidth();
gestureEvent.position[0].y /= (float)GetScreenHeight();
// Gesture data is sent to gestures-system for processing
// Prevent calling ProcessGestureEvent() when Emscripten is present and there's a touch gesture, so EmscriptenTouchCallback() can handle it itself
if (GetMouseX() != 0 || GetMouseY() != 0) ProcessGestureEvent(gestureEvent);
#endif
}
// GLFW3 Cursor Position Callback, runs on mouse move
static void MouseCursorPosCallback(GLFWwindow *window, double x, double y)
{
CORE.Input.Mouse.currentPosition.x = (float)x;
CORE.Input.Mouse.currentPosition.y = (float)y;
CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;
#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES) // PLATFORM_DESKTOP
// Process mouse events as touches to be able to use mouse-gestures
GestureEvent gestureEvent = { 0 };
gestureEvent.touchAction = TOUCH_ACTION_MOVE;
// Assign a pointer ID
gestureEvent.pointId[0] = 0;
// Register touch points count
gestureEvent.pointCount = 1;
// Register touch points position, only one point registered
gestureEvent.position[0] = CORE.Input.Touch.position[0];
// Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
gestureEvent.position[0].x /= (float)GetScreenWidth();
gestureEvent.position[0].y /= (float)GetScreenHeight();
// Gesture data is sent to gestures-system for processing
ProcessGestureEvent(gestureEvent);
#endif
}
// GLFW3 Scrolling Callback, runs on mouse wheel
static void MouseScrollCallback(GLFWwindow *window, double xoffset, double yoffset)
{
CORE.Input.Mouse.currentWheelMove = (Vector2){ (float)xoffset, (float)yoffset };
}
// GLFW3 CursorEnter Callback, when cursor enters the window
static void CursorEnterCallback(GLFWwindow *window, int enter)
{
if (enter == true) CORE.Input.Mouse.cursorOnScreen = true;
else CORE.Input.Mouse.cursorOnScreen = false;
}
// GLFW3 Window Drop Callback, runs when drop files into window
static void WindowDropCallback(GLFWwindow *window, int count, const char **paths)
{
if (count > 0)
{
// In case previous dropped filepaths have not been freed, we free them
if (CORE.Window.dropFileCount > 0)
{
for (unsigned int i = 0; i < CORE.Window.dropFileCount; i++) RL_FREE(CORE.Window.dropFilepaths[i]);
RL_FREE(CORE.Window.dropFilepaths);
CORE.Window.dropFileCount = 0;
CORE.Window.dropFilepaths = NULL;
}
// WARNING: Paths are freed by GLFW when the callback returns, we must keep an internal copy
CORE.Window.dropFileCount = count;
CORE.Window.dropFilepaths = (char **)RL_CALLOC(CORE.Window.dropFileCount, sizeof(char *));
for (unsigned int i = 0; i < CORE.Window.dropFileCount; i++)
{
CORE.Window.dropFilepaths[i] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char));
strcpy(CORE.Window.dropFilepaths[i], paths[i]);
}
}
}