From 25af16fac3127523d532d4b4797eec7947ed5a72 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Fri, 1 Oct 2010 22:07:45 +0000 Subject: Initial inventory window --- src/client/Makefile.am | 4 +- src/client/client.cc | 10 +++ src/client/client.h | 1 + src/client/inventory.cc | 191 ++++++++++++++++++++++++++++++++++++++++ src/client/inventory.h | 57 ++++++++++++ src/client/inventorylistview.cc | 33 +++---- src/client/inventorylistview.h | 2 +- src/client/inventorymenu.cc | 19 ---- src/client/inventorymenu.h | 26 ------ src/client/keyboard.cc | 2 +- src/client/playerview.cc | 64 +++++++++++--- src/client/playerview.h | 8 ++ src/client/worldview.cc | 10 ++- src/client/worldview.h | 1 + 14 files changed, 343 insertions(+), 85 deletions(-) create mode 100644 src/client/inventory.cc create mode 100644 src/client/inventory.h delete mode 100644 src/client/inventorymenu.cc delete mode 100644 src/client/inventorymenu.h (limited to 'src') diff --git a/src/client/Makefile.am b/src/client/Makefile.am index ddc78a6..8edef84 100644 --- a/src/client/Makefile.am +++ b/src/client/Makefile.am @@ -8,7 +8,7 @@ noinst_LTLIBRARIES = libclient.la endif libclient_la_SOURCES = action.cc buymenu.cc chat.cc client.cc clientext.cc \ - entitymenu.cc hud.cc infowidget.cc input.cc inventorylistview.cc inventorymenu.cc joystick.cc key.cc \ + entitymenu.cc hud.cc infowidget.cc input.cc inventorylistview.cc inventory.cc joystick.cc key.cc \ keyboard.cc map.cc notifications.cc playerview.cc soundext.cc targeticonbutton.cc \ targets.cc trademenu.cc video.cc worldview.cc @@ -17,7 +17,7 @@ libclient_la_CFLAGS = $(LIBSDL_CFLAGS) $(GL_CFLAGS) libclient_la_LDFLAGS = -avoid-version -no-undefined $(GL_LIBS) $(LIBSDL_LIBS) noinst_HEADERS = action.h chat.h client.h clientext.h hud.h entitymenu.h \ - input.h inventorylistview.h inventorymenu.h joystick.h key.h keyboard.h map.h notifications.h soundext.h \ + input.h inventorylistview.h inventory.h joystick.h key.h keyboard.h map.h notifications.h soundext.h \ targets.h video.h infowidget.h playerview.h worldview.h trademenu.h buymenu.h \ targeticonbutton.h diff --git a/src/client/client.cc b/src/client/client.cc index 5bf783a..a40023b 100644 --- a/src/client/client.cc +++ b/src/client/client.cc @@ -144,6 +144,9 @@ void Client::init(int count, char **arguments) func = core::Func::add("ui_chatbar", Client::func_ui_chatbar); func->set_info("toggle chat bar"); + + func = core::Func::add("ui_inventory", Client::func_ui_inventory); + func->set_info("toggle inventory"); func = core::Func::add("ui_map", Client::func_ui_map); func->set_info("toggle map"); @@ -488,6 +491,13 @@ void Client::func_ui_chatbar(std::string const &args) } } +void Client::func_ui_inventory(std::string const &args) +{ + if (client()->connected() && client()->worldview()->playerview()->visible()) { + client()->worldview()->playerview()->toggle_inventory(); + } +} + void Client::func_ui_map(std::string const &args) { if (client()->connected() && client()->worldview()->playerview()->visible()) { diff --git a/src/client/client.h b/src/client/client.h index 2de731d..288e9fa 100644 --- a/src/client/client.h +++ b/src/client/client.h @@ -85,6 +85,7 @@ private: static void func_ui(std::string const &args); static void func_ui_chat(std::string const &args); static void func_ui_chatbar(std::string const &args); + static void func_ui_inventory(std::string const &args); static void func_ui_map(std::string const &args); static void func_ui_menu(std::string const &args); diff --git a/src/client/inventory.cc b/src/client/inventory.cc new file mode 100644 index 0000000..5837a17 --- /dev/null +++ b/src/client/inventory.cc @@ -0,0 +1,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 + +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 diff --git a/src/client/inventory.h b/src/client/inventory.h new file mode 100644 index 0000000..f66ec56 --- /dev/null +++ b/src/client/inventory.h @@ -0,0 +1,57 @@ +/* + client/inventory.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_CLIENT_INVENTORY_H__ +#define __INCLUDED_CLIENT_INVENTORY_H__ + +#include "core/info.h" +#include "core/inventory.h" +#include "ui/listview.h" +#include "ui/plaintext.h" +#include "ui/window.h" + +namespace client { + +/** + * @brief an inventory window widget + */ +class Inventory : public ui::Window +{ +public: + Inventory(ui::Widget *parent = 0); + ~Inventory(); + + void toggle(); +protected: + /// resize event handler + virtual void resize(); + + /// draw event handler + virtual void draw(); + + /// emit event handler + virtual bool on_emit(Widget *sender, const Event event, void *data); + + /// keypress event handler + virtual bool on_keypress(const int key, const unsigned int modifier); + +private: + void update_inventory(); + + void update_selection(); + + bool verify() const; + + unsigned long menu_inventorytimestamp; + unsigned long menu_infotimestamp; + ui::ListView *menu_listview; + ui::Label *menu_namelabel; + ui::PlainText *menu_inventorytext; + +}; // class Inventory + +} +#endif // __INCLUDED_CLIENT_INVENTORY_H__ diff --git a/src/client/inventorylistview.cc b/src/client/inventorylistview.cc index 87cc367..a8d98f3 100644 --- a/src/client/inventorylistview.cc +++ b/src/client/inventorylistview.cc @@ -80,8 +80,16 @@ void InventoryListView::set_inventory(core::Inventory *inventory, core::InfoType emit(EventListViewChanged); } -bool InventoryListView::verify_inventory() +bool InventoryListView::verify() const { + if (!listview_inventory || !listview_infotype) { + return true; + } + + if (listview_timestamp != listview_inventory->timestamp()) { + return false; + } + 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)) { @@ -93,28 +101,9 @@ bool InventoryListView::verify_inventory() void InventoryListView::draw() { - if (listview_inventory && listview_infotype) { - // inventory was updated - if (listview_timestamp != listview_inventory->timestamp()) { - //con_debug << "CLIENT inventory update from " << listview_timestamp << " to " << listview_inventory->timestamp() << std::endl; - set_inventory(listview_inventory, listview_infotype); - // inventory info was updated - } else if (!verify_inventory()) { - //con_debug << "CLIENT inventory info update" << std::endl; - set_inventory(listview_inventory, listview_infotype); - } - } - - /* - * DEBUG - std::ostringstream str; - if (listview_inventory && listview_infotype) { - str << listview_timestamp << " " << listview_inventory->timestamp() << " " << listview_infotimestamp; - } else { - str << "EMPTY"; + if (!verify()) { + set_inventory(listview_inventory, listview_infotype); } - ui::Paint::draw_label(global_location(), size(), font(), str.str() , ui::AlignBottom) ; - */ ListView::draw(); } diff --git a/src/client/inventorylistview.h b/src/client/inventorylistview.h index 5d5a85f..1688560 100644 --- a/src/client/inventorylistview.h +++ b/src/client/inventorylistview.h @@ -30,7 +30,7 @@ protected: virtual void draw(); private: - bool verify_inventory(); + bool verify() const; unsigned long listview_timestamp; unsigned long listview_infotimestamp; diff --git a/src/client/inventorymenu.cc b/src/client/inventorymenu.cc deleted file mode 100644 index 435c3b2..0000000 --- a/src/client/inventorymenu.cc +++ /dev/null @@ -1,19 +0,0 @@ -/* - client/inventorymenu.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 "client/inventorymenu.h" - -namespace client { - -InventoryWindow::InventoryWindow(ui::Widget *parent) : ui::Window(parent) -{ -} - -InventoryWindow::~InventoryWindow() -{ -} - -} // namespace client diff --git a/src/client/inventorymenu.h b/src/client/inventorymenu.h deleted file mode 100644 index 7d4809a..0000000 --- a/src/client/inventorymenu.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - client/inventorymenu.h - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -#ifndef __INCLUDED_CLIENT_INVENTORYWINDOW_H__ -#define __INCLUDED_CLIENT_INVENTORYWINDOW_H__ - -#include "ui/window.h" - -namespace client { - -/** - * @brief an inventory window widget - */ -class InventoryWindow : public ui::Window -{ -public: - InventoryWindow(ui::Widget *parent = 0); - ~InventoryWindow(); - -}; // class InventoryWindow - -} -#endif // __INCLUDED_CLIENT_INVENTORYWINDOW_H__ diff --git a/src/client/keyboard.cc b/src/client/keyboard.cc index f3d2181..e617c59 100644 --- a/src/client/keyboard.cc +++ b/src/client/keyboard.cc @@ -180,7 +180,7 @@ Keyboard::Keyboard() #ifdef _WIN32 key->assign(Key::Alt, "quit"); #endif - add_key("f5", SDLK_F5); + add_key("f5", SDLK_F5, 0, "ui_inventory"); add_key("f6", SDLK_F6, 0, "ui_map"); add_key("f7", SDLK_F7); add_key("f8", SDLK_F8); diff --git a/src/client/playerview.cc b/src/client/playerview.cc index 7791a2a..159371c 100644 --- a/src/client/playerview.cc +++ b/src/client/playerview.cc @@ -36,6 +36,7 @@ PlayerView::PlayerView(ui::Widget *parent) : ui::Widget(parent) view_entitymenu = new EntityMenu(this); view_buymenu = new BuyMenu(this); view_trademenu = new TradeMenu(this); + view_inventory = new Inventory(this); //view_hud->set_focus(); //view_hud->raise(); @@ -68,11 +69,17 @@ void PlayerView::toggle_map() { if (!map()->visible()) { - if (chat()->visible() && !chat()->small_view()) + if (chat()->visible() && !chat()->small_view()) { chat()->hide(); + } - if (view_entitymenu->visible()) + if (inventory()->visible()) { + inventory()->hide(); + } + + if (view_entitymenu->visible()) { view_entitymenu->hide(); + } } map()->toggle(); @@ -83,11 +90,40 @@ void PlayerView::toggle_map() } } +void PlayerView::toggle_inventory() +{ + + if (!inventory()->visible()) { + if (chat()->visible() && !chat()->small_view()) { + chat()->hide(); + } + + if (map()->visible()) { + map()->hide(); + } + + if (view_entitymenu->visible()) { + view_entitymenu->hide(); + } + } + + inventory()->toggle(); + audio::play("ui/menu"); + + if (inventory()->visible() && chat()->visible() && chat()->small_view()) { + chat()->raise(); + } +} + void PlayerView::toggle_chat() { if (!chat()->visible()) { if (map()->visible()) map()->hide(); + + if (inventory()->visible()) { + inventory()->hide(); + } if (view_entitymenu->visible()) view_entitymenu->hide(); @@ -189,7 +225,11 @@ void PlayerView::resize() // reposition map view_map->set_size(width() - smallmargin * 2, height() - smallmargin * 4); view_map->set_location(smallmargin, smallmargin * 2); - + + // reposition inventory + view_inventory->set_size(width() - smallmargin * 2, height() - smallmargin * 4); + view_inventory->set_location(smallmargin, smallmargin * 2); + // reposition notifications view_notify->set_geometry(view_map->location(), view_map->size()); @@ -228,7 +268,7 @@ void PlayerView::draw() audio::play("ui/menu"); } else if (!view_entitymenu->visible() && !view_buymenu->visible() && !view_trademenu->visible() && - !map()->visible() && (!chat()->visible() || chat()->small_view())) { + !inventory()->visible() && !map()->visible() && (!chat()->visible() || chat()->small_view())) { // show the menu if there's no other window open view_entitymenu->show(); @@ -270,7 +310,7 @@ void PlayerView::draw() label_viewname->hide(); } - if (!map()->visible() && !chat()->visible()) { + if (!map()->visible() && !chat()->visible() && !inventory()->visible()) { view_hud->set_focus(); } @@ -278,13 +318,15 @@ void PlayerView::draw() } // reposition chat widget - if (view_chat->small_view()) { - view_chat->set_size(width() - smallmargin * 2, font()->height() * 2); - view_chat->set_location(smallmargin, height() - smallmargin *2 - view_chat->height()); - } else { - view_chat->set_geometry(view_map->location(), view_map->size()); + if (chat()->visible()) { + if (view_chat->small_view()) { + view_chat->set_size(width() - smallmargin * 2, font()->height() * 2); + view_chat->set_location(smallmargin, height() - smallmargin *2 - view_chat->height()); + } else { + view_chat->set_geometry(view_map->location(), view_map->size()); + } + view_chat->event_resize(); } - view_chat->event_resize(); } } diff --git a/src/client/playerview.h b/src/client/playerview.h index 300e9e5..66672cf 100644 --- a/src/client/playerview.h +++ b/src/client/playerview.h @@ -10,6 +10,7 @@ #include "client/chat.h" #include "client/buymenu.h" #include "client/entitymenu.h" +#include "client/inventory.h" #include "client/trademenu.h" #include "client/hud.h" #include "client/map.h" @@ -33,6 +34,9 @@ public: /// toggle map window void toggle_map(); + + /// toggle inventory window + void toggle_inventory(); /// togge chat window void toggle_chat(); @@ -52,6 +56,9 @@ public: inline Chat *chat() { return view_chat; } + inline Inventory *inventory() { + return view_inventory; + } inline Notifications *notify() { return view_notify; } @@ -68,6 +75,7 @@ private: HUD *view_hud; Chat *view_chat; Map *view_map; + Inventory *view_inventory; EntityMenu *view_entitymenu; BuyMenu *view_buymenu; diff --git a/src/client/worldview.cc b/src/client/worldview.cc index 684e08e..265277f 100644 --- a/src/client/worldview.cc +++ b/src/client/worldview.cc @@ -36,6 +36,7 @@ WorldView::WorldView(ui::Widget *parent) : ui::Widget(parent) view_chatbutton = new ui::IconButton(this, "bitmaps/icons/button_chat", "ui_chat"); view_mapbutton = new ui::IconButton(this, "bitmaps/icons/button_map", "ui_map"); + view_inventorybutton = new ui::IconButton(this, "bitmaps/icons/button_inventory", "ui_inventory"); } WorldView::~WorldView() @@ -69,7 +70,7 @@ void WorldView::resize() // icons const float icon_margin = 4.0f; const float icon_size = 48.0f; - const float icon_count = 6; + const float icon_count = 7; const float l = (width() - ((icon_count + 1) * icon_margin) - (icon_count * icon_size)) * 0.5f; view_menubutton->set_geometry(l, icon_margin, icon_size, icon_size); @@ -77,8 +78,9 @@ void WorldView::resize() view_dockbutton->set_geometry(l + 2.0f *(icon_margin + icon_size), icon_margin, icon_size, icon_size); view_launchbutton->set_geometry(l + 2.0f *(icon_margin + icon_size), icon_margin, icon_size, icon_size); // spacer - view_chatbutton->set_geometry(l + 4.0f *(icon_margin + icon_size), icon_margin, icon_size, icon_size); - view_mapbutton->set_geometry(l + 5.0f *(icon_margin + icon_size), icon_margin, icon_size, icon_size); + view_inventorybutton->set_geometry(l + 4.0f *(icon_margin + icon_size), icon_margin, icon_size, icon_size); + view_chatbutton->set_geometry(l + 5.0f *(icon_margin + icon_size), icon_margin, icon_size, icon_size); + view_mapbutton->set_geometry(l + 6.0f *(icon_margin + icon_size), icon_margin, icon_size, icon_size); } void WorldView::clear() @@ -107,6 +109,7 @@ void WorldView::draw() view_launchbutton->hide(); view_chatbutton->hide(); view_mapbutton->hide(); + view_inventorybutton->hide(); } else { view_playerview->show(); view_playerview->set_focus(); @@ -122,6 +125,7 @@ void WorldView::draw() view_menubutton->show(); view_chatbutton->show(); view_mapbutton->show(); + view_inventorybutton->show(); } } diff --git a/src/client/worldview.h b/src/client/worldview.h index 11473f8..266d3eb 100644 --- a/src/client/worldview.h +++ b/src/client/worldview.h @@ -48,6 +48,7 @@ private: ui::IconButton *view_chatbutton; ui::IconButton *view_mapbutton; + ui::IconButton *view_inventorybutton; }; } -- cgit v1.2.3