| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name mouse.cpp - The mouse handling. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2009 by Lutz Sammer and Jimmy Salmon |
| 13 |
// |
| 14 |
// This program is free software; you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation; only version 2 of the License. |
| 17 |
// |
| 18 |
// This program is distributed in the hope that it will be useful, |
| 19 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
// GNU General Public License for more details. |
| 22 |
// |
| 23 |
// You should have received a copy of the GNU General Public License |
| 24 |
// along with this program; if not, write to the Free Software |
| 25 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 26 |
// 02111-1307, USA. |
| 27 |
// |
| 28 |
|
| 29 |
//@{ |
| 30 |
|
| 31 |
#define ICON_SIZE_X (UI.ButtonPanel.Buttons[0].Style->Width) |
| 32 |
#define ICON_SIZE_Y (UI.ButtonPanel.Buttons[0].Style->Height) |
| 33 |
|
| 34 |
/*---------------------------------------------------------------------------- |
| 35 |
-- Includes |
| 36 |
----------------------------------------------------------------------------*/ |
| 37 |
|
| 38 |
#include <stdio.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include <ctype.h> |
| 41 |
|
| 42 |
#include "stratagus.h" |
| 43 |
#include "video.h" |
| 44 |
#include "map.h" |
| 45 |
#include "sound.h" |
| 46 |
#include "unitsound.h" |
| 47 |
#include "unittype.h" |
| 48 |
#include "player.h" |
| 49 |
#include "unit.h" |
| 50 |
#include "missile.h" |
| 51 |
#include "commands.h" |
| 52 |
#include "minimap.h" |
| 53 |
#include "font.h" |
| 54 |
#include "cursor.h" |
| 55 |
#include "interface.h" |
| 56 |
#include "menus.h" |
| 57 |
#include "sound.h" |
| 58 |
#include "ui.h" |
| 59 |
#include "network.h" |
| 60 |
#include "spells.h" |
| 61 |
#include "widgets.h" |
| 62 |
|
| 63 |
/*---------------------------------------------------------------------------- |
| 64 |
-- Variables |
| 65 |
----------------------------------------------------------------------------*/ |
| 66 |
|
| 67 |
int MouseButtons; /// Current pressed mouse buttons |
| 68 |
|
| 69 |
int KeyModifiers; /// Current keyboard modifiers |
| 70 |
|
| 71 |
CUnit *UnitUnderCursor; /// Unit under cursor |
| 72 |
int ButtonAreaUnderCursor = -1; /// Button area under cursor |
| 73 |
int ButtonUnderCursor = -1; /// Button under cursor |
| 74 |
bool GameMenuButtonClicked; /// Menu button was clicked |
| 75 |
bool GameDiplomacyButtonClicked; /// Diplomacy button was clicked |
| 76 |
bool LeaveStops; /// Mouse leaves windows stops scroll |
| 77 |
|
| 78 |
enum _cursor_on_ CursorOn = CursorOnUnknown; /// Cursor on field |
| 79 |
|
| 80 |
/*---------------------------------------------------------------------------- |
| 81 |
-- Functions |
| 82 |
----------------------------------------------------------------------------*/ |
| 83 |
static void HandlePieMenuMouseSelection(void); |
| 84 |
|
| 85 |
/** |
| 86 |
** Cancel building cursor mode. |
| 87 |
*/ |
| 88 |
void CancelBuildingMode(void) |
| 89 |
{ |
| 90 |
CursorBuilding = NULL; |
| 91 |
UI.StatusLine.Clear(); |
| 92 |
ClearCosts(); |
| 93 |
CurrentButtonLevel = 0; |
| 94 |
UI.ButtonPanel.Update(); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
** Check whether ButtonAction::UnitMask includes the specified unit |
| 99 |
** type. If not, the user cannot click that button to give an order |
| 100 |
** to units of that type. |
| 101 |
** |
| 102 |
** Even if this function returns true, the button may still be |
| 103 |
** disabled for other reasons; call ::IsButtonAllowed to check those. |
| 104 |
*/ |
| 105 |
static bool IsButtonAllowedForUnitType(const CUnitType *type, const ButtonAction *button) |
| 106 |
{ |
| 107 |
return button->UnitMask == "*" |
| 108 |
|| button->UnitMask.find(',' + type->Ident + ',') != std::string::npos; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
** Find the unit type that @a builder could build on top of @a base. |
| 113 |
** |
| 114 |
** @param builder The unit who would like to build something. |
| 115 |
** @param base The building would go on top of this unit. |
| 116 |
** |
| 117 |
** If nothing can be built there, or is not clear which type of unit |
| 118 |
** should be built, this function returns NULL. |
| 119 |
*/ |
| 120 |
static CUnitType *CanBuildSomethingOnTop(const CUnit *builder, const CUnit *base) |
| 121 |
{ |
| 122 |
// This check should in principle be in DoRightButton, |
| 123 |
// but doing it here is easier. |
| 124 |
if (base == NULL) |
| 125 |
{ |
| 126 |
return NULL; |
| 127 |
} |
| 128 |
|
| 129 |
CUnitType *foundType = NULL; |
| 130 |
for (std::vector<ButtonAction *>::const_iterator |
| 131 |
iterator = UnitButtonTable.begin(); |
| 132 |
iterator != UnitButtonTable.end(); ++iterator) { |
| 133 |
const ButtonAction *const button = *iterator; |
| 134 |
if (button->Action == ButtonBuild |
| 135 |
&& IsButtonAllowedForUnitType(builder->Type, button) |
| 136 |
&& IsButtonAllowed(builder, button)) |
| 137 |
{ |
| 138 |
// Which type of unit the player could build |
| 139 |
// by clicking this button. |
| 140 |
CUnitType *const buildingType = UnitTypes.at(button->Value); |
| 141 |
if (CanBuildHere(builder, buildingType, base->X, base->Y) == base) |
| 142 |
{ |
| 143 |
// buildingType can be built on top of base. |
| 144 |
// But is it unambiguous? |
| 145 |
if (foundType != NULL && buildingType != foundType) |
| 146 |
{ |
| 147 |
// Oops, we already found a different |
| 148 |
// type that could be built here. |
| 149 |
// Better return neither. |
| 150 |
return NULL; |
| 151 |
} |
| 152 |
|
| 153 |
foundType = buildingType; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return foundType; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
** Called when right button is pressed |
| 163 |
** |
| 164 |
** @param sx X map position in pixels. |
| 165 |
** @param sy Y map position in pixels. |
| 166 |
*/ |
| 167 |
void DoRightButton(int sx, int sy) |
| 168 |
{ |
| 169 |
int i; |
| 170 |
int x; // coordinate in tile. |
| 171 |
int y; // coordinate in tile. |
| 172 |
CUnit *dest; // unit under the cursor if any. |
| 173 |
CUnit *unit; // one of the selected unit. |
| 174 |
CUnitType *type; |
| 175 |
int action; // default action for unit. |
| 176 |
int acknowledged; // to play sound |
| 177 |
int flush; // append command to old command. |
| 178 |
unsigned int spellnum; // spell id for spell cast |
| 179 |
|
| 180 |
// No unit selected |
| 181 |
if (!NumSelected) { |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
x = sx / TileSizeX; |
| 186 |
y = sy / TileSizeY; |
| 187 |
|
| 188 |
// |
| 189 |
// Right mouse with SHIFT appends command to old commands. |
| 190 |
// |
| 191 |
flush = !(KeyModifiers & ModifierShift); |
| 192 |
|
| 193 |
if (UnitUnderCursor && !UnitUnderCursor->Type->Decoration) { |
| 194 |
dest = UnitUnderCursor; |
| 195 |
} else { |
| 196 |
dest = NULL; |
| 197 |
} |
| 198 |
|
| 199 |
// |
| 200 |
// Unit selected isn't owned by the player. |
| 201 |
// You can't select your own units + foreign unit(s) |
| 202 |
// except if it is neutral and it is a resource. |
| 203 |
// |
| 204 |
if (!CanSelectMultipleUnits(Selected[0]->Player)) { |
| 205 |
unit = Selected[0]; |
| 206 |
if (unit->Player->Index != PlayerNumNeutral || dest == NULL || |
| 207 |
!(dest->Player == ThisPlayer || dest->IsTeamed(ThisPlayer))) { |
| 208 |
return; |
| 209 |
} |
| 210 |
// tell to go and harvest from a unit |
| 211 |
if (dest->Type->Harvester && unit->Type->CanHarvestFrom) { |
| 212 |
unit->Blink = 4; |
| 213 |
SendCommandResource(dest, unit, flush); |
| 214 |
return; |
| 215 |
} |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
if (dest && dest->Type->CanTransport) { |
| 220 |
for (i = 0; i < NumSelected; i++) { |
| 221 |
if (CanTransport(dest, Selected[i])) { |
| 222 |
// We are clicking on a transporter. We have to: |
| 223 |
// 1) Flush the transporters orders. |
| 224 |
// 2) Tell the transporter to follow the units. We have to queue all |
| 225 |
// these follow orders, so the transport will go and pick them up |
| 226 |
// 3) Tell all selected land units to go board the transporter. |
| 227 |
|
| 228 |
// Here we flush the order queue |
| 229 |
SendCommandStopUnit(dest); |
| 230 |
break; |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
acknowledged = 0; |
| 236 |
for (i = 0; i < NumSelected; ++i) { |
| 237 |
unit = Selected[i]; |
| 238 |
// don't self targetting. |
| 239 |
if (unit == dest) { |
| 240 |
continue; |
| 241 |
} |
| 242 |
Assert(unit); |
| 243 |
if (!acknowledged) { |
| 244 |
PlayUnitSound(unit, VoiceAcknowledging); |
| 245 |
acknowledged = 1; |
| 246 |
} |
| 247 |
type = unit->Type; |
| 248 |
action = type->MouseAction; |
| 249 |
|
| 250 |
// |
| 251 |
// Control + right click on unit is follow anything. |
| 252 |
// |
| 253 |
if ((KeyModifiers & ModifierControl) && dest) { |
| 254 |
dest->Blink = 4; |
| 255 |
SendCommandFollow(unit, dest, flush); |
| 256 |
continue; |
| 257 |
} |
| 258 |
|
| 259 |
// |
| 260 |
// Enter transporters ? |
| 261 |
// |
| 262 |
if (dest) { |
| 263 |
// dest is the transporter |
| 264 |
if (dest->Type->CanTransport) { |
| 265 |
// Let the transporter move to the unit. And QUEUE!!! |
| 266 |
if (CanMove(dest) && CanTransport(dest, unit)) { |
| 267 |
DebugPrint("Send command follow\n"); |
| 268 |
// is flush value correct ? |
| 269 |
SendCommandFollow(dest, unit, 0); |
| 270 |
} |
| 271 |
// FIXME : manage correctly production units. |
| 272 |
if (!CanMove(unit) || CanTransport(dest, unit)) { |
| 273 |
dest->Blink = 4; |
| 274 |
DebugPrint("Board transporter\n"); |
| 275 |
SendCommandBoard(unit, -1, -1, dest, flush); |
| 276 |
continue; |
| 277 |
} |
| 278 |
} |
| 279 |
// unit is the transporter |
| 280 |
// FIXME : Make it more configurable ? NumSelect == 1, lua option |
| 281 |
if (CanTransport(unit, dest)) { |
| 282 |
// Let the transporter move to the unit. And QUEUE!!! |
| 283 |
if (CanMove(unit)) { |
| 284 |
DebugPrint("Send command follow\n"); |
| 285 |
// is flush value correct ? |
| 286 |
SendCommandFollow(unit, dest, 0); |
| 287 |
} else if (!CanMove(dest)) { |
| 288 |
DebugPrint("Want to transport but no unit can move\n"); |
| 289 |
continue; |
| 290 |
} |
| 291 |
dest->Blink = 4; |
| 292 |
DebugPrint("Board transporter\n"); |
| 293 |
SendCommandBoard(dest, -1, -1, unit, flush); |
| 294 |
continue; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
// |
| 299 |
// Handle resource workers. |
| 300 |
// |
| 301 |
if (action == MouseActionHarvest) { |
| 302 |
// Go and repair |
| 303 |
if (type->RepairRange && dest && |
| 304 |
dest->Type->RepairHP && |
| 305 |
dest->Variable[HP_INDEX].Value < dest->Variable[HP_INDEX].Max && |
| 306 |
(dest->Player == unit->Player || unit->IsAllied(dest))) { |
| 307 |
dest->Blink = 4; |
| 308 |
SendCommandRepair(unit, x, y, dest, flush); |
| 309 |
continue; |
| 310 |
} |
| 311 |
// Harvest |
| 312 |
if (type->Harvester) { |
| 313 |
if (dest) { |
| 314 |
// Go and harvest from a unit |
| 315 |
if (dest->Type->CanHarvestFrom && type->Harvester && |
| 316 |
dest->Player->Index == PlayerNumNeutral) { |
| 317 |
dest->Blink = 4; |
| 318 |
SendCommandResource(unit, dest, flush); |
| 319 |
continue; |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
// Follow another unit |
| 324 |
if (UnitUnderCursor && dest && dest != unit && |
| 325 |
(dest->Player == unit->Player || unit->IsAllied(dest))) { |
| 326 |
dest->Blink = 4; |
| 327 |
SendCommandFollow(unit, dest, flush); |
| 328 |
continue; |
| 329 |
} |
| 330 |
// Build on top of the clicked unit |
| 331 |
CUnitType *buildingType = CanBuildSomethingOnTop(unit, dest); |
| 332 |
if (buildingType != NULL) { |
| 333 |
SendCommandBuildBuilding(unit, dest->X, dest->Y, buildingType, flush); |
| 334 |
continue; |
| 335 |
} |
| 336 |
// Move |
| 337 |
SendCommandMove(unit, x, y, flush); |
| 338 |
continue; |
| 339 |
} |
| 340 |
|
| 341 |
// |
| 342 |
// Fighters |
| 343 |
// |
| 344 |
if (action == MouseActionSpellCast || action == MouseActionAttack) { |
| 345 |
if (dest && unit->Orders[0]->Action != UnitActionBuilt) { |
| 346 |
if (unit->IsEnemy(dest)) { |
| 347 |
dest->Blink = 4; |
| 348 |
if (action == MouseActionSpellCast) { |
| 349 |
// This is for demolition squads and such |
| 350 |
Assert(unit->Type->CanCastSpell); |
| 351 |
for (spellnum = 0; !type->CanCastSpell[spellnum] && |
| 352 |
spellnum < SpellTypeTable.size() ; spellnum++) ; |
| 353 |
SendCommandSpellCast(unit, x, y, dest, spellnum, flush); |
| 354 |
} else { |
| 355 |
if (CanTarget(type, dest->Type)) { |
| 356 |
SendCommandAttack(unit, x, y, dest, flush); |
| 357 |
} else { |
| 358 |
// No valid target |
| 359 |
SendCommandAttack(unit, x, y, NoUnitP, flush); |
| 360 |
} |
| 361 |
} |
| 362 |
continue; |
| 363 |
} |
| 364 |
|
| 365 |
if ((dest->Player == unit->Player || unit->IsAllied(dest)) && |
| 366 |
dest != unit) { |
| 367 |
dest->Blink = 4; |
| 368 |
SendCommandFollow(unit, dest, flush); |
| 369 |
continue; |
| 370 |
} |
| 371 |
|
| 372 |
} |
| 373 |
|
| 374 |
// empty space |
| 375 |
if ((KeyModifiers & ModifierControl)) { |
| 376 |
if (RightButtonAttacks) { |
| 377 |
SendCommandMove(unit, x, y, flush); |
| 378 |
} else { |
| 379 |
SendCommandAttack(unit, x, y, NoUnitP, flush); |
| 380 |
} |
| 381 |
} else { |
| 382 |
if (RightButtonAttacks) { |
| 383 |
SendCommandAttack(unit, x, y, NoUnitP, flush); |
| 384 |
} else { |
| 385 |
// Note: move is correct here, right default is move |
| 386 |
SendCommandMove(unit, x, y, flush); |
| 387 |
} |
| 388 |
} |
| 389 |
// FIXME: ALT-RIGHT-CLICK, move but fight back if attacked. |
| 390 |
continue; |
| 391 |
} |
| 392 |
|
| 393 |
// FIXME: attack/follow/board ... |
| 394 |
if ((action == MouseActionMove) && |
| 395 |
(dest && dest != unit) && |
| 396 |
(dest->Player == unit->Player || unit->IsAllied(dest))) { |
| 397 |
dest->Blink = 4; |
| 398 |
SendCommandFollow(unit, dest, flush); |
| 399 |
continue; |
| 400 |
} |
| 401 |
|
| 402 |
// Manage harvester from the destination side. |
| 403 |
if (dest && dest->Type->Harvester) { |
| 404 |
// tell to go and harvest from a building |
| 405 |
if (type->CanHarvestFrom && dest->Type->Harvester && |
| 406 |
dest->Player == unit->Player) { |
| 407 |
unit->Blink = 4; |
| 408 |
SendCommandResource(dest, unit, flush); |
| 409 |
continue; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
// Manage new order. |
| 414 |
if (!CanMove(unit) && dest) { |
| 415 |
// Go and repair |
| 416 |
if (dest->Type->RepairHP && |
| 417 |
(dest->Player == unit->Player || unit->IsAllied(dest))) { |
| 418 |
dest->Blink = 4; |
| 419 |
SendCommandRepair(unit, x, y, dest, flush); |
| 420 |
continue; |
| 421 |
} |
| 422 |
// Go and harvest from a unit |
| 423 |
if (dest->Type->CanHarvestFrom && |
| 424 |
(dest->Player == unit->Player || dest->Player->Index == PlayerNumNeutral)) { |
| 425 |
dest->Blink = 4; |
| 426 |
SendCommandResource(unit, dest, flush); |
| 427 |
continue; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
SendCommandMove(unit, x, y, flush); |
| 432 |
} |
| 433 |
ShowOrdersCount = GameCycle + Preference.ShowOrders * CYCLES_PER_SECOND; |
| 434 |
} |
| 435 |
|
| 436 |
/** |
| 437 |
** Check if the mouse is on a button |
| 438 |
** |
| 439 |
** @param x X coordinate. |
| 440 |
** @param y Y coordinate. |
| 441 |
** @param button Button to check. |
| 442 |
** |
| 443 |
** @return True if mouse is on the button, False otherwise. |
| 444 |
*/ |
| 445 |
static inline bool OnButton(int x, int y, CUIButton *button) |
| 446 |
{ |
| 447 |
return x >= button->X && x < button->X + button->Style->Width && |
| 448 |
y >= button->Y && y < button->Y + button->Style->Height; |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
** Check if the mouse is on a graphic |
| 453 |
** |
| 454 |
** @param x X coordinate. |
| 455 |
** @param y Y coordinate. |
| 456 |
** @param g Graphic. |
| 457 |
** @param gx Graphic X coordinate. |
| 458 |
** @param gy Graphic Y coordinate. |
| 459 |
** |
| 460 |
** @return True if mouse is on the graphic, False otherwise. |
| 461 |
*/ |
| 462 |
static inline bool OnGraphic(int x, int y, CGraphic *g, int gx, int gy) |
| 463 |
{ |
| 464 |
x -= gx; |
| 465 |
y -= gy; |
| 466 |
if (x >= 0 && x < g->Width && y >= 0 && y < g->Height) { |
| 467 |
return !g->TransparentPixel(x, y); |
| 468 |
} |
| 469 |
return false; |
| 470 |
} |
| 471 |
|
| 472 |
/** |
| 473 |
** Set flag on which area is the cursor. |
| 474 |
** |
| 475 |
** @param x X coordinate. |
| 476 |
** @param y Y coordinate. |
| 477 |
*/ |
| 478 |
static void HandleMouseOn(int x, int y) |
| 479 |
{ |
| 480 |
int i; |
| 481 |
bool on_ui; |
| 482 |
|
| 483 |
MouseScrollState = ScrollNone; |
| 484 |
ButtonAreaUnderCursor = -1; |
| 485 |
ButtonUnderCursor = -1; |
| 486 |
|
| 487 |
// BigMapMode is the mode which show only the map (without panel, minimap) |
| 488 |
if (BigMapMode) { |
| 489 |
CursorOn = CursorOnMap; |
| 490 |
// |
| 491 |
// Scrolling Region Handling. |
| 492 |
// |
| 493 |
HandleMouseScrollArea(x, y); |
| 494 |
return; |
| 495 |
} |
| 496 |
// |
| 497 |
// Handle buttons |
| 498 |
// |
| 499 |
if (!IsNetworkGame()) { |
| 500 |
if (UI.MenuButton.X != -1) { |
| 501 |
if (OnButton(x, y, &UI.MenuButton)) { |
| 502 |
ButtonAreaUnderCursor = ButtonAreaMenu; |
| 503 |
ButtonUnderCursor = ButtonUnderMenu; |
| 504 |
CursorOn = CursorOnButton; |
| 505 |
return; |
| 506 |
} |
| 507 |
} |
| 508 |
} else { |
| 509 |
if (UI.NetworkMenuButton.X != -1) { |
| 510 |
if (OnButton(x, y, &UI.NetworkMenuButton)) { |
| 511 |
ButtonAreaUnderCursor = ButtonAreaMenu; |
| 512 |
ButtonUnderCursor = ButtonUnderNetworkMenu; |
| 513 |
CursorOn = CursorOnButton; |
| 514 |
return; |
| 515 |
} |
| 516 |
} |
| 517 |
if (UI.NetworkDiplomacyButton.X != -1) { |
| 518 |
if (OnButton(x, y, &UI.NetworkDiplomacyButton)) { |
| 519 |
ButtonAreaUnderCursor = ButtonAreaMenu; |
| 520 |
ButtonUnderCursor = ButtonUnderNetworkDiplomacy; |
| 521 |
CursorOn = CursorOnButton; |
| 522 |
return; |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
for (i = 0; i < (int)UI.ButtonPanel.Buttons.size(); ++i) { |
| 527 |
if (OnButton(x, y, &UI.ButtonPanel.Buttons[i])) { |
| 528 |
ButtonAreaUnderCursor = ButtonAreaButton; |
| 529 |
if (CurrentButtons && CurrentButtons[i].Pos != -1) { |
| 530 |
ButtonUnderCursor = i; |
| 531 |
CursorOn = CursorOnButton; |
| 532 |
return; |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
if (NumSelected == 1 && Selected[0]->Type->CanTransport && |
| 537 |
Selected[0]->BoardCount) { |
| 538 |
i = Selected[0]->BoardCount < (int)UI.TransportingButtons.size() ? |
| 539 |
Selected[0]->BoardCount - 1 : (int)UI.TransportingButtons.size() - 1; |
| 540 |
for (; i >= 0; --i) { |
| 541 |
if (OnButton(x, y, &UI.TransportingButtons[i])) { |
| 542 |
ButtonAreaUnderCursor = ButtonAreaTransporting; |
| 543 |
ButtonUnderCursor = i; |
| 544 |
CursorOn = CursorOnButton; |
| 545 |
return; |
| 546 |
} |
| 547 |
} |
| 548 |
} |
| 549 |
if (NumSelected == 1) { |
| 550 |
if (Selected[0]->Orders[0]->Action == UnitActionTrain) { |
| 551 |
if (Selected[0]->OrderCount == 1) { |
| 552 |
if (OnButton(x, y, UI.SingleTrainingButton)) { |
| 553 |
ButtonAreaUnderCursor = ButtonAreaTraining; |
| 554 |
ButtonUnderCursor = 0; |
| 555 |
CursorOn = CursorOnButton; |
| 556 |
return; |
| 557 |
} |
| 558 |
} else { |
| 559 |
i = Selected[0]->OrderCount < (int)UI.TrainingButtons.size() ? |
| 560 |
Selected[0]->OrderCount - 1 : UI.TrainingButtons.size() - 1; |
| 561 |
for (; i >= 0; --i) { |
| 562 |
if (Selected[0]->Orders[i]->Action == UnitActionTrain && |
| 563 |
OnButton(x, y, &UI.TrainingButtons[i])) { |
| 564 |
ButtonAreaUnderCursor = ButtonAreaTraining; |
| 565 |
ButtonUnderCursor = i; |
| 566 |
CursorOn = CursorOnButton; |
| 567 |
return; |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
} |
| 572 |
} |
| 573 |
if (NumSelected == 1) { |
| 574 |
if (UI.SingleSelectedButton && OnButton(x, y, UI.SingleSelectedButton)) { |
| 575 |
ButtonAreaUnderCursor = ButtonAreaSelected; |
| 576 |
ButtonUnderCursor = 0; |
| 577 |
CursorOn = CursorOnButton; |
| 578 |
return; |
| 579 |
} |
| 580 |
} else { |
| 581 |
i = NumSelected > (int)UI.SelectedButtons.size() ? |
| 582 |
UI.SelectedButtons.size() - 1 : NumSelected - 1; |
| 583 |
for (; i >= 0; --i) { |
| 584 |
if (OnButton(x, y, &UI.SelectedButtons[i])) { |
| 585 |
ButtonAreaUnderCursor = ButtonAreaSelected; |
| 586 |
ButtonUnderCursor = i; |
| 587 |
CursorOn = CursorOnButton; |
| 588 |
return; |
| 589 |
} |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
// |
| 594 |
// Minimap |
| 595 |
// |
| 596 |
if (x >= UI.Minimap.X && x < UI.Minimap.X + UI.Minimap.W && |
| 597 |
y >= UI.Minimap.Y && y < UI.Minimap.Y + UI.Minimap.H) { |
| 598 |
CursorOn = CursorOnMinimap; |
| 599 |
return; |
| 600 |
} |
| 601 |
|
| 602 |
// |
| 603 |
// On UI graphic |
| 604 |
// |
| 605 |
on_ui = false; |
| 606 |
for (i = 0; i < (int)UI.Fillers.size(); ++i) { |
| 607 |
if (OnGraphic(x, y, UI.Fillers[i].G, UI.Fillers[i].X, UI.Fillers[i].Y)) { |
| 608 |
on_ui = true; |
| 609 |
break; |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
// |
| 614 |
// Map |
| 615 |
// |
| 616 |
if (!on_ui && x >= UI.MapArea.X && x <= UI.MapArea.EndX && |
| 617 |
y >= UI.MapArea.Y && y <= UI.MapArea.EndY) { |
| 618 |
CViewport *vp; |
| 619 |
|
| 620 |
vp = GetViewport(x, y); |
| 621 |
Assert(vp); |
| 622 |
// viewport changed |
| 623 |
if (UI.MouseViewport != vp) { |
| 624 |
UI.MouseViewport = vp; |
| 625 |
DebugPrint("current viewport changed to %ld.\n" _C_ |
| 626 |
static_cast<long int>(vp - UI.Viewports)); |
| 627 |
} |
| 628 |
|
| 629 |
// Note cursor on map can be in scroll area |
| 630 |
CursorOn = CursorOnMap; |
| 631 |
} else { |
| 632 |
CursorOn = CursorOnUnknown; |
| 633 |
} |
| 634 |
|
| 635 |
// |
| 636 |
// Scrolling Region Handling. |
| 637 |
// |
| 638 |
HandleMouseScrollArea(x, y); |
| 639 |
} |
| 640 |
|
| 641 |
/** |
| 642 |
** Handle cursor exits the game window (only for some videomodes) |
| 643 |
** @todo FIXME: make it so that the game is partially 'paused'. |
| 644 |
** Game should run (for network play), but not react on or show |
| 645 |
** interactive events. |
| 646 |
*/ |
| 647 |
void HandleMouseExit(void) |
| 648 |
{ |
| 649 |
// Disabled |
| 650 |
if (!LeaveStops) { |
| 651 |
return; |
| 652 |
} |
| 653 |
|
| 654 |
// Denote cursor not on anything in window (used?) |
| 655 |
CursorOn = CursorOnUnknown; |
| 656 |
|
| 657 |
// Prevent scrolling while out of focus (on other applications) |
| 658 |
KeyScrollState = MouseScrollState = ScrollNone; |
| 659 |
} |
| 660 |
|
| 661 |
/** |
| 662 |
** Restrict mouse cursor to viewport. |
| 663 |
*/ |
| 664 |
void RestrictCursorToViewport(void) |
| 665 |
{ |
| 666 |
if (CursorX < UI.SelectedViewport->X) { |
| 667 |
CursorStartX = UI.SelectedViewport->X; |
| 668 |
} else if (CursorX >= UI.SelectedViewport->EndX) { |
| 669 |
CursorStartX = UI.SelectedViewport->EndX - 1; |
| 670 |
} else { |
| 671 |
CursorStartX = CursorX; |
| 672 |
} |
| 673 |
|
| 674 |
if (CursorY < UI.SelectedViewport->Y) { |
| 675 |
CursorStartY = UI.SelectedViewport->Y; |
| 676 |
} else if (CursorY >= UI.SelectedViewport->EndY) { |
| 677 |
CursorStartY = UI.SelectedViewport->EndY - 1; |
| 678 |
} else { |
| 679 |
CursorStartY = CursorY; |
| 680 |
} |
| 681 |
|
| 682 |
UI.MouseWarpX = CursorX = CursorStartX; |
| 683 |
UI.MouseWarpY = CursorY = CursorStartY; |
| 684 |
CursorOn = CursorOnMap; |
| 685 |
} |
| 686 |
|
| 687 |
/** |
| 688 |
** Restrict mouse cursor to minimap |
| 689 |
*/ |
| 690 |
void RestrictCursorToMinimap(void) |
| 691 |
{ |
| 692 |
if (CursorX < UI.Minimap.X) { |
| 693 |
CursorStartX = UI.Minimap.X; |
| 694 |
} else if (CursorX >= UI.Minimap.X + UI.Minimap.W) { |
| 695 |
CursorStartX = UI.Minimap.X + UI.Minimap.W - 1; |
| 696 |
} else { |
| 697 |
CursorStartX = CursorX; |
| 698 |
} |
| 699 |
|
| 700 |
if (CursorY < UI.Minimap.Y) { |
| 701 |
CursorStartY = UI.Minimap.Y; |
| 702 |
} else if (CursorY >= UI.Minimap.Y + UI.Minimap.H) { |
| 703 |
CursorStartY = UI.Minimap.Y + UI.Minimap.H - 1; |
| 704 |
} else { |
| 705 |
CursorStartY = CursorY; |
| 706 |
} |
| 707 |
|
| 708 |
CursorX = UI.MouseWarpX = CursorStartX; |
| 709 |
CursorY = UI.MouseWarpY = CursorStartY; |
| 710 |
CursorOn = CursorOnMinimap; |
| 711 |
} |
| 712 |
|
| 713 |
/** |
| 714 |
** Use the mouse to scroll the map |
| 715 |
** |
| 716 |
** @param x Screen X position. |
| 717 |
** @param y Screen Y position. |
| 718 |
*/ |
| 719 |
void MouseScrollMap(int x, int y) |
| 720 |
{ |
| 721 |
int speed; |
| 722 |
|
| 723 |
if (KeyModifiers & ModifierControl) { |
| 724 |
speed = UI.MouseScrollSpeedControl; |
| 725 |
} else { |
| 726 |
speed = UI.MouseScrollSpeedDefault; |
| 727 |
} |
| 728 |
|
| 729 |
UI.MouseViewport->Set(UI.MouseViewport->MapX, UI.MouseViewport->MapY, |
| 730 |
UI.MouseViewport->OffsetX + speed * (x - CursorStartX), |
| 731 |
UI.MouseViewport->OffsetY + speed * (y - CursorStartY)); |
| 732 |
UI.MouseWarpX = CursorStartX; |
| 733 |
UI.MouseWarpY = CursorStartY; |
| 734 |
} |
| 735 |
|
| 736 |
/** |
| 737 |
** Handle movement of the cursor. |
| 738 |
** |
| 739 |
** @param x Screen X position. |
| 740 |
** @param y Screen Y position. |
| 741 |
*/ |
| 742 |
void UIHandleMouseMove(int x, int y) |
| 743 |
{ |
| 744 |
int mx; |
| 745 |
int my; |
| 746 |
enum _cursor_on_ OldCursorOn; |
| 747 |
|
| 748 |
OldCursorOn = CursorOn; |
| 749 |
// |
| 750 |
// Selecting units. |
| 751 |
// |
| 752 |
if (CursorState == CursorStateRectangle) { |
| 753 |
// Restrict cursor to viewport. |
| 754 |
if (CursorX < UI.SelectedViewport->X) { |
| 755 |
CursorX = UI.SelectedViewport->X; |
| 756 |
} else if (CursorX >= UI.SelectedViewport->EndX) { |
| 757 |
CursorX = UI.SelectedViewport->EndX - 1; |
| 758 |
} |
| 759 |
if (CursorY < UI.SelectedViewport->Y) { |
| 760 |
CursorY = UI.SelectedViewport->Y; |
| 761 |
} else if (CursorY >= UI.SelectedViewport->EndY) { |
| 762 |
CursorY = UI.SelectedViewport->EndY - 1; |
| 763 |
} |
| 764 |
UI.MouseWarpX = CursorX; |
| 765 |
UI.MouseWarpY = CursorY; |
| 766 |
return; |
| 767 |
} |
| 768 |
|
| 769 |
// |
| 770 |
// Move map. |
| 771 |
// |
| 772 |
if (GameCursor == UI.Scroll.Cursor) { |
| 773 |
MouseScrollMap(x, y); |
| 774 |
return; |
| 775 |
} |
| 776 |
|
| 777 |
UnitUnderCursor = NULL; |
| 778 |
GameCursor = UI.Point.Cursor; // Reset |
| 779 |
HandleMouseOn(x, y); |
| 780 |
|
| 781 |
// |
| 782 |
// Make the piemenu "follow" the mouse |
| 783 |
// |
| 784 |
if (CursorState == CursorStatePieMenu && CursorOn == CursorOnMap) { |
| 785 |
if (CursorX - CursorStartX > UI.PieMenu.X[2]) { |
| 786 |
CursorStartX = CursorX - UI.PieMenu.X[2]; |
| 787 |
} |
| 788 |
if (CursorStartX - CursorX > UI.PieMenu.X[2]) { |
| 789 |
CursorStartX = CursorX + UI.PieMenu.X[2]; |
| 790 |
} |
| 791 |
if (CursorStartY - CursorY > UI.PieMenu.Y[4]) { |
| 792 |
CursorStartY = CursorY + UI.PieMenu.Y[4]; |
| 793 |
} |
| 794 |
if (CursorY - CursorStartY > UI.PieMenu.Y[4]) { |
| 795 |
CursorStartY = CursorY - UI.PieMenu.Y[4]; |
| 796 |
} |
| 797 |
return; |
| 798 |
} |
| 799 |
|
| 800 |
// Restrict mouse to minimap when dragging |
| 801 |
if (OldCursorOn == CursorOnMinimap && CursorOn != CursorOnMinimap && |
| 802 |
(MouseButtons & LeftButton)) { |
| 803 |
RestrictCursorToMinimap(); |
| 804 |
UI.SelectedViewport->Center( |
| 805 |
UI.Minimap.Screen2MapX(CursorX), UI.Minimap.Screen2MapY(CursorY), |
| 806 |
TileSizeX / 2, TileSizeY / 2); |
| 807 |
return; |
| 808 |
} |
| 809 |
|
| 810 |
// |
| 811 |
// User may be draging with button pressed. |
| 812 |
// |
| 813 |
if (GameMenuButtonClicked || GameDiplomacyButtonClicked) { |
| 814 |
return; |
| 815 |
} |
| 816 |
|
| 817 |
// This is forbidden for unexplored and not visible space |
| 818 |
// FIXME: This must done new, moving units, scrolling... |
| 819 |
if (CursorOn == CursorOnMap && UI.MouseViewport->IsInsideMapArea(CursorX, CursorY)) { |
| 820 |
const CViewport *vp = UI.MouseViewport; |
| 821 |
if (Map.IsFieldExplored(ThisPlayer, vp->Viewport2MapX(x), vp->Viewport2MapY(y)) || |
| 822 |
ReplayRevealMap) { |
| 823 |
UnitUnderCursor = UnitOnScreen(x - vp->X + vp->MapX * TileSizeX + vp->OffsetX, |
| 824 |
y - vp->Y + vp->MapY * TileSizeY + vp->OffsetY); |
| 825 |
} |
| 826 |
} else if (CursorOn == CursorOnMinimap) { |
| 827 |
mx = UI.Minimap.Screen2MapX(x); |
| 828 |
my = UI.Minimap.Screen2MapY(y); |
| 829 |
if (Map.IsFieldExplored(ThisPlayer, mx, my) || ReplayRevealMap) { |
| 830 |
UnitUnderCursor = UnitOnMapTile(mx, my); |
| 831 |
} |
| 832 |
} |
| 833 |
|
| 834 |
// NOTE: If unit is not selectable as a goal, you can't get a cursor hint |
| 835 |
if (UnitUnderCursor && !UnitUnderCursor->IsVisibleAsGoal(ThisPlayer) && |
| 836 |
!ReplayRevealMap) { |
| 837 |
UnitUnderCursor = NULL; |
| 838 |
} |
| 839 |
|
| 840 |
// |
| 841 |
// Selecting target. |
| 842 |
// |
| 843 |
if (CursorState == CursorStateSelect) { |
| 844 |
if (CursorOn == CursorOnMap || CursorOn == CursorOnMinimap) { |
| 845 |
GameCursor = UI.YellowHair.Cursor; |
| 846 |
if (UnitUnderCursor && !UnitUnderCursor->Type->Decoration) { |
| 847 |
if (UnitUnderCursor->Player == ThisPlayer || |
| 848 |
ThisPlayer->IsAllied(UnitUnderCursor)) { |
| 849 |
GameCursor = UI.GreenHair.Cursor; |
| 850 |
} else if (UnitUnderCursor->Player->Index != PlayerNumNeutral) { |
| 851 |
GameCursor = UI.RedHair.Cursor; |
| 852 |
} |
| 853 |
} |
| 854 |
if (CursorOn == CursorOnMinimap && (MouseButtons & RightButton)) { |
| 855 |
// |
| 856 |
// Minimap move viewpoint |
| 857 |
// |
| 858 |
UI.SelectedViewport->Center( |
| 859 |
UI.Minimap.Screen2MapX(CursorX), |
| 860 |
UI.Minimap.Screen2MapY(CursorY), TileSizeX / 2, TileSizeY / 2); |
| 861 |
} |
| 862 |
} |
| 863 |
// FIXME: must move minimap if right button is down ! |
| 864 |
return; |
| 865 |
} |
| 866 |
|
| 867 |
// |
| 868 |
// Cursor pointing. |
| 869 |
// |
| 870 |
if (CursorOn == CursorOnMap) { |
| 871 |
// |
| 872 |
// Map |
| 873 |
// |
| 874 |
if (UnitUnderCursor && !UnitUnderCursor->Type->Decoration && |
| 875 |
(UnitUnderCursor->IsVisible(ThisPlayer) || ReplayRevealMap)) { |
| 876 |
GameCursor = UI.Glass.Cursor; |
| 877 |
} |
| 878 |
|
| 879 |
return; |
| 880 |
} |
| 881 |
|
| 882 |
if (CursorOn == CursorOnMinimap && (MouseButtons & LeftButton)) { |
| 883 |
// |
| 884 |
// Minimap move viewpoint |
| 885 |
// |
| 886 |
UI.SelectedViewport->Center( |
| 887 |
UI.Minimap.Screen2MapX(CursorX), UI.Minimap.Screen2MapY(CursorY), |
| 888 |
TileSizeX / 2, TileSizeY / 2); |
| 889 |
CursorStartX = CursorX; |
| 890 |
CursorStartY = CursorY; |
| 891 |
return; |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
//............................................................................. |
| 896 |
|
| 897 |
/** |
| 898 |
** Send selected units to repair |
| 899 |
** |
| 900 |
** @param sx X screen map position. |
| 901 |
** @param sy Y screen map position. |
| 902 |
*/ |
| 903 |
static int SendRepair(int sx, int sy) |
| 904 |
{ |
| 905 |
CUnit *dest; |
| 906 |
int ret = 0; |
| 907 |
|
| 908 |
// Check if the dest is repairable! |
| 909 |
if ((dest = UnitUnderCursor) && dest->Variable[HP_INDEX].Value < dest->Variable[HP_INDEX].Max && |
| 910 |
dest->Type->RepairHP && |
| 911 |
(dest->Player == ThisPlayer || ThisPlayer->IsAllied(dest))) { |
| 912 |
for (int i = 0; i < NumSelected; ++i) { |
| 913 |
CUnit *unit = Selected[i]; |
| 914 |
if (unit->Type->RepairRange) { |
| 915 |
SendCommandRepair(unit, sx / TileSizeX, sy / TileSizeY, dest, |
| 916 |
!(KeyModifiers & ModifierShift)); |
| 917 |
ret = 1; |
| 918 |
} else { |
| 919 |
DebugPrint("Non-worker repairs\n"); |
| 920 |
} |
| 921 |
} |
| 922 |
} |
| 923 |
return ret; |
| 924 |
} |
| 925 |
|
| 926 |
/** |
| 927 |
** Send selected units to point. |
| 928 |
** |
| 929 |
** @param sx X screen tile position. |
| 930 |
** @param sy Y screen tile position. |
| 931 |
** |
| 932 |
** @todo To reduce the CPU load for pathfinder, we should check if |
| 933 |
** the destination is reachable and handle nice group movements. |
| 934 |
*/ |
| 935 |
static int SendMove(int sx, int sy) |
| 936 |
{ |
| 937 |
int i; |
| 938 |
int flush; |
| 939 |
CUnit *unit; |
| 940 |
CUnit *transporter; |
| 941 |
int ret; |
| 942 |
|
| 943 |
ret = 0; |
| 944 |
// Move to a transporter. |
| 945 |
if ((transporter = UnitUnderCursor) && transporter->Type->CanTransport) { |
| 946 |
for (i = 0; i < NumSelected; ++i) { |
| 947 |
if (CanTransport(transporter, Selected[i])) { |
| 948 |
SendCommandStopUnit(transporter); |
| 949 |
ret = 1; |
| 950 |
break; |
| 951 |
} |
| 952 |
} |
| 953 |
if (i == NumSelected) { |
| 954 |
transporter = NULL; |
| 955 |
} |
| 956 |
} else { |
| 957 |
transporter = NULL; |
| 958 |
} |
| 959 |
|
| 960 |
flush = !(KeyModifiers & ModifierShift); |
| 961 |
|
| 962 |
for (i = 0; i < NumSelected; ++i) { |
| 963 |
unit = Selected[i]; |
| 964 |
if (transporter && CanTransport(transporter, unit)) { |
| 965 |
transporter->Blink = 4; |
| 966 |
SendCommandFollow(transporter, unit, 0); |
| 967 |
SendCommandBoard(unit, -1, -1, transporter, flush); |
| 968 |
ret = 1; |
| 969 |
} else { |
| 970 |
SendCommandMove(unit, sx / TileSizeX, sy / TileSizeY, flush); |
| 971 |
ret = 1; |
| 972 |
} |
| 973 |
} |
| 974 |
return ret; |
| 975 |
} |
| 976 |
|
| 977 |
/** |
| 978 |
** Send the current selected group attacking. |
| 979 |
** |
| 980 |
** To empty field: |
| 981 |
** Move to this field attacking all enemy units in reaction range. |
| 982 |
** |
| 983 |
** To unit: |
| 984 |
** Move to unit attacking and tracing the unit until dead. |
| 985 |
** |
| 986 |
** @param sx X screen map position. |
| 987 |
** @param sy Y screen map position. |
| 988 |
** |
| 989 |
** @return 1 if any unit have a new order, 0 else. |
| 990 |
** |
| 991 |
** @see Selected, @see NumSelected |
| 992 |
*/ |
| 993 |
static int SendAttack(int sx, int sy) |
| 994 |
{ |
| 995 |
int i; |
| 996 |
CUnit *unit; // selected unit. |
| 997 |
CUnit *dest; // unit under cursor if any. |
| 998 |
int ret; |
| 999 |
|
| 1000 |
ret = 0; |
| 1001 |
dest = UnitUnderCursor; |
| 1002 |
if (dest && dest->Type->Decoration) { |
| 1003 |
dest = NULL; |
| 1004 |
} |
| 1005 |
for (i = 0; i < NumSelected; ++i) { |
| 1006 |
unit = Selected[i]; |
| 1007 |
if (unit->Type->CanAttack) { |
| 1008 |
if (!dest || (dest != unit && CanTarget(unit->Type, dest->Type))) { |
| 1009 |
if (dest) { |
| 1010 |
dest->Blink = 4; |
| 1011 |
} |
| 1012 |
SendCommandAttack(unit, sx / TileSizeX, sy / TileSizeY, dest, |
| 1013 |
!(KeyModifiers & ModifierShift)); |
| 1014 |
ret = 1; |
| 1015 |
} |
| 1016 |
} else { |
| 1017 |
if (CanMove(unit)) { |
| 1018 |
SendCommandMove(unit, sx / TileSizeX, sy / TileSizeY, |
| 1019 |
!(KeyModifiers & ModifierShift)); |
| 1020 |
ret = 1; |
| 1021 |
} |
| 1022 |
} |
| 1023 |
} |
| 1024 |
return ret; |
| 1025 |
} |
| 1026 |
|
| 1027 |
/** |
| 1028 |
** Send the current selected group ground attacking. |
| 1029 |
** |
| 1030 |
** @param sx X screen map position. |
| 1031 |
** @param sy Y screen map position. |
| 1032 |
*/ |
| 1033 |
static int SendAttackGround(int sx, int sy) |
| 1034 |
{ |
| 1035 |
int ret = 0; |
| 1036 |
|
| 1037 |
for (int i = 0; i < NumSelected; ++i) { |
| 1038 |
CUnit *unit = Selected[i]; |
| 1039 |
if (unit->Type->CanAttack) { |
| 1040 |
SendCommandAttackGround(unit, sx / TileSizeX, sy / TileSizeY, |
| 1041 |
!(KeyModifiers & ModifierShift)); |
| 1042 |
ret = 1; |
| 1043 |
} else { |
| 1044 |
SendCommandMove(unit, sx / TileSizeX, sy / TileSizeY, |
| 1045 |
!(KeyModifiers & ModifierShift)); |
| 1046 |
ret = 1; |
| 1047 |
} |
| 1048 |
} |
| 1049 |
return ret; |
| 1050 |
} |
| 1051 |
|
| 1052 |
/** |
| 1053 |
** Let units patrol between current postion and the selected. |
| 1054 |
** |
| 1055 |
** @param sx X screen map position. |
| 1056 |
** @param sy Y screen map position. |
| 1057 |
*/ |
| 1058 |
static int SendPatrol(int sx, int sy) |
| 1059 |
{ |
| 1060 |
int ret = 0; |
| 1061 |
|
| 1062 |
for (int i = 0; i < NumSelected; ++i) { |
| 1063 |
CUnit *unit = Selected[i]; |
| 1064 |
SendCommandPatrol(unit, sx / TileSizeX, sy / TileSizeY, |
| 1065 |
!(KeyModifiers & ModifierShift)); |
| 1066 |
ret = 1; |
| 1067 |
} |
| 1068 |
return ret; |
| 1069 |
} |
| 1070 |
|
| 1071 |
/** |
| 1072 |
** Let units harvest wood/mine gold/haul oil |
| 1073 |
** |
| 1074 |
** @param sx X screen map position |
| 1075 |
** @param sy Y screen map position |
| 1076 |
** |
| 1077 |
** @see Selected |
| 1078 |
*/ |
| 1079 |
static int SendResource(int sx, int sy) |
| 1080 |
{ |
| 1081 |
int i; |
| 1082 |
int x; |
| 1083 |
int y; |
| 1084 |
CUnit *unit; |
| 1085 |
CUnit *dest; |
| 1086 |
int ret; |
| 1087 |
|
| 1088 |
ret = 0; |
| 1089 |
dest = UnitUnderCursor; |
| 1090 |
x = sx / TileSizeX; |
| 1091 |
y = sy / TileSizeY; |
| 1092 |
|
| 1093 |
for (i = 0; i < NumSelected; ++i) { |
| 1094 |
unit = Selected[i]; |
| 1095 |
if (unit->Type->Harvester) { |
| 1096 |
if (dest && dest->Type->CanHarvestFrom && unit->Type->Harvester) { |
| 1097 |
dest->Blink = 4; |
| 1098 |
SendCommandResource(Selected[i],dest, !(KeyModifiers & ModifierShift)); |
| 1099 |
ret = 1; |
| 1100 |
continue; |
| 1101 |
} |
| 1102 |
} |
| 1103 |
if (!CanMove(unit)) { |
| 1104 |
if (dest && dest->Type->CanHarvestFrom) { |
| 1105 |
dest->Blink = 4; |
| 1106 |
SendCommandResource(unit, dest, !(KeyModifiers & ModifierShift)); |
| 1107 |
ret = 1; |
| 1108 |
continue; |
| 1109 |
} |
| 1110 |
SendCommandMove(unit, x, y, !(KeyModifiers & ModifierShift)); |
| 1111 |
ret = 1; |
| 1112 |
continue; |
| 1113 |
} |
| 1114 |
} |
| 1115 |
return ret; |
| 1116 |
} |
| 1117 |
|
| 1118 |
/** |
| 1119 |
** Send selected units to unload passengers. |
| 1120 |
** |
| 1121 |
** @param sx X screen map position. |
| 1122 |
** @param sy Y screen map position. |
| 1123 |
*/ |
| 1124 |
static int SendUnload(int sx, int sy) |
| 1125 |
{ |
| 1126 |
int ret = 0; |
| 1127 |
|
| 1128 |
for (int i = 0; i < NumSelected; ++i) { |
| 1129 |
// FIXME: not only transporter selected? |
| 1130 |
SendCommandUnload(Selected[i], sx / TileSizeX, sy / TileSizeY, NoUnitP, |
| 1131 |
!(KeyModifiers & ModifierShift)); |
| 1132 |
ret = 1; |
| 1133 |
} |
| 1134 |
return ret; |
| 1135 |
} |
| 1136 |
|
| 1137 |
/** |
| 1138 |
** Send the current selected group for spell cast. |
| 1139 |
** |
| 1140 |
** To empty field: |
| 1141 |
** To unit: |
| 1142 |
** Spell cast on unit or on map spot. |
| 1143 |
** |
| 1144 |
** @param sx X screen map position. |
| 1145 |
** @param sy Y screen map position. |
| 1146 |
** |
| 1147 |
** @see Selected, @see NumSelected |
| 1148 |
*/ |
| 1149 |
static int SendSpellCast(int sx, int sy) |
| 1150 |
{ |
| 1151 |
CUnit *dest; |
| 1152 |
int ret = 0; |
| 1153 |
|
| 1154 |
dest = UnitUnderCursor; |
| 1155 |
|
| 1156 |
/* NOTE: Vladi: |
| 1157 |
This is a high-level function, it sends target spot and unit |
| 1158 |
(if exists). All checks are performed at spell cast handle |
| 1159 |
function which will cancel function if cannot be executed |
| 1160 |
*/ |
| 1161 |
for (int i = 0; i < NumSelected; ++i) { |
| 1162 |
CUnit *unit = Selected[i]; |
| 1163 |
if (!unit->Type->CanCastSpell) { |
| 1164 |
DebugPrint("but unit %d(%s) can't cast spells?\n" _C_ |
| 1165 |
unit->Slot _C_ unit->Type->Name.c_str()); |
| 1166 |
// this unit cannot cast spell |
| 1167 |
continue; |
| 1168 |
} |
| 1169 |
if (dest && unit == dest) { |
| 1170 |
// no unit can cast spell on himself |
| 1171 |
// n0b0dy: why not? |
| 1172 |
continue; |
| 1173 |
} |
| 1174 |
// CursorValue here holds the spell type id |
| 1175 |
SendCommandSpellCast(unit, sx / TileSizeX, sy / TileSizeY, dest, |
| 1176 |
CursorValue, !(KeyModifiers & ModifierShift)); |
| 1177 |
ret = 1; |
| 1178 |
} |
| 1179 |
return ret; |
| 1180 |
} |
| 1181 |
|
| 1182 |
/** |
| 1183 |
** Send a command to selected units. |
| 1184 |
** |
| 1185 |
** @param sx X screen map position |
| 1186 |
** @param sy Y screen map position |
| 1187 |
*/ |
| 1188 |
static void SendCommand(int sx, int sy) |
| 1189 |
{ |
| 1190 |
int ret; |
| 1191 |
|
| 1192 |
ret = 0; |
| 1193 |
CurrentButtonLevel = 0; |
| 1194 |
UI.ButtonPanel.Update(); |
| 1195 |
switch (CursorAction) { |
| 1196 |
case ButtonMove: |
| 1197 |
ret = SendMove(sx, sy); |
| 1198 |
break; |
| 1199 |
case ButtonRepair: |
| 1200 |
ret = SendRepair(sx, sy); |
| 1201 |
break; |
| 1202 |
case ButtonAttack: |
| 1203 |
ret = SendAttack(sx, sy); |
| 1204 |
break; |
| 1205 |
case ButtonAttackGround: |
| 1206 |
ret = SendAttackGround(sx, sy); |
| 1207 |
break; |
| 1208 |
case ButtonPatrol: |
| 1209 |
ret = SendPatrol(sx, sy); |
| 1210 |
break; |
| 1211 |
case ButtonHarvest: |
| 1212 |
ret = SendResource(sx, sy); |
| 1213 |
break; |
| 1214 |
case ButtonUnload: |
| 1215 |
ret = SendUnload(sx, sy); |
| 1216 |
break; |
| 1217 |
case ButtonSpellCast: |
| 1218 |
ret = SendSpellCast(sx, sy); |
| 1219 |
break; |
| 1220 |
default: |
| 1221 |
DebugPrint("Unsupported send action %d\n" _C_ CursorAction); |
| 1222 |
break; |
| 1223 |
} |
| 1224 |
|
| 1225 |
if (ret) { |
| 1226 |
// Acknowledge the command with first selected unit. |
| 1227 |
for (int i = 0; i < NumSelected; ++i) { |
| 1228 |
if (Selected[i]->Type->Sound.Acknowledgement.Sound) { |
| 1229 |
PlayUnitSound(Selected[i], VoiceAcknowledging); |
| 1230 |
break; |
| 1231 |
} |
| 1232 |
} |
| 1233 |
ShowOrdersCount = GameCycle + Preference.ShowOrders * CYCLES_PER_SECOND; |
| 1234 |
} |
| 1235 |
} |
| 1236 |
|
| 1237 |
//............................................................................. |
| 1238 |
|
| 1239 |
/** |
| 1240 |
** Mouse button press on selection/group area. |
| 1241 |
** |
| 1242 |
** @param num Button number. |
| 1243 |
** @param button Mouse Button pressed. |
| 1244 |
*/ |
| 1245 |
static void DoSelectionButtons(int num, unsigned button) |
| 1246 |
{ |
| 1247 |
if (GameObserve || GamePaused) { |
| 1248 |
return; |
| 1249 |
} |
| 1250 |
|
| 1251 |
if (num >= NumSelected || !(MouseButtons & LeftButton)) { |
| 1252 |
return; |
| 1253 |
} |
| 1254 |
|
| 1255 |
CUnit *unit = Selected[num]; |
| 1256 |
|
| 1257 |
if ((KeyModifiers & ModifierControl) || |
| 1258 |
(MouseButtons & (LeftButton << MouseDoubleShift))) { |
| 1259 |
if (KeyModifiers & ModifierShift) { |
| 1260 |
ToggleUnitsByType(unit); |
| 1261 |
} else { |
| 1262 |
SelectUnitsByType(unit); |
| 1263 |
} |
| 1264 |
} else if (KeyModifiers & ModifierAlt) { |
| 1265 |
if (KeyModifiers & ModifierShift) { |
| 1266 |
AddGroupFromUnitToSelection(unit); |
| 1267 |
} else { |
| 1268 |
SelectGroupFromUnit(unit); |
| 1269 |
} |
| 1270 |
} else if (KeyModifiers & ModifierShift) { |
| 1271 |
ToggleSelectUnit(unit); |
| 1272 |
} else { |
| 1273 |
SelectSingleUnit(unit); |
| 1274 |
} |
| 1275 |
|
| 1276 |
UI.StatusLine.Clear(); |
| 1277 |
ClearCosts(); |
| 1278 |
CurrentButtonLevel = 0; |
| 1279 |
SelectionChanged(); |
| 1280 |
} |
| 1281 |
|
| 1282 |
//............................................................................. |
| 1283 |
|
| 1284 |
/** |
| 1285 |
** Handle mouse button pressed in select state. |
| 1286 |
** |
| 1287 |
** Select state is used for target of patrol, attack, move, .... |
| 1288 |
** |
| 1289 |
** @param button Button pressed down. |
| 1290 |
*/ |
| 1291 |
static void UISelectStateButtonDown(unsigned button) |
| 1292 |
{ |
| 1293 |
if (GameObserve || GamePaused) { |
| 1294 |
return; |
| 1295 |
} |
| 1296 |
|
| 1297 |
// |
| 1298 |
// Clicking on the map. |
| 1299 |
// |
| 1300 |
if (CursorOn == CursorOnMap && UI.MouseViewport->IsInsideMapArea(CursorX, CursorY)) { |
| 1301 |
UI.StatusLine.Clear(); |
| 1302 |
ClearCosts(); |
| 1303 |
CursorState = CursorStatePoint; |
| 1304 |
GameCursor = UI.Point.Cursor; |
| 1305 |
CurrentButtonLevel = 0; |
| 1306 |
UI.ButtonPanel.Update(); |
| 1307 |
|
| 1308 |
if (MouseButtons & LeftButton) { |
| 1309 |
const CViewport *vp = UI.MouseViewport; |
| 1310 |
if (!ClickMissile.empty()) { |
| 1311 |
int mx = vp->MapX * TileSizeX + CursorX - vp->X + vp->OffsetX; |
| 1312 |
int my = vp->MapY * TileSizeY + CursorY - vp->Y + vp->OffsetY; |
| 1313 |
MakeLocalMissile(MissileTypeByIdent(ClickMissile), |
| 1314 |
mx, my, mx, my); |
| 1315 |
} |
| 1316 |
int sx = CursorX - vp->X + TileSizeX * vp->MapX + vp->OffsetX; |
| 1317 |
int sy = CursorY - vp->Y + TileSizeY * vp->MapY + vp->OffsetY; |
| 1318 |
SendCommand(sx, sy); |
| 1319 |
} |
| 1320 |
return; |
| 1321 |
} |
| 1322 |
|
| 1323 |
// |
| 1324 |
// Clicking on the minimap. |
| 1325 |
// |
| 1326 |
if (CursorOn == CursorOnMinimap) { |
| 1327 |
int mx = UI.Minimap.Screen2MapX(CursorX); |
| 1328 |
int my = UI.Minimap.Screen2MapY(CursorY); |
| 1329 |
|
| 1330 |
if (MouseButtons & LeftButton) { |
| 1331 |
int sx = mx * TileSizeX; |
| 1332 |
int sy = my * TileSizeY; |
| 1333 |
UI.StatusLine.Clear(); |
| 1334 |
ClearCosts(); |
| 1335 |
CursorState = CursorStatePoint; |
| 1336 |
GameCursor = UI.Point.Cursor; |
| 1337 |
CurrentButtonLevel = 0; |
| 1338 |
UI.ButtonPanel.Update(); |
| 1339 |
if (!ClickMissile.empty()) { |
| 1340 |
MakeLocalMissile(MissileTypeByIdent(ClickMissile), |
| 1341 |
sx + TileSizeX / 2, sy + TileSizeY / 2, 0, 0); |
| 1342 |
} |
| 1343 |
SendCommand(sx, sy); |
| 1344 |
} else { |
| 1345 |
UI.SelectedViewport->Center(mx, my, TileSizeX / 2, TileSizeY / 2); |
| 1346 |
} |
| 1347 |
return; |
| 1348 |
} |
| 1349 |
|
| 1350 |
if (CursorOn == CursorOnButton) { |
| 1351 |
// FIXME: other buttons? |
| 1352 |
if (ButtonAreaUnderCursor == ButtonAreaButton) { |
| 1353 |
UI.ButtonPanel.DoClicked(ButtonUnderCursor); |
| 1354 |
return; |
| 1355 |
} |
| 1356 |
} |
| 1357 |
|
| 1358 |
UI.StatusLine.Clear(); |
| 1359 |
ClearCosts(); |
| 1360 |
CursorState = CursorStatePoint; |
| 1361 |
GameCursor = UI.Point.Cursor; |
| 1362 |
CurrentButtonLevel = 0; |
| 1363 |
UI.ButtonPanel.Update(); |
| 1364 |
} |
| 1365 |
|
| 1366 |
|
| 1367 |
/** |
| 1368 |
** Called if mouse button pressed down. |
| 1369 |
** |
| 1370 |
** @param button Button pressed down. |
| 1371 |
*/ |
| 1372 |
void UIHandleButtonDown(unsigned button) |
| 1373 |
{ |
| 1374 |
static bool OldShowSightRange; |
| 1375 |
static bool OldShowReactionRange; |
| 1376 |
static bool OldShowAttackRange; |
| 1377 |
static bool OldValid = false; |
| 1378 |
CUnit *uins; |
| 1379 |
|
| 1380 |
/** |
| 1381 |
** Detect long selection click, FIXME: tempory hack to test the feature. |
| 1382 |
*/ |
| 1383 |
#define LongSelected (MouseButtons & ((LeftButton << MouseHoldShift))) |
| 1384 |
|
| 1385 |
if (LongSelected) { |
| 1386 |
if (!OldValid) { |
| 1387 |
OldShowSightRange = Preference.ShowSightRange; |
| 1388 |
OldShowAttackRange = Preference.ShowAttackRange; |
| 1389 |
OldShowReactionRange = Preference.ShowReactionRange; |
| 1390 |
OldValid = true; |
| 1391 |
|
| 1392 |
Preference.ShowSightRange = true; |
| 1393 |
Preference.ShowAttackRange = true; |
| 1394 |
Preference.ShowReactionRange = true; |
| 1395 |
} |
| 1396 |
} else if (OldValid) { |
| 1397 |
Preference.ShowSightRange = OldShowSightRange; |
| 1398 |
Preference.ShowAttackRange = OldShowAttackRange; |
| 1399 |
Preference.ShowReactionRange = OldShowReactionRange; |
| 1400 |
OldValid = false; |
| 1401 |
} |
| 1402 |
|
| 1403 |
// select mode |
| 1404 |
if (CursorState == CursorStateRectangle) { |
| 1405 |
return; |
| 1406 |
} |
| 1407 |
// CursorOn should have changed with BigMapMode, so recompute it. |
| 1408 |
HandleMouseOn(CursorX, CursorY); |
| 1409 |
// |
| 1410 |
// Selecting target. (Move,Attack,Patrol,... commands); |
| 1411 |
// |
| 1412 |
if (CursorState == CursorStateSelect) { |
| 1413 |
UISelectStateButtonDown(button); |
| 1414 |
return; |
| 1415 |
} |
| 1416 |
|
| 1417 |
if (CursorState == CursorStatePieMenu) { |
| 1418 |
if (CursorOn == CursorOnMap) { |
| 1419 |
HandlePieMenuMouseSelection(); |
| 1420 |
return; |
| 1421 |
} else { |
| 1422 |
// Pie Menu canceled |
| 1423 |
CursorState = CursorStatePoint; |
| 1424 |
// Don't return, we might be over another button |
| 1425 |
} |
| 1426 |
} |
| 1427 |
|
| 1428 |
// |
| 1429 |
// Cursor is on the map area |
| 1430 |
// |
| 1431 |
if (CursorOn == CursorOnMap) { |
| 1432 |
Assert(UI.MouseViewport); |
| 1433 |
|
| 1434 |
if ((MouseButtons & LeftButton) && |
| 1435 |
UI.SelectedViewport != UI.MouseViewport) { |
| 1436 |
UI.SelectedViewport = UI.MouseViewport; |
| 1437 |
DebugPrint("selected viewport changed to %ld.\n" _C_ |
| 1438 |
static_cast<long int>(UI.SelectedViewport - UI.Viewports)); |
| 1439 |
} |
| 1440 |
|
| 1441 |
// to redraw the cursor immediately (and avoid up to 1 sec delay |
| 1442 |
if (CursorBuilding) { |
| 1443 |
// Possible Selected[0] was removed from map |
| 1444 |
// need to make sure there is a unit to build |
| 1445 |
if (Selected[0] && (MouseButtons & LeftButton) && |
| 1446 |
UI.MouseViewport->IsInsideMapArea(CursorX, CursorY)) {// enter select mode |
| 1447 |
int explored; |
| 1448 |
int x = UI.MouseViewport->Viewport2MapX(CursorX); |
| 1449 |
int y = UI.MouseViewport->Viewport2MapY(CursorY); |
| 1450 |
|
| 1451 |
// FIXME: error messages |
| 1452 |
|
| 1453 |
explored = 1; |
| 1454 |
for (int j = 0; explored && j < Selected[0]->Type->TileHeight; ++j) { |
| 1455 |
for (int i = 0; i < Selected[0]->Type->TileWidth; ++i) { |
| 1456 |
if (!Map.IsFieldExplored(ThisPlayer, x + i, y + j)) { |
| 1457 |
explored = 0; |
| 1458 |
break; |
| 1459 |
} |
| 1460 |
} |
| 1461 |
} |
| 1462 |
// 0 Test build, don't really build |
| 1463 |
if (CanBuildUnitType(Selected[0], CursorBuilding, x, y, 0) && |
| 1464 |
(explored || ReplayRevealMap)) { |
| 1465 |
PlayGameSound(GameSounds.PlacementSuccess.Sound, |
| 1466 |
MaxSampleVolume); |
| 1467 |
for (int i = 0; i < NumSelected; ++i) { |
| 1468 |
SendCommandBuildBuilding(Selected[i], x, y, CursorBuilding, |
| 1469 |
!(KeyModifiers & ModifierShift)); |
| 1470 |
} |
| 1471 |
if (!(KeyModifiers & (ModifierAlt | ModifierShift))) { |
| 1472 |
CancelBuildingMode(); |
| 1473 |
} |
| 1474 |
} else { |
| 1475 |
PlayGameSound(GameSounds.PlacementError.Sound, |
| 1476 |
MaxSampleVolume); |
| 1477 |
} |
| 1478 |
} else { |
| 1479 |
CancelBuildingMode(); |
| 1480 |
} |
| 1481 |
return; |
| 1482 |
} |
| 1483 |
|
| 1484 |
if (MouseButtons & UI.PieMenu.MouseButton) { // enter pie menu |
| 1485 |
UnitUnderCursor = NULL; |
| 1486 |
GameCursor = UI.Point.Cursor; // Reset |
| 1487 |
CursorStartX = CursorX; |
| 1488 |
CursorStartY = CursorY; |
| 1489 |
if (NumSelected && Selected[0]->Player == ThisPlayer && |
| 1490 |
CursorState == CursorStatePoint) { |
| 1491 |
CursorState = CursorStatePieMenu; |
| 1492 |
} |
| 1493 |
} else if (MouseButtons & LeftButton) { // enter select mode |
| 1494 |
CursorStartX = CursorX; |
| 1495 |
CursorStartY = CursorY; |
| 1496 |
CursorStartScrMapX = CursorStartX - UI.MouseViewport->X + |
| 1497 |
TileSizeX * UI.MouseViewport->MapX + UI.MouseViewport->OffsetX; |
| 1498 |
CursorStartScrMapY = CursorStartY - UI.MouseViewport->Y + |
| 1499 |
TileSizeY * UI.MouseViewport->MapY + UI.MouseViewport->OffsetY; |
| 1500 |
GameCursor = UI.Cross.Cursor; |
| 1501 |
CursorState = CursorStateRectangle; |
| 1502 |
} else if (MouseButtons & MiddleButton) {// enter move map mode |
| 1503 |
CursorStartX = CursorX; |
| 1504 |
CursorStartY = CursorY; |
| 1505 |
GameCursor = UI.Scroll.Cursor; |
| 1506 |
} else if (MouseButtons & RightButton) { |
| 1507 |
int x; |
| 1508 |
int y; |
| 1509 |
|
| 1510 |
if (!GameObserve && !GamePaused && UI.MouseViewport->IsInsideMapArea(CursorX, CursorY)) { |
| 1511 |
CUnit *unit; |
| 1512 |
// FIXME: Rethink the complete chaos of coordinates here |
| 1513 |
// FIXME: Johns: Perhaps we should use a pixel map coordinates |
| 1514 |
|
| 1515 |
x = UI.MouseViewport->Viewport2MapX(CursorX); |
| 1516 |
y = UI.MouseViewport->Viewport2MapY(CursorY); |
| 1517 |
|
| 1518 |
if (UnitUnderCursor && (unit = UnitOnMapTile(x, y)) && |
| 1519 |
!UnitUnderCursor->Type->Decoration) { |
| 1520 |
unit->Blink = 4; // if right click on building -- blink |
| 1521 |
} else { // if not not click on building -- green cross |
| 1522 |
if (!ClickMissile.empty()) { |
| 1523 |
MakeLocalMissile(MissileTypeByIdent(ClickMissile), |
| 1524 |
UI.MouseViewport->MapX * TileSizeX + |
| 1525 |
CursorX - UI.MouseViewport->X + UI.MouseViewport->OffsetX, |
| 1526 |
UI.MouseViewport->MapY * TileSizeY + |
| 1527 |
CursorY - UI.MouseViewport->Y + UI.MouseViewport->OffsetY, 0, 0); |
| 1528 |
} |
| 1529 |
} |
| 1530 |
DoRightButton(x * TileSizeX, y * TileSizeY); |
| 1531 |
} |
| 1532 |
} |
| 1533 |
// |
| 1534 |
// Cursor is on the minimap area |
| 1535 |
// |
| 1536 |
} else if (CursorOn == CursorOnMinimap) { |
| 1537 |
if (MouseButtons & LeftButton) { // enter move mini-mode |
| 1538 |
UI.SelectedViewport->Center( |
| 1539 |
UI.Minimap.Screen2MapX(CursorX), UI.Minimap.Screen2MapY(CursorY), |
| 1540 |
TileSizeX / 2, TileSizeY / 2); |
| 1541 |
} else if (MouseButtons & RightButton) { |
| 1542 |
if (!GameObserve && !GamePaused) { |
| 1543 |
if (!ClickMissile.empty()) { |
| 1544 |
MakeLocalMissile(MissileTypeByIdent(ClickMissile), |
| 1545 |
UI.Minimap.Screen2MapX(CursorX) * TileSizeX + TileSizeX / 2, |
| 1546 |
UI.Minimap.Screen2MapY(CursorY) * TileSizeY + TileSizeY / 2, 0, 0); |
| 1547 |
} |
| 1548 |
// DoRightButton() takes screen map coordinates |
| 1549 |
DoRightButton(UI.Minimap.Screen2MapX(CursorX) * TileSizeX, |
| 1550 |
UI.Minimap.Screen2MapY(CursorY) * TileSizeY); |
| 1551 |
} |
| 1552 |
} |
| 1553 |
// |
| 1554 |
// Cursor is on the buttons: group or command |
| 1555 |
// |
| 1556 |
} else if (CursorOn == CursorOnButton) { |
| 1557 |
// |
| 1558 |
// clicked on info panel - selection shown |
| 1559 |
// |
| 1560 |
if (NumSelected > 1 && ButtonAreaUnderCursor == ButtonAreaSelected) { |
| 1561 |
DoSelectionButtons(ButtonUnderCursor, button); |
| 1562 |
} else if ((MouseButtons & LeftButton)) { |
| 1563 |
// |
| 1564 |
// clicked on menu button |
| 1565 |
// |
| 1566 |
if (ButtonAreaUnderCursor == ButtonAreaMenu) { |
| 1567 |
if ((ButtonUnderCursor == ButtonUnderMenu || |
| 1568 |
ButtonUnderCursor == ButtonUnderNetworkMenu) && |
| 1569 |
!GameMenuButtonClicked) { |
| 1570 |
PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); |
| 1571 |
GameMenuButtonClicked = true; |
| 1572 |
} else if (ButtonUnderCursor == ButtonUnderNetworkDiplomacy && |
| 1573 |
!GameDiplomacyButtonClicked) { |
| 1574 |
PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); |
| 1575 |
GameDiplomacyButtonClicked = true; |
| 1576 |
} |
| 1577 |
// |
| 1578 |
// clicked on selected button |
| 1579 |
// |
| 1580 |
} else if (ButtonAreaUnderCursor == ButtonAreaSelected) { |
| 1581 |
// |
| 1582 |
// clicked on single unit shown |
| 1583 |
// |
| 1584 |
if (ButtonUnderCursor == 0 && NumSelected == 1) { |
| 1585 |
PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); |
| 1586 |
UI.SelectedViewport->Center(Selected[0]->X, |
| 1587 |
Selected[0]->Y, Selected[0]->IX + TileSizeX / 2, |
| 1588 |
Selected[0]->IY + TileSizeY / 2); |
| 1589 |
} |
| 1590 |
// |
| 1591 |
// clicked on training button |
| 1592 |
// |
| 1593 |
} else if (ButtonAreaUnderCursor == ButtonAreaTraining) { |
| 1594 |
if (!GameObserve && !GamePaused && |
| 1595 |
ThisPlayer->IsTeamed(Selected[0])) { |
| 1596 |
if (ButtonUnderCursor < Selected[0]->OrderCount && |
| 1597 |
Selected[0]->Orders[ButtonUnderCursor]->Action == UnitActionTrain) { |
| 1598 |
DebugPrint("Cancel slot %d %s\n" _C_ |
| 1599 |
ButtonUnderCursor _C_ |
| 1600 |
Selected[0]->Orders[ButtonUnderCursor]->Type->Ident.c_str()); |
| 1601 |
SendCommandCancelTraining(Selected[0], |
| 1602 |
ButtonUnderCursor, |
| 1603 |
Selected[0]->Orders[ButtonUnderCursor]->Type); |
| 1604 |
} |
| 1605 |
} |
| 1606 |
// |
| 1607 |
// clicked on button panel |
| 1608 |
// |
| 1609 |
} else if (ButtonAreaUnderCursor == ButtonAreaTransporting) { |
| 1610 |
// |
| 1611 |
// for transporter |
| 1612 |
// |
| 1613 |
if (!GameObserve && !GamePaused && |
| 1614 |
ThisPlayer->IsTeamed(Selected[0])) { |
| 1615 |
if (Selected[0]->BoardCount >= ButtonUnderCursor) { |
| 1616 |
uins = Selected[0]->UnitInside; |
| 1617 |
for (int i = ButtonUnderCursor; i; uins = uins->NextContained) { |
| 1618 |
if (uins->Boarded) { |
| 1619 |
--i; |
| 1620 |
} |
| 1621 |
} |
| 1622 |
Assert(uins->Boarded); |
| 1623 |
SendCommandUnload(Selected[0], |
| 1624 |
Selected[0]->X, Selected[0]->Y, uins, |
| 1625 |
!(KeyModifiers & ModifierShift)); |
| 1626 |
} |
| 1627 |
} |
| 1628 |
} else if (ButtonAreaUnderCursor == ButtonAreaButton) { |
| 1629 |
if (!GameObserve && !GamePaused && |
| 1630 |
ThisPlayer->IsTeamed(Selected[0])) { |
| 1631 |
UI.ButtonPanel.DoClicked(ButtonUnderCursor); |
| 1632 |
} |
| 1633 |
} |
| 1634 |
} else if ((MouseButtons & MiddleButton)) { |
| 1635 |
// |
| 1636 |
// clicked on info panel - single unit shown |
| 1637 |
// |
| 1638 |
if (ButtonAreaUnderCursor == ButtonAreaSelected && |
| 1639 |
ButtonUnderCursor == 0 && NumSelected == 1) { |
| 1640 |
PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); |
| 1641 |
if (UI.SelectedViewport->Unit == Selected[0]) { |
| 1642 |
UI.SelectedViewport->Unit = NULL; |
| 1643 |
} else { |
| 1644 |
UI.SelectedViewport->Unit = Selected[0]; |
| 1645 |
} |
| 1646 |
} |
| 1647 |
} else if ((MouseButtons & RightButton)) { |
| 1648 |
} |
| 1649 |
} |
| 1650 |
} |
| 1651 |
|
| 1652 |
/** |
| 1653 |
** Called if mouse button released. |
| 1654 |
** |
| 1655 |
** @param button Button released. |
| 1656 |
*/ |
| 1657 |
void UIHandleButtonUp(unsigned button) |
| 1658 |
{ |
| 1659 |
// |
| 1660 |
// Move map. |
| 1661 |
// |
| 1662 |
if (GameCursor == UI.Scroll.Cursor) { |
| 1663 |
GameCursor = UI.Point.Cursor; |
| 1664 |
return; |
| 1665 |
} |
| 1666 |
|
| 1667 |
// |
| 1668 |
// Pie Menu |
| 1669 |
// |
| 1670 |
if (CursorState == CursorStatePieMenu) { |
| 1671 |
// Little threshold |
| 1672 |
if (CursorStartX < CursorX - 1 || CursorStartX > CursorX + 1 || |
| 1673 |
CursorStartY < CursorY - 1 || CursorStartY > CursorY + 1) { |
| 1674 |
// there was a move, handle the selected button/pie |
| 1675 |
HandlePieMenuMouseSelection(); |
| 1676 |
} |
| 1677 |
} |
| 1678 |
|
| 1679 |
// |
| 1680 |
// Menu (F10) button |
| 1681 |
// |
| 1682 |
if ((1 << button) == LeftButton && GameMenuButtonClicked) { |
| 1683 |
GameMenuButtonClicked = false; |
| 1684 |
if (ButtonAreaUnderCursor == ButtonAreaMenu) { |
| 1685 |
if (ButtonUnderCursor == ButtonUnderMenu || |
| 1686 |
ButtonUnderCursor == ButtonUnderNetworkMenu) { |
| 1687 |
// FIXME: Not if, in input mode. |
| 1688 |
if (!IsNetworkGame()) { |
| 1689 |
GamePaused = true; |
| 1690 |
UI.StatusLine.Set(_("Game Paused")); |
| 1691 |
} |
| 1692 |
if (ButtonUnderCursor == ButtonUnderMenu) { |
| 1693 |
if (UI.MenuButton.Callback) { |
| 1694 |
UI.MenuButton.Callback->action(""); |
| 1695 |
} |
| 1696 |
} else { |
| 1697 |
if (UI.NetworkMenuButton.Callback) { |
| 1698 |
UI.NetworkMenuButton.Callback->action(""); |
| 1699 |
} |
| 1700 |
} |
| 1701 |
return; |
| 1702 |
} |
| 1703 |
} |
| 1704 |
} |
| 1705 |
|
| 1706 |
// |
| 1707 |
// Diplomacy button |
| 1708 |
// |
| 1709 |
if ((1 << button) == LeftButton && GameDiplomacyButtonClicked) { |
| 1710 |
GameDiplomacyButtonClicked = false; |
| 1711 |
if (ButtonAreaUnderCursor == ButtonAreaMenu && |
| 1712 |
ButtonUnderCursor == ButtonUnderNetworkDiplomacy) { |
| 1713 |
if (UI.NetworkDiplomacyButton.Callback) { |
| 1714 |
UI.NetworkDiplomacyButton.Callback->action(""); |
| 1715 |
} |
| 1716 |
return; |
| 1717 |
} |
| 1718 |
} |
| 1719 |
|
| 1720 |
// SHIFT toggles select/unselect a single unit and |
| 1721 |
// add the content of the rectangle to the selectection |
| 1722 |
// ALT takes group of unit |
| 1723 |
// CTRL takes all units of same type |
| 1724 |
if (CursorState == CursorStateRectangle && |
| 1725 |
!(MouseButtons & LeftButton)) { // leave select mode |
| 1726 |
int num; |
| 1727 |
CUnit *unit; |
| 1728 |
|
| 1729 |
unit = NULL; |
| 1730 |
// |
| 1731 |
// Little threshold |
| 1732 |
// |
| 1733 |
if (CursorStartX < CursorX - 1 || CursorStartX > CursorX + 1 || |
| 1734 |
CursorStartY < CursorY - 1 || CursorStartY > CursorY + 1) { |
| 1735 |
int x0; |
| 1736 |
int y0; |
| 1737 |
int x1; |
| 1738 |
int y1; |
| 1739 |
|
| 1740 |
x0 = CursorStartScrMapX; |
| 1741 |
y0 = CursorStartScrMapY; |
| 1742 |
x1 = CursorX - UI.MouseViewport->X + |
| 1743 |
UI.MouseViewport->MapX * TileSizeX + UI.MouseViewport->OffsetX; |
| 1744 |
y1 = CursorY - UI.MouseViewport->Y + |
| 1745 |
UI.MouseViewport->MapY * TileSizeY + UI.MouseViewport->OffsetY; |
| 1746 |
|
| 1747 |
if (x0 > x1) { |
| 1748 |
int swap; |
| 1749 |
|
| 1750 |
swap = x0; |
| 1751 |
x0 = x1; |
| 1752 |
x1 = swap; |
| 1753 |
} |
| 1754 |
if (y0 > y1) { |
| 1755 |
int swap; |
| 1756 |
|
| 1757 |
swap = y0; |
| 1758 |
y0 = y1; |
| 1759 |
y1 = swap; |
| 1760 |
} |
| 1761 |
if (KeyModifiers & ModifierShift) { |
| 1762 |
if (KeyModifiers & ModifierAlt) { |
| 1763 |
num = AddSelectedGroundUnitsInRectangle(x0, y0, x1, y1); |
| 1764 |
} else if (KeyModifiers & ModifierControl) { |
| 1765 |
num = AddSelectedAirUnitsInRectangle(x0, y0, x1, y1); |
| 1766 |
} else { |
| 1767 |
num = AddSelectedUnitsInRectangle(x0, y0, x1, y1); |
| 1768 |
} |
| 1769 |
} else { |
| 1770 |
if (KeyModifiers & ModifierAlt) { |
| 1771 |
num = SelectGroundUnitsInRectangle(x0, y0, x1, y1); |
| 1772 |
} else if (KeyModifiers & ModifierControl) { |
| 1773 |
num = SelectAirUnitsInRectangle(x0, y0, x1, y1); |
| 1774 |
} else { |
| 1775 |
num = SelectUnitsInRectangle(x0, y0, x1, y1); |
| 1776 |
} |
| 1777 |
} |
| 1778 |
} else { |
| 1779 |
// |
| 1780 |
// Select single unit |
| 1781 |
// |
| 1782 |
// cade: cannot select unit on invisible space |
| 1783 |
// FIXME: johns: only complete invisibile units |
| 1784 |
if (Map.IsFieldVisible(ThisPlayer, |
| 1785 |
UI.MouseViewport->Viewport2MapX(CursorX), |
| 1786 |
UI.MouseViewport->Viewport2MapY(CursorY)) || ReplayRevealMap) { |
| 1787 |
unit = UnitOnScreen( |
| 1788 |
CursorX - UI.MouseViewport->X + UI.MouseViewport->MapX * TileSizeX + UI.MouseViewport->OffsetX, |
| 1789 |
CursorY - UI.MouseViewport->Y + UI.MouseViewport->MapY * TileSizeY + UI.MouseViewport->OffsetY); |
| 1790 |
} |
| 1791 |
if (unit) { |
| 1792 |
// FIXME: Not nice coded, button number hardcoded! |
| 1793 |
if ((KeyModifiers & ModifierControl) |
| 1794 |
|| (button & (1 << MouseDoubleShift))) { |
| 1795 |
if (KeyModifiers & ModifierShift) { |
| 1796 |
num = ToggleUnitsByType(unit); |
| 1797 |
} else { |
| 1798 |
num = SelectUnitsByType(unit); |
| 1799 |
} |
| 1800 |
} else if ((KeyModifiers & ModifierAlt) && unit->LastGroup) { |
| 1801 |
if (KeyModifiers & ModifierShift) { |
| 1802 |
num = AddGroupFromUnitToSelection(unit); |
| 1803 |
} else { |
| 1804 |
num = SelectGroupFromUnit(unit); |
| 1805 |
} |
| 1806 |
|
| 1807 |
// Don't allow to select own and enemy units. |
| 1808 |
// Don't allow mixing buildings |
| 1809 |
} else if (KeyModifiers & ModifierShift && |
| 1810 |
(unit->Player == ThisPlayer || ThisPlayer->IsTeamed(unit)) && |
| 1811 |
!unit->Type->Building && |
| 1812 |
(NumSelected != 1 || !Selected[0]->Type->Building) && |
| 1813 |
(NumSelected != 1 || Selected[0]->Player == ThisPlayer || |
| 1814 |
ThisPlayer->IsTeamed(Selected[0]))) { |
| 1815 |
num = ToggleSelectUnit(unit); |
| 1816 |
if (!num) { |
| 1817 |
SelectionChanged(); |
| 1818 |
} |
| 1819 |
} else { |
| 1820 |
SelectSingleUnit(unit); |
| 1821 |
num = 1; |
| 1822 |
} |
| 1823 |
} else { |
| 1824 |
num = 0; |
| 1825 |
} |
| 1826 |
} |
| 1827 |
|
| 1828 |
if (num) { |
| 1829 |
UI.StatusLine.Clear(); |
| 1830 |
ClearCosts(); |
| 1831 |
CurrentButtonLevel = 0; |
| 1832 |
SelectionChanged(); |
| 1833 |
|
| 1834 |
// |
| 1835 |
// Play selecting sound. |
| 1836 |
// Buildings, |
| 1837 |
// This player, or neutral unit (goldmine,critter) |
| 1838 |
// Other clicks. |
| 1839 |
// |
| 1840 |
if (NumSelected == 1) { |
| 1841 |
if (Selected[0]->Orders[0]->Action == UnitActionBuilt) { |
| 1842 |
PlayUnitSound(Selected[0], VoiceBuilding); |
| 1843 |
} else if (Selected[0]->Burning) { |
| 1844 |
// FIXME: use GameSounds.Burning |
| 1845 |
PlayGameSound(SoundForName("burning"), MaxSampleVolume); |
| 1846 |
} else if (Selected[0]->Player == ThisPlayer || ThisPlayer->IsTeamed(Selected[0]) || |
| 1847 |
Selected[0]->Player->Type == PlayerNeutral) { |
| 1848 |
PlayUnitSound(Selected[0], VoiceSelected); |
| 1849 |
} else { |
| 1850 |
PlayGameSound(GameSounds.Click.Sound, MaxSampleVolume); |
| 1851 |
} |
| 1852 |
if (Selected[0]->Player == ThisPlayer) { |
| 1853 |
char buf[64]; |
| 1854 |
if (Selected[0]->Player->UnitTypesCount[Selected[0]->Type->Slot] > 1) { |
| 1855 |
sprintf_s(buf, sizeof(buf), _("You have ~<%d~> %ss"), |
| 1856 |
Selected[0]->Player->UnitTypesCount[Selected[0]->Type->Slot], |
| 1857 |
Selected[0]->Type->Name.c_str()); |
| 1858 |
} else { |
| 1859 |
sprintf_s(buf, sizeof(buf), _("You have ~<1~> %s"), |
| 1860 |
Selected[0]->Type->Name.c_str()); |
| 1861 |
} |
| 1862 |
UI.StatusLine.Set(buf); |
| 1863 |
} |
| 1864 |
} |
| 1865 |
} |
| 1866 |
|
| 1867 |
CursorStartX = 0; |
| 1868 |
CursorStartY = 0; |
| 1869 |
GameCursor = UI.Point.Cursor; |
| 1870 |
CursorState = CursorStatePoint; |
| 1871 |
} |
| 1872 |
} |
| 1873 |
|
| 1874 |
/** |
| 1875 |
** Get pie menu under the cursor |
| 1876 |
** |
| 1877 |
** @return Index of the pie menu under the cursor or -1 for none |
| 1878 |
*/ |
| 1879 |
static int GetPieUnderCursor(void) |
| 1880 |
{ |
| 1881 |
int x = CursorX - (CursorStartX - ICON_SIZE_X / 2); |
| 1882 |
int y = CursorY - (CursorStartY - ICON_SIZE_Y / 2); |
| 1883 |
for (int i = 0; i < 8; ++i) { |
| 1884 |
if (x > UI.PieMenu.X[i] && x < UI.PieMenu.X[i] + ICON_SIZE_X && |
| 1885 |
y > UI.PieMenu.Y[i] && y < UI.PieMenu.Y[i] + ICON_SIZE_Y) { |
| 1886 |
return i; |
| 1887 |
} |
| 1888 |
} |
| 1889 |
return -1; // no pie under cursor |
| 1890 |
} |
| 1891 |
|
| 1892 |
/** |
| 1893 |
** Draw Pie Menu |
| 1894 |
*/ |
| 1895 |
void DrawPieMenu(void) |
| 1896 |
{ |
| 1897 |
int i; |
| 1898 |
const ButtonAction *buttons; |
| 1899 |
CViewport *vp; |
| 1900 |
CPlayer *player; |
| 1901 |
|
| 1902 |
if (CursorState != CursorStatePieMenu) |
| 1903 |
return; |
| 1904 |
|
| 1905 |
if (!(buttons = CurrentButtons)) { // no buttons |
| 1906 |
CursorState = CursorStatePoint; |
| 1907 |
return; |
| 1908 |
} |
| 1909 |
|
| 1910 |
vp = UI.SelectedViewport; |
| 1911 |
PushClipping(); |
| 1912 |
SetClipping(vp->X, vp->Y, vp->EndX, vp->EndY); |
| 1913 |
|
| 1914 |
// Draw background |
| 1915 |
if (UI.PieMenu.G) { |
| 1916 |
UI.PieMenu.G->DrawFrameClip(0, |
| 1917 |
CursorStartX - UI.PieMenu.G->Width / 2, |
| 1918 |
CursorStartY - UI.PieMenu.G->Height / 2); |
| 1919 |
} |
| 1920 |
player = Selected[0]->Player; |
| 1921 |
|
| 1922 |
for (i = 0; i < (int)UI.ButtonPanel.Buttons.size() && i < 8; ++i) { |
| 1923 |
if (buttons[i].Pos != -1) { |
| 1924 |
int x; |
| 1925 |
int y; |
| 1926 |
|
| 1927 |
x = CursorStartX - ICON_SIZE_X / 2 + UI.PieMenu.X[i]; |
| 1928 |
y = CursorStartY - ICON_SIZE_Y / 2 + UI.PieMenu.Y[i]; |
| 1929 |
// Draw icon |
| 1930 |
buttons[i].Icon.Icon->DrawIcon(player, x, y); |
| 1931 |
|
| 1932 |
// Tutorial show command key in icons |
| 1933 |
if (UI.ButtonPanel.ShowCommandKey) { |
| 1934 |
std::string text("?"); |
| 1935 |
|
| 1936 |
if (CurrentButtons[i].Key == 27) { |
| 1937 |
text = "ESC"; |
| 1938 |
} else { |
| 1939 |
text[0] = toupper(CurrentButtons[i].Key); |
| 1940 |
} |
| 1941 |
VideoDrawTextClip(x + 4, y + 4, GameFont, text); |
| 1942 |
} |
| 1943 |
} |
| 1944 |
} |
| 1945 |
|
| 1946 |
PopClipping(); |
| 1947 |
|
| 1948 |
i = GetPieUnderCursor(); |
| 1949 |
if (i != -1 && KeyState != KeyStateInput && buttons[i].Pos != -1) { |
| 1950 |
UpdateStatusLineForButton(&buttons[i]); |
| 1951 |
} |
| 1952 |
} |
| 1953 |
|
| 1954 |
/** |
| 1955 |
** Handle pie menu mouse selection |
| 1956 |
*/ |
| 1957 |
static void HandlePieMenuMouseSelection(void) |
| 1958 |
{ |
| 1959 |
if (!CurrentButtons) { // no buttons |
| 1960 |
return; |
| 1961 |
} |
| 1962 |
|
| 1963 |
int pie = GetPieUnderCursor(); |
| 1964 |
if (pie != -1) { |
| 1965 |
UI.ButtonPanel.DoClicked(pie); |
| 1966 |
if (CurrentButtons[pie].Action == ButtonButton) { |
| 1967 |
// there is a submenu => stay in piemenu mode |
| 1968 |
// and recenter the piemenu around the cursor |
| 1969 |
CursorStartX = CursorX; |
| 1970 |
CursorStartY = CursorY; |
| 1971 |
} else { |
| 1972 |
if (CursorState == CursorStatePieMenu) { |
| 1973 |
CursorState = CursorStatePoint; |
| 1974 |
} |
| 1975 |
CursorOn = CursorOnUnknown; |
| 1976 |
UIHandleMouseMove(CursorX, CursorY); // recompute CursorOn and company |
| 1977 |
} |
| 1978 |
} else { |
| 1979 |
CursorState = CursorStatePoint; |
| 1980 |
CursorOn = CursorOnUnknown; |
| 1981 |
UIHandleMouseMove(CursorX, CursorY); // recompute CursorOn and company |
| 1982 |
} |
| 1983 |
} |
| 1984 |
//@} |