Loading...
Searching...
No Matches
Avatar.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith.
2// Malena Framework — Licensed under PolyForm Noncommercial 1.0.0; commercial use requires a paid license. See LICENSE.
3
4//
5// Avatar — a compact identity chip: a square colour tile with the person's
6// INITIALS centred on it, the tile colour derived from a hash of the name (so a
7// given name is always the same colour) or an explicit seed, plus an optional
8// presence dot in the corner. Reusable anywhere a roster/list shows people.
9//
10#ifndef MALENA_AVATAR_H
11#define MALENA_AVATAR_H
12
16#include <cctype>
17#include <cstddef>
18#include <string>
19#include <vector>
20
21namespace ml {
22
23class Avatar : public Component<>
24{
25 Rectangle _chip;
26 Text _label;
27 Rectangle _dot; // presence indicator (hidden unless set)
28 sf::Vector2f _pos;
29 float _size = 26.f;
30 bool _showDot = false;
31 bool _seedSet = false;
32 std::size_t _seed = 0;
33 std::string _name;
34 std::vector<sf::Color> _palette{ {124, 92, 255}, {58, 160, 224}, {224, 138, 58} };
35 sf::Color _dotColor{ 70, 200, 120 };
36
37 static std::size_t hash(const std::string& s)
38 { std::size_t h = 0; for (char c : s) h = h * 31u + (unsigned char)c; return h; }
39
40 void applyColor()
41 {
42 if (_palette.empty()) return;
43 const std::size_t k = (_seedSet ? _seed : hash(_name)) % _palette.size();
44 _chip.setFillColor(_palette[k]);
45 }
46
47 void relayout()
48 {
49 _chip.setPosition(_pos);
50 _chip.setSize({ _size, _size });
51 auto b = _label.getLocalBounds();
52 _label.setPosition({ _pos.x + _size * 0.5f - (b.position.x + b.size.x) * 0.5f,
53 _pos.y + _size * 0.5f - _size * 0.30f });
54 const float d = std::max(6.f, _size * 0.28f);
55 _dot.setSize({ d, d });
56 _dot.setPosition({ _pos.x + _size - d, _pos.y + _size - d });
57 }
58
59public:
61 {
62 _label.setFillColor(sf::Color(235, 238, 245));
63 _label.setCharacterSize(11);
64 _dot.setFillColor(_dotColor);
65 _dot.setVisible(false);
66 addComponent(_chip);
67 addComponent(_label);
68 addComponent(_dot); // last → draws over the chip corner
69 applyColor();
70 relayout();
71 }
72
74 static std::string initialsOf(const std::string& name)
75 {
76 std::string out; bool atStart = true;
77 for (char c : name)
78 {
79 if (c == ' ') { atStart = true; continue; }
80 if (atStart && out.size() < 2) { out += (char)std::toupper((unsigned char)c); atStart = false; }
81 }
82 if (out.empty() && !name.empty()) out += (char)std::toupper((unsigned char)name[0]);
83 return out;
84 }
85
86 void setName(const std::string& name)
87 {
88 _name = name;
89 _label.setString(initialsOf(name));
90 applyColor();
91 relayout();
92 }
93
94 void setColorSeed(std::size_t seed) { _seed = seed; _seedSet = true; applyColor(); }
95 void setPalette(std::vector<sf::Color> palette) { _palette = std::move(palette); applyColor(); }
96 void setAvatarSize(float px) { _size = px; _label.setCharacterSize((unsigned)(px * 0.42f)); relayout(); }
97 void setInitialsColor(const sf::Color& c) { _label.setFillColor(c); }
99 void setPresence(bool present) { _showDot = present; _dot.setVisible(present); }
100 void setPresenceColor(const sf::Color& c) { _dotColor = c; _dot.setFillColor(c); }
101
102 void setPosition(const sf::Vector2f& position) override { _pos = position; relayout(); }
103 sf::Vector2f getPosition() const override { return _pos; }
104 sf::FloatRect getGlobalBounds() const override { return sf::FloatRect{ _pos, { _size, _size } }; }
105};
106
107} // namespace ml
108
109#endif // MALENA_AVATAR_H
sf::FloatRect getGlobalBounds() const override
Definition Avatar.h:104
void setPresenceColor(const sf::Color &c)
Definition Avatar.h:100
void setPalette(std::vector< sf::Color > palette)
Definition Avatar.h:95
void setPresence(bool present)
Show/hide the corner presence dot.
Definition Avatar.h:99
void setColorSeed(std::size_t seed)
Force the tile colour by seed instead of the name hash.
Definition Avatar.h:94
void setPosition(const sf::Vector2f &position) override
Definition Avatar.h:102
sf::Vector2f getPosition() const override
Definition Avatar.h:103
void setInitialsColor(const sf::Color &c)
Definition Avatar.h:97
void setAvatarSize(float px)
Definition Avatar.h:96
void setName(const std::string &name)
Definition Avatar.h:86
static std::string initialsOf(const std::string &name)
Two-letter initials from a display name (first + last word, else first char).
Definition Avatar.h:74
Primary base class for all user-facing Malena components.
Definition Component.h:299
A framework-integrated rectangle with optional rounded corners.
Definition Rectangle.h:48
A framework-integrated text object with word-wrap support.
Definition Text.h:57
Definition Animate.h:25
Rect< float > FloatRect
Vector2< float > Vector2f