forked from floft/sprouts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathguilabel.h
48 lines (43 loc) · 1.4 KB
/
guilabel.h
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
#ifndef GUILABEL_H_INCLUDED
#define GUILABEL_H_INCLUDED
#include "guielement.h"
#include "platform.h"
#include "text.h"
#include <functional>
#include <stdexcept>
/**************
*Description:
*Input:
*Output:
**************/
using namespace std;
struct GUILabel : public GUIElement
{
wstring text;
Color textColor;
GUILabel(wstring text, float minX, float maxX, float minY, float maxY, Color textColor = Color::V(0))
: GUIElement(minX, maxX, minY, maxY), text(text), textColor(textColor)
{
}
virtual bool canHaveKeyboardFocus() const override final
{
return false;
}
protected:
virtual Mesh render(float minZ, float maxZ, bool hasFocus) override
{
float textWidth = Text::width(text);
float textHeight = Text::height(text);
if(textWidth == 0)
textWidth = 1;
if(textHeight == 0)
textHeight = 1;
float textScale = 0.5 * (maxY - minY) / textHeight;
textScale = min(textScale, (maxX - minX) / textWidth);
float xOffset = -0.5 * textWidth, yOffset = -0.5 * textHeight;
xOffset = textScale * xOffset + 0.5 * (minX + maxX);
yOffset = textScale * yOffset + 0.5 * (minY + maxY);
return (Mesh)transform(Matrix::scale(textScale).concat(Matrix::translate(xOffset, yOffset, -1).concat(Matrix::scale(minZ))), Text::mesh(text, textColor));
}
};
#endif // GUILABEL_H_INCLUDED