Advanced Strategic Command
actionmanager.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 "actionmanager.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 "actionwidget.h"
39 
40 
42 {
43 
44  ActionContainer& actions;
45  PG_CheckButton* check;
46 
47  public:
48  ActionSelectionWidget( PG_Widget* parent, const PG_Point& pos, int width, const Command& action, ActionContainer& actionContainer, GameMap* map )
49  : ActionWidget( parent,pos, width, action, map ) , actions( actionContainer )
50  {
51  check = new PG_CheckButton( this, PG_Rect( 5, (Height()-15)/2, 15, 15 ));
52  if ( actions.isActive_req( &action ) )
53  check->SetPressed();
54 
55  check->sigClick.connect( sigc::mem_fun( *this, &ActionSelectionWidget::click ));
56  }
57  protected:
58  bool click( bool b )
59  {
60  actions.setActive( &act, check->GetPressed() );
61  return true;
62  }
63 
64 };
65 
66 
68  ActionContainer& actions;
69  ActionContainer::Actions::const_iterator a;
70  GameMap* map;
71  public:
72  ActionFactory( GameMap* map ) : actions( map->actions )
73  {
74  this->map = map;
75  restart();
76  };
77 
78  void restart()
79  {
80  a = actions.getActions().begin();
81  };
82 
83  SelectionWidget* spawnNextItem( PG_Widget* parent, const PG_Point& pos )
84  {
85  if ( a != actions.getActions().end() ) {
86  return new ActionSelectionWidget( parent, pos, parent->Width() - 20, **(a++), actions, map );
87  } else
88  return NULL;
89  }
90 
91  void itemSelected( const SelectionWidget* widget, bool mouse )
92  {
93  if ( mouse ) {
94  const ActionSelectionWidget* asw = dynamic_cast<const ActionSelectionWidget*>( widget );
95  if ( asw ) {
96  vector<MapCoordinate> pos = asw->getCoordinates();
97  if ( pos.size() > 0 ) {
99  md->cursor.goTo( pos[0] );
100  }
101  }
102  }
103  }
104 
105 };
106 
107 class ActionManager : public ASC_PG_Dialog {
108  GameMap* gamemap;
109 
110  bool ok() {
111  QuitModal();
112  return true;
113  }
114 
115  bool run() {
116  Hide();
117  try {
118  ActionResult res = gamemap->actions.rerun( createContext( gamemap ));
119  if ( !res.successful() )
120  displayActionError( res );
121  repaintMap();
122  } catch ( ActionResult res ) {
123  displayActionError( res );
124  }
125  Show();
126  return true;
127  }
128 
129  ItemSelectorWidget* selection;
130 
131  public:
132  ActionManager( GameMap* map ) : ASC_PG_Dialog( NULL, PG_Rect( -1, -1, 400, 550 ), "Manage Actions" ), gamemap( map )
133  {
135  AddStandardButton("Close")->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &ActionManager::ok )));
136  AddStandardButton("Run")->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &ActionManager::run )));
137  selection = new ItemSelectorWidget( this, PG_Rect( 5, 20, Width()-10, Height() - 70 ), new ActionFactory( map ));
138  }
139 
140 };
141 
142 
143 void actionManager( GameMap* map )
144 {
145  ActionManager am ( map );
146  am.Show();
147  am.RunModal();
148 }
149 
ActionManager(GameMap *map)
void itemSelected(const SelectionWidget *widget, bool mouse)
Context createContext(GameMap *gamemap)
void StandardButtonDirection(StandardButtonDirectonType dir)
Definition: paradialog.cpp:511
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
void setActive(const Command *action, bool active)
struct MapDisplayPG::Cursor cursor
const Actions & getActions() const
ActionFactory(GameMap *map)
void actionManager(GameMap *map)
ActionSelectionWidget(PG_Widget *parent, const PG_Point &pos, int width, const Command &action, ActionContainer &actionContainer, GameMap *map)
SelectionWidget * spawnNextItem(PG_Widget *parent, const PG_Point &pos)
const Command & act
Definition: actionwidget.h:39
bool isActive_req(const Command *action)
returns if the command is active in the queued command list
Adapter class for using Paragui Dialogs in ASC. This class transfers the event control from ASC to Pa...
Definition: paradialog.h:127
bool successful() const
MapDisplayPG * getMapDisplay()
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
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182