From 4bc13ef2753375419313f60856973715b55a49ee Mon Sep 17 00:00:00 2001 From: Agent X <44549182+Agent-11@users.noreply.github.com> Date: Fri, 13 May 2022 22:55:47 -0400 Subject: [PATCH] djui_hud_get_raw_mouse_x/y, djui_hud_set_mouse_locked (#91) --- autogen/lua_definitions/functions.lua | 16 ++++++++ docs/lua/functions-3.md | 56 +++++++++++++++++++++++++++ docs/lua/functions.md | 3 ++ src/pc/controller/controller_sdl2.c | 25 +++++++++++- src/pc/djui/djui_hud_utils.c | 13 +++++++ src/pc/djui/djui_hud_utils.h | 4 ++ src/pc/djui/djui_panel_camera.c | 2 + src/pc/lua/smlua_functions_autogen.c | 32 +++++++++++++++ 8 files changed, 149 insertions(+), 2 deletions(-) diff --git a/autogen/lua_definitions/functions.lua b/autogen/lua_definitions/functions.lua index fdae952a..aeebeb84 100644 --- a/autogen/lua_definitions/functions.lua +++ b/autogen/lua_definitions/functions.lua @@ -3575,6 +3575,16 @@ function djui_hud_get_mouse_y() -- ... end +--- @return number +function djui_hud_get_raw_mouse_x() + -- ... +end + +--- @return number +function djui_hud_get_raw_mouse_y() + -- ... +end + --- @return integer function djui_hud_get_screen_height() -- ... @@ -3637,6 +3647,12 @@ function djui_hud_set_font(fontType) -- ... end +--- @param locked boolean +--- @return nil +function djui_hud_set_mouse_locked(locked) + -- ... +end + --- @param resolutionType HudUtilsResolution --- @return nil function djui_hud_set_resolution(resolutionType) diff --git a/docs/lua/functions-3.md b/docs/lua/functions-3.md index 8a56c89b..ee9e7787 100644 --- a/docs/lua/functions-3.md +++ b/docs/lua/functions-3.md @@ -2033,6 +2033,42 @@
+## [djui_hud_get_raw_mouse_x](#djui_hud_get_raw_mouse_x) + +### Lua Example +`local numberValue = djui_hud_get_raw_mouse_x()` + +### Parameters +- None + +### Returns +- `number` + +### C Prototype +`f32 djui_hud_get_raw_mouse_x(void);` + +[:arrow_up_small:](#) + +
+ +## [djui_hud_get_raw_mouse_y](#djui_hud_get_raw_mouse_y) + +### Lua Example +`local numberValue = djui_hud_get_raw_mouse_y()` + +### Parameters +- None + +### Returns +- `number` + +### C Prototype +`f32 djui_hud_get_raw_mouse_y(void);` + +[:arrow_up_small:](#) + +
+ ## [djui_hud_get_screen_height](#djui_hud_get_screen_height) ### Lua Example @@ -2205,6 +2241,26 @@
+## [djui_hud_set_mouse_locked](#djui_hud_set_mouse_locked) + +### Lua Example +`djui_hud_set_mouse_locked(locked)` + +### Parameters +| Field | Type | +| ----- | ---- | +| locked | `boolean` | + +### Returns +- None + +### C Prototype +`void djui_hud_set_mouse_locked(bool locked);` + +[:arrow_up_small:](#) + +
+ ## [djui_hud_set_resolution](#djui_hud_set_resolution) ### Lua Example diff --git a/docs/lua/functions.md b/docs/lua/functions.md index 1aeae144..586339b3 100644 --- a/docs/lua/functions.md +++ b/docs/lua/functions.md @@ -704,6 +704,8 @@ - djui_hud_utils.h - [djui_hud_get_mouse_x](functions-3.md#djui_hud_get_mouse_x) - [djui_hud_get_mouse_y](functions-3.md#djui_hud_get_mouse_y) + - [djui_hud_get_raw_mouse_x](functions-3.md#djui_hud_get_raw_mouse_x) + - [djui_hud_get_raw_mouse_y](functions-3.md#djui_hud_get_raw_mouse_y) - [djui_hud_get_screen_height](functions-3.md#djui_hud_get_screen_height) - [djui_hud_get_screen_width](functions-3.md#djui_hud_get_screen_width) - [djui_hud_measure_text](functions-3.md#djui_hud_measure_text) @@ -712,6 +714,7 @@ - [djui_hud_render_rect_interpolated](functions-3.md#djui_hud_render_rect_interpolated) - [djui_hud_set_color](functions-3.md#djui_hud_set_color) - [djui_hud_set_font](functions-3.md#djui_hud_set_font) + - [djui_hud_set_mouse_locked](functions-3.md#djui_hud_set_mouse_locked) - [djui_hud_set_resolution](functions-3.md#djui_hud_set_resolution) - [djui_hud_world_pos_to_screen_pos](functions-3.md#djui_hud_world_pos_to_screen_pos) diff --git a/src/pc/controller/controller_sdl2.c b/src/pc/controller/controller_sdl2.c index 5437e06a..b8057b4b 100644 --- a/src/pc/controller/controller_sdl2.c +++ b/src/pc/controller/controller_sdl2.c @@ -26,6 +26,7 @@ #include "game/level_update.h" #include "pc/djui/djui.h" +#include "pc/djui/djui_hud_utils.h" #define MAX_JOYBINDS 32 #define MAX_MOUSEBUTTONS 8 // arbitrary @@ -160,16 +161,20 @@ static inline void update_button(const int i, const bool new) { } } +u8 ignore_lock = FALSE; static void controller_sdl_read(OSContPad *pad) { if (!init_ok) { return; } #ifdef BETTERCAMERA - if (newcam_mouse == 1 && sCurrPlayMode != 2) + if (newcam_mouse == 1 && sCurrPlayMode != 2) { SDL_SetRelativeMouseMode(SDL_TRUE); - else + ignore_lock = TRUE; + } else { SDL_SetRelativeMouseMode(SDL_FALSE); + ignore_lock = FALSE; + } u32 mouse = SDL_GetRelativeMouseState(&mouse_x, &mouse_y); @@ -182,6 +187,22 @@ static void controller_sdl_read(OSContPad *pad) { last_mouse = (mouse_buttons ^ mouse) & mouse; mouse_buttons = mouse; #endif + if (!ignore_lock && sCurrPlayMode != 2) { + SDL_SetRelativeMouseMode(gDjuiHudLockMouse ? SDL_TRUE : SDL_FALSE); + +#ifndef BETTERCAMERA + u32 mouse = SDL_GetRelativeMouseState(&mouse_x, &mouse_y); + + if (!gInteractableOverridePad) { + for (u32 i = 0; i < num_mouse_binds; ++i) + if (mouse & SDL_BUTTON(mouse_binds[i][0])) + pad->button |= mouse_binds[i][1]; + } + // remember buttons that changed from 0 to 1 + last_mouse = (mouse_buttons ^ mouse) & mouse; + mouse_buttons = mouse; +#endif + } SDL_GameControllerUpdate(); diff --git a/src/pc/djui/djui_hud_utils.c b/src/pc/djui/djui_hud_utils.c index 91680839..d9c1efcc 100644 --- a/src/pc/djui/djui_hud_utils.c +++ b/src/pc/djui/djui_hud_utils.c @@ -22,6 +22,7 @@ static enum HudUtilsResolution sResolution = RESOLUTION_DJUI; static enum DjuiFontType sFont = FONT_NORMAL; f32 gDjuiHudUtilsZ = 0; +u8 gDjuiHudLockMouse = FALSE; extern ALIGNED8 const u8 texture_hud_char_camera[]; extern ALIGNED8 const u8 texture_hud_char_lakitu[]; @@ -162,6 +163,18 @@ f32 djui_hud_get_mouse_y(void) { return mouse_window_y / djui_gfx_get_scale(); } +f32 djui_hud_get_raw_mouse_x(void) { + return mouse_x; +} + +f32 djui_hud_get_raw_mouse_y(void) { + return mouse_y; +} + +void djui_hud_set_mouse_locked(bool locked) { + gDjuiHudLockMouse = locked; +} + f32 djui_hud_measure_text(const char* message) { if (message == NULL) { return 0; } const struct DjuiFont* font = gDjuiFonts[sFont]; diff --git a/src/pc/djui/djui_hud_utils.h b/src/pc/djui/djui_hud_utils.h index 8ae7b704..70a54755 100644 --- a/src/pc/djui/djui_hud_utils.h +++ b/src/pc/djui/djui_hud_utils.h @@ -26,6 +26,7 @@ struct GlobalTextures { extern struct GlobalTextures gGlobalTextures; extern f32 gDjuiHudUtilsZ; +extern u8 gDjuiHudLockMouse; void djui_hud_set_resolution(enum HudUtilsResolution resolutionType); void djui_hud_set_font(enum DjuiFontType fontType); @@ -36,6 +37,9 @@ u32 djui_hud_get_screen_height(void); f32 djui_hud_get_mouse_x(void); f32 djui_hud_get_mouse_y(void); +f32 djui_hud_get_raw_mouse_x(void); +f32 djui_hud_get_raw_mouse_y(void); +void djui_hud_set_mouse_locked(bool locked); f32 djui_hud_measure_text(const char* message); void djui_hud_print_text(const char* message, float x, float y, float scale); diff --git a/src/pc/djui/djui_panel_camera.c b/src/pc/djui/djui_panel_camera.c index 5184cef8..95b13d21 100644 --- a/src/pc/djui/djui_panel_camera.c +++ b/src/pc/djui/djui_panel_camera.c @@ -26,10 +26,12 @@ void djui_panel_camera_create(struct DjuiBase* caller) { djui_interactable_hook_value_change(&checkbox2->base, djui_panel_camera_value_changed); djui_base_set_size(&checkbox2->base, 1.0f, 32); +#ifdef BETTERCAMERA struct DjuiCheckbox* checkbox3 = djui_checkbox_create(&body->base, "Mouse Look", &configCameraMouse); djui_base_set_size_type(&checkbox3->base, DJUI_SVT_RELATIVE, DJUI_SVT_ABSOLUTE); djui_interactable_hook_value_change(&checkbox3->base, djui_panel_camera_value_changed); djui_base_set_size(&checkbox3->base, 1.0f, 32); +#endif struct DjuiCheckbox* checkbox4 = djui_checkbox_create(&body->base, "Invert X", &configCameraInvertX); djui_base_set_size_type(&checkbox4->base, DJUI_SVT_RELATIVE, DJUI_SVT_ABSOLUTE); diff --git a/src/pc/lua/smlua_functions_autogen.c b/src/pc/lua/smlua_functions_autogen.c index 8f0d8deb..9ece0bd5 100644 --- a/src/pc/lua/smlua_functions_autogen.c +++ b/src/pc/lua/smlua_functions_autogen.c @@ -7204,6 +7204,24 @@ int smlua_func_djui_hud_get_mouse_y(UNUSED lua_State* L) { return 1; } +int smlua_func_djui_hud_get_raw_mouse_x(UNUSED lua_State* L) { + if(!smlua_functions_valid_param_count(L, 0)) { return 0; } + + + lua_pushnumber(L, djui_hud_get_raw_mouse_x()); + + return 1; +} + +int smlua_func_djui_hud_get_raw_mouse_y(UNUSED lua_State* L) { + if(!smlua_functions_valid_param_count(L, 0)) { return 0; } + + + lua_pushnumber(L, djui_hud_get_raw_mouse_y()); + + return 1; +} + int smlua_func_djui_hud_get_screen_height(UNUSED lua_State* L) { if(!smlua_functions_valid_param_count(L, 0)) { return 0; } @@ -7320,6 +7338,17 @@ int smlua_func_djui_hud_set_font(lua_State* L) { return 1; } +int smlua_func_djui_hud_set_mouse_locked(lua_State* L) { + if(!smlua_functions_valid_param_count(L, 1)) { return 0; } + + bool locked = smlua_to_boolean(L, 1); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 1"); return 0; } + + djui_hud_set_mouse_locked(locked); + + return 1; +} + int smlua_func_djui_hud_set_resolution(lua_State* L) { if(!smlua_functions_valid_param_count(L, 1)) { return 0; } @@ -16856,6 +16885,8 @@ void smlua_bind_functions_autogen(void) { // djui_hud_utils.h smlua_bind_function(L, "djui_hud_get_mouse_x", smlua_func_djui_hud_get_mouse_x); smlua_bind_function(L, "djui_hud_get_mouse_y", smlua_func_djui_hud_get_mouse_y); + smlua_bind_function(L, "djui_hud_get_raw_mouse_x", smlua_func_djui_hud_get_raw_mouse_x); + smlua_bind_function(L, "djui_hud_get_raw_mouse_y", smlua_func_djui_hud_get_raw_mouse_y); smlua_bind_function(L, "djui_hud_get_screen_height", smlua_func_djui_hud_get_screen_height); smlua_bind_function(L, "djui_hud_get_screen_width", smlua_func_djui_hud_get_screen_width); smlua_bind_function(L, "djui_hud_measure_text", smlua_func_djui_hud_measure_text); @@ -16864,6 +16895,7 @@ void smlua_bind_functions_autogen(void) { smlua_bind_function(L, "djui_hud_render_rect_interpolated", smlua_func_djui_hud_render_rect_interpolated); smlua_bind_function(L, "djui_hud_set_color", smlua_func_djui_hud_set_color); smlua_bind_function(L, "djui_hud_set_font", smlua_func_djui_hud_set_font); + smlua_bind_function(L, "djui_hud_set_mouse_locked", smlua_func_djui_hud_set_mouse_locked); smlua_bind_function(L, "djui_hud_set_resolution", smlua_func_djui_hud_set_resolution); smlua_bind_function(L, "djui_hud_world_pos_to_screen_pos", smlua_func_djui_hud_world_pos_to_screen_pos);