/* $Id: keyconfigwindow.cpp,v 1.6 2005/12/18 14:43:44 chfreund Exp $ */ #include "keyconfigwindow.hpp" #include "keyboardconfig.hpp" #include "keytable.hpp" #include "textlabel.h" #include "textfield.h" #include "button.h" KeyConfigWindow::KeyConfigWindow( KeyboardConfig* config, Uint32 width, Uint32 height ) : m_config( config ), m_selectedKeyIndex( -1 ), m_keyFontSize( 14 ) { m_normalColor.r = 0; m_normalColor.g = 0; m_normalColor.b = 0; m_highlightColor.r = 255; m_highlightColor.g = 0; m_highlightColor.b = 0; m_container1 = new WidgetComposite(); TableLayout* layout1 = new TableLayout( m_container1, 2 ); layout1->setHorizontalSpacing( 10 ); layout1->setColumnStretch( 1, FILL ); m_container1->setLayout( layout1 ); m_container1->setWidth( ( width - 20 ) / 2 ); m_container2 = new WidgetComposite(); TableLayout* layout2 = new TableLayout( m_container2, 2 ); layout2->setHorizontalSpacing( 10 ); layout2->setColumnStretch( 1, FILL ); m_container2->setLayout( layout2 ); m_container2->setWidth( ( width - 20 ) / 2 ); addKeys(); m_columnContainer = new WidgetComposite(); HorizontalLayout* columnLayout = new HorizontalLayout( m_columnContainer ); // columnLayout->setSpacing( 15 ); m_columnContainer->setLayout( columnLayout ); m_columnContainer->setWidth( width ); m_columnContainer->add( m_container1 ); m_columnContainer->add( m_container2 ); SDL_Color blue = { 0, 0, 255, 0 }; TextLabel* label = new TextLabel( "Click on key name to change key assignment" ); label->setColor( blue ); m_backButton = new TextButton( "Back" ); m_backButton->setWidth( width ); m_backButton->setHighlightable( true ); m_backButton->addActionListener( this ); m_mainContainer = new WidgetComposite(); VerticalLayout* mainLayout = new VerticalLayout( m_mainContainer ); m_mainContainer->setLayout( mainLayout ); m_mainContainer->add( m_columnContainer ); m_mainContainer->add( label ); m_mainContainer->add( m_backButton ); setTitle( "Keyboard configuration" ); setWidget( m_mainContainer ); } void KeyConfigWindow::actionPerformed( Uint32 cmd, void* source ) { if ( source == m_backButton && cmd == Button::BUTTON_PRESSED ) { fireAction( BACK_PRESSED, this ); } else { for ( int i = 0; i < KeyboardConfig::LAST_KEY_ID; i++ ) { if ( source == m_labels[i] || source == m_values[i] ) { m_values[i]->setText( "Press new key" ); m_values[i]->setColor( m_highlightColor ); m_selectedKeyIndex = i; return; } } } } bool KeyConfigWindow::handleEvent( SDL_Event* event ) { if ( m_selectedKeyIndex > -1 ) { if ( event->type == SDL_KEYDOWN ) { if ( event->key.keysym.sym != SDLK_ESCAPE ) { m_config->key[m_selectedKeyIndex] = event->key.keysym.sym; } m_values[m_selectedKeyIndex]->setText( KeyTable::getKeyName( m_config->key[m_selectedKeyIndex] ) ); m_values[m_selectedKeyIndex]->setColor( m_normalColor ); m_selectedKeyIndex = -1; } return true; } else { if ( event->type == SDL_KEYDOWN ) { if ( event->key.keysym.sym == SDLK_ESCAPE ) { fireAction( BACK_PRESSED, this ); return true; } } } return m_mainContainer->handleEvent( event ); } void KeyConfigWindow::addKeys() { for ( int i = 0; i < KeyboardConfig::LAST_KEY_ID / 2; i++ ) { TextLabel* label = new TextLabel( KeyboardConfig::keyName[i], m_keyFontSize ); label->setColor( m_normalColor ); label->addActionListener( this ); TextLabel* value = new TextLabel( KeyTable::getKeyName( m_config->key[i] ), m_keyFontSize ); value->setColor( m_normalColor ); value->addActionListener( this ); m_labels.push_back( label ); m_values.push_back( value ); m_container1->add( label ); m_container1->add( value ); } for ( int i = KeyboardConfig::LAST_KEY_ID / 2; i < KeyboardConfig::LAST_KEY_ID; i++ ) { TextLabel* label = new TextLabel( KeyboardConfig::keyName[i], m_keyFontSize ); label->setColor( m_normalColor ); label->addActionListener( this ); TextLabel* value = new TextLabel( KeyTable::getKeyName( m_config->key[i] ), m_keyFontSize ); value->setColor( m_normalColor ); value->addActionListener( this ); m_labels.push_back( label ); m_values.push_back( value ); m_container2->add( label ); m_container2->add( value ); } } void KeyConfigWindow::updateKeys() { for ( int i = 0; i < KeyboardConfig::LAST_KEY_ID / 2; i++ ) { TextLabel* value = m_values[i]; value->setText( KeyTable::getKeyName( m_config->key[i] ) ); } for ( int i = KeyboardConfig::LAST_KEY_ID / 2; i < KeyboardConfig::LAST_KEY_ID; i++ ) { TextLabel* value = m_values[i]; value->setText( KeyTable::getKeyName( m_config->key[i] ) ); } }