From 86bab5b7c3265055189f28e34986430b20f42654 Mon Sep 17 00:00:00 2001 From: Megamouse Date: Sat, 20 Jan 2024 18:34:27 +0100 Subject: [PATCH] cellImeJp: fix character deletion after change to cursor --- rpcs3/Emu/Cell/Modules/cellImeJp.cpp | 58 ++++++++++++++++++---------- rpcs3/Emu/Cell/Modules/cellImeJp.h | 1 + 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/rpcs3/Emu/Cell/Modules/cellImeJp.cpp b/rpcs3/Emu/Cell/Modules/cellImeJp.cpp index 836dd081e3..c191ebb92b 100644 --- a/rpcs3/Emu/Cell/Modules/cellImeJp.cpp +++ b/rpcs3/Emu/Cell/Modules/cellImeJp.cpp @@ -92,34 +92,52 @@ bool ime_jp_manager::addString(vm::cptr str) bool ime_jp_manager::backspaceWord() { - if (!focus_begin || focus_begin > CELL_IMEJP_STRING_MAXLENGTH || focus_begin > input_string.length()) - return false; - - // Delete the character in front of the cursor - input_string.erase(focus_begin, 1); - - // Move cursors - move_focus(-1); - - if (input_string.empty()) - input_state = CELL_IMEJP_BEFORE_INPUT; - - return true; + return remove_character(false); } bool ime_jp_manager::deleteWord() { - if (focus_begin >= (CELL_IMEJP_STRING_MAXLENGTH - 1ULL) || focus_begin > (input_string.length() - 1)) + return remove_character(true); +} + +bool ime_jp_manager::remove_character(bool forward) +{ + if (!forward && !cursor) + { return false; + } - // Delete the character at the cursor - input_string.erase(focus_begin, 1); + const usz pos = forward ? cursor : (cursor - 1); - // Sanitize cursors - move_focus(0); + if (pos >= (CELL_IMEJP_STRING_MAXLENGTH - 1ULL) || pos >= input_string.length()) + { + return false; + } + + // Delete the character at the position + input_string.erase(pos, 1); + + // Move cursor and focus + const bool deleted_part_of_focus = pos > focus_begin && pos <= (focus_begin + focus_length); + + if (!forward) + { + move_cursor(-1); + } + + if (deleted_part_of_focus) + { + move_focus_end(-1, false); + } + else if (focus_begin > pos) + { + move_focus(-1); + } if (input_string.empty()) + { input_state = CELL_IMEJP_BEFORE_INPUT; + } return true; } @@ -781,7 +799,7 @@ static error_code cellImeJpExtendConvertArea(CellImeJpHandle hImeJpHandle) return CELL_IMEJP_ERROR_ERR; } - // Move end of cursor by one. Wrap around if the cursor end is already at the end of the input string. + // Move end of the focus by one. Wrap around if the focus end is already at the end of the input string. manager.move_focus_end(1, true); return CELL_OK; @@ -804,7 +822,7 @@ static error_code cellImeJpShortenConvertArea(CellImeJpHandle hImeJpHandle) return CELL_IMEJP_ERROR_ERR; } - // Move end of cursor by one. Wrap around if the cursor end is already at the beginning of the input string. + // Move end of focus by one. Wrap around if the focus end is already at the beginning of the input string. manager.move_focus_end(-1, true); return CELL_OK; diff --git a/rpcs3/Emu/Cell/Modules/cellImeJp.h b/rpcs3/Emu/Cell/Modules/cellImeJp.h index 9d332e89f3..22e92adf29 100644 --- a/rpcs3/Emu/Cell/Modules/cellImeJp.h +++ b/rpcs3/Emu/Cell/Modules/cellImeJp.h @@ -132,6 +132,7 @@ struct ime_jp_manager bool addString(vm::cptr str); bool backspaceWord(); bool deleteWord(); + bool remove_character(bool forward); void clear_input(); void move_cursor(s8 amount); // s8 because CELL_IMEJP_STRING_MAXLENGTH is 128 void move_focus(s8 amount); // s8 because CELL_IMEJP_STRING_MAXLENGTH is 128