~ubuntu-branches/ubuntu/maverick/asc/maverick

« back to all changes in this revision

Viewing changes to source/dialogs/alliancesetup.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese, Eddy Petrișor, Gonéri Le Bouder, Cyril Brulebois, Barry deFreese
  • Date: 2008-01-08 19:54:18 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080108195418-n19fc4eobhhqxcy5
Tags: 2.0.1.0-1
[ Eddy Petrișor ]
* fixed Homepage semifield

[ Gonéri Le Bouder ]
* add a watchfile
* move homepage from the description to the new Homepage field

[ Cyril Brulebois ]
* Added Vcs-Svn and Vcs-Browser fields in the control file.

[ Barry deFreese ]
* Fix make-clean lintian warning
* New upstream release
* Bump debhelper build-dep to match compat
* Add desktop file
* Update watch file for new upstream naming
* Remove nostrip check from rules
* Bump Standards Version to 3.7.3
* Add myself to uploaders

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
     This file is part of Advanced Strategic Command; http://www.asc-hq.de
 
3
     Copyright (C) 1994-1999  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
#include <pglistboxbaseitem.h>
 
25
#include <pgtooltiphelp.h>
 
26
 
 
27
#include "../iconrepository.h"
 
28
#include "../gamemap.h"
 
29
#include "../paradialog.h"
 
30
#include "playersetup.h"
 
31
#include "alliancesetup.h"
 
32
 
 
33
 
 
34
const int diplomaticStateIconSize = 20;
 
35
const int diplomaticStateIconSpace = 2;
 
36
 
 
37
 
 
38
template<typename SelectionType>
 
39
int getItemNum() { return 0; };
 
40
 
 
41
template<typename SelectionType>
 
42
const char* getStateName(int s) { return NULL; };
 
43
 
 
44
 
 
45
template<>
 
46
int getItemNum<DiplomaticStates>() { return diplomaticStateNum; };
 
47
 
 
48
template<>
 
49
const char* getStateName<DiplomaticStates>(int s) { return diplomaticStateNames[s]; };
 
50
 
 
51
 
 
52
template<>
 
53
int getItemNum<AllianceSetupWidget::DiplomaticTransitions>() { return diplomaticStateNum+1; };
 
54
 
 
55
template<>
 
56
const char* getStateName<AllianceSetupWidget::DiplomaticTransitions>(int s) 
 
57
 
58
   if ( s == 0 ) 
 
59
      return "Sneak Attack"; 
 
60
   else 
 
61
      return diplomaticStateNames[s-1]; 
 
62
};
 
63
 
 
64
 
 
65
ASCString getDiplomaticStateImage( DiplomaticStates s )
 
66
{
 
67
   ASCString filename = "diplo-" + ASCString::toString(s) + ".png";
 
68
   return filename;
 
69
};
 
70
 
 
71
ASCString getDiplomaticStateImage( AllianceSetupWidget::DiplomaticTransitions s )
 
72
{
 
73
   if ( s == AllianceSetupWidget::SNEAK_ATTACK ) {
 
74
      ASCString filename = "diplo-sneak.png";
 
75
      return filename;
 
76
   } else 
 
77
      return getDiplomaticStateImage( DiplomaticStates( s-1 ));
 
78
};
 
79
 
 
80
 
 
81
template <typename SelectionType>
 
82
class ListBoxImageItem : public PG_ListBoxBaseItem {
 
83
      SelectionType state;
 
84
   public:
 
85
      ListBoxImageItem( PG_ListBox *parent, PG_Point pos, SelectionType s )  : PG_ListBoxBaseItem( parent, diplomaticStateIconSize + 2*diplomaticStateIconSpace ), state(s)
 
86
      {
 
87
         new PG_Image( this, PG_Point(diplomaticStateIconSpace,diplomaticStateIconSpace), IconRepository::getIcon( getDiplomaticStateImage(s)).getBaseSurface() , false );
 
88
         new PG_Label( this, PG_Rect(diplomaticStateIconSize + 5, diplomaticStateIconSpace, 200,diplomaticStateIconSize), getStateName<SelectionType>(s));
 
89
      }
 
90
      
 
91
      SigC::Signal1<void,SelectionType> sigSet;
 
92
      
 
93
      bool eventMouseButtonUp(const SDL_MouseButtonEvent* button) 
 
94
      {
 
95
            if(button->button != 1) {
 
96
                     return false;
 
97
            }
 
98
            
 
99
            PG_ListBox* listbox = dynamic_cast<PG_ListBox*>(GetParent());
 
100
      
 
101
            if(listbox == NULL || !listbox->IsVisible()) {
 
102
                     return true;
 
103
            }
 
104
      
 
105
            listbox->SelectItem(this);
 
106
            listbox->QuitModal();
 
107
            sigSet(state);
 
108
      
 
109
            return true;
 
110
      }
 
111
};
 
112
 
 
113
 
 
114
 
 
115
 
 
116
 
 
117
template <typename SelectionType>
 
118
class DiplomaticModeChooser : public PG_Widget {
 
119
      SelectionType& mode;
 
120
      bool writePossible;
 
121
      
 
122
   protected:
 
123
 
 
124
      void selectMode()
 
125
      {
 
126
         PG_ListBox listBox( NULL, PG_Rect( x, y + Height(), 250, getItemNum<SelectionType>() * ( diplomaticStateIconSpace * 2 + diplomaticStateIconSize ) + 4 ));
 
127
         for ( int i = 0; i < getItemNum<SelectionType>(); ++i) {
 
128
            ListBoxImageItem<SelectionType>* item = new ListBoxImageItem<SelectionType>( NULL, PG_Point(0,0), SelectionType(i));
 
129
            item->sigSet.connect( SigC::slot( *this, &DiplomaticModeChooser::SetState));
 
130
            listBox.AddChild( item );
 
131
         }
 
132
         listBox.Show();
 
133
         listBox.RunModal();
 
134
      }     
 
135
            
 
136
   public:
 
137
      DiplomaticModeChooser ( PG_Widget *parent, const PG_Rect& pos, SelectionType& dm, bool writeable ) : PG_Widget( parent, pos, true ), mode(dm), writePossible( writeable )
 
138
      {
 
139
      };
 
140
 
 
141
      SigC::Signal1<void,SelectionType> sigStateChange;
 
142
            
 
143
      void eventDraw (SDL_Surface *surface, const PG_Rect &rect)
 
144
      {
 
145
         Surface s = Surface::Wrap( surface );
 
146
         s.Blit(  IconRepository::getIcon( getDiplomaticStateImage(mode) ) );
 
147
      }
 
148
      
 
149
      void SetState( SelectionType state )
 
150
      {
 
151
         mode = state;
 
152
         Redraw();
 
153
         Update(true);
 
154
         sigStateChange(state);
 
155
      }
 
156
      
 
157
      bool eventMouseButtonUp(const SDL_MouseButtonEvent* button) 
 
158
      {
 
159
         if (button->button != 1) {
 
160
            return false;
 
161
         }
 
162
 
 
163
         if ( writePossible )   
 
164
            selectMode();
 
165
         return true;
 
166
      }
 
167
};      
 
168
 
 
169
 
 
170
AllianceSetupWidget::AllianceSetupWidget( GameMap* gamemap, bool allEditable, PG_Widget *parent, const PG_Rect &r, const std::string &style ) : PG_ScrollWidget( parent, r, style ) , actmap ( gamemap )
 
171
{
 
172
   this->allEditable = allEditable;
 
173
   int playerNum = 0;
 
174
   
 
175
   for ( int i = 0; i < actmap->getPlayerCount(); ++i ) {
 
176
      vector<DiplomaticTransitions> t;
 
177
      vector< DiplomaticStates > s;
 
178
      
 
179
      const DiplomaticStateVector& diplo = actmap->player[i].diplomacy;
 
180
      
 
181
      for ( int j = 0; j < actmap->getPlayerCount(); ++j )  {
 
182
         DiplomaticStateVector::QueuedStateChanges::const_iterator change = diplo.queuedStateChanges.find(j);
 
183
         if ( change != diplo.queuedStateChanges.end() )
 
184
            t.push_back( DiplomaticTransitions( change->second + 1 ));
 
185
         else
 
186
            t.push_back( DiplomaticTransitions( diplo.getState(j) + 1 ));
 
187
         s.push_back( diplo.getState(j));
 
188
      }   
 
189
       
 
190
      stateChanges.push_back ( t );        
 
191
      states.push_back ( s );
 
192
      
 
193
      if ( actmap->player[i].exist() ) 
 
194
         ++playerNum;
 
195
   }      
 
196
 
 
197
         
 
198
   const int colWidth = 40;
 
199
   const int lineHeight = 30;
 
200
   const int barSpace = 5;
 
201
   const int spacing = 10;
 
202
   const int nameLength = 200;
 
203
   const int barOverhang = 20;
 
204
   const int sqaureWidth = lineHeight;
 
205
   const int lineLength = sqaureWidth + nameLength + playerNum * (colWidth + spacing) + barOverhang;
 
206
   const int colHeight  =  barOverhang + playerNum * (lineHeight + spacing) - spacing +  barOverhang;
 
207
 
 
208
   #define calcx(counter) (sqaureWidth + nameLength + spacing + counter * (colWidth + spacing) )
 
209
   #define calcy(counter) (barOverhang + counter * (lineHeight + spacing))
 
210
 
 
211
   int counter = 0; 
 
212
   for ( int i = 0; i < actmap->getPlayerCount(); ++i ) 
 
213
      if ( actmap->player[i].exist() ) {
 
214
         int x = calcx(counter);
 
215
         
 
216
         ColoredBar* verticalBar = new ColoredBar( actmap->player[i].getColor(), this, PG_Rect( x, 0, colWidth, colHeight ));
 
217
         verticalBar->SetTransparency( 128 );
 
218
         
 
219
         ++counter;
 
220
      }
 
221
   
 
222
                        
 
223
   counter = 0; 
 
224
   for ( int i = 0; i < actmap->getPlayerCount(); ++i ) // rows
 
225
      if ( actmap->player[i].exist() ) {
 
226
      
 
227
         PlayerWidgets pw;
 
228
         pw.pos  = i;
 
229
         
 
230
         int y = calcy(counter);
 
231
         
 
232
         ColoredBar* horizontalBar = new ColoredBar( actmap->player[i].getColor(), this, PG_Rect( 0, y, lineLength, lineHeight ));
 
233
         horizontalBar->SetTransparency( 128 );
 
234
         
 
235
         pw.name = new PG_LineEdit( horizontalBar, PG_Rect( sqaureWidth, barSpace, nameLength, lineHeight-2*barSpace ));
 
236
         pw.name->SetText( actmap->player[i].getName());
 
237
         pw.name->SetEditable( false );
 
238
 
 
239
         new PG_ToolTipHelp( pw.name, "Position: " + ASCString::toString(i) );
 
240
 
 
241
         PG_ThemeWidget* col = new PG_ThemeWidget( horizontalBar, PG_Rect( barSpace, barSpace, sqaureWidth - 2*barSpace, lineHeight - 2*barSpace ));
 
242
         col->SetSimpleBackground(true);
 
243
         col->SetBackgroundColor ( actmap->player[i].getColor());
 
244
         col->SetBorderSize(0);
 
245
         
 
246
         int cnt2 = 0;
 
247
         for ( int j = 0; j < actmap->getPlayerCount(); ++j ) // columns
 
248
            if ( actmap->player[j].exist() )  {
 
249
               if ( i != j ) {
 
250
                  int x = calcx(cnt2) - barSpace;
 
251
                  PG_Rect rect ( x + (colWidth - diplomaticStateIconSize)/2 ,  (lineHeight - diplomaticStateIconSize)/2,diplomaticStateIconSize,diplomaticStateIconSize); 
 
252
                  if ( allEditable ) {
 
253
                     DiplomaticModeChooser<DiplomaticStates>* dmc = new DiplomaticModeChooser<DiplomaticStates>( horizontalBar, rect, getState(i,j), true );
 
254
                     dmc->sigStateChange.connect( SigC::bind( SigC::slot( *this, &AllianceSetupWidget::setState ), j, i));
 
255
                     diplomaticWidgets[ linearize( i,j) ] = dmc;
 
256
                  } else {
 
257
                     DiplomaticTransitions& s = stateChanges[i][j];
 
258
                     DiplomaticModeChooser<DiplomaticTransitions>* dmc = new DiplomaticModeChooser<DiplomaticTransitions>( horizontalBar, rect, s, i == gamemap->actplayer );
 
259
                     // dmc->sigStateChange.connect( SigC::bind( SigC::slot( *this, &AllianceSetupWidget::setState ), j, i));
 
260
                     diplomaticWidgets[ linearize( i,j) ] = dmc;
 
261
                  }   
 
262
                     
 
263
               }
 
264
               ++cnt2;
 
265
            }   
 
266
                        
 
267
         playerWidgets.push_back( pw );
 
268
                        
 
269
         ++counter;
 
270
      } 
 
271
      
 
272
   SetTransparency(255);
 
273
};
 
274
 
 
275
 
 
276
void AllianceSetupWidget::setState( DiplomaticStates s, int actingPlayer, int secondPlayer )
 
277
{
 
278
   getState( actingPlayer, secondPlayer) = s;
 
279
   
 
280
   DiplomaticWidgets::iterator i = diplomaticWidgets.find( linearize( actingPlayer, secondPlayer) );
 
281
   if ( i != diplomaticWidgets.end() )
 
282
      i->second->Redraw(true);
 
283
}
 
284
 
 
285
DiplomaticStates& AllianceSetupWidget::getState( int actingPlayer, int secondPlayer )
 
286
{
 
287
   return states.at(actingPlayer).at(secondPlayer);
 
288
}
 
289
 
 
290
int AllianceSetupWidget::linearize( int actingPlayer, int secondPlayer  )
 
291
{
 
292
   return actingPlayer * actmap->getPlayerCount() + secondPlayer;
 
293
}
 
294
 
 
295
 
 
296
void AllianceSetupWidget::Apply() 
 
297
{
 
298
   for ( int acting = 0; acting < actmap->getPlayerCount(); ++acting )
 
299
      for ( int second = 0; second < actmap->getPlayerCount(); ++second ) {
 
300
      
 
301
         if ( allEditable ) {
 
302
            if ( getState( acting, second ) != actmap->player[acting].diplomacy.getState( second ))
 
303
               actmap->player[acting].diplomacy.setState( second, getState( acting, second ));         
 
304
         } else {
 
305
            if ( acting == actmap->actplayer ) {
 
306
               if ( stateChanges[acting][second] == SNEAK_ATTACK ) {
 
307
                  actmap->player[acting].diplomacy.sneakAttack( second );         
 
308
               } else {
 
309
                  DiplomaticStates s = DiplomaticStates( stateChanges[acting][second] - 1);
 
310
                  DiplomaticStates t;
 
311
                  DiplomaticStateVector::QueuedStateChanges::iterator q = actmap->player[acting].diplomacy.queuedStateChanges.find( second );
 
312
                  if ( q == actmap->player[acting].diplomacy.queuedStateChanges.end() )
 
313
                     t = getState( acting, second );
 
314
                  else
 
315
                     t = q->second;   
 
316
                     
 
317
                  if ( t != s )
 
318
                     actmap->player[acting].diplomacy.propose( second, s );         
 
319
               }   
 
320
            }
 
321
         }
 
322
      }
 
323
};
 
324
 
 
325
 
 
326
AllianceSetupWidget::~AllianceSetupWidget()
 
327
{
 
328
}   
 
329
 
 
330
 
 
331
 
 
332
 
 
333
 
 
334
 
 
335
 
 
336
 
 
337
 
 
338
 
 
339
class AllianceSetupWindow : public ASC_PG_Dialog {
 
340
      AllianceSetupWidget* asw;
 
341
      bool changed;
 
342
   public:
 
343
      AllianceSetupWindow( GameMap* actmap, bool allEditable, PG_Widget *parent, const PG_Rect &r ) : ASC_PG_Dialog( parent, r, "Diplomacy" ), changed(false)
 
344
      {
 
345
         asw = new AllianceSetupWidget( actmap, allEditable, this, PG_Rect( 5, 30, r.Width() - 10, r.Height() - 60 ));
 
346
         PG_Button* ok = new PG_Button( this, PG_Rect( Width() - 200, Height() - 30, 90, 20 ), "OK" );
 
347
         ok->sigClick.connect( SigC::slot( *this, &AllianceSetupWindow::Apply ));
 
348
         PG_Button* cancel = new PG_Button( this, PG_Rect( Width() - 100, Height() - 30, 90, 20 ), "Cancel" );
 
349
         cancel->sigClick.connect( SigC::slot( *this, &AllianceSetupWindow::QuitModal ));
 
350
      }
 
351
 
 
352
      bool Apply()
 
353
      {
 
354
         asw->Apply();
 
355
         QuitModal();
 
356
         changed = true;
 
357
         return true;
 
358
      }
 
359
 
 
360
      bool isSomethingChanged() { return changed; };
 
361
      
 
362
};
 
363
 
 
364
bool  setupalliances( GameMap* actmap, bool supervisor  )
 
365
{
 
366
   AllianceSetupWindow asw ( actmap, supervisor, NULL, PG_Rect( 100, 100, 600, 500 ));
 
367
   asw.Show();
 
368
   asw.RunModal();
 
369
   return asw.isSomethingChanged();
 
370
}