Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 5837a176522065c207234fd150629f57f7365798 (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
/*
   client/inventory.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 "core/application.h"
#include "client/inventory.h"
#include "ui/label.h"
#include "ui/listitem.h"
#include "ui/paint.h"
#include "ui/ui.h"

#include <iomanip>

namespace client {
	
Inventory::Inventory(ui::Widget *parent) : ui::Window(parent)
{
	menu_infotimestamp = 0;
	menu_inventorytimestamp = 0;
	
	menu_namelabel = new ui::Label(this);
	menu_namelabel->set_label("label");
	menu_namelabel->set_background(false);
	menu_namelabel->set_border(false);
	menu_namelabel->set_font(ui::root()->font_large());
	menu_namelabel->set_alignment(ui::AlignCenter);
	menu_namelabel->set_text("Inventory");
	
	menu_listview = new ui::ListView(this);
	menu_listview->set_label("listview");
	menu_listview->set_background(false);
	menu_listview->set_border(true);
	
	menu_inventorytext = new ui::PlainText(this);
	menu_inventorytext->set_label("inventorytext");
	menu_inventorytext->set_background(false);
	menu_inventorytext->set_border(false);
	menu_inventorytext->set_font(ui::root()->font_small());
	
	hide();
}

Inventory::~Inventory()
{
}

void Inventory::toggle()
{
	if (visible())
		hide();
	else
		show();
}
void Inventory::update_inventory()
{
	menu_listview->clear();
	menu_infotimestamp = 0;
	menu_inventorytimestamp = 0;
	
	if (!core::localcontrol() || !core::localcontrol()->inventory()) {
		return;
	}
	const core::Item *selecteditem = (menu_listview->selected() ? menu_listview->selected()->item() : 0);
	
	for (core::Inventory::Items::const_iterator it = core::localcontrol()->inventory()->items().begin(); it != core::localcontrol()->inventory()->items().end(); it++) {
		core::Item *item = (*it);
	
		if (!item->info()) {
			continue;
		}
	
		// this makes sure inventory info gets requested from the server
		core::game()->request_info(item->info()->id());
		
		if (item->info()->timestamp() >	menu_infotimestamp) {
			menu_infotimestamp = item->info()->timestamp();
		}
		
		if (item->amount() != 0)  {
			ui::ListItem *listitem = 0;
			
			std::ostringstream str;
			str << item->info()->name().c_str();
			if (item->amount() > 0) {
				str << " (" << item->amount() << ")";
			}
			listitem = new ui::ListItem(menu_listview, str.str().c_str());
			listitem->set_height(listitem->font()->height() * 2.0f);
			listitem->set_item(item);
			listitem->set_info(item->info());
			
			// preserve previous selection during update
			if (item == selecteditem) {
				menu_listview->select(listitem);
			}
		}
	}
	menu_listview->event_resize();
	menu_inventorytimestamp = core::localcontrol()->inventory()->timestamp();
}

void Inventory::update_selection()
{
}

bool Inventory::verify() const
{
	if (!core::localcontrol() || !core::localcontrol()->inventory()) {
		
	}
	
	if (menu_inventorytimestamp != core::localcontrol()->inventory()->timestamp()) {
		return false;
	}
	
	for (core::Inventory::Items::const_iterator it = core::localcontrol()->inventory()->items().begin(); it != core::localcontrol()->inventory()->items().end(); it++) {
		core::Item *item = (*it);
	
		if (!item->info()) {
			continue;
		}	
		if (item->info()->timestamp() >	menu_infotimestamp) {
			return false;
		}
	}
	return true;
}

void Inventory::resize()
{
	const float smallmargin = ui::UI::elementsize.height();
	const float fontmargin =  ui::root()->font_large()->height();
	
	// resize label
	menu_namelabel->set_size(width() - fontmargin * 2.0f, menu_namelabel->font()->height());	
	menu_namelabel->set_location(fontmargin, fontmargin);
	
	// resize inventory listview
	menu_listview->set_size(ui::UI::elementsize.width(), height() - smallmargin * 2.0f - fontmargin * 6.0f);
	menu_listview->set_location(fontmargin, fontmargin * 3.0f);
	
	menu_inventorytext->set_size(menu_listview->width(), fontmargin * 2.0f);
	menu_inventorytext->set_location(menu_listview->left(),  menu_listview->bottom() + fontmargin);
}

void Inventory::draw()
{
	if (!verify()) {
		update_inventory();	
	}
	
	std::stringstream str;
	str << "Credits: " << std::setw(12) << core::localplayer()->credits();

	if (core::localcontrol() && core::localcontrol()->inventory()) {
		core::Inventory *inventory = core::localcontrol()->inventory();
		
		std::stringstream cargostr;
		cargostr << inventory->capacity_used() << " of " << inventory->capacity();
		
		str << '\n' << "Cargo:   " << aux::pad_left(cargostr.str(),12);
	}
	menu_inventorytext->set_text(str.str());
	
	Window::draw();
}

bool Inventory::on_emit(Widget *sender, const Event event, void *data)
{
	return Window::on_emit(sender, event, data);
}

bool Inventory::on_keypress(const int key, const unsigned int modifier)
{
	switch (key) {

		case SDLK_ESCAPE:
			this->hide();
			return true;
			break;
		default:
			break;
	}

	return Window::on_keypress(key, modifier);
}

	
} // namespace client