diff --git a/Source/UI/Edit/UIEditBoxEx.cpp b/Source/UI/Edit/UIEditBoxEx.cpp index c3f28df5..65d64add 100644 --- a/Source/UI/Edit/UIEditBoxEx.cpp +++ b/Source/UI/Edit/UIEditBoxEx.cpp @@ -205,8 +205,8 @@ void CUIEditCtrlEx::Synchronize() { const std::string prefix = g_serverLanguage->Prefix(false); const std::string fullText = prefix + this->text; - std::vector lines; std::vector breaks; + std::vector lines; if (reinterpret_cast(&this->unknown_0184)[1] == 0) { lines.push_back(fullText); breaks.push_back(false); @@ -217,110 +217,89 @@ void CUIEditCtrlEx::Synchronize() { } this->lines.clear(); - if (lines.empty()) { - this->UpdateScrollBar(); - this->Invalidate(); - return; - } - - int anchor = 0; - int cursor = 0; - g_serverLanguage->Selection(anchor, cursor); - int selStart = anchor; - int selEnd = cursor; - if (selStart > selEnd) { - std::swap(selStart, selEnd); - } - - int currentOffset = 0; - for (const std::string& rawLine : lines) { - EditLine line; - auto crPos = rawLine.find("\r\n"); - bool hasCR = crPos != std::string::npos; - if (hasCR) { - line.text = rawLine.substr(0, crPos); - } else { - line.text = rawLine; + if (!lines.empty()) { + int anchor = 0; + int cursor = 0; + g_serverLanguage->Selection(anchor, cursor); + if (anchor > cursor) { + std::swap(anchor, cursor); } - line.unknown_0018 = hasCR ? 1 : 0; - int lineLen = static_cast(line.text.size()); - int rawLineEnd = currentOffset + static_cast(rawLine.size()); + int currentOffset = 0; + for (const std::string& rawLine : lines) { + std::size_t crPos = rawLine.find("\r\n"); + EditLine line; + line.unknown_0018 = crPos != std::string::npos; + line.text = crPos != std::string::npos ? rawLine.substr(0, crPos) : rawLine; - int begin = 0; - int end = 0; - if (static_cast(selStart) <= static_cast(rawLineEnd)) { - int val = selStart - currentOffset; - if (lineLen < val) { - begin = lineLen; - } else { - begin = std::max(0, val); + int lineLen = static_cast(line.text.size()); + std::size_t lineEnd = currentOffset + rawLine.size(); + if (static_cast(anchor) <= lineEnd) { + int offset = anchor - currentOffset; + if (lineLen < offset) { + line.selectionBegin = lineLen; + } else { + line.selectionBegin = std::max(offset, 0); + } + line.selectionEnd = lineLen; } - end = lineLen; + if (static_cast(cursor) <= lineEnd) { + int offset = cursor - currentOffset; + if (lineLen < offset) { + line.selectionEnd = lineLen; + } else { + line.selectionEnd = std::max(offset, 0); + } + } + currentOffset = static_cast(lineEnd); + + this->lines.push_back(line); } - if (static_cast(selEnd) <= static_cast(rawLineEnd)) { - int val = selEnd - currentOffset; - if (lineLen >= val) { - end = std::max(0, val); + + if (this->lines.back().unknown_0018 != 0) { + this->lines.emplace_back(); + } + + int remaining = cursor + static_cast(prefix.size()); + this->line = 0; + this->column = 0; + for (std::size_t i = 0; i < this->lines.size(); ++i) { + int fullLen = static_cast(this->lines[i].text.size()) + (this->lines[i].unknown_0018 ? 2 : 0); + if (remaining - fullLen <= 0) { + this->column = remaining; + this->line = static_cast(i); + break; + } + remaining -= fullLen; + } + + const EditLine& current = this->lines[this->line]; + if (current.unknown_0018 != 0 || this->unknown_01DC == 0) { + int fullLen = static_cast(current.text.size()) + (current.unknown_0018 ? 2 : 0); + if (remaining == fullLen && static_cast(this->line) < this->lines.size() - 1) { + this->column = 0; + this->line++; } } - line.selectionBegin = begin; - line.selectionEnd = end; - currentOffset = rawLineEnd; - this->lines.push_back(line); - } + const int candidateCursor = g_serverLanguage->compositionCursor; + if (candidateCursor != 0) { + this->column += candidateCursor; + int cursorX = 0; + if (this->column > 0) { + cursorX = this->TextWidth(this->lines[this->line].text.c_str(), this->column, 0, 12, 0, 0); + } - if (this->lines.back().unknown_0018 != 0) { - EditLine emptyLine{}; - this->lines.push_back(emptyLine); - } - - this->line = 0; - this->column = 0; - int remaining = std::max(anchor, cursor) + static_cast(prefix.size()); - int lineOffset = remaining; - for (std::size_t i = 0; i < this->lines.size(); ++i) { - int cr = this->lines[i].unknown_0018 ? 2 : 0; - int lineLen = static_cast(this->lines[i].text.size()); - int fullLen = lineLen + cr; - if (remaining - fullLen <= 0) { - this->column = remaining; - this->line = static_cast(i); - lineOffset = remaining; - break; + int end = std::max(anchor, cursor) + candidateCursor + static_cast(prefix.size()); + int readingOffset = end - currentOffset; + int readingX = 0; + if (readingOffset > 0) { + readingX = this->TextWidth(this->lines[this->line].text.c_str(), readingOffset, 0, 12, 0, 0); + } + int delta = std::max(cursorX - readingX, 2); + this->MoveCandidateWindow(cursorX + 2 + delta); + this->MoveReadingWindow(readingX); } - remaining -= fullLen; - lineOffset = remaining; - } - - const EditLine& current = this->lines[this->line]; - if (current.unknown_0018 != 0 || this->unknown_01DC == 0) { - int cr = current.unknown_0018 ? 2 : 0; - int fullLen = static_cast(current.text.size()) + cr; - if (lineOffset == fullLen && this->line < static_cast(this->lines.size()) - 1) { - this->column = 0; - this->line++; - } - } - - const int candidateCursor = g_serverLanguage->compositionCursor; - if (candidateCursor != 0) { - this->column += candidateCursor; - int cursorX = 0; - if (this->column > 0) { - cursorX = this->TextWidth(this->lines[this->line].text.c_str(), this->column, 0, 12, 0, 0); - } - - int end = std::max(anchor, cursor) + candidateCursor + static_cast(prefix.size()); - int readingOffset = end - currentOffset; - int readingX = 0; - if (readingOffset > 0) { - readingX = this->TextWidth(this->lines[this->line].text.c_str(), readingOffset, 0, 12, 0, 0); - } - int delta = std::max(cursorX - readingX, 2); - this->MoveCandidateWindow(cursorX + 2 + delta); - this->MoveReadingWindow(readingX); } this->UpdateScrollBar(); diff --git a/Source/UI/Edit/UIEditBoxEx.h b/Source/UI/Edit/UIEditBoxEx.h index 29a14500..dffe46d1 100644 --- a/Source/UI/Edit/UIEditBoxEx.h +++ b/Source/UI/Edit/UIEditBoxEx.h @@ -13,9 +13,9 @@ struct EditLine { /* 0000 */ std::string text; - /* 0018 */ unsigned char unknown_0018; - /* 001C */ int selectionBegin; - /* 0020 */ int selectionEnd; + /* 0018 */ unsigned char unknown_0018 = 0; + /* 001C */ int selectionBegin = 0; + /* 0020 */ int selectionEnd = 0; }; class CUIEditCtrlEx : public UIEditCtrl3 { @@ -31,7 +31,7 @@ public: void sub_860C60(std::vector* lines); - void RefreshPlaceholder(); + __declspec(noinline) void RefreshPlaceholder(); void HandleKeyDown(int key, std::intptr_t lparam); void ResetDragState(); void DrawCaret(int x, int y);