Setup handling window focus events. Also cleaned up mouse pointer handling.

This commit is contained in:
Relintai 2024-01-03 15:35:25 +01:00
parent 1a761c83ca
commit 9d0476a48d
6 changed files with 66 additions and 20 deletions

View File

@ -97,6 +97,7 @@ Application::Application() {
// TODO add a helper static method // TODO add a helper static method
memnew(AppWindow()); memnew(AppWindow());
memnew(Input()); memnew(Input());
Input::get_singleton()->set_main_loop(this);
_init_window(); _init_window();
} }

View File

@ -16,6 +16,19 @@ class Application : public Object {
SFW_OBJECT(Application, Object); SFW_OBJECT(Application, Object);
public: public:
enum {
NOTIFICATION_WM_MOUSE_ENTER = 1002,
NOTIFICATION_WM_MOUSE_EXIT = 1003,
NOTIFICATION_WM_FOCUS_IN = 1004,
NOTIFICATION_WM_FOCUS_OUT = 1005,
NOTIFICATION_WM_QUIT_REQUEST = 1006,
NOTIFICATION_WM_GO_BACK_REQUEST = 1007,
NOTIFICATION_WM_UNFOCUS_REQUEST = 1008,
NOTIFICATION_APP_RESUMED = 1014,
NOTIFICATION_APP_PAUSED = 1015,
};
bool running; bool running;
int target_fps; int target_fps;

View File

@ -20,13 +20,11 @@ Input *Input::get_singleton() {
} }
void Input::set_mouse_mode(MouseMode p_mode) { void Input::set_mouse_mode(MouseMode p_mode) {
//ERR_FAIL_INDEX((int)p_mode, 4); AppWindow::get_singleton()->set_mouse_mode((AppWindow::MouseMode)p_mode);
//OS::get_singleton()->set_mouse_mode((OS::MouseMode)p_mode);
} }
Input::MouseMode Input::get_mouse_mode() const { Input::MouseMode Input::get_mouse_mode() const {
//return (MouseMode)OS::get_singleton()->get_mouse_mode(); return (MouseMode)AppWindow::get_singleton()->get_mouse_mode();
return MOUSE_MODE_VISIBLE;
} }
bool Input::is_key_pressed(int p_scancode) const { bool Input::is_key_pressed(int p_scancode) const {
@ -1010,7 +1008,6 @@ Input::Input() {
} }
void Input::GLFWkeyfunCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { void Input::GLFWkeyfunCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
ERR_PRINT("GLFWkeyfunCallback");
} }
void Input::GLFWcharfunCallback(GLFWwindow *window, unsigned int codepoint) { void Input::GLFWcharfunCallback(GLFWwindow *window, unsigned int codepoint) {
ERR_PRINT("GLFWcharfunCallback"); ERR_PRINT("GLFWcharfunCallback");
@ -1037,18 +1034,30 @@ void Input::GLFWjoystickfunCallback(int jid, int event) {
ERR_PRINT("GLFWjoystickfunCallback"); ERR_PRINT("GLFWjoystickfunCallback");
} }
void Input::GLFWwindowfocusfunCallback(GLFWwindow *window, int focused) {
Input *self = Input::get_singleton();
if (focused) {
self->main_loop->notification(Application::NOTIFICATION_WM_FOCUS_IN);
} else {
self->release_pressed_events();
self->main_loop->notification(Application::NOTIFICATION_WM_FOCUS_OUT);
}
}
void Input::_setup_window_callbacks() { void Input::_setup_window_callbacks() {
GLFWwindow *window = (GLFWwindow *)AppWindow::get_singleton()->get_handle(); GLFWwindow *window = (GLFWwindow *)AppWindow::get_singleton()->get_handle();
glfwSetKeyCallback(window, &Input::GLFWkeyfunCallback); glfwSetKeyCallback(window, &Input::GLFWkeyfunCallback);
glfwSetCharCallback(window, &Input::GLFWcharfunCallback); //glfwSetCharCallback(window, &Input::GLFWcharfunCallback);
glfwSetCharModsCallback(window, &Input::GLFWcharmodsfunCallback); //glfwSetCharModsCallback(window, &Input::GLFWcharmodsfunCallback);
glfwSetMouseButtonCallback(window, &Input::GLFWmousebuttonfunCallback); glfwSetMouseButtonCallback(window, &Input::GLFWmousebuttonfunCallback);
glfwSetCursorPosCallback(window, &Input::GLFWcursorposfunCallback); glfwSetCursorPosCallback(window, &Input::GLFWcursorposfunCallback);
glfwSetCursorEnterCallback(window, &Input::GLFWcursorenterfunCallback); glfwSetCursorEnterCallback(window, &Input::GLFWcursorenterfunCallback);
glfwSetScrollCallback(window, &Input::GLFWscrollfunCallback); glfwSetScrollCallback(window, &Input::GLFWscrollfunCallback);
glfwSetDropCallback(window, &Input::GLFWdropfunCallback); glfwSetDropCallback(window, &Input::GLFWdropfunCallback);
glfwSetJoystickCallback(&Input::GLFWjoystickfunCallback); glfwSetJoystickCallback(&Input::GLFWjoystickfunCallback);
glfwSetWindowFocusCallback(window, &Input::GLFWwindowfocusfunCallback);
} }
Input::JoyEvent Input::_get_mapped_button_event(const JoyDeviceMapping &mapping, int p_button) { Input::JoyEvent Input::_get_mapped_button_event(const JoyDeviceMapping &mapping, int p_button) {

View File

@ -30,8 +30,7 @@ public:
enum MouseMode { enum MouseMode {
MOUSE_MODE_VISIBLE, MOUSE_MODE_VISIBLE,
MOUSE_MODE_HIDDEN, MOUSE_MODE_HIDDEN,
MOUSE_MODE_CAPTURED, MOUSE_MODE_CAPTURED
MOUSE_MODE_CONFINED
}; };
#undef CursorShape #undef CursorShape
@ -196,6 +195,7 @@ protected:
static void GLFWscrollfunCallback(GLFWwindow *window, double xoffset, double yoffset); static void GLFWscrollfunCallback(GLFWwindow *window, double xoffset, double yoffset);
static void GLFWdropfunCallback(GLFWwindow *window, int path_count, const char *paths[]); static void GLFWdropfunCallback(GLFWwindow *window, int path_count, const char *paths[]);
static void GLFWjoystickfunCallback(int jid, int event); static void GLFWjoystickfunCallback(int jid, int event);
static void GLFWwindowfocusfunCallback(GLFWwindow *window, int focused);
protected: protected:
enum JoyType { enum JoyType {

View File

@ -826,7 +826,7 @@ void AppWindow::set_focus() {
glfwFocusWindow(_window); glfwFocusWindow(_window);
} }
int AppWindow::has_focus() { int AppWindow::has_focus() {
return !!glfwGetWindowAttrib(_window, GLFW_FOCUSED); return glfwGetWindowAttrib(_window, GLFW_FOCUSED);
} }
void AppWindow::create_default_cursors() { void AppWindow::create_default_cursors() {
@ -856,17 +856,33 @@ void AppWindow::set_cursor_shape(unsigned mode) {
create_default_cursors(); create_default_cursors();
if (mode == CURSOR_SW_AUTO) { // UI (nuklear) driven cursor
glfwSetCursor(_window, cursors[0]);
} else {
glfwSetCursor(_window, mode < 7 ? cursors[mode] : NULL); glfwSetCursor(_window, mode < 7 ? cursors[mode] : NULL);
}
void AppWindow::set_mouse_mode(MouseMode p_mode) {
int im;
if (p_mode == MOUSE_MODE_HIDDEN) {
im = GLFW_CURSOR_HIDDEN;
} else if (p_mode == MOUSE_MODE_CAPTURED) {
im = GLFW_CURSOR_DISABLED;
} else {
//MOUSE_MODE_VISIBLE
im = GLFW_CURSOR_NORMAL;
} }
glfwSetInputMode(_window, GLFW_CURSOR, im);
} }
void AppWindow::set_cursor(int visible) { AppWindow::MouseMode AppWindow::get_mouse_mode() const {
glfwSetInputMode(_window, GLFW_CURSOR, visible ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_DISABLED); int im = glfwGetInputMode(_window, GLFW_CURSOR);
}
int AppWindow::has_cursor() { if (im == GLFW_CURSOR_HIDDEN) {
return glfwGetInputMode(_window, GLFW_CURSOR) == GLFW_CURSOR_NORMAL; return MOUSE_MODE_HIDDEN;
} else if (im == GLFW_CURSOR_DISABLED) {
return MOUSE_MODE_CAPTURED;
}
return MOUSE_MODE_VISIBLE;
} }
void AppWindow::set_visible(int visible) { void AppWindow::set_visible(int visible) {

View File

@ -69,8 +69,14 @@ public:
void set_fullscreen(int enabled); void set_fullscreen(int enabled);
int has_fullscreen(); int has_fullscreen();
void set_cursor(int visible); enum MouseMode {
int has_cursor(); MOUSE_MODE_VISIBLE, //=GLFW_CURSOR_NORMAL
MOUSE_MODE_HIDDEN, //=GLFW_CURSOR_HIDDEN
MOUSE_MODE_CAPTURED //=GLFW_CURSOR_DISABLED
};
void set_mouse_mode(MouseMode p_mode);
MouseMode get_mouse_mode() const;
void set_pause(int paused); void set_pause(int paused);
int has_pause(); int has_pause();
@ -93,6 +99,7 @@ public:
double get_fps(); double get_fps();
double get_fps_target(); double get_fps_target();
void fps_lock(float fps); void fps_lock(float fps);
void fps_unlock(); void fps_unlock();