/* client/trademenu.cc This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ #include #include "core/application.h" #include "core/info.h" #include "core/inventory.h" #include "ui/button.h" #include "ui/paint.h" #include "ui/ui.h" #include "ui/listitem.h" #include "client/trademenu.h" namespace client { TradeMenu::TradeMenu(ui::Widget *parent, const char * label) : ui::Window(parent) { set_border(false); set_background(false); if (label) set_label(label); else set_label("trademenu"); menu_tradewindow = new ui::Window(this); menu_tradewindow->set_label("tradewindow"); menu_tradewindow->set_border(true); menu_modelview = new ui::ModelView(menu_tradewindow); menu_modelview->set_label("modelview"); menu_modelview->set_background(false); menu_modelview->set_border(false); menu_namelabel = new ui::Label(menu_tradewindow); 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->show(); menu_inventorylistview = new InventoryListView(menu_tradewindow); menu_inventorylistview->set_label("inventorylistview"); menu_inventorylistview->set_background(false); menu_inventorylistview->set_border(true); menu_inventorytext = new ui::PlainText(menu_tradewindow); menu_inventorytext->set_label("inventorytext"); menu_inventorytext->set_background(false); menu_inventorytext->set_border(false); menu_inventorytext->set_font(ui::root()->font_small()); menu_scrollpane = new ui::ScrollPane(menu_tradewindow, menu_infotext); menu_scrollpane->set_background(false); menu_scrollpane->set_border(false); menu_scrollpane->set_alignment(ui::AlignTop); menu_traderlistview = new InventoryListView(menu_tradewindow); menu_traderlistview->set_label("traderlistview"); menu_traderlistview->set_background(false); menu_traderlistview->set_border(true); menu_tradertext = new ui::PlainText(menu_tradewindow); menu_tradertext->set_label("tradertext"); menu_tradertext->set_background(false); menu_tradertext->set_border(false); menu_tradertext->set_font(ui::root()->font_small()); menu_closebutton = new ui::Button(menu_tradewindow, "Return", "view hide"); menu_buyallbutton = new ui::Button(menu_modelview, "<<"); menu_buybutton = new ui::Button(menu_modelview, "<"); menu_sellallbutton = new ui::Button(menu_modelview, ">>"); menu_sellbutton = new ui::Button(menu_modelview, ">"); std::string test("test"); set_item_type(0); hide(); } TradeMenu::~TradeMenu() { } void TradeMenu::set_item_type(core::InfoType *item_type) { // reset menu_namelabel->set_text(0); core::Inventory *inventory_player = 0; core::Inventory *inventory_view = 0; if (core::localcontrol()) inventory_player = core::localcontrol()->inventory(); if(core::localplayer()->view()) inventory_view = core::localplayer()->view()->inventory(); menu_inventorylistview->set_inventory(inventory_player, item_type); menu_traderlistview->set_inventory(inventory_view, item_type); set_item(0); } void TradeMenu::set_item(ui::ListItem *item) { menu_infotext.clear(); menu_namelabel->set_text("Trade"); menu_tradertext->clear(); if (!item || !item->info()) { menu_traderlistview->deselect(); menu_inventorylistview->deselect(); menu_scrollpane->hide(); menu_modelview->hide(); return; } if (item->parent() == menu_inventorylistview) { menu_traderlistview->deselect(); // item in ship inventory selected (SELL) menu_namelabel->set_text("Sell " + item->text()); menu_buyallbutton->hide(); menu_buybutton->hide(); menu_sellallbutton->show(); menu_sellallbutton->set_command("remote sell " + item->info()->type()->label() + " " + item->info()->label() + " -1"); menu_sellbutton->show(); menu_sellbutton->set_command("remote sell " + item->info()->type()->label() + " " + item->info()->label() + " 1"); } else if (item->parent() == menu_traderlistview) { menu_inventorylistview->deselect(); // item in trader inventory selected (BUY) menu_namelabel->set_text("Buy " + item->text()); menu_buyallbutton->show(); menu_buyallbutton->set_command("remote buy " + item->info()->type()->label() + " " + item->info()->label() + " -1"); menu_buybutton->show(); menu_buybutton->set_command("remote buy " + item->info()->type()->label() + " " + item->info()->label() + " 1; "); std::ostringstream str; str << "Price: " << std::setw(8) << item->item()->price() << '\n' << "Volume: " << std::setw(7) << std::setprecision(2) << item->info()->volume(); menu_tradertext->set_text(str.str()); menu_sellallbutton->hide(); menu_sellbutton->hide(); } else { menu_traderlistview->deselect(); menu_inventorylistview->deselect(); menu_buyallbutton->hide(); menu_buybutton->hide(); menu_sellallbutton->hide(); menu_sellbutton->hide(); } if (item->info()) { for (core::Info::Text::const_iterator it = item->info()->text().begin(); it != item->info()->text().end(); it++) { menu_infotext.push_back((*it)); } menu_scrollpane->show(); menu_modelview->set_modelname(item->info()->modelname()); menu_modelview->show(); } } void TradeMenu::resize() { const float smallmargin = ui::UI::elementsize.height(); const float fontmargin = ui::root()->font_large()->height(); // this menu takes the entire screen set_size(parent()->size()); // resize the subwindow menu_tradewindow->set_size(width() - smallmargin * 2.0f, height()- smallmargin * 4.0f); menu_tradewindow->set_location(smallmargin, smallmargin * 2.0f); // resize label menu_namelabel->set_size(menu_tradewindow->width() - fontmargin * 2.0f, menu_namelabel->font()->height()); menu_namelabel->set_location(fontmargin, fontmargin); // resize inventory listview menu_inventorylistview->set_size(ui::UI::elementsize.width(), menu_tradewindow->height() - smallmargin * 2.0f - fontmargin * 6.0f); menu_inventorylistview->set_location(fontmargin, fontmargin * 3.0f); menu_inventorytext->set_size(menu_inventorylistview->width(), fontmargin); menu_inventorytext->set_location(menu_inventorylistview->left(), menu_inventorylistview->bottom() + fontmargin); // resize modelview menu_modelview->set_size(menu_tradewindow->width() - 2.0f * ui::UI::elementsize.width() - fontmargin * 4.0f, ui::UI::elementsize.width()); menu_modelview->set_location(fontmargin * 2.0f + ui::UI::elementsize.width(), fontmargin * 3.0f); // resize buttons menu_buyallbutton->set_size(fontmargin, fontmargin); menu_buyallbutton->set_location(0, menu_modelview->height() - fontmargin); menu_buybutton->set_size(fontmargin, fontmargin); menu_buybutton->set_location(2.0f * fontmargin, menu_modelview->height() - fontmargin); menu_sellallbutton->set_size(fontmargin, fontmargin); menu_sellallbutton->set_location(menu_modelview->width() - fontmargin, menu_modelview->height() - fontmargin); menu_sellbutton->set_size(fontmargin, fontmargin); menu_sellbutton->set_location(menu_modelview->width() - 3.0f * fontmargin, menu_modelview->height() - fontmargin); // resize scrollpane menu_scrollpane->set_size(menu_tradewindow->width() - 2.0f * ui::UI::elementsize.width() - fontmargin * 4.0f, menu_inventorylistview->height() + 2.0f * fontmargin - ui::UI::elementsize.width()); menu_scrollpane->set_location(fontmargin * 2.0f + ui::UI::elementsize.width(), fontmargin * 4.0f + ui::UI::elementsize.width()); // resize trader listview menu_traderlistview->set_size(ui::UI::elementsize.width(), menu_tradewindow->height() - smallmargin * 2.0f - fontmargin * 6.0f); menu_traderlistview->set_location(menu_tradewindow->width() - menu_traderlistview->width() - fontmargin, fontmargin * 3.0f); menu_tradertext->set_size(menu_traderlistview->width(), fontmargin); menu_tradertext->set_location(menu_traderlistview->left(), menu_traderlistview->bottom() + fontmargin); // resize close button menu_closebutton->set_size(ui::UI::elementsize); menu_closebutton->set_location(0.5f * (menu_tradewindow->width() - ui::UI::elementsize.width()), menu_tradewindow->height() - smallmargin * 1.5f ); } bool TradeMenu::on_emit(Widget *sender, const Event event, void *data) { if (event == ui::Widget::EventListItemClicked) { set_item(static_cast(sender)); return true; } return ui::Window::on_emit(sender, event, data); } void TradeMenu::draw() { std::stringstream str; str << "Credit: " << std::setw(8) << core::localplayer()->credits(); if (core::localcontrol() && core::localcontrol()->inventory()) { core::Inventory *inventory = core::localcontrol()->inventory(); str << '\n'; str << "Cargo: " << std::setprecision(2) << std::setw(6) << inventory->capacity_used() << " of " << inventory->capacity(); } menu_inventorytext->set_text(str.str()); Window::draw(); } } // namespace client