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

#include "client/serverlistmenu.h"
#include "client/closeiconbutton.h"

#include "ui/ui.h"

#include "core/commandbuffer.h"

#include "sys/sys.h"


namespace client
{

ServerListMenu::ServerListMenu(ui::Widget *parent, const char *label) : 
	ui::Window(parent)
{
	set_label(label);
	set_border(true);
	set_background(true);
	set_font(ui::root()->font_small());
	
	// window title
	serverlistmenu_titlelabel = new ui::Label(this);
	serverlistmenu_titlelabel->set_label("connect");
	serverlistmenu_titlelabel->set_text("CONNECT");
	serverlistmenu_titlelabel->set_background(false);
	serverlistmenu_titlelabel->set_border(false);
	serverlistmenu_titlelabel->set_font(ui::root()->font_large());
	serverlistmenu_titlelabel->set_alignment(ui::AlignCenter);
	
	// close button
	serverlistmenu_closebutton = new CloseIconButton(serverlistmenu_titlelabel);
		
	// server listview
	serverlistmenu_serverlistview = new ui::ListView(this);
	serverlistmenu_serverlistview->set_label("files");
	
	// save button
	serverlistmenu_connectbutton = new ui::Button(this, "Connect");
	serverlistmenu_connectbutton->set_label("connect");
}

ServerListMenu::~ServerListMenu()
{
}

void ServerListMenu::resize()
{
	const float padding =  ui::UI::padding;
	
	// resize title label
	serverlistmenu_titlelabel->set_size(width() - padding * 2.0f, serverlistmenu_titlelabel->font()->height());	
	serverlistmenu_titlelabel->set_location(padding, padding);

	// resize close button
	serverlistmenu_closebutton->set_size(serverlistmenu_titlelabel->font()->height(), serverlistmenu_titlelabel->font()->height());
	serverlistmenu_closebutton->set_location(serverlistmenu_titlelabel->width() - serverlistmenu_closebutton->width(), 0);
	
	// resize files listview
	serverlistmenu_serverlistview->set_size(
		width() - padding * 2.0f, 
		height() - ui::UI::elementsize.height() - padding * 3.0f - serverlistmenu_titlelabel->bottom()
	);
	serverlistmenu_serverlistview->set_location(
		padding, 
		serverlistmenu_titlelabel->bottom() + padding
	);
	
	// resize connect button
	serverlistmenu_connectbutton->set_size(ui::UI::elementsize);
	serverlistmenu_connectbutton->set_location(
		(width() - padding * 2.0f - serverlistmenu_connectbutton->width()) * 0.5f,
		height() - padding - serverlistmenu_connectbutton->height()
	);
}

void ServerListMenu::refresh()
{	
	const float padding =  ui::UI::padding;
	
	serverlistmenu_serverlistview->clear();
	serverlistmenu_connectbutton->show();
	serverlistmenu_connectbutton->disable();
	
	// FIXME support custom port numbers
	ui::ListItem *listitem = new ui::ListItem(serverlistmenu_serverlistview, "osirion.org development server");
	listitem->set_value("osirion.org");
	listitem->set_sortkey("");
	
	listitem->set_font(ui::root()->font_tiny());
	listitem->set_height(listitem->font()->height() + padding);
	
	serverlistmenu_serverlistview->sort(false);
	serverlistmenu_serverlistview->event_resize();

}

void ServerListMenu::hide()
{
	Window::hide();
}

void ServerListMenu::show()
{
	Window::show();
	refresh();
}

void ServerListMenu::show_connect_info()
{
	if (!serverlistmenu_serverlistview->selected()) {
		serverlistmenu_connectbutton->disable();
	} else {
		serverlistmenu_connectbutton->enable();
	}
}

void ServerListMenu::connect()
{
	if (serverlistmenu_serverlistview->selected()) {
		std::stringstream str;
		str << "connect " << serverlistmenu_serverlistview->selected()->value();
		core::CommandBuffer::exec(str.str());
	}
}

bool ServerListMenu::on_keypress(const int key, const unsigned int modifier)
{
	if (key == SDLK_ESCAPE) {
		if (visible()) {
			// hide the parent mainwindow
			parent()->hide();
			return true;
		}
	}
	return false;
}

bool ServerListMenu::on_emit(ui::Widget *sender, const ui::Widget::Event event, void *data)
{
	if (sender == serverlistmenu_closebutton) {
		if (event == ui::Widget::EventButtonClicked) {
			// hide the parent mainwindow
			parent()->hide();
			return true;
		}
	} else if (sender == serverlistmenu_connectbutton) {
		if (event == ui::Widget::EventButtonClicked) {
			// do the actual connect and close the window
			parent()->hide();
			connect();
			return true;
		}
	} else if (sender == serverlistmenu_serverlistview) {
		if (event == ui::Widget::EventListViewChanged) {
			show_connect_info();
			return true;
		}
	}
	
	return Window::on_emit(sender, event, data);
}


} // namespace client