Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setting callbacks for Camera and TouchControls #15700

Merged
merged 3 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/client/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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.
Expand All @@ -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();
}

Expand Down
3 changes: 3 additions & 0 deletions src/client/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}

/****************************************************************************/
Expand Down
26 changes: 24 additions & 2 deletions src/gui/touchcontrols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -314,6 +335,7 @@ void TouchControls::applyLayout(const ButtonLayout &layout)

TouchControls::~TouchControls()
{
g_settings->deregisterAllChangedCallbacks(this);
releaseAll();
}

Expand Down
25 changes: 16 additions & 9 deletions src/gui/touchcontrols.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16, recti> m_hotbar_rects;
std::optional<u16> m_hotbar_selection = std::nullopt;
Expand Down Expand Up @@ -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<IGUIImage> m_joystick_btn_off;
std::shared_ptr<IGUIImage> m_joystick_btn_bg;
std::shared_ptr<IGUIImage> m_joystick_btn_center;
Expand Down Expand Up @@ -237,8 +246,6 @@ class TouchControls

bool m_place_pressed = false;
u64 m_place_pressed_until = 0;

ButtonLayout m_layout;
};

extern TouchControls *g_touchcontrols;
7 changes: 1 addition & 6 deletions src/gui/touchscreeneditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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());
Expand Down
Loading