Ported to textfield

This commit is contained in:
InviseDivine
2026-03-10 21:37:12 +02:00
parent ae84705332
commit 2d17f9d152
5 changed files with 48 additions and 45 deletions

View File

View File

@@ -9,7 +9,7 @@
UsernameScreen::UsernameScreen()
: _btnDone(0, "Done"),
_input(""),
tUsername(0, "Username"),
_cursorBlink(0)
{
}
@@ -23,54 +23,64 @@ void UsernameScreen::init()
_input = "";
_btnDone.active = false; // disabled until name typed
buttons.push_back(&_btnDone);
tabButtons.push_back(&_btnDone);
textBoxes.push_back(&tUsername);
setupPositions();
}
void UsernameScreen::setupPositions()
{
int cx = width / 2;
int cy = height / 2;
_btnDone.width = 120;
_btnDone.height = 20;
_btnDone.x = (width - _btnDone.width) / 2;
_btnDone.y = height / 2 + 52;
tUsername.x = _btnDone.x;
tUsername.y = _btnDone.y - 60;
tUsername.width = 120;
tUsername.height = 20;
}
void UsernameScreen::tick()
{
_cursorBlink++;
for (auto* tb : textBoxes)
tb->tick(minecraft);
}
void UsernameScreen::keyPressed(int eventKey)
{
if (eventKey == Keyboard::KEY_BACKSPACE) {
if (!_input.empty())
_input.erase(_input.size() - 1, 1);
} else if (eventKey == Keyboard::KEY_RETURN) {
if (!_input.empty())
if (eventKey == Keyboard::KEY_RETURN) {
if (!tUsername.text.empty())
buttonClicked(&_btnDone);
}
// deliberately do NOT call super::keyPressed — that would close the screen on Escape
_btnDone.active = !_input.empty();
_btnDone.active = !tUsername.text.empty();
Screen::keyPressed(eventKey);
}
void UsernameScreen::keyboardNewChar(char inputChar)
{
if (_input.size() < 16 && inputChar >= 32 && inputChar < 127)
_input += inputChar;
_btnDone.active = !_input.empty();
for (auto* tb : textBoxes) tb->handleChar(inputChar);
}
void UsernameScreen::mouseClicked(int x, int y, int button)
{
int cx = width / 2;
int cy = height / 2;
int boxW = 160;
int boxH = 18;
int boxX = cx - boxW / 2;
int boxY = cy - 5;
if (x >= boxX && x <= boxX + boxW && y >= boxY && y <= boxY + boxH) {
minecraft->platform()->showKeyboard();
int lvlTop = tUsername.y - (Font::DefaultLineHeight + 4);
int lvlBottom = tUsername.y + tUsername.height;
int lvlLeft = tUsername.x;
int lvlRight = tUsername.x + tUsername.width;
bool clickedLevel = x >= lvlLeft && x < lvlRight && y >= lvlTop && y < lvlBottom;
if (clickedLevel) {
tUsername.setFocus(minecraft);
} else {
super::mouseClicked(x, y, button);
// click outside both fields -> blur both
tUsername.loseFocus(minecraft);
}
}
@@ -81,10 +91,10 @@ void UsernameScreen::removed()
void UsernameScreen::buttonClicked(Button* button)
{
if (button == &_btnDone && !_input.empty()) {
minecraft->options.username = _input;
if (button == &_btnDone && !tUsername.text.empty()) {
minecraft->options.username = tUsername.text;
minecraft->options.save();
minecraft->user->name = _input;
minecraft->user->name = tUsername.text;
minecraft->setScreen(NULL); // goes to StartMenuScreen
}
}
@@ -105,23 +115,8 @@ void UsernameScreen::render(int xm, int ym, float a)
drawCenteredString(font, "identify you in chat. Don't worry, you can", cx, cy - 40, 0xffaaaaaa);
drawCenteredString(font, "change it anytime.", cx, cy - 28, 0xffaaaaaa);
// Input box background
int boxW = 160;
int boxH = 18;
int boxX = cx - boxW / 2;
int boxY = cy - 5;
fill(boxX - 1, boxY - 1, boxX + boxW + 1, boxY + boxH + 1, 0xff000000);
fill(boxX, boxY, boxX + boxW, boxY + boxH, 0xff202020);
// Build display string with cursor
std::string display = _input;
if ((_cursorBlink / 10) % 2 == 0)
display += '|';
font->draw(display, (float)(boxX + 4), (float)(boxY + (boxH - 8) / 2 + 1), 0xffffffff, false);
// Hint below box
drawCenteredString(font, "Max 16 characters", cx, cy + 20, 0xff808080);
// // Hint below box
// drawCenteredString(font, "Max 16 characters", cx, cy + 20, 0xff808080);
// Buttons (Done)
super::render(xm, ym, a);

View File

@@ -3,6 +3,7 @@
#include "../Screen.h"
#include "../components/Button.h"
#include "client/gui/components/TextBox.h"
#include <string>
class UsernameScreen : public Screen
@@ -30,6 +31,7 @@ protected:
private:
Button _btnDone;
TextBox tUsername;
std::string _input;
int _cursorBlink;
};

View File

@@ -64,6 +64,7 @@ void AvailableGamesList::renderItem( int i, int x, int y, int h, Tesselator& t )
JoinGameScreen::JoinGameScreen()
: bJoin( 2, "Join Game"),
bBack( 3, "Back"),
bJoinByIp(4, "Join By IP"),
bHeader(0, ""),
gamesList(NULL)
{
@@ -80,6 +81,7 @@ void JoinGameScreen::init()
{
//buttons.push_back(&bJoin);
buttons.push_back(&bBack);
buttons.push_back(&bJoinByIp);
buttons.push_back(&bHeader);
minecraft->raknetInstance->clearServerList();
@@ -88,6 +90,7 @@ void JoinGameScreen::init()
#ifdef ANDROID
//tabButtons.push_back(&bJoin);
tabButtons.push_back(&bBack);
tabButtons.push_back(&bJoinByIp);
#endif
}
@@ -97,14 +100,16 @@ void JoinGameScreen::setupPositions() {
//#ifdef ANDROID
bJoin.y = 0;
bBack.y = 0;
bJoinByIp.y = 0;
bHeader.y = 0;
//#endif
// Center buttons
//bJoin.x = width / 2 - 4 - bJoin.w;
bBack.x = 0;//width / 2 + 4;
bHeader.x = bBack.width;
bHeader.width = width - bHeader.x;
bJoinByIp.x = width - bJoinByIp.width;;
bHeader.x = bJoinByIp.width;
bHeader.width = width - (bBack.width + bJoinByIp.width);
}
void JoinGameScreen::buttonClicked(Button* button)

View File

@@ -65,6 +65,7 @@ public:
private:
Button bJoin;
TButton bBack;
TButton bJoinByIp;
THeader bHeader;
AvailableGamesList* gamesList;
};