Auto-select newly created character in selection screen

After creating a character, automatically select it in the character
list instead of defaulting to the previously selected character.

Changes:
- Added selectCharacterByName() method to CharacterScreen
- Store created character name in Application
- On creation success, auto-select the new character by name
- Falls back to saved selection if new character name doesn't match
This commit is contained in:
Kelsi 2026-02-09 22:51:13 -08:00
parent 743b9c7333
commit 5afc9a57d1
4 changed files with 39 additions and 4 deletions

View file

@ -107,6 +107,7 @@ private:
AppState state = AppState::AUTHENTICATION;
bool running = false;
std::string pendingCreatedCharacterName_; // Auto-select after character creation
bool playerCharacterSpawned = false;
bool npcsSpawned = false;
bool spawnSnapToGround = true;

View file

@ -49,12 +49,18 @@ public:
*/
void setStatus(const std::string& message);
/**
* Select character by name (used after character creation)
*/
void selectCharacterByName(const std::string& name);
private:
// UI state
int selectedCharacterIndex = -1;
bool characterSelected = false;
uint64_t selectedCharacterGuid = 0;
bool restoredLastCharacter = false;
std::string newlyCreatedCharacterName; // Auto-select this character if set
// Status
std::string statusMessage;

View file

@ -581,6 +581,7 @@ void Application::setupUICallbacks() {
// Character create screen callbacks
uiManager->getCharacterCreateScreen().setOnCreate([this](const game::CharCreateData& data) {
pendingCreatedCharacterName_ = data.name; // Store name for auto-selection
gameHandler->createCharacter(data);
});
@ -591,9 +592,15 @@ void Application::setupUICallbacks() {
// Character create result callback
gameHandler->setCharCreateCallback([this](bool success, const std::string& msg) {
if (success) {
// Auto-select the newly created character
if (!pendingCreatedCharacterName_.empty()) {
uiManager->getCharacterScreen().selectCharacterByName(pendingCreatedCharacterName_);
pendingCreatedCharacterName_.clear();
}
setState(AppState::CHARACTER_SELECTION);
} else {
uiManager->getCharacterCreateScreen().setStatus(msg, true);
pendingCreatedCharacterName_.clear();
}
});

View file

@ -56,16 +56,31 @@ void CharacterScreen::render(game::GameHandler& gameHandler) {
// Restore last-selected character (once per screen visit)
if (!restoredLastCharacter) {
uint64_t lastGuid = loadLastCharacter();
if (lastGuid != 0) {
// Priority 1: Select newly created character if set
if (!newlyCreatedCharacterName.empty()) {
for (size_t i = 0; i < characters.size(); ++i) {
if (characters[i].guid == lastGuid) {
if (characters[i].name == newlyCreatedCharacterName) {
selectedCharacterIndex = static_cast<int>(i);
selectedCharacterGuid = lastGuid;
selectedCharacterGuid = characters[i].guid;
saveLastCharacter(characters[i].guid);
newlyCreatedCharacterName.clear();
break;
}
}
}
// Priority 2: Restore last selected character
if (selectedCharacterIndex < 0) {
uint64_t lastGuid = loadLastCharacter();
if (lastGuid != 0) {
for (size_t i = 0; i < characters.size(); ++i) {
if (characters[i].guid == lastGuid) {
selectedCharacterIndex = static_cast<int>(i);
selectedCharacterGuid = lastGuid;
break;
}
}
}
}
// Fall back to first character if nothing matched
if (selectedCharacterIndex < 0) {
selectedCharacterIndex = 0;
@ -298,6 +313,12 @@ void CharacterScreen::setStatus(const std::string& message) {
statusMessage = message;
}
void CharacterScreen::selectCharacterByName(const std::string& name) {
newlyCreatedCharacterName = name;
restoredLastCharacter = false; // Allow re-selection in render()
selectedCharacterIndex = -1;
}
ImVec4 CharacterScreen::getFactionColor(game::Race race) const {
// Alliance races: blue
if (race == game::Race::HUMAN ||