Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: b683778db081c30b225063030e7f8a9f19db1dcd (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
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
/*
   client/console.cc
   This file is part of the Osirion project and is distributed under
   the terms and conditions of the GNU General Public License version 2
*/

#include <iostream>
#include <fstream>
#include <cmath>

#include "auxiliary/functions.h"
#include "audio/audio.h"
#include "core/application.h"
#include "core/core.h"
#include "core/gameinterface.h"
#include "filesystem/filesystem.h"
#include "render/gl.h"
#include "ui/console.h"
#include "ui/definitions.h"
#include "ui/paint.h"
#include "ui/ui.h"

namespace ui
{

const float DEFAULT_CONSOLE_HEIGHT = 0.7f;
const size_t DEFAULT_MAX_HISTO_LINES = 512;
// number of lines to scroll
const size_t SCROLL_LINES = 3;

// the global console buffer object
ConsoleBuffer Console::con_buffer;

/* -- ConsoleBuffer ------------------------------------------------ */
ConsoleBuffer::ConsoleBuffer() : sys::ConsoleInterface()
{
	//con_print << "^BInitializing console..." << std::endl;

}

ConsoleBuffer::~ConsoleBuffer()
{
	//con_print << "^BShutting down console..." << std::endl;
}

//--- Console -----------------------------------------------------

Console::Console(ui::Widget *parent) : ui::Window(parent)
{
	set_visible(false);
	set_border(false);
	set_background(true);
	set_label("console");

	history.clear();
	history.push_back("");
	history_pos = history.rbegin();

	std::string version("^F" + core::name() + ' ' + core::version());
	console_versionlabel = new Label(this, version.c_str());
	console_versionlabel->set_border(false);
	console_versionlabel->set_background(false);
	
	console_scrollpane = new ScrollPane(this, con_buffer.log());
	console_scrollpane->set_border(false);
	console_scrollpane->set_background(false);
	console_scrollpane->set_scroll(0);
	
	console_scrollbar = new ScrollBar(this);
	console_scrollbar->set_border(false);
	console_scrollbar->set_background(false);

	console_input = new InputBox(this);
	console_input->set_focus();
	console_input->set_border(false);
	console_input->set_background(false);
	console_input->set_prompt("^N>");

	load_history();
}

Console::~Console()
{
	save_history();
	history.clear();
}

void Console::show()
{
	ui::Window::show();
	raise();
	
	SDL_SetRelativeMouseMode(SDL_FALSE);

	console_scrollpane->set_scroll(0);
	console_scrollpane->set_offset(3);

	history_pos = history.rbegin();
	(*history_pos).clear();
	console_input->set_text((*history_pos));
	console_input->set_focus();
	
	audio::play("ui/console");
}

void Console::hide()
{	
	ui::Window::hide();
	
	core::Cvar *input_grab = core::Cvar::find("input_grab");
	if (input_grab && input_grab->value()) {
		SDL_SetRelativeMouseMode(SDL_TRUE);
	} else {
		SDL_SetRelativeMouseMode(SDL_FALSE);
	}
	
	audio::play("ui/console");
}

void Console::toggle()
{
	if (visible())
		hide();
	else
		show();
}

bool Console::on_emit(Widget *sender, const Event event, void *data)
{
	if (sender == console_scrollbar) {
		 if (event == ui::Widget::EventScrollBarChanged) {
			 console_scrollpane->set_scroll(  (con_buffer.log().size() - console_scrollbar->value()) );
			 return true;
		 }
	}
	
	return false;
}

bool Console::on_keypress(const int key, const unsigned int modifier)
{
	ui::Text::reverse_iterator upit;

	switch (key) {
		case SDLK_ESCAPE:
			if (visible()) {
				hide();
				return true;
			} else {
				return false;
			}
			break;
		
		case SDLK_TAB:
			console_input->complete();
			return true;
			break;

		case SDLK_RETURN:
			if (console_input->text().size()) {
				// store input in history
				while (history.size() >= DEFAULT_MAX_HISTO_LINES) {
					history.pop_front();
				}
				core::cmd() << console_input->text() << std::endl;
				con_print << "^B>" << console_input->text() << std::endl;
				(*history.rbegin()).assign(console_input->text());
				history.push_back("");
				history_pos = history.rbegin();
				console_input->set_text((*history_pos));
			}
			return true;
			break;

		case SDLK_UP:
			upit = history_pos;
			++upit;
			if (upit != history.rend()) {
				history_pos = upit;
				console_input->set_text((*history_pos));
			}
			return true;
			break;
		case SDLK_DOWN:
			if (history_pos != history.rbegin()) {
				--history_pos;
				console_input->set_text((*history_pos));
			}
			return true;
			break;
		case SDLK_PAGEUP:
			console_scrollpane->inc_scroll(SCROLL_LINES);
			return true;
			break;
		case SDLK_PAGEDOWN:
			console_scrollpane->dec_scroll(SCROLL_LINES);
			return true;
			break;
	}

	return false;
}

bool Console::on_mousewheel(const math::Vector2f & direction)
{
	if (direction.y() > 0 )
	{
		console_scrollpane->inc_scroll(SCROLL_LINES);
		return true;
	} else if (direction.y() < 0) {
		console_scrollpane->dec_scroll(SCROLL_LINES);
		return true;
	}
	return false;
}

void Console::draw()
{
	console_scrollbar->set_range(0, (float) con_buffer.log().size());
	console_scrollbar->set_value( (float) (con_buffer.log().size() - console_scrollpane->scroll()));
	
	// disable mouse cursor
	if (has_mouse_focus()) {
		ui::root()->set_pointer();
	}
}

void Console::resize()
{
	const float padding = font()->width();
	
	if (core::application()->connected()) {
		set_size(parent()->size().width(), parent()->size().height() * DEFAULT_CONSOLE_HEIGHT);
	} else {
		set_size(parent()->size());
	}

	// resize scrollbar
	console_scrollbar->set_size(
		font()->height(), 
		height()
	);
	console_scrollbar->set_location(
		width() - console_scrollbar->width(), 
		0.0f
	);
	
	// resize version label
	console_versionlabel->set_size(
		width() - console_scrollbar->width() - padding,
		font()->height() * 1.5f
	);
	console_versionlabel->set_location(
		padding, 
		0.0f
	);
	
	// resize input bar
	console_input->set_size(
		console_versionlabel->width(), 
		font()->height()
	);
	console_input->set_location(
		console_versionlabel ->left(),
		bottom() - console_input->height()
	);
	
	// resize scroll pane
	console_scrollpane->set_size(
		console_versionlabel->width(), 
		console_input->top() - console_versionlabel->bottom()
	);
	console_scrollpane->set_location(
		console_versionlabel->left(), 
		console_versionlabel->bottom()
	);
}

void Console::save_history()
{
	if (history.size() <= 1)
		return;

	std::string filename(filesystem::writedir());
	filename.append("history.txt");
	std::ofstream ofs(filename.c_str());

	if (!ofs.is_open()) {
		con_warn << "Could not write " << filename << std::endl;
		return;
	}
	ui::Text::iterator it;
	size_t l = 1;
	for (it = history.begin(); it != history.end(); it++) {
		if (l < history.size())
			ofs << (*it) << std::endl;
		l++;
	}

	ofs.close();
}

void Console::load_history()
{
	std::string filename(filesystem::writedir());
	filename.append("history.txt");
	std::ifstream ifs(filename.c_str(), std::ifstream::in);

	if (!ifs.is_open()) {
		con_warn << "Could not read " << filename << std::endl;
		return;
	}

	history.clear();
	char line[MAXCMDSIZE];
	while (ifs.getline(line, MAXCMDSIZE - 1)) {
		history.push_back(line);
	}

	ifs.close();

	history.push_back("");
	history_pos = history.rbegin();
}

}