Advanced Strategic Command
taskmanager.cpp
Go to the documentation of this file.
1 
2 /*
3  This file is part of Advanced Strategic Command; http://www.asc-hq.de
4  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; see the file COPYING. If not, write to the
18  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19  Boston, MA 02111-1307 USA
20 */
21 
22 #include "taskmanager.h"
23 
24 #include "../paradialog.h"
25 #include "../actions/actioncontainer.h"
26 #include "../actions/action.h"
27 #include "../actions/command.h"
28 #include "../gamemap.h"
29 #include "../sg.h"
30 #include "../dialog.h"
31 #include "../widgets/vehicletypeimage.h"
32 #include "../actions/unitcommand.h"
33 #include "../itemrepository.h"
34 #include "../mapdisplay.h"
35 #include "selectionwindow.h"
36 #include "../mainscreenwidget.h"
37 #include "../spfst.h"
38 #include "../tasks/taskcontainer.h"
39 
40 #include "actionwidget.h"
41 #include "../actions/taskinterface.h"
42 #include "../contextutils.h"
43 
44 
45 class TaskWidget: public ActionWidget
46 {
47  GameMap* gamemap;
48  TaskContainer* taskContainer;
49  Command* command;
50 
51  bool run() {
52  TaskInterface* ti = dynamic_cast<TaskInterface*>( command );
53 
54  if ( ti->operatable() ) {
55  ReplayContext context( gamemap );
56  ActionResult res = command->execute( context.getContext() );
57  if ( !res.successful() )
58  displayActionError(res);
59  }
60 
61  taskContainer->remove( command );
62  Hide();
63  return true;
64  }
65 
66  bool cancel() {
67  taskContainer->remove( command );
68  delete command;
69  Hide();
70  return true;
71  }
72 
73  public:
74  TaskWidget( PG_Widget* parent, const PG_Point& pos, int width, Command* command, GameMap* map, TaskContainer* taskContainer )
75  : ActionWidget( parent,pos, width, *command, map ) , gamemap( map )
76  {
77  this->taskContainer = taskContainer;
78  this->command = command;
79 
80  (new PG_Button(this, PG_Rect( width-70, 5, 60, 15 ), "Run"))->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &TaskWidget::run )));
81  (new PG_Button(this, PG_Rect( width-70, 22, 60, 15 ), "Cancel"))->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &TaskWidget::cancel )));
82  }
83  protected:
84  bool click( )
85  {
86  return true;
87  }
88 
89 };
90 
91 
93  TaskContainer* tasks;
94  TaskContainer::CommandContainer::const_iterator a;
95  GameMap* map;
96  public:
98  {
99  this->map = map;
100  tasks = dynamic_cast<TaskContainer*>( map->tasks );
101  restart();
102  };
103 
104  void restart()
105  {
106  a = tasks->pendingCommands.begin();
107  };
108 
109  SelectionWidget* spawnNextItem( PG_Widget* parent, const PG_Point& pos )
110  {
111  if ( a != tasks->pendingCommands.end() ) {
112  return new TaskWidget( parent, pos, parent->Width() - 20, *(a++),map, tasks );
113  } else
114  return NULL;
115  }
116 
117  void itemSelected( const SelectionWidget* widget, bool mouse )
118  {
119  if ( mouse ) {
120  const TaskWidget* asw = dynamic_cast<const TaskWidget*>( widget );
121  if ( asw ) {
122  vector<MapCoordinate> pos = asw->getCoordinates();
123  if ( pos.size() > 0 ) {
125  md->cursor.goTo( pos[0] );
126  }
127  }
128  }
129  }
130 
131 };
132 
133 class TaskManager : public ASC_PG_Dialog {
134  GameMap* gamemap;
135 
136  bool ok()
137  {
138  QuitModal();
139  return true;
140  }
141 
142  bool run()
143  {
144  Hide();
145  try {
146  TaskContainer* tc = dynamic_cast<TaskContainer*>( gamemap->tasks );
147  if ( tc ) {
148  while ( tc->pendingCommands.begin() != tc->pendingCommands.end() ) {
149  Command* c = tc->pendingCommands.front();
150  TaskInterface* ti = dynamic_cast<TaskInterface*>( c );
151 
152  if ( ti->operatable() ) {
153  ReplayContext context( gamemap );
154  ActionResult res = c->execute( context.getContext() );
155  if ( !res.successful() )
156  dispmessage2(res);
157  }
158 
159  tc->pendingCommands.erase( tc->pendingCommands.begin() );
160  }
161  }
162 
163  ActionResult res = gamemap->actions.rerun( createContext( gamemap ));
164  if ( !res.successful() )
165  displayActionError( res );
166  repaintMap();
167  } catch ( ActionResult res ) {
168  displayActionError( res );
169  }
170  Show();
171  selection->reLoad(true);
172  return true;
173  }
174 
175  ItemSelectorWidget* selection;
176 
177  public:
178  TaskManager( GameMap* map ) : ASC_PG_Dialog( NULL, PG_Rect( -1, -1, 600, 550 ), "Manage Tasks" ), gamemap( map )
179  {
181  AddStandardButton("Close")->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &TaskManager::ok )));
182  AddStandardButton("Run All")->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &TaskManager::run )));
183  selection = new ItemSelectorWidget( this, PG_Rect( 5, 20, Width()-10, Height() - 70 ), new TaskFactory( map ));
184  }
185 
186 };
187 
188 
189 void taskManager( GameMap* map )
190 {
191  if ( ! dynamic_cast<TaskContainer*>( map->tasks ) ) {
192  warningMessage("No Tasks to manage");
193  return;
194  }
195 
196  TaskManager am ( map );
197  am.Show();
198  am.RunModal();
199 }
200 
Context & getContext()
void reLoad(bool show=false)
void itemSelected(const SelectionWidget *widget, bool mouse)
Context createContext(GameMap *gamemap)
void StandardButtonDirection(StandardButtonDirectonType dir)
Definition: paradialog.cpp:511
TaskWidget(PG_Widget *parent, const PG_Point &pos, int width, Command *command, GameMap *map, TaskContainer *taskContainer)
Definition: taskmanager.cpp:74
void warningMessage(const ASCString &str)
vector< MapCoordinate > getCoordinates() const
sigc::signal< void > repaintMap
}@
Definition: spfst.cpp:45
A Command is an action that the player initiates.
Definition: command.h:114
struct MapDisplayPG::Cursor cursor
virtual bool operatable()=0
checks if the task can still be operated.
void dispmessage2(const ActionResult &result)
Definition: dialog.cpp:466
bool click()
Definition: taskmanager.cpp:84
ActionResult execute(const Context &context)
Definition: action.cpp:41
TaskManager(GameMap *map)
SelectionWidget * spawnNextItem(PG_Widget *parent, const PG_Point &pos)
Adapter class for using Paragui Dialogs in ASC. This class transfers the event control from ASC to Pa...
Definition: paradialog.h:127
creates a context that will move the map so that any action can be seen by the user ...
Definition: contextutils.h:28
void remove(Command *cmd)
AbstractTaskContainer * tasks
Definition: gamemap.h:394
bool successful() const
MapDisplayPG * getMapDisplay()
void restart()
CommandContainer pendingCommands
Definition: taskcontainer.h:44
ActionContainer actions
Records all action that have been done for undo/redo purposes.
Definition: gamemap.h:390
void goTo(const MapCoordinate &position)
MainScreenWidget * getMainScreenWidget()
ActionResult rerun(const Context &context)
reruns the necessary commands to propage take the changes done by setActive to the map ...
void displayActionError(const ActionResult &result, const ASCString &additionalInfo)
Definition: dialog.cpp:2168
PG_Button * AddStandardButton(const ASCString &name)
Definition: paradialog.cpp:516
TaskFactory(GameMap *map)
Definition: taskmanager.cpp:97
void taskManager(GameMap *map)
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182