#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); }