Add setting callbacks for Camera and TouchControls (#15700)

This commit is contained in:
grorp 2025-01-24 10:50:51 -05:00 committed by GitHub
parent b5e084c9a5
commit 41dfac96c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 71 additions and 25 deletions

View file

@ -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 (auto name : setting_names)
g_settings->registerChangedCallback(name, settingChangedCallback, this);
}
void Camera::settingChangedCallback(const std::string &name, void *data)
{
static_cast<Camera *>(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();
}

View file

@ -70,6 +70,9 @@ public:
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

View file

@ -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());
}
/****************************************************************************/

View file

@ -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 (auto name : setting_names)
g_settings->registerChangedCallback(name, settingChangedCallback, this);
}
void TouchControls::settingChangedCallback(const std::string &name, void *data)
{
static_cast<TouchControls *>(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();
}

View file

@ -120,19 +120,31 @@ public:
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<u16, recti> m_hotbar_rects;
std::optional<u16> m_hotbar_selection = std::nullopt;
@ -165,9 +177,6 @@ private:
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<IGUIImage> m_joystick_btn_off;
std::shared_ptr<IGUIImage> m_joystick_btn_bg;
std::shared_ptr<IGUIImage> m_joystick_btn_center;
@ -237,8 +246,6 @@ private:
bool m_place_pressed = false;
u64 m_place_pressed_until = 0;
ButtonLayout m_layout;
};
extern TouchControls *g_touchcontrols;

View file

@ -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<IGUIStaticText>(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());