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

Fix Input::remove_joy_mapping #98792

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
30 changes: 29 additions & 1 deletion core/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1712,16 +1712,44 @@ void Input::add_joy_mapping(const String &p_mapping, bool p_update_existing) {
}

void Input::remove_joy_mapping(const String &p_guid) {
int fallback_mapping_offset = 0; // Fix the fallback, if we invalidate its index.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
int fallback_mapping_offset = 0; // Fix the fallback, if we invalidate its index.
int index_removed = 0;

I think it might be simpler and more intuitive to use a variable to record the index position of the removal.

The old index can be used for comparison later. index greater than this value needs to be index - 1; fallback_mapping needs to be reset to -1 if equal. Double loop can be avoided.


for (int i = map_db.size() - 1; i >= 0; i--) {
if (p_guid == map_db[i].uid) {
map_db.remove_at(i);

if (i == fallback_mapping) {
fallback_mapping = -1;
WARN_PRINT_ONCE(vformat("Removed fallback joypad input mapping \"%s\". This could lead to joypads not working as intended.", p_guid));
} else if (i < fallback_mapping) {
fallback_mapping_offset--;
}
}
}

if (fallback_mapping_offset < 0) {
fallback_mapping += fallback_mapping_offset;
}

for (KeyValue<int, Joypad> &E : joy_names) {
Joypad &joy = E.value;
int mapping;

if (joy.uid == p_guid) {
_set_joypad_mapping(joy, -1);
mapping = -1;
} else if (joy.mapping == (fallback_mapping - fallback_mapping_offset)) {
// Fix the mapping for the joypad that uses an outdated fallback index.
mapping = fallback_mapping;
} else {
// Re-validate the joypad's correct mapping. Fix it if necessary.
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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/input/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class Input : public Object {

HashSet<uint32_t> ignored_device_ids;

int fallback_mapping = -1;
int fallback_mapping = -1; // Index of the guid in map_db.

CursorShape default_shape = CURSOR_ARROW;

Expand Down
2 changes: 1 addition & 1 deletion doc/classes/Input.xml
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@
<return type="void" />
<param index="0" name="guid" type="String" />
<description>
Removes all mappings from the internal database that match the given GUID.
Removes all mappings from the internal database that match the given GUID. All currently connected joypads that use this GUID will become unmapped.
</description>
</method>
<method name="set_accelerometer">
Expand Down