| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name ui.cpp - The user interface globals. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1999-2010 by Lutz Sammer, Andreas Arens, and |
| 13 |
// Jimmy Salmon |
| 14 |
// |
| 15 |
// This program is free software; you can redistribute it and/or modify |
| 16 |
// it under the terms of the GNU General Public License as published by |
| 17 |
// the Free Software Foundation; only version 2 of the License. |
| 18 |
// |
| 19 |
// This program is distributed in the hope that it will be useful, |
| 20 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
// GNU General Public License for more details. |
| 23 |
// |
| 24 |
// You should have received a copy of the GNU General Public License |
| 25 |
// along with this program; if not, write to the Free Software |
| 26 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 27 |
// 02111-1307, USA. |
| 28 |
// |
| 29 |
|
| 30 |
//@{ |
| 31 |
|
| 32 |
/*---------------------------------------------------------------------------- |
| 33 |
-- Includes |
| 34 |
----------------------------------------------------------------------------*/ |
| 35 |
|
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
|
| 40 |
#include "stratagus.h" |
| 41 |
#include "video.h" |
| 42 |
#include "font.h" |
| 43 |
#include "interface.h" |
| 44 |
#include "map.h" |
| 45 |
#include "ui.h" |
| 46 |
#include "menus.h" |
| 47 |
#include "iolib.h" |
| 48 |
#include "unit.h" |
| 49 |
#include "title.h" |
| 50 |
|
| 51 |
/*---------------------------------------------------------------------------- |
| 52 |
-- Variables |
| 53 |
----------------------------------------------------------------------------*/ |
| 54 |
|
| 55 |
bool RightButtonAttacks; /// right button attacks |
| 56 |
|
| 57 |
|
| 58 |
/** |
| 59 |
** The user interface configuration |
| 60 |
*/ |
| 61 |
CUserInterface UI; |
| 62 |
|
| 63 |
/*---------------------------------------------------------------------------- |
| 64 |
-- Functions |
| 65 |
----------------------------------------------------------------------------*/ |
| 66 |
|
| 67 |
CUserInterface::CUserInterface() : |
| 68 |
MouseScroll(false), KeyScroll(false), |
| 69 |
MouseScrollSpeedDefault(0), MouseScrollSpeedControl(0), |
| 70 |
MouseWarpX(0), MouseWarpY(0), |
| 71 |
SingleSelectedButton(NULL), |
| 72 |
MaxSelectedFont(NULL), MaxSelectedTextX(0), MaxSelectedTextY(0), |
| 73 |
SingleTrainingButton(NULL), |
| 74 |
SingleTrainingFont(NULL), SingleTrainingTextX(0), SingleTrainingTextY(0), |
| 75 |
TrainingFont(NULL), TrainingTextX(0), TrainingTextY(0), |
| 76 |
CompletedBarColor(0), CompletedBarShadow(0), |
| 77 |
ViewportMode(VIEWPORT_SINGLE), MouseViewport(NULL), |
| 78 |
SelectedViewport(NULL), NumViewports(0), |
| 79 |
MessageFont(NULL), MessageScrollSpeed(5), |
| 80 |
ViewportCursorColor(0), Offset640X(0), Offset480Y(0) |
| 81 |
{ |
| 82 |
memset(&CompletedBarColorRGB, 0, sizeof(CompletedBarColorRGB)); |
| 83 |
|
| 84 |
memset(SavedMapPositionX, 0, sizeof(SavedMapPositionX)); |
| 85 |
memset(SavedMapPositionY, 0, sizeof(SavedMapPositionY)); |
| 86 |
|
| 87 |
Point.Name = "cursor-point"; |
| 88 |
Glass.Name = "cursor-glass"; |
| 89 |
Cross.Name = "cursor-cross"; |
| 90 |
YellowHair.Name = "cursor-yellow-hair"; |
| 91 |
GreenHair.Name = "cursor-green-hair"; |
| 92 |
RedHair.Name = "cursor-red-hair"; |
| 93 |
Scroll.Name = "cursor-scroll"; |
| 94 |
|
| 95 |
ArrowE.Name = "cursor-arrow-e"; |
| 96 |
ArrowNE.Name = "cursor-arrow-ne"; |
| 97 |
ArrowN.Name = "cursor-arrow-n"; |
| 98 |
ArrowNW.Name = "cursor-arrow-nw"; |
| 99 |
ArrowW.Name = "cursor-arrow-w"; |
| 100 |
ArrowSW.Name = "cursor-arrow-sw"; |
| 101 |
ArrowS.Name = "cursor-arrow-s"; |
| 102 |
ArrowSE.Name = "cursor-arrow-se"; |
| 103 |
|
| 104 |
NormalFontColor = "light-blue"; |
| 105 |
ReverseFontColor = "yellow"; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
/** |
| 110 |
** Initialize the user interface. |
| 111 |
*/ |
| 112 |
void InitUserInterface(void) |
| 113 |
{ |
| 114 |
UI.Offset640X = (Video.Width - 640) / 2; |
| 115 |
UI.Offset480Y = (Video.Height - 480) / 2; |
| 116 |
|
| 117 |
// |
| 118 |
// Calculations |
| 119 |
// |
| 120 |
if (Map.Info.MapWidth) { |
| 121 |
if (UI.MapArea.EndX > Map.Info.MapWidth * TileSizeX - 1) { |
| 122 |
UI.MapArea.EndX = Map.Info.MapWidth * TileSizeX - 1; |
| 123 |
} |
| 124 |
if (UI.MapArea.EndY > Map.Info.MapHeight * TileSizeY - 1) { |
| 125 |
UI.MapArea.EndY = Map.Info.MapHeight * TileSizeY - 1; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
UI.SelectedViewport = UI.Viewports; |
| 130 |
|
| 131 |
SetViewportMode(VIEWPORT_SINGLE); |
| 132 |
|
| 133 |
UI.CompletedBarColor = Video.MapRGB(TheScreen->format, |
| 134 |
UI.CompletedBarColorRGB.r, |
| 135 |
UI.CompletedBarColorRGB.g, |
| 136 |
UI.CompletedBarColorRGB.b); |
| 137 |
UI.ViewportCursorColor = ColorWhite; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
** Load Cursor. |
| 142 |
*/ |
| 143 |
void CursorConfig::Load() |
| 144 |
{ |
| 145 |
Assert(!Name.empty()); |
| 146 |
Cursor = CursorByIdent(Name); |
| 147 |
Assert(Name == Cursor->Ident); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
** Load the user interface graphics. |
| 152 |
*/ |
| 153 |
void CUserInterface::Load(void) |
| 154 |
{ |
| 155 |
int i; |
| 156 |
|
| 157 |
// |
| 158 |
// Load graphics |
| 159 |
// |
| 160 |
for (i = 0; i < (int)Fillers.size(); ++i) { |
| 161 |
Fillers[i].G->Load(); |
| 162 |
Fillers[i].G->UseDisplayFormat(); |
| 163 |
} |
| 164 |
|
| 165 |
for (i = 0; i < MaxCosts; ++i) { |
| 166 |
if (Resources[i].G) { |
| 167 |
Resources[i].G->Load(); |
| 168 |
Resources[i].G->UseDisplayFormat(); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if (ButtonPanel.G) { |
| 173 |
ButtonPanel.G->Load(); |
| 174 |
ButtonPanel.G->UseDisplayFormat(); |
| 175 |
} |
| 176 |
if (PieMenu.G) { |
| 177 |
PieMenu.G->Load(); |
| 178 |
PieMenu.G->UseDisplayFormat(); |
| 179 |
} |
| 180 |
|
| 181 |
// |
| 182 |
// Resolve cursors |
| 183 |
// |
| 184 |
Point.Load(); |
| 185 |
Glass.Load(); |
| 186 |
Cross.Load(); |
| 187 |
YellowHair.Load(); |
| 188 |
GreenHair.Load(); |
| 189 |
RedHair.Load(); |
| 190 |
Scroll.Load(); |
| 191 |
|
| 192 |
ArrowE.Load(); |
| 193 |
ArrowNE.Load(); |
| 194 |
ArrowN.Load(); |
| 195 |
ArrowNW.Load(); |
| 196 |
ArrowW.Load(); |
| 197 |
ArrowSW.Load(); |
| 198 |
ArrowS.Load(); |
| 199 |
ArrowSE.Load(); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
** Save the viewports. |
| 204 |
** |
| 205 |
** @param file Save file handle |
| 206 |
** @param ui User interface to save |
| 207 |
*/ |
| 208 |
static void SaveViewports(CFile *file, const CUserInterface *ui) |
| 209 |
{ |
| 210 |
int i; |
| 211 |
const CViewport *vp; |
| 212 |
|
| 213 |
file->printf("DefineViewports(\"mode\", %d", ui->ViewportMode); |
| 214 |
for (i = 0; i < ui->NumViewports; ++i) { |
| 215 |
vp = &ui->Viewports[i]; |
| 216 |
file->printf(",\n \"viewport\", {%d, %d, %d}", vp->MapX, vp->MapY, |
| 217 |
vp->Unit ? UnitNumber(vp->Unit) : -1); |
| 218 |
} |
| 219 |
file->printf(")\n\n"); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
** Save the saved map positions. |
| 224 |
** |
| 225 |
** @param file Save file handle |
| 226 |
** @param ui User interface to save |
| 227 |
*/ |
| 228 |
static void SaveSavedMapPositions(CFile *file, const CUserInterface *ui) |
| 229 |
{ |
| 230 |
for (int i = 0; i < MAX_SAVED_MAP_POSITIONS; ++i) |
| 231 |
{ |
| 232 |
file->printf("SetSavedMapPosition(%d, %d, %d)\n", |
| 233 |
i, |
| 234 |
ui->SavedMapPositionX[i], |
| 235 |
ui->SavedMapPositionY[i]); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
** Save the user interface module. |
| 241 |
** |
| 242 |
** @param file Save file handle |
| 243 |
*/ |
| 244 |
void SaveUserInterface(CFile *file) |
| 245 |
{ |
| 246 |
SaveViewports(file, &UI); |
| 247 |
SaveSavedMapPositions(file, &UI); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
** Clean up a user interface. |
| 252 |
*/ |
| 253 |
CUserInterface::~CUserInterface() |
| 254 |
{ |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
** Clean up the user interface module. |
| 259 |
*/ |
| 260 |
void CleanUserInterface(void) |
| 261 |
{ |
| 262 |
int i; |
| 263 |
|
| 264 |
// Filler |
| 265 |
for (i = 0; i < (int)UI.Fillers.size(); ++i) { |
| 266 |
CGraphic::Free(UI.Fillers[i].G); |
| 267 |
} |
| 268 |
UI.Fillers.clear(); |
| 269 |
|
| 270 |
// Resource Icons |
| 271 |
for (i = 0; i < MaxCosts; ++i) { |
| 272 |
CGraphic::Free(UI.Resources[i].G); |
| 273 |
} |
| 274 |
|
| 275 |
delete UI.SingleSelectedButton; |
| 276 |
UI.SelectedButtons.clear(); |
| 277 |
delete UI.SingleTrainingButton; |
| 278 |
UI.SingleTrainingText.clear(); |
| 279 |
UI.TrainingButtons.clear(); |
| 280 |
UI.TrainingText.clear(); |
| 281 |
UI.TransportingButtons.clear(); |
| 282 |
|
| 283 |
// Button Panel |
| 284 |
CGraphic::Free(UI.ButtonPanel.G); |
| 285 |
|
| 286 |
// Pie Menu |
| 287 |
CGraphic::Free(UI.PieMenu.G); |
| 288 |
|
| 289 |
// Title Screens |
| 290 |
if (TitleScreens) { |
| 291 |
for (i = 0; TitleScreens[i]; ++i) { |
| 292 |
delete TitleScreens[i]; |
| 293 |
} |
| 294 |
delete[] TitleScreens; |
| 295 |
TitleScreens = NULL; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
#ifdef DEBUG |
| 300 |
void FreeButtonStyles() |
| 301 |
{ |
| 302 |
std::map<std::string, ButtonStyle *>::iterator i; |
| 303 |
for (i = ButtonStyleHash.begin(); i != ButtonStyleHash.end(); ++i) { |
| 304 |
delete (*i).second; |
| 305 |
} |
| 306 |
ButtonStyleHash.clear(); |
| 307 |
} |
| 308 |
#endif |
| 309 |
|
| 310 |
/** |
| 311 |
** Takes coordinates of a pixel in stratagus's window and computes |
| 312 |
** the map viewport which contains this pixel. |
| 313 |
** |
| 314 |
** @param x x pixel coordinate with origin at UL corner of screen |
| 315 |
** @param y y pixel coordinate with origin at UL corner of screen |
| 316 |
** |
| 317 |
** @return viewport pointer or NULL if this pixel is not inside |
| 318 |
** any of the viewports. |
| 319 |
** |
| 320 |
** @note This functions only works with rectangular viewports, when |
| 321 |
** we support shaped map window, this must be rewritten. |
| 322 |
*/ |
| 323 |
CViewport *GetViewport(int x, int y) |
| 324 |
{ |
| 325 |
CViewport *vp; |
| 326 |
|
| 327 |
for (vp = UI.Viewports; vp < UI.Viewports + UI.NumViewports; ++vp) { |
| 328 |
if (x >= vp->X && x <= vp->EndX && y >= vp->Y && y <= vp->EndY) { |
| 329 |
return vp; |
| 330 |
} |
| 331 |
} |
| 332 |
return NULL; |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
** Takes an array of new Viewports which are supposed to have their |
| 337 |
** pixel geometry (CViewport::[XY] and CViewport::End[XY]) already |
| 338 |
** computed. Using this information as well as old viewport's |
| 339 |
** parameters fills in new viewports' CViewport::Map* parameters. |
| 340 |
** Then it replaces the old viewports with the new ones and finishes |
| 341 |
** the set-up of the new mode. |
| 342 |
** |
| 343 |
** @param new_vps The array of the new viewports |
| 344 |
** @param num_vps The number of elements in the new_vps[] array. |
| 345 |
*/ |
| 346 |
static void FinishViewportModeConfiguration(CViewport new_vps[], int num_vps) |
| 347 |
{ |
| 348 |
int i; |
| 349 |
|
| 350 |
if (UI.NumViewports < num_vps) { |
| 351 |
// Compute location of the viewport using oldviewport |
| 352 |
for (i = 0; i < num_vps; ++i) { |
| 353 |
const CViewport *vp; |
| 354 |
|
| 355 |
new_vps[i].MapX = 0; |
| 356 |
new_vps[i].MapY = 0; |
| 357 |
vp = GetViewport(new_vps[i].X, new_vps[i].Y); |
| 358 |
if (vp) { |
| 359 |
new_vps[i].OffsetX = new_vps[i].X - vp->X + vp->MapX * TileSizeX + vp->OffsetX; |
| 360 |
new_vps[i].OffsetY = new_vps[i].Y - vp->Y + vp->MapY * TileSizeY + vp->OffsetY; |
| 361 |
} else { |
| 362 |
new_vps[i].OffsetX = 0; |
| 363 |
new_vps[i].OffsetY = 0; |
| 364 |
} |
| 365 |
} |
| 366 |
} else { |
| 367 |
for (i = 0; i < num_vps; ++i) { |
| 368 |
new_vps[i].MapX = UI.Viewports[i].MapX; |
| 369 |
new_vps[i].MapY = UI.Viewports[i].MapY; |
| 370 |
new_vps[i].OffsetX = UI.Viewports[i].OffsetX; |
| 371 |
new_vps[i].OffsetY = UI.Viewports[i].OffsetY; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
// Affect the old viewport. |
| 376 |
for (i = 0; i < num_vps; ++i) { |
| 377 |
CViewport *vp; |
| 378 |
|
| 379 |
vp = UI.Viewports + i; |
| 380 |
vp->X = new_vps[i].X; |
| 381 |
vp->EndX = new_vps[i].EndX; |
| 382 |
vp->Y = new_vps[i].Y; |
| 383 |
vp->EndY = new_vps[i].EndY; |
| 384 |
vp->Set(new_vps[i].MapX, new_vps[i].MapY, new_vps[i].OffsetX, new_vps[i].OffsetY); |
| 385 |
} |
| 386 |
UI.NumViewports = num_vps; |
| 387 |
|
| 388 |
// |
| 389 |
// Update the viewport pointers |
| 390 |
// |
| 391 |
UI.MouseViewport = GetViewport(CursorX, CursorY); |
| 392 |
if (UI.SelectedViewport > UI.Viewports + UI.NumViewports - 1) { |
| 393 |
UI.SelectedViewport = UI.Viewports + UI.NumViewports - 1; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
** Takes a viewport which is supposed to have its CViewport::[XY] |
| 399 |
** correctly filled-in and computes CViewport::End[XY] attributes |
| 400 |
** according to clipping information passed in other two arguments. |
| 401 |
** |
| 402 |
** @param vp The viewport. |
| 403 |
** @param ClipX Maximum x-coordinate of the viewport's right side |
| 404 |
** as dictated by current UI's geometry and ViewportMode. |
| 405 |
** @param ClipY Maximum y-coordinate of the viewport's bottom side |
| 406 |
** as dictated by current UI's geometry and ViewportMode. |
| 407 |
** |
| 408 |
** @note It is supposed that values passed in Clip[XY] will |
| 409 |
** never be greater than UI::MapArea::End[XY]. |
| 410 |
** However, they can be smaller according to the place |
| 411 |
** the viewport vp takes in context of current ViewportMode. |
| 412 |
*/ |
| 413 |
static void ClipViewport(CViewport *vp, int ClipX, int ClipY) |
| 414 |
{ |
| 415 |
// begin with maximum possible viewport size |
| 416 |
vp->EndX = vp->X + Map.Info.MapWidth * TileSizeX - 1; |
| 417 |
vp->EndY = vp->Y + Map.Info.MapHeight * TileSizeY - 1; |
| 418 |
|
| 419 |
// first clip it to MapArea size if necessary |
| 420 |
if (vp->EndX > ClipX) { |
| 421 |
vp->EndX = ClipX; |
| 422 |
} |
| 423 |
|
| 424 |
// the same for y |
| 425 |
if (vp->EndY > ClipY) { |
| 426 |
vp->EndY = ClipY; |
| 427 |
} |
| 428 |
|
| 429 |
Assert(vp->EndX <= UI.MapArea.EndX); |
| 430 |
Assert(vp->EndY <= UI.MapArea.EndY); |
| 431 |
} |
| 432 |
|
| 433 |
/** |
| 434 |
** Compute viewport parameters for single viewport mode. |
| 435 |
** |
| 436 |
** The parameters include viewport's width and height expressed |
| 437 |
** in pixels, its position with respect to Stratagus's window |
| 438 |
** origin, and the corresponding map parameters expressed in map |
| 439 |
** tiles with origin at map origin (map tile (0,0)). |
| 440 |
*/ |
| 441 |
static void SetViewportModeSingle(void) |
| 442 |
{ |
| 443 |
CViewport new_vps[MAX_NUM_VIEWPORTS]; |
| 444 |
|
| 445 |
DebugPrint("Single viewport set\n"); |
| 446 |
|
| 447 |
new_vps[0].X = UI.MapArea.X; |
| 448 |
new_vps[0].Y = UI.MapArea.Y; |
| 449 |
ClipViewport(new_vps, UI.MapArea.EndX, UI.MapArea.EndY); |
| 450 |
|
| 451 |
FinishViewportModeConfiguration(new_vps, 1); |
| 452 |
} |
| 453 |
|
| 454 |
/** |
| 455 |
** Compute viewport parameters for horizontally split viewport mode. |
| 456 |
** This mode splits the UI::MapArea with a horizontal line to |
| 457 |
** 2 (approximately) equal parts. |
| 458 |
** |
| 459 |
** The parameters include viewport's width and height expressed |
| 460 |
** in pixels, its position with respect to Stratagus's window |
| 461 |
** origin, and the corresponding map parameters expressed in map |
| 462 |
** tiles with origin at map origin (map tile (0,0)). |
| 463 |
*/ |
| 464 |
static void SetViewportModeSplitHoriz(void) |
| 465 |
{ |
| 466 |
CViewport new_vps[MAX_NUM_VIEWPORTS]; |
| 467 |
|
| 468 |
DebugPrint("Two horizontal viewports set\n"); |
| 469 |
|
| 470 |
new_vps[0].X = UI.MapArea.X; |
| 471 |
new_vps[0].Y = UI.MapArea.Y; |
| 472 |
ClipViewport(new_vps, UI.MapArea.EndX, |
| 473 |
UI.MapArea.Y + (UI.MapArea.EndY - UI.MapArea.Y + 1) / 2); |
| 474 |
|
| 475 |
new_vps[1].X = UI.MapArea.X; |
| 476 |
new_vps[1].Y = new_vps[0].EndY + 1; |
| 477 |
ClipViewport(new_vps + 1, UI.MapArea.EndX, UI.MapArea.EndY); |
| 478 |
|
| 479 |
FinishViewportModeConfiguration(new_vps, 2); |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
** Compute viewport parameters for horizontal 3-way split viewport mode. |
| 484 |
** This mode splits the UI::MapArea with a horizontal line to |
| 485 |
** 2 (approximately) equal parts, then splits the bottom part vertically |
| 486 |
** to another 2 parts. |
| 487 |
** |
| 488 |
** The parameters include viewport's width and height expressed |
| 489 |
** in pixels, its position with respect to Stratagus's window |
| 490 |
** origin, and the corresponding map parameters expressed in map |
| 491 |
** tiles with origin at map origin (map tile (0,0)). |
| 492 |
*/ |
| 493 |
static void SetViewportModeSplitHoriz3(void) |
| 494 |
{ |
| 495 |
CViewport new_vps[MAX_NUM_VIEWPORTS]; |
| 496 |
|
| 497 |
DebugPrint("Horizontal 3-way viewport division set\n"); |
| 498 |
|
| 499 |
new_vps[0].X = UI.MapArea.X; |
| 500 |
new_vps[0].Y = UI.MapArea.Y; |
| 501 |
ClipViewport(new_vps, UI.MapArea.EndX, |
| 502 |
UI.MapArea.Y + (UI.MapArea.EndY - UI.MapArea.Y + 1) / 2); |
| 503 |
|
| 504 |
new_vps[1].X = UI.MapArea.X; |
| 505 |
new_vps[1].Y = new_vps[0].EndY + 1; |
| 506 |
ClipViewport(new_vps + 1, |
| 507 |
UI.MapArea.X + (UI.MapArea.EndX - UI.MapArea.X + 1) / 2, |
| 508 |
UI.MapArea.EndY); |
| 509 |
|
| 510 |
new_vps[2].X = new_vps[1].EndX + 1; |
| 511 |
new_vps[2].Y = new_vps[0].EndY + 1; |
| 512 |
ClipViewport(new_vps + 2, UI.MapArea.EndX, UI.MapArea.EndY); |
| 513 |
|
| 514 |
FinishViewportModeConfiguration(new_vps, 3); |
| 515 |
} |
| 516 |
|
| 517 |
/** |
| 518 |
** Compute viewport parameters for vertically split viewport mode. |
| 519 |
** This mode splits the UI::MapArea with a vertical line to |
| 520 |
** 2 (approximately) equal parts. |
| 521 |
** |
| 522 |
** The parameters include viewport's width and height expressed |
| 523 |
** in pixels, its position with respect to Stratagus's window |
| 524 |
** origin, and the corresponding map parameters expressed in map |
| 525 |
** tiles with origin at map origin (map tile (0,0)). |
| 526 |
*/ |
| 527 |
static void SetViewportModeSplitVert(void) |
| 528 |
{ |
| 529 |
CViewport new_vps[MAX_NUM_VIEWPORTS]; |
| 530 |
|
| 531 |
DebugPrint("Two vertical viewports set\n"); |
| 532 |
|
| 533 |
new_vps[0].X = UI.MapArea.X; |
| 534 |
new_vps[0].Y = UI.MapArea.Y; |
| 535 |
ClipViewport(new_vps, |
| 536 |
UI.MapArea.X + (UI.MapArea.EndX - UI.MapArea.X + 1) / 2, |
| 537 |
UI.MapArea.EndY); |
| 538 |
|
| 539 |
new_vps[1].X = new_vps[0].EndX + 1; |
| 540 |
new_vps[1].Y = UI.MapArea.Y; |
| 541 |
ClipViewport(new_vps + 1, UI.MapArea.EndX, UI.MapArea.EndY); |
| 542 |
|
| 543 |
FinishViewportModeConfiguration(new_vps, 2); |
| 544 |
} |
| 545 |
|
| 546 |
/** |
| 547 |
** Compute viewport parameters for 4-way split viewport mode. |
| 548 |
** This mode splits the UI::MapArea vertically *and* horizontally |
| 549 |
** to 4 (approximately) equal parts. |
| 550 |
** |
| 551 |
** The parameters include viewport's width and height expressed |
| 552 |
** in pixels, its position with respect to Stratagus's window |
| 553 |
** origin, and the corresponding map parameters expressed in map |
| 554 |
** tiles with origin at map origin (map tile (0,0)). |
| 555 |
*/ |
| 556 |
static void SetViewportModeQuad(void) |
| 557 |
{ |
| 558 |
CViewport new_vps[MAX_NUM_VIEWPORTS]; |
| 559 |
|
| 560 |
DebugPrint("Four viewports set\n"); |
| 561 |
|
| 562 |
new_vps[0].X = UI.MapArea.X; |
| 563 |
new_vps[0].Y = UI.MapArea.Y; |
| 564 |
ClipViewport(new_vps, |
| 565 |
UI.MapArea.X + (UI.MapArea.EndX - UI.MapArea.X + 1) / 2, |
| 566 |
UI.MapArea.Y + (UI.MapArea.EndY - UI.MapArea.Y + 1) / 2); |
| 567 |
|
| 568 |
new_vps[1].X = new_vps[0].EndX + 1; |
| 569 |
new_vps[1].Y = UI.MapArea.Y; |
| 570 |
ClipViewport(new_vps + 1, |
| 571 |
UI.MapArea.EndX, |
| 572 |
UI.MapArea.Y + (UI.MapArea.EndY - UI.MapArea.Y + 1) / 2); |
| 573 |
|
| 574 |
new_vps[2].X = UI.MapArea.X; |
| 575 |
new_vps[2].Y = new_vps[0].EndY + 1; |
| 576 |
ClipViewport(new_vps + 2, |
| 577 |
UI.MapArea.X + (UI.MapArea.EndX - UI.MapArea.X + 1) / 2, |
| 578 |
UI.MapArea.EndY); |
| 579 |
|
| 580 |
new_vps[3].X = new_vps[1].X; |
| 581 |
new_vps[3].Y = new_vps[2].Y; |
| 582 |
ClipViewport(new_vps + 3, UI.MapArea.EndX, UI.MapArea.EndY); |
| 583 |
|
| 584 |
FinishViewportModeConfiguration(new_vps, 4); |
| 585 |
} |
| 586 |
|
| 587 |
/** |
| 588 |
** Sets up (calls geometry setup routines for) a new viewport mode. |
| 589 |
** |
| 590 |
** @param new_mode New mode's number. |
| 591 |
*/ |
| 592 |
void SetViewportMode(ViewportModeType new_mode) |
| 593 |
{ |
| 594 |
switch (UI.ViewportMode = new_mode) { |
| 595 |
case VIEWPORT_SINGLE: |
| 596 |
SetViewportModeSingle(); |
| 597 |
break; |
| 598 |
case VIEWPORT_SPLIT_HORIZ: |
| 599 |
SetViewportModeSplitHoriz(); |
| 600 |
break; |
| 601 |
case VIEWPORT_SPLIT_HORIZ3: |
| 602 |
SetViewportModeSplitHoriz3(); |
| 603 |
break; |
| 604 |
case VIEWPORT_SPLIT_VERT: |
| 605 |
SetViewportModeSplitVert(); |
| 606 |
break; |
| 607 |
case VIEWPORT_QUAD: |
| 608 |
SetViewportModeQuad(); |
| 609 |
break; |
| 610 |
default: |
| 611 |
DebugPrint("trying to set an unknown mode!!\n"); |
| 612 |
break; |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
/** |
| 617 |
** Cycles through predefined viewport modes (geometry configurations) |
| 618 |
** in order defined by the ViewportMode enumerated type. |
| 619 |
** |
| 620 |
** @param step The size of step used for cycling. Values that |
| 621 |
** make sense are mostly 1 (next viewport mode) and |
| 622 |
** -1 (previous viewport mode). |
| 623 |
*/ |
| 624 |
void CycleViewportMode(int step) |
| 625 |
{ |
| 626 |
int new_mode; |
| 627 |
|
| 628 |
new_mode = UI.ViewportMode + step; |
| 629 |
if (new_mode >= NUM_VIEWPORT_MODES) { |
| 630 |
new_mode = 0; |
| 631 |
} |
| 632 |
if (new_mode < 0) { |
| 633 |
new_mode = NUM_VIEWPORT_MODES - 1; |
| 634 |
} |
| 635 |
SetViewportMode((ViewportModeType)new_mode); |
| 636 |
} |
| 637 |
|
| 638 |
/** |
| 639 |
** Check if mouse scrolling is enabled |
| 640 |
*/ |
| 641 |
bool GetMouseScroll(void) |
| 642 |
{ |
| 643 |
return UI.MouseScroll; |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
** Enable/disable scrolling with the mouse |
| 648 |
** |
| 649 |
** @param enabled True to enable mouse scrolling, false to disable |
| 650 |
*/ |
| 651 |
void SetMouseScroll(bool enabled) |
| 652 |
{ |
| 653 |
UI.MouseScroll = enabled; |
| 654 |
} |
| 655 |
|
| 656 |
/** |
| 657 |
** Check if keyboard scrolling is enabled |
| 658 |
*/ |
| 659 |
bool GetKeyScroll() |
| 660 |
{ |
| 661 |
return UI.KeyScroll; |
| 662 |
} |
| 663 |
|
| 664 |
/** |
| 665 |
** Enable/disable scrolling with the keyboard |
| 666 |
** |
| 667 |
** @param enabled True to enable keyboard scrolling, false to disable |
| 668 |
*/ |
| 669 |
void SetKeyScroll(bool enabled) |
| 670 |
{ |
| 671 |
UI.KeyScroll = enabled; |
| 672 |
} |
| 673 |
|
| 674 |
/** |
| 675 |
** Check if mouse grabbing is enabled |
| 676 |
*/ |
| 677 |
bool GetGrabMouse(void) |
| 678 |
{ |
| 679 |
return SdlGetGrabMouse(); |
| 680 |
} |
| 681 |
|
| 682 |
/** |
| 683 |
** Enable/disable grabbing the mouse |
| 684 |
** |
| 685 |
** @param enabled True to enable mouse grabbing, false to disable |
| 686 |
*/ |
| 687 |
void SetGrabMouse(bool enabled) |
| 688 |
{ |
| 689 |
ToggleGrabMouse(enabled ? 1 : -1); |
| 690 |
} |
| 691 |
|
| 692 |
/** |
| 693 |
** Check if scrolling stops when leaving the window |
| 694 |
*/ |
| 695 |
bool GetLeaveStops(void) |
| 696 |
{ |
| 697 |
return LeaveStops; |
| 698 |
} |
| 699 |
|
| 700 |
/** |
| 701 |
** Enable/disable leaving the window stops scrolling |
| 702 |
** |
| 703 |
** @param enabled True to stop scrolling, false to disable |
| 704 |
*/ |
| 705 |
void SetLeaveStops(bool enabled) |
| 706 |
{ |
| 707 |
LeaveStops = enabled; |
| 708 |
} |
| 709 |
|
| 710 |
/** |
| 711 |
** Set saved map position |
| 712 |
** |
| 713 |
** @param index Saved map position slot. |
| 714 |
** @param x X coordinate |
| 715 |
** @param y Y coordinate |
| 716 |
*/ |
| 717 |
void SetSavedMapPosition(int index, int x, int y) |
| 718 |
{ |
| 719 |
if (index < 0 || index >= MAX_SAVED_MAP_POSITIONS) { |
| 720 |
return; |
| 721 |
} |
| 722 |
|
| 723 |
UI.SavedMapPositionX[index] = x; |
| 724 |
UI.SavedMapPositionY[index] = y; |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
/** |
| 729 |
** Recall map position |
| 730 |
** |
| 731 |
** @param index Saved map position slot. |
| 732 |
*/ |
| 733 |
void RecallSavedMapPosition(int index) |
| 734 |
{ |
| 735 |
if (index < 0 || index >= MAX_SAVED_MAP_POSITIONS) { |
| 736 |
return; |
| 737 |
} |
| 738 |
|
| 739 |
UI.SelectedViewport->Set( |
| 740 |
UI.SavedMapPositionX[index], UI.SavedMapPositionY[index], |
| 741 |
TileSizeX / 2, TileSizeY / 2); |
| 742 |
} |
| 743 |
|
| 744 |
//@} |