decomp(inputform): calcCursorPos and create structure (93.05 -> 99.88, 96.40 -> 98.16) #1317

Merged
coah80 merged 1 commits from agent/w0929/g-keyboard-land into main 2026-10-09 08:49:48 +00:00
3 changed files with 108 additions and 39 deletions

View File

@@ -30,6 +30,7 @@ namespace textinput {
class Base;
class EventHandler;
class LayoutByNW4R;
struct InputFormAnimationFile;
#endif
class EditBuffer {
#ifdef TIMANAGER_IMPLEMENTATION
@@ -356,6 +357,7 @@ namespace textinput {
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
friend class EventHandler;
inline const nw4r::lyt::Pane* getLanguageTextPane() const;
inline void addButtonAnimation(MEMAllocator*, nw4rmanager::AnmPane*, const InputFormAnimationFile* const&, const char*);
#endif
public:
#if defined(MYTIMANAGER_IMPLEMENTATION) || defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)

View File

@@ -1108,7 +1108,36 @@ LayoutByNW4R::LayoutByNW4R(Manager* manager, nw4r::lyt::MultiArcResourceAccessor
mpLanguageData(&csLanguageDependencyDataUEJ__Q29textinput9inputform), mpFontName(fontName),
mbUpVisible(false), mbDownVisible(false), mbRepeat(false), mpInputEventHandler(NULL) {}
static inline const char* selectInputTextName(const nw4rmanager::Layout& layout, const void* const& languageData) {
if (layout.getPane(static_cast<const LanguagePaneData*>(languageData)->textBox)) {
return static_cast<const LanguagePaneData*>(languageData)->textBox;
}
return "T_2l_TextBox";
}
inline void LayoutByNW4R::addButtonAnimation(MEMAllocator* allocator, nw4rmanager::AnmPane* animationPane, const InputFormAnimationFile* const& file, const char* bindingName) {
void* resource = mpMultiArcResourceAccessor->GetResource(0, file->fileName, NULL);
AnimTransformPane* transform = static_cast<AnimTransformPane*>(getLayout()->CreateAnimTransform(resource, mpMultiArcResourceAccessor));
if (!bindingName) animationPane->addAnimation(allocator, file->id, transform, false, true);
else animationPane->forceAddAnimation(allocator, file->id, transform, bindingName, false, true);
}
void LayoutByNW4R::create(MEMAllocator* allocator, EditBuffer* editBuffer) {
Info_* listEnd;
void* memory;
nw4r::lyt::Pane* pane;
const char* bindingName;
u16 next;
Info_* rows;
u16 previous;
void* handlerMemory;
u32 count;
u16 animationIndex;
Info_* selected;
nw4rmanager::AnmPane* animationPane;
nw4r::lyt::TextBox* textBox;
u16 selectedIndex;
u32 buttonIndex;
mpAllocator = allocator;
textdrawer::Base::create(allocator);
mpString = static_cast<tistring::Decolated*>(editBuffer->mpString);
@@ -1117,18 +1146,18 @@ void LayoutByNW4R::create(MEMAllocator* allocator, EditBuffer* editBuffer) {
mriManager.mpAllocator = allocator;
mriManager.mpInfo = static_cast<Info_*>(MEMAllocFromAllocator(allocator, (mriManager.mMaxLength + 2) * sizeof(Info_)));
mriManager.init();
Info_* rows = mriManager.mpInfo;
Info_* selected = &rows[rows[mriManager.mMaxLength].Next];
u16 previous = selected->Back;
u16 next = selected->Next;
u16 selectedIndex = rows[next].Back;
rows = mriManager.mpInfo;
selected = &rows[rows[mriManager.mMaxLength].Next];
previous = selected->Back;
next = selected->Next;
selectedIndex = rows[next].Back;
rows[next].Back = previous;
rows[previous].Next = next;
selected->Back = selectedIndex;
selected->Next = selectedIndex;
Info_* listEnd = &mriManager.mpInfo[mriManager.mpInfo[static_cast<u16>(mriManager.mMaxLength + 1)].Back];
rows = mriManager.mpInfo;
if (selected->Back == selected->Next) {
listEnd = &mriManager.mpInfo[mriManager.mpInfo[static_cast<u16>(mriManager.mMaxLength + 1)].Back];
if (selected->Next == selected->Back) {
next = listEnd->Next;
selected->Back = rows[next].Back;
selected->Next = next;
@@ -1138,7 +1167,7 @@ void LayoutByNW4R::create(MEMAllocator* allocator, EditBuffer* editBuffer) {
selected->StrCount = 0;
selected->DispRowCount = 1;
mpCursorLine = selected;
void* handlerMemory = MEMAllocFromAllocator(allocator, sizeof(EventHandler));
handlerMemory = MEMAllocFromAllocator(allocator, sizeof(EventHandler));
mpInputEventHandler = new (handlerMemory) EventHandler(this);
nw4rmanager::Layout::createWithEventHandler(allocator, mpInputEventHandler);
if (!mpFontName) {
@@ -1150,7 +1179,7 @@ void LayoutByNW4R::create(MEMAllocator* allocator, EditBuffer* editBuffer) {
mpMultiArcResourceAccessor->GetFont(mpFontName)->SetAlternateChar(0xe06b);
setFont(*mpMultiArcResourceAccessor->GetFont(mpFontName));
}
nw4r::lyt::TextBox* textBox = static_cast<nw4r::lyt::TextBox*>(mpLayout->GetRootPane()->FindPaneByName(static_cast<const char*>(mpLayoutData), true));
textBox = static_cast<nw4r::lyt::TextBox*>(mpLayout->GetRootPane()->FindPaneByName(static_cast<const char*>(mpLayoutData), true));
textBox->SetString(L"", 0);
mfCharacterSpacing = textBox->GetCharSpace();
mfLineSpacing = textBox->GetLineSpace();
@@ -1162,29 +1191,24 @@ void LayoutByNW4R::create(MEMAllocator* allocator, EditBuffer* editBuffer) {
setVisible("N_2line", true);
mpPaneManager->setAllComponentTriggerTarget(false);
mpPaneManager->setAllBoundingBoxComponentTriggerTarget(true);
nw4r::lyt::Pane* pane = mpLayout->GetRootPane()->FindPaneByName(static_cast<const LanguagePaneData*>(mpLanguageData)->textBox, true);
if (!pane) pane = mpLayout->GetRootPane()->FindPaneByName(getLanguageTextPane() ? static_cast<const LanguagePaneData*>(mpLanguageData)->textBox : "T_2l_TextBox", true);
pane = mpLayout->GetRootPane()->FindPaneByName(static_cast<const LanguagePaneData*>(mpLanguageData)->textBox, true);
if (!pane) pane = mpLayout->GetRootPane()->FindPaneByName(selectInputTextName(*this, mpLanguageData), true);
mpPaneManager->getPaneComponentByPane(pane)->setTriggerTarget(true);
for (u32 buttonIndex = 0; buttonIndex < 2; ++buttonIndex) {
for (buttonIndex = 0; buttonIndex < 2; ++buttonIndex) {
const ButtonAnimations& button = csButtonAnimations[static_cast<u16>(buttonIndex)];
nw4rmanager::AnmPane* animationPane = NULL;
animationPane = NULL;
switch (button.type) {
case KT_NormalButton: {
void* memory = MEMAllocFromAllocator(allocator, sizeof(NormalButtonAnmPane));
memory = MEMAllocFromAllocator(allocator, sizeof(NormalButtonAnmPane));
animationPane = new (memory) NormalButtonAnmPane(getPane(button.paneName), NULL);
break;
}
}
nw4r::ut::List_Append(&mAnmPanes, animationPane);
u32 count;
const char* bindingName = button.bindingName;
bindingName = button.bindingName;
count = button.count;
for (u16 animationIndex = 0; animationIndex < count; ++animationIndex) {
const InputFormAnimationFile* const& file = button.files[animationIndex];
void* resource = mpMultiArcResourceAccessor->GetResource(0, file->fileName, NULL);
AnimTransformPane* transform = static_cast<AnimTransformPane*>(getLayout()->CreateAnimTransform(resource, mpMultiArcResourceAccessor));
if (!bindingName) animationPane->addAnimation(allocator, file->id, transform, false, true);
else animationPane->forceAddAnimation(allocator, file->id, transform, bindingName, false, true);
for (animationIndex = 0; animationIndex < count; ++animationIndex) {
addButtonAnimation(allocator, animationPane, button.files[animationIndex], bindingName);
}
}
init();
@@ -1247,12 +1271,6 @@ void LayoutByNW4R::calc() {
}
}
}
static inline const char* selectInputTextName(const nw4rmanager::Layout& layout, const void* const& languageData) {
if (layout.getPane(static_cast<const LanguagePaneData*>(languageData)->textBox)) {
return static_cast<const LanguagePaneData*>(languageData)->textBox;
}
return "T_2l_TextBox";
}
void LayoutByNW4R::setLanguage(Language language) {
Base::setLanguage(language);
@@ -3113,6 +3131,16 @@ bool Base::onCursor(CursorPos* cursor) {
return positioned;
}
static inline f32 scaledExtent(f32 scale, f32 start, f32 end) {
f32 extent = end - start;
return scale * extent;
}
static inline f32 glyphBottom(f32 origin, const Base::DrawInfo& glyph, const nw4r::math::VEC2& scale) {
f32 extent = glyph.rect.GetHeight();
return origin + scale.y * extent;
}
u32 Base::calcCursorPos(f32 x, f32 y) {
f32 cursorX;
f32 lineTop;
@@ -3135,8 +3163,8 @@ u32 Base::calcCursorPos(f32 x, f32 y) {
info.rect.bottom = 0.0f;
info.character = *string;
calcRect(info);
lineBottom = lineTop + scale.y * (info.rect.bottom - info.rect.top);
f32 right = cursorX + scale.x * info.rect.GetWidth();
lineBottom = glyphBottom(lineTop, info, scale);
f32 right = cursorX + scaledExtent(scale.x, info.rect.left, info.rect.right);
if (doWordWrap(mpString->getWCString(), pos, cursorX)) {
if (localY >= lineTop && localY <= lineBottom) {
if (pos != 0) return pos - 1;
@@ -3144,35 +3172,35 @@ u32 Base::calcCursorPos(f32 x, f32 y) {
}
lineTop += scale.y * getLineHeight();
cursorX = getScale().x;
lineBottom = lineTop + scale.y * (info.rect.bottom - info.rect.top);
right = cursorX + scale.x * info.rect.GetWidth();
lineBottom = glyphBottom(lineTop, info, scale);
right = cursorX + scaledExtent(scale.x, info.rect.left, info.rect.right);
}
if (right >= scale.x * mRect.GetWidth()) {
if (right >= scaledExtent(scale.x, mRect.left, mRect.right)) {
if (localY >= lineTop && localY <= lineBottom) return pos;
lineTop += scale.y * getLineHeight();
cursorX = getScale().x;
lineBottom = lineTop + scale.y * (info.rect.bottom - info.rect.top);
right = cursorX + scale.x * info.rect.GetWidth();
lineBottom = glyphBottom(lineTop, info, scale);
right = cursorX + scaledExtent(scale.x, info.rect.left, info.rect.right);
}
if (x <= info.rect.left && localY >= lineTop && localY <= lineBottom) return pos;
if (*string == L'\n') {
if (localY >= lineTop && localY <= lineBottom) return pos;
lineTop += scale.y * getLineHeight();
cursorX = getScale().x;
lineBottom = lineTop + scale.y * (info.rect.bottom - info.rect.top);
lineBottom = glyphBottom(lineTop, info, scale);
if (!string[1] && localY >= lineTop && localY <= lineBottom) return pos + 1;
} else {
if (x >= cursorX && x < right && localY >= lineTop && localY < lineBottom) {
if (cursorX + 0.5f * (right - cursorX) > x) return pos;
if (cursorX + (right - cursorX) / 2.0f > x) return pos;
return pos + 1;
}
cursorX += scale.x * info.rect.GetWidth();
cursorX += scaledExtent(scale.x, info.rect.left, info.rect.right);
}
++string;
++pos;
if (!*string && localY >= lineTop && localY <= lineBottom) return pos;
}
if (lineTop + (lineBottom - lineTop) * 0.5f <= localY) {
if (lineTop + (lineBottom - lineTop) / 2.0f <= localY) {
return Base::calcCursorPos(x, 1.0f + lineTop + mfScrollY);
}
return Base::calcCursorPos(x, 1.0f);

View File

@@ -0,0 +1,39 @@
# g-keyboard attempts
Worktree data-d4; branch agent/w1009/g-keyboard; base 54ca052b.
Read opus-common.md in full. No pushes, PRs, rebases, or cross-worktree edits.
Initial full 43U build passed; DOL SHA1 26116613f624061ba99c8d1a299aaa6efa85670d.
Baseline odiff: calcCursorPos 43/326, create 153/356, inputChar 73/136.
Two InputForm saved diffs are byte-identical and omit addButtonAnimation declaration.
Unrelated preexisting untracked files preserved.
o2-keyboard.wip.diff: original build fails for missing member declaration. Repaired declaration: pool identical, calcCursorPos 39/326, create 128/356, equal sizes. opus-inputform.wip.diff SHA identical; same source/build/measurement.
opus-tistring.wip.diff: compiled, pool identical, 70/136 differing, size 0x210 vs 0x220. Rejected: append tests an always-zero buffer slot and alters generic newline behavior. Restored baseline for honest trials.
## Compiled trials
Single/pair pragma sweep on saved InputForm + original tiString: no exact hits or gains.
32 direct rectangle-reference/copy/size-vector forms: best 39, copies worse.
28 consistent geometry helper forms: best 50.
208 helper-scope pragmas/newline reset variants for inputChar: best 60; none exact.
90 chain/temporary/ref-return/clear variants: best 59, none exact.
GC versions 3.0a3 through 3.0a5.2: tiString unchanged; older InputForm versions regress 5 functions. GC 2.6/2.7 reject ipa.
Create: existing selectInputTextName reuse gives 120 differences; complete scoped declarations 135-138; constrained regsim reaches only 8/15 requested assignments.
Read-only prior scratch has a fully consistent calcCursorPos geometry-helper form, reproduced at 8/326. All surviving differences swap the zero constant f29/f30 with scale.x. Prior 20-difference inconsistent-helper forms excluded.
Initializer/declaration trials: best stays 8. Malformed generated named-zero-top trials used a self initializer; excluded from all results and never retained.
Further calc trials: 36 helper order/body forms, 48 scalar-reference forms, 150 scoped single/pair pragma combinations, 14 corrected constant/declaration variants, 30 local-math bodies, 18 glyphBottom signature/definition-order forms. Best remains 8; no exact.
Further create trials: 24 free animation-helper forms best 120; 16 whole animation-loop helpers best 106 but runtime count reload changes the target instruction shape. 16 row-helper forms best 117.
Fresh allocator trace saved in /tmp/g-keyboard-calc8trace. FPR zero = v89/f30, scale.x = v82/f29, scale.y = v95/f31. Simulator proves moving scale.x beyond v89 or zero below v82 fixes all colors, but the tested readable expressions do not produce that numbering.
## Retained source
Consistent glyphBottom and scaledExtent use at every glyph calculation; no mixed direct/helper forms. Reuse existing selectInputTextName in create and the saved substantial animation-resource helper with its missing header declaration. No optimization pragmas retained.
Fresh own-object report: calcCursorPos 93.05215 -> 99.877304, odiff 43 -> 8, 0x518 both; create 96.40169 -> 98.16011, odiff 153 -> 120, 0x590 both. Unit exact functions stay 219/221; data stays 3772/3772.
inputChar unchanged, 90.757355%, odiff 73/136; exact functions stay 41/42, data 288/288.
No fully exact unit; Matching flips are prohibited for this result.
24 glyph-initialization helper and 12 grouped geometry/output helper forms did not beat 8. Compiler tie remains: f29/f30 swap between zero and scale.x. InputChar reset/chain helpers retain a real conditional block or fold the indexed writes; no honest form reproduced the branchless dead compare.
Final review extended scaledExtent to the input-form width bound as well as every glyph width. It preserves the same 8 differences. Chaining the height helper through scaledExtent was measured separately.
Initial gate after retained changes: GATE PASS, zero regressions, zero forbidden patterns, zero readability warnings. Full build and DOL SHA1 passed. A final gate after the width-bound consistency edit follows.
Final GATE PASS: full 43U build ok; DOL 26116613f624061ba99c8d1a299aaa6efa85670d; identical pools; all owned data sections 100%; 0 regressions; 0 forbidden-pattern additions; 0 readability warnings. No exact function gains. Units remain NonMatching.
Height helper chaining gave 81 differences and was rejected. Final instruction counts remain calcCursorPos 8/326, create 120/356, inputChar 73/136.