Player12
An open source football management simulator
Loading...
Searching...
No Matches
player_ui.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2// Football Management Project
3// Copyright (c) 2025 - 2026 Flavio Milinanni. All Rights Reserved.
4//
5// This file is part of the Football Management Project.
6// See the LICENSE file in the project root.
7// -----------------------------------------------------------------------------
8
9#pragma once
10
11#include <imgui.h>
12
13#include <algorithm>
14#include <format>
15#include <string>
16
18#include "global/stats_config.h"
20#include "gui/widgets/format.h"
21#include "gui/widgets/theme.h"
22#include "gui/widgets/widgets.h"
23#include "model/player.h"
24#include "model/role_utils.h"
25
26namespace PlayerUI
27{
36inline void detailPanel(const char* id, const Player* player,
37 const StatsConfig& statsConfig,
38 const Player* comparison = nullptr, float height = 0.0f)
39{
40 const Theme::Palette& palette = Theme::palette();
41 const float dpi = ImGui::GetStyle().FontScaleDpi;
42 // A negative height sizes the card to its content (inside a scrolling
43 // parent), 0 fills the remaining space.
44 if (height < 0.0f)
45 UI::beginAutoHeightCard(id, nullptr);
46 else
47 UI::beginCard(id, nullptr, ImVec2(0.0f, height), true);
48 if (!player)
49 {
50 ImGui::PushStyleColor(ImGuiCol_Text, palette.muted);
51 ImGui::TextWrapped("%s", LOC("PLAYER_SELECT_PROMPT"));
52 ImGui::PopStyleColor();
54 return;
55 }
56
57 const double overall = player->getOverall(statsConfig);
58 UI::ratingChip(overall);
59 ImGui::SameLine();
60 {
62 ImGui::TextUnformatted(player->getName().c_str());
63 }
64 ImGui::PushStyleColor(ImGuiCol_Text, palette.muted);
65 ImGui::TextWrapped(
66 "%s · %s %d · %d cm · %s", RoleUtils::longName(player->getRole()),
67 LOC("PLAYER_AGE"), player->getAge(), player->getHeight(),
68 player->getFoot() == Foot::Right ? LOC("PLAYER_FOOT_RIGHT")
69 : LOC("PLAYER_FOOT_LEFT"));
70 ImGui::PopStyleColor();
71
72 const float keyWidth = 130.0f * dpi;
73 const std::string wage = Format::money(player->getWage());
74 UI::keyValue(LOC("PLAYER_WEEKLY_WAGE"), wage.c_str(), keyWidth);
75 const std::string contract = std::to_string(player->getContractYears());
76 ImGui::PushStyleColor(ImGuiCol_Text, player->getContractYears() <= 1
77 ? palette.negative
78 : palette.text);
79 UI::keyValue(LOC("PLAYER_CONTRACT"), contract.c_str(), keyWidth);
80 ImGui::PopStyleColor();
81
82 if (comparison)
83 {
84 const double delta = overall - comparison->getOverall(statsConfig);
85 ImGui::TextColored(delta >= 0.0 ? palette.positive : palette.negative,
86 "%s: %+.1f", LOC("PLAYER_OVERALL_CHANGE"), delta);
87 if (player->getRole() == comparison->getRole())
88 {
89 ImGui::TextColored(palette.positive, "%s", LOC("PLAYER_ROLE_MATCH"));
90 }
91 else
92 {
93 ImGui::TextColored(palette.warning, "%s: %s -> %s",
94 LOC("PLAYER_ROLE_CHANGE"),
95 RoleUtils::shortName(comparison->getRole()),
96 RoleUtils::shortName(player->getRole()));
97 }
98 }
99
100 ImGui::Dummy(ImVec2(0.0f, Theme::Space::XS * dpi));
101 UI::sectionLabel(LOC("PLAYER_ATTRIBUTES"));
102 const float labelWidth = 100.0f * dpi;
103 for (const auto& [name, value] : player->getStats())
104 {
105 const std::string label = PlayerView::statLabel(name);
106 if (!comparison)
107 {
108 UI::attributeBar(label.c_str(), value, labelWidth);
109 continue;
110 }
111 const auto other = comparison->getStats().find(name);
112 const float delta =
113 value - (other != comparison->getStats().end() ? other->second : 0.0f);
114 const std::string deltaText = std::format("{:+.0f}", delta);
115 const float deltaWidth = ImGui::CalcTextSize("+00").x;
116 const float rowStart = ImGui::GetCursorPosX();
117 const float available = ImGui::GetContentRegionAvail().x;
118 UI::attributeBar(label.c_str(), value, labelWidth, 100.0f,
119 available - deltaWidth - Theme::Space::S * dpi);
120 ImGui::SameLine(rowStart + available - deltaWidth);
121 ImGui::TextColored(delta >= 0.0f ? palette.positive : palette.negative,
122 "%s", deltaText.c_str());
123 }
124 UI::endCard();
125}
126} // namespace PlayerUI
Persistent career identity, attributes, traits and day-to-day state.
Definition player.h:104
Foot getFoot() const
Gets the player's preferred foot.
Definition player.cpp:93
uint32_t getWage() const
Gets the player's wage.
Definition player.cpp:71
PlayerRole getRole() const
Gets the player's role.
Definition player.cpp:67
uint8_t getHeight() const
Gets the player's height.
Definition player.cpp:91
int getAge() const
Gets the player's age.
Definition player.cpp:59
std::string getName() const
Gets the player's full name.
Definition player.cpp:53
uint8_t getContractYears() const
Gets the player's remaining contract years.
Definition player.cpp:75
double getOverall(const StatsConfig &stats_config) const
Calculates the player's overall rating based on stats and configuration.
Definition player.cpp:105
const std::map< std::string, float > & getStats() const
Gets the player's stats map.
Definition player.cpp:97
static const char * shortName(PlayerRole role)
Localised short position name for display ("GK", "POR"). The codes of toString() are for persistence ...
Definition role_utils.cpp:35
static const char * longName(PlayerRole role)
Localised full position name ("Goalkeeper", "Portiere").
Definition role_utils.cpp:41
Pushes a typography level for the lifetime of the object.
Definition theme.h:176
const char * name
Definition input_actions.cpp:26
const char * label
Definition input_actions.cpp:27
#define LOC(key)
Global helper macro for concise UI code localization.
Definition language_manager.h:130
std::string money(int64_t amount)
Compact currency in the current language, e.g. "€12.4M", "€850K", "-€3.1M"; Italian "€ 12,...
Definition format.cpp:35
Definition player_ui.h:27
void detailPanel(const char *id, const Player *player, const StatsConfig &statsConfig, const Player *comparison=nullptr, float height=0.0f)
Compact player card used beside tables (squad, lineup, subs).
Definition player_ui.h:36
std::string statLabel(std::string_view statName)
Localised name of a player attribute (falls back to the raw name).
Definition player_view.cpp:131
constexpr float XS
Definition theme.h:33
constexpr float S
Definition theme.h:34
const Palette & palette()
Returns the active palette (accent included).
Definition theme.cpp:374
void ratingChip(double rating)
Rounded rating chip, coloured by the rating scale.
Definition widgets.cpp:311
void sectionLabel(const char *text)
Small uppercase section label.
Definition widgets.cpp:121
void beginCard(const char *id, const char *title, ImVec2 size, bool scroll)
Starts a bordered card. Always pair with endCard().
Definition widgets.cpp:152
void beginAutoHeightCard(const char *id, const char *title, float width)
Starts a card that grows to fit its content (never scrolls).
Definition widgets.cpp:160
void endCard()
Ends a card started with beginCard() or beginAutoHeightCard().
Definition widgets.cpp:166
void keyValue(const char *key, const char *value, float keyWidth)
Muted key on the left, value on the right of the current row.
Definition widgets.cpp:1235
void attributeBar(const char *label, float value, float labelWidth, float maxValue, float width)
Labelled horizontal bar coloured with the rating scale.
Definition widgets.cpp:292
Contains structures to simplify JSON parsing and loading of player statistics.
Configuration mapping roles to their focus areas and listing all possible stats.
Definition stats_config.h:42
Semantic colours used by widgets.
Definition theme.h:90
ImVec4 muted
Definition theme.h:97
ImVec4 text
Definition theme.h:96
ImVec4 negative
Definition theme.h:101
ImVec4 warning
Definition theme.h:100
ImVec4 positive
Definition theme.h:99