Advanced Strategic Command
edittechadapter.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 
22 #include "edittechadapter.h"
23 
24 #include "../player.h"
25 #include "../gamemap.h"
26 
27 #include "../paradialog.h"
28 #include "../itemrepository.h"
29 #include "../iconrepository.h"
30 #include "../vehicletype.h"
31 #include "unitinfodialog.h"
32 
33 #include "selectionwindow.h"
34 
35 #include "pgmultilineedit.h"
36 #include "../widgets/textrenderer.h"
37 
39 {
40  ASCString name;
41  public:
42  TechAdapterWidget( PG_Widget* parent, const PG_Point& pos, int width, const ASCString& adapterName ) : SelectionWidget( parent, PG_Rect( pos.x, pos.y, width, 10 )), name(adapterName)
43  {
44  PG_Label* lbl1 = new PG_Label( this, PG_Rect( 5, 0, width - 10, Height() ), name );
45  lbl1->SetFontSize( lbl1->GetFontSize() -3 );
46 
47  SetTransparency( 255 );
48  };
49 
51  {
52  return name;
53  };
54 
55  int gap() {
56  return 2;
57  }
58 
59  protected:
60 
61  void display( SDL_Surface * surface, const PG_Rect & src, const PG_Rect & dst )
62  {
63  }
64  ;
65 };
66 
67 
68 
69 
70  class TechAdapterSelectionItemFactory: public SelectionItemFactory, public sigc::trackable {
71 
72  ASCString selected;
73 
74  void add( set<ASCString>& list, const TechAdapterDependency& adapter );
75 
76  public:
77  typedef set<ASCString> Container;
78 
79  protected:
80  Container::iterator it;
83 
84  public:
86  TechAdapterSelectionItemFactory( set<ASCString>& adapter );
87 
88 
89 
90  void restart();
91  sigc::signal<void,const ASCString&> techSelected;
92 
93  SelectionWidget* spawnNextItem( PG_Widget* parent, const PG_Point& pos );
94 
95  void itemSelected( const SelectionWidget* widget, bool mouse );
96  void itemMarked( const SelectionWidget* widget, bool mouse );
97 
98  ASCString getSelected() { return selected; };
99  };
100 
101 
102 
104  {
105  restart();
106  };
107 
108  TechAdapterSelectionItemFactory :: TechAdapterSelectionItemFactory( set<ASCString>& adapter ) : adapterForPlayer( &adapter )
109  {
110  restart();
111  };
112 
113 
114  void TechAdapterSelectionItemFactory::add( set<ASCString>& list, const TechAdapterDependency& adapter )
115  {
117  for ( TechAdapterDependency::RequiredTechAdapter::const_iterator i = deps.begin(); i != deps.end(); ++i ) {
118  list.insert( *i );
119  }
120  }
121 
122 
124  {
125  items.clear();
126 
127  if ( !adapterForPlayer ) {
128  for ( TechAdapterContainer::iterator i = techAdapterContainer.begin(); i != techAdapterContainer.end(); ++i )
129  items.insert( (*i)->getName() );
130  } else {
132  }
133 
134  it = items.begin();
135  };
136 
137 
138  SelectionWidget* TechAdapterSelectionItemFactory::spawnNextItem( PG_Widget* parent, const PG_Point& pos )
139  {
140  if ( it != items.end() ) {
141  ASCString v = *(it++);
142 
143  return new TechAdapterWidget( parent, pos, parent->Width() - 15, v );
144  } else
145  return NULL;
146  };
147 
148 
150  {
151  itemSelected(widget, mouse );
152  }
153 
155  {
156  if ( !widget )
157  return;
158 
159  const TechAdapterWidget* tw = dynamic_cast<const TechAdapterWidget*>(widget);
160  assert( tw );
161 
162  selected = tw->getName();
163  techSelected(tw->getName());
164  }
165 
166 
167 
169 {
170  ItemSelectorWidget* allItemSelector;
171  ItemSelectorWidget* playerItemSelector;
172  TechAdapterSelectionItemFactory* allAdapterfactory;
173  TechAdapterSelectionItemFactory* playerFactory;
174 
175  set<ASCString> playerAdapter;
176 
177  TextRenderer* objectList;
178  Player& player;
179 
180  bool containsAdapter( const ASCString& adapter, const TechAdapterDependency& dependency )
181  {
182  return find( dependency.listAdapters().begin(), dependency.listAdapters().end(), adapter ) != dependency.listAdapters().end();
183  }
184 
185  void techAdapterSelected( const ASCString& adapter )
186  {
187  const ASCString big = "#fontsize=14#";
188  const ASCString medium = "#fontsize=12#";
189  const ASCString small = "#fontsize=10#";
190 
191  ASCString complete = big + "TechAdapter " + adapter + " is used by:" + small + "\n";
192  ASCString local;
193 
194  for ( int i = 0; i < vehicleTypeRepository.getNum(); ++i )
195  if ( containsAdapter( adapter, vehicleTypeRepository.getObject_byPos(i)->techDependency ))
196  local += "#vehicletype=" + ASCString::toString(vehicleTypeRepository.getObject_byPos(i)->id) + "# " + vehicleTypeRepository.getObject_byPos(i)->getName() + " \n";
197 
198  if ( !local.empty() )
199  complete += medium + "VehicleTypes" + small + "\n" + local;
200  local.clear();
201 
202  for ( int i = 0; i < buildingTypeRepository.getNum(); ++i )
203  if ( containsAdapter( adapter, buildingTypeRepository.getObject_byPos(i)->techDependency ))
204  local += " " + buildingTypeRepository.getObject_byPos(i)->getName() + "\n";
205 
206  if ( !local.empty() )
207  complete += medium + "BuildingTypes" + small + "\n" + local;
208  local.clear();
209 
210  for ( int i = 0; i < objectTypeRepository.getNum(); ++i )
211  if ( containsAdapter( adapter, objectTypeRepository.getObject_byPos(i)->techDependency ))
212  local += " " + objectTypeRepository.getObject_byPos(i)->getName() + "\n";
213 
214  if ( !local.empty() )
215  complete += medium + "ObjectTypes" + small + "\n" + local;
216 
217  objectList->SetText( complete );
218  };
219 
220  protected:
221  bool handleButtonClick(PG_Button* button)
222  {
223  return false;
224  };
225 
226  bool ok()
227  {
228  player.research.setPredefinedTechAdapter( playerAdapter );
229  QuitModal();
230  return true;
231  }
232 
233  bool cancel()
234  {
235  QuitModal();
236  return true;
237  }
238 
239  bool add()
240  {
241  playerAdapter.insert( allAdapterfactory->getSelected() );
242  playerItemSelector->reLoad( true );
243  return true;
244  }
245 
246  bool remove()
247  {
248  set<ASCString>::iterator i = playerAdapter.find( playerFactory->getSelected());
249  if ( i != playerAdapter.end() )
250  playerAdapter.erase( playerFactory->getSelected() );
251  playerItemSelector->reLoad( true );
252 
253  return true;
254  }
255 
256  bool eventKeyDown(const SDL_KeyboardEvent* key)
257  {
258  int mod = SDL_GetModState() & ~(KMOD_NUM | KMOD_CAPS | KMOD_MODE);
259 
260  if ( !mod )
261  if ( key->keysym.sym == SDLK_RETURN )
262  return ok();
263 
264  return false;
265  };
266 
267 
268  public:
269  EditTechAdapter( Player& my_player ) : ASC_PG_Dialog( NULL, PG_Rect( -1, -1, 770, 600), "Edit Tech Adapter" ) , allAdapterfactory(NULL), playerFactory(NULL), player( my_player )
270  {
271  playerAdapter.insert( player.research.getPredefinedTechAdapter().begin(), player.research.getPredefinedTechAdapter().end() );
272 
273  new PG_Label(this, PG_Rect( 10, 30, 300, 20), "All TechAdapter");
274  PG_Rect left ( 10, 55, 300, Height() - 220 );
275 
276  new PG_Label(this, PG_Rect( 420, 30, 300, 20), "TechAdapter for " + player.getName() );
277  PG_Rect right( 420, 55, 300, Height() - 220 );
278 
279  Emboss* eleft = new Emboss( this, left, true );
280  Emboss* eright = new Emboss( this, right, true );
281 
282 
283  allAdapterfactory = new TechAdapterSelectionItemFactory();
284  allAdapterfactory->techSelected.connect( sigc::mem_fun( *this, &EditTechAdapter::techAdapterSelected ));
285  allItemSelector = new ItemSelectorWidget( eleft, PG_Rect( 10, 10, eleft->Width() - 20, eleft->Height()-20 ), allAdapterfactory );
286 
287  playerFactory = new TechAdapterSelectionItemFactory( playerAdapter );
288  playerFactory->techSelected.connect( sigc::mem_fun( *this, &EditTechAdapter::techAdapterSelected ));
289  playerItemSelector = new ItemSelectorWidget( eright, PG_Rect( 10, 10, eright->Width() - 20, eright->Height()-20 ), playerFactory );
290 
291 
292  (new PG_Button(this, PG_Rect( left.x + left.Width() + 10, left.y, right.x - left.x - left.Width() - 20, 30), "Add >>" ))->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &EditTechAdapter::add)));
293  (new PG_Button(this, PG_Rect( left.x + left.Width() + 10, left.y + 40, right.x - left.x - left.Width() - 20, 30), "Remove <<" ))->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &EditTechAdapter::remove)));
294 
295  Emboss* deps = new Emboss(this, PG_Rect ( 10, Height() - 160, 330, 120 ), true);
296  objectList = new TextRenderer( deps, PG_Rect(1,1,deps->Width() - 2, deps->Height() - 2) );
297 
298  AddStandardButton("~O~K")->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &EditTechAdapter::ok )));
299  AddStandardButton("~C~ancel")->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &EditTechAdapter::cancel )));
300  };
301 };
302 
303 
304 
305 
306 void editTechAdapter( Player& player )
307 {
308  EditTechAdapter eta( player);
309  eta.Show();
310  eta.RunModal();
311 }
312 
313 
314 
void reLoad(bool show=false)
const vector< ASCString > & getPredefinedTechAdapter()
Definition: research.h:234
ItemRepositoryLoader< ObjectType > objectTypeRepository("objecttype")
const RequiredTechAdapter & listAdapters() const
Definition: research.h:119
ASCString getName() const
returns the name of the player
Definition: player.cpp:274
void setPredefinedTechAdapter(const set< ASCString > &adapter)
Definition: research.cpp:920
void editTechAdapter(Player &player)
ItemRepositoryLoader< BuildingType > buildingTypeRepository("buildingtype")
SelectionWidget * spawnNextItem(PG_Widget *parent, const PG_Point &pos)
TechAdapterWidget(PG_Widget *parent, const PG_Point &pos, int width, const ASCString &adapterName)
ASCString getName() const
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
void SetText(const std::string &text)
ItemRepositoryLoader< VehicleType > vehicleTypeRepository("vehicletype")
void itemSelected(const SelectionWidget *widget, bool mouse)
TechAdapterContainer techAdapterContainer
Adapter class for using Paragui Dialogs in ASC. This class transfers the event control from ASC to Pa...
Definition: paradialog.h:127
void itemMarked(const SelectionWidget *widget, bool mouse)
bool handleButtonClick(PG_Button *button)
Research research
the status of the scientific research
Definition: player.h:142
bool eventKeyDown(const SDL_KeyboardEvent *key)
EditTechAdapter(Player &my_player)
vector< ASCString > RequiredTechAdapter
Definition: research.h:104
sigc::signal< void, const ASCString & > techSelected
the different players in ASC. There may be 8 players (0..7) and neutral units (8) ...
Definition: player.h:99
void display(SDL_Surface *surface, const PG_Rect &src, const PG_Rect &dst)
PG_Button * AddStandardButton(const ASCString &name)
Definition: paradialog.cpp:516