Zappy GUI
C++ graphic client: renderer, camera, network
Loading...
Searching...
No Matches
TooltipRenderer.cpp
Go to the documentation of this file.
1#include "TooltipRenderer.hpp"
2
3#include <stdexcept>
4
5#include "TextRenderer.hpp"
6
8
10 : _bgColor(BLACK),
11 _bgAlpha(200),
12 _borderColor(WHITE),
13 _borderThickness(2),
14 _fontSize(20),
15 _padding(10),
16 _anchor(Anchor::TopLeft)
17{
18}
19
20TooltipRenderer::Builder& TooltipRenderer::Builder::addLine(const std::string& text, Color color)
21{
22 _lines.push_back({{text, color}});
23 return *this;
24}
25
27 const std::vector<std::string>& segments, const std::vector<Color>& colors)
28{
29 if (segments.size() != colors.size()) {
30 throw std::invalid_argument("Segments and colors size mismatch");
31 }
32
33 std::vector<ColoredSegment> line;
34 for (size_t i = 0; i < segments.size(); ++i) {
35 line.push_back({segments[i], colors[i]});
36 }
37 _lines.push_back(line);
38 return *this;
39}
40
42{
43 _bgColor = color;
44 return *this;
45}
46
48{
49 _bgAlpha = alpha;
50 return *this;
51}
52
54{
55 _borderColor = color;
56 return *this;
57}
58
60{
61 _borderThickness = thickness;
62 return *this;
63}
64
66{
67 _fontSize = size;
68 return *this;
69}
70
72{
73 _padding = padding;
74 return *this;
75}
76
78{
79 _anchor = anchor;
80 return *this;
81}
82
83void TooltipRenderer::Builder::draw(Vector2 position)
84{
85 if (_lines.empty()) return;
86
87 int lineSpacing = _fontSize / 4; // 25% of font size spacing between lines
88 int boxWidth = 0;
89 int boxHeight = 0;
90
91 for (const auto& line : _lines) {
92 int lineWidth = 0;
93 for (const auto& segment : line) {
94 lineWidth += TextRenderer::measure(segment.text, _fontSize);
95 }
96 boxWidth = std::max(boxWidth, lineWidth);
97 boxHeight += _fontSize + lineSpacing;
98 }
99 boxHeight -= lineSpacing; // Remove spacing after last line
100
101 boxWidth += _padding * 2;
102 boxHeight += _padding * 2;
103
104 Vector2 anchoredPos = _calculateAnchoredPosition(position, boxWidth, boxHeight);
105 Rectangle rect = {anchoredPos.x, anchoredPos.y, static_cast<float>(boxWidth),
106 static_cast<float>(boxHeight)};
107
108 // Draw background
109 Color bgColorWithAlpha = {_bgColor.r, _bgColor.g, _bgColor.b, _bgAlpha};
110 DrawRectangleRounded(rect, 0.2f, 8, bgColorWithAlpha);
111
112 // Draw border
113 if (_borderThickness > 0) {
114 DrawRectangleRoundedLines(rect, 0.2f, 8, _borderThickness, _borderColor);
115 }
116
117 // Draw text
118 float textY = anchoredPos.y + _padding;
119 for (const auto& line : _lines) {
120 float textX = anchoredPos.x + _padding;
121 for (const auto& segment : line) {
122 TextRenderer::draw(segment.text, static_cast<int>(textX), static_cast<int>(textY),
123 _fontSize, segment.color);
124 textX += TextRenderer::measure(segment.text, _fontSize);
125 }
126 textY += _fontSize + lineSpacing;
127 }
128}
129
130Vector2 TooltipRenderer::Builder::_calculateAnchoredPosition(Vector2 position, int boxWidth,
131 int boxHeight) const
132{
133 Vector2 anchoredPos = position;
134
135 switch (_anchor) {
136 case Anchor::TopLeft:
137 break;
139 anchoredPos.x -= boxWidth / 2.0f;
140 break;
141 case Anchor::TopRight:
142 anchoredPos.x -= boxWidth;
143 break;
145 anchoredPos.y -= boxHeight / 2.0f;
146 break;
147 case Anchor::Center:
148 anchoredPos.x -= boxWidth / 2.0f;
149 anchoredPos.y -= boxHeight / 2.0f;
150 break;
152 anchoredPos.x -= boxWidth;
153 anchoredPos.y -= boxHeight / 2.0f;
154 break;
156 anchoredPos.y -= boxHeight;
157 break;
159 anchoredPos.x -= boxWidth / 2.0f;
160 anchoredPos.y -= boxHeight;
161 break;
163 anchoredPos.x -= boxWidth;
164 anchoredPos.y -= boxHeight;
165 break;
166 }
167
168 return anchoredPos;
169}
static void draw(const std::string &text, int x, int y, int fontSize, Color color)
static int measure(const std::string &text, int fontSize)
Builder & setFontSize(int size)
Sets the font size for all text.
Builder & addColoredText(const std::vector< std::string > &segments, const std::vector< Color > &colors)
Adds a line with multiple colored segments.
Builder & setPadding(int padding)
Sets the internal padding.
Builder & setBorderColor(Color color)
Sets the border color.
Builder & addLine(const std::string &text, Color color=WHITE)
Adds a single-color line to the tooltip.
Vector2 _calculateAnchoredPosition(Vector2 position, int boxWidth, int boxHeight) const
void draw(Vector2 position)
Draws the tooltip at the given position.
Builder & setBackgroundAlpha(unsigned char alpha)
Sets the background alpha transparency.
Builder & setBackgroundColor(Color color)
Sets the background color (ignoring alpha).
Builder & setBorderThickness(int thickness)
Sets the border thickness.
Builder & setAnchor(Anchor anchor)
Sets the anchor point for positioning.
static Builder create()
Creates a new tooltip builder.