summaryrefslogtreecommitdiff
path: root/gui.cpp
blob: ecdd7e2271de388333dd8c55925c3bd375ab8e2e (plain)
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
#include "gui.h"
#include "video.h"

#include "widgets/FormattedListboxTextItem.h"

#include <boost/format.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <RendererModules/OpenGL/CEGUIOpenGLRenderer.h>

using namespace CEGUI;

CEGUI::Window *GUI::root = NULL;
void GUI::init() {
	OpenGLRenderer::bootstrapSystem();

	DefaultResourceProvider* rp = static_cast<DefaultResourceProvider*>
		(System::getSingleton().getResourceProvider());

	rp->setResourceGroupDirectory("schemes", "GUI/");
	rp->setResourceGroupDirectory("imagesets", "GUI/");
	rp->setResourceGroupDirectory("fonts", "fonts/");
	rp->setResourceGroupDirectory("layouts", "GUI/layouts/");
	rp->setResourceGroupDirectory("looknfeels", "GUI/");

	Imageset::setDefaultResourceGroup("imagesets");
	Font::setDefaultResourceGroup("fonts");
	Scheme::setDefaultResourceGroup("schemes");
	WidgetLookManager::setDefaultResourceGroup("looknfeels");
	WindowManager::setDefaultResourceGroup("layouts");

	SchemeManager::getSingleton().create( "VanillaSkin.scheme" );
	SchemeManager::getSingleton().create( "Transparent.scheme" );

	FontManager::getSingleton().create("VeraMono.font");

	System::getSingleton().setDefaultFont( "VeraMono" );
	System::getSingleton().setDefaultMouseCursor( "Transparent-Images", "MouseArrow" );

	WindowManager& wmgr(WindowManager::getSingleton());

	root = wmgr.createWindow("DefaultWindow", "root");
	System::getSingleton().setGUISheet(root);
}

void GUI::pre_render() {
	MouseCursor::getSingleton().hide();
}

void GUI::render() {
	/* Disable depth testing to avoid cursor bugginess. */
	glDisable(GL_DEPTH_TEST);
	System::getSingleton().renderGUI();
	glEnable(GL_DEPTH_TEST);
}

void GUI::show() {
	wnd->show();
	_showing = true;
}

void GUI::hide() {
	wnd->hide();
	_showing = false;
}

bool GUI::showing() {
	return _showing;
}

/* ConsoleWindow */

ConsoleWindow::ConsoleWindow(Lua *lua) {
	this->lua = lua;

	WindowManager& wmgr(WindowManager::getSingleton());

	wnd = wmgr.loadWindowLayout("console.layout");
	root->addChildWindow(wnd);

	editbox = wmgr.getWindow("console_editbox");
	editbox->subscribeEvent(Editbox::EventMouseClick, Event::Subscriber(&ConsoleWindow::clicked, this));
	editbox->subscribeEvent(Editbox::EventKeyDown, Event::Subscriber(&ConsoleWindow::keydown, this));

	listbox = static_cast<Listbox*>(wmgr.getWindow("console_listbox"));
}

ConsoleWindow::~ConsoleWindow() {
	WindowManager& wmgr(WindowManager::getSingleton());
	wmgr.destroyWindow(wnd);
	wmgr.cleanDeadPool();
}

bool ConsoleWindow::clicked(const EventArgs& e) {
	handle_input();

	return true;
}

void ConsoleWindow::erase_editbox_text() {
	String s(editbox->getText());

	if(s.size() == 0)
		return;

	s.erase(s.size()-1);
	editbox->setText(s);
}

bool ConsoleWindow::keydown(const EventArgs& e) {
	const KeyEventArgs& ke = static_cast<const KeyEventArgs&>(e);
	switch(ke.scancode) {
		// enter
		// TODO: FIX!
#ifdef WIN32
		case 28:
#else
		case 36:
#endif
			handle_input();
			break;
		// backspace
		// TODO: FIX!
#ifdef WIN32
		case 14:
#else
		case 22:
#endif
			erase_editbox_text();
			break;
		default:
			break;
	}

	return true;
}

void ConsoleWindow::handle_input() {
	std::string s = editbox->getText().c_str();
	editbox->setText("");
	boost::trim(s);
	if(!s.size())
		return;

	add_line(s);

	try {
		lua->dostring(s);
	} catch(LuaError& e) {
		add_line(e.what());
	}
}

void ConsoleWindow::add_line(const std::string& line) {
	add_line(line.c_str());
}

void ConsoleWindow::add_line(const char *line) {
	FormattedListboxTextItem *item = new FormattedListboxTextItem(String((utf8*)line), HTF_WORDWRAP_LEFT_ALIGNED);
	listbox->addItem(item);
	listbox->ensureItemIsVisible(item);
}

void ConsoleWindow::update() {
	MouseCursor::getSingleton().show();
}

void ConsoleWindow::show() {
	GUI::show();
	WindowManager::getSingleton().getWindow("console_editbox")->activate();
}

/* ChatWindow */

ChatWindow::ChatWindow() {
	WindowManager& wmgr(WindowManager::getSingleton());

	wnd = wmgr.loadWindowLayout("chat.layout");
	root->addChildWindow(wnd);

	listbox = static_cast<Listbox*>(wmgr.getWindow("chat_listbox"));
}

ChatWindow::~ChatWindow() {
	WindowManager& wmgr(WindowManager::getSingleton());
	wmgr.destroyWindow(wnd);
	wmgr.cleanDeadPool();
}

void ChatWindow::add_line(const std::string& line) {
	add_line(line.c_str());
}

void ChatWindow::add_line(const char *line) {
	FormattedListboxTextItem *item = new FormattedListboxTextItem(String((utf8*)line), HTF_WORDWRAP_LEFT_ALIGNED);
	listbox->addItem(item);
	listbox->ensureItemIsVisible(item);
}

/* RaiseWindow */

/* default values */
float RaiseWindow::radius = 5;
float RaiseWindow::focus = .8;
float RaiseWindow::strength = .3;

RaiseWindow::RaiseWindow() {
	WindowManager& wmgr(WindowManager::getSingleton());

	wnd = wmgr.loadWindowLayout("raisewnd.layout");
	root->addChildWindow(wnd);

	radius_sb = static_cast<Scrollbar*>(wmgr.getWindow("radius_sb"));
	radius_sb_lbl = wmgr.getWindow("radius_sb_lbl");
	focus_sb = static_cast<Scrollbar*>(wmgr.getWindow("focus_sb"));
	focus_sb_lbl = wmgr.getWindow("focus_sb_lbl");
	strength_sb = static_cast<Scrollbar*>(wmgr.getWindow("strength_sb"));
	strength_sb_lbl = wmgr.getWindow("strength_sb_lbl");

	/* load default/last used values */
	radius_sb->setScrollPosition(radius);
	focus_sb->setScrollPosition(focus);
	strength_sb->setScrollPosition(strength);
}

RaiseWindow::~RaiseWindow() {
	/* store current values */
	radius = get_radius();
	focus = get_focus();
	strength = get_strength();

	WindowManager& wmgr(WindowManager::getSingleton());
	wmgr.destroyWindow(wnd);
	wmgr.cleanDeadPool();
}

void RaiseWindow::update() {
	MouseCursor::getSingleton().show();

	radius_sb_lbl->setText((boost::format("%.2f") % radius_sb->getScrollPosition()).str().c_str());
	focus_sb_lbl->setText((boost::format("%.2f") % focus_sb->getScrollPosition()).str().c_str());
	strength_sb_lbl->setText((boost::format("%.2f") % strength_sb->getScrollPosition()).str().c_str());
}

float RaiseWindow::get_radius() {
	return radius_sb->getScrollPosition();
}

float RaiseWindow::get_focus() {
	return focus_sb->getScrollPosition();
}

float RaiseWindow::get_strength() {
	return strength_sb->getScrollPosition();
}