Advanced Strategic Command
gameevent_dialogs.cpp
Go to the documentation of this file.
1 
5 /*
6  This file is part of Advanced Strategic Command; http://www.asc-hq.de
7  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
8 
9  This program is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; see the file COPYING. If not, write to the
21  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22  Boston, MA 02111-1307 USA
23 */
24 
25 #include <stdio.h>
26 
27 #include "global.h"
28 #include "buildingtype.h"
29 #include "vehicletype.h"
30 #include "newfont.h"
31 #include "typen.h"
32 // #include "basegfx.h"
33 #include "dlg_box.h"
34 #include "newfont.h"
35 #include "vehicletype.h"
36 #include "buildingtype.h"
37 #include "spfst.h"
38 
39 #include "gameevents.h"
40 // #include "events.h"
41 #include "gameoptions.h"
42 #include "loadimage.h"
43 #include "errors.h"
44 #include "itemrepository.h"
45 #include "gameevent_dialogs.h"
46 #include "dialog.h"
47 #include "spfst-legacy.h"
48 
49 #include "dialogs/fieldmarker.h"
50 #include "widgets/textrenderer.h"
53 #include <pgpropertyeditor.h>
54 #include <pgpropertyfield_checkbox.h>
55 
56 #ifdef karteneditor
57 # include "edmisc.h"
58 # include "edselfnt.h"
59 # include "maped-mainscreen.h"
60  extern int selectfield(int * cx ,int * cy);
61 
62 #else
63 int selectfield(int * cx ,int * cy)
64 {
65  return 0;
66 }
67 void editpolygon (Poly_gon& poly) {};
68 Vehicle* selectUnitFromMap() { return NULL; };
69 #endif
70 
71 // �S GetXYSel
72 
73 bool chooseWeather( int& weather )
74 {
75 #ifdef karteneditor
76  vector<ASCString> entries;
77 
78  for ( int w = 0; w < cwettertypennum; ++w )
79  entries.push_back ( cwettertypen[ w ] );
80 
81  int value = chooseString ( "choose operation target", entries, weather );
82  if ( value < 0 )
83  return false;
84  else
85  weather = value;
86 
87 #endif
88  return true;
89 }
90 
91 bool chooseTerrain( int& terrainID )
92 {
93 #ifdef karteneditor
94  selectItemID( terrainID, terrainTypeRepository );
95 #endif
96  return true;
97 }
98 
99 bool chooseObject( int& objectID )
100 {
101 #ifdef karteneditor
102  selectItemID( objectID, objectTypeRepository );
103 #endif
104  return true;
105 }
106 
107 bool chooseVehicleType( int& vehicleTypeID )
108 {
109 #ifdef karteneditor
110  selectItemID( vehicleTypeID, vehicleTypeRepository );
111 #endif
112  return true;
113 }
114 
115 #if 0
116 
117 class tgetxy : public tdialogbox {
118  ASCString titlename;
119  public :
120  int action;
121  int x,y;
122  void init(void);
123  virtual void run(void);
124  virtual ASCString getTitle() { return "X/Y Pos"; };
125  virtual int condition(void);
126  virtual void buttonpressed(int id);
127  };
128 
129 void tgetxy::init(void)
130 {
132 
133  titlename = getTitle();
134  title = titlename.c_str();
135  x1 = 150;
136  xsize = 320;
137  y1 = 150;
138  ysize = 140;
139  action = 0;
140 
141  windowstyle = windowstyle ^ dlg_in3d;
142 
143 
144  addbutton("~D~one",20,ysize - 40,100,ysize - 20,0,1,1,true);
145  addkey(1,ct_enter);
146  addbutton("~C~ancel",120,ysize - 40,200,ysize - 20,0,1,2,true);
147  addbutton("~M~ap",220,ysize - 40,300,ysize - 20,0,1,3,true);
148  addbutton("~X~-Pos",20,60,150,80,2,1,4,true);
149  addeingabe(4,&x,-1,actmap->xsize - 1);
150  addbutton("~Y~-Pos",170,60,300,80,2,1,5,true);
151  addeingabe(5,&y,-1,actmap->ysize - 1);
152 
153  buildgraphics();
154 
155  mousevisible(true);
156 }
157 
158 
159 void tgetxy::run(void)
160 {
161 
162  do {
163  tdialogbox::run();
164  } while (! ( (taste == ct_esc) || (action == 1) || (action == 2) ) );
165  if ((action == 2) || (taste == ct_esc)) x = 50000;
166 }
167 
168 int tgetxy::condition(void)
169 {
170  return 1;
171 }
172 
173 void tgetxy::buttonpressed(int id)
174 {
175 
177  switch (id) {
178 
179  case 1:
180  case 2: action = id;
181  break;
182  case 3: {
183  mousevisible(false);
184  x = actmap->getCursor().x;
185  y = actmap->getCursor().y;
186  displaymap();
187  mousevisible(true);
188  do {
189  if ( !selectfield(&x,&y) )
190  break;
191  } while ( condition() != 1 ); /* enddo */
192  mousevisible(false);
193  redraw();
194  enablebutton(4);
195  enablebutton(5);
196  mousevisible(true);
197 
198  }
199  break;
200  }
201 }
202 
203 
204 void getxy( int *x, int *y)
205 { tgetxy ce;
206 
207  ce.x = *x ;
208  ce.y = *y ;
209  ce.init();
210  ce.run();
211  if ( ce.x != 50000 )
212  {
213  *x = ce.x;
214  *y = ce.y;
215  }
216  ce.done();
217 }
218 
219 
221 {
222  typedef vector<MapCoordinate>::iterator It;
223  vector<MapCoordinate> backup = fields;
224 
225  actmap->cleartemps();
226  for ( It i = fields.begin(); i != fields.end(); ++i )
227  actmap->getField(*i)->a.temp = 1;
228 
229  displaymap();
230  displaymessage("use space to select the fields\nfinish the selection by pressing enter",3);
231  int res ;
232  do {
233  int x,y;
234  res = selectfield(&x,&y);
235  if ( res == 2 ) {
236  It i = find ( fields.begin(), fields.end(), MapCoordinate (x,y) );
237  if ( i != fields.end() ) {
238  actmap->getField(*i)->a.temp = 0;
239  fields.erase( i );
240  } else {
241  actmap->getField(x,y)->a.temp = 1;
242  fields.push_back ( MapCoordinate(x,y));
243  }
244  displaymap();
245  }
246 
247  } while ( res == 2 ); /* enddo */
248 
249  if ( res == 0 )
250  fields = backup;
251 
252  actmap->cleartemps();
253  displaymap();
254 }
255 
256 
257 // �S GetXYUnitSel
258 
259 class tgetxyunit : public tgetxy {
260  public :
261  ASCString getTitle() { return "Select Unit"; };
262  virtual int condition(void);
263  //virtual bool container( ContainerBase* cnt );
264  };
265 
266 
267 int tgetxyunit::condition(void)
268 {
269  if ( getfield( x, y ) )
270  if ( getfield(x,y)->vehicle )
271  return 1;
272  return 0;
273 }
274 
275 
276 
277 void getxy_unit(int *x,int *y)
278 { tgetxyunit gu;
279 
280  gu.x = *x;
281  gu.y = *y;
282  gu.init();
283  gu.run();
284  *x = gu.x;
285  *y = gu.y;
286  gu.done();
287 }
288 
289 class tgetxybuilding : public tgetxy {
290  public :
291  virtual int condition(void);
292  };
293 
294 
295 // �S GetXYBuildingSel
296 
297 int tgetxybuilding::condition(void)
298 {
299  if ( getfield( x, y ) )
300  if ( getfield(x,y)->building )
301  return 1;
302  return 0;
303 }
304 
305 void getxy_building(int *x,int *y)
306 { tgetxybuilding gb;
307 
308  gb.x = *x ;
309  gb.y = *y ;
310  gb.init();
311  gb.run();
312  *x = gb.x;
313  *y = gb.y;
314  gb.done();
315 }
316 
317 
318 #endif
319 
320 
321 void getxy_building(int *x,int *y)
322 {
324  list.push_back ( MapCoordinate( *x, *y ));
325 
326  SelectBuildingFromMap sbfm( list, actmap );
327  sbfm.Show();
328  sbfm.RunModal();
329 
330  if ( list.empty() ) {
331  *x = -1;
332  *y = -1;
333  } else {
334  *x = list.begin()->x;
335  *y = list.begin()->y;
336  }
337 }
338 
340 {
341  SelectFromMap sbfm( fields, actmap );
342  sbfm.Show();
343  sbfm.RunModal();
344 }
345 
346 class UnitListFactory: public SelectionItemFactory, public sigc::trackable {
347  public:
348  typedef list<const VehicleType*> UnitList;
349  private:
350  const UnitList& unitList;
351  protected:
352  UnitList::const_iterator it;
353  void itemSelected( const SelectionWidget* widget, bool mouse )
354  {
355  };
356  public:
357  UnitListFactory( const UnitList& units ) : unitList( units )
358  {
359  restart();
360  };
361 
362  void restart()
363  {
364  it = unitList.begin();
365  }
366 
367  SelectionWidget* spawnNextItem( PG_Widget* parent, const PG_Point& pos )
368  {
369  if ( it != unitList.end() ) {
370  const VehicleType* v = *(it++);
371  return new VehicleTypeBaseWidget( parent, pos, parent->Width() - 15, v, actmap->getCurrentPlayer() );
372  } else
373  return NULL;
374  };
375 };
376 
377 
378 
380  private:
381  bool eventKeyDown(const SDL_KeyboardEvent* key)
382  {
383  if ( key->keysym.sym == SDLK_RETURN ) {
384  QuitModal();
385  return true;
386  }
387  return ItemSelectorWindow::eventKeyDown( key );
388  };
389 
390  public:
391  UnitAvailabilityWindow ( PG_Widget *parent, const PG_Rect &r , const ASCString& title, UnitListFactory* itemFactory )
392  : ItemSelectorWindow( parent, r, title, itemFactory )
393  {
394  };
395 };
396 
397 
398 
399 void ShowNewTechnology::showTechnology( const Technology* tech, const TechnologyPresenter::Gadgets& newGadgetsAvailable )
400 {
401  if ( tech ) {
402  ASCString text = "#fontsize=18#Research completed#fontsize=12#\n\n";
403 
404  text = "Our scientists have mastered a new technology:\n\n#fontsize=18#";
405 
406  text += tech->name + "#fontsize=12#\n\n";
407 
408  if ( tech->relatedUnitID > 0 )
409  text += "#vehicletype=" + ASCString::toString(tech->relatedUnitID) + "#\n\n";
410 
411  text += tech->infotext;
412 
413  if ( newGadgetsAvailable.units.size() ) {
414  text += "\n#fontsize=15#\nNew units available for production:#fontsize=12#\n";
415 
416  for ( std::list<const VehicleType*>::const_iterator i = newGadgetsAvailable.units.begin(); i != newGadgetsAvailable.units.end(); ++i )
417  text += "#vehicletype=" + ASCString::toString((*i)->id) + "#\n";
418  }
419 
420  ViewFormattedText tr ("Research", text, PG_Rect(-1,-1, 400,250) );
421  tr.Show();
422  tr.RunModal();
423  }
424 }
425 
426 
427 
428 int selectunit ( int unitnetworkid )
429 {
431 
432  Vehicle* v = actmap->getUnit ( unitnetworkid );
433  if ( v )
434  list.push_back ( v->getPosition() );
435 
436  SelectUnitFromMap sufm ( list, actmap );
437  sufm.Show();
438  sufm.RunModal();
439 
440  if ( list.empty() )
441  return 0;
442 
443  MapField* fld = actmap->getField( *list.begin() );
444  if ( fld && fld->vehicle )
445  return fld->vehicle->networkid;
446  else
447  return 0;
448 }
449 
450 
451 class tplayerselall : public tdialogbox {
452  public :
453  int action;
454  int bkgcolor;
456  void init(void);
457  virtual void run(void);
458  virtual void buttonpressed(int id);
459  void anzeige(void);
460  };
461 
462 
463 
464 
466 {
467  char *s1;
468 
469  tdialogbox::init();
470  title = "Player Select";
471  x1 = 50;
472  xsize = 370;
473  y1 = 50;
474  ysize = 380;
475  action = 0;
477 
479 
480  int i;
481  for (i=0;i<8 ;i++ ) {
482  s1 = new(char[12]);
483  strcpy(s1,"Player ~");
484  strcat(s1,strrr(i+1));
485  strcat(s1,"~");
486  addbutton(s1,20,55+i*30,150,75+i*30,0,1,6+i,true);
487  addkey(1,ct_1+i);
488  }
489 
490 // addbutton("~A~ll not allied",20,ysize - 40,170,ysize - 20,0,1,1,true);
491 // addkey(1,ct_enter);
492  addbutton("~O~K",200,ysize - 40,350,ysize - 20,0,1,2,true);
493 
494  buildgraphics();
495 
496  for ( i=0; i<8 ;i++ )
497  bar(x1 + 170,y1 + 60 + i*30 ,x1 + 190 ,y1 + 70 + i * 30,20 + ( i << 3 ));
498 
499  anzeige();
500 
501  mousevisible(true);
502 }
503 
505 {
506  mousevisible(false);
507  for (int i=0;i<8 ;i++ ) {
508  if ( playerbit & ( 1 << i ) )
509  rectangle (x1 + 16,y1+51+i*30,x1+154,y1+79+i*30, 20 );
510  else
511  rectangle (x1 + 16,y1+51+i*30,x1+154,y1+79+i*30, bkgcolor );
512  }
513  mousevisible(true);
514 }
515 
516 
518 {
519 
520  do {
521  tdialogbox::run();
522  // if (taste == ct_f1) help ( 1060 );
523  } while (!((taste == ct_esc) || ((action == 1) || (action ==2))));
524  if ((action == 1) || (taste == ct_esc))
525  playerbit = 1;
526 }
527 
528 
530 {
532  switch (id) {
533 
534  case 1:
535  case 2: action = id;
536  break;
537  case 6:
538  case 7:
539  case 8:
540  case 9:
541  case 10:
542  case 11:
543  case 12:
544  case 13: {
545  playerbit ^= 1 << ( id-6 ) ;
546  anzeige();
547  }
548  break;
549  }
550 }
551 
552 
553 void playerselall( int *playerbitmap)
554 { tplayerselall sc;
555 
556  sc.playerbit = *playerbitmap;
557  sc.init();
558  sc.run();
559  sc.done();
560  *playerbitmap = sc.playerbit;
561 }
562 
563 
564 
566 {
567  MapCoordinate pos = actmap->getCursor();
568  if ( !accept(pos))
569  return false;
570 
571  CoordinateList::iterator i = find( coordinateList.begin(), coordinateList.end(), pos );
572  if ( i == coordinateList.end() )
573  coordinateList.push_back ( pos );
574 
575  cut( pos );
576 
578 
579  updateList();
580  return true;
581 }
582 
583 void ReinforcementSelector::cut( const MapCoordinate& pos )
584 {
585  MapField* fld = actmap->getField( pos );
586  if (!fld )
587  return;
588 
589  cutPositions.push_back( pos );
590 
591  if ( fld->vehicle ) {
592  MemoryStream stream ( &buf, tnstream::appending );
593  stream.writeInt( Reinforcements::ReinfVehicle );
594  fld->vehicle->write ( stream );
595  objectNum++;
596  delete fld->vehicle;
597  fld->vehicle = NULL;
598  } else
599  if ( fld->building ) {
600  MemoryStream stream ( &buf, tnstream::appending );
601  stream.writeInt( Reinforcements::ReinfBuilding );
602  fld->building->write ( stream );
603  objectNum++;
604  delete fld->building;
605  fld->building = NULL;
606  }
607 }
608 
609 
611 {
612  for ( CoordinateList::const_iterator i = coordinateList.begin(); i != coordinateList.end(); ++i )
613  if ( find( cutPositions.begin(), cutPositions.end(), *i ) == cutPositions.end() )
614  cut( *i );
615  return true;
616 }
617 
618 
619 bool BitMapEditor::ok()
620 {
621  reference = 0;
622  propertyEditor->Apply();
623  for ( int i = 0; i < bitCount; ++i )
624  if ( values[i] )
625  reference |= 1 << i;
626 
627  QuitModal();
628  return true;
629 }
630 
631 BitMapEditor::BitMapEditor( BitType& value, const ASCString& title, const vector<ASCString>& names ) : ASC_PG_Dialog(NULL, PG_Rect(-1,-1,300,500), title ), reference(value)
632 {
633  propertyEditor = new ASC_PropertyEditor( this, PG_Rect( 10, GetTitlebarHeight(), Width() - 20, Height() - GetTitlebarHeight() - 50 ), "PropertyEditor", 70 );
634 
635 
636  bitCount = names.size();
637  int counter = 0;
638  for ( vector<ASCString>::const_iterator i = names.begin(); i != names.end(); ++i ) {
639  bool v = value & (1 << counter );
640  values[counter] = v;
641  new PG_PropertyField_Checkbox<bool>( propertyEditor, *i, &(values[counter]) );
642  ++counter;
643  }
644 
645  PG_Button* ok = new PG_Button( this, PG_Rect( Width() - 100, Height() - 40, 90, 30), "OK" );
646  ok->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &BitMapEditor::ok )));
647 }
648 
void write(tnstream &stream, bool includeLoadedUnits=true) const
Definition: vehicle.cpp:1121
Player & getCurrentPlayer()
Definition: gamemap.h:268
UnitList::const_iterator it
void displaymap()
Definition: spfst.cpp:58
The (base-) classes which are thrown as exceptions.
int x1
Definition: dlg_box.h:40
int relatedUnitID
Definition: research.h:139
ItemRepositoryLoader< ObjectType > objectTypeRepository("objecttype")
void cleartemps(int b=-1, int value=0)
Definition: gamemap.cpp:964
MapCoordinate & getCursor()
Definition: gamemap.cpp:933
int xsize
the size of the map
Definition: gamemap.h:201
map accessing and usage routines used by ASC and the mapeditor
bool chooseTerrain(int &terrainID)
Vehicle * vehicle
Definition: mapfield.h:89
void getxy_building(int *x, int *y)
int y1
Definition: dlg_box.h:40
const char * cwettertypen[cwettertypennum]
the names of the different weather
Definition: typen.cpp:78
virtual void run(void)
Definition: dlg_box.cpp:1477
CoordinateList & coordinateList
Definition: fieldmarker.h:48
virtual void buttonpressed(int id)
Definition: dlg_box.cpp:369
bool eventKeyDown(const SDL_KeyboardEvent *key)
int xsize
Definition: dlg_box.h:40
list< const VehicleType * > UnitList
virtual bool accept(const MapCoordinate &pos)
Definition: fieldmarker.cpp:89
void buildgraphics(void)
Definition: dlg_box.cpp:1180
MapCoordinate3D getPosition() const
returns the units position
Definition: vehicle.cpp:1552
#define ct_enter
Definition: keysymbols.h:51
Global platform dependant definitions. This file just branches to the platform specific files in thei...
Interface for some basic classes from which all of ASC's dialogs are derived.
a single field of the map
Definition: mapfield.h:26
void itemSelected(const SelectionWidget *widget, bool mouse)
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
SelectionWidget * spawnNextItem(PG_Widget *parent, const PG_Point &pos)
int selectfield(int *cx, int *cy)
tkey taste
Definition: dlg_box.h:88
bool chooseVehicleType(int &vehicleTypeID)
int ysize
Definition: dlg_box.h:40
MapField * getfield(int x, int y)
returns the field at the given coordinates
Definition: spfst.cpp:199
virtual void run(void)
The class describing properties that are common to all vehicles of a certain kind.
Definition: vehicletype.h:177
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
void done(void)
Definition: dlg_box.cpp:1235
std::list< const VehicleType * > units
void Show(bool fade=false)
ItemRepositoryLoader< VehicleType > vehicleTypeRepository("vehicletype")
char * strrr(int a)
converts a to a string.
Definition: misc.cpp:66
int windowstyle
Definition: dlg_box.h:163
Interface to the event handling of ASC.
ItemRepositoryLoader< TerrainType > terrainTypeRepository("terraintype")
Interface to game event dialogs.
Coordinate on the twodimensional map.
Definition: typen.h:202
Adapter class for using Paragui Dialogs in ASC. This class transfers the event control from ASC to Pa...
Definition: paradialog.h:127
ASCString infotext
Definition: research.h:128
Reads data from or writes data to a MemoryStreamStorage This allows a completely volatile storage of ...
Definition: basestrm.h:204
void displaymessage(const char *formatstring, int num,...)
displays a dialog box with a message
Definition: dlg_box.cpp:1849
void addbutton(const char *ltxt, int lx1, int ly1, int lx2, int ly2, int lart, int lstyle, int lid, bool enabled)
Definition: dlg_box.cpp:471
#define ct_esc
Definition: keysymbols.h:43
void editpolygon(Poly_gon &poly)
The interface for the buildingtype class.
bool chooseWeather(int &weather)
void playerselall(int *playerbitmap)
void bar(tmouserect rect, int color)
Definition: dlg_box.cpp:537
void addkey(int id, tkey key)
Definition: dlg_box.cpp:774
Vehicle * getUnit(int x, int y, int nwid)
Definition: gamemap.cpp:1215
int ysize
Definition: gamemap.h:201
virtual void showFieldMarking(const CoordinateList &coordinateList)
Definition: fieldmarker.cpp:93
bool selectItemID(int &id, const ItemRepository< ItemType > &itemRepository)
opens a window in which the user can select an item of the given type ( template parameter ItemType )...
Definition: edselfnt.h:210
#define ct_1
Definition: keysymbols.h:108
virtual void showTechnology(const Technology *tech, const Gadgets &newGadgetsAvailable)
int selectunit(int unitnetworkid)
void selectFields(FieldAddressing::Fields &fields)
Interface for all the dialog boxes used by the game and the mapeditor.
vector< MapCoordinate > CoordinateList
Definition: fieldmarker.h:45
GameMap * actmap
Definition: fieldmarker.h:47
vector< MapCoordinate > Fields
Definition: gameevents.h:89
Building * building
Definition: mapfield.h:102
ASCString name
Definition: research.h:137
#define dlg_in3d
Definition: dlg_box.h:33
GameMap * actmap
Definition: spfst.cpp:64
void init(void)
Definition: dlg_box.cpp:314
bool chooseObject(int &objectID)
BitMapEditor(BitType &value, const ASCString &title, const vector< ASCString > &names)
virtual void buttonpressed(int id)
void mousevisible(int an)
Definition: events.cpp:75
Vehicle * selectUnitFromMap()
int chooseString(const ASCString &title, const vector< ASCString > &entries, int defaultEntry)
displays a dialogbox which lets you choose one of a number of strings.
Definition: dlg_box.cpp:3788
const int cwettertypennum
The number of different weather.
Definition: typen.h:61
int networkid
a unique identification of the unit that is used everywhere in ASC (and not only the network protocol...
Definition: vehicle.h:140
const int lightgray
Definition: basegfx.h:94
void rectangle(int x1, int y1, int x2, int y2, Uint8 color)
draws a simple rectangl
Definition: basegfx.cpp:297
UnitAvailabilityWindow(PG_Widget *parent, const PG_Rect &r, const ASCString &title, UnitListFactory *itemFactory)
const char * title
Definition: dlg_box.h:162
UnitListFactory(const UnitList &units)
MapField * getField(int x, int y)
Definition: gamemap.h:465
void write(tnstream &stream, bool includeLoadedUnits=true) const
Definition: buildings.cpp:451