taskmanager.cpp

Go to the documentation of this file.
00001 
00002 /*
00003      This file is part of Advanced Strategic Command; http://www.asc-hq.de
00004      Copyright (C) 1994-2010  Martin Bickel  and  Marc Schellenberger
00005  
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010  
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015  
00016      You should have received a copy of the GNU General Public License
00017      along with this program; see the file COPYING. If not, write to the 
00018      Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
00019      Boston, MA  02111-1307  USA
00020 */
00021 
00022 #include "taskmanager.h"
00023 
00024 #include "../paradialog.h"
00025 #include "../actions/actioncontainer.h"
00026 #include "../actions/action.h"
00027 #include "../actions/command.h"
00028 #include "../gamemap.h"
00029 #include "../sg.h"
00030 #include "../dialog.h"
00031 #include "../widgets/vehicletypeimage.h"
00032 #include "../actions/unitcommand.h"
00033 #include "../itemrepository.h"
00034 #include "../mapdisplay.h"
00035 #include "selectionwindow.h"
00036 #include "../mainscreenwidget.h"
00037 #include "../spfst.h"
00038 #include "../tasks/taskcontainer.h"
00039 
00040 #include "actionwidget.h"
00041 #include "../actions/taskinterface.h"
00042 #include "../contextutils.h"
00043 
00044 
00045 class TaskWidget: public ActionWidget
00046 {
00047       GameMap* gamemap;
00048       TaskContainer* taskContainer;
00049       Command* command;
00050    
00051       bool run() {
00052          TaskInterface* ti = dynamic_cast<TaskInterface*>( command );
00053          
00054          if ( ti->operatable() ) {
00055             ActionResult res = command->execute( createFollowerContext( gamemap ));
00056             if ( !res.successful() )
00057                displayActionError(res);
00058          }
00059          
00060          taskContainer->remove( command );
00061          Hide();
00062          return true;
00063       }
00064       
00065       bool cancel() {
00066          taskContainer->remove( command );
00067          delete command;
00068          Hide();
00069          return true;
00070       }
00071    
00072    public:
00073       TaskWidget( PG_Widget* parent, const PG_Point& pos, int width, Command* command, GameMap* map, TaskContainer* taskContainer ) 
00074    : ActionWidget( parent,pos, width, *command, map ) , gamemap( map )
00075       {
00076          this->taskContainer = taskContainer;
00077          this->command = command;
00078          
00079          (new PG_Button(this, PG_Rect( width-70, 5, 60, 15 ), "Run"))->sigClick.connect( SigC::slot( *this, &TaskWidget::run ));
00080          (new PG_Button(this, PG_Rect( width-70, 22, 60, 15 ), "Cancel"))->sigClick.connect( SigC::slot( *this, &TaskWidget::cancel ));
00081       }
00082    protected:
00083       bool click( )
00084       {
00085          return true;
00086       }
00087 
00088 };
00089 
00090 
00091 class TaskFactory : public SelectionItemFactory {
00092    TaskContainer* tasks;
00093    TaskContainer::CommandContainer::const_iterator a;
00094    GameMap* map;
00095    public:
00096       TaskFactory( GameMap* map ) 
00097       {
00098          this->map = map;
00099          tasks = dynamic_cast<TaskContainer*>( map->tasks );
00100          restart();
00101       };
00102       
00103       void restart()
00104       {
00105          a = tasks->pendingCommands.begin();
00106       };
00107       
00108       SelectionWidget* spawnNextItem( PG_Widget* parent, const PG_Point& pos ) 
00109       {
00110          if ( a != tasks->pendingCommands.end() ) {
00111             return new TaskWidget( parent, pos, parent->Width() - 20, *(a++),map, tasks );
00112          } else
00113             return NULL;
00114       }
00115       
00116       void itemSelected( const SelectionWidget* widget, bool mouse ) 
00117       {
00118          if ( mouse ) {
00119             const TaskWidget* asw = dynamic_cast<const TaskWidget*>( widget );
00120             if ( asw ) {
00121                vector<MapCoordinate>  pos = asw->getCoordinates();
00122                if ( pos.size() > 0 ) {
00123                   MapDisplayPG* md = getMainScreenWidget()->getMapDisplay();
00124                   md->cursor.goTo( pos[0] );
00125                }
00126             }
00127          }
00128       }
00129 
00130 };
00131 
00132 class TaskManager : public ASC_PG_Dialog {
00133    GameMap* gamemap;
00134       
00135    bool ok() 
00136    {
00137       QuitModal();  
00138       return true;
00139    }
00140       
00141    bool run() 
00142    {
00143       Hide();
00144       try {
00145          TaskContainer* tc = dynamic_cast<TaskContainer*>( gamemap->tasks );
00146          if ( tc ) {
00147             while ( tc->pendingCommands.begin() != tc->pendingCommands.end() ) {
00148                Command* c = tc->pendingCommands.front();
00149                TaskInterface* ti = dynamic_cast<TaskInterface*>( c );
00150          
00151                if ( ti->operatable() ) {
00152                   ActionResult res = c->execute( createFollowerContext( gamemap ));
00153                   if ( !res.successful() )
00154                      dispmessage2(res);
00155                }
00156          
00157                tc->pendingCommands.erase( tc->pendingCommands.begin() );
00158             }
00159          }
00160          
00161          ActionResult res = gamemap->actions.rerun( createContext( gamemap ));
00162          if ( !res.successful() )
00163             displayActionError( res );
00164          repaintMap();
00165       } catch ( ActionResult res ) {
00166          displayActionError( res );
00167       }
00168       Show();
00169       selection->reLoad(true);
00170       return true;  
00171    }
00172       
00173    ItemSelectorWidget* selection;
00174       
00175    public:   
00176       TaskManager( GameMap* map ) : ASC_PG_Dialog( NULL, PG_Rect( -1, -1, 600, 550 ), "Manage Tasks" ), gamemap( map ) 
00177       {
00178          StandardButtonDirection( Horizontal );
00179          AddStandardButton("Close")->sigClick.connect( SigC::slot( *this, &TaskManager::ok ));
00180          AddStandardButton("Run All")->sigClick.connect( SigC::slot( *this, &TaskManager::run ));
00181          selection = new ItemSelectorWidget( this, PG_Rect( 5, 20, Width()-10, Height() - 70 ), new TaskFactory( map ));
00182       }
00183    
00184 };
00185 
00186 
00187 void taskManager( GameMap* map )
00188 {
00189    if ( ! dynamic_cast<TaskContainer*>( map->tasks ) ) {
00190       warningMessage("No Tasks to manage");
00191       return;
00192    }
00193    
00194    TaskManager am ( map );
00195    am.Show();
00196    am.RunModal();  
00197 }
00198 

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