MinecraftConsoles/Minecraft.Client/Common/UI/UIControl_TextInput.cpp
MrTheShy 05a27edc25 Fix TextInput caret behavior and add proper cursor editing for KBM direct edit
The direct text editing mode introduced for KBM users had several
issues with the TextInput control's caret (blinking cursor) and text
manipulation:

1. Caret visible when not editing:
   When navigating to the world name field with keyboard/mouse, Flash's
   Iggy focus system would show the blinking caret even though the field
   wasn't active for editing yet (Enter not pressed). This was misleading
   since typing had no effect in that state.

   Fix: access the FJ_TextInput's internal m_mcCaret MovieClip and
   force its visibility based on editing state. This is enforced every
   tick because setLabel() and Flash focus transitions continuously
   reset the caret state.

2. No cursor movement during editing:
   The direct edit implementation treated the text as a simple buffer
   with push_back/pop_back — there was no concept of cursor position.
   Backspace only deleted from the end, and arrow keys did nothing.

   Fix: track cursor position (m_iCursorPos) in C++ and use wstring
   insert/erase at that position. Arrow keys (Left/Right), Home, End,
   and Delete now work as expected. The visual caret position is synced
   to Flash via the FJ_TextInput's SetCaretIndex method.

3. setLabel() resetting caret position:
   Every call to setLabel() (when text changes) caused Flash to reset
   the caret to the end of the string, making the cursor jump visually
   even though the C++ position was correct.

   Fix: enforce caret position via setCaretIndex every tick during
   editing, so any Flash-side resets are immediately corrected.

New UIControl_TextInput API:
- setCaretVisible(bool): toggles m_mcCaret.visible in Flash
- setCaretIndex(int): calls FJ_TextInput.SetCaretIndex in Flash
2026-03-05 22:04:34 +01:00

104 lines
2.7 KiB
C++

#include "stdafx.h"
#include "UI.h"
#include "UIControl_TextInput.h"
UIControl_TextInput::UIControl_TextInput()
{
m_bHasFocus = false;
}
bool UIControl_TextInput::setupControl(UIScene *scene, IggyValuePath *parent, const string &controlName)
{
UIControl::setControlType(UIControl::eTextInput);
bool success = UIControl_Base::setupControl(scene,parent,controlName);
//TextInput specific initialisers
m_textName = registerFastName(L"text");
m_funcChangeState = registerFastName(L"ChangeState");
m_funcSetCharLimit = registerFastName(L"SetCharLimit");
m_funcSetCaretIndex = registerFastName(L"SetCaretIndex");
return success;
}
void UIControl_TextInput::init(UIString label, int id)
{
m_label = label;
m_id = id;
IggyDataValue result;
IggyDataValue value[2];
value[0].type = IGGY_DATATYPE_string_UTF16;
IggyStringUTF16 stringVal;
stringVal.string = (IggyUTF16*)label.c_str();
stringVal.length = label.length();
value[0].string16 = stringVal;
value[1].type = IGGY_DATATYPE_number;
value[1].number = id;
IggyResult out = IggyPlayerCallMethodRS ( m_parentScene->getMovie() , &result, getIggyValuePath() , m_initFunc , 2 , value );
#ifdef __PSVITA__
// 4J-TomK - add this buttonlist to the vita touch box list
switch(m_parentScene->GetParentLayer()->m_iLayer)
{
case eUILayer_Fullscreen:
case eUILayer_Scene:
case eUILayer_HUD:
ui.TouchBoxAdd(this,m_parentScene);
break;
}
#endif
}
void UIControl_TextInput::ReInit()
{
UIControl_Base::ReInit();
init(m_label, m_id);
}
void UIControl_TextInput::setFocus(bool focus)
{
if(m_bHasFocus != focus)
{
m_bHasFocus = focus;
IggyDataValue result;
IggyDataValue value[1];
value[0].type = IGGY_DATATYPE_number;
value[0].number = focus?0:1;
IggyResult out = IggyPlayerCallMethodRS ( m_parentScene->getMovie() , &result, getIggyValuePath() , m_funcChangeState , 1 , value );
}
}
void UIControl_TextInput::SetCharLimit(int iLimit)
{
IggyDataValue result;
IggyDataValue value[1];
value[0].type = IGGY_DATATYPE_number;
value[0].number = iLimit;
IggyResult out = IggyPlayerCallMethodRS ( m_parentScene->getMovie() , &result, getIggyValuePath() , m_funcSetCharLimit , 1 , value );
}
void UIControl_TextInput::setCaretVisible(bool visible)
{
// Always send to Flash — Iggy's focus system can re-enable the caret at any time
IggyValuePath caretPath;
if (IggyValuePathMakeNameRef(&caretPath, getIggyValuePath(), "m_mcCaret"))
{
IggyValueSetBooleanRS(&caretPath, m_nameVisible, NULL, visible);
}
}
void UIControl_TextInput::setCaretIndex(int index)
{
IggyDataValue result;
IggyDataValue value[1];
value[0].type = IGGY_DATATYPE_number;
value[0].number = index;
IggyResult out = IggyPlayerCallMethodRS ( m_parentScene->getMovie() , &result, getIggyValuePath() , m_funcSetCaretIndex , 1 , value );
}