Ver código fonte

Merge pull request #108007 from adamscott/the-spirit-of-105601

[Web] Poll controllers only if at least one is detected
Thaddeus Crews 2 meses atrás
pai
commit
7ed506f342

+ 11 - 8
platform/web/display_server_web.cpp

@@ -829,10 +829,13 @@ void DisplayServerWeb::gamepad_callback(int p_index, int p_connected, const char
 }
 
 void DisplayServerWeb::_gamepad_callback(int p_index, int p_connected, const String &p_id, const String &p_guid) {
-	Input *input = Input::get_singleton();
-	DisplayServerWeb *ds = get_singleton();
-	ds->active_gamepad_sample_count = -1; // Invalidate cache
+	if (p_connected) {
+		DisplayServerWeb::get_singleton()->gamepad_count += 1;
+	} else {
+		DisplayServerWeb::get_singleton()->gamepad_count -= 1;
+	}
 
+	Input *input = Input::get_singleton();
 	if (p_connected) {
 		input->joy_connection_changed(p_index, true, p_id, p_guid);
 	} else {
@@ -1435,11 +1438,11 @@ DisplayServer::VSyncMode DisplayServerWeb::window_get_vsync_mode(WindowID p_vsyn
 void DisplayServerWeb::process_events() {
 	process_keys();
 	Input::get_singleton()->flush_buffered_events();
-	if (active_gamepad_sample_count == -1) {
-		active_gamepad_sample_count = godot_js_input_gamepad_sample();
-	}
-	if (active_gamepad_sample_count > 0) {
-		process_joypads();
+
+	if (gamepad_count > 0) {
+		if (godot_js_input_gamepad_sample() == OK) {
+			process_joypads();
+		}
 	}
 }
 

+ 1 - 1
platform/web/display_server_web.h

@@ -104,7 +104,7 @@ private:
 	bool swap_cancel_ok = false;
 	NativeMenu *native_menu = nullptr;
 
-	int active_gamepad_sample_count = -1;
+	int gamepad_count = 0;
 
 	MouseMode mouse_mode_base = MOUSE_MODE_VISIBLE;
 	MouseMode mouse_mode_override = MOUSE_MODE_VISIBLE;

+ 2 - 4
platform/web/js/libs/library_godot_input.js

@@ -205,7 +205,6 @@ const GodotInputGamepads = {
 		sample: function () {
 			const pads = GodotInputGamepads.get_pads();
 			const samples = [];
-			let active = 0;
 			for (let i = 0; i < pads.length; i++) {
 				const pad = pads[i];
 				if (!pad) {
@@ -225,10 +224,8 @@ const GodotInputGamepads = {
 					s.axes.push(pad.axes[a]);
 				}
 				samples.push(s);
-				active++;
 			}
 			GodotInputGamepads.samples = samples;
-			return active;
 		},
 
 		init: function (onchange) {
@@ -662,7 +659,8 @@ const GodotInput = {
 	godot_js_input_gamepad_sample__proxy: 'sync',
 	godot_js_input_gamepad_sample__sig: 'i',
 	godot_js_input_gamepad_sample: function () {
-		return GodotInputGamepads.sample();
+		GodotInputGamepads.sample();
+		return 0;
 	},
 
 	godot_js_input_gamepad_sample_get__proxy: 'sync',