Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: f921e79f1e6aa4a9ed63815d5511ed4db1e0f4cc (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
/*
   client/chat.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 "auxiliary/functions.h"
#include "client/chat.h"
#include "client/client.h"
#include "client/closeiconbutton.h"
#include "core/core.h"
#include "core/gameinterface.h"
#include "sys/sys.h"
#include "ui/ui.h"

namespace client
{

const size_t DEFAULT_CHAT_LOG_SIZE = 2048;
const size_t DEFAULT_CHAT_HISTO_SIZE = 512;

Chat::Chat(ui::Widget *parent) : ui::Window(parent)
{
	set_label("chat");
	history.clear();
	history.push_back("");
	history_pos = history.rbegin();
	
	// window title
	chat_titlelabel = new ui::Label(this);
	chat_titlelabel->set_label("title");
	chat_titlelabel->set_background(false);
	chat_titlelabel->set_border(false);
	chat_titlelabel->set_font(ui::root()->font_large());
	chat_titlelabel->set_alignment(ui::AlignCenter);
	chat_titlelabel->set_text("CHAT");
	
	// close button
	chat_closebutton = new CloseIconButton(chat_titlelabel);
	
	// player list
	chat_playerlist = new ui::ListView(this);
	chat_playerlist->set_label("playerlist");
	
	// ---- right pane
	chat_pane_right = new ui::Widget(this);
	chat_pane_right->set_background(true);
	chat_pane_right->set_border(true);
	chat_pane_right->set_focus();
	
	// chat text
	chat_scrollpane = new ui::ScrollPane(chat_pane_right, chat_log);
	chat_scrollpane->set_background(false);
	chat_scrollpane->set_border(false);
	chat_scrollpane->set_label("text");
	
	// chat input
	chat_input = new ui::InputBox(chat_pane_right);
	chat_input->set_background(false);
	chat_input->set_border(false);
	set_prompt();
	chat_input->set_focus();

	set_background(true);
	set_visible(false);

	chat_small = false;
	chat_playerlist_timestamp = 0;
}

Chat::~Chat()
{
	history.clear();
}

void Chat::clear()
{
	chat_log.clear();
	chat_input->clear();
	chat_playerlist->clear();
	chat_playerlist_timestamp = 0;
}


void Chat::set_small_view(bool small_chat_view)
{
	chat_small = small_chat_view;
	resize();
}

void Chat::event_text(const std::string & text)
{
	while (chat_log.size() >= DEFAULT_CHAT_LOG_SIZE) {
		chat_log.pop_front();
	}
	chat_log.push_back(text);
}

void Chat::show()
{
	Window::show();

	history_pos = history.rbegin();
	(*history_pos).clear();
	chat_input->set_text((*history_pos));

	chat_scrollpane->set_scroll(0);
}

bool Chat::on_emit(ui::Widget *sender, const ui::Widget::Event event, void *data)
{
	if (sender == chat_closebutton) {
		if (event == ui::Widget::EventButtonClicked) {
			hide();
			return true;
		}
	}
	
	return Window::on_emit(sender, event, data);
}

bool Chat::on_keypress(const int key, const unsigned int modifier)
{
	// number of lines to scroll
	const size_t scroll_offset = 3;

	History::reverse_iterator upit;

	switch (key) {
		case SDLK_ESCAPE:
			if (visible()) {
				hide();
				return true;
			} else {
				return false;
			}
			
		case SDLK_RETURN:
			if (chat_input->text().size()) {
				// store input into history
				while (history.size() >= DEFAULT_CHAT_HISTO_SIZE) {
					history.pop_front();
				}

				if (chat_input->text().c_str()[0] == '/' || chat_input->text().c_str()[0] == '\\') {
					core::cmd() <<  &chat_input->text().c_str()[1] << std::endl;
				} else {
					// FIXME semi-column ; truncates the command
					//core::cmd() << "say " << chat_input->text() << std::endl;
					client()->say(chat_input->text());
				}
				(*history.rbegin()) = chat_input->text();

				history.push_back("");
				history_pos = history.rbegin();
				chat_input->set_text((*history_pos));

				if (chat_small)
					hide();
			} else {
				hide();
			}
			return true;
			break;

		case SDLK_TAB:
			if (chat_input->text().size() && chat_input->text()[0] == '/') {
				// command mode
				chat_input->complete();
			} else {
				// TODO chat mode, switch channel
			}
			return true;
			break;

		case SDLK_UP:
			upit = history_pos;
			++upit;
			if (upit != history.rend()) {
				history_pos = upit;
				chat_input->set_text((*history_pos));
			}
			return true;
			break;

		case SDLK_DOWN:
			if (history_pos != history.rbegin()) {
				--history_pos;
				chat_input->set_text((*history_pos));
			}
			return true;
			break;

		case SDLK_PAGEUP:
			chat_scrollpane->inc_scroll(scroll_offset);
			return true;
			break;

		case SDLK_PAGEDOWN:
			chat_scrollpane->dec_scroll(scroll_offset);
			return true;
			break;
	}

	return false;
}

void Chat::set_prompt()
{
	if (chat_input->text().size() && chat_input->text()[0] == '/') {
		chat_input->set_prompt("^BCommand^F:^B ");
	} else {
		chat_input->set_prompt("^BSay^F:^B ");
	}
}

void Chat::draw()
{
	if (!chat_small && (chat_playerlist_timestamp != core::game()->playerlist_timestamp())) {
		refresh();
	}
	
	set_prompt();
	ui::Window::draw();
}

void Chat::refresh()
{
	chat_playerlist->clear();
	
	ui::ListItem *listitem = 0;
	/*
	listitem = new ui::ListItem(chat_playerlist, "Shout");
	listitem->set_height(listitem->font()->height() * 2.0f);
	listitem->set_sortkey(" 0");
	
	listitem = new ui::ListItem(chat_playerlist, "Say");
	listitem->set_height(listitem->font()->height() * 2.0f);
	listitem->set_sortkey(" 1");
	*/
	for (core::GameInterface::Players::const_iterator it = core::game()->players().begin(); it != core::game()->players().end(); it++) {
		std::string descr(aux::text_strip((*it)->name().c_str()));
		if ((*it)->zone()) {
			descr.append("\n^N");
			descr.append(aux::pad_left((*it)->zone()->name(), 20));
		}
		listitem = new ui::ListItem(chat_playerlist, descr.c_str());
		listitem->set_height(listitem->font()->height() * 3.0f);
		listitem->set_sortkey(aux::text_strip_lowercase((*it)->name()));
	}	
	chat_playerlist->sort();
	
	chat_playerlist_timestamp = core::game()->playerlist_timestamp();
}

void Chat::resize()
{
	const float padding =  ui::root()->font_large()->height();
	
	if (chat_small) {
		chat_titlelabel->hide();
		chat_playerlist->hide();
		chat_scrollpane->hide();

		// resize right panbe to input bar size
		chat_pane_right->set_size(size());
		chat_pane_right->set_location(0.0f, 0.0f);
		
		// input bar
		chat_input->set_size(chat_pane_right->width() - 2.0f * padding, chat_input->font()->height());
		chat_input->set_location(padding, (chat_pane_right->height() - chat_input->height()) * 0.5f );
		
	} else {
		chat_titlelabel->show();
		chat_playerlist->show();
		chat_scrollpane->show();
		
		// resize title label
		chat_titlelabel->set_size(width() - padding * 2.0f, chat_titlelabel->font()->height());	
		chat_titlelabel->set_location(padding, padding);

		// resize close button
		chat_closebutton->set_size(chat_titlelabel->font()->height(), chat_titlelabel->font()->height());
		chat_closebutton->set_location(chat_titlelabel->width() - chat_closebutton->width(), 0);
		
		// resize player names listview
		chat_playerlist->set_location(padding, chat_titlelabel->bottom() + padding);
		chat_playerlist->set_size(ui::UI::elementsize.width(), height() - chat_titlelabel->bottom() - padding * 2.0f);
				
		// resize right pane
		chat_pane_right->set_size(width() - chat_playerlist->right() - padding * 2.0f, chat_playerlist->height());
		chat_pane_right->set_location(chat_playerlist->right() + padding, chat_titlelabel->bottom() + padding);
			
		{
			const ui::Widget *pane = chat_pane_right;
			const float margin_horizontal = pane->font()->width();
			const float margin_vertical = pane->font()->height() * 0.5f;
		
			// resize input bar
			chat_input->set_size(pane->width() - 2.0f * margin_horizontal, chat_input->font()->height());
			chat_input->set_location(margin_horizontal, pane->height() - chat_input->height() - margin_vertical);
			
			// resize chat text pane
			chat_scrollpane->set_size(pane->width() - 2.0f * margin_horizontal, chat_input->top() - 2.0f * margin_vertical);
			chat_scrollpane->set_location(margin_horizontal, margin_vertical);
		}
	}

}


} // namespace client