actionmanager.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 "actionmanager.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 "actionwidget.h"
00039 
00040 
00041 class ActionSelectionWidget: public ActionWidget
00042 {
00043       
00044       ActionContainer& actions;
00045       PG_CheckButton* check;
00046       
00047    public:
00048       ActionSelectionWidget( PG_Widget* parent, const PG_Point& pos, int width, const Command& action, ActionContainer& actionContainer, GameMap* map ) 
00049          : ActionWidget( parent,pos, width, action, map ) ,  actions( actionContainer )
00050       {
00051          check = new PG_CheckButton( this, PG_Rect( 5, (Height()-15)/2, 15, 15 ));
00052          if ( actions.isActive_req( &action ) )
00053             check->SetPressed();
00054          
00055          check->sigClick.connect( SigC::slot( *this, &ActionSelectionWidget::click ));
00056       }
00057    protected:
00058       bool click( )
00059       {
00060          actions.setActive( &act, check->GetPressed() );
00061          return true;
00062       }
00063 
00064 };
00065 
00066 
00067 class ActionFactory : public SelectionItemFactory {
00068       ActionContainer& actions;
00069       ActionContainer::Actions::const_iterator a;
00070       GameMap* map;
00071    public:
00072       ActionFactory( GameMap* map ) : actions( map->actions )
00073       {
00074          this->map = map;
00075          restart();
00076       };
00077       
00078       void restart()
00079       {
00080          a = actions.getActions().begin();
00081       };
00082       
00083       SelectionWidget* spawnNextItem( PG_Widget* parent, const PG_Point& pos ) 
00084       {
00085          if ( a != actions.getActions().end() ) {
00086             return new ActionSelectionWidget( parent, pos, parent->Width() - 20, **(a++), actions, map );
00087          } else
00088             return NULL;
00089       }
00090       
00091       void itemSelected( const SelectionWidget* widget, bool mouse ) 
00092       {
00093          if ( mouse ) {
00094             const ActionSelectionWidget* asw = dynamic_cast<const ActionSelectionWidget*>( widget );
00095             if ( asw ) {
00096                vector<MapCoordinate>  pos = asw->getCoordinates();
00097                if ( pos.size() > 0 ) {
00098                   MapDisplayPG* md = getMainScreenWidget()->getMapDisplay();
00099                   md->cursor.goTo( pos[0] );
00100                }
00101             }
00102          }
00103       }
00104 
00105 };
00106 
00107 class ActionManager : public ASC_PG_Dialog {
00108       GameMap* gamemap;
00109       
00110       bool ok() {
00111          QuitModal();  
00112          return true;
00113       }
00114       
00115       bool run() {
00116          Hide();
00117          try {
00118             ActionResult res = gamemap->actions.rerun( createContext( gamemap ));
00119             if ( !res.successful() )
00120                displayActionError( res );
00121             repaintMap();
00122          } catch ( ActionResult res ) {
00123             displayActionError( res );
00124          }
00125          Show();
00126          return true;  
00127       }
00128       
00129       ItemSelectorWidget* selection;
00130       
00131    public:   
00132       ActionManager( GameMap* map ) : ASC_PG_Dialog( NULL, PG_Rect( -1, -1, 400, 550 ), "Manage Actions" ), gamemap( map ) 
00133       {
00134          StandardButtonDirection( Horizontal );
00135          AddStandardButton("Close")->sigClick.connect( SigC::slot( *this, &ActionManager::ok ));
00136          AddStandardButton("Run")->sigClick.connect( SigC::slot( *this, &ActionManager::run ));
00137          selection = new ItemSelectorWidget( this, PG_Rect( 5, 20, Width()-10, Height() - 70 ), new ActionFactory( map ));
00138       }
00139    
00140 };
00141 
00142 
00143 void actionManager( GameMap* map )
00144 {
00145    ActionManager am ( map );
00146    am.Show();
00147    am.RunModal();  
00148 }
00149 

Generated on Mon May 14 01:31:39 2012 for Advanced Strategic Command by  doxygen 1.5.1