Zappy GUI
C++ graphic client: renderer, camera, network
Loading...
Searching...
No Matches
HudWidget.cpp
Go to the documentation of this file.
1#include "HudWidget.hpp"
2
3#include <algorithm>
4#include <unordered_map>
5
6#include "I18n.hpp"
7#include "TooltipRenderer.hpp"
8
9void HudWidget::setWorld(const WorldState* world) { _world = world; }
10void HudWidget::setDevMode(bool dev, int port, const std::string& machine)
11{
12 _devMode = dev;
13 _devPort = port;
14 _devMachine = machine;
15}
16void HudWidget::setTimeUnit(int timeUnit) { _timeUnit = timeUnit; }
17void HudWidget::setServerUptime(unsigned int uptime) { _serverUptimeSeconds = uptime; }
18void HudWidget::setTeamColorFunc(std::function<Color(const std::string&)> func)
19{
20 _colorFunc = std::move(func);
21}
22
23bool HudWidget::handleInput() { return false; } // HUD is not interactive
24
25void HudWidget::draw(int scaledFontSize) const
26{
27 if (!_world) return;
28
29 Color bgColor = {20, 25, 35, 220};
30 Color borderColor = {60, 70, 90, 200};
31 Color accentColor = {210, 220, 240, 255};
32
33 std::string mapText = std::string(I18n::get(I18n::Key::HUD_MAP)) +
34 std::to_string(_world->width) + "x" + std::to_string(_world->height);
35
36 std::string uptimeText;
37 if (_serverUptimeSeconds == 0) {
39 } else {
40 int uptimeHours = _serverUptimeSeconds / 3600;
41 int uptimeMinutes = (_serverUptimeSeconds % 3600) / 60;
42 int uptimeSeconds = _serverUptimeSeconds % 60;
44 if (uptimeHours > 0)
45 uptimeText += std::to_string(uptimeHours) + I18n::get(I18n::Key::HUD_UPTIME_H);
46 if (uptimeMinutes > 0 || uptimeHours > 0)
47 uptimeText += std::to_string(uptimeMinutes) + I18n::get(I18n::Key::HUD_UPTIME_M);
48 uptimeText += std::to_string(uptimeSeconds) + I18n::get(I18n::Key::HUD_UPTIME_S);
49 }
50
51 std::unordered_map<std::string, int> teamPlayerCounts;
52 for (const auto& teamName : _world->teams) teamPlayerCounts[teamName] = 0;
53 for (const auto& [id, player] : _world->players) teamPlayerCounts[player.team]++;
54
55 std::vector<std::pair<std::string, int>> sortedTeams(teamPlayerCounts.begin(),
56 teamPlayerCounts.end());
57 std::sort(sortedTeams.begin(), sortedTeams.end(),
58 [](const auto& a, const auto& b) { return a.second > b.second; });
59
60 auto builder = TooltipRenderer::create();
61
62 if (_devMode) {
63 int fps = GetFPS();
64 Color fpsColor = fps >= 55 ? GREEN : (fps >= 30 ? YELLOW : RED);
65 builder.addLine(std::string(I18n::get(I18n::Key::HUD_FPS)) + std::to_string(fps), fpsColor);
66 builder.addLine(
67 std::string(I18n::get(I18n::Key::HUD_TIME_UNIT)) + std::to_string(_timeUnit),
68 accentColor);
69 builder.addLine(std::string(I18n::get(I18n::Key::HUD_PORT)) + std::to_string(_devPort),
70 accentColor);
71 builder.addLine(std::string(I18n::get(I18n::Key::HUD_MACHINE)) + _devMachine, accentColor);
72 }
73
74 builder.addLine(mapText, accentColor).addLine(uptimeText, accentColor);
75
76 for (size_t i = 0; i < std::min(sortedTeams.size(), size_t(5)); i++) {
77 const auto& [teamName, playerCount] = sortedTeams[i];
78 std::string teamLine = teamName + ": " + std::to_string(playerCount);
79 builder.addLine(teamLine, _colorFunc ? _colorFunc(teamName) : WHITE);
80 }
81
82 builder.setBackgroundColor(bgColor)
83 .setBackgroundAlpha(180)
84 .setBorderColor(borderColor)
85 .setBorderThickness(2)
86 .setPadding(10)
87 .setFontSize(scaledFontSize)
89 .draw({10.0f, 10.0f});
90}
HUD panel displaying general game information.
unsigned int _serverUptimeSeconds
Definition HudWidget.hpp:33
void setServerUptime(unsigned int uptime)
Definition HudWidget.cpp:17
const WorldState * _world
Definition HudWidget.hpp:28
std::string _devMachine
Definition HudWidget.hpp:31
int _timeUnit
Definition HudWidget.hpp:32
bool _devMode
Definition HudWidget.hpp:29
void setTeamColorFunc(std::function< Color(const std::string &)> func)
Definition HudWidget.cpp:18
void setDevMode(bool dev, int port, const std::string &machine)
Definition HudWidget.cpp:10
bool handleInput() override
Processes user input.
Definition HudWidget.cpp:23
void draw(int scaledFontSize) const override
Draws the widget.
Definition HudWidget.cpp:25
std::function< Color(const std::string &)> _colorFunc
Definition HudWidget.hpp:34
void setWorld(const WorldState *world)
Definition HudWidget.cpp:9
void setTimeUnit(int timeUnit)
Definition HudWidget.cpp:16
static const char * get(Key key)
Definition I18n.hpp:86
@ HUD_UPTIME_PREFIX
@ HUD_UPTIME_UNKNOWN
static Builder create()
Creates a new tooltip builder.
Represents the state of the world in the game, including the map size, tile contents,...
std::vector< std::string > teams
std::unordered_map< int, Player > players