/* client/gamewindow.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 #include #include "core/info.h" #include "core/application.h" #include "ui/ui.h" #include "ui/iconbutton.h" #include "client/targeticonbutton.h" #include "client/gamewindow.h" #include "client/hud.h" #include "client/chat.h" #include "client/inventorywindow.h" #include "client/mapwindow.h" #include "client/reputationwindow.h" #include "client/buymenu.h" #include "client/entitymenu.h" #include "client/trademenu.h" namespace client { GameWindow::GameWindow(ui::Widget *parent) : ui::Window(parent) { set_label("gamewindow"); set_border(false); set_background(false); gamewindow_hud = new HUD(this); //label_viewname = new ui::Label(this); //label_viewname->set_label("viewnamelabel"); //label_viewname->set_alignment(ui::AlignCenter); //label_viewname->set_background(true); //label_viewname->set_font(ui::root()->font_large()); // sub menus gamewindow_map = new MapWindow(this); gamewindow_entitymenu = new EntityMenu(this, "entitymenu"); gamewindow_buymenu = new BuyMenu(this); gamewindow_trademenu = new TradeMenu(this); gamewindow_inventory = new InventoryWindow(this); gamewindow_reputation = new ReputationWindow(this); gamewindow_chat = new Chat(this); // icon buttons gamewindow_menubutton = new ui::IconButton(this, "bitmaps/icons/button_menu", "ui_menu"); gamewindow_launchbutton = new ui::IconButton(this, "bitmaps/icons/button_launch", "launch"); gamewindow_freeflightbutton = new ui::IconButton(this, "bitmaps/icons/button_freeflight", "freeflight"); gamewindow_gotobutton = new TargetIconButton(this, "bitmaps/icons/button_goto", "goto"); gamewindow_dockbutton = new TargetIconButton(this, "bitmaps/icons/button_dock", "dock", core::Entity::Dockable); gamewindow_formationbutton = new TargetIconButton(this, "bitmaps/icons/button_formation", "formation"); gamewindow_homebutton = new ui::IconButton(this, "bitmaps/icons/button_home", "view main"); gamewindow_chatbutton = new ui::IconButton(this, "bitmaps/icons/button_chat", "ui_chat"); gamewindow_mapbutton = new ui::IconButton(this, "bitmaps/icons/button_map", "ui_map"); gamewindow_inventorybutton = new ui::IconButton(this, "bitmaps/icons/button_ship", "ui_inventory"); gamewindow_reputationbutton = new ui::IconButton(this, "bitmaps/icons/button_reputation", "ui_reputation"); } GameWindow::~GameWindow() { } void GameWindow::clear() { gamewindow_chat->clear(); gamewindow_chat->hide(); gamewindow_map->hide(); gamewindow_inventory->hide(); gamewindow_reputation->hide(); gamewindow_entitymenu->hide(); gamewindow_buymenu->hide(); gamewindow_trademenu->hide(); } void GameWindow::event_text(const std::string & text) { gamewindow_chat->event_text(text); } void GameWindow::toggle_map() { if (!gamewindow_map->visible()) { if (gamewindow_chat->visible() && !gamewindow_chat->small_view()) { gamewindow_chat->hide(); } if (gamewindow_inventory->visible()) { gamewindow_inventory->hide(); } if (gamewindow_entitymenu->visible()) { gamewindow_entitymenu->hide(); } if (gamewindow_buymenu->visible()) { gamewindow_buymenu->hide(); } if (gamewindow_trademenu->visible()) { gamewindow_trademenu->hide(); } if (gamewindow_reputation->visible()) { gamewindow_reputation->hide(); } gamewindow_map->show(); } else { gamewindow_map->hide(); } } void GameWindow::toggle_inventory() { if (!gamewindow_inventory->visible()) { if (gamewindow_chat->visible() && !gamewindow_chat->small_view()) { gamewindow_chat->hide(); } if (gamewindow_map->visible()) { gamewindow_map->hide(); } if (gamewindow_reputation->visible()) { gamewindow_reputation->hide(); } if (gamewindow_entitymenu->visible()) { gamewindow_entitymenu->hide(); } if (gamewindow_buymenu->visible()) { gamewindow_buymenu->hide(); } if (gamewindow_trademenu->visible()) { gamewindow_trademenu->hide(); } gamewindow_inventory->show(); } else { gamewindow_inventory->hide(); } } void GameWindow::toggle_reputation() { if (!gamewindow_reputation->visible()) { if (gamewindow_chat->visible() && !gamewindow_chat->small_view()) { gamewindow_chat->hide(); } if (gamewindow_map->visible()) { gamewindow_map->hide(); } if (gamewindow_inventory->visible()) { gamewindow_inventory->hide(); } if (gamewindow_entitymenu->visible()) { gamewindow_entitymenu->hide(); } if (gamewindow_buymenu->visible()) { gamewindow_buymenu->hide(); } if (gamewindow_trademenu->visible()) { gamewindow_trademenu->hide(); } gamewindow_reputation->show(); } else { gamewindow_reputation->hide(); } } void GameWindow::toggle_chat() { // if the chat window is in small view, hide it first and open the full view if (gamewindow_chat->visible() && gamewindow_chat->small_view()) { gamewindow_chat->set_small_view(false); gamewindow_chat->hide(); } if (!gamewindow_chat->visible()) { if (gamewindow_map->visible()) gamewindow_map->hide(); if (gamewindow_inventory->visible()) { gamewindow_inventory->hide(); } if (gamewindow_reputation->visible()) { gamewindow_reputation->hide(); } if (gamewindow_entitymenu->visible()) { gamewindow_entitymenu->hide(); } if (gamewindow_buymenu->visible()) { gamewindow_buymenu->hide(); } if (gamewindow_trademenu->visible()) { gamewindow_trademenu->hide(); } gamewindow_chat->set_small_view(false); gamewindow_chat->show(); } else { gamewindow_chat->hide(); } } void GameWindow::toggle_chatbar() { if (gamewindow_chat->visible() && !gamewindow_chat->small_view()) { gamewindow_chat->set_small_view(true); gamewindow_chat->hide(); } if (!gamewindow_chat->visible()) { gamewindow_chat->set_small_view(true); gamewindow_chat->show(); } else { gamewindow_chat->hide(); } } void GameWindow::show_menu(const std::string & args) { if (!args.size()) return; if (!core::localplayer()->view()) return; if (!core::localplayer()->view()->menus().size()) return; std::stringstream argstr(args); std::string label; if (!(argstr >> label)) return; if (label.compare("buy") == 0) { // buy menu, single item unsigned int id; if (argstr >> id) { // hide other menus gamewindow_entitymenu->hide(); gamewindow_trademenu->hide(); // hide other windows gamewindow_chat->hide(); gamewindow_map->hide(); gamewindow_inventory->hide(); gamewindow_reputation->hide(); // requesting the info through game makes sure it is retreived from the server gamewindow_buymenu->set_item( core::game()->request_info(id)); // show buy menu gamewindow_buymenu->show(); } else { con_print << "usage: view buy [info id] show the buy menu for this kind of item" << std::endl; } } else if (label.compare("trade") == 0) { // invetory based trade std::string typestr; if(argstr >> typestr) { aux::to_label(typestr); // hide other menus gamewindow_buymenu->hide(); gamewindow_entitymenu->hide(); // hide other windows gamewindow_chat->hide(); gamewindow_map->hide(); gamewindow_inventory->hide(); gamewindow_reputation->hide(); // show trade menu gamewindow_trademenu->set_itemtype(core::InfoType::find(typestr)); gamewindow_trademenu->show(); } else { con_print << "usage: view trade [string] show the trade menu for this type of items" << std::endl; } } else if (label.compare("hide") == 0) { // hide all menus gamewindow_buymenu->hide(); gamewindow_entitymenu->hide(); gamewindow_trademenu->hide(); } else { // hide other menus gamewindow_buymenu->hide(); gamewindow_trademenu->hide(); // hide other windows gamewindow_chat->hide(); gamewindow_map->hide(); gamewindow_inventory->hide(); gamewindow_reputation->hide(); // show other menus gamewindow_entitymenu->generate(core::localplayer()->view(), label.c_str()); gamewindow_entitymenu->show(); } } void GameWindow::resize() { const float smallmargin = ui::UI::elementsize.height(); set_size(parent()->size()); // icons const float icon_margin = 4.0f; const float icon_size = 48.0f; const float icon_count = 11; const float l = (width() - ((icon_count + 1) * icon_margin) - (icon_count * icon_size)) * 0.5f; gamewindow_menubutton->set_geometry(l, icon_margin, icon_size, icon_size); // spacer gamewindow_freeflightbutton->set_geometry(l + 2.0f *(icon_margin + icon_size), icon_margin, icon_size, icon_size); gamewindow_gotobutton->set_geometry(l + 3.0f *(icon_margin + icon_size), icon_margin, icon_size, icon_size); gamewindow_dockbutton->set_geometry(l + 4.0f *(icon_margin + icon_size), icon_margin, icon_size, icon_size); gamewindow_launchbutton->set_geometry(l + 4.0f *(icon_margin + icon_size), icon_margin, icon_size, icon_size); gamewindow_formationbutton->set_geometry(l + 5.0f *(icon_margin + icon_size), icon_margin, icon_size, icon_size); gamewindow_homebutton->set_geometry(l + 6.0f *(icon_margin + icon_size), icon_margin, icon_size, icon_size); gamewindow_inventorybutton->set_geometry(l + 7.0f *(icon_margin + icon_size), icon_margin, icon_size, icon_size); gamewindow_chatbutton->set_geometry(l + 8.0f *(icon_margin + icon_size), icon_margin, icon_size, icon_size); gamewindow_mapbutton->set_geometry(l + 9.0f *(icon_margin + icon_size), icon_margin, icon_size, icon_size); gamewindow_reputationbutton->set_geometry(l + 10.0f *(icon_margin + icon_size), icon_margin, icon_size, icon_size); // reposition buy menu gamewindow_buymenu->event_resize(); // reposition trade menu gamewindow_trademenu->event_resize(); // set hud geometry gamewindow_hud->set_geometry(0, 0, width(), height()); gamewindow_hud->event_resize(); // reposition entity menus gamewindow_entitymenu->set_size(width() - smallmargin * 2, height() - smallmargin * 4); gamewindow_entitymenu->set_location(smallmargin, smallmargin * 2); // reposition map gamewindow_map->set_size(width() - smallmargin * 2, height() - smallmargin * 4); gamewindow_map->set_location(smallmargin, smallmargin * 2); // resize inventory window gamewindow_inventory->set_size(width() - smallmargin * 2, height() - smallmargin * 4); gamewindow_inventory->set_location(smallmargin, smallmargin * 2); // resize reputation window gamewindow_reputation->set_size(width() - smallmargin * 2, height() - smallmargin * 4); gamewindow_reputation->set_location(smallmargin, smallmargin * 2); } void GameWindow::draw() { ui::Window::draw(); const float smallmargin = ui::UI::elementsize.height(); if (core::localcontrol()->state() == core::Entity::Docked) { gamewindow_launchbutton->show(); gamewindow_freeflightbutton->hide(); gamewindow_gotobutton->hide(); gamewindow_dockbutton->hide(); gamewindow_formationbutton->hide(); } else { gamewindow_launchbutton->hide(); gamewindow_freeflightbutton->show(); if (core::localplayer()->autopilot_target()) { gamewindow_freeflightbutton->enable(); } else { gamewindow_freeflightbutton->disable(); } gamewindow_gotobutton->show(); gamewindow_dockbutton->show(); gamewindow_formationbutton->show(); } if (core::localplayer()->view()) { // hide hide when a view is set gamewindow_hud->hide(); // docking view if (core::localplayer()->view()->menus().size()) { if (gamewindow_entitymenu->generated_entity() != core::localplayer()->view()) { // initially show the menu show_menu("main"); gamewindow_map->hide(); gamewindow_chat->hide(); // hide other windows gamewindow_chat->hide(); gamewindow_map->hide(); gamewindow_inventory->hide(); gamewindow_reputation->hide(); } else if ( !gamewindow_entitymenu->visible() && !gamewindow_buymenu->visible() && !gamewindow_trademenu->visible() && !gamewindow_inventory->visible() && !gamewindow_reputation->visible() && !gamewindow_map->visible() && (!gamewindow_chat->visible() || gamewindow_chat->small_view()) ) { // show the menu if there's no other window open gamewindow_entitymenu->show(); } gamewindow_homebutton->show(); } else { // entity without menus, plain view this->hide(); return; } } else { gamewindow_homebutton->hide(); if (gamewindow_entitymenu->visible()) { gamewindow_entitymenu->hide(); } if (gamewindow_entitymenu->generated_entity()) { gamewindow_entitymenu->generate(0, 0); } if (gamewindow_buymenu->visible()) { gamewindow_buymenu->hide(); } if (gamewindow_trademenu->visible()) { gamewindow_trademenu->hide(); } if (!gamewindow_map->visible() && !gamewindow_chat->visible() && !gamewindow_inventory->visible() && !gamewindow_reputation->visible()) { gamewindow_hud->set_focus(); } gamewindow_hud->show(); } // reposition chat widget if (gamewindow_chat->visible()) { if (gamewindow_chat->small_view()) { gamewindow_chat->set_size(width() - smallmargin * 2, font()->height() * 2); gamewindow_chat->set_location(smallmargin, height() - smallmargin *2 - gamewindow_chat->height()); } else { gamewindow_chat->set_geometry(gamewindow_map->location(), gamewindow_map->size()); } gamewindow_chat->event_resize(); } } bool GameWindow::on_keypress(const int key, const unsigned int modifier) { switch (key) { case SDLK_ESCAPE: if (gamewindow_entitymenu->visible()) { gamewindow_entitymenu->hide(); return true; } if (gamewindow_buymenu->visible()) { gamewindow_buymenu->hide(); return true; } if (gamewindow_trademenu->visible()) { gamewindow_trademenu->hide(); return true; } if (gamewindow_reputation->visible()) { gamewindow_reputation->hide(); return true; } if (gamewindow_map->visible()) { gamewindow_map->hide(); return true; } if (gamewindow_inventory->visible()) { gamewindow_inventory->hide(); return true; } break; default: break; } return Window::on_keypress(key, modifier); } }