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
memnew(AppWindow());
memnew(Input());
Input::get_singleton()->set_main_loop(this);
_init_window();
}

View File

@ -16,6 +16,19 @@ class Application : public Object {
SFW_OBJECT(Application, Object);
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;
int target_fps;

View File

@ -20,13 +20,11 @@ Input *Input::get_singleton() {
}
void Input::set_mouse_mode(MouseMode p_mode) {
//ERR_FAIL_INDEX((int)p_mode, 4);
//OS::get_singleton()->set_mouse_mode((OS::MouseMode)p_mode);
AppWindow::get_singleton()->set_mouse_mode((AppWindow::MouseMode)p_mode);
}
Input::MouseMode Input::get_mouse_mode() const {
//return (MouseMode)OS::get_singleton()->get_mouse_mode();
return MOUSE_MODE_VISIBLE;
return (MouseMode)AppWindow::get_singleton()->get_mouse_mode();
}
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) {
ERR_PRINT("GLFWkeyfunCallback");
}
void Input::GLFWcharfunCallback(GLFWwindow *window, unsigned int codepoint) {
ERR_PRINT("GLFWcharfunCallback");
@ -1037,18 +1034,30 @@ void Input::GLFWjoystickfunCallback(int jid, int event) {
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() {
GLFWwindow *window = (GLFWwindow *)AppWindow::get_singleton()->get_handle();
glfwSetKeyCallback(window, &Input::GLFWkeyfunCallback);
glfwSetCharCallback(window, &Input::GLFWcharfunCallback);
glfwSetCharModsCallback(window, &Input::GLFWcharmodsfunCallback);
//glfwSetCharCallback(window, &Input::GLFWcharfunCallback);
//glfwSetCharModsCallback(window, &Input::GLFWcharmodsfunCallback);
glfwSetMouseButtonCallback(window, &Input::GLFWmousebuttonfunCallback);
glfwSetCursorPosCallback(window, &Input::GLFWcursorposfunCallback);
glfwSetCursorEnterCallback(window, &Input::GLFWcursorenterfunCallback);
glfwSetScrollCallback(window, &Input::GLFWscrollfunCallback);
glfwSetDropCallback(window, &Input::GLFWdropfunCallback);
glfwSetJoystickCallback(&Input::GLFWjoystickfunCallback);
glfwSetWindowFocusCallback(window, &Input::GLFWwindowfocusfunCallback);
}
Input::JoyEvent Input::_get_mapped_button_event(const JoyDeviceMapping &mapping, int p_button) {

View File

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

View File

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

View File

@ -69,8 +69,14 @@ public:
void set_fullscreen(int enabled);
int has_fullscreen();
void set_cursor(int visible);
int has_cursor();
enum MouseMode {
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);
int has_pause();
@ -93,6 +99,7 @@ public:
double get_fps();
double get_fps_target();
void fps_lock(float fps);
void fps_unlock();