| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name botpanel.cpp - The bottom panel. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1999-2007 by Lutz Sammer, Vladi Belperchinov-Shabanski, |
| 13 |
// and 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 |
#include <ctype.h> |
| 40 |
#include <vector> |
| 41 |
#include <sstream> |
| 42 |
|
| 43 |
#include "stratagus.h" |
| 44 |
|
| 45 |
#include "unit.h" |
| 46 |
#include "unittype.h" |
| 47 |
#include "upgrade.h" |
| 48 |
#include "interface.h" |
| 49 |
#include "ui.h" |
| 50 |
#include "player.h" |
| 51 |
#include "spells.h" |
| 52 |
#include "sound.h" |
| 53 |
#include "map.h" |
| 54 |
#include "actions.h" |
| 55 |
#include "commands.h" |
| 56 |
#include "video.h" |
| 57 |
#include "font.h" |
| 58 |
#include "guichan/key.h" |
| 59 |
#include "guichan/sdl/sdlinput.h" |
| 60 |
|
| 61 |
/*---------------------------------------------------------------------------- |
| 62 |
-- Defines |
| 63 |
----------------------------------------------------------------------------*/ |
| 64 |
|
| 65 |
/*---------------------------------------------------------------------------- |
| 66 |
-- Variables |
| 67 |
----------------------------------------------------------------------------*/ |
| 68 |
|
| 69 |
/// for unit buttons sub-menus etc. |
| 70 |
int CurrentButtonLevel; |
| 71 |
/// All buttons for units |
| 72 |
std::vector<ButtonAction *> UnitButtonTable; |
| 73 |
/// Pointer to current buttons |
| 74 |
ButtonAction *CurrentButtons; |
| 75 |
|
| 76 |
/*---------------------------------------------------------------------------- |
| 77 |
-- Functions |
| 78 |
----------------------------------------------------------------------------*/ |
| 79 |
|
| 80 |
/** |
| 81 |
** Initialize the buttons. |
| 82 |
*/ |
| 83 |
void InitButtons(void) |
| 84 |
{ |
| 85 |
// Resolve the icon names. |
| 86 |
for (int z = 0; z < (int)UnitButtonTable.size(); ++z) { |
| 87 |
UnitButtonTable[z]->Icon.Load(); |
| 88 |
} |
| 89 |
delete[] CurrentButtons; |
| 90 |
CurrentButtons = NULL; |
| 91 |
} |
| 92 |
|
| 93 |
/*---------------------------------------------------------------------------- |
| 94 |
-- Buttons structures |
| 95 |
----------------------------------------------------------------------------*/ |
| 96 |
|
| 97 |
/** |
| 98 |
** Add a button |
| 99 |
*/ |
| 100 |
int AddButton(int pos, int level, const std::string &icon_ident, |
| 101 |
ButtonCmd action, const std::string &value, const ButtonCheckFunc func, |
| 102 |
const std::string &allow, const std::string &hint, const std::string &umask) |
| 103 |
{ |
| 104 |
std::string buf; |
| 105 |
ButtonAction *ba; |
| 106 |
|
| 107 |
ba = new ButtonAction; |
| 108 |
Assert(ba); |
| 109 |
|
| 110 |
ba->Pos = pos; |
| 111 |
ba->Level = level; |
| 112 |
ba->Icon.Name = icon_ident; |
| 113 |
// FIXME: check if already initited |
| 114 |
//ba->Icon.Load(); |
| 115 |
ba->Action = action; |
| 116 |
if (!value.empty()) { |
| 117 |
ba->ValueStr = value; |
| 118 |
switch (action) { |
| 119 |
case ButtonSpellCast: |
| 120 |
ba->Value = SpellTypeByIdent(value)->Slot; |
| 121 |
#ifdef DEBUG |
| 122 |
if (ba->Value < 0) { |
| 123 |
DebugPrint("Spell %s does not exist?\n" _C_ value.c_str()); |
| 124 |
Assert(ba->Value >= 0); |
| 125 |
} |
| 126 |
#endif |
| 127 |
break; |
| 128 |
case ButtonTrain: |
| 129 |
ba->Value = UnitTypeIdByIdent(value); |
| 130 |
break; |
| 131 |
case ButtonBuild: |
| 132 |
ba->Value = UnitTypeIdByIdent(value); |
| 133 |
break; |
| 134 |
default: |
| 135 |
ba->Value = atoi(value.c_str()); |
| 136 |
break; |
| 137 |
} |
| 138 |
} else { |
| 139 |
ba->ValueStr.clear(); |
| 140 |
ba->Value = 0; |
| 141 |
} |
| 142 |
|
| 143 |
ba->Allowed = func; |
| 144 |
ba->AllowStr = allow; |
| 145 |
int key = GetHotKey(hint); |
| 146 |
if (isascii(key) && isupper(key)) { |
| 147 |
key = tolower(key); |
| 148 |
} |
| 149 |
ba->Key = key; |
| 150 |
ba->Hint = hint; |
| 151 |
// FIXME: here should be added costs to the hint |
| 152 |
// FIXME: johns: show should be nice done? |
| 153 |
ba->UnitMask = umask; |
| 154 |
UnitButtonTable.push_back(ba); |
| 155 |
// FIXME: check if already initialized |
| 156 |
//Assert(ba->Icon.Icon != NULL);// just checks, that's why at the end |
| 157 |
return 1; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
/** |
| 162 |
** Cleanup buttons. |
| 163 |
*/ |
| 164 |
void CleanButtons(void) |
| 165 |
{ |
| 166 |
// Free the allocated buttons. |
| 167 |
for (int z = 0; z < (int)UnitButtonTable.size(); ++z) { |
| 168 |
Assert(UnitButtonTable[z]); |
| 169 |
delete UnitButtonTable[z]; |
| 170 |
} |
| 171 |
UnitButtonTable.clear(); |
| 172 |
|
| 173 |
CurrentButtonLevel = 0; |
| 174 |
delete[] CurrentButtons; |
| 175 |
CurrentButtons = NULL; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
** Return Status of button. |
| 180 |
** |
| 181 |
** @param button button to check status |
| 182 |
** |
| 183 |
** @return status of button |
| 184 |
** @return Icon(Active | Selected | Clicked | AutoCast | Disabled). |
| 185 |
** |
| 186 |
** @todo FIXME : add IconDisabled when needed. |
| 187 |
** @todo FIXME : Should show the rally action for training unit ? (NewOrder) |
| 188 |
*/ |
| 189 |
static int GetButtonStatus(const ButtonAction *button) |
| 190 |
{ |
| 191 |
int res; |
| 192 |
int action; |
| 193 |
int i; |
| 194 |
|
| 195 |
Assert(button); |
| 196 |
Assert(NumSelected); |
| 197 |
|
| 198 |
res = 0; |
| 199 |
// cursor is on that button |
| 200 |
if (ButtonAreaUnderCursor == ButtonAreaButton && ButtonUnderCursor == button->Pos - 1) { |
| 201 |
res |= IconActive; |
| 202 |
if (MouseButtons & LeftButton) { |
| 203 |
// Overwrite IconActive. |
| 204 |
res = IconClicked; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
action = UnitActionNone; |
| 209 |
switch (button->Action) { |
| 210 |
case ButtonStop: |
| 211 |
action = UnitActionStill; |
| 212 |
break; |
| 213 |
case ButtonStandGround: |
| 214 |
action = UnitActionStandGround; |
| 215 |
break; |
| 216 |
case ButtonAttack: |
| 217 |
action = UnitActionAttack; |
| 218 |
break; |
| 219 |
case ButtonAttackGround: |
| 220 |
action = UnitActionAttackGround; |
| 221 |
break; |
| 222 |
case ButtonPatrol: |
| 223 |
action = UnitActionPatrol; |
| 224 |
break; |
| 225 |
case ButtonHarvest: |
| 226 |
action = UnitActionResource; |
| 227 |
break; |
| 228 |
default: |
| 229 |
break; |
| 230 |
} |
| 231 |
// Simple case. |
| 232 |
if (action != UnitActionNone) { |
| 233 |
for (i = 0; i < NumSelected; ++i) { |
| 234 |
if (Selected[i]->Orders[0]->Action != action) { |
| 235 |
break; |
| 236 |
} |
| 237 |
} |
| 238 |
if (i == NumSelected) { |
| 239 |
res |= IconSelected; |
| 240 |
} |
| 241 |
return res; |
| 242 |
} |
| 243 |
// other cases : manage AutoCast and different possible action. |
| 244 |
switch (button->Action) { |
| 245 |
case ButtonMove: |
| 246 |
for (i = 0; i < NumSelected; ++i) { |
| 247 |
if (Selected[i]->Orders[0]->Action != UnitActionMove && |
| 248 |
Selected[i]->Orders[0]->Action != UnitActionBuild && |
| 249 |
Selected[i]->Orders[0]->Action != UnitActionFollow) { |
| 250 |
break; |
| 251 |
} |
| 252 |
} |
| 253 |
if (i == NumSelected) { |
| 254 |
res |= IconSelected; |
| 255 |
} |
| 256 |
break; |
| 257 |
case ButtonSpellCast: |
| 258 |
// FIXME : and IconSelected ? |
| 259 |
|
| 260 |
// Autocast |
| 261 |
for (i = 0; i < NumSelected; ++i) { |
| 262 |
Assert(Selected[i]->AutoCastSpell); |
| 263 |
if (Selected[i]->AutoCastSpell[button->Value] != 1) { |
| 264 |
break; |
| 265 |
} |
| 266 |
} |
| 267 |
if (i == NumSelected) { |
| 268 |
res |= IconAutoCast; |
| 269 |
} |
| 270 |
break; |
| 271 |
case ButtonRepair: |
| 272 |
for (i = 0; i < NumSelected; ++i) { |
| 273 |
if (Selected[i]->Orders[0]->Action != UnitActionRepair) { |
| 274 |
break; |
| 275 |
} |
| 276 |
} |
| 277 |
if (i == NumSelected) { |
| 278 |
res |= IconSelected; |
| 279 |
} |
| 280 |
// Auto repair |
| 281 |
for (i = 0; i < NumSelected; ++i) { |
| 282 |
if (Selected[i]->AutoRepair != 1) { |
| 283 |
break; |
| 284 |
} |
| 285 |
} |
| 286 |
if (i == NumSelected) { |
| 287 |
res |= IconAutoCast; |
| 288 |
} |
| 289 |
break; |
| 290 |
// FIXME: must handle more actions |
| 291 |
default: |
| 292 |
break; |
| 293 |
} |
| 294 |
return res; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
** Draw popup |
| 299 |
*/ |
| 300 |
static void DrawPopup() |
| 301 |
{ |
| 302 |
ButtonAction *button = &CurrentButtons[ButtonUnderCursor]; |
| 303 |
CUIButton *uibutton = &UI.ButtonPanel.Buttons[ButtonUnderCursor]; |
| 304 |
Uint32 backgroundColor = Video.MapRGB(TheScreen->format, 255, 255, 200); |
| 305 |
|
| 306 |
// Draw hint |
| 307 |
if (button->Action != ButtonBuild && button->Action != ButtonTrain) { |
| 308 |
int popupWidth = SmallFont->Width(button->Hint) + 10; |
| 309 |
int popupHeight = 19; |
| 310 |
int x = std::min(uibutton->X, Video.Width - 1 - popupWidth); |
| 311 |
int y = uibutton->Y - popupHeight - 10; |
| 312 |
std::string nc, rc; |
| 313 |
|
| 314 |
GetDefaultTextColors(nc, rc); |
| 315 |
SetDefaultTextColors("black", "red"); |
| 316 |
|
| 317 |
// Background |
| 318 |
Video.FillRectangle(backgroundColor, x, y, popupWidth, popupHeight); |
| 319 |
Video.DrawRectangle(ColorBlack, x, y, popupWidth, popupHeight); |
| 320 |
|
| 321 |
// Hint |
| 322 |
VideoDrawText(x + 5, y + 3, SmallFont, button->Hint); |
| 323 |
|
| 324 |
SetDefaultTextColors(nc, rc); |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
int popupWidth = 140; |
| 329 |
int popupHeight = 115; |
| 330 |
std::string nc, rc; |
| 331 |
CUnitType *type; |
| 332 |
|
| 333 |
int x = std::min(uibutton->X, Video.Width - 1 - popupWidth); |
| 334 |
int y = uibutton->Y - popupHeight - 10; |
| 335 |
type = UnitTypes[button->Value]; |
| 336 |
|
| 337 |
GetDefaultTextColors(nc, rc); |
| 338 |
SetDefaultTextColors("black", "red"); |
| 339 |
|
| 340 |
// Background |
| 341 |
Video.FillRectangle(backgroundColor, x, y, popupWidth, popupHeight); |
| 342 |
Video.DrawRectangle(ColorBlack, x, y, popupWidth, popupHeight); |
| 343 |
|
| 344 |
// Name |
| 345 |
VideoDrawText(x + 5, y + 3, SmallFont, type->Name); |
| 346 |
Video.DrawHLine(ColorBlack, x, y + 15, popupWidth); |
| 347 |
|
| 348 |
y += 20; |
| 349 |
|
| 350 |
// Costs |
| 351 |
for (int i = 0; i < MaxCosts; ++i) { |
| 352 |
if (type->ProductionCosts[i]) { |
| 353 |
if (UI.Resources[i].G) { |
| 354 |
UI.Resources[i].G->DrawFrameClip(UI.Resources[i].IconFrame, |
| 355 |
x + 5 + 60 * i, y); |
| 356 |
} |
| 357 |
VideoDrawNumber(x + 20 + 60 * i, y, SmallFont, type->ProductionCosts[i] / CYCLES_PER_SECOND); |
| 358 |
} |
| 359 |
} |
| 360 |
y += 15; |
| 361 |
|
| 362 |
// Hit Points |
| 363 |
std::ostringstream hitPoints; |
| 364 |
hitPoints << "Hit Points: " << type->Variable[HP_INDEX].Value; |
| 365 |
VideoDrawText(x + 5, y, SmallFont, hitPoints.str()); |
| 366 |
y += 15; |
| 367 |
|
| 368 |
if (type->CanAttack) { |
| 369 |
// Damage |
| 370 |
int min_damage = std::max(1, type->Variable[PIERCINGDAMAGE_INDEX].Value / 2); |
| 371 |
int max_damage = type->Variable[PIERCINGDAMAGE_INDEX].Value + type->Variable[BASICDAMAGE_INDEX].Value; |
| 372 |
std::ostringstream damage; |
| 373 |
damage << "Damage: " << min_damage << "-" << max_damage; |
| 374 |
VideoDrawText(x + 5, y, SmallFont, damage.str()); |
| 375 |
y += 15; |
| 376 |
|
| 377 |
// Attack Range |
| 378 |
std::ostringstream attackRange; |
| 379 |
attackRange << "Attack Range: " << type->Variable[ATTACKRANGE_INDEX].Value; |
| 380 |
VideoDrawText(x + 5, y, SmallFont, attackRange.str()); |
| 381 |
y += 15; |
| 382 |
} |
| 383 |
|
| 384 |
// Armor |
| 385 |
std::ostringstream armor; |
| 386 |
armor << "Armor: " << type->Variable[ARMOR_INDEX].Value; |
| 387 |
VideoDrawText(x + 5, y, SmallFont, armor.str()); |
| 388 |
y += 15; |
| 389 |
|
| 390 |
if (type->Variable[RADAR_INDEX].Value) { |
| 391 |
// Radar Range |
| 392 |
std::ostringstream radarRange; |
| 393 |
radarRange << "Radar Range: " << type->Variable[RADAR_INDEX].Value; |
| 394 |
VideoDrawText(x + 5, y, SmallFont, radarRange.str()); |
| 395 |
} else { |
| 396 |
// Sight Range |
| 397 |
std::ostringstream sightRange; |
| 398 |
sightRange << "Sight Range: " << type->Variable[SIGHTRANGE_INDEX].Value; |
| 399 |
VideoDrawText(x + 5, y, SmallFont, sightRange.str()); |
| 400 |
} |
| 401 |
y += 15; |
| 402 |
|
| 403 |
SetDefaultTextColors(nc, rc); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
** Draw button panel. |
| 408 |
** |
| 409 |
** Draw all action buttons. |
| 410 |
*/ |
| 411 |
void CButtonPanel::Draw(void) |
| 412 |
{ |
| 413 |
CPlayer *player; |
| 414 |
const ButtonAction *buttons; |
| 415 |
char buf[8]; |
| 416 |
|
| 417 |
// |
| 418 |
// Draw background |
| 419 |
// |
| 420 |
if (UI.ButtonPanel.G) { |
| 421 |
UI.ButtonPanel.G->DrawClip(UI.ButtonPanel.X, UI.ButtonPanel.Y); |
| 422 |
} |
| 423 |
|
| 424 |
// No buttons |
| 425 |
if (!(buttons = CurrentButtons)) { |
| 426 |
return; |
| 427 |
} |
| 428 |
|
| 429 |
Assert(NumSelected > 0); |
| 430 |
player = Selected[0]->Player; |
| 431 |
|
| 432 |
// |
| 433 |
// Draw all buttons. |
| 434 |
// |
| 435 |
for (int i = 0; i < (int)UI.ButtonPanel.Buttons.size(); ++i) { |
| 436 |
if (buttons[i].Pos == -1) { |
| 437 |
continue; |
| 438 |
} |
| 439 |
Assert(buttons[i].Pos == i + 1); |
| 440 |
|
| 441 |
// |
| 442 |
// Tutorial show command key in icons |
| 443 |
// |
| 444 |
if (ShowCommandKey) { |
| 445 |
if (CurrentButtons[i].Key == gcn::Key::ESCAPE) { |
| 446 |
strcpy_s(buf, sizeof(buf), "ESC"); |
| 447 |
} else { |
| 448 |
buf[0] = toupper(CurrentButtons[i].Key); |
| 449 |
buf[1] = '\0'; |
| 450 |
} |
| 451 |
} else { |
| 452 |
buf[0] = '\0'; |
| 453 |
} |
| 454 |
|
| 455 |
// |
| 456 |
// Draw main Icon. |
| 457 |
// |
| 458 |
buttons[i].Icon.Icon->DrawUnitIcon(player, UI.ButtonPanel.Buttons[i].Style, |
| 459 |
GetButtonStatus(&buttons[i]), |
| 460 |
UI.ButtonPanel.Buttons[i].X, UI.ButtonPanel.Buttons[i].Y, buf); |
| 461 |
|
| 462 |
// |
| 463 |
// Update status line for this button |
| 464 |
// |
| 465 |
if (ButtonAreaUnderCursor == ButtonAreaButton && |
| 466 |
ButtonUnderCursor == i && KeyState != KeyStateInput) { |
| 467 |
DrawPopup(); |
| 468 |
UpdateStatusLineForButton(&buttons[i]); |
| 469 |
} |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
/** |
| 474 |
** Update the status line with hints from the button |
| 475 |
** |
| 476 |
** @param button Button |
| 477 |
*/ |
| 478 |
void UpdateStatusLineForButton(const ButtonAction *button) |
| 479 |
{ |
| 480 |
Assert(button); |
| 481 |
UI.StatusLine.Set(button->Hint); |
| 482 |
|
| 483 |
switch (button->Action) { |
| 484 |
case ButtonBuild: |
| 485 |
case ButtonTrain: |
| 486 |
SetCosts(0, UnitTypes[button->Value]->ProductionCosts); |
| 487 |
break; |
| 488 |
case ButtonSpellCast: |
| 489 |
SetCosts(SpellTypeTable[button->Value]->ChargeCost, NULL); |
| 490 |
break; |
| 491 |
default: |
| 492 |
ClearCosts(); |
| 493 |
break; |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
/*---------------------------------------------------------------------------- |
| 498 |
-- Functions |
| 499 |
----------------------------------------------------------------------------*/ |
| 500 |
|
| 501 |
/** |
| 502 |
** Check if the button is allowed for the unit. |
| 503 |
** |
| 504 |
** @param unit unit which checks for allow. |
| 505 |
** @param buttonaction button to check if it is allowed. |
| 506 |
** |
| 507 |
** @return 1 if button is allowed, 0 else. |
| 508 |
** |
| 509 |
** @todo FIXME: better check. (dependancy, resource, ...) |
| 510 |
*/ |
| 511 |
bool IsButtonAllowed(const CUnit *unit, const ButtonAction *buttonaction) |
| 512 |
{ |
| 513 |
bool res; |
| 514 |
|
| 515 |
Assert(unit); |
| 516 |
Assert(buttonaction); |
| 517 |
|
| 518 |
if (buttonaction->Allowed) { |
| 519 |
return buttonaction->Allowed(unit, buttonaction); |
| 520 |
} |
| 521 |
|
| 522 |
res = false; |
| 523 |
// FIXME: we have to check and if these unit buttons are available |
| 524 |
// i.e. if button action is ButtonTrain for example check if |
| 525 |
// required unit is not restricted etc... |
| 526 |
switch (buttonaction->Action) { |
| 527 |
case ButtonStop: |
| 528 |
case ButtonStandGround: |
| 529 |
case ButtonButton: |
| 530 |
case ButtonMove: |
| 531 |
case ButtonHarvest: |
| 532 |
res = true; |
| 533 |
break; |
| 534 |
case ButtonRepair: |
| 535 |
res = unit->Type->RepairRange > 0; |
| 536 |
break; |
| 537 |
case ButtonPatrol: |
| 538 |
res = CanMove(unit); |
| 539 |
break; |
| 540 |
case ButtonAttack: |
| 541 |
res = ButtonCheckAttack(unit, buttonaction); |
| 542 |
break; |
| 543 |
case ButtonAttackGround: |
| 544 |
if (unit->Type->GroundAttack) { |
| 545 |
res = true; |
| 546 |
} |
| 547 |
break; |
| 548 |
case ButtonTrain: |
| 549 |
res = UnitIdAllowed(unit->Player, buttonaction->Value) != 0 |
| 550 |
&& TerrainAllowsTraining(unit, UnitTypes[buttonaction->Value]); |
| 551 |
break; |
| 552 |
case ButtonBuild: |
| 553 |
res = UnitIdAllowed(unit->Player, buttonaction->Value) != 0; |
| 554 |
break; |
| 555 |
case ButtonSpellCast: |
| 556 |
res = SpellIsAvailable(unit->Player, buttonaction->Value); |
| 557 |
break; |
| 558 |
case ButtonUnload: |
| 559 |
res = (Selected[0]->Type->CanTransport && Selected[0]->BoardCount); |
| 560 |
break; |
| 561 |
case ButtonCancel: |
| 562 |
res = true; |
| 563 |
break; |
| 564 |
case ButtonCancelTrain: |
| 565 |
res = unit->Orders[0]->Action == UnitActionTrain; |
| 566 |
break; |
| 567 |
case ButtonCancelBuild: |
| 568 |
res = unit->Orders[0]->Action == UnitActionBuilt; |
| 569 |
break; |
| 570 |
} |
| 571 |
#if 0 |
| 572 |
// there is a additional check function -- call it |
| 573 |
if (res && buttonaction->Disabled) { |
| 574 |
return buttonaction->Disabled(unit, buttonaction); |
| 575 |
} |
| 576 |
#endif |
| 577 |
return res; |
| 578 |
} |
| 579 |
|
| 580 |
/** |
| 581 |
** Update bottom panel for multiple units. |
| 582 |
** |
| 583 |
** @return array of UI.ButtonPanel.NumButtons buttons to show. |
| 584 |
** |
| 585 |
** @todo FIXME : make UpdateButtonPanelMultipleUnits more configurable. |
| 586 |
** @todo show all possible buttons or just same button... |
| 587 |
*/ |
| 588 |
static ButtonAction *UpdateButtonPanelMultipleUnits(void) |
| 589 |
{ |
| 590 |
std::string unit_ident; |
| 591 |
int z; |
| 592 |
ButtonAction *res; |
| 593 |
bool allow; // button is available for at least 1 unit. |
| 594 |
|
| 595 |
res = new ButtonAction[UI.ButtonPanel.Buttons.size()]; |
| 596 |
for (z = 0; z < (int)UI.ButtonPanel.Buttons.size(); ++z) { |
| 597 |
res[z].Pos = -1; |
| 598 |
} |
| 599 |
|
| 600 |
// FIXME: hardcoded race name |
| 601 |
unit_ident = ",elites-group,"; |
| 602 |
|
| 603 |
for (z = 0; z < (int)UnitButtonTable.size(); ++z) { |
| 604 |
if (UnitButtonTable[z]->Level != CurrentButtonLevel) { |
| 605 |
continue; |
| 606 |
} |
| 607 |
|
| 608 |
// any unit or unit in list |
| 609 |
if (UnitButtonTable[z]->UnitMask[0] != '*' && |
| 610 |
UnitButtonTable[z]->UnitMask.find(unit_ident) == std::string::npos) { |
| 611 |
continue; |
| 612 |
} |
| 613 |
allow = true; |
| 614 |
for (int i = 0; i < NumSelected; i++) { |
| 615 |
if (!IsButtonAllowed(Selected[i], UnitButtonTable[z])) { |
| 616 |
allow = false; |
| 617 |
break; |
| 618 |
} |
| 619 |
} |
| 620 |
Assert(1 <= UnitButtonTable[z]->Pos); |
| 621 |
Assert(UnitButtonTable[z]->Pos <= (int)UI.ButtonPanel.Buttons.size()); |
| 622 |
|
| 623 |
// is button allowed after all? |
| 624 |
if (allow) { |
| 625 |
// OverWrite, So take last valid button. |
| 626 |
res[UnitButtonTable[z]->Pos - 1] = *UnitButtonTable[z]; |
| 627 |
} |
| 628 |
} |
| 629 |
return res; |
| 630 |
} |
| 631 |
|
| 632 |
/** |
| 633 |
** Update bottom panel for single unit. |
| 634 |
** or unit group with the same type. |
| 635 |
** |
| 636 |
** @param unit unit which has actions shown with buttons. |
| 637 |
** |
| 638 |
** @return array of UI.ButtonPanel.NumButtons buttons to show. |
| 639 |
** |
| 640 |
** @todo FIXME : Remove Hack for cancel button. |
| 641 |
*/ |
| 642 |
static ButtonAction *UpdateButtonPanelSingleUnit(const CUnit *unit) |
| 643 |
{ |
| 644 |
int allow; |
| 645 |
std::string unit_ident; |
| 646 |
ButtonAction *buttonaction; |
| 647 |
ButtonAction *res; |
| 648 |
int z; |
| 649 |
|
| 650 |
Assert(unit); |
| 651 |
|
| 652 |
res = new ButtonAction[UI.ButtonPanel.Buttons.size()]; |
| 653 |
for (z = 0; z < (int)UI.ButtonPanel.Buttons.size(); ++z) { |
| 654 |
res[z].Pos = -1; |
| 655 |
} |
| 656 |
|
| 657 |
// |
| 658 |
// FIXME: johns: some hacks for cancel buttons |
| 659 |
// |
| 660 |
if (unit->Orders[0]->Action == UnitActionBuilt) { |
| 661 |
// Trick 17 to get the cancel-build button |
| 662 |
unit_ident = ",cancel-build,"; |
| 663 |
} else { |
| 664 |
unit_ident = "," + unit->Type->Ident + ","; |
| 665 |
} |
| 666 |
|
| 667 |
for (z = 0; z < (int)UnitButtonTable.size(); ++z) { |
| 668 |
int pos; // keep position, modified if alt-buttons required |
| 669 |
|
| 670 |
buttonaction = UnitButtonTable[z]; |
| 671 |
Assert(0 < buttonaction->Pos && buttonaction->Pos <= (int)UI.ButtonPanel.Buttons.size()); |
| 672 |
|
| 673 |
// Same level |
| 674 |
if (buttonaction->Level != CurrentButtonLevel) { |
| 675 |
continue; |
| 676 |
} |
| 677 |
|
| 678 |
// any unit or unit in list |
| 679 |
if (buttonaction->UnitMask[0] != '*' && |
| 680 |
buttonaction->UnitMask.find(unit_ident) == std::string::npos) { |
| 681 |
continue; |
| 682 |
} |
| 683 |
allow = IsButtonAllowed(unit, buttonaction); |
| 684 |
|
| 685 |
pos = buttonaction->Pos; |
| 686 |
|
| 687 |
// is button allowed after all? |
| 688 |
if (allow) { |
| 689 |
// OverWrite, So take last valid button. |
| 690 |
res[pos - 1] = *buttonaction; |
| 691 |
} |
| 692 |
} |
| 693 |
return res; |
| 694 |
} |
| 695 |
|
| 696 |
/** |
| 697 |
** Update button panel. |
| 698 |
** |
| 699 |
** @internal Affect CurrentButtons with buttons to show. |
| 700 |
*/ |
| 701 |
void CButtonPanel::Update(void) |
| 702 |
{ |
| 703 |
CUnit *unit; |
| 704 |
bool sameType; |
| 705 |
|
| 706 |
// Default is no button. |
| 707 |
delete[] CurrentButtons; |
| 708 |
CurrentButtons = NULL; |
| 709 |
|
| 710 |
if (!NumSelected) { |
| 711 |
return; |
| 712 |
} |
| 713 |
|
| 714 |
unit = Selected[0]; |
| 715 |
// foreign unit |
| 716 |
if (unit->Player != ThisPlayer && !ThisPlayer->IsTeamed(unit)) { |
| 717 |
return; |
| 718 |
} |
| 719 |
|
| 720 |
sameType = true; |
| 721 |
// multiple selected |
| 722 |
for (int i = 1; i < NumSelected; ++i) { |
| 723 |
if (Selected[i]->Type != unit->Type) { |
| 724 |
sameType = false; |
| 725 |
break; |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
// We have selected different units types |
| 730 |
if (!sameType) { |
| 731 |
CurrentButtons = UpdateButtonPanelMultipleUnits(); |
| 732 |
} else { |
| 733 |
// We have same type units selected |
| 734 |
// -- continue with setting buttons as for the first unit |
| 735 |
CurrentButtons = UpdateButtonPanelSingleUnit(unit); |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
/** |
| 740 |
** Handle bottom button clicked. |
| 741 |
** |
| 742 |
** @param button Button that was clicked. |
| 743 |
*/ |
| 744 |
void CButtonPanel::DoClicked(int button) |
| 745 |
{ |
| 746 |
int i; |
| 747 |
CUnitType *type; |
| 748 |
|
| 749 |
Assert(0 <= button && button < (int)UI.ButtonPanel.Buttons.size()); |
| 750 |
// no buttons |
| 751 |
if (!CurrentButtons) { |
| 752 |
return; |
| 753 |
} |
| 754 |
// |
| 755 |
// Button not available. |
| 756 |
// or Not Teamed |
| 757 |
// |
| 758 |
if (CurrentButtons[button].Pos == -1 || |
| 759 |
!ThisPlayer->IsTeamed(Selected[0])) { |
| 760 |
return; |
| 761 |
} |
| 762 |
|
| 763 |
PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); |
| 764 |
|
| 765 |
// |
| 766 |
// Handle action on button. |
| 767 |
// |
| 768 |
switch (CurrentButtons[button].Action) { |
| 769 |
case ButtonUnload: |
| 770 |
// |
| 771 |
// Unload on coast, transporter standing, unload all units right now. |
| 772 |
// That or a bunker. |
| 773 |
// |
| 774 |
if ((NumSelected == 1 && Selected[0]->Orders[0]->Action == UnitActionStill && |
| 775 |
Map.CoastOnMap(Selected[0]->X, Selected[0]->Y)) || !CanMove(Selected[0])) { |
| 776 |
SendCommandUnload(Selected[0], |
| 777 |
Selected[0]->X, Selected[0]->Y, NoUnitP, |
| 778 |
!(KeyModifiers & ModifierShift)); |
| 779 |
break; |
| 780 |
} |
| 781 |
CursorState = CursorStateSelect; |
| 782 |
GameCursor = UI.YellowHair.Cursor; |
| 783 |
CursorAction = CurrentButtons[button].Action; |
| 784 |
CursorValue = CurrentButtons[button].Value; |
| 785 |
CurrentButtonLevel = 9; // level 9 is cancel-only |
| 786 |
UI.ButtonPanel.Update(); |
| 787 |
UI.StatusLine.Set(_("Select Target")); |
| 788 |
break; |
| 789 |
case ButtonSpellCast: |
| 790 |
if (KeyModifiers & ModifierControl) { |
| 791 |
int autocast; |
| 792 |
int spellId; |
| 793 |
|
| 794 |
spellId = CurrentButtons[button].Value; |
| 795 |
if (!SpellTypeTable[spellId]->AutoCast) { |
| 796 |
PlayGameSound(GameSounds.PlacementError.Sound, |
| 797 |
MaxSampleVolume); |
| 798 |
break; |
| 799 |
} |
| 800 |
|
| 801 |
autocast = 0; |
| 802 |
// If any selected unit doesn't have autocast on turn it on |
| 803 |
// for everyone |
| 804 |
for (i = 0; i < NumSelected; ++i) { |
| 805 |
if (Selected[i]->AutoCastSpell[spellId] == 0) { |
| 806 |
autocast = 1; |
| 807 |
break; |
| 808 |
} |
| 809 |
} |
| 810 |
for (i = 0; i < NumSelected; ++i) { |
| 811 |
if (Selected[i]->AutoCastSpell[spellId] != autocast) { |
| 812 |
SendCommandAutoSpellCast(Selected[i], |
| 813 |
spellId, autocast); |
| 814 |
} |
| 815 |
} |
| 816 |
break; |
| 817 |
} |
| 818 |
// Follow Next -> Select target. |
| 819 |
case ButtonRepair: |
| 820 |
if (KeyModifiers & ModifierControl) { |
| 821 |
unsigned autorepair; |
| 822 |
|
| 823 |
autorepair = 0; |
| 824 |
// If any selected unit doesn't have autocast on turn it on |
| 825 |
// for everyone |
| 826 |
for (i = 0; i < NumSelected; ++i) { |
| 827 |
if (Selected[i]->AutoRepair == 0) { |
| 828 |
autorepair = 1; |
| 829 |
break; |
| 830 |
} |
| 831 |
} |
| 832 |
for (i = 0; i < NumSelected; ++i) { |
| 833 |
if (Selected[i]->AutoRepair != autorepair) { |
| 834 |
SendCommandAutoRepair(Selected[i], autorepair); |
| 835 |
} |
| 836 |
} |
| 837 |
break; |
| 838 |
} |
| 839 |
// Follow Next -> Select target. |
| 840 |
case ButtonMove: |
| 841 |
case ButtonPatrol: |
| 842 |
case ButtonHarvest: |
| 843 |
case ButtonAttack: |
| 844 |
case ButtonAttackGround: |
| 845 |
// Select target. |
| 846 |
CursorState = CursorStateSelect; |
| 847 |
GameCursor = UI.YellowHair.Cursor; |
| 848 |
CursorAction = CurrentButtons[button].Action; |
| 849 |
CursorValue = CurrentButtons[button].Value; |
| 850 |
CurrentButtonLevel = 9; // level 9 is cancel-only |
| 851 |
UI.ButtonPanel.Update(); |
| 852 |
UI.StatusLine.Set(_("Select Target")); |
| 853 |
break; |
| 854 |
case ButtonStop: |
| 855 |
for (i = 0; i < NumSelected; ++i) { |
| 856 |
SendCommandStopUnit(Selected[i]); |
| 857 |
} |
| 858 |
break; |
| 859 |
case ButtonStandGround: |
| 860 |
for (i = 0; i < NumSelected; ++i) { |
| 861 |
SendCommandStandGround(Selected[i], |
| 862 |
!(KeyModifiers & ModifierShift)); |
| 863 |
} |
| 864 |
break; |
| 865 |
case ButtonButton: |
| 866 |
CurrentButtonLevel = CurrentButtons[button].Value; |
| 867 |
UI.ButtonPanel.Update(); |
| 868 |
break; |
| 869 |
|
| 870 |
case ButtonCancel: |
| 871 |
UI.StatusLine.Clear(); |
| 872 |
ClearCosts(); |
| 873 |
CurrentButtonLevel = 0; |
| 874 |
UI.ButtonPanel.Update(); |
| 875 |
GameCursor = UI.Point.Cursor; |
| 876 |
CursorBuilding = NULL; |
| 877 |
CursorState = CursorStatePoint; |
| 878 |
break; |
| 879 |
|
| 880 |
case ButtonCancelTrain: |
| 881 |
Assert(Selected[0]->Orders[0]->Action == UnitActionTrain); |
| 882 |
SendCommandCancelTraining(Selected[0], -1, NULL); |
| 883 |
UI.StatusLine.Clear(); |
| 884 |
ClearCosts(); |
| 885 |
break; |
| 886 |
|
| 887 |
case ButtonCancelBuild: |
| 888 |
// FIXME: johns is this not sure, only building should have this? |
| 889 |
Assert(Selected[0]->Orders[0]->Action == UnitActionBuilt); |
| 890 |
if (NumSelected == 1) { |
| 891 |
SendCommandDismiss(Selected[0]); |
| 892 |
} |
| 893 |
UI.StatusLine.Clear(); |
| 894 |
ClearCosts(); |
| 895 |
break; |
| 896 |
|
| 897 |
case ButtonBuild: |
| 898 |
// FIXME: store pointer in button table! |
| 899 |
type = UnitTypes[CurrentButtons[button].Value]; |
| 900 |
UI.StatusLine.Set(_("Select Location")); |
| 901 |
ClearCosts(); |
| 902 |
CursorBuilding = type; |
| 903 |
// FIXME: check is this =9 necessary? |
| 904 |
CurrentButtonLevel = 9; // level 9 is cancel-only |
| 905 |
UI.ButtonPanel.Update(); |
| 906 |
break; |
| 907 |
|
| 908 |
case ButtonTrain: |
| 909 |
// FIXME: store pointer in button table! |
| 910 |
type = UnitTypes[CurrentButtons[button].Value]; |
| 911 |
// FIXME: Johns: I want to place commands in queue, even if not |
| 912 |
// FIXME: enough resources are available. |
| 913 |
// FIXME: training queue full check is not correct for network. |
| 914 |
if (Selected[0]->Player->CheckLimits(type) >= 0) { |
| 915 |
SendCommandTrainUnit(Selected[0], type, |
| 916 |
!(KeyModifiers & ModifierShift)); |
| 917 |
UI.StatusLine.Clear(); |
| 918 |
ClearCosts(); |
| 919 |
} |
| 920 |
break; |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
|
| 925 |
/** |
| 926 |
** Lookup key for bottom panel buttons. |
| 927 |
** |
| 928 |
** @param key Internal key symbol for pressed key. |
| 929 |
** |
| 930 |
** @return True, if button is handled (consumed). |
| 931 |
*/ |
| 932 |
int CButtonPanel::DoKey(int key) |
| 933 |
{ |
| 934 |
SDL_keysym keysym; |
| 935 |
memset(&keysym, 0, sizeof(keysym)); |
| 936 |
keysym.sym = (SDLKey)key; |
| 937 |
gcn::Key k = gcn::SDLInput::convertKeyCharacter(keysym); |
| 938 |
key = k.getValue(); |
| 939 |
|
| 940 |
if (CurrentButtons) { |
| 941 |
// This is required for action queues SHIFT+M should be `m' |
| 942 |
if (isascii(key) && isupper(key)) { |
| 943 |
key = tolower(key); |
| 944 |
} |
| 945 |
|
| 946 |
for (int i = 0; i < (int)UI.ButtonPanel.Buttons.size(); ++i) { |
| 947 |
if (CurrentButtons[i].Pos != -1 && key == CurrentButtons[i].Key) { |
| 948 |
UI.ButtonPanel.DoClicked(i); |
| 949 |
return 1; |
| 950 |
} |
| 951 |
} |
| 952 |
} |
| 953 |
return 0; |
| 954 |
} |
| 955 |
|
| 956 |
//@} |