00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00071 updateList();
00072 };
00073
00074
00075 bool SelectFromMap::ProcessEvent ( const SDL_Event * event,bool bModal )
00076 {
00077 if ( md->ProcessEvent( event, bModal ) )
00078 return true;
00079
00080 if ( omp && omp->ProcessEvent( event, bModal ) )
00081 return true;
00082
00083 return ASC_PG_Dialog::ProcessEvent( event, bModal );
00084 }
00085
00086 bool SelectFromMap::accept( const MapCoordinate& pos ) {
00087 return !readOnly && actmap->getField( pos ) != NULL;
00088 };
00089
00090 void SelectFromMap::showFieldMarking( const CoordinateList& coordinateList )
00091 {
00092 actmap->cleartemps( 7 );
00093 for ( CoordinateList::const_iterator i = coordinateList.begin(); i != coordinateList.end(); ++i ) {
00094 tfield* fld = actmap->getField( *i );
00095 if ( fld )
00096 fld->a.temp = 1;
00097 }
00098 repaintMap();
00099 }
00100
00101 bool SelectFromMap::markField2( const MapCoordinate& pos, const SPoint& mouse, bool cursorChanged, int button, int prio )
00102 {
00103 if ( prio <= 2 )
00104 markField( pos );
00105 return true;
00106 }
00107
00108
00109 bool SelectFromMap::markField( const MapCoordinate& pos )
00110 {
00111 if ( !accept(pos))
00112 return false;
00113
00114 if( justOne ) {
00115 coordinateList.clear();
00116 coordinateList.push_back ( pos );
00117 } else {
00118 CoordinateList::iterator i = find( coordinateList.begin(), coordinateList.end(), pos );
00119 if ( i == coordinateList.end() ) {
00120 coordinateList.push_back ( pos );
00121 } else {
00122 coordinateList.erase ( i );
00123 }
00124 }
00125 showFieldMarking( coordinateList );
00126 updateList();
00127 return true;
00128 }
00129
00130
00131 bool SelectFromMap::mark()
00132 {
00133 MapCoordinate pos = actmap->getCursor();
00134 return markField( pos );
00135 }
00136
00137 bool SelectFromMap::eventKeyDown (const SDL_KeyboardEvent *key)
00138 {
00139 if ( key->type == SDL_KEYDOWN ) {
00140 if ( key->keysym.sym == SDLK_SPACE ) {
00141 mark();
00142 return true;
00143 }
00144 if ( key->keysym.sym == SDLK_RETURN || key->keysym.sym == SDLK_KP_ENTER ) {
00145 if ( isOk() )
00146 QuitModal();
00147 return true;
00148 }
00149 }
00150 return ASC_PG_Dialog::eventKeyDown( key );
00151 }
00152
00153 void SelectFromMap::updateList()
00154 {
00155 listbox->DeleteAll();
00156 for ( CoordinateList::iterator i = coordinateList.begin(); i != coordinateList.end(); ++i )
00157 new CoordinateItem( listbox, *i );
00158
00159 listbox->Show();
00160 }
00161
00162 bool SelectFromMap::listItemClicked( PG_ListBoxBaseItem* item )
00163 {
00164 if ( item ) {
00165 CoordinateItem* i = dynamic_cast<CoordinateItem*>(item);
00166 if ( i )
00167 md->cursor.goTo( i->getPos() );
00168 }
00169 return true;
00170 }
00171
00172
00173
00174 void SelectFromMap::Show( bool fade)
00175 {
00176 getMainScreenWidget()->BringToFront();
00177 md->BringToFront();
00178 md->Show();
00179 ASC_PG_Dialog::Show( fade );
00180 repaintMap();
00181 }
00182
00183 SelectFromMap::~SelectFromMap()
00184 {
00185 md->setSignalPriority( oldprio );
00186
00187 getMainScreenWidget()->SendToBack();
00188 actmap->cleartemps(7);
00189 repaintMap();
00190 }
00191
00192
00193 bool SelectBuildingFromMap::accept( const MapCoordinate& pos ) {
00194 return actmap->getField( pos ) && actmap->getField( pos )->building;
00195 };
00196
00197 bool SelectUnitFromMap::accept( const MapCoordinate& pos ) {
00198 return actmap->getField( pos ) && actmap->getField( pos )->vehicle;
00199 };
00200