mirror of
https://github.com/BernardoGiordano/Checkpoint
synced 2024-11-10 03:54:13 +00:00
Optimize string fitting
This commit is contained in:
parent
5b7d897b61
commit
f955d5784b
6 changed files with 40 additions and 16 deletions
|
@ -28,5 +28,6 @@ void SDLH_Render(void);
|
|||
|
||||
void drawOutline(u32 x, u32 y, u16 w, u16 h, u8 size, SDL_Color color);
|
||||
void drawPulsingOutline(u32 x, u32 y, u16 w, u16 h, u8 size, SDL_Color color);
|
||||
std::string trimToFit(const std::string& text, u32 maxsize, size_t textsize);
|
||||
|
||||
#endif
|
|
@ -39,6 +39,7 @@
|
|||
struct User {
|
||||
u128 id;
|
||||
std::string name;
|
||||
std::string shortName;
|
||||
SDL_Texture* icon;
|
||||
};
|
||||
|
||||
|
@ -50,6 +51,7 @@ namespace Account {
|
|||
SDL_Texture* icon(u128 id);
|
||||
u128 selectAccount(void);
|
||||
std::string username(u128 id);
|
||||
std::string shortName(u128 id);
|
||||
}
|
||||
|
||||
#endif
|
|
@ -42,5 +42,6 @@ inline bool g_ftpAvailable = false;
|
|||
inline bool g_shouldExitNetworkLoop = false;
|
||||
inline std::string g_selectedCheatKey;
|
||||
inline std::vector<std::string> g_selectedCheatCodes;
|
||||
inline u32 g_username_dotsize;
|
||||
|
||||
#endif
|
|
@ -68,21 +68,8 @@ void MainScreen::draw() const
|
|||
SDLH_DrawImageScale(
|
||||
Account::icon(g_currentUId), 1280 - SIDEBAR_w + (SIDEBAR_w - USER_ICON_SIZE) / 2, 720 - USER_ICON_SIZE - 30, USER_ICON_SIZE, USER_ICON_SIZE);
|
||||
|
||||
// TODO: optimize this
|
||||
u32 username_dotsize;
|
||||
u32 username_w, username_h;
|
||||
SDLH_GetTextDimensions(13, "...", &username_dotsize, NULL);
|
||||
std::string username = "";
|
||||
for (size_t i = 0, len = Account::username(g_currentUId).length(); i < len; i++) {
|
||||
SDLH_GetTextDimensions(13, username.c_str(), &username_w, NULL);
|
||||
if (username_w < SIDEBAR_w - username_dotsize * 2) {
|
||||
username += Account::username(g_currentUId)[i];
|
||||
}
|
||||
else {
|
||||
username += "...";
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::string username = Account::shortName(g_currentUId);
|
||||
SDLH_GetTextDimensions(13, username.c_str(), &username_w, &username_h);
|
||||
SDLH_DrawTextBox(13, 1280 - SIDEBAR_w + (SIDEBAR_w - username_w) / 2, 720 - 28 + (28 - username_h) / 2, theme().c6, SIDEBAR_w, username.c_str());
|
||||
|
||||
|
|
|
@ -52,6 +52,9 @@ bool SDLH_Init(void)
|
|||
plGetSharedFontByType(&fontData, PlSharedFontType_Standard);
|
||||
plGetSharedFontByType(&fontExtData, PlSharedFontType_NintendoExt);
|
||||
|
||||
// utils
|
||||
SDLH_GetTextDimensions(13, "...", &g_username_dotsize, NULL);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -193,4 +196,21 @@ void drawPulsingOutline(u32 x, u32 y, u16 w, u16 h, u8 size, SDL_Color color)
|
|||
color = FC_MakeColor(color.r + (255 - color.r) * highlight_multiplier, color.g + (255 - color.g) * highlight_multiplier,
|
||||
color.b + (255 - color.b) * highlight_multiplier, 255);
|
||||
drawOutline(x, y, w, h, size, color);
|
||||
}
|
||||
|
||||
std::string trimToFit(const std::string& text, u32 maxsize, size_t textsize)
|
||||
{
|
||||
u32 width;
|
||||
std::string newtext = "";
|
||||
for (size_t i = 0, len = text.length(); i < len; i++) {
|
||||
SDLH_GetTextDimensions(textsize, newtext.c_str(), &width, NULL);
|
||||
if (width < maxsize) {
|
||||
newtext += text[i];
|
||||
}
|
||||
else {
|
||||
newtext += "...";
|
||||
break;
|
||||
}
|
||||
}
|
||||
return newtext;
|
||||
}
|
|
@ -52,13 +52,14 @@ std::vector<u128> Account::ids(void)
|
|||
|
||||
static User getUser(u128 id)
|
||||
{
|
||||
User user{id, "", NULL};
|
||||
User user{id, "", "", NULL};
|
||||
AccountProfile profile;
|
||||
AccountProfileBase profilebase;
|
||||
memset(&profilebase, 0, sizeof(profilebase));
|
||||
|
||||
if (R_SUCCEEDED(accountGetProfile(&profile, id)) && R_SUCCEEDED(accountProfileGet(&profile, NULL, &profilebase))) {
|
||||
user.name = std::string(profilebase.username, 0x20);
|
||||
user.name = std::string(profilebase.username, 0x20);
|
||||
user.shortName = trimToFit(user.name, 96 - g_username_dotsize * 2, 13);
|
||||
|
||||
// load icon
|
||||
u8* buffer;
|
||||
|
@ -86,6 +87,18 @@ std::string Account::username(u128 id)
|
|||
return got->second.name;
|
||||
}
|
||||
|
||||
std::string Account::shortName(u128 id)
|
||||
{
|
||||
std::map<u128, User>::const_iterator got = mUsers.find(id);
|
||||
if (got == mUsers.end()) {
|
||||
User user = getUser(id);
|
||||
mUsers.insert({id, user});
|
||||
return user.shortName;
|
||||
}
|
||||
|
||||
return got->second.shortName;
|
||||
}
|
||||
|
||||
SDL_Texture* Account::icon(u128 id)
|
||||
{
|
||||
std::map<u128, User>::const_iterator got = mUsers.find(id);
|
||||
|
|
Loading…
Reference in a new issue