Skip to content
Open
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
1 change: 1 addition & 0 deletions core/config/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,7 @@ ProjectSettings::ProjectSettings() {
GLOBAL_DEF_INTERNAL("internationalization/locale/translations", PackedStringArray());
GLOBAL_DEF_INTERNAL("internationalization/locale/translations_pot_files", PackedStringArray());

GLOBAL_DEF("input_devices/config/ignore_joypad_on_unfocused_window", false);
ProjectSettings::get_singleton()->add_hidden_prefix("input/");
}

Expand Down
27 changes: 26 additions & 1 deletion core/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "core/input/default_controller_mappings.h"
#include "core/input/input_map.h"
#include "core/os/os.h"
#include "servers/display_server.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was discussed in a PR-review meeting and Input should not depend on display_server.
An alternative approach would be to notify Input from the main loop about application focus changes.


#ifdef DEV_ENABLED
#include "core/os/thread.h"
Expand Down Expand Up @@ -270,6 +271,19 @@ bool Input::is_mouse_button_pressed(MouseButton p_button) const {
return mouse_button_mask.has_flag(mouse_button_to_mask(p_button));
}

void Input::_project_settings_changed() {
ignore_joypad_on_unfocused = bool(GLOBAL_GET("input_devices/config/ignore_joypad_on_unfocused_window"));
}

bool Input::_config_ignore_joypad_on_unfocused_window() {
if (ignore_joypad_on_unfocused) {
if (!DisplayServer::get_singleton()->window_is_focused(DisplayServer::MAIN_WINDOW_ID)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach doesn't cover the case, that an application can have multiple OS-Windows. Perhaps a better approach would be to take application focus instead of window focus into consideration.

return true;
}
}
return false;
}

static JoyAxis _combine_device(JoyAxis p_value, int p_device) {
return JoyAxis((int)p_value | (p_device << 20));
}
Expand Down Expand Up @@ -1053,6 +1067,9 @@ void Input::set_event_dispatch_function(EventDispatchFunc p_function) {

void Input::joy_button(int p_device, JoyButton p_button, bool p_pressed) {
_THREAD_SAFE_METHOD_;
if (_config_ignore_joypad_on_unfocused_window()) {
return;
}
Joypad &joy = joy_names[p_device];
ERR_FAIL_INDEX((int)p_button, (int)JoyButton::MAX);

Expand Down Expand Up @@ -1080,7 +1097,9 @@ void Input::joy_button(int p_device, JoyButton p_button, bool p_pressed) {

void Input::joy_axis(int p_device, JoyAxis p_axis, float p_value) {
_THREAD_SAFE_METHOD_;

if (_config_ignore_joypad_on_unfocused_window()) {
return;
}
ERR_FAIL_INDEX((int)p_axis, (int)JoyAxis::MAX);

Joypad &joy = joy_names[p_device];
Expand Down Expand Up @@ -1147,6 +1166,10 @@ void Input::joy_axis(int p_device, JoyAxis p_axis, float p_value) {

void Input::joy_hat(int p_device, BitField<HatMask> p_val) {
_THREAD_SAFE_METHOD_;
if (_config_ignore_joypad_on_unfocused_window()) {
return;
}

const Joypad &joy = joy_names[p_device];

JoyEvent map[(size_t)HatDir::MAX];
Expand Down Expand Up @@ -1616,6 +1639,8 @@ Input::Input() {
// Always use standard behavior in the editor.
legacy_just_pressed_behavior = false;
}

ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &Input::_project_settings_changed));
}

Input::~Input() {
Expand Down
4 changes: 4 additions & 0 deletions core/input/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class Input : public Object {
Vector2 mouse_pos;
int64_t mouse_window = 0;
bool legacy_just_pressed_behavior = false;
bool ignore_joypad_on_unfocused = false;

struct Action {
uint64_t pressed_physics_frame = UINT64_MAX;
Expand Down Expand Up @@ -247,6 +248,9 @@ class Input : public Object {

EventDispatchFunc event_dispatch_function = nullptr;

bool _config_ignore_joypad_on_unfocused_window();
void _project_settings_changed();

protected:
static void _bind_methods();

Expand Down
3 changes: 3 additions & 0 deletions doc/classes/ProjectSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,9 @@
If [code]false[/code], no input will be lost.
[b]Note:[/b] You should in nearly all cases prefer the [code]false[/code] setting. The legacy behavior is to enable supporting old projects that rely on the old logic, without changes to script.
</member>
<member name="input_devices/config/ignore_joypad_on_unfocused_window" type="bool" setter="" getter="" default="false">
If [code]true[/code], joypad input will be ignored when the window is not focused.
</member>
<member name="input_devices/pen_tablet/driver" type="String" setter="" getter="">
Specifies the tablet driver to use. If left empty, the default driver will be used.
[b]Note:[/b] The driver in use can be overridden at runtime via the [code]--tablet-driver[/code] [url=$DOCS_URL/tutorials/editor/command_line_tutorial.html]command line argument[/url].
Expand Down