From a9e1c072b41a60653c87ce28799b432ab6e364a9 Mon Sep 17 00:00:00 2001 From: terencehill Date: Wed, 23 Aug 2023 22:32:33 +0200 Subject: [PATCH] Refactor spectator list code in the Connect Four minigame to better handle shortened names and use a proper fontsize. Also make "Spectators:" translatable --- qcsrc/common/minigames/minigame/c4.qc | 40 ++++++++++++++++----------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/qcsrc/common/minigames/minigame/c4.qc b/qcsrc/common/minigames/minigame/c4.qc index 142e50c89..1cbb1bfc5 100644 --- a/qcsrc/common/minigames/minigame/c4.qc +++ b/qcsrc/common/minigames/minigame/c4.qc @@ -354,6 +354,10 @@ void c4_hud_status(vector pos, vector mySize) entity e; string allspecs = ""; + float allspecs_width = 0; + float max_allspecs_width = mySize.x * 1.7; + float max_current_spec_width = hud_fontsize.x * 5; + float allspecs_lines = 2; FOREACH_MINIGAME_ENTITY(e) { if ( e.classname == "minigame_player" && e.team != C4_SPECTATOR_TEAM ) @@ -373,32 +377,36 @@ void c4_hud_status(vector pos, vector mySize) mypos_x += tile_size_x; } - if (e.classname == "minigame_player" && e.team == C4_SPECTATOR_TEAM) + if (allspecs_width >= 0 && e.classname == "minigame_player" && e.team == C4_SPECTATOR_TEAM) { - string current_spec = ""; + string current_spec = ColorTranslateRGB(entcs_GetName(e.minigame_playerslot - 1)); + current_spec = textShortenToWidth(current_spec, max_current_spec_width, hud_fontsize, stringwidth_colors); + if (allspecs != "") + current_spec = strcat(", ", current_spec); + else + current_spec = current_spec; + + allspecs_width = stringwidth(allspecs, true, hud_fontsize); - string cmpctString = entcs_GetName(e.minigame_playerslot - 1); - if (strlennocol(cmpctString) > 8) + float max_width = max_allspecs_width * allspecs_lines - max_current_spec_width; + if (allspecs_width + stringwidth(current_spec, true, hud_fontsize) < max_width) + allspecs = strcat(allspecs, current_spec); + else { - int new_length = textLengthUpToLength(cmpctString, 8, strlennocol); - cmpctString = strcat(substring(cmpctString, 0, new_length), ".."); + // current_spec doesn't fit in the list + allspecs = strcat(allspecs, ", ..."); + allspecs_width = -1; // skip remaining spectators } - if (strlen(allspecs) > 0) - current_spec = strcat(",", cmpctString); - else - current_spec = cmpctString; - if (strlen(allspecs) < 90) - allspecs = strcat(allspecs, current_spec); } } - if (strlen(allspecs) > 0) + if (allspecs != "") { pos_y = pos_y * 0.3; pos_x = pos_x * 0.41; - ts = minigame_drawstring_wrapped(mySize_x * 1.7, pos, "Spectators: ", '14 14 0', '0.85 0.47 0.42', panel_fg_alpha, DRAWFLAG_NORMAL, 0); - pos_y += 14; - ts = minigame_drawcolorcodedstring_wrapped(mySize_x * 1.7, pos, allspecs, hud_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL, 0); + ts = minigame_drawstring_wrapped(max_allspecs_width, pos, _("Spectators:"), hud_fontsize * 1.25, '0.85 0.47 0.42', panel_fg_alpha, DRAWFLAG_NORMAL, 0); + pos.y += hud_fontsize.y * 1.25; + ts = minigame_drawcolorcodedstring_wrapped(max_allspecs_width, pos, allspecs, hud_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL, 0); } } -- 2.39.2