Skip to content
Draft
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
22 changes: 22 additions & 0 deletions core/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#include "core/os/thread.h"
#endif

#ifdef TOOLS_ENABLED
#include "editor/editor_settings.h"
#endif

static const char *_joy_buttons[(size_t)JoyButton::SDL_MAX] = {
"a",
"b",
Expand Down Expand Up @@ -765,6 +769,24 @@ void Input::vibrate_handheld(int p_duration_ms) {
OS::get_singleton()->vibrate_handheld(p_duration_ms);
}

void Input::set_force_disable_joypad(bool p_force) {
force_disable_joypad = p_force;
}

bool Input::get_enable_joypad() const {
if (force_disable_joypad) {
return false;
}

#ifdef TOOLS_ENABLED
if (Engine::get_singleton()->is_editor_hint()) {
return EDITOR_GET("input_devices/joypad/enabled");
}
#endif

return GLOBAL_GET("input_devices/joypad/enabled");
}

void Input::set_gravity(const Vector3 &p_gravity) {
_THREAD_SAFE_METHOD_

Expand Down
5 changes: 5 additions & 0 deletions core/input/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ class Input : public Object {

CursorShape default_shape = CURSOR_ARROW;

bool force_disable_joypad = false;

enum JoyType {
TYPE_BUTTON,
TYPE_AXIS,
Expand Down Expand Up @@ -309,6 +311,9 @@ class Input : public Object {

void parse_input_event(const Ref<InputEvent> &p_event);

bool get_enable_joypad() const;
void set_force_disable_joypad(bool p_force);

void set_gravity(const Vector3 &p_gravity);
void set_accelerometer(const Vector3 &p_accel);
void set_magnetometer(const Vector3 &p_magnetometer);
Expand Down
3 changes: 3 additions & 0 deletions doc/classes/ProjectSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,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/joypad/enabled" type="bool" setter="" getter="" default="true">
If [code]true[/code], Godot will connect to joypads. If [code]false[/code], Godot will not connect to joypads.
</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
13 changes: 12 additions & 1 deletion main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ static PhysicsServer2D *physics_server_2d = nullptr;
static NavigationServer3D *navigation_server_3d = nullptr;
static NavigationServer2D *navigation_server_2d = nullptr;
static ThemeDB *theme_db = nullptr;
static bool force_disable_joypad = false;
// We error out if setup2() doesn't turn this true
static bool _start_success = false;

Expand Down Expand Up @@ -1533,7 +1534,8 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
OS::get_singleton()->print("Missing --xr-mode argument, aborting.\n");
goto error;
}

} else if (I->get() == "--disable-joypad") {
force_disable_joypad = true;
} else if (I->get() == "--benchmark") {
OS::get_singleton()->set_use_benchmark(true);
} else if (I->get() == "--benchmark-file") {
Expand Down Expand Up @@ -2173,6 +2175,9 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
// GLOBAL_DEF("xr/openxr/in_editor", false);
#endif

// Joypad settings.
GLOBAL_DEF_BASIC("input_devices/joypad/enabled", true);

Engine::get_singleton()->set_frame_delay(frame_delay);

message_queue = memnew(MessageQueue);
Expand Down Expand Up @@ -2358,6 +2363,7 @@ Error Main::setup2() {
/* Initialize Input */

input = memnew(Input);
input->set_force_disable_joypad(force_disable_joypad);

/* Initialize Display Server */

Expand Down Expand Up @@ -3500,6 +3506,11 @@ bool Main::start() {
}
}

#ifdef TOOLS_ENABLED
if (editor) {
EDITOR_DEF_RST("input_devices/joypad/enabled", true);
Copy link
Member

@AThousandShips AThousandShips Nov 24, 2023

Choose a reason for hiding this comment

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

I think this should be moved to where other editor settings are defined so that it can be documented, I suspect it not being available for documentation is because either it occurs in the wrong place, or the doctool generation doesn't set editor to true

Copy link
Contributor Author

@jsjtxietian jsjtxietian Nov 26, 2023

Choose a reason for hiding this comment

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

Yes, initially I was thinking about get this editor setting defined before the joypad is initialized, but it seems it's too early. And now we have to find a place to define it, maybe in Input?

Copy link
Member

@AThousandShips AThousandShips Nov 26, 2023

Choose a reason for hiding this comment

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

You need to remove editor to make it work, it is not assigned when running the doctool, and shouldn't matter anyway as you're already in the tool check

It also needs to be moved up before the doctool is run, so up to 2905

Copy link
Contributor Author

@jsjtxietian jsjtxietian Nov 26, 2023

Choose a reason for hiding this comment

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

Sorry I don't get it, I understand now that I need to remove the editor check, but what do you mean by "It also needs to be moved up before the doctool is run" ? Sorry I don't understand the doctool part !

Copy link
Member

Choose a reason for hiding this comment

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

The part that generates the documentation is tun on line 2905, hence why it needs to be moved up there 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah thanks, but anyway I think I need to move it to a later stage because now the EditorSettings is not instantiated yet. How stupid am I, I did not check the console error message.

Now the problem is, the joypad is inited before the EditorSettings, so editor def and get will not work, which means under the current implentation, the joypad editor setting is useless ( User can still use a command line arg but that;'s more work ).

Copy link
Member

Choose a reason for hiding this comment

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

You should probably register it in the editor settings file, but might not work in any case as you say

}
#endif
OS::get_singleton()->benchmark_end_measure("startup_begin");
OS::get_singleton()->benchmark_dump();

Expand Down
9 changes: 9 additions & 0 deletions platform/windows/joypad_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ JoypadWindows::JoypadWindows(HWND *hwnd) {
xinput_dll = nullptr;
xinput_get_state = nullptr;
xinput_set_state = nullptr;
is_enabled = Input::get_singleton()->get_enable_joypad();

load_xinput();

Expand Down Expand Up @@ -305,6 +306,10 @@ void JoypadWindows::close_joypad(int id) {
}

void JoypadWindows::probe_joypads() {
if (!is_enabled) {
return;
}

ERR_FAIL_NULL_MSG(dinput, "DirectInput not initialized. Rebooting your PC may solve this issue.");
DWORD dwResult;
for (DWORD i = 0; i < XUSER_MAX_COUNT; i++) {
Expand Down Expand Up @@ -345,6 +350,10 @@ void JoypadWindows::probe_joypads() {
}

void JoypadWindows::process_joypads() {
if (!is_enabled) {
return;
}

HRESULT hr;

for (int i = 0; i < XUSER_MAX_COUNT; i++) {
Expand Down
2 changes: 2 additions & 0 deletions platform/windows/joypad_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ class JoypadWindows {
dinput_gamepad d_joypads[JOYPADS_MAX];
xinput_gamepad x_joypads[XUSER_MAX_COUNT];

bool is_enabled = true;

static BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE *p_instance, void *p_context);
static BOOL CALLBACK objectsCallback(const DIDEVICEOBJECTINSTANCE *instance, void *context);

Expand Down