00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <sstream>
00023 #include <pgimage.h>
00024
00025 #include "playersetup.h"
00026
00027
00028
00029
00030
00031 int PlayerSetupWidget::guessHeight( GameMap* gamemap )
00032 {
00033 int counter = 0;
00034 for ( int i = 0; i < gamemap->getPlayerCount(); ++i )
00035 if ( gamemap->player[i].exist() )
00036 ++counter;
00037
00038 return yoffset + counter * spacing + 5;
00039 }
00040
00041 PlayerSetupWidget::PlayerSetupWidget( GameMap* gamemap, Mode mode, PG_Widget *parent, const PG_Rect &r, const std::string &style ) : PG_ScrollWidget( parent, r, style ) , actmap ( gamemap )
00042 {
00043 this->mode = mode;
00044
00045 int counter = 0;
00046 for ( int i = 0; i < actmap->getPlayerCount(); ++i )
00047 if ( actmap->player[i].exist() ) {
00048
00049 PlayerWidgets pw;
00050 pw.pos = i;
00051
00052 int y = yoffset + counter * spacing;
00053
00054 ColoredBar* colbar = new ColoredBar( actmap->player[i].getColor(), this, PG_Rect( 20, y, Width() - 60, 30 ));
00055 colbar->SetTransparency( 128 );
00056
00057
00058 int y1 = Width() * 4 / 10;
00059
00060 pw.name = new PG_LineEdit( colbar, PG_Rect( 40, 5, y1 - 40, 20 ));
00061 pw.name->SetText( actmap->player[i].getName());
00062
00063
00064 PG_Rect r = PG_Rect( y1 + 20, 5, colbar->Width() - y1 - 40, 20 );
00065 if ( mode != SelfEditable || actmap->actplayer == i ) {
00066 pw.type = new PG_DropDown( colbar, r);
00067
00068 int pos = 0;
00069 while ( Player :: playerStatusNames[pos] ) {
00070 pw.type->AddItem( Player :: playerStatusNames[pos] );
00071 ++pos;
00072 }
00073
00074 pw.type->SelectItem( actmap->player[i].stat );
00075 pw.type->SetEditable(false);
00076 } else {
00077 pw.name->SetEditable( false );
00078 pw.type = NULL;
00079 PG_LineEdit* le = new PG_LineEdit( colbar, r );
00080 le->SetText( Player :: playerStatusNames[ actmap->player[i].stat ] );
00081 le->SetEditable( false );
00082 }
00083
00084 PG_ThemeWidget* col = new PG_ThemeWidget( colbar, PG_Rect( 5, 5, 20, 20 ));
00085 col->SetSimpleBackground(true);
00086 col->SetBackgroundColor ( actmap->player[i].getColor());
00087 col->SetBorderSize(0);
00088
00089 playerWidgets.push_back( pw );
00090
00091 ++counter;
00092 } else
00093 actmap->player[i].stat = Player::off;
00094
00095 SetTransparency(255);
00096 };
00097
00098 bool PlayerSetupWidget::Valid() {
00099 if ( mode == AllEditableSinglePlayer ) {
00100 int humanNum = 0;
00101 for ( vector<PlayerWidgets>::iterator i = playerWidgets.begin(); i != playerWidgets.end(); ++i )
00102 if ( i->type )
00103 if ( Player::PlayerStatus( i->type->GetSelectedItemIndex()) == Player::human
00104 || Player::PlayerStatus( i->type->GetSelectedItemIndex()) == Player::supervisor
00105 || Player::PlayerStatus( i->type->GetSelectedItemIndex()) == Player::suspended )
00106 ++humanNum;
00107
00108 if ( humanNum > 1 ) {
00109 MessagingHub::Instance().error("Only a single human player allowed in SinglePlayer mode allowed!");
00110 return false;
00111 }
00112 }
00113 return true;
00114 }
00115
00116
00117 bool PlayerSetupWidget::Apply() {
00118 if ( !Valid() )
00119 return false;
00120
00121 for ( vector<PlayerWidgets>::iterator i = playerWidgets.begin(); i != playerWidgets.end(); ++i ) {
00122 actmap->player[i->pos].setName( i->name->GetText() );
00123 if ( i->type )
00124 actmap->player[i->pos].stat = Player::PlayerStatus( i->type->GetSelectedItemIndex() );
00125 }
00126 return true;
00127 };
00128
00129
00130 class PlayerSetupWindow : public ASC_PG_Dialog {
00131 PlayerSetupWidget* asw;
00132 public:
00133 PlayerSetupWindow( GameMap* actmap, bool allEditable, PG_Widget *parent, const PG_Rect &r ) : ASC_PG_Dialog( parent, r, "Players" )
00134 {
00135 asw = new PlayerSetupWidget( actmap, PlayerSetupWidget::AllEditable, this, PG_Rect( 5, 30, r.Width() - 10, r.Height() - 60 ));
00136 PG_Button* ok = new PG_Button( this, PG_Rect( Width() - 200, Height() - 30, 90, 20 ), "OK" );
00137 ok->sigClick.connect( SigC::slot( *this, &PlayerSetupWindow::Apply ));
00138 PG_Button* cancel = new PG_Button( this, PG_Rect( Width() - 100, Height() - 30, 90, 20 ), "Cancel" );
00139 cancel->sigClick.connect( SigC::slot( *this, &PlayerSetupWindow::QuitModal ));
00140 }
00141
00142 bool Apply()
00143 {
00144 asw->Apply();
00145 QuitModal();
00146 return true;
00147 }
00148
00149 };
00150
00151 void setupPlayers( GameMap* actmap, bool supervisor )
00152 {
00153 PlayerSetupWindow asw ( actmap, supervisor, NULL, PG_Rect( 100, 100, 600, 500 ));
00154 asw.Show();
00155 asw.RunModal();
00156 }