Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 9d51adcb3dab9417a0192ac7bf65de76704b42ee (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
/*
   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 "core/core.h"
#include "sys/sys.h"
#include "ui/ui.h"

namespace client {

const size_t DEFAULT_MAX_HISTO_LINES = 512;

Chat::Chat(ui::Widget *parent) : ui::Window(parent)
{
	set_label("chat");
	history.clear();
	history.push_back("");
	history_pos = history.rbegin();

	chat_label = new ui::Label(this, "^BSay^F:^B");
	chat_label->set_alignment(ui::AlignLeft | ui::AlignVCenter);
	chat_label->set_border(false);

	chat_input = new ui::InputBox(this);
	chat_input->set_border(false);

	chat_input->set_focus();

	set_background(true);
	set_visible(false);
}

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


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

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

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

void Chat::resize()
{
	chat_label->set_location(font()->width(), height() / 5.0f);
	chat_label->set_size(width() - font()->width() * 2, height() / 5.0f);

	chat_input->set_location(font()->width(), height() / 5.0f * 3.0f);
	chat_input->set_size(width() - font()->width() * 2, height() / 5.0f);
}

bool Chat::on_keypress(const int key, const unsigned int modifier)
{
	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_MAX_HISTO_LINES) {
				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 {
				core::cmd() << "say " << chat_input->text() << std::endl;
			}
			(*history.rbegin()) = chat_input->text();

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

		hide();
		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;
	}

	return false;
}

void Chat::event_draw()
{
	if (!core::application()->connected()) {
		hide();
		return;
	}

	if (ui::root()->active())
		return;
	else
		Widget::event_draw();
}

} // namespace client