Player12
An open source football management simulator
Loading...
Searching...
No Matches
widgets.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 <climits>
14#include <cstdint>
15#include <span>
16#include <string>
17#include <string_view>
18#include <type_traits>
19
74namespace UI
75{
76
78enum class DialogResult : uint8_t
79{
80 NONE,
81 CONFIRM,
82 CANCEL
83};
84
86enum class Outcome : uint8_t
87{
88 WIN,
89 DRAW,
90 LOSS
91};
92
95{
96 std::string_view label;
97 float value = 0.0f;
98 ImVec4 color;
99 std::string_view valueText;
100};
101
106bool drawTextFitted(ImDrawList* drawList, ImVec2 position, ImU32 color,
107 std::string_view text, float maxWidth);
108
113void textFitted(std::string_view text, float maxWidth, const ImVec4& color);
114
116void pageHeader(const char* title, const char* subtitle = nullptr);
117
119void sectionLabel(const char* text);
120
128void beginCard(const char* id, const char* title, ImVec2 size,
129 bool scroll = false);
130
135void beginAutoHeightCard(const char* id, const char* title, float width = 0.0f);
136
138void endCard();
139
141void statTile(const char* id, const char* caption, const char* value,
142 const char* footnote, const ImVec4& valueColor, float width);
143
149{
150 public:
151 explicit TileRow(int count, float minimumWidth = 175.0f);
153 [[nodiscard]] float width() const { return tile_width; }
155 void next();
156
157 private:
158 int per_row = 1;
159 int index = 0;
160 float tile_width = 0.0f;
161};
162
164float statTileHeight();
165
170void attributeBar(const char* label, float value, float labelWidth,
171 float maxValue = 100.0f, float width = 0.0f);
172
174void meter(const char* label, float fraction, float labelWidth,
175 const ImVec4& color, const char* valueText);
176
178void ratingChip(double rating);
179
181void badge(const char* text, const ImVec4& color);
182
188void clubBadge(const char* code, uint32_t primary, uint32_t secondary,
189 float height);
190
192void drawClubBadge(ImDrawList* drawList, ImVec2 min, float height,
193 const char* code, uint32_t primary, uint32_t secondary);
194
196void formStrip(std::span<const Outcome> outcomes);
197
199void emptyState(const char* title, const char* body);
200
205DialogResult confirmDialog(const char* id, const char* title, const char* body,
206 const char* confirmLabel, const char* cancelLabel);
207
209void sparkline(const char* id, std::span<const float> values, ImVec2 size,
210 const ImVec4& color);
211
213void deltaBars(const char* id, std::span<const float> values, ImVec2 size);
214
216void barChart(const char* id, std::span<const BarDatum> bars, float width,
217 float labelWidth);
218
224bool beginDataTable(const char* id, int columns, ImGuiTableFlags flags,
225 float minWidth, ImVec2 size, int freezeColumns = 1);
226
231void staticHeadersRow();
232
234namespace Size
235{
236constexpr float BUTTON = 36.0f;
237constexpr float BUTTON_COMPACT = 28.0f;
238constexpr float BUTTON_PADDING_X = 16.0f;
239constexpr float BUTTON_MIN_WIDTH = 96.0f;
240} // namespace Size
241
243enum class ButtonSize : uint8_t
244{
245 REGULAR,
246 COMPACT
247};
248
250bool primaryButton(const char* label, ImVec2 size = ImVec2(0, 0),
251 ButtonSize buttonSize = ButtonSize::REGULAR);
252
254bool secondaryButton(const char* label, ImVec2 size = ImVec2(0, 0),
255 ButtonSize buttonSize = ButtonSize::REGULAR);
256
258bool dangerButton(const char* label, ImVec2 size = ImVec2(0, 0),
259 ButtonSize buttonSize = ButtonSize::REGULAR);
260
263
266{
267 const char* label;
268 int64_t value;
269};
270
273{
274 int64_t minimum = 0;
275 int64_t maximum = INT64_MAX;
276 std::span<const MoneyChip> chips = {};
277 float width = 0.0f;
278};
279
287bool moneyInput(const char* id, int64_t& value,
288 const MoneyInputOptions& options = {});
289
294int64_t stepMoney(int64_t value, bool up, double fraction);
295
300bool parseMoney(std::string_view text, int64_t& value);
301
304{
305 std::string text;
306 bool error = false;
307};
308
310MoneyPreview moneyPreview(std::string_view text,
311 const MoneyInputOptions& options = {});
312
317bool toggleButton(const char* label, bool active, ImVec2 size = ImVec2(0, 0),
318 ButtonSize buttonSize = ButtonSize::REGULAR);
319
325bool segmented(const char* id, int& selected,
326 std::span<const char* const> labels, float width = 0.0f);
327
332void summaryRow(const char* label, const char* value,
333 const ImVec4* valueColor = nullptr, bool emphasis = false);
334
339void budgetImpact(const char* label, int64_t current, int64_t after,
340 const char* reasonWhenNegative = nullptr);
341
343struct Column
344{
345 const char* label;
346 float width = 0.0f;
347 int priority = 0;
348 ImGuiTableColumnFlags flags = ImGuiTableColumnFlags_None;
349 ImGuiID user_id = 0;
350};
351
353using ColumnMask = uint32_t;
354
362ColumnMask fitColumns(std::span<const Column> columns, float availableWidth,
363 float stretchMinimum = 160.0f, ColumnMask hidden = 0);
364
371ColumnMask hiddenColumns(std::string_view table,
372 std::span<const Column> columns);
373
383bool columnPicker(std::string_view table, std::span<const Column> columns,
384 ColumnMask fitted);
385
387enum class TableHeader : uint8_t
388{
389 STATIC,
390 SORTABLE,
391 NONE
392};
393
400bool beginResponsiveTable(const char* id, std::span<const Column> columns,
401 ColumnMask mask, ImGuiTableFlags flags,
403 ImVec2 size = ImVec2(0, 0));
404
406bool cell(ColumnMask mask, int index);
407
409bool link(const char* label, const char* id);
410
412void keyValue(const char* key, const char* value, float keyWidth);
413
419bool sameLineIfFits(float nextWidth, float spacing = -1.0f);
420
422float buttonWidth(const char* label,
423 ButtonSize buttonSize = ButtonSize::REGULAR);
424
426void textRight(const char* text);
427
429void textRightColored(const ImVec4& color, const char* text);
430
432template <typename T>
433int compare(const T& left, const T& right)
434{
435 if constexpr (std::is_same_v<T, bool>)
436 {
437 if (left == right) return 0;
438 return left ? 1 : -1;
439 }
440 else
441 {
442 if (left < right) return -1;
443 if (right < left) return 1;
444 return 0;
445 }
446}
447
448} // namespace UI
Lays out a row of equal tiles that wraps onto more lines when the region is too narrow for them all (...
Definition widgets.h:149
float width() const
Width for each tile.
Definition widgets.h:153
void next()
Call before each tile; keeps it on the row or wraps.
Definition widgets.cpp:182
int index
Definition widgets.h:159
float tile_width
Definition widgets.h:160
int per_row
Definition widgets.h:158
ImGuiKey key
Definition input_actions.cpp:25
const char * label
Definition input_actions.cpp:27
Definition match_tracking.h:25
constexpr float BUTTON_MIN_WIDTH
Definition widgets.h:239
constexpr float BUTTON_COMPACT
Definition widgets.h:237
constexpr float BUTTON_PADDING_X
Definition widgets.h:238
constexpr float BUTTON
Definition widgets.h:236
Small vector icons drawn with ImDrawList (no icon font needed).
Definition icons.cpp:33
float statTileHeight()
Height used by statTile(), for layout calculations.
Definition widgets.cpp:188
void deltaBars(const char *id, std::span< const float > values, ImVec2 size)
Bars above/below a zero line (positive green, negative red).
Definition widgets.cpp:542
bool beginResponsiveTable(const char *id, std::span< const Column > columns, ColumnMask mask, ImGuiTableFlags flags, TableHeader header, ImVec2 size)
BeginTable + setup of the visible columns and the header row. Pair with ImGui::EndTable() when it ret...
Definition widgets.cpp:1168
ColumnMask fitColumns(std::span< const Column > columns, float availableWidth, float stretchMinimum, ColumnMask hidden)
Chooses the columns that fit the available width, dropping the highest priority numbers first (never ...
Definition widgets.cpp:1037
bool secondaryButton(const char *label, ImVec2 size, ButtonSize buttonSize)
Neutral button for secondary actions.
Definition widgets.cpp:722
void clubBadge(const char *code, uint32_t primary, uint32_t secondary, float height)
Two-colour club shield (kit colours 0xRRGGBB) with the short code drawn inside when the shield is tal...
Definition widgets.cpp:397
bool parseMoney(std::string_view text, int64_t &value)
Parses "14.4M", "1,5M", "850k", "€1,200,000", "€ 2,25 mln" (false when invalid or ambiguous; see Numb...
Definition widgets.cpp:734
float buttonHeight(ButtonSize buttonSize)
Scaled height of a button class.
Definition widgets.cpp:709
void staticHeadersRow()
Header row of a non-sortable table: plain labels without the hover highlight that suggests a clickabl...
Definition widgets.cpp:627
Outcome
Match outcome for form strips.
Definition widgets.h:87
bool columnPicker(std::string_view table, std::span< const Column > columns, ColumnMask fitted)
"Columns" button opening a checklist of the table's columns; the choice is saved in the settings at o...
Definition widgets.cpp:1095
void drawClubBadge(ImDrawList *drawList, ImVec2 min, float height, const char *code, uint32_t primary, uint32_t secondary)
clubBadge() drawn at a position (height in pixels), no layout.
Definition widgets.cpp:350
bool moneyInput(const char *id, int64_t &value, const MoneyInputOptions &options)
Currency field showing the full amount; typing accepts plain numbers, either decimal mark and k/M/B (...
Definition widgets.cpp:758
void formStrip(std::span< const Outcome > outcomes)
Row of W/D/L markers (latest last).
Definition widgets.cpp:406
float buttonWidth(const char *label, ButtonSize buttonSize)
Width of a button with this label (at least the minimum width).
Definition widgets.cpp:1271
bool drawTextFitted(ImDrawList *drawList, ImVec2 position, ImU32 color, std::string_view text, float maxWidth)
Draws text cut to maxWidth with a trailing ellipsis when needed.
Definition widgets.cpp:46
void textRightColored(const ImVec4 &color, const char *text)
Coloured text right-aligned in the current cell.
Definition widgets.cpp:1289
bool cell(ColumnMask mask, int index)
Moves to the next cell when column index is visible.
Definition widgets.cpp:1204
DialogResult
Result of a modal confirmation dialog.
Definition widgets.h:79
void textFitted(std::string_view text, float maxWidth, const ImVec4 &color)
Text item limited to maxWidth; shows the full text as a tooltip when it had to be shortened.
Definition widgets.cpp:87
MoneyPreview moneyPreview(std::string_view text, const MoneyInputOptions &options)
What moneyInput() shows for the typed text.
Definition widgets.cpp:742
bool link(const char *label, const char *id)
Selectable text that looks like a link; returns true on click.
Definition widgets.cpp:1212
int64_t stepMoney(int64_t value, bool up, double fraction)
Next value of a money input's -/+ button: fraction up or down, rounded to three significant figures,...
Definition widgets.cpp:698
void ratingChip(double rating)
Rounded rating chip, coloured by the rating scale.
Definition widgets.cpp:311
bool toggleButton(const char *label, bool active, ImVec2 size, ButtonSize buttonSize)
Filter/tab button: neutral when off, selection fill with an accent underline when on (one segment of ...
Definition widgets.cpp:899
void sectionLabel(const char *text)
Small uppercase section label.
Definition widgets.cpp:121
void pageHeader(const char *title, const char *subtitle)
Large page title with an optional muted subtitle.
Definition widgets.cpp:105
bool beginDataTable(const char *id, int columns, ImGuiTableFlags flags, float minWidth, ImVec2 size, int freezeColumns)
Begins a data table that scrolls horizontally only when the region is narrower than minWidth,...
Definition widgets.cpp:613
TableHeader
Header row emitted by beginResponsiveTable().
Definition widgets.h:388
bool sameLineIfFits(float nextWidth, float spacing)
SameLine() only when an item of the given width still fits.
Definition widgets.cpp:1261
DialogResult confirmDialog(const char *id, const char *title, const char *body, const char *confirmLabel, const char *cancelLabel)
Modal confirmation. Open with ImGui::OpenPopup(id) beforehand.
Definition widgets.cpp:476
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 sparkline(const char *id, std::span< const float > values, ImVec2 size, const ImVec4 &color)
Line chart of a series in a fixed box.
Definition widgets.cpp:514
uint32_t ColumnMask
Visible-column mask produced by fitColumns().
Definition widgets.h:353
ButtonSize
Button height class.
Definition widgets.h:244
void meter(const char *label, float fraction, float labelWidth, const ImVec4 &color, const char *valueText)
Labelled bar with an explicit colour and value text (0-1).
Definition widgets.cpp:305
void statTile(const char *id, const char *caption, const char *value, const char *footnote, const ImVec4 &valueColor, float width)
Figure tile: caption, big value and an optional footnote.
Definition widgets.cpp:200
int compare(const T &left, const T &right)
Strictly-weak three-way comparison helper for table sorting.
Definition widgets.h:433
void barChart(const char *id, std::span< const BarDatum > bars, float width, float labelWidth)
Horizontal bars with labels and values, scaled to the maximum.
Definition widgets.cpp:571
void budgetImpact(const char *label, int64_t current, int64_t after, const char *reasonWhenNegative)
Budget before/after bar: "€20.0M -> €5.6M" (drawn arrow). The after part turns red below zero and the...
Definition widgets.cpp:974
void emptyState(const char *title, const char *body)
Muted placeholder shown when a list has nothing to display.
Definition widgets.cpp:448
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 summaryRow(const char *label, const char *value, const ImVec4 *valueColor, bool emphasis)
Label on the left, value right-aligned; emphasis draws a rule above and a larger value (totals).
Definition widgets.cpp:952
void endCard()
Ends a card started with beginCard() or beginAutoHeightCard().
Definition widgets.cpp:166
bool segmented(const char *id, int &selected, std::span< const char *const > labels, float width)
Segmented control for small enumerations (joined buttons).
Definition widgets.cpp:919
void textRight(const char *text)
Text right-aligned in the current table cell or content region.
Definition widgets.cpp:1280
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
bool primaryButton(const char *label, ImVec2 size, ButtonSize buttonSize)
Accent-filled call-to-action button.
Definition widgets.cpp:715
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
ColumnMask hiddenColumns(std::string_view table, std::span< const Column > columns)
Columns the user hid in a table, saved in the settings per table.
Definition widgets.cpp:1080
bool dangerButton(const char *label, ImVec2 size, ButtonSize buttonSize)
Red button for destructive actions (release, cancel contract).
Definition widgets.cpp:728
void badge(const char *text, const ImVec4 &color)
Small coloured badge with text.
Definition widgets.cpp:334
int index
Definition runtime_editor.cpp:94
std::string_view table
Definition runtime_editor_validation.cpp:44
One bar in a horizontal bar chart.
Definition widgets.h:95
std::string_view label
Definition widgets.h:96
float value
Definition widgets.h:97
ImVec4 color
Definition widgets.h:98
std::string_view valueText
Definition widgets.h:99
One column of a responsive table.
Definition widgets.h:344
ImGuiTableColumnFlags flags
Definition widgets.h:348
float width
Definition widgets.h:346
const char * label
Definition widgets.h:345
ImGuiID user_id
Definition widgets.h:349
int priority
Definition widgets.h:347
A quick-pick value offered under a money input.
Definition widgets.h:266
const char * label
Definition widgets.h:267
int64_t value
Definition widgets.h:268
Options of moneyInput().
Definition widgets.h:273
float width
Definition widgets.h:277
int64_t minimum
Definition widgets.h:274
std::span< const MoneyChip > chips
Definition widgets.h:276
int64_t maximum
Definition widgets.h:275
Line shown under a money input while typing.
Definition widgets.h:304
std::string text
Definition widgets.h:305
bool error
Definition widgets.h:306
std::array< const char *, 6 > labels
Definition transfer_terms_editor.cpp:77