Advanced Strategic Command
playersetup.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 <sstream>
23 #include <pgimage.h>
24 
25 #include "playersetup.h"
26 
27 
28 
29 
30 
32 {
33  int counter = 0;
34  for ( int i = 0; i < gamemap->getPlayerCount(); ++i )
35  if ( gamemap->player[i].exist() )
36  ++counter;
37 
38  return yoffset + counter * spacing + 5;
39 }
40 
41 PlayerSetupWidget::PlayerSetupWidget( GameMap* gamemap, Mode mode, PG_Widget *parent, const PG_Rect &r, const std::string &style ) : PG_ScrollWidget( parent, r, style ) , actmap ( gamemap )
42 {
43  this->mode = mode;
44 
45  int counter = 0;
46  for ( int i = 0; i < actmap->getPlayerCount(); ++i )
47  if ( actmap->player[i].exist() ) {
48 
49  PlayerWidgets pw;
50  pw.pos = i;
51 
52  int y = yoffset + counter * spacing;
53 
54  ColoredBar* colbar = new ColoredBar( actmap->player[i].getColor(), this, PG_Rect( 20, y, Width() - 60, 30 ));
55  colbar->SetTransparency( 128 );
56 
57 
58  int y1 = Width() * 4 / 10;
59 
60  pw.name = new PG_LineEdit( colbar, PG_Rect( 40, 5, y1 - 40, 20 ));
61  pw.name->SetText( actmap->player[i].getName());
62 
63 
64  PG_Rect r = PG_Rect( y1 + 20, 5, colbar->Width() - y1 - 40, 20 );
65  if ( mode != SelfEditable || actmap->actplayer == i ) {
66  pw.type = new PG_DropDown( colbar, r);
67 
68  int pos = 0;
69  while ( Player :: playerStatusNames[pos] ) {
70  pw.type->AddItem( Player :: playerStatusNames[pos] );
71  ++pos;
72  }
73 
74  pw.type->SelectItem( actmap->player[i].stat );
75  pw.type->SetEditable(false);
76  } else {
77  pw.name->SetEditable( false );
78  pw.type = NULL;
79  PG_LineEdit* le = new PG_LineEdit( colbar, r );
80  le->SetText( Player :: playerStatusNames[ actmap->player[i].stat ] );
81  le->SetEditable( false );
82  }
83 
84  PG_ThemeWidget* col = new PG_ThemeWidget( colbar, PG_Rect( 5, 5, 20, 20 ));
85  col->SetSimpleBackground(true);
86  col->SetBackgroundColor ( actmap->player[i].getColor());
87  col->SetBorderSize(0);
88 
89  playerWidgets.push_back( pw );
90 
91  ++counter;
92  } else
93  actmap->player[i].stat = Player::off;
94 
95  SetTransparency(255);
96 };
97 
99  if ( mode == AllEditableSinglePlayer ) {
100  int humanNum = 0;
101  for ( vector<PlayerWidgets>::iterator i = playerWidgets.begin(); i != playerWidgets.end(); ++i )
102  if ( i->type )
103  if ( Player::PlayerStatus( i->type->GetSelectedItemIndex()) == Player::human
104  || Player::PlayerStatus( i->type->GetSelectedItemIndex()) == Player::supervisor
105  || Player::PlayerStatus( i->type->GetSelectedItemIndex()) == Player::suspended )
106  ++humanNum;
107 
108  if ( humanNum > 1 ) {
109  MessagingHub::Instance().error("Only a single human player allowed in SinglePlayer mode allowed!");
110  return false;
111  }
112  }
113  return true;
114 }
115 
116 
118  if ( !Valid() )
119  return false;
120 
121  for ( vector<PlayerWidgets>::iterator i = playerWidgets.begin(); i != playerWidgets.end(); ++i ) {
122  actmap->player[i->pos].setName( i->name->GetText() );
123  if ( i->type )
124  actmap->player[i->pos].stat = Player::PlayerStatus( i->type->GetSelectedItemIndex() );
125  }
126  return true;
127 };
128 
129 
131  PlayerSetupWidget* asw;
132  public:
133  PlayerSetupWindow( GameMap* actmap, bool allEditable, PG_Widget *parent, const PG_Rect &r ) : ASC_PG_Dialog( parent, r, "Players" )
134  {
135  asw = new PlayerSetupWidget( actmap, PlayerSetupWidget::AllEditable, this, PG_Rect( 5, 30, r.Width() - 10, r.Height() - 60 ));
136  PG_Button* ok = new PG_Button( this, PG_Rect( Width() - 200, Height() - 30, 90, 20 ), "OK" );
137  ok->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &PlayerSetupWindow::Apply )));
138  PG_Button* cancel = new PG_Button( this, PG_Rect( Width() - 100, Height() - 30, 90, 20 ), "Cancel" );
139  cancel->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &PlayerSetupWindow::QuitModal )));
140  }
141 
142  bool Apply()
143  {
144  asw->Apply();
145  QuitModal();
146  return true;
147  }
148 
149 };
150 
151 void setupPlayers( GameMap* actmap, bool supervisor )
152 {
153  PlayerSetupWindow asw ( actmap, supervisor, NULL, PG_Rect( 100, 100, 600, 500 ));
154  asw.Show();
155  asw.RunModal();
156 }
static const char * playerStatusNames[]
Definition: player.h:156
ASCString getName() const
returns the name of the player
Definition: player.cpp:274
void setName(const ASCString &name)
Definition: player.h:160
void setupPlayers(GameMap *actmap, bool supervisor)
enum Player::PlayerStatus stat
Adapter class for using Paragui Dialogs in ASC. This class transfers the event control from ASC to Pa...
Definition: paradialog.h:127
PlayerSetupWindow(GameMap *actmap, bool allEditable, PG_Widget *parent, const PG_Rect &r)
DI_Color getColor() const
Definition: player.cpp:249
signed char actplayer
the player who is currently making his moves (may be human or AI)
Definition: gamemap.h:232
Player player[9]
Definition: gamemap.h:253
PlayerStatus
the status of the player
Definition: player.h:150
static int guessHeight(GameMap *gamemap)
Definition: playersetup.cpp:31
GameMap * actmap
Definition: spfst.cpp:64
PlayerSetupWidget(GameMap *gamemap, Mode mode, PG_Widget *parent, const PG_Rect &r, const std::string &style="ScrollWidget")
Definition: playersetup.cpp:41
int getPlayerCount() const
Definition: gamemap.h:255
bool exist() const
does the player exist at all
Definition: player.cpp:313
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182