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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
/*
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 <pglistboxbaseitem.h>
#include <pgtooltiphelp.h>
#include "../iconrepository.h"
#include "../gamemap.h"
#include "../paradialog.h"
#include "playersetup.h"
#include "alliancesetup.h"
#include "../actions/diplomacycommand.h"
#include "../dialog.h"
const int diplomaticStateIconSize = 20;
const int diplomaticStateIconSpace = 2;
template<typename SelectionType>
int getItemNum() { return 0; };
template<typename SelectionType>
const char* getStateName(int s) { return NULL; };
template<>
int getItemNum<DiplomaticStates>() { return diplomaticStateNum; };
template<>
const char* getStateName<DiplomaticStates>(int s) { return diplomaticStateNames[s]; };
template<>
int getItemNum<AllianceSetupWidget::DiplomaticTransitions>() { return diplomaticStateNum+1; };
template<>
const char* getStateName<AllianceSetupWidget::DiplomaticTransitions>(int s)
{
if ( s == 0 )
return "Sneak Attack";
else
return diplomaticStateNames[s-1];
};
ASCString getDiplomaticStateImage( DiplomaticStates s )
{
ASCString filename = "diplo-" + ASCString::toString(s) + ".png";
return filename;
};
ASCString getDiplomaticStateImage( AllianceSetupWidget::DiplomaticTransitions s )
{
if ( s == AllianceSetupWidget::SNEAK_ATTACK ) {
ASCString filename = "diplo-sneak.png";
return filename;
} else
return getDiplomaticStateImage( DiplomaticStates( s-1 ));
};
template <typename SelectionType>
class ListBoxImageItem : public PG_ListBoxBaseItem {
SelectionType state;
public:
ListBoxImageItem( PG_ListBox *parent, PG_Point pos, SelectionType s ) : PG_ListBoxBaseItem( parent, diplomaticStateIconSize + 2*diplomaticStateIconSpace ), state(s)
{
new PG_Image( this, PG_Point(diplomaticStateIconSpace,diplomaticStateIconSpace), IconRepository::getIcon( getDiplomaticStateImage(s)).getBaseSurface() , false );
new PG_Label( this, PG_Rect(diplomaticStateIconSize + 5, diplomaticStateIconSpace, 200,diplomaticStateIconSize), getStateName<SelectionType>(s));
}
SigC::Signal1<void,SelectionType> sigSet;
bool eventMouseButtonUp(const SDL_MouseButtonEvent* button)
{
if(button->button != 1) {
return false;
}
PG_ListBox* listbox = dynamic_cast<PG_ListBox*>(GetParent());
if(listbox == NULL || !listbox->IsVisible()) {
return true;
}
listbox->SelectItem(this);
listbox->QuitModal();
sigSet(state);
return true;
}
};
template <typename SelectionType>
class DiplomaticModeChooser : public PG_Widget {
SelectionType& mode;
bool writePossible;
protected:
void selectMode()
{
PG_ListBox listBox( NULL, PG_Rect( x, y + Height(), 250, getItemNum<SelectionType>() * ( diplomaticStateIconSpace * 2 + diplomaticStateIconSize ) + 4 ));
for ( int i = 0; i < getItemNum<SelectionType>(); ++i) {
ListBoxImageItem<SelectionType>* item = new ListBoxImageItem<SelectionType>( NULL, PG_Point(0,0), SelectionType(i));
item->sigSet.connect( SigC::slot( *this, &DiplomaticModeChooser::SetState));
listBox.AddChild( item );
}
listBox.Show();
listBox.RunModal();
}
public:
DiplomaticModeChooser ( PG_Widget *parent, const PG_Rect& pos, SelectionType& dm, bool writeable ) : PG_Widget( parent, pos, true ), mode(dm), writePossible( writeable )
{
};
SigC::Signal1<void,SelectionType> sigStateChange;
void eventDraw (SDL_Surface *surface, const PG_Rect &rect)
{
Surface s = Surface::Wrap( surface );
s.Blit( IconRepository::getIcon( getDiplomaticStateImage(mode) ) );
}
void SetState( SelectionType state )
{
mode = state;
Redraw();
Update(true);
sigStateChange(state);
}
bool eventMouseButtonUp(const SDL_MouseButtonEvent* button)
{
if (button->button != 1) {
return false;
}
if ( writePossible )
selectMode();
return true;
}
};
AllianceSetupWidget::AllianceSetupWidget( GameMap* gamemap, ApplyStrategy* applyStrategy, bool allEditable, PG_Widget *parent, const PG_Rect &r, const std::string &style )
: PG_ScrollWidget( parent, r, style ) , actmap ( gamemap ), strategy( applyStrategy )
{
this->allEditable = allEditable;
int playerNum = 0;
for ( int i = 0; i < actmap->getPlayerCount(); ++i ) {
vector<DiplomaticTransitions> t;
vector< DiplomaticStates > s;
const DiplomaticStateVector& diplo = actmap->player[i].diplomacy;
for ( int j = 0; j < actmap->getPlayerCount(); ++j ) {
DiplomaticStateVector::QueuedStateChanges::const_iterator change = diplo.queuedStateChanges.find(j);
if ( change != diplo.queuedStateChanges.end() )
t.push_back( DiplomaticTransitions( change->second + 1 ));
else
t.push_back( DiplomaticTransitions( diplo.getState(j) + 1 ));
s.push_back( diplo.getState(j));
}
stateChanges.push_back ( t );
states.push_back ( s );
if ( actmap->player[i].exist() )
++playerNum;
}
const int colWidth = 40;
const int lineHeight = 30;
const int barSpace = 5;
const int spacing = 10;
const int nameLength = 200;
const int barOverhang = 20;
const int sqaureWidth = lineHeight;
const int lineLength = sqaureWidth + nameLength + playerNum * (colWidth + spacing) + barOverhang;
const int colHeight = barOverhang + playerNum * (lineHeight + spacing) - spacing + barOverhang;
#define calcx(counter) (sqaureWidth + nameLength + spacing + counter * (colWidth + spacing) )
#define calcy(counter) (barOverhang + counter * (lineHeight + spacing))
int counter = 0;
for ( int i = 0; i < actmap->getPlayerCount(); ++i )
if ( actmap->player[i].exist() ) {
int x = calcx(counter);
ColoredBar* verticalBar = new ColoredBar( actmap->player[i].getColor(), this, PG_Rect( x, 0, colWidth, colHeight ));
verticalBar->SetTransparency( 128 );
++counter;
}
counter = 0;
for ( int i = 0; i < actmap->getPlayerCount(); ++i ) // rows
if ( actmap->player[i].exist() ) {
PlayerWidgets pw;
pw.pos = i;
int y = calcy(counter);
ColoredBar* horizontalBar = new ColoredBar( actmap->player[i].getColor(), this, PG_Rect( 0, y, lineLength, lineHeight ));
horizontalBar->SetTransparency( 128 );
pw.name = new PG_LineEdit( horizontalBar, PG_Rect( sqaureWidth, barSpace, nameLength, lineHeight-2*barSpace ));
pw.name->SetText( actmap->player[i].getName());
pw.name->SetEditable( false );
new PG_ToolTipHelp( pw.name, "Position: " + ASCString::toString(i) );
PG_ThemeWidget* col = new PG_ThemeWidget( horizontalBar, PG_Rect( barSpace, barSpace, sqaureWidth - 2*barSpace, lineHeight - 2*barSpace ));
col->SetSimpleBackground(true);
col->SetBackgroundColor ( actmap->player[i].getColor());
col->SetBorderSize(0);
int cnt2 = 0;
for ( int j = 0; j < actmap->getPlayerCount(); ++j ) // columns
if ( actmap->player[j].exist() ) {
if ( i != j ) {
int x = calcx(cnt2) - barSpace;
PG_Rect rect ( x + (colWidth - diplomaticStateIconSize)/2 , (lineHeight - diplomaticStateIconSize)/2,diplomaticStateIconSize,diplomaticStateIconSize);
if ( allEditable ) {
DiplomaticModeChooser<DiplomaticStates>* dmc = new DiplomaticModeChooser<DiplomaticStates>( horizontalBar, rect, getState(i,j), true );
dmc->sigStateChange.connect( SigC::bind( SigC::slot( *this, &AllianceSetupWidget::setState ), j, i));
diplomaticWidgets[ linearize( i,j) ] = dmc;
} else {
DiplomaticTransitions& s = stateChanges[i][j];
DiplomaticModeChooser<DiplomaticTransitions>* dmc = new DiplomaticModeChooser<DiplomaticTransitions>( horizontalBar, rect, s, i == gamemap->actplayer );
// dmc->sigStateChange.connect( SigC::bind( SigC::slot( *this, &AllianceSetupWidget::setState ), j, i));
diplomaticWidgets[ linearize( i,j) ] = dmc;
}
}
++cnt2;
}
playerWidgets.push_back( pw );
++counter;
}
SetTransparency(255);
};
void AllianceSetupWidget::setState( DiplomaticStates s, int actingPlayer, int secondPlayer )
{
getState( actingPlayer, secondPlayer) = s;
DiplomaticWidgets::iterator i = diplomaticWidgets.find( linearize( actingPlayer, secondPlayer) );
if ( i != diplomaticWidgets.end() )
i->second->Redraw(true);
}
DiplomaticStates& AllianceSetupWidget::getState( int actingPlayer, int secondPlayer )
{
return states.at(actingPlayer).at(secondPlayer);
}
int AllianceSetupWidget::linearize( int actingPlayer, int secondPlayer )
{
return actingPlayer * actmap->getPlayerCount() + secondPlayer;
}
void AllianceSetupWidget::Apply()
{
for ( int acting = 0; acting < actmap->getPlayerCount(); ++acting )
for ( int second = 0; second < actmap->getPlayerCount(); ++second ) {
if ( allEditable ) {
if ( getState( acting, second ) != actmap->player[acting].diplomacy.getState( second ))
actmap->player[acting].diplomacy.setState( second, getState( acting, second ));
} else {
if ( acting == actmap->actplayer ) {
if ( stateChanges[acting][second] == SNEAK_ATTACK ) {
strategy->sneakAttack( actmap, acting, second );
} else {
DiplomaticStates s = DiplomaticStates( stateChanges[acting][second] - 1);
DiplomaticStates t;
DiplomaticStateVector::QueuedStateChanges::iterator q = actmap->player[acting].diplomacy.queuedStateChanges.find( second );
if ( q == actmap->player[acting].diplomacy.queuedStateChanges.end() )
t = getState( acting, second );
else
t = q->second;
if ( t != s ) {
strategy->setState( actmap, acting, second, s );
}
}
}
}
}
};
AllianceSetupWidget::~AllianceSetupWidget()
{
}
class AllianceSetupWindow : public ASC_PG_Dialog {
AllianceSetupWidget* asw;
bool changed;
public:
AllianceSetupWindow( GameMap* actmap, AllianceSetupWidget::ApplyStrategy* strategy, bool allEditable, PG_Widget *parent, const PG_Rect &r ) : ASC_PG_Dialog( parent, r, "Diplomacy" ), changed(false)
{
asw = new AllianceSetupWidget( actmap, strategy, 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, &AllianceSetupWindow::Apply ));
PG_Button* cancel = new PG_Button( this, PG_Rect( Width() - 100, Height() - 30, 90, 20 ), "Cancel" );
cancel->sigClick.connect( SigC::slot( *this, &AllianceSetupWindow::QuitModal ));
}
bool Apply()
{
asw->Apply();
QuitModal();
changed = true;
return true;
}
bool isSomethingChanged() { return changed; };
};
bool setupalliances( GameMap* actmap, AllianceSetupWidget::ApplyStrategy* strategy, bool supervisor )
{
AllianceSetupWindow asw ( actmap, strategy, supervisor, NULL, PG_Rect( 100, 100, 700, 500 ));
asw.Show();
asw.RunModal();
return asw.isSomethingChanged();
}
void DirectAllianceSetupStrategy::sneakAttack ( GameMap* map, int actingPlayer, int towardsPlayer )
{
map->getPlayer( actingPlayer ).diplomacy.setState( towardsPlayer, WAR );
}
void DirectAllianceSetupStrategy::setState ( GameMap* map, int actingPlayer, int towardsPlayer, DiplomaticStates newState )
{
map->getPlayer( actingPlayer ).diplomacy.setState( towardsPlayer, newState );
}
|