From 2d70b68b731c07ad4e147078aec039a3a7cb5a4e Mon Sep 17 00:00:00 2001 From: mschiller890 Date: Wed, 11 Mar 2026 11:03:06 +0200 Subject: [PATCH] Fixed Done button not working and backspace on Android --- UsernameScreen.cpp | 126 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 UsernameScreen.cpp diff --git a/UsernameScreen.cpp b/UsernameScreen.cpp new file mode 100644 index 0000000..c1455d9 --- /dev/null +++ b/UsernameScreen.cpp @@ -0,0 +1,126 @@ +#include "UsernameScreen.h" +#include "StartMenuScreen.h" +#include "../../Minecraft.h" +#include "../../User.h" +#include "../Font.h" +#include "../components/Button.h" +#include "../../../platform/input/Keyboard.h" +#include "../../../AppPlatform.h" + +UsernameScreen::UsernameScreen() +: _btnDone(0, "Done"), + tUsername(0, "Username"), + _cursorBlink(0) +{ +} + +UsernameScreen::~UsernameScreen() +{ +} + +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() +{ + for (auto* tb : textBoxes) + tb->tick(minecraft); +} + +void UsernameScreen::keyPressed(int eventKey) +{ + 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 = !tUsername.text.empty(); + + Screen::keyPressed(eventKey); +} + +void UsernameScreen::keyboardNewChar(char inputChar) +{ + for (auto* tb : textBoxes) tb->handleChar(inputChar); +} + +void UsernameScreen::mouseClicked(int x, int y, int button) +{ + 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 { + // click outside both fields -> blur both + tUsername.loseFocus(minecraft); + } + + // also let the parent class handle button presses/etc. + Screen::mouseClicked(x, y, button); +} + +void UsernameScreen::removed() +{ + minecraft->platform()->hideKeyboard(); +} + +void UsernameScreen::buttonClicked(Button* button) +{ + if (button == &_btnDone && !tUsername.text.empty()) { + minecraft->options.username = tUsername.text; + minecraft->options.save(); + minecraft->user->name = tUsername.text; + minecraft->setScreen(NULL); // goes to StartMenuScreen + } +} + +void UsernameScreen::render(int xm, int ym, float a) +{ + // Dark dirt background + renderBackground(); + + int cx = width / 2; + int cy = height / 2; + + // Title + drawCenteredString(font, "Enter your username", cx, cy - 70, 0xffffffff); + + // Subtitle + drawCenteredString(font, "Please choose a username so others can easily", cx, cy - 52, 0xffaaaaaa); + drawCenteredString(font, "identify you in chat. Don't worry, you can", cx, cy - 40, 0xffaaaaaa); + drawCenteredString(font, "change it anytime.", cx, cy - 28, 0xffaaaaaa); + + // // Hint below box + // drawCenteredString(font, "Max 16 characters", cx, cy + 20, 0xff808080); + + // Buttons (Done) + super::render(xm, ym, a); +}