From a6f9773c358dd7d091ff64cbda504ab8d8066dd3 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Wed, 22 Sep 2010 21:32:34 +0000 Subject: full trading support for networked games --- src/client/inventorylistview.cc | 81 +++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 24 deletions(-) (limited to 'src/client/inventorylistview.cc') diff --git a/src/client/inventorylistview.cc b/src/client/inventorylistview.cc index d87ed2b..e2b613b 100644 --- a/src/client/inventorylistview.cc +++ b/src/client/inventorylistview.cc @@ -4,14 +4,20 @@ the terms and conditions of the GNU General Public License version 2 */ +#include "core/application.h" #include "client/inventorylistview.h" #include "ui/listitem.h" +#include "ui/paint.h" namespace client { InventoryListView::InventoryListView(ui::Widget *parent) : ui::ListView (parent) { + listview_inventory = 0; + listview_infotype = 0; listview_timestamp = 0; + listview_infotimestamp = 0; + set_inventory(0, 0); } @@ -22,55 +28,82 @@ void InventoryListView::set_inventory(core::Inventory *inventory, core::InfoType { remove_children(); + deselect(); + + listview_timestamp = 0; + listview_infotimestamp = 0; listview_inventory = inventory; listview_infotype = infotype; - - const core::Item *selecteditem = (selected() ? selected()->item() : 0); - - if (!inventory || !infotype) { + + if (!listview_inventory || !listview_infotype) { return; } - // TODO scan the inventories and request updated infos - // update when necessary + const core::Item *selecteditem = (selected() ? selected()->item() : 0); + listview_timestamp = inventory->timestamp(); for (core::Inventory::Items::const_iterator it = inventory->items().begin(); it != inventory->items().end(); it++) { core::Item *item = (*it); - if (item->info() && (item->info()->type() == infotype) && (item->amount() != 0) ) { - ui::ListItem *listitem = 0; + if (item->info()) { - std::ostringstream str; - str << item->info()->name().c_str(); + // this makes sure inventory info gets requested from the server + core::game()->request_info(item->info()->id()); - if (item->amount() > 0) { - str << " (" << item->amount() << ")"; + if (item->info()->timestamp() > listview_infotimestamp) { + listview_infotimestamp = item->info()->timestamp(); } - listitem = new ui::ListItem(this, 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) { - ui::ListView::select(listitem); + + if ((item->info()->type() == infotype) && (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(this, 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) { + ui::ListView::select(listitem); + } } } } - listview_timestamp = inventory->timestamp(); - event_resize(); emit(EventListViewChanged); } +bool InventoryListView::verify_inventory() +{ + for (core::Inventory::Items::const_iterator it = listview_inventory->items().begin(); it != listview_inventory->items().end(); it++) { + core::Item *item = (*it); + if (item->info() && (item->info()->timestamp() > listview_infotimestamp)) { + return false; + } + } + return true; +} + void InventoryListView::draw() { - if (listview_inventory && (listview_timestamp != listview_inventory->timestamp())) { - set_inventory(listview_inventory, listview_infotype); + if (listview_inventory && listview_infotype) { + // inventory was updated + if (listview_timestamp != listview_inventory->timestamp()) { + set_inventory(listview_inventory, listview_infotype); + // inventory info was updated + } else if (!verify_inventory()) { + set_inventory(listview_inventory, listview_infotype); + } } ListView::draw(); } -} // namespace client \ No newline at end of file +} // namespace client -- cgit v1.2.3