Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 18d474df9054f2cabfd1a93bb96bd18a10f49614 (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
/*
   client/savegamemenu.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/savegamemenu.h"
#include "filesystem/filesystem.h"
#include "ui/ui.h"

namespace client
{

SaveGameMenu::SaveGameMenu(ui::Widget *parent, const char *label, const Mode mode) : 
	ui::Window(parent)
{
	set_label(label);
	set_border(true);
	set_background(true);
	set_font(ui::root()->font_small());
	
	savegamemenu_mode = mode;
	
	// window title
	savegamemenu_titlelabel = new ui::Label(this);
	savegamemenu_titlelabel->set_label("title");
	savegamemenu_titlelabel->set_background(false);
	savegamemenu_titlelabel->set_border(false);
	savegamemenu_titlelabel->set_font(ui::root()->font_large());
	savegamemenu_titlelabel->set_alignment(ui::AlignCenter);
	if (savegamemenu_mode == Save) {
		savegamemenu_titlelabel->set_text("SAVE GAME");
	} else {
		savegamemenu_titlelabel->set_text("LOAD GAME");
	}
	
	// close button
	savegamemenu_closebutton = new ui::IconButton(savegamemenu_titlelabel, "bitmaps/icons/window_close");
	savegamemenu_closebutton->set_label("close");
	
	// file listview
	savegamemenu_filelistview = new ui::ListView(this);
	savegamemenu_filelistview->set_label("files");
	
	savegamemenu_descrlabel = new ui::Label(this);
	savegamemenu_descrlabel->set_label("description");
	
	//savegamemenu_descrinput = new ui::InputBox(this);
	
	// delete button
	savegamemenu_deletebutton = new ui::IconButton(this, "bitmaps/icons/button_delete");
	savegamemenu_deletebutton->set_label("delete");
}

SaveGameMenu::~SaveGameMenu()
{
}

void SaveGameMenu::resize()
{
	const float padding =  ui::root()->font_large()->height();
	const float icon_size = 24.0f; // small icons
	
	// resize title label
	savegamemenu_titlelabel->set_size(width() - padding * 2.0f, savegamemenu_titlelabel->font()->height());	
	savegamemenu_titlelabel->set_location(padding, padding);

	// resize close button
	savegamemenu_closebutton->set_size(savegamemenu_titlelabel->font()->height(), savegamemenu_titlelabel->font()->height());
	savegamemenu_closebutton->set_location(savegamemenu_titlelabel->width() - savegamemenu_closebutton->width(), 0);
	
	// resize files listview
	savegamemenu_filelistview->set_size(
		ui::UI::elementsize.width(), 
		height() - icon_size - padding * 3.0f - savegamemenu_titlelabel->bottom()
	);
	savegamemenu_filelistview->set_location(
		padding, 
		savegamemenu_titlelabel->bottom() + padding
	);
	
	// resize description label
	savegamemenu_descrlabel->set_size(
		width() - savegamemenu_filelistview->right() - 2.0f * padding,
		savegamemenu_filelistview->height()
	);
	savegamemenu_descrlabel->set_location(savegamemenu_filelistview->right() + padding, savegamemenu_filelistview->top());
	
	
	// resize eject button
	savegamemenu_deletebutton->set_size(icon_size, icon_size);
	savegamemenu_deletebutton->set_location(
		savegamemenu_filelistview->right() - icon_size, 
		savegamemenu_filelistview->bottom() + padding
	);
}

void SaveGameMenu::refresh()
{	
	ui::ListItem *listitem = 0;
	
	std::string savegamepath(filesystem::writedir());
	savegamepath.append("savegames");
	
	filesystem::Directory directory(savegamepath);

	savegamemenu_filelistview->clear();
	
	if (savegamemenu_mode == Save) {
		listitem = new ui::ListItem(savegamemenu_filelistview, "NEW SAVEGAME");
		listitem->set_label("new");
		listitem->set_font(ui::root()->font_tiny());
		listitem->set_height(listitem->font()->height() * 2.0f);
	}
	
	for (filesystem::Directory::FileNames::const_iterator it = directory.filenames().begin();
	    	it != directory.filenames().end(); ++it) 
	{
		const std::string filename((*it).c_str());
		if (filename.size() < 5) {
			continue;
		}
		
		std::string ext = filename.substr(filename.size() - 4);
		aux::lowercase(ext);
		if (ext.compare(".sav")) {
			continue;
		}
		
		std::string label = filename.substr(0, filename.size() - 4);
		
		listitem = new ui::ListItem(savegamemenu_filelistview, label.c_str());
		listitem->set_value(filename);
		
		listitem->set_font(ui::root()->font_tiny());
		listitem->set_height(listitem->font()->height() * 2.0f);
		
		// TODO savegame info, sortkey timestamp
	}
	
	savegamemenu_filelistview->event_resize();
}

void SaveGameMenu::show()
{
	Window::show();
	event_resize();
	refresh();
}

void SaveGameMenu::show_file_info()
{
	savegamemenu_descrlabel->clear();

	if (savegamemenu_filelistview->selected()) {
		std::string filename(savegamemenu_filelistview->selected()->value().c_str());
		savegamemenu_descrlabel->set_text(filename);
	}
}
bool SaveGameMenu::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 SaveGameMenu::on_emit(ui::Widget *sender, const ui::Widget::Event event, void *data)
{
	if (sender == savegamemenu_closebutton) {
		if (event == ui::Widget::EventButtonClicked) {
			// hide the parent mainwindow
			parent()->hide();
			return true;
		}
	} else if (sender == savegamemenu_filelistview) {
		if (event == ui::Widget::EventListViewChanged) {
			show_file_info();
			return true;
		}
	}
	
	return Window::on_emit(sender, event, data);
}


} // namespace client