/
GUI.cpp
259 lines (234 loc) · 9.81 KB
/
GUI.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
#include <GL\glew.h>
#include "GUI.h"
#include <SDL2/SDL_timer.h>
namespace Adina{
CEGUI::OpenGL3Renderer* GUI::m_renderer = nullptr;
void GUI::init(const std::string& resourceDirectory) {
// Check if the renderer and system were not already initialized
if (m_renderer == nullptr) {
m_renderer = &CEGUI::OpenGL3Renderer::bootstrapSystem();
CEGUI::DefaultResourceProvider* rp = static_cast<CEGUI::DefaultResourceProvider*>(CEGUI::System::getSingleton().getResourceProvider());
rp->setResourceGroupDirectory("imagesets", resourceDirectory + "/imagesets/");
rp->setResourceGroupDirectory("schemes", resourceDirectory + "/schemes/");
rp->setResourceGroupDirectory("fonts", resourceDirectory + "/fonts/");
rp->setResourceGroupDirectory("layouts", resourceDirectory + "/layouts/");
rp->setResourceGroupDirectory("looknfeels", resourceDirectory + "/looknfeel/");
rp->setResourceGroupDirectory("lua_scripts", resourceDirectory + "/lua_scripts/");
CEGUI::ImageManager::setImagesetDefaultResourceGroup("imagesets");
CEGUI::Scheme::setDefaultResourceGroup("schemes");
CEGUI::Font::setDefaultResourceGroup("fonts");
CEGUI::WidgetLookManager::setDefaultResourceGroup("looknfeels");
CEGUI::WindowManager::setDefaultResourceGroup("layouts");
CEGUI::ScriptModule::setDefaultResourceGroup("lua_scripts");
}
m_context = &CEGUI::System::getSingleton().createGUIContext(m_renderer->getDefaultRenderTarget());
m_root = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "root");
m_context->setRootWindow(m_root);
}
void GUI::destroy() {
CEGUI::System::getSingleton().destroyGUIContext(*m_context);
CEGUI::WindowManager::getSingleton().destroyWindow(m_root);
m_context = nullptr;
m_root = nullptr;
}
void GUI::draw() {
glDisable(GL_DEPTH_TEST);
m_renderer->beginRendering();
m_context->draw();
m_renderer->endRendering();
// Clean up after CEGUI
glBindVertexArray(0);
glDisable(GL_SCISSOR_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
void GUI::update() {
unsigned int elapsed;
if (m_lastTime == 0) {
elapsed = 0;
m_lastTime = SDL_GetTicks();
}
else {
unsigned int nextTime = SDL_GetTicks();
elapsed = nextTime - m_lastTime;
m_lastTime = nextTime;
}
m_context->injectTimePulse((float)elapsed / 1000.0f);
}
void GUI::setMouseCursor(const std::string& imageFile) {
m_context->getMouseCursor().setDefaultImage(imageFile);
}
void GUI::showMouseCursor() {
m_context->getMouseCursor().show();
}
void GUI::hideMouseCursor() {
m_context->getMouseCursor().hide();
}
CEGUI::Key::Scan SDLKeyToCEGUIKey(SDL_Keycode key) {
using namespace CEGUI;
switch (key) {
case SDLK_BACKSPACE: return Key::Backspace;
case SDLK_TAB: return Key::Tab;
case SDLK_RETURN: return Key::Return;
case SDLK_PAUSE: return Key::Pause;
case SDLK_ESCAPE: return Key::Escape;
case SDLK_SPACE: return Key::Space;
case SDLK_COMMA: return Key::Comma;
case SDLK_MINUS: return Key::Minus;
case SDLK_PERIOD: return Key::Period;
case SDLK_SLASH: return Key::Slash;
case SDLK_0: return Key::Zero;
case SDLK_1: return Key::One;
case SDLK_2: return Key::Two;
case SDLK_3: return Key::Three;
case SDLK_4: return Key::Four;
case SDLK_5: return Key::Five;
case SDLK_6: return Key::Six;
case SDLK_7: return Key::Seven;
case SDLK_8: return Key::Eight;
case SDLK_9: return Key::Nine;
case SDLK_COLON: return Key::Colon;
case SDLK_SEMICOLON: return Key::Semicolon;
case SDLK_EQUALS: return Key::Equals;
case SDLK_LEFTBRACKET: return Key::LeftBracket;
case SDLK_BACKSLASH: return Key::Backslash;
case SDLK_RIGHTBRACKET: return Key::RightBracket;
case SDLK_a: return Key::A;
case SDLK_b: return Key::B;
case SDLK_c: return Key::C;
case SDLK_d: return Key::D;
case SDLK_e: return Key::E;
case SDLK_f: return Key::F;
case SDLK_g: return Key::G;
case SDLK_h: return Key::H;
case SDLK_i: return Key::I;
case SDLK_j: return Key::J;
case SDLK_k: return Key::K;
case SDLK_l: return Key::L;
case SDLK_m: return Key::M;
case SDLK_n: return Key::N;
case SDLK_o: return Key::O;
case SDLK_p: return Key::P;
case SDLK_q: return Key::Q;
case SDLK_r: return Key::R;
case SDLK_s: return Key::S;
case SDLK_t: return Key::T;
case SDLK_u: return Key::U;
case SDLK_v: return Key::V;
case SDLK_w: return Key::W;
case SDLK_x: return Key::X;
case SDLK_y: return Key::Y;
case SDLK_z: return Key::Z;
case SDLK_DELETE: return Key::Delete;
case SDLK_KP_PERIOD: return Key::Decimal;
case SDLK_KP_DIVIDE: return Key::Divide;
case SDLK_KP_MULTIPLY: return Key::Multiply;
case SDLK_KP_MINUS: return Key::Subtract;
case SDLK_KP_PLUS: return Key::Add;
case SDLK_KP_ENTER: return Key::NumpadEnter;
case SDLK_KP_EQUALS: return Key::NumpadEquals;
case SDLK_UP: return Key::ArrowUp;
case SDLK_DOWN: return Key::ArrowDown;
case SDLK_RIGHT: return Key::ArrowRight;
case SDLK_LEFT: return Key::ArrowLeft;
case SDLK_INSERT: return Key::Insert;
case SDLK_HOME: return Key::Home;
case SDLK_END: return Key::End;
case SDLK_PAGEUP: return Key::PageUp;
case SDLK_PAGEDOWN: return Key::PageDown;
case SDLK_F1: return Key::F1;
case SDLK_F2: return Key::F2;
case SDLK_F3: return Key::F3;
case SDLK_F4: return Key::F4;
case SDLK_F5: return Key::F5;
case SDLK_F6: return Key::F6;
case SDLK_F7: return Key::F7;
case SDLK_F8: return Key::F8;
case SDLK_F9: return Key::F9;
case SDLK_F10: return Key::F10;
case SDLK_F11: return Key::F11;
case SDLK_F12: return Key::F12;
case SDLK_F13: return Key::F13;
case SDLK_F14: return Key::F14;
case SDLK_F15: return Key::F15;
case SDLK_RSHIFT: return Key::RightShift;
case SDLK_LSHIFT: return Key::LeftShift;
case SDLK_RCTRL: return Key::RightControl;
case SDLK_LCTRL: return Key::LeftControl;
case SDLK_RALT: return Key::RightAlt;
case SDLK_LALT: return Key::LeftAlt;
case SDLK_SYSREQ: return Key::SysRq;
case SDLK_MENU: return Key::AppMenu;
case SDLK_POWER: return Key::Power;
default: return Key::Unknown;
}
}
CEGUI::MouseButton SDLButtonToCEGUIButton(Uint8 sdlButton) {
switch (sdlButton) {
case SDL_BUTTON_LEFT: return CEGUI::MouseButton::LeftButton;
case SDL_BUTTON_MIDDLE: return CEGUI::MouseButton::MiddleButton;
case SDL_BUTTON_RIGHT: return CEGUI::MouseButton::RightButton;
case SDL_BUTTON_X1: return CEGUI::MouseButton::X1Button;
case SDL_BUTTON_X2: return CEGUI::MouseButton::X2Button;
}
return CEGUI::MouseButton::NoButton;
}
void GUI::onSDLEvent(SDL_Event& evnt) {
CEGUI::utf32 codePoint;
switch (evnt.type) {
case SDL_MOUSEMOTION:
m_context->injectMousePosition(evnt.motion.x, evnt.motion.y);
break;
case SDL_KEYDOWN:
m_context->injectKeyDown(SDLKeyToCEGUIKey(evnt.key.keysym.sym));
break;
case SDL_KEYUP:
m_context->injectKeyUp(SDLKeyToCEGUIKey(evnt.key.keysym.sym));
break;
case SDL_TEXTINPUT:
codePoint = 0;
// TODO: This is wrong! We need to decode utf-8 and convert to utf-32.
// Thanks to Spartan190 for figuring this out. You need to get a utf conversion library
// or function that can convert the text, such as from UTF8-CPP: http://utfcpp.sourceforge.net/
// If you use UTF8-CPP just use this code and it should work:
// std::string evntText = std::string(evnt.text.text);
// std::vector<int> utf32result;
// case SDL_TEXTINPUT:
// utf8::utf8to32(evnt.text.text, evnt.text.text + evntText.size(), std::back_inserter(utf32result));
// codePoint = (CEGUI::utf32)utf32result[0];
// m_context->injectChar(codePoint);
for (int i = 0; evnt.text.text[i] != '\0'; i++) {
codePoint |= (((CEGUI::utf32)*(unsigned char*)&evnt.text.text[i]) << (i * 8));
}
m_context->injectChar(codePoint);
break;
case SDL_MOUSEBUTTONDOWN:
m_context->injectMouseButtonDown(SDLButtonToCEGUIButton(evnt.button.button));
break;
case SDL_MOUSEBUTTONUP:
m_context->injectMouseButtonUp(SDLButtonToCEGUIButton(evnt.button.button));
break;
}
}
void GUI::loadScheme(const std::string& schemeFile) {
CEGUI::SchemeManager::getSingleton().createFromFile(schemeFile);
}
CEGUI::Window* GUI::createWidget(const std::string& type, const glm::vec4& destRectPerc, const glm::vec4& destRectPix, const std::string& name /*= ""*/) {
CEGUI::Window* newWindow = CEGUI::WindowManager::getSingleton().createWindow(type, name);
m_root->addChild(newWindow);
setWidgetDestRect(newWindow, destRectPerc, destRectPix);
return newWindow;
}
void GUI::setWidgetDestRect(CEGUI::Window* widget, const glm::vec4& destRectPerc, const glm::vec4& destRectPix) {
widget->setPosition(CEGUI::UVector2(CEGUI::UDim(destRectPerc.x, destRectPix.x), CEGUI::UDim(destRectPerc.y, destRectPix.y)));
widget->setSize(CEGUI::USize(CEGUI::UDim(destRectPerc.z, destRectPix.z), CEGUI::UDim(destRectPerc.w, destRectPix.w)));
}
void GUI::setFont(const std::string& fontFile) {
CEGUI::FontManager::getSingleton().createFromFile(fontFile + ".font");
m_context->setDefaultFont(fontFile);
}
}