From 5afc9a57d1622fe540fa376ca17f72c30fd05d2c Mon Sep 17 00:00:00 2001 From: Kelsi Date: Mon, 9 Feb 2026 22:51:13 -0800 Subject: [PATCH] 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 --- include/core/application.hpp | 1 + include/ui/character_screen.hpp | 6 ++++++ src/core/application.cpp | 7 +++++++ src/ui/character_screen.cpp | 29 +++++++++++++++++++++++++---- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/include/core/application.hpp b/include/core/application.hpp index 2492e73d..6b016842 100644 --- a/include/core/application.hpp +++ b/include/core/application.hpp @@ -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; diff --git a/include/ui/character_screen.hpp b/include/ui/character_screen.hpp index b872b7bf..a8bc21c1 100644 --- a/include/ui/character_screen.hpp +++ b/include/ui/character_screen.hpp @@ -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; diff --git a/src/core/application.cpp b/src/core/application.cpp index d8ca5658..6251e936 100644 --- a/src/core/application.cpp +++ b/src/core/application.cpp @@ -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(); } }); diff --git a/src/ui/character_screen.cpp b/src/ui/character_screen.cpp index 9e8bd67e..5efb3c70 100644 --- a/src/ui/character_screen.cpp +++ b/src/ui/character_screen.cpp @@ -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(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(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 ||