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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "QuitBox.h"
#include "MouseHandler.h"
#include "Game/Player.h"
#include "Game/PlayerHandler.h"
#include "Game/GameSetup.h"
#include "Game/GlobalUnsynced.h"
#include "Rendering/glFont.h"
#include "Rendering/GL/myGL.h"
#include "Sim/Misc/GlobalSynced.h"
#include "Sim/Misc/ModInfo.h"
#include "Sim/Misc/TeamHandler.h"
#include "System/Log/ILog.h"
#include "System/NetProtocol.h"
#include "System/TimeUtil.h"
#include "System/mmgr.h"
#include "System/FileSystem/FileSystem.h"
#include "System/LoadSave/LoadSaveHandler.h"
#include <SDL_keysym.h>
#define MAX_QUIT_TEAMS (teamHandler->ActiveTeams() - 1)
#undef CreateDirectory
CQuitBox::CQuitBox(void)
{
box.x1 = 0.34f;
box.y1 = 0.18f;
box.x2 = 0.66f;
box.y2 = 0.78f;
resignBox.x1=0.02f;
resignBox.y1=0.53f;
resignBox.x2=0.30f;
resignBox.y2=0.57f;
saveBox.x1=0.02f;
saveBox.y1=0.49f;
saveBox.x2=0.30f;
saveBox.y2=0.53f;
giveAwayBox.x1=0.02f;
giveAwayBox.y1=0.44f;
giveAwayBox.x2=0.30f;
giveAwayBox.y2=0.48f;
teamBox.x1=0.02f;
teamBox.y1=0.11f;
teamBox.x2=0.30f;
teamBox.y2=0.43f;
scrollbarBox.x1 = 0.28f;
scrollbarBox.y1 = 0.11f;
scrollbarBox.x2 = 0.30f;
scrollbarBox.y2 = 0.43f;
scrollBox.x1 = 0.28f;
scrollBox.y1 = 0.40f;
scrollBox.x2 = 0.30f;
scrollBox.y2 = 0.43f;
quitBox.x1=0.02f;
quitBox.y1=0.06f;
quitBox.x2=0.30f;
quitBox.y2=0.10f;
cancelBox.x1=0.02f;
cancelBox.y1=0.02f;
cancelBox.x2=0.30f;
cancelBox.y2=0.06f;
startTeam = 0;
numTeamsDisp = (int)((teamBox.y2 - teamBox.y1) / 0.025f);
scrolling = false;
scrollGrab = 0.0f;
hasScroll = MAX_QUIT_TEAMS > numTeamsDisp;
moveBox=false;
noAlliesLeft=true;
shareTeam=0;
// if we have alive allies left, set the shareteam to an undead ally.
for(int team=0;team<teamHandler->ActiveTeams();++team){
if (teamHandler->Team(team)->gaia) continue;
if(team!=gu->myTeam && !teamHandler->Team(team)->isDead)
{
if(shareTeam==gu->myTeam || teamHandler->Team(shareTeam)->isDead)
shareTeam=team;
if(teamHandler->Ally(gu->myAllyTeam, teamHandler->AllyTeam(team))){
noAlliesLeft=false;
shareTeam=team;
break;
}
}
}
}
CQuitBox::~CQuitBox(void)
{
}
void CQuitBox::Draw(void)
{
float mx=MouseX(mouse->lastx);
float my=MouseY(mouse->lasty);
glDisable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glDisable(GL_ALPHA_TEST);
// Large Box
glColor4f(0.2f,0.2f,0.2f,guiAlpha);
DrawBox(box);
// resign Box on mouse over
if(InBox(mx,my,box+resignBox)){
glColor4f(0.7f,0.2f,0.2f,guiAlpha);
DrawBox(box+resignBox);
}
// save Box on mouse over
if(InBox(mx,my,box+saveBox)){
glColor4f(0.7f,0.2f,0.2f,guiAlpha);
DrawBox(box+saveBox);
}
// give away Box on mouse over
if(InBox(mx,my,box+giveAwayBox)){
glColor4f(0.7f,0.2f,0.2f,guiAlpha);
DrawBox(box+giveAwayBox);
}
// cancel Box on mouse over
if(InBox(mx,my,box+cancelBox)){
glColor4f(0.7f,0.2f,0.2f,guiAlpha);
DrawBox(box+cancelBox);
}
// quit Box on mouse over
if(InBox(mx,my,box+quitBox)){
glColor4f(0.7f,0.2f,0.2f,guiAlpha);
DrawBox(box+quitBox);
}
glColor4f(0.2f,0.2f,0.2f,guiAlpha);
DrawBox(box+teamBox);
if (hasScroll) {
glColor4f(0.1f, 0.1f, 0.1f, guiAlpha);
DrawBox(box + scrollbarBox);
float sz = scrollbarBox.y2 - scrollbarBox.y1;
float tsz = sz / (float)(MAX_QUIT_TEAMS);
float psz = tsz * (float)numTeamsDisp;
scrollBox.y2 = scrollbarBox.y2 - startTeam * tsz;
scrollBox.y1 = scrollBox.y2 - psz;
glColor4f(0.8f, 0.8f, 0.8f, guiAlpha);
DrawBox(box + scrollBox);
}
glEnable(GL_TEXTURE_2D);
glColor4f(1,1,0.4f,0.8f);
font->glPrint(box.x1+0.045f,box.y1+0.58f,0.7f, FONT_VCENTER | FONT_SCALE | FONT_NORM,"Do you want to ...");
glColor4f(1,1,1,0.8f);
font->glPrint(box.x1 + resignBox.x1 + 0.025f,
box.y1 + (resignBox.y1 + resignBox.y2)/2, 1, FONT_VCENTER | FONT_SCALE | FONT_NORM, "Resign");
font->glPrint(box.x1 + saveBox.x1 + 0.025f,
box.y1 + (saveBox.y1 + saveBox.y2)/2, 1, FONT_VCENTER | FONT_SCALE | FONT_NORM, "Save");
font->glPrint(box.x1 + giveAwayBox.x1 + 0.025f,
box.y1 + (giveAwayBox.y1 + giveAwayBox.y2)/2, 1, FONT_VCENTER | FONT_SCALE | FONT_NORM, "Give everything to ...");
font->glPrint(box.x1 + cancelBox.x1 + 0.025f,
box.y1 + (cancelBox.y1 + cancelBox.y2)/2, 1, FONT_VCENTER | FONT_SCALE | FONT_NORM, "Cancel");
font->glPrint(box.x1 + quitBox.x1 + 0.025f,
box.y1 + (quitBox.y1 + quitBox.y2)/2, 1, FONT_VCENTER | FONT_SCALE | FONT_NORM, "Quit");
int teamPos = 0;
for(int team = startTeam; team < MAX_QUIT_TEAMS && teamPos < numTeamsDisp; ++team, ++teamPos) {
int actualTeam=team;
if (team >= gu->myTeam) {
actualTeam++;
}
if (teamHandler->Team(actualTeam)->gaia) continue;
if (shareTeam == actualTeam) {
glColor4f(1,1,1,0.8f);
} else {
glColor4f(1,1,1,0.4f);
}
std::string teamName;
if (teamHandler->Team(actualTeam)->leader >= 0)
teamName = playerHandler->Player(teamHandler->Team(actualTeam)->leader)->name;
else
teamName = "uncontrolled";
std::string ally, dead;
if (teamHandler->Ally(gu->myAllyTeam, teamHandler->AllyTeam(actualTeam))) {
ally = " <Ally>)";
} else {
ally = " <Enemy>";
}
if(teamHandler->Team(actualTeam)->isDead) {
dead = " <Dead>";
}
if (actualTeam == teamHandler->GaiaTeamID()) {
teamName = "Gaia";
ally = " <Gaia>";
}
font->glFormat(box.x1 + teamBox.x1 + 0.002f,
box.y1 + teamBox.y2 - 0.025f - teamPos * 0.025f, 0.7f, FONT_SCALE | FONT_NORM,
"Team%i (%s)%s%s", actualTeam,
teamName.c_str(), ally.c_str(), dead.c_str());
}
}
bool CQuitBox::IsAbove(int x, int y)
{
float mx=MouseX(x);
float my=MouseY(y);
if(InBox(mx,my,box))
return true;
return false;
}
std::string CQuitBox::GetTooltip(int x, int y)
{
float mx=MouseX(x);
float my=MouseY(y);
if(InBox(mx,my,box+resignBox))
return "Resign the match, remain in the game";
if(InBox(mx,my,box+saveBox))
return "Save the current game state to a file \nfor later reload";
if(InBox(mx,my,box+giveAwayBox))
return "Give away all units and resources \nto the team specified below";
if (hasScroll && InBox(mx, my, box + scrollBox))
return "Scroll the team list here";
if (hasScroll && InBox(mx, my, box + scrollbarBox))
return "Scroll the team list here";
if(InBox(mx,my,box+teamBox))
return "Select which team recieves everything";
if(InBox(mx,my,box+cancelBox))
return "Return to the game";
if(InBox(mx,my,box+quitBox))
return "Forget about the other players and quit";
if(InBox(mx,my,box))
return " ";
return "";
}
bool CQuitBox::MousePress(int x, int y, int button)
{
float mx=MouseX(x);
float my=MouseY(y);
if(InBox(mx,my,box)){
moveBox=true;
if(InBox(mx,my,box+resignBox) || InBox(mx,my,box+saveBox) || InBox(mx,my,box+giveAwayBox) || InBox(mx,my,box+teamBox) || InBox(mx,my,box+cancelBox) || InBox(mx,my,box+quitBox) ||
InBox(mx, my, box + scrollbarBox) || InBox(mx, my, box + scrollBox))
moveBox=false;
if (hasScroll && InBox(mx, my, box + scrollBox)) {
scrolling = true;
scrollGrab = (box + scrollBox).y2 - my;
}
else if (hasScroll && InBox(mx, my, box + scrollbarBox)) {
if(my < (box + scrollBox).y1)
*(volatile int *)&startTeam = startTeam + std::min(MAX_QUIT_TEAMS - numTeamsDisp - startTeam, numTeamsDisp);
if(my > (box + scrollBox).y2)
*(volatile int *)&startTeam = startTeam - std::min(startTeam, numTeamsDisp);
}
else if(InBox(mx,my,box+teamBox)){
int team = startTeam + (int)((box.y1 + teamBox.y2 - my) / 0.025f);
if(team>=gu->myTeam)
team++;
if(teamHandler->IsValidTeam(team) && !teamHandler->Team(team)->isDead){
// we don't want to give everything to the enemy if there are allies left
if(noAlliesLeft || (!noAlliesLeft && teamHandler->Ally(gu->myAllyTeam, teamHandler->AllyTeam(team)))){
shareTeam=team;
}
}
}
return true;
}
return false;
}
void CQuitBox::MouseRelease(int x,int y,int button)
{
float mx=MouseX(x);
float my=MouseY(y);
scrolling = false;
scrollGrab = 0.0f;
if(InBox(mx,my,box+resignBox)
|| (InBox(mx,my,box+saveBox) && !teamHandler->Team(gu->myTeam)->isDead)
|| (InBox(mx,my,box+giveAwayBox) && !teamHandler->Team(shareTeam)->isDead && !teamHandler->Team(gu->myTeam)->isDead)) {
// give away all units (and resources)
if(InBox(mx,my,box+giveAwayBox) && !playerHandler->Player(gu->myPlayerNum)->spectator) {
net->Send(CBaseNetProtocol::Get().SendGiveAwayEverything(gu->myPlayerNum, shareTeam, playerHandler->Player(gu->myPlayerNum)->team));
}
// resign, so self-d all units
if (InBox(mx,my,box+resignBox) && !playerHandler->Player(gu->myPlayerNum)->spectator) {
net->Send(CBaseNetProtocol::Get().SendResign(gu->myPlayerNum));
}
// save current game state
if (InBox(mx,my,box+saveBox)) {
if (FileSystem::CreateDirectory("Saves")) {
std::string timeStr = CTimeUtil::GetCurrentTimeStr();
std::string saveFileName(timeStr + "_" + modInfo.filename + "_" + gameSetup->mapName);
saveFileName = "Saves/" + saveFileName + ".ssf";
if (!FileSystem::FileExists(saveFileName)) {
LOG("Saving game to %s", saveFileName.c_str());
ILoadSaveHandler* ls = ILoadSaveHandler::Create();
ls->mapName = gameSetup->mapName;
ls->modName = modInfo.filename;
ls->SaveGame(saveFileName);
delete ls;
} else {
LOG_L(L_ERROR, "File %s already exists, game NOT saved!",
saveFileName.c_str());
}
}
}
}
else if (InBox(mx, my, box + quitBox)) {
LOG("User exited");
gu->globalQuit = true;
}
// if we're still in the game, remove the resign box
if(InBox(mx,my,box+resignBox) || InBox(mx,my,box+saveBox) || InBox(mx,my,box+giveAwayBox) || InBox(mx,my,box+cancelBox) || InBox(mx,my,box+quitBox)){
delete this;
return;
}
moveBox=false;
}
void CQuitBox::MouseMove(int x, int y, int dx,int dy, int button)
{
float mx=MouseX(x);
float my=MouseY(y);
if(scrolling) {
float scr = (box+scrollbarBox).y2 - (my + scrollGrab);
float sz = scrollbarBox.y2 - scrollbarBox.y1;
float tsz = sz / (float)MAX_QUIT_TEAMS;
*(volatile int *)&startTeam = std::max(0, std::min((int)(scr / tsz + 0.5), MAX_QUIT_TEAMS - numTeamsDisp));
return;
}
if(moveBox){
box.x1+=MouseMoveX(dx);
box.x2+=MouseMoveX(dx);
box.y1+=MouseMoveY(dy);
box.y2+=MouseMoveY(dy);
}
if(!(hasScroll && (InBox(mx, my, box + scrollBox) || InBox(mx, my, box + scrollbarBox))) && InBox(mx,my,box+teamBox)){
int team = startTeam + (int)((box.y1 + teamBox.y2 - my) / 0.025f);
if(team>=gu->myTeam)
team++;
if(teamHandler->IsValidTeam(team) && !teamHandler->Team(team)->isDead){
// we don't want to give everything to the enemy if there are allies left
if(noAlliesLeft || (!noAlliesLeft && teamHandler->Ally(gu->myAllyTeam, teamHandler->AllyTeam(team)))){
shareTeam=team;
}
}
}
}
bool CQuitBox::KeyPressed(unsigned short key, bool isRepeat)
{
if (key == SDLK_ESCAPE) {
delete this;
return true;
}
return false;
}
|