Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: fc53b0952e22cdd40044f734014d97b7a3493718 (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
   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 "filesystem/filesystem.h"
#include "core/core.h"
#include "render/render.h"
#include "client/console.h"
#include "client/video.h"
#include "client/keyboard.h"

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

namespace client {

namespace console {

//--- private definition ------------------------------------------


/// private client console implementation
class Console : public sys::ConsoleInterface {
public:
	/// stream to send normal messages too
	virtual std::ostream & messagestream();

	/// stream to send warning messages too
	virtual std::ostream & warningstream();

	/// stream to send error messages too
	virtual std::ostream & errorstream();
	
	/// stream to send debug messages too
	virtual std::ostream & debugstream();

	/// console text buffer
	std::stringstream buffer;
};

// private client console object
Console console;

// console text data
std::deque<std::string> text;

// input history
std::deque<std::string> history;
std::deque<std::string>::reverse_iterator history_pos;
size_t input_pos = 0;

// console visibility
bool console_visible = false;
size_t console_scroll = 0;

// notifications
size_t notify_pos = 0;
std::string notify_text[MAXNOTIFYLINES];
float notify_time[MAXNOTIFYLINES];


//--- engine functions --------------------------------------------

void func_con_toggle(std::string const &args)
{
	std::istringstream argstream(args);
	int i;

	if (argstream >> i) {
		if (i) console_visible = true; else console_visible = false;
	} else
		console_visible = !console_visible;
}

//--- public ------------------------------------------------------

void init()
{
	con_print << "Initializing console..." << std::endl;
	
	console_visible = false;	
	
	// add engine functions
	core::Func::add("con_toggle", (core::FuncPtr) func_con_toggle);
	
	text.clear();
	console_scroll = 0;	

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

	load_history();
}

void shutdown()
{
	con_print << "Shutting down console..." << std::endl;

	save_history();

	// remove engine functions
	core::Func::remove("con_toggle");
	
	text.clear();
	console_scroll = 0;	

	history.clear();
	input_pos = 0;
}

void draw()
{
	using namespace render;
		
	if(!console_visible)
		return;

	float con_height = 0.70f;

	glBindTexture(GL_TEXTURE_2D, render::textures[1]); // bitmaps/conchars.tga

	// draw version below the bottom of the console
	gl::enable(GL_TEXTURE_2D);
	gl::color(0.0f, 1.0f, 0.0f, 0.5f);
	std::string version(core::name());
	version += ' ';
	version.append(core::version());
	draw_text(video::width-CHARWIDTH*(version.size()+1), video::height*con_height-CHARHEIGHT-4, version);
	gl::disable(GL_TEXTURE_2D);

	// draw the transparent console background
	gl::color(1.0f, 1.0f, 1.0f, 0.02f);
	
	gl::begin(gl::Quads);
	gl::vertex(0.0f, 0.0f, 0.0f);
	gl::vertex(video::width, 0.0f,0.0f);
	gl::vertex(video::width,video::height*con_height,0.0f);
	gl::vertex(0,video::height*con_height,0.0f);
	gl::end();
	
	// draw the console text
	if (console_scroll > text.size())
		console_scroll = text.size();

	gl::enable(GL_TEXTURE_2D);
	std::deque<std::string>::reverse_iterator rit = text.rbegin();
	float bottom = video::height*con_height-2*CHARHEIGHT-8;
	float y = bottom+console_scroll*CHARHEIGHT;
	while (y > 0 && rit < text.rend()) {
		if (y <= bottom) {
			std::string line(*rit);
		
			if (line[0] == '?')
				gl::color(0.7f,0.7f,0.7f, 1.0f);
			else if (line[0] == '*')
				gl::color(1.0f,1.0f,0.0f, 1.0f);
			else if (line[0] == '!')
				gl::color(1.0f,0.0f,0.0f, 1.0f);
			else
				gl::color(1.0f,1.0f,1.0f, 1.0f);
			line.erase(0,2);
	
			draw_text(CHARWIDTH, y, line);
		}
		y -= CHARHEIGHT;
		++rit;
	}

	// draw the console input
	gl::color(0.0f, 1.0f, 0.0f, 1.0f);
	draw_text(CHARWIDTH, video::height*con_height - CHARHEIGHT - 4, (*history_pos));

	// draw cursor
	if ((core::application()->time() - ::floorf(core::application()->time())) < 0.5f) {
		std::string cursor("_");
		draw_text(CHARWIDTH*(input_pos+1), video::height*con_height - CHARHEIGHT - 4 , cursor);
	}
	gl::disable(GL_TEXTURE_2D);
}

void flush() 
{
	char line[MAXCMDSIZE];
	while(console.buffer.getline(line, MAXCMDSIZE-1)) {	
		while (text.size() >= MAXCONLINES) {
			text.pop_front();
		}
		text.push_back(std::string(line));

		// save notification
		notify_text[notify_pos] = line;
		notify_time[notify_pos] = sys::time();
		notify_pos = (notify_pos+1) % MAXNOTIFYLINES;

		// print to stdout
		std::cout << line << std::endl;
	}
	
	console.buffer.clear();
}

void toggle()
{
	console_visible = !console_visible;
	
	if (console_visible) {
		console_scroll = 0;
		input_pos = 0;

		history_pos = history.rbegin();
		(*history_pos).clear();
	}
}

void keypressed(int key)
{
	std::deque<std::string>::reverse_iterator upit;

	switch( key ) {
	case SDLK_TAB:
		core::CommandBuffer::complete( (*history_pos), input_pos);
		break;
	case SDLK_RETURN:
		if ((*history_pos).size()) {
			// store input into history
			while (history.size() >= MAXHISTOLINES) {
				history.pop_front();
			}

			core::cmd() << (*history_pos) << std::endl;
			(*history.rbegin()) = (*history_pos);

			history.push_back("");
			history_pos = history.rbegin();
			input_pos = 0;
		}
		break;
	case SDLK_UP:
		upit = history_pos;
		++upit;
		if (upit != history.rend()) {
			history_pos = upit;
			input_pos = (*history_pos).size();
		}
		break;
	case SDLK_DOWN:
		if (history_pos != history.rbegin()) {
			--history_pos;
			input_pos = (*history_pos).size();
		}
		break;
	case SDLK_HOME:
		input_pos = 0;
		break;
	case SDLK_END:		
		input_pos = (*history_pos).size();
		break;
	case SDLK_LEFT:
		if (input_pos > 0)
			input_pos--;
		break;
	case SDLK_RIGHT:
		if (input_pos < (*history_pos).size())
			input_pos++;
		break;
	case SDLK_BACKSPACE:
		if ((*history_pos).size() && input_pos) {
			(*history_pos).erase(input_pos-1, 1);
			input_pos--;
		}
		break;
	case SDLK_PAGEUP:
		console_scroll +=5;
		if (console_scroll > text.size()) console_scroll = text.size();
		break;
	case SDLK_PAGEDOWN:
		if (console_scroll > 5)	console_scroll -=5;
		else console_scroll = 0;
		break;
	default:
		if ((key >= 32 ) && (key <175)) {
			if (input_pos == (*history_pos).size())
				(*history_pos) += (char)key;
			else
				(*history_pos).insert(input_pos, 1, (char)key);
			input_pos++;
		}
		break;
	}

	
}

bool visible()
{
	return console_visible;
}

void 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;
	}
	std::deque<std::string>::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 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();
	input_pos = 0;
}
//--- private -----------------------------------------------------

std::ostream & Console::messagestream()
{
	return (buffer << ". ");
}

std::ostream & Console::warningstream()
{
	return (buffer << "* ");
}

std::ostream & Console::errorstream()
{
	return (buffer << "! ");
}

std::ostream & Console::debugstream()
{
	return (buffer << "? ");
}

} // namespace console

} // namespace client