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
|
// Copyright 2014 Intel Corporation All Rights Reserved
//
// Intel makes no representations about the suitability of this software for any purpose.
// THIS SOFTWARE IS PROVIDED ""AS IS."" INTEL SPECIFICALLY DISCLAIMS ALL WARRANTIES,
// EXPRESS OR IMPLIED, AND ALL LIABILITY, INCLUDING CONSEQUENTIAL AND OTHER INDIRECT DAMAGES,
// FOR THE USE OF THIS SOFTWARE, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PROPRIETARY
// RIGHTS, AND INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// Intel does not assume any responsibility for any errors which may appear in this software
// nor any responsibility to update it.
#pragma once
#include "font.h"
#include <vector>
#include <string>
class GUIControl
{
protected:
int mX = 0;
int mY = 0;
int mWidth = 0;
int mHeight = 0;
std::string mTextureFile = "";
bool mVisible = true;;
public:
GUIControl() {}
virtual ~GUIControl() {}
// For now, empty string means use the font texture/shader... let's not overengineer this yet :)
std::string TextureFile() const { return mTextureFile; }
void Visible(bool visible) { mVisible = visible; }
bool Visible() const { return mVisible; }
virtual SpriteVertex* Draw(float viewportWidth, float viewportHeight, SpriteVertex* outVertex) const = 0;
bool HitTest(int x, int y) const
{
return mVisible && (x >= mX && x < (mX + mWidth) && y > mY && y < (mY + mHeight));
}
};
class GUIText : public GUIControl
{
private:
std::string mText;
const BitmapFont* mFont;
void ComputeDimensions()
{
mFont->GetDimensions(mText.c_str(), &mWidth, &mHeight);
}
public:
// Font lifetime managed by caller
GUIText(int x, int y, const BitmapFont* font, const std::string& text)
: mFont(font), mText(text)
{
mX = x;
mY = y;
ComputeDimensions();
}
void Text(const std::string& text)
{
mText = text;
ComputeDimensions();
}
virtual SpriteVertex* Draw(float viewportWidth, float viewportHeight, SpriteVertex* outVertex) const override
{
return mFont->DrawString(mText.c_str(), float(mX), float(mY), viewportWidth, viewportHeight, outVertex);
}
};
class GUISprite : public GUIControl
{
private:
std::string mSpriteFile;
public:
GUISprite(int x, int y, int width, int height, const std::string& spriteFile)
: mSpriteFile(spriteFile)
{
mX = x;
mY = y;
mWidth = width;
mHeight = height;
mTextureFile = spriteFile;
}
virtual SpriteVertex* Draw(float viewportWidth, float viewportHeight, SpriteVertex* outVertex) const override
{
return DrawSprite(float(mX), float(mY), float(mWidth), float(mHeight), viewportWidth, viewportHeight, outVertex);
}
};
class GUI
{
private:
std::vector<GUIControl*> mControls;
IntelClearBold mFont; // Single font for the entire GUI works for now
public:
GUI()
{
}
~GUI()
{
for (auto i : mControls) {
delete i;
}
}
const BitmapFont* Font() const { return &mFont; }
GUIText* AddText(int x, int y, const std::string& text = "")
{
auto control = new GUIText(x, y, &mFont, text);
mControls.push_back(control);
return control;
}
GUISprite* AddSprite(int x, int y, int width, int height, const std::string& spriteFile)
{
auto control = new GUISprite(x, y, width, height, spriteFile);
mControls.push_back(control);
return control;
}
// NOTE: Caller needs to preadjust x/y for any stretching/scaling going on
GUIControl* HitTest(int x, int y) const
{
for (auto i : mControls) {
if (i->HitTest(x, y)) return i;
}
return nullptr;
}
GUIControl* operator[](size_t i) { return mControls[i]; }
size_t size() const { return mControls.size(); }
};
|