2
0
Эх сурвалжийг харах

Updating the index after the first mapping record of p_guid requires a second loop

MJacred 9 сар өмнө
parent
commit
f355382ef5

+ 22 - 9
core/input/input.cpp

@@ -1709,16 +1709,18 @@ void Input::add_joy_mapping(const String &p_mapping, bool p_update_existing) {
 }
 }
 
 
 void Input::remove_joy_mapping(const String &p_guid) {
 void Input::remove_joy_mapping(const String &p_guid) {
-	int index_removed = -1;
-	int count = 0;
+	int count = 0; // The amount of removals performed.
+	int index_removed = -1; // The smallest index where an entry was removed.
+	int fallback_mapping_offset = 0;
 
 
 	for (int i = map_db.size() - 1; i >= 0; i--) {
 	for (int i = map_db.size() - 1; i >= 0; i--) {
 		if (p_guid == map_db[i].uid) {
 		if (p_guid == map_db[i].uid) {
 			map_db.remove_at(i);
 			map_db.remove_at(i);
 			index_removed = i;
 			index_removed = i;
+			count++;
 
 
-			if (i < fallback_mapping){
-				count++;
+			if (i < fallback_mapping) {
+				fallback_mapping_offset++;
 			} else if (i == fallback_mapping) {
 			} else if (i == fallback_mapping) {
 				fallback_mapping = -1;
 				fallback_mapping = -1;
 				WARN_PRINT_ONCE(vformat("Removed fallback joypad input mapping \"%s\". This could lead to joypads not working as intended.", p_guid));
 				WARN_PRINT_ONCE(vformat("Removed fallback joypad input mapping \"%s\". This could lead to joypads not working as intended.", p_guid));
@@ -1730,19 +1732,30 @@ void Input::remove_joy_mapping(const String &p_guid) {
 		return; // Not found.
 		return; // Not found.
 	}
 	}
 
 
-	if (fallback_mapping > 0){
+	if (fallback_mapping > 0) {
 		// Fixing the shifted index.
 		// Fixing the shifted index.
-		fallback_mapping -= count;
+		fallback_mapping -= fallback_mapping_offset;
 	}
 	}
 
 
 	for (KeyValue<int, Joypad> &E : joy_names) {
 	for (KeyValue<int, Joypad> &E : joy_names) {
 		Joypad &joy = E.value;
 		Joypad &joy = E.value;
 
 
-		if (joy.puid == _guid) {
+		if (joy.uid == p_guid) {
 			_set_joypad_mapping(joy, fallback_mapping);
 			_set_joypad_mapping(joy, fallback_mapping);
 		} else if (joy.mapping > index_removed) {
 		} else if (joy.mapping > index_removed) {
-			// The map_db update offset this joypad's mapping reference, update it:
-			_set_joypad_mapping(joy, joy.mapping - 1);
+			if (count == 1) {
+				// The map_db update offset this joypad's mapping reference, update it:
+				_set_joypad_mapping(joy, joy.mapping - 1);
+			} else {
+				// Re-validate the joypad's correct mapping. Fix it if necessary.
+				int mapping = fallback_mapping;
+				for (int i = 0; i < map_db.size(); i++) {
+					if (joy.uid == map_db[i].uid) {
+						mapping = i;
+					}
+				}
+				_set_joypad_mapping(joy, mapping);
+			}
 		}
 		}
 	}
 	}
 }
 }