fieldmarker.cpp

Go to the documentation of this file.
00001 /*
00002      This file is part of Advanced Strategic Command; http://www.asc-hq.de
00003      Copyright (C) 1994-2010  Martin Bickel  and  Marc Schellenberger
00004  
00005      This program is free software; you can redistribute it and/or modify
00006      it under the terms of the GNU General Public License as published by
00007      the Free Software Foundation; either version 2 of the License, or
00008      (at your option) any later version.
00009  
00010      This program is distributed in the hope that it will be useful,
00011      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013      GNU General Public License for more details.
00014  
00015      You should have received a copy of the GNU General Public License
00016      along with this program; see the file COPYING. If not, write to the 
00017      Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
00018      Boston, MA  02111-1307  USA
00019 */
00020 
00021 #include <pglistboxitem.h>
00022 #include "../gamemap.h"
00023 #include "fieldmarker.h"
00024 #include "../mapdisplay.h"
00025 #include "../overviewmappanel.h"
00026 #include "../spfst.h"
00027 #include "../mainscreenwidget.h"
00028 
00029 class CoordinateItem : public PG_ListBoxItem {
00030    ASCString toString( const MapCoordinate& pos ) {
00031       ASCString s;
00032       s.format( "%d / %d", pos.x, pos.y );
00033       return s;
00034    };
00035    MapCoordinate p;
00036    
00037    public:
00038       CoordinateItem( PG_Widget *parent, const MapCoordinate& pos ) : PG_ListBoxItem( parent, 20, toString( pos ) ), p(pos) {};
00039       MapCoordinate getPos() const { return p; };
00040 };
00041 
00042 
00043 SelectFromMap::SelectFromMap( CoordinateList& list, GameMap* map, bool justOne, bool readOnly ) : ASC_PG_Dialog( NULL, PG_Rect( PG_Application::GetScreenWidth() - 150, PG_Application::GetScreenHeight() - 300, 150, 300 ), "Select Fields" ), listbox(NULL), actmap( map ), coordinateList (list)
00044 {
00045    this->justOne = justOne;
00046    this->readOnly = readOnly;
00047 
00048    
00049    listbox = new PG_ListBox( this, PG_Rect( 10, 30, 130, readOnly ? 210 : 180 ));
00050    listbox->sigSelectItem.connect( SigC::slot( *this, &SelectFromMap::listItemClicked ));
00051 
00052    if ( !readOnly ) {
00053       PG_Button* m = new PG_Button ( this, PG_Rect( 10, 230, 130, 20 ), "mark (~space~)");
00054       m->sigClick.connect( SigC::slot( *this, &SelectFromMap::mark ));
00055    }
00056 
00057 
00058    PG_Button* b = new PG_Button ( this, PG_Rect( 10, 270, 130, 20 ), "~O~K");
00059    b->sigClick.connect( SigC::slot( *this, &SelectFromMap::closeDialog ));
00060 
00061    omp = getMainScreenWidget()->getOverviewMapPanel();
00062    md  = getMainScreenWidget()->getMapDisplay();
00063    md->mouseButtonOnField.connect( SigC::slot( *this, &SelectFromMap::markField2 ));
00064    oldprio = md->setSignalPriority( 2 );
00065    
00066 
00067    showFieldMarking( coordinateList );
00068    if ( !list.empty() ) {
00069       md->cursor.goTo( *list.begin() );
00070       actmap->getCursor() =  *list.begin();
00071       cursorMoved();
00072    }
00073 
00074    updateList();
00075 };
00076 
00077    
00078 bool SelectFromMap::ProcessEvent ( const SDL_Event *   event,bool   bModal   )
00079 {
00080    if ( md->ProcessEvent( event, bModal ) )
00081       return true;
00082 
00083    if ( omp && omp->ProcessEvent( event, bModal ) )
00084       return true;
00085 
00086    return ASC_PG_Dialog::ProcessEvent( event, bModal );
00087 }
00088 
00089 bool SelectFromMap::accept( const MapCoordinate& pos ) {
00090    return !readOnly && actmap->getField( pos ) != NULL;
00091 };
00092 
00093 void SelectFromMap::showFieldMarking( const CoordinateList& coordinateList )
00094 {
00095    actmap->cleartemps( 7 );
00096    for ( CoordinateList::const_iterator i = coordinateList.begin(); i != coordinateList.end(); ++i ) {
00097       MapField* fld = actmap->getField( *i );
00098       if ( fld )
00099          fld->a.temp = 1;
00100    }
00101    repaintMap();
00102 }
00103 
00104 bool SelectFromMap::markField2( const MapCoordinate& pos, const SPoint& mouse, bool cursorChanged, int button, int prio )
00105 {
00106    if ( prio <= 2 )
00107       markField( pos );
00108    return true;
00109 }
00110 
00111 
00112 bool SelectFromMap::markField( const MapCoordinate& pos )
00113 {
00114    if  ( !accept(pos))
00115       return false;
00116 
00117    if( justOne ) {
00118       coordinateList.clear();
00119       coordinateList.push_back ( pos );
00120    } else {
00121       CoordinateList::iterator i = find( coordinateList.begin(), coordinateList.end(), pos );
00122       if ( i == coordinateList.end() ) {
00123          coordinateList.push_back ( pos );
00124       } else {
00125          coordinateList.erase ( i );
00126       }
00127    }
00128    showFieldMarking( coordinateList );
00129    updateList();
00130    return true;
00131 }
00132 
00133 
00134 bool SelectFromMap::mark()
00135 {
00136    MapCoordinate pos = actmap->getCursor();
00137    return markField( pos );
00138 }
00139 
00140 bool SelectFromMap::eventKeyDown (const SDL_KeyboardEvent *key)
00141 {
00142    if ( key->type == SDL_KEYDOWN ) {
00143       if ( key->keysym.sym == SDLK_SPACE  ) {
00144          mark();
00145          return true;
00146       }
00147       if ( key->keysym.sym == SDLK_RETURN || key->keysym.sym == SDLK_KP_ENTER ) {
00148          if ( isOk() )
00149             QuitModal();
00150          return true;
00151       }
00152    }
00153    return ASC_PG_Dialog::eventKeyDown( key );
00154 }
00155 
00156 void SelectFromMap::updateList()
00157 {
00158    listbox->DeleteAll();
00159    for ( CoordinateList::iterator i = coordinateList.begin(); i != coordinateList.end(); ++i )
00160       new CoordinateItem( listbox, *i );
00161 
00162    listbox->Show();
00163 }
00164 
00165 bool SelectFromMap::listItemClicked( PG_ListBoxBaseItem* item )
00166 {
00167    if ( item ) {
00168       CoordinateItem* i = dynamic_cast<CoordinateItem*>(item);
00169       if ( i ) {
00170          md->cursor.goTo( i->getPos() );
00171          actmap->getCursor() =  i->getPos();
00172          cursorMoved();
00173       }
00174    }
00175    return true;
00176 }
00177 
00178 
00179 
00180 void SelectFromMap::Show( bool fade)
00181 {
00182    getMainScreenWidget()->BringToFront();
00183    md->BringToFront();
00184    md->Show();
00185    ASC_PG_Dialog::Show( fade );
00186    repaintMap();
00187 }
00188 
00189 SelectFromMap::~SelectFromMap()
00190 {
00191    md->setSignalPriority( oldprio );
00192          
00193    getMainScreenWidget()->SendToBack();
00194    actmap->cleartemps(7);
00195    repaintMap();
00196 }
00197 
00198 
00199 bool SelectBuildingFromMap::accept( const MapCoordinate& pos ) {
00200    return actmap->getField( pos ) && actmap->getField( pos )->building;
00201 };
00202 
00203 bool SelectUnitFromMap::accept( const MapCoordinate& pos ) {
00204    return actmap->getField( pos ) && actmap->getField( pos )->vehicle;
00205 };
00206 

Generated on Mon May 21 01:26:32 2012 for Advanced Strategic Command by  doxygen 1.5.1