From efc31f70595728422199435a905b1737219fe510 Mon Sep 17 00:00:00 2001 From: hetuw Date: Fri, 17 Jul 2020 13:42:14 +0200 Subject: [PATCH] phex - changed the way names are drawn, maybe this fixes a bug --- gameSource/LivingLifePage.h | 3 --- gameSource/hetuwmod.cpp | 11 +++++++---- gameSource/phex.cpp | 24 +++++++++++++----------- gameSource/phex.h | 5 +++-- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/gameSource/LivingLifePage.h b/gameSource/LivingLifePage.h index 1e37227d6..2d30f90c8 100644 --- a/gameSource/LivingLifePage.h +++ b/gameSource/LivingLifePage.h @@ -24,7 +24,6 @@ #include "TextField.h" -#include #define NUM_HUNGER_BOX_SPRITES 20 @@ -311,8 +310,6 @@ typedef struct LiveObject { char shouldDrawPathMarks; double pathMarkFade; - std::string phexHash; - // messages that arrive while we're still showing our current // movement animation SimpleVector pendingReceivedMessages; diff --git a/gameSource/hetuwmod.cpp b/gameSource/hetuwmod.cpp index 8bf728784..dd946668f 100644 --- a/gameSource/hetuwmod.cpp +++ b/gameSource/hetuwmod.cpp @@ -2781,8 +2781,9 @@ void HetuwMod::drawPlayerNames( LiveObject* player ) { getRelationNameColor( player->relationName, playerNameColor ); setDrawColor( 0.0, 0.0, 0.0, 0.8 ); - if ( itsTimeToDrawPhexName() && player->phexHash.length() > 0) { - std::string* name = Phex::getUserDisplayName(Phex::users[player->phexHash]); + if (itsTimeToDrawPhexName() && + Phex::playerIdToHash.find(player->id) != Phex::playerIdToHash.end()) { + std::string* name = Phex::getUserDisplayName(Phex::playerIdToHash[player->id]); float textWidth = customFont->measureString( name->c_str() ); drawRect( playerNamePos, textWidth/2 + 6, 16 ); setDrawColor( playerNameColor[0], playerNameColor[1], playerNameColor[2], 1 ); @@ -2825,10 +2826,12 @@ void HetuwMod::drawHighlightedPlayer() { livingLifePage->hetuwDrawScaledHandwritingFont( player->curseName, playerNamePos, guiScale, alignCenter ); playerNamePos.y -= 32*guiScale; } - if ( itsTimeToDrawPhexName() && player->phexHash.length() > 0) { + if (itsTimeToDrawPhexName() && + Phex::playerIdToHash.find(player->id) != Phex::playerIdToHash.end()) { + double scale = customFont->hetuwGetScaleFactor(); customFont->hetuwSetScaleFactor(scale * guiScale); - std::string* name = Phex::getUserDisplayName(Phex::users[player->phexHash]); + std::string* name = Phex::getUserDisplayName(Phex::playerIdToHash[player->id]); textWidth = customFont->measureString( name->c_str() ); setDrawColor( 0.0, 0.0, 0.0, 0.8 ); drawRect( playerNamePos, textWidth/2 + 6*guiScale, 16*guiScale ); diff --git a/gameSource/phex.cpp b/gameSource/phex.cpp index ffb5436e9..338ef4eab 100644 --- a/gameSource/phex.cpp +++ b/gameSource/phex.cpp @@ -24,6 +24,7 @@ std::string Phex::strCmdChar; std::string Phex::publicHash = ""; std::unordered_map Phex::users; +std::unordered_map Phex::playerIdToHash; bool Phex::hasFocus = false; bool Phex::isMinimized = false; @@ -351,7 +352,7 @@ void Phex::serverCmdSAY(std::vector input) { chatElement.text = joinStr(input, " ", 4); createUser(chatElement.hash); - chatElement.name = string(*getUserDisplayName(users[chatElement.hash])); + chatElement.name = string(*getUserDisplayName(chatElement.hash)); chatElement.textToDraw = colorCodeNamesInChat+chatElement.name+": "+colorCodeWhite+chatElement.text; mainChatWindow.addElement(chatElement); @@ -448,8 +449,8 @@ void Phex::serverCmdHASH_SERVER_LIFE(std::vector input) { printf("Phex cant find player with id: %d\n", playerID); return; } - player->phexHash = string(input[1]); createUser(input[1]); + playerIdToHash[playerID] = input[1]; users[input[1]].inGameServerPlayerID = playerID; } catch(std::exception const & ex) { printf("Phex EXCEPTION when receiving HASH_SERVER_LIFE command\n"); @@ -702,20 +703,21 @@ void Phex::Text::draw() { HetuwMod::hDrawRecFromPercent(drawRec); } -void Phex::createUser(std::string hash) { +void Phex::createUser(std::string &hash) { users[hash].hash = hash; //if (users.find(hash) == users.end()) return; } -std::string* Phex::getUserDisplayName(User &user) { - if (user.displayName.length() > 0) return &user.displayName; - user.displayName = string(user.name); - if (user.displayName.length() < 1) { - user.displayName = user.hash; - if (user.displayName.length() > ChatElement::maxHashDisplayLength) - user.displayName = user.displayName.substr(0, ChatElement::maxHashDisplayLength); +std::string* Phex::getUserDisplayName(std::string &hash) { + createUser(hash); + if (users[hash].displayName.length() > 0) return &users[hash].displayName; + users[hash].displayName = string(users[hash].name); + if (users[hash].displayName.length() < 1) { + users[hash].displayName = users[hash].hash; + if (users[hash].displayName.length() > ChatElement::maxHashDisplayLength) + users[hash].displayName = users[hash].displayName.substr(0, ChatElement::maxHashDisplayLength); } - return &user.displayName; + return &users[hash].displayName; } time_t Phex::strToTimeT(std::string str) { diff --git a/gameSource/phex.h b/gameSource/phex.h index 76827fe0c..20769a0dc 100644 --- a/gameSource/phex.h +++ b/gameSource/phex.h @@ -226,6 +226,7 @@ class Phex { static std::string publicHash; static std::unordered_map users; + static std::unordered_map playerIdToHash; static bool hasFocus; static bool isMinimized; @@ -340,8 +341,8 @@ class Phex { static double getLineHeight(HetuwFont *font); static void drawString(std::string str, doublePair startPos); - static void createUser(std::string hash); - static std::string* getUserDisplayName(User &user); + static void createUser(std::string &hash); + static std::string* getUserDisplayName(std::string &hash); static time_t strToTimeT(std::string str); static void addCmdMessageToChatWindow(std::string msg, int type = 1);