/* $Id: serverlistwindow.cpp,v 1.18 2005/07/17 17:27:31 chfreund Exp $ */ #include "serverlistwindow.hpp" #include #include "backgroundwidget.h" #include "borderwidget.h" #include "layout.h" using namespace SDLwidgets; #include "wopsettings.hpp" ServerListWindow::ServerListWindow( Uint32 width, Uint32 height ) : m_width( width ), m_height( height ) { m_content = new WidgetComposite(); VerticalLayout* layout = new VerticalLayout( m_content ); layout->setSpacing( 5 ); m_content->setLayout( layout ); m_content->setMinHeight( height - 20 ); m_searchLabel = new TextLabel( "Searching for servers...", 14 ); m_searchLabel->setColor( m_labelColor ); m_content->add( m_searchLabel ); layout->setStretch( m_searchLabel, FILL ); m_entriesContainer = new WidgetComposite( m_content ); TableLayout* entriesLayout = new TableLayout( m_entriesContainer, 3 ); entriesLayout->setHorizontalSpacing( 10 ); entriesLayout->setColumnStretch( 0, FILL ); entriesLayout->setColumnStretch( 1, FILL ); m_entriesContainer->setLayout( entriesLayout ); // m_content->add( m_entriesContainer ); m_labelColor.r = 0; m_labelColor.g = 0; m_labelColor.b = 0; m_buttonColor.r = 200; m_buttonColor.g = 200; m_buttonColor.b = 200; m_header[0] = new TextLabel( "Server name", 12 ); m_header[0]->setColor( m_labelColor ); m_header[1] = new TextLabel( "Theme", 12 ); m_header[1]->setColor( m_labelColor ); m_header[2] = new TextLabel( "#Players", 12 ); m_header[2]->setColor( m_labelColor ); m_entriesContainer->add( m_header[0] ); m_entriesContainer->add( m_header[1] ); m_entriesContainer->add( m_header[2] ); m_entriesContainer->setWidth( m_width ); m_buttonRow = new WidgetComposite( m_content ); HorizontalLayout* m_buttonRowLayout = new HorizontalLayout( m_buttonRow ); m_buttonRow->setLayout( m_buttonRowLayout ); m_buttonRowLayout->setSpacing( 5 ); m_connectButton = new TextButton( "Connect!" ); m_connectButton->setHighlightable( true ); m_connectButton->addActionListener( this ); m_buttonRow->add( m_connectButton ); m_buttonRowLayout->setStretch( m_connectButton, FILL ); m_updateButton = new TextButton( "Refresh" ); m_updateButton->setHighlightable( true ); m_updateButton->addActionListener( this ); m_buttonRow->add( m_updateButton ); m_buttonRowLayout->setStretch( m_updateButton, FILL ); m_backButton = new TextButton( "Back" ); m_backButton->setHighlightable( true ); m_backButton->addActionListener( this ); m_buttonRow->add( m_backButton ); m_buttonRowLayout->setStretch( m_backButton, FILL ); m_buttonRow->setWidth( m_width ); m_content->add( m_buttonRow ); m_normalBackground.r = 0; m_normalBackground.g = 80; m_normalBackground.b = 0; m_highlightBackground.r = 0; m_highlightBackground.g = 160; m_highlightBackground.b = 0; m_selectedIndex = -1; m_content->doLayout(); std::ostringstream path; path << WopSettings::getInstance()->getData(); path << "/images/gui/bg.png"; m_background = new BackgroundWidget( m_content, path.str().c_str() ); setTitle( "Choose a server" ); setWidget( m_background ); } ServerListWindow::~ServerListWindow() { delete m_header; } void ServerListWindow::clearEntries() { m_content->removeAll(); m_content->add( m_searchLabel ); VerticalLayout* layout = dynamic_cast( m_content->getLayout() ); layout->setStretch( m_searchLabel, FILL ); m_content->add( m_buttonRow ); } void ServerListWindow::setEntries( std::vector& entries ) { m_entriesContainer->removeAll(); m_entriesContainer->add( m_header[0] ); m_entriesContainer->add( m_header[1] ); m_entriesContainer->add( m_header[2] ); m_entries.clear(); for ( std::vector::const_iterator iter = entries.begin(); iter != entries.end(); iter++ ) { m_entries.push_back( *iter ); } for ( std::vector::iterator iter = m_buttons.begin(); iter != m_buttons.end(); iter++ ) { delete *iter; } m_buttons.clear(); for ( std::vector::iterator iter = m_labels.begin(); iter != m_labels.end(); iter++ ) { remove( *iter ); delete *iter; } m_labels.clear(); for ( std::vector::iterator it = m_entries.begin(); it != m_entries.end(); it++ ) { // InfoMessage* info = infos[ it - m_entries.begin() ]; // TODO: handle entries of type DIRECT if ( (*it)->type == ServerEntry::REMOTE ) { RemoteServerEntry* entry = static_cast( *it ); std::string s; if ( entry->name.size() > 0 ) { s = entry->name; } else { String ipString; if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) { ipString.format( "%u.%u.%u.%u", entry->address.host & 255, (entry->address.host >> 8) & 255, (entry->address.host >> 16) & 255, (entry->address.host >> 24) & 255 ); } else { ipString.format( "%u.%u.%u.%u", (entry->address.host >> 24) & 255, (entry->address.host >> 16) & 255, (entry->address.host >> 8) & 255, entry->address.host & 255 ); } s = ipString; } TextButton* button = new TextButton( s ); button->addActionListener( this ); button->setColor( m_buttonColor ); m_buttons.push_back( button ); BackgroundWidget* widget = new BackgroundWidget( button, m_normalBackground, 255 ); m_labels.push_back( widget ); m_entriesContainer->add( widget ); s = (*it)->theme; TextLabel* label = new TextLabel( s ); label->setColor( m_labelColor ); m_entriesContainer->add( label ); std::ostringstream ostr; int np = (*it)->numberPlayers; ostr << np; s = ostr.str(); label = new TextLabel( s ); label->setColor( m_labelColor ); m_entriesContainer->add( label ); } } m_selectedIndex = -1; m_content->removeAll(); m_content->add( m_entriesContainer ); VerticalLayout* layout = dynamic_cast( m_content->getLayout() ); layout->setStretch( m_entriesContainer, FILL ); m_content->add( m_buttonRow ); m_content->doLayout(); // Automatically select first entry in list if not empty if ( m_entries.size() > 0 ) { m_selectedIndex = 0; m_labels[m_selectedIndex]->setColor( m_highlightBackground ); } } void ServerListWindow::actionPerformed( Uint32 cmd, void* source ) { if ( source == m_updateButton && cmd == Button::BUTTON_PRESSED ) { fireAction( REFRESH_PRESSED, this ); } else if ( source == m_connectButton && cmd == Button::BUTTON_PRESSED ) { fireAction( CONNECT_PRESSED, this ); } else if ( source == m_backButton && cmd == Button::BUTTON_PRESSED ) { fireAction( BACK_PRESSED, this ); } else { for ( std::vector::iterator it = m_buttons.begin(); it != m_buttons.end(); it++ ) { if ( *it == source ) { if ( m_selectedIndex >= 0 ) { m_labels[m_selectedIndex]->setColor( m_normalBackground ); } m_selectedIndex = it - m_buttons.begin(); m_labels[m_selectedIndex]->setColor( m_highlightBackground ); } } } } bool ServerListWindow::handleEvent( SDL_Event* event ) { if ( event->type == SDL_KEYDOWN ) { if ( event->key.keysym.sym == SDLK_ESCAPE ) { fireAction( BACK_PRESSED, this ); return true; } } return WopWindow::handleEvent( event ); }