File: playersetup.cpp

package info (click to toggle)
asc 2.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 75,080 kB
  • ctags: 24,943
  • sloc: cpp: 155,023; sh: 8,829; ansic: 6,890; makefile: 650; perl: 138
file content (156 lines) | stat: -rw-r--r-- 5,459 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
     This file is part of Advanced Strategic Command; http://www.asc-hq.de
     Copyright (C) 1994-1999  Martin Bickel  and  Marc Schellenberger
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
 
     You should have received a copy of the GNU General Public License
     along with this program; see the file COPYING. If not, write to the 
     Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
     Boston, MA  02111-1307  USA
*/


#include <sstream>
#include <pgimage.h>

#include "playersetup.h"





int PlayerSetupWidget::guessHeight( GameMap* gamemap )
{
   int counter = 0;
   for ( int i = 0; i < gamemap->getPlayerCount(); ++i )
      if ( gamemap->player[i].exist() )
         ++counter;
   
   return yoffset + counter * spacing + 5;
}

PlayerSetupWidget::PlayerSetupWidget( GameMap* gamemap, Mode mode, PG_Widget *parent, const PG_Rect &r, const std::string &style ) : PG_ScrollWidget( parent, r, style ) , actmap ( gamemap )
{
   this->mode = mode;
   
   int counter = 0; 
   for ( int i = 0; i < actmap->getPlayerCount(); ++i ) 
      if ( actmap->player[i].exist() ) {
      
         PlayerWidgets pw;
         pw.pos  = i;
         
         int y = yoffset + counter * spacing;
         
         ColoredBar* colbar = new ColoredBar( actmap->player[i].getColor(), this, PG_Rect( 20, y, Width() - 60, 30 ));
         colbar->SetTransparency( 128 );
         
         
         int y1 = Width() * 4 / 10;
         
         pw.name = new PG_LineEdit( colbar, PG_Rect( 40, 5, y1 - 40, 20 ));
         pw.name->SetText( actmap->player[i].getName());
         
         
         PG_Rect r = PG_Rect( y1 + 20, 5, colbar->Width() - y1 - 40, 20 );
         if ( mode != SelfEditable  || actmap->actplayer == i ) {
            pw.type = new PG_DropDown( colbar, r);
            
            int pos = 0;
            while ( Player :: playerStatusNames[pos] ) {
               pw.type->AddItem( Player :: playerStatusNames[pos] );
               ++pos;
            }
            
            pw.type->SelectItem( actmap->player[i].stat );
            pw.type->SetEditable(false);
         } else {
            pw.name->SetEditable( false );
            pw.type = NULL;
            PG_LineEdit* le = new PG_LineEdit( colbar, r );
            le->SetText( Player :: playerStatusNames[ actmap->player[i].stat ] );
            le->SetEditable( false );
         }
         
         PG_ThemeWidget* col = new PG_ThemeWidget( colbar, PG_Rect( 5, 5, 20, 20 ));
         col->SetSimpleBackground(true);
         col->SetBackgroundColor ( actmap->player[i].getColor());
         col->SetBorderSize(0);

         playerWidgets.push_back( pw );
                        
         ++counter;
      } else
         actmap->player[i].stat = Player::off;
      
   SetTransparency(255);
};

bool PlayerSetupWidget::Valid() {
   if ( mode == AllEditableSinglePlayer ) {
      int humanNum = 0;
      for ( vector<PlayerWidgets>::iterator i = playerWidgets.begin(); i != playerWidgets.end(); ++i ) 
         if ( i->type )
            if (    Player::PlayerStatus( i->type->GetSelectedItemIndex()) == Player::human
                 || Player::PlayerStatus( i->type->GetSelectedItemIndex()) == Player::supervisor
                 || Player::PlayerStatus( i->type->GetSelectedItemIndex()) == Player::suspended  ) 
               ++humanNum;
               
       if ( humanNum > 1 ) {
         MessagingHub::Instance().error("Only a single human player allowed in SinglePlayer mode allowed!");
         return false;
       }
   }
   return true;
}


bool PlayerSetupWidget::Apply() {
   if ( !Valid() )
      return false;
      
   for ( vector<PlayerWidgets>::iterator i = playerWidgets.begin(); i != playerWidgets.end(); ++i ) {
      actmap->player[i->pos].setName( i->name->GetText() );
      if ( i->type )
         actmap->player[i->pos].stat = Player::PlayerStatus( i->type->GetSelectedItemIndex() );
   }
   return true;
};


class PlayerSetupWindow : public ASC_PG_Dialog {
   PlayerSetupWidget* asw;
   public:
      PlayerSetupWindow( GameMap* actmap, bool allEditable, PG_Widget *parent, const PG_Rect &r ) : ASC_PG_Dialog( parent, r, "Players" )
      {
         asw = new PlayerSetupWidget( actmap, PlayerSetupWidget::AllEditable, this, PG_Rect( 5, 30, r.Width() - 10, r.Height() - 60 ));
         PG_Button* ok = new PG_Button( this, PG_Rect( Width() - 200, Height() - 30, 90, 20 ), "OK" );
         ok->sigClick.connect( SigC::slot( *this, &PlayerSetupWindow::Apply ));
         PG_Button* cancel = new PG_Button( this, PG_Rect( Width() - 100, Height() - 30, 90, 20 ), "Cancel" );
         cancel->sigClick.connect( SigC::slot( *this, &PlayerSetupWindow::QuitModal ));
      }

      bool Apply()
      {
         asw->Apply();
         QuitModal();
         return true;
      }

};

void  setupPlayers( GameMap* actmap, bool supervisor  )
{
   PlayerSetupWindow asw ( actmap, supervisor, NULL, PG_Rect( 100, 100, 600, 500 ));
   asw.Show();
   asw.RunModal();
}