From d19a21d9ffe34ef7f3580cc51bde5bd4fec74ac6 Mon Sep 17 00:00:00 2001 From: grorp Date: Wed, 22 Jan 2025 14:36:43 -0500 Subject: [PATCH 1/3] Add setting change callbacks for camera settings --- src/client/camera.cpp | 27 +++++++++++++++++++++------ src/client/camera.h | 3 +++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/client/camera.cpp b/src/client/camera.cpp index 76e914b80fdfc..b88bd19e8043e 100644 --- a/src/client/camera.cpp +++ b/src/client/camera.cpp @@ -33,6 +33,11 @@ static constexpr f32 CAMERA_OFFSET_STEP = 200; #define WIELDMESH_AMPLITUDE_X 7.0f #define WIELDMESH_AMPLITUDE_Y 10.0f +static const char *setting_names[] = { + "fall_bobbing_amount", "view_bobbing_amount", "fov", "arm_inertia", + "show_nametag_backgrounds", +}; + Camera::Camera(MapDrawControl &draw_control, Client *client, RenderingEngine *rendering_engine): m_draw_control(draw_control), m_client(client), @@ -54,11 +59,21 @@ Camera::Camera(MapDrawControl &draw_control, Client *client, RenderingEngine *re m_wieldnode->setItem(ItemStack(), m_client); m_wieldnode->drop(); // m_wieldmgr grabbed it - /* TODO: Add a callback function so these can be updated when a setting - * changes. At this point in time it doesn't matter (e.g. /set - * is documented to change server settings only) - * - * TODO: Local caching of settings is not optimal and should at some stage + m_nametags.clear(); + + readSettings(); + for (const auto &name : setting_names) + g_settings->registerChangedCallback(name, settingChangedCallback, this); +} + +void Camera::settingChangedCallback(const std::string &name, void *data) +{ + static_cast(data)->readSettings(); +} + +void Camera::readSettings() +{ + /* TODO: Local caching of settings is not optimal and should at some stage * be updated to use a global settings object for getting thse values * (as opposed to the this local caching). This can be addressed in * a later release. @@ -69,12 +84,12 @@ Camera::Camera(MapDrawControl &draw_control, Client *client, RenderingEngine *re // as a zoom FOV and load world beyond the set server limits. m_cache_fov = g_settings->getFloat("fov", 45.0f, 160.0f); m_arm_inertia = g_settings->getBool("arm_inertia"); - m_nametags.clear(); m_show_nametag_backgrounds = g_settings->getBool("show_nametag_backgrounds"); } Camera::~Camera() { + g_settings->deregisterAllChangedCallbacks(this); m_wieldmgr->drop(); } diff --git a/src/client/camera.h b/src/client/camera.h index de6f753a0f27e..fe05ed329a46a 100644 --- a/src/client/camera.h +++ b/src/client/camera.h @@ -70,6 +70,9 @@ class Camera Camera(MapDrawControl &draw_control, Client *client, RenderingEngine *rendering_engine); ~Camera(); + static void settingChangedCallback(const std::string &name, void *data); + void readSettings(); + // Get camera scene node. // It has the eye transformation, pitch and view bobbing applied. inline scene::ICameraSceneNode* getCameraNode() const From 5cabd19ff4221353e1fe6f023111ba5d8c664cdd Mon Sep 17 00:00:00 2001 From: grorp Date: Wed, 22 Jan 2025 14:36:44 -0500 Subject: [PATCH 2/3] Add setting change callbacks for touch controls settings --- src/client/game.cpp | 8 ++++++-- src/gui/touchcontrols.cpp | 26 ++++++++++++++++++++++++-- src/gui/touchcontrols.h | 25 ++++++++++++++++--------- src/gui/touchscreeneditor.cpp | 7 +------ 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/src/client/game.cpp b/src/client/game.cpp index e9d6278d0f130..665176982a30c 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -820,6 +820,8 @@ Game::Game() : &settingChangedCallback, this); g_settings->registerChangedCallback("pause_on_lost_focus", &settingChangedCallback, this); + g_settings->registerChangedCallback("touch_use_crosshair", + &settingChangedCallback, this); readSettings(); } @@ -896,8 +898,6 @@ bool Game::startup(bool *kill, m_first_loop_after_window_activation = true; - m_touch_use_crosshair = g_settings->getBool("touch_use_crosshair"); - g_client_translations->clear(); // address can change if simple_singleplayer_mode @@ -4111,6 +4111,10 @@ void Game::readSettings() m_invert_hotbar_mouse_wheel = g_settings->getBool("invert_hotbar_mouse_wheel"); m_does_lost_focus_pause_game = g_settings->getBool("pause_on_lost_focus"); + + m_touch_use_crosshair = g_settings->getBool("touch_use_crosshair"); + if (g_touchcontrols) + g_touchcontrols->setUseCrosshair(!isTouchCrosshairDisabled()); } /****************************************************************************/ diff --git a/src/gui/touchcontrols.cpp b/src/gui/touchcontrols.cpp index 334c664e4727c..1ae5f78ae34a1 100644 --- a/src/gui/touchcontrols.cpp +++ b/src/gui/touchcontrols.cpp @@ -201,19 +201,40 @@ static EKEY_CODE id_to_keycode(touch_gui_button_id id) } +static const char *setting_names[] = { + "touchscreen_threshold", "touch_long_tap_delay", + "fixed_virtual_joystick", "virtual_joystick_triggers_aux1", + "touch_layout", +}; + TouchControls::TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc): m_device(device), m_guienv(device->getGUIEnvironment()), m_receiver(device->getEventReceiver()), m_texturesource(tsrc) +{ + m_screensize = m_device->getVideoDriver()->getScreenSize(); + m_button_size = ButtonLayout::getButtonSize(m_screensize); + + readSettings(); + for (const char *name : setting_names) + g_settings->registerChangedCallback(name, settingChangedCallback, this); +} + +void TouchControls::settingChangedCallback(const std::string &name, void *data) +{ + static_cast(data)->readSettings(); +} + +void TouchControls::readSettings() { m_touchscreen_threshold = g_settings->getU16("touchscreen_threshold"); m_long_tap_delay = g_settings->getU16("touch_long_tap_delay"); m_fixed_joystick = g_settings->getBool("fixed_virtual_joystick"); m_joystick_triggers_aux1 = g_settings->getBool("virtual_joystick_triggers_aux1"); - m_screensize = m_device->getVideoDriver()->getScreenSize(); - m_button_size = ButtonLayout::getButtonSize(m_screensize); + // Note that "fixed_virtual_joystick" and "virtual_joystick_triggers_aux1" + // also affect the layout. applyLayout(ButtonLayout::loadFromSettings()); } @@ -314,6 +335,7 @@ void TouchControls::applyLayout(const ButtonLayout &layout) TouchControls::~TouchControls() { + g_settings->deregisterAllChangedCallbacks(this); releaseAll(); } diff --git a/src/gui/touchcontrols.h b/src/gui/touchcontrols.h index bcd0f3178942b..ea71e2c86d384 100644 --- a/src/gui/touchcontrols.h +++ b/src/gui/touchcontrols.h @@ -120,19 +120,31 @@ class TouchControls bool isStatusTextOverriden() { return m_overflow_open; } IGUIStaticText *getStatusText() { return m_status_text.get(); } - ButtonLayout getLayout() { return m_layout; } - void applyLayout(const ButtonLayout &layout); - private: IrrlichtDevice *m_device = nullptr; IGUIEnvironment *m_guienv = nullptr; IEventReceiver *m_receiver = nullptr; ISimpleTextureSource *m_texturesource = nullptr; + bool m_visible = true; + + // changes to these two values are handled in TouchControls::step v2u32 m_screensize; s32 m_button_size; + + // cached settings double m_touchscreen_threshold; u16 m_long_tap_delay; - bool m_visible = true; + bool m_fixed_joystick; + bool m_joystick_triggers_aux1; + + static void settingChangedCallback(const std::string &name, void *data); + void readSettings(); + + ButtonLayout m_layout; + void applyLayout(const ButtonLayout &layout); + + // not read from a setting, but set by Game via setUseCrosshair + bool m_draw_crosshair = false; std::unordered_map m_hotbar_rects; std::optional m_hotbar_selection = std::nullopt; @@ -165,9 +177,6 @@ class TouchControls float m_joystick_direction = 0.0f; // assume forward float m_joystick_speed = 0.0f; // no movement bool m_joystick_status_aux1 = false; - bool m_fixed_joystick = false; - bool m_joystick_triggers_aux1 = false; - bool m_draw_crosshair = false; std::shared_ptr m_joystick_btn_off; std::shared_ptr m_joystick_btn_bg; std::shared_ptr m_joystick_btn_center; @@ -237,8 +246,6 @@ class TouchControls bool m_place_pressed = false; u64 m_place_pressed_until = 0; - - ButtonLayout m_layout; }; extern TouchControls *g_touchcontrols; diff --git a/src/gui/touchscreeneditor.cpp b/src/gui/touchscreeneditor.cpp index b30ce5a2003c6..44c357841712d 100644 --- a/src/gui/touchscreeneditor.cpp +++ b/src/gui/touchscreeneditor.cpp @@ -23,10 +23,7 @@ GUITouchscreenLayout::GUITouchscreenLayout(gui::IGUIEnvironment* env, GUIModalMenu(env, parent, id, menumgr), m_tsrc(tsrc) { - if (g_touchcontrols) - m_layout = g_touchcontrols->getLayout(); - else - m_layout = ButtonLayout::loadFromSettings(); + m_layout = ButtonLayout::loadFromSettings(); m_gui_help_text = grab_gui_element(Environment->addStaticText( L"", core::recti(), false, false, this, -1)); @@ -378,8 +375,6 @@ bool GUITouchscreenLayout::OnEvent(const SEvent& event) } if (event.GUIEvent.Caller == m_gui_done_btn.get()) { - if (g_touchcontrols) - g_touchcontrols->applyLayout(m_layout); std::ostringstream oss; m_layout.serializeJson(oss); g_settings->set("touch_layout", oss.str()); From 434d304edda009b6be8887a74ad58675b9906546 Mon Sep 17 00:00:00 2001 From: grorp Date: Wed, 22 Jan 2025 14:43:51 -0500 Subject: [PATCH 3/3] auto name --- src/client/camera.cpp | 2 +- src/gui/touchcontrols.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/camera.cpp b/src/client/camera.cpp index b88bd19e8043e..344676f6e0069 100644 --- a/src/client/camera.cpp +++ b/src/client/camera.cpp @@ -62,7 +62,7 @@ Camera::Camera(MapDrawControl &draw_control, Client *client, RenderingEngine *re m_nametags.clear(); readSettings(); - for (const auto &name : setting_names) + for (auto name : setting_names) g_settings->registerChangedCallback(name, settingChangedCallback, this); } diff --git a/src/gui/touchcontrols.cpp b/src/gui/touchcontrols.cpp index 1ae5f78ae34a1..d18a69edc5d15 100644 --- a/src/gui/touchcontrols.cpp +++ b/src/gui/touchcontrols.cpp @@ -217,7 +217,7 @@ TouchControls::TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc) m_button_size = ButtonLayout::getButtonSize(m_screensize); readSettings(); - for (const char *name : setting_names) + for (auto name : setting_names) g_settings->registerChangedCallback(name, settingChangedCallback, this); }