Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 0d78025ff09a218c1f2ef0bd61090d7c1fb0d966 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
   client/inventorywindow.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/inventorywindow.h"
#include "ui/label.h"
#include "ui/listitem.h"
#include "ui/paint.h"
#include "ui/ui.h"

#include <iomanip>

namespace client {
	
InventoryWindow::InventoryWindow(ui::Widget *parent) : ui::Window(parent)
{
	inventorywindow_infotimestamp = 0;
	inventorywindow_inventorytimestamp = 0;
	inventorywindow_inforecord = 0;
	inventorywindow_amount = 0;
	
	inventorywindow_titlelabel = new ui::Label(this);
	inventorywindow_titlelabel->set_label("title");
	inventorywindow_titlelabel->set_background(false);
	inventorywindow_titlelabel->set_border(false);
	inventorywindow_titlelabel->set_font(ui::root()->font_large());
	inventorywindow_titlelabel->set_alignment(ui::AlignCenter);
	inventorywindow_titlelabel->set_text("INVENTORY");
	
	// close button
	inventorywindow_closebutton = new ui::IconButton(inventorywindow_titlelabel, "bitmaps/icons/window_close");
	
	inventorywindow_modelview = new ui::ModelView(this);
	inventorywindow_modelview->set_label("modelview");
	inventorywindow_modelview->set_background(false);
	inventorywindow_modelview->set_border(false);
	
	inventorywindow_modeltitlelabel = new ui::Label(inventorywindow_modelview);
	inventorywindow_modeltitlelabel->set_background(false);
	inventorywindow_modeltitlelabel->set_border(false);
	//inventorywindow_modeltitlelabel->set_font(ui::root()->font_large());
	inventorywindow_modeltitlelabel->set_alignment(ui::AlignCenter);
	
	inventorywindow_modelnamelabel = new ui::Label(inventorywindow_modelview);
	inventorywindow_modelnamelabel->set_background(false);
	inventorywindow_modelnamelabel->set_border(false);
	//inventorywindow_modelnamelabel->set_font(ui::root()->font_large());
	inventorywindow_modelnamelabel->set_alignment(ui::AlignCenter);
	
	inventorywindow_listview = new ui::ListView(this);
	inventorywindow_listview->set_label("listview");
	//inventorywindow_listview->set_background(true);
	//inventorywindow_listview->set_border(true);
	
	inventorywindow_inventorytext = new ui::PlainText(this);
	inventorywindow_inventorytext->set_label("inventorytext");
	inventorywindow_inventorytext->set_background(true);
	inventorywindow_inventorytext->set_border(true);
	inventorywindow_inventorytext->set_font(ui::root()->font_small());
	
	inventorywindow_scrollpane = new ui::ScrollPane(this, inventorywindow_infotext);
	inventorywindow_scrollpane->set_background(false);
	inventorywindow_scrollpane->set_border(false);
	inventorywindow_scrollpane->set_alignment(ui::AlignTop);
	
	inventorywindow_shipbutton = new ui::IconButton(this, "bitmaps/icons/button_ship");	
	inventorywindow_ejectbutton = new ui::IconButton(this, "bitmaps/icons/button_eject");
	
	// eject dialog
	inventorywindow_ejectconfirmbutton = new ui::Button(inventorywindow_scrollpane, "Eject");
	inventorywindow_ejectcancelbutton = new ui::Button(inventorywindow_scrollpane, "Cancel");
	inventorywindow_ejectslider = new ui::Slider(inventorywindow_scrollpane);
	
	hide();
}

InventoryWindow::~InventoryWindow()
{
}

void InventoryWindow::toggle()
{
	if (visible())
		hide();
	else
		show();
}
void InventoryWindow::update_inventory()
{
	inventorywindow_listview->clear();
	inventorywindow_inventorytimestamp = 0;
	inventorywindow_infotimestamp = 0;
	
	if (!core::localcontrol() || !core::localcontrol()->inventory()) {
		return;
	}

	const core::Item *selecteditem = (inventorywindow_listview->selected() ? inventorywindow_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() >	inventorywindow_infotimestamp) {
			inventorywindow_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 << '\n' << item->amount();
			}
			
			listitem = new ui::ListItem(inventorywindow_listview, str.str().c_str());
			listitem->set_font(ui::root()->font_tiny());
			listitem->set_height(listitem->font()->height() * 3.0f);
			listitem->set_item(item);
			listitem->set_info(item->info());
			
			std::string sortkey(item->info()->type()->label());
			sortkey += '.';
			sortkey += item->info()->label();
			listitem->set_sortkey(sortkey);
			
			// preserve previous selection during update
			if (item == selecteditem) {
				inventorywindow_listview->select(listitem);
			}
		}
	}
	
	// request ship info
	if (core::localcontrol()->info()) {
		core::game()->request_info(core::localcontrol()->info()->id());
		if (core::localcontrol()->info()->timestamp() > inventorywindow_infotimestamp)
			inventorywindow_infotimestamp = core::localcontrol()->info()->timestamp();
	}
	
	inventorywindow_inventorytimestamp = core::localcontrol()->inventory()->timestamp();

	if (inventorywindow_listview->selected()) {
		set_info(inventorywindow_listview->selected()->info(), inventorywindow_listview->selected()->item()->amount());
	} else if (inventorywindow_inforecord != core::localcontrol()->info()) {
		set_info(0, 0);
	}
	
	inventorywindow_listview->sort();
	inventorywindow_listview->event_resize();
}

bool InventoryWindow::verify() const
{
	if (!core::localcontrol() || !core::localcontrol()->inventory()) {
		return true;	
	}
	
	if (inventorywindow_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() >	inventorywindow_infotimestamp) {
			return false;
		}
	}
	return true;
}

void InventoryWindow::resize()
{
	const float padding =  ui::root()->font_large()->height();
	const float icon_size = 24.0f; // small icons
	
	// resize label
	inventorywindow_titlelabel->set_size(width() - padding * 2.0f, inventorywindow_titlelabel->font()->height());	
	inventorywindow_titlelabel->set_location(padding, padding);
	
	// resize close button
	inventorywindow_closebutton->set_size(inventorywindow_titlelabel->font()->height(), inventorywindow_titlelabel->font()->height());
	inventorywindow_closebutton->set_location(inventorywindow_titlelabel->width() - inventorywindow_closebutton->width(), 0);
	
	// resize inventory listview
	inventorywindow_listview->set_size(ui::UI::elementsize.width(), height() -icon_size - padding * 8.0f);
	inventorywindow_listview->set_location(padding, padding * 6.0f);
	
	inventorywindow_inventorytext->set_size(ui::UI::elementsize.width(), padding * 2.0f);
	inventorywindow_inventorytext->set_location(inventorywindow_listview->left(),  padding * 3.0f);
	
	inventorywindow_shipbutton->set_size(icon_size, icon_size);
	inventorywindow_shipbutton->set_location(inventorywindow_inventorytext->left(), height() - icon_size - padding);
	
	inventorywindow_ejectbutton->set_size(icon_size, icon_size);
	inventorywindow_ejectbutton->set_location(inventorywindow_inventorytext->right() - icon_size, height() - icon_size - padding);
	
	// resize modelview
	inventorywindow_modelview->set_size( width() - inventorywindow_listview->right() - padding * 2.0f ,ui::UI::elementsize.width());
	inventorywindow_modelview->set_location(inventorywindow_listview->right() + padding, padding * 3.0f);
	
	// modelview title label
	inventorywindow_modelnamelabel->set_location(0, 0);
	inventorywindow_modelnamelabel->set_size(inventorywindow_modelview->width(),  inventorywindow_modelnamelabel->font()->height());
	
	// modelview name label
	inventorywindow_modeltitlelabel->set_size(inventorywindow_modelview->width(),  inventorywindow_modeltitlelabel->font()->height());
	inventorywindow_modeltitlelabel->set_location(0, inventorywindow_modelview->height() - inventorywindow_modeltitlelabel->height());
	
	// resize infotext
	inventorywindow_scrollpane->set_size(inventorywindow_modelview->width(), height() - inventorywindow_modelview->bottom() - 2.0f * padding);
	inventorywindow_scrollpane->set_location(inventorywindow_modelview->left(), inventorywindow_modelview->bottom() + padding);

	// eject dialog buttons
	inventorywindow_ejectslider->set_size(
		inventorywindow_scrollpane->width() - ui::UI::elementsize.width(),
		inventorywindow_ejectslider->font()->height()
	);
	inventorywindow_ejectslider->set_location(
		(inventorywindow_scrollpane->width() - inventorywindow_ejectslider->width()) * 0.5f,
		0
	);	
	inventorywindow_ejectconfirmbutton->set_size(ui::UI::elementsize);
	inventorywindow_ejectconfirmbutton->set_location(		
		(inventorywindow_scrollpane->width() - ui::UI::elementsize.width() * 2.0f - padding) * 0.5f,
		inventorywindow_ejectslider->bottom() + padding
	);
	inventorywindow_ejectcancelbutton->set_size(ui::UI::elementsize);
	inventorywindow_ejectcancelbutton->set_location(		
		inventorywindow_ejectconfirmbutton->right() + padding,
		inventorywindow_ejectslider->bottom() + padding
	);
	
}

void InventoryWindow::draw()
{
	if (!verify()) {
		update_inventory();
	}
	
	if (inventorywindow_inforecord && (inventorywindow_inforecord->timestamp() > inventorywindow_infotimestamp)) {
		set_info(inventorywindow_inforecord, inventorywindow_amount);
	}
	
	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);
	}
	inventorywindow_inventorytext->set_text(str.str());
	
	Window::draw();
}

void InventoryWindow::show()
{
	inventorywindow_listview->deselect();
	set_info(core::localcontrol() ? core::localcontrol()->info() : 0, 0);
	inventorywindow_modeltitlelabel->set_text("Ship Info");
	Window::show();
}

void InventoryWindow::show_eject_dialog(const core::Info *info, const int amount)
{
	if (!info || amount < 0)
		return;
	
	inventorywindow_modeltitlelabel->set_text("Eject Cargo");
	inventorywindow_infotext.clear();
	
	inventorywindow_ejectconfirmbutton->show();
	inventorywindow_ejectcancelbutton->show();
	
	inventorywindow_ejectslider->set_range(1, amount);
	inventorywindow_ejectslider->set_value(amount);
	inventorywindow_ejectslider->show();
	show_eject_amount(amount);
	
	//std::string text("Select amount to eject");
	//inventorywindow_infotext.push_back(text);
	
}

void InventoryWindow::show_eject_amount(const int amount)
{
	std::ostringstream str;
	str << "Eject Cargo";
	
	if (amount > 0) {
		str << " (";
		str << inventorywindow_ejectslider->value();
		str << ")";
	}
	inventorywindow_modeltitlelabel->set_text(str.str());
}

void InventoryWindow::act_eject()
{
	if (!inventorywindow_listview->selected()) {
		return;
	}
	if (!inventorywindow_listview->selected()->item()) {
		return;
	}
	
	std::ostringstream cmdstr;
	
	cmdstr << "remote eject ";
	cmdstr << inventorywindow_listview->selected()->item()->info()->type()->label() << " ";
	cmdstr << inventorywindow_listview->selected()->item()->info()->label() << " ";
	cmdstr << inventorywindow_ejectslider->value();
	
	con_debug "Executing command '" << cmdstr.str() << "'" << std::endl;
	core::CommandBuffer::exec(cmdstr.str());
	
	show_item_info(0);
}

void InventoryWindow::show_item_info(const ui::ListItem *listitem)
{
	if (listitem) {
		set_info(listitem->info(), listitem->item()->amount());
		if (listitem->item()->amount()) {
			inventorywindow_ejectbutton->enable();
		}
	} else {
		set_info(0, 0);
	}
}

void InventoryWindow::set_info(const core::Info *info, const int amount)
{
	inventorywindow_inforecord = info;
	inventorywindow_infotext.clear();
	inventorywindow_amount = amount;
	inventorywindow_ejectbutton->disable();
	
	inventorywindow_ejectconfirmbutton->hide();
	inventorywindow_ejectcancelbutton->hide();
	inventorywindow_ejectslider->hide();
	
	if (!info) {
		inventorywindow_modelview->set_modelname(0);
		inventorywindow_modeltitlelabel->set_text("");
		inventorywindow_modelnamelabel->set_text("");
	} else {
		core::game()->request_info(info->id());
		inventorywindow_modelview->set_modelname(info->modelname());
		inventorywindow_modelnamelabel->set_text(info->name());
		inventorywindow_modelview->set_colors(core::localplayer()->color(), core::localplayer()->color_second());
		
		for (core::Info::Text::const_iterator it = inventorywindow_inforecord->text().begin(); it != inventorywindow_inforecord->text().end(); it++) {
			inventorywindow_infotext.push_back((*it));
		}
		if (info->timestamp() > inventorywindow_infotimestamp) {
			inventorywindow_infotimestamp = info->timestamp();
		}
	}
	
}

bool InventoryWindow::on_emit(Widget *sender, const Event event, void *data)
{
	if (event == ui::Widget::EventButtonClicked) {
	
		if (sender == inventorywindow_shipbutton) {
			inventorywindow_listview->deselect();
			set_info(core::localcontrol() ? core::localcontrol()->info() : 0, 0);
			inventorywindow_modeltitlelabel->set_text("Ship Info");
		} else if (sender == inventorywindow_ejectbutton) {
			if (inventorywindow_listview->selected()) {
				show_eject_dialog(inventorywindow_listview->selected()->info(), inventorywindow_listview->selected()->item()->amount());
			}
		} else if (sender == inventorywindow_ejectcancelbutton) {
			show_item_info(inventorywindow_listview->selected());
		} else if (sender == inventorywindow_ejectconfirmbutton) {
			// TODO do actual eject
			act_eject();
		} else if (sender == inventorywindow_closebutton) {
			hide();
		}
		
		return true;
	
	} else if (event == ui::Widget::EventListViewChanged) {
		
		if (sender == inventorywindow_listview) {
			inventorywindow_modeltitlelabel->set_text("Cargo Info");
			show_item_info(inventorywindow_listview->selected());
		}
		
		return true;
		
	} else if (event == ui::Widget::EventSliderChanged) {
		
		if (sender == inventorywindow_ejectslider) {
			show_eject_amount(inventorywindow_ejectslider->value());
		}
		return true;
	}
	
	return Window::on_emit(sender, event, data);
}

bool InventoryWindow::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