Zappy GUI
C++ graphic client: renderer, camera, network
Loading...
Searching...
No Matches
PlayerPanel.cpp
Go to the documentation of this file.
1#include "PlayerPanel.hpp"
2
3#include <algorithm>
4
5#include "I18n.hpp"
6
7void PlayerPanel::setWorld(const WorldState* world) { _world = world; }
8
10{
11 if (IsKeyPressed(KEY_TAB)) _isOpen = !_isOpen;
12
13 if (!_isOpen || !_world) return false;
14
20
21 std::vector<const Player*> players;
22 for (const auto& [id, player] : _world->players) players.push_back(&player);
23
24 std::sort(players.begin(), players.end(), [](const Player* a, const Player* b) {
25 if (a->team != b->team) return a->team < b->team;
26 return a->id < b->id;
27 });
28
29 _background.setExtraBottomPadding(players.size() * ROW_HEIGHT + 10);
30
31 Rectangle bgBounds = _background.getLastBounds();
32
33 float startY = bgBounds.y + 40.0f;
34
35 _playerButtons.clear();
36 for (const Player* p : players) {
37 Color tColor = _colorFunc ? _colorFunc(p->team) : WHITE;
38 std::string txt = "P" + std::to_string(p->id) + " (" + I18n::get(I18n::Key::PANEL_LVL) +
39 " " + std::to_string(p->level) + ") - " + p->team;
40
41 ButtonWidget btn;
42 btn.setLabel(txt)
43 .setPosition(bgBounds.x + 10, startY)
44 .setSize(bgBounds.width > 20 ? bgBounds.width - 20 : PANEL_WIDTH - 20, ROW_HEIGHT)
45 .setNormalColor({0, 0, 0, 0})
46 .setHoverColor({100, 120, 160, 200})
47 .setTextColor(tColor)
49 .setRoundness(0.2f);
50
51 int pId = p->id;
52 btn.setOnClick([this, pId]() {
55 });
56
57 _playerButtons.push_back(btn);
58 startY += ROW_HEIGHT;
59 }
60
61 bool consumed = false;
62 for (auto& btn : _playerButtons) {
63 if (btn.handleInput()) consumed = true;
64 }
65
66 if (CheckCollisionPointRec(GetMousePosition(), bgBounds)) consumed = true;
67
68 return consumed;
69}
70
71std::optional<SelectionFinder::Selection> PlayerPanel::getPendingSelection()
72{
73 auto val = _pendingSelection;
74 _pendingSelection = std::nullopt;
75 return val;
76}
77
78void PlayerPanel::draw(int scaledFontSize) const
79{
80 if (!_isOpen || !_world) return;
81
82 _background.draw(scaledFontSize);
83
84 for (const auto& btn : _playerButtons) btn.draw(scaledFontSize);
85}
Widget displaying players and allowing selection.
A configurable, stateful clickable button.
ButtonWidget & setOnClick(std::function< void()> callback)
Registers a callback invoked when the button is clicked.
ButtonWidget & setRoundness(float roundness)
ButtonWidget & setSize(float width, float height)
ButtonWidget & setPosition(float x, float y)
Sets the button position directly (requires Anchor::None).
ButtonWidget & setBorderThickness(float thickness)
ButtonWidget & setLabel(const std::string &label)
static const char * get(Key key)
Definition I18n.hpp:86
@ PANEL_PLAYER_LIST
std::optional< SelectionFinder::Selection > _pendingSelection
void draw(int scaledFontSize) const override
Draws the panel and player list.
const WorldState * _world
bool handleInput() override
Processes mouse clicks on player rows.
static constexpr int ROW_HEIGHT
std::function< Color(const std::string &)> _colorFunc
void setWorld(const WorldState *world)
Sets the world state pointer.
std::optional< SelectionFinder::Selection > getPendingSelection()
Gets and clears any pending selection made by the user.
static constexpr int PANEL_WIDTH
TooltipWidget _background
std::vector< ButtonWidget > _playerButtons
TooltipWidget & addLine(const std::string &text, Color color=WHITE)
Adds a single-color text line.
TooltipWidget & setExtraBottomPadding(float px)
Adds extra blank space at the bottom of the box (e.g. to reserve room for a button).
TooltipWidget & clearLines()
Removes all lines.
TooltipWidget & setTextAlign(TextAlign align)
TooltipWidget & setAnchor(Anchor anchor, float margin=10.0f)
Auto-positioning relative to screen edges.
TooltipWidget & setMinWidth(float minWidth)
Override minimum panel width (0 = fit to content).
void draw(int scaledFontSize) const override
Draws the widget.
Rectangle getLastBounds() const
Returns the bounding rectangle for the last drawn frame. Useful for positioning sibling widgets (e....
Represents the state of the world in the game, including the map size, tile contents,...
std::unordered_map< int, Player > players