| 1 |
#include <vector> |
| 2 |
#include <sstream> |
| 3 |
|
| 4 |
#include <SDL/SDL.h> |
| 5 |
|
| •6 |
#include "guichan.hpp" |
| 7 |
|
| 8 |
#include "ui/menuaction.h" |
| 9 |
|
| 10 |
#include "settings.h" |
| 11 |
#include "main.h" |
| 12 |
|
| 13 |
#include "gameoptions.h" |
| 14 |
|
| 15 |
using namespace std; |
| 16 |
using namespace gcn; |
| 17 |
|
| 18 |
GameOptions::GameOptions() |
| 19 |
: enemyVisibilityAction(0), playersAction(0), planetsAction(0), |
| 20 |
startSinglePlayerGameAction(0), planets(2), players(1) |
| 21 |
{ |
| 22 |
setPlanets(Settings::getNumberOfPlanets()); |
| 23 |
setPlayers(Settings::getNumberOfComputerPlayers()); |
| 24 |
setEnemyVisibility(Settings::getEnemyVisibility()); |
| 25 |
} |
| 26 |
|
| 27 |
void |
| 28 |
GameOptions::setEnemyVisibility(bool b) |
| 29 |
{ |
| 30 |
enemyVisibility = b; |
| 31 |
|
| 32 |
if (enemyVisibilityAction) |
| 33 |
enemyVisibilityAction->update(); |
| 34 |
} |
| 35 |
|
| 36 |
void |
| 37 |
GameOptions::toggleEnemyVisibility() |
| 38 |
{ |
| 39 |
setEnemyVisibility(!enemyVisibility); |
| 40 |
} |
| 41 |
|
| 42 |
class EnemyVisibilityAction : public MenuAction |
| 43 |
{ |
| 44 |
GameOptions &gameOptions; |
| 45 |
CheckBox *check; |
| 46 |
|
| 47 |
public: |
| 48 |
EnemyVisibilityAction(GameOptions &go) |
| 49 |
: gameOptions(go) { } |
| 50 |
|
| 51 |
virtual gcn::Widget *getWidget(string s) |
| 52 |
{ |
| 53 |
return check = new CheckBox(s, gameOptions.getEnemyVisibility()); |
| 54 |
} |
| 55 |
|
| 56 |
void invoke() |
| 57 |
{ |
| 58 |
gameOptions.toggleEnemyVisibility(); |
| 59 |
} |
| 60 |
|
| 61 |
void previous() |
| 62 |
{ |
| 63 |
invoke(); |
| 64 |
} |
| 65 |
|
| 66 |
void next() |
| 67 |
{ |
| 68 |
invoke(); |
| 69 |
} |
| 70 |
|
| 71 |
void update() |
| 72 |
{ |
| 73 |
check->setMarked(gameOptions.getEnemyVisibility()); |
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
MenuAction * |
| 78 |
GameOptions::getEnemyVisibilityAction() |
| 79 |
{ |
| 80 |
if (!enemyVisibilityAction) |
| 81 |
enemyVisibilityAction = new EnemyVisibilityAction(*this); |
| 82 |
|
| 83 |
return enemyVisibilityAction; |
| 84 |
} |
| 85 |
|
| 86 |
void |
| 87 |
GameOptions::setPlanets(int p) |
| 88 |
{ |
| 89 |
planets = p; |
| 90 |
|
| 91 |
if (planets < 2) |
| 92 |
planets = 2; |
| 93 |
|
| 94 |
if (planets < players + 1) |
| 95 |
planets = players + 1; |
| 96 |
|
| 97 |
if (planetsAction) |
| 98 |
planetsAction->update(); |
| 99 |
} |
| 100 |
|
| 101 |
class PlanetsAction : public MenuAction |
| 102 |
{ |
| 103 |
GameOptions &gameOptions; |
| 104 |
Label *label; |
| 105 |
string baseCaption; |
| 106 |
|
| 107 |
public: |
| 108 |
PlanetsAction(GameOptions &go) |
| 109 |
: gameOptions(go) { } |
| 110 |
|
| 111 |
virtual gcn::Widget *getWidget(string s) |
| 112 |
{ |
| 113 |
baseCaption = s; |
| 114 |
stringstream ss; |
| 115 |
ss << baseCaption; |
| 116 |
ss << " "; |
| 117 |
ss << gameOptions.getPlanets(); |
| 118 |
|
| 119 |
return label = new Label(ss.str()); |
| 120 |
} |
| 121 |
|
| 122 |
void invoke() |
| 123 |
{ |
| 124 |
next(); |
| 125 |
} |
| 126 |
|
| 127 |
void next() |
| 128 |
{ |
| 129 |
gameOptions.setPlanets(gameOptions.getPlanets() + 1); |
| 130 |
} |
| 131 |
|
| 132 |
void previous() |
| 133 |
{ |
| 134 |
gameOptions.setPlanets(gameOptions.getPlanets() - 1); |
| 135 |
} |
| 136 |
|
| 137 |
void update() |
| 138 |
{ |
| 139 |
stringstream ss; |
| 140 |
ss << baseCaption; |
| 141 |
ss << " "; |
| 142 |
ss << gameOptions.getPlanets(); |
| 143 |
|
| 144 |
label->setCaption(ss.str()); |
| 145 |
label->adjustSize(); |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
MenuAction * |
| 150 |
GameOptions::getPlanetsAction() |
| 151 |
{ |
| 152 |
if (!planetsAction) |
| 153 |
planetsAction = new PlanetsAction(*this); |
| 154 |
|
| 155 |
return planetsAction; |
| 156 |
} |
| 157 |
|
| 158 |
void |
| 159 |
GameOptions::setPlayers(int p) |
| 160 |
{ |
| 161 |
players = p; |
| 162 |
|
| 163 |
if (players <= 0) |
| 164 |
players = 1; |
| 165 |
|
| 166 |
if (players > planets - 1) |
| 167 |
players = planets - 1; |
| 168 |
|
| 169 |
if (playersAction) |
| 170 |
playersAction->update(); |
| 171 |
} |
| 172 |
|
| 173 |
class PlayersAction : public MenuAction |
| 174 |
{ |
| 175 |
GameOptions &gameOptions; |
| 176 |
Label *label; |
| 177 |
string baseCaption; |
| 178 |
|
| 179 |
public: |
| 180 |
PlayersAction(GameOptions &go) |
| 181 |
: gameOptions(go) { } |
| 182 |
|
| 183 |
virtual gcn::Widget *getWidget(string s) |
| 184 |
{ |
| 185 |
baseCaption = s; |
| 186 |
stringstream ss; |
| 187 |
ss << baseCaption; |
| 188 |
ss << " "; |
| 189 |
ss << gameOptions.getPlayers(); |
| 190 |
|
| 191 |
return label = new Label(ss.str()); |
| 192 |
} |
| 193 |
|
| 194 |
void invoke() |
| 195 |
{ |
| 196 |
next(); |
| 197 |
} |
| 198 |
|
| 199 |
void next() |
| 200 |
{ |
| 201 |
gameOptions.setPlayers(gameOptions.getPlayers() + 1); |
| 202 |
} |
| 203 |
|
| 204 |
void previous() |
| 205 |
{ |
| 206 |
gameOptions.setPlayers(gameOptions.getPlayers() - 1); |
| 207 |
} |
| 208 |
|
| 209 |
void update() |
| 210 |
{ |
| 211 |
stringstream ss; |
| 212 |
ss << baseCaption; |
| 213 |
ss << " "; |
| 214 |
ss << gameOptions.getPlayers(); |
| 215 |
|
| 216 |
label->setCaption(ss.str()); |
| 217 |
label->adjustSize(); |
| 218 |
} |
| 219 |
|
| 220 |
}; |
| 221 |
|
| 222 |
MenuAction * |
| 223 |
GameOptions::getPlayersAction() |
| 224 |
{ |
| 225 |
if (!playersAction) |
| 226 |
playersAction = new PlayersAction(*this); |
| 227 |
|
| 228 |
return playersAction; |
| 229 |
} |
| 230 |
|
| 231 |
class StartSinglePlayerGameAction : public MenuAction |
| 232 |
{ |
| 233 |
Main &main; |
| 234 |
GameOptions &gameOptions; |
| 235 |
|
| 236 |
public: |
| 237 |
StartSinglePlayerGameAction(Main &m, GameOptions &go) |
| 238 |
: main(m), gameOptions(go) { } |
| 239 |
|
| 240 |
void invoke() |
| 241 |
{ |
| 242 |
main.startSinglePlayerGame(gameOptions); |
| 243 |
} |
| 244 |
}; |
| 245 |
|
| 246 |
MenuAction * |
| 247 |
GameOptions::getStartSinglePlayerGameAction(Main &main) |
| 248 |
{ |
| 249 |
if (!startSinglePlayerGameAction) |
| 250 |
startSinglePlayerGameAction = new StartSinglePlayerGameAction(main, *this); |
| 251 |
|
| 252 |
return startSinglePlayerGameAction; |
| 253 |
} |