-
Notifications
You must be signed in to change notification settings - Fork 4
/
Canvas.cpp
402 lines (348 loc) · 9.66 KB
/
Canvas.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "pch.h"
#include "Canvas.h"
#undef max
#define CHECK_CTX assert(GCanvas->ctxSet)
#define FORWARD(method, ...) CHECK_CTX; return GCanvas->canvas.method(__VA_ARGS__)
static Canvas::CanvasContext GCanvasDefaultContext;
Canvas::CanvasContext* GCanvas = &GCanvasDefaultContext;
const Canvas::Color Canvas::Color::WHITE = { 255, 255, 255 };
const Canvas::Color Canvas::Color::BLACK = { 0, 0, 0 };
const Canvas::Color Canvas::Color::RED = { 255, 0, 0 };
const Canvas::Color Canvas::Color::GREEN = { 0, 255, 0 };
const Canvas::Color Canvas::Color::BLUE = { 0, 0, 255 };
void Canvas::SetContext(CanvasWrapper canvas)
{
GCanvas->canvas = canvas;
GCanvas->ctxSet = true;
}
bool Canvas::IsContextSet()
{
return GCanvas->ctxSet;
}
float Canvas::GetCharHeight()
{
return GCanvas->canvas.GetStringSize("A", GCanvas->scale, GCanvas->scale).Y;
}
float Canvas::GetStringWidth(std::string str)
{
return GCanvas->canvas.GetStringSize(str, GCanvas->scale, GCanvas->scale).X;
}
char Canvas::GetGlobalAlpha()
{
return GCanvas->alpha;
}
void Canvas::SetGlobalAlpha(char alpha)
{
GCanvas->alpha = alpha;
}
void Canvas::SetColor(Color color)
{
SetColor(color, GCanvas->alpha);
}
void Canvas::SetColor(Color color, char alpha)
{
SetColor(color.r, color.g, color.b, alpha);
}
void Canvas::SetScale(float scale)
{
assert(scale >= 0);
GCanvas->scale = scale;
}
void Canvas::SetPosition(int x, int y)
{
SetPosition(Vector2{x, y});
}
void Canvas::SetPosition(float x, float y)
{
SetPosition(Vector2F{x, y});
}
#pragma region FORWARDS
void Canvas::SetColor(char red, char green, char blue, char alpha)
{
FORWARD(SetColor, red, green, blue, alpha);
}
void Canvas::SetPosition(Vector2 pos)
{
FORWARD(SetPosition, pos);
}
void Canvas::SetPosition(Vector2F pos)
{
FORWARD(SetPosition, pos);
}
Vector2 Canvas::GetPosition()
{
FORWARD(GetPosition);
}
Vector2F Canvas::GetPositionFloat()
{
FORWARD(GetPositionFloat);
}
void Canvas::DrawBox(Vector2 size)
{
FORWARD(DrawBox, size);
}
void Canvas::DrawBox(Vector2F size)
{
FORWARD(DrawBox, size);
}
void Canvas::FillBox(Vector2 size)
{
FORWARD(FillBox, size);
}
void Canvas::FillBox(Vector2F size)
{
FORWARD(FillBox, size);
}
void Canvas::FillTriangle(Vector2 p1, Vector2 p2, Vector2 p3, LinearColor color)
{
FORWARD(FillTriangle, p1, p2, p3, color);
}
void Canvas::FillTriangle(Vector2F p1, Vector2F p2, Vector2F p3, LinearColor color)
{
FORWARD(FillTriangle, p1, p2, p3, color);
}
void Canvas::DrawString(std::string text)
{
FORWARD(DrawString, text, GCanvas->scale, GCanvas->scale);
}
void Canvas::DrawString(std::string text, float xScale, float yScale)
{
FORWARD(DrawString, text, xScale, yScale);
}
void Canvas::DrawLine(Vector2 start, Vector2 end)
{
FORWARD(DrawLine, start, end);
}
void Canvas::DrawLine(Vector2 start, Vector2 end, float width)
{
FORWARD(DrawLine, start, end, width);
}
void Canvas::DrawLine(Vector2F start, Vector2F end)
{
FORWARD(DrawLine, start, end);
}
void Canvas::DrawLine(Vector2F start, Vector2F end, float width)
{
FORWARD(DrawLine, start, end, width);
}
void Canvas::DrawRect(Vector2 start, Vector2 end)
{
FORWARD(DrawRect, start, end);
}
void Canvas::DrawRect(Vector2F start, Vector2F end)
{
FORWARD(DrawRect, start, end);
}
Vector2F Canvas::ProjectF(Vector location)
{
FORWARD(ProjectF, location);
}
Vector2 Canvas::Project(Vector location)
{
FORWARD(Project, location);
}
Vector2 Canvas::GetSize()
{
FORWARD(GetSize);
}
#pragma endregion FORWARDS
// Helpers
void Canvas::DrawRect(Vector2 size)
{
auto pos = GetPosition();
DrawRect(pos, pos + size);
}
void Canvas::BeginTable(CanvasTableOptions tableOptions)
{
CHECK_CTX;
GCanvas->tableContext.tableOptions = tableOptions;
GCanvas->tableContext.rows.clear();
}
void Canvas::Columns(std::vector<CanvasColumnOptions> columns)
{
assert(columns.size() >= 1);
GCanvas->tableContext.totalCols = (int)columns.size();
GCanvas->tableContext.columnOptions = columns;
}
void Canvas::Row(const std::vector<std::string>& rowData)
{
CHECK_CTX;
GCanvas->tableContext.rows.push_back(rowData);
}
void CalculateAutoWidths(std::vector<float>& colSizes, int startIdx, int numCols, float remainingWidth)
{
float distWidth = remainingWidth / numCols;
for (int i = startIdx; i < colSizes.size(); i++)
{
const auto& colOpt = GCanvas->tableContext.columnOptions[i];
if (colOpt.width == 0)
colSizes[i] = distWidth;
}
}
void Canvas::EndTable()
{
CHECK_CTX;
assert(GCanvas->tableContext.totalCols >= 1);
assert(GCanvas->tableContext.rows.size() >= 1);
Vector2F pos = GetPositionFloat();
std::vector<float> colSizes(GCanvas->tableContext.totalCols, 0);
// Fixed width set
if (GCanvas->tableContext.tableOptions.width > 0)
{
for (auto& row : GCanvas->tableContext.rows)
{
// Fill empty columns
while (row.size() < GCanvas->tableContext.totalCols)
{
row.push_back("");
}
}
int colNum = 0;
int numFixedCols = 0;
float totalSetWidths = 0;
for (const auto& colOpt : GCanvas->tableContext.columnOptions)
{
if (colOpt.width > 0)
{
totalSetWidths += colSizes[colNum] = colOpt.width;
numFixedCols++;
}
colNum++;
}
float remainingWidth = std::max(GCanvas->tableContext.tableOptions.width - totalSetWidths, 0.0f);
float newColWidth = remainingWidth / (GCanvas->tableContext.totalCols - numFixedCols);
int numAutoCols = GCanvas->tableContext.totalCols - numFixedCols;
CalculateAutoWidths(colSizes, 0, numAutoCols, remainingWidth);
int i = 0;
for (const auto& colOpt : GCanvas->tableContext.columnOptions)
{
if (colOpt.maxWidth.has_value() && colOpt.maxWidth.value() < colSizes[i])
{
colSizes[i] = colOpt.maxWidth.value();
remainingWidth -= colSizes[i];
CalculateAutoWidths(colSizes, i + 1, --numAutoCols, remainingWidth);
}
i++;
}
}
// Expand to fit columns
else
{
for (auto& row : GCanvas->tableContext.rows)
{
// Fill empty columns
while (row.size() < GCanvas->tableContext.totalCols)
{
row.push_back("");
}
int colNum = 0;
for (const auto& col : row)
{
colSizes[colNum] = std::max({ GetStringWidth(col) + GCanvas->tableContext.tableOptions.padding * 2, colSizes[colNum], 5.0f });
colNum++;
}
}
int colNum = 0;
for (auto& colOpt : GCanvas->tableContext.columnOptions)
{
if (colOpt.width > 0)
colSizes[colNum] = colOpt.width;
colNum++;
}
}
float totalWidth = std::accumulate(colSizes.begin(), colSizes.end(), 0.0f);
float maxX = pos.X + totalWidth;
float maxY = pos.Y + GetTableRowHeight() * (int)GCanvas->tableContext.rows.size();
// Draw background
if (GCanvas->tableContext.tableOptions.background)
{
SetColor(GCanvas->tableContext.tableOptions.backgroundColor);
DrawRect(Vector2F{ pos.X, pos.Y }, { maxX, maxY + (GCanvas->tableContext.tableOptions.borders ? 0.0f : GCanvas->tableContext.verticalPadding * 2) });
}
if (GCanvas->tableContext.tableOptions.borders)
{
SetColor(GCanvas->tableContext.tableOptions.borderColor);
// For some reason vertical lines draw x-1 and horizontal lines draw y-1
DrawLine(Vector2F{ pos.X + 1, pos.Y }, { pos.X + 1, maxY });
DrawLine(Vector2F{ pos.X, pos.Y + 0 }, { maxX, pos.Y + 0 });
}
// Draw data
float xPos = pos.X;
float yPos = pos.Y + 1 + (GCanvas->tableContext.tableOptions.borders ? 0 : 5);
float ellipsisWidth = GetStringWidth("...");
for (const auto& row : GCanvas->tableContext.rows)
{
// Reset back to start
xPos = pos.X;
int i = 0;
for (auto col : row)
{
if (i >= GCanvas->tableContext.totalCols)
break;
float colSize = colSizes[i];
auto& options = GCanvas->tableContext.columnOptions[i];
float strWidth = GetStringWidth(col);
// truncate if necessary
float remainingPixels = (float)(colSize - GCanvas->tableContext.tableOptions.padding * 2);
// not enough room for the string
if (strWidth >= remainingPixels)
{
int characters = 0;
float pixels = 0;
// remove space for the ellipsis for calculation
remainingPixels -= ellipsisWidth;
for (char ch : col)
{
float width = GetStringWidth({ch});
//width = 8;
if ((pixels + width) > remainingPixels)
break;
pixels += width;
characters++;
}
col = col.substr(0, characters) + "...";
strWidth = GetStringWidth(col);
}
switch (options.alignment) {
case Alignment::LEFT:
SetPosition(Vector2F{ xPos + GCanvas->tableContext.tableOptions.padding, yPos });
break;
case Alignment::CENTER: {
float offset = (colSize - strWidth) / 2;
SetPosition(Vector2F{ xPos + offset, yPos });
break;
}
case Alignment::RIGHT: {
float offset = colSize - GCanvas->tableContext.tableOptions.padding - strWidth;
SetPosition(Vector2F{ xPos + offset, yPos });
break;
}
}
SetColor(GCanvas->tableContext.tableOptions.textColor);
DrawString(col, GCanvas->scale, GCanvas->scale);
xPos += colSize;
// TODO: make this drawn once per column from top to bottom
if (GCanvas->tableContext.tableOptions.borders)
{
SetColor(GCanvas->tableContext.tableOptions.borderColor);
// We don't x+1 here because we want it to overlap the edge of the bg
// Looks like first row line start too high
DrawLine(Vector2F{ xPos, yPos - 1 }, { xPos, yPos - 1 + GetTableRowHeight() - 1 });
}
i++;
}
yPos += GetTableRowHeight();
if (GCanvas->tableContext.tableOptions.borders)
{
SetColor(GCanvas->tableContext.tableOptions.borderColor);
// y-1 here because we went up 1 at the start for the text padding
DrawLine(Vector2F{ pos.X, yPos - 1 }, { maxX, yPos - 1 });
}
}
// Set position to after the table to make next UI line up
SetPosition(pos.X, yPos - 1 + (GCanvas->tableContext.tableOptions.borders ? 0 : 2));
}
float Canvas::GetTableRowHeight()
{
return GetCharHeight() + GCanvas->tableContext.verticalPadding;
}