Some clean up.
This commit is contained in:
parent
0f29012a23
commit
c911d001d0
5 changed files with 210 additions and 209 deletions
|
@ -724,7 +724,7 @@ int stratagusMain(int argc, char **argv)
|
|||
|
||||
// Init player data
|
||||
ThisPlayer = NULL;
|
||||
//Don't clear the Players strucure as it would erase the allowed units.
|
||||
//Don't clear the Players structure as it would erase the allowed units.
|
||||
// memset(Players, 0, sizeof(Players));
|
||||
NumPlayers = 0;
|
||||
|
||||
|
|
|
@ -82,14 +82,12 @@ CIcon::~CIcon()
|
|||
*/
|
||||
/* static */ CIcon *CIcon::New(const std::string &ident)
|
||||
{
|
||||
CIcon *icon = Icons[ident];
|
||||
if (icon) {
|
||||
return icon;
|
||||
} else {
|
||||
CIcon *&icon = Icons[ident];
|
||||
|
||||
if (icon == NULL) {
|
||||
icon = new CIcon(ident);
|
||||
Icons[ident] = icon;
|
||||
return icon;
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -101,11 +99,11 @@ CIcon::~CIcon()
|
|||
*/
|
||||
/* static */ CIcon *CIcon::Get(const std::string &ident)
|
||||
{
|
||||
CIcon *icon = Icons[ident];
|
||||
if (!icon) {
|
||||
IconMap::iterator it = Icons.find(ident);
|
||||
if (it == Icons.end()) {
|
||||
DebugPrint("icon not found: %s\n" _C_ ident.c_str());
|
||||
}
|
||||
return icon;
|
||||
return it->second;
|
||||
}
|
||||
|
||||
void CIcon::Load()
|
||||
|
@ -156,8 +154,8 @@ void CIcon::DrawCooldownSpellIcon(const PixelPos &pos, const int percent) const
|
|||
// TO-DO: implement more effect types (clock-like)
|
||||
this->GScale->DrawFrameClip(this->Frame, pos.x, pos.y);
|
||||
const int height = (G->Height * (100 - percent)) / 100;
|
||||
this->G->DrawSubClip(G->frame_map[Frame].x, G->frame_map[Frame].y + G->Height - height, G->Width,
|
||||
height, pos.x, pos.y + G->Height - height);
|
||||
this->G->DrawSubClip(G->frame_map[Frame].x, G->frame_map[Frame].y + G->Height - height,
|
||||
G->Width, height, pos.x, pos.y + G->Height - height);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -168,8 +166,8 @@ void CIcon::DrawCooldownSpellIcon(const PixelPos &pos, const int percent) const
|
|||
** @param pos display pixel position
|
||||
** @param text Optional text to display
|
||||
*/
|
||||
void CIcon::DrawUnitIcon(const ButtonStyle &style,
|
||||
unsigned flags, const PixelPos &pos, const std::string &text) const
|
||||
void CIcon::DrawUnitIcon(const ButtonStyle &style, unsigned flags,
|
||||
const PixelPos &pos, const std::string &text) const
|
||||
{
|
||||
ButtonStyle s(style);
|
||||
|
||||
|
|
|
@ -97,10 +97,8 @@ CUnit *LastIdleWorker; /// Last called idle worker
|
|||
*/
|
||||
static void ShowInput()
|
||||
{
|
||||
char *input;
|
||||
|
||||
snprintf(InputStatusLine, sizeof(InputStatusLine), _("MESSAGE:%s~!_"), Input);
|
||||
input = InputStatusLine;
|
||||
char *input = InputStatusLine;
|
||||
// FIXME: This is slow!
|
||||
while (UI.StatusLine.Font->Width(input) > UI.StatusLine.Width) {
|
||||
++input;
|
||||
|
@ -433,8 +431,10 @@ void UiFindIdleWorker()
|
|||
}
|
||||
CUnit *unit = ThisPlayer->FreeWorkers[0];
|
||||
if (LastIdleWorker) {
|
||||
std::vector<CUnit *>::const_iterator it = std::find(ThisPlayer->FreeWorkers.begin(),
|
||||
ThisPlayer->FreeWorkers.end(), LastIdleWorker);
|
||||
const std::vector<CUnit *>& freeWorkers = ThisPlayer->FreeWorkers;
|
||||
std::vector<CUnit *>::const_iterator it = std::find(freeWorkers.begin(),
|
||||
freeWorkers.end(),
|
||||
LastIdleWorker);
|
||||
if (it != ThisPlayer->FreeWorkers.end()) {
|
||||
if (*it != ThisPlayer->FreeWorkers.back()) {
|
||||
unit = *(++it);
|
||||
|
@ -486,7 +486,6 @@ void UiTrackUnit()
|
|||
*/
|
||||
bool HandleCommandKey(int key)
|
||||
{
|
||||
bool ret;
|
||||
int base = lua_gettop(Lua);
|
||||
|
||||
lua_getglobal(Lua, "HandleCommandKey");
|
||||
|
@ -500,13 +499,13 @@ bool HandleCommandKey(int key)
|
|||
lua_pushboolean(Lua, (KeyModifiers & ModifierShift));
|
||||
LuaCall(4, 0);
|
||||
if (lua_gettop(Lua) - base == 1) {
|
||||
ret = LuaToBoolean(Lua, base + 1);
|
||||
bool ret = LuaToBoolean(Lua, base + 1);
|
||||
lua_pop(Lua, 1);
|
||||
return ret;
|
||||
} else {
|
||||
LuaError(Lua, "HandleCommandKey must return a boolean");
|
||||
ret = false;
|
||||
return false;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
|
@ -746,8 +745,6 @@ static bool CommandKey(int key)
|
|||
*/
|
||||
int HandleCheats(const std::string &input)
|
||||
{
|
||||
int ret;
|
||||
|
||||
#if defined(DEBUG) || defined(PROF)
|
||||
if (input == "ai me") {
|
||||
if (ThisPlayer->AiEnabled) {
|
||||
|
@ -781,13 +778,42 @@ int HandleCheats(const std::string &input)
|
|||
lua_pushstring(Lua, input.c_str());
|
||||
LuaCall(1, 0, false);
|
||||
if (lua_gettop(Lua) - base == 1) {
|
||||
ret = LuaToBoolean(Lua, -1);
|
||||
int ret = LuaToBoolean(Lua, -1);
|
||||
lua_pop(Lua, 1);
|
||||
return ret;
|
||||
} else {
|
||||
DebugPrint("HandleCheats must return a boolean");
|
||||
ret = 0;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Replace ~~ with ~
|
||||
static void Replace2TildeByTilde(char *s)
|
||||
{
|
||||
for (char *p = s; *p; ++p) {
|
||||
if (*p == '~') {
|
||||
++p;
|
||||
}
|
||||
*s++ = *p;
|
||||
}
|
||||
*s = '\0';
|
||||
}
|
||||
|
||||
// Replace ~ with ~~
|
||||
static void ReplaceTildeBy2Tilde(char *s)
|
||||
{
|
||||
for (char *p = s; *p; ++p) {
|
||||
if (*p != '~') {
|
||||
continue;
|
||||
}
|
||||
char *q = p + strlen(p);
|
||||
q[1] = '\0';
|
||||
while (q > p) {
|
||||
*q = *(q - 1);
|
||||
--q;
|
||||
}
|
||||
++p;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -798,22 +824,11 @@ int HandleCheats(const std::string &input)
|
|||
*/
|
||||
static int InputKey(int key)
|
||||
{
|
||||
int i;
|
||||
char *namestart;
|
||||
char *p;
|
||||
char *q;
|
||||
|
||||
switch (key) {
|
||||
case SDLK_RETURN:
|
||||
case SDLK_KP_ENTER: { // RETURN
|
||||
// Replace ~~ with ~
|
||||
for (p = q = Input; *p;) {
|
||||
if (*p == '~') {
|
||||
++p;
|
||||
}
|
||||
*q++ = *p++;
|
||||
}
|
||||
*q = '\0';
|
||||
Replace2TildeByTilde(Input);
|
||||
#ifdef DEBUG
|
||||
if (Input[0] == '-') {
|
||||
if (!GameObserve && !GamePaused) {
|
||||
|
@ -841,17 +856,7 @@ static int InputKey(int key)
|
|||
|
||||
if (Input[0]) {
|
||||
// Replace ~ with ~~
|
||||
for (p = Input; *p; ++p) {
|
||||
if (*p == '~') {
|
||||
q = p + strlen(p);
|
||||
q[1] = '\0';
|
||||
while (q > p) {
|
||||
*q = *(q - 1);
|
||||
--q;
|
||||
}
|
||||
++p;
|
||||
}
|
||||
}
|
||||
ReplaceTildeBy2Tilde(Input);
|
||||
char chatMessage[sizeof(Input) + 40];
|
||||
snprintf(chatMessage, sizeof(chatMessage), "~%s~<%s>~> %s",
|
||||
PlayerColorNames[ThisPlayer->Index].c_str(),
|
||||
|
@ -882,8 +887,8 @@ static int InputKey(int key)
|
|||
}
|
||||
return 1;
|
||||
|
||||
case SDLK_TAB:
|
||||
namestart = strrchr(Input, ' ');
|
||||
case SDLK_TAB: {
|
||||
char *namestart = strrchr(Input, ' ');
|
||||
if (namestart) {
|
||||
++namestart;
|
||||
} else {
|
||||
|
@ -892,7 +897,7 @@ static int InputKey(int key)
|
|||
if (!strlen(namestart)) {
|
||||
return 1;
|
||||
}
|
||||
for (i = 0; i < PlayerMax; ++i) {
|
||||
for (int i = 0; i < PlayerMax; ++i) {
|
||||
if (Players[i].Type != PlayerPerson) {
|
||||
continue;
|
||||
}
|
||||
|
@ -907,7 +912,7 @@ static int InputKey(int key)
|
|||
}
|
||||
}
|
||||
return 1;
|
||||
|
||||
}
|
||||
default:
|
||||
if (key >= ' ') {
|
||||
gcn::Key k(key);
|
||||
|
@ -940,9 +945,8 @@ static void Screenshot()
|
|||
{
|
||||
CFile fd;
|
||||
char filename[30];
|
||||
int i;
|
||||
|
||||
for (i = 1; i <= 99; ++i) {
|
||||
for (int i = 1; i <= 99; ++i) {
|
||||
// FIXME: what if we can't write to this directory?
|
||||
snprintf(filename, sizeof(filename), "screen%02d.png", i);
|
||||
if (fd.open(filename, CL_OPEN_READ) == -1) {
|
||||
|
@ -1145,8 +1149,7 @@ void HandleKeyRepeat(unsigned, unsigned keychar)
|
|||
/**
|
||||
** Handle the mouse in scroll area
|
||||
**
|
||||
** @param x Screen X position.
|
||||
** @param y Screen Y position.
|
||||
** @param mousePos Screen position.
|
||||
**
|
||||
** @return true if the mouse is in the scroll area, false otherwise
|
||||
*/
|
||||
|
@ -1214,8 +1217,7 @@ void HandleCursorMove(int *x, int *y)
|
|||
/**
|
||||
** Handle movement of the cursor.
|
||||
**
|
||||
** @param x screen pixel X position.
|
||||
** @param y screen pixel Y position.
|
||||
** @param screePos screen pixel position.
|
||||
*/
|
||||
void HandleMouseMove(const PixelPos &screenPos)
|
||||
{
|
||||
|
@ -1252,23 +1254,22 @@ void HandleButtonUp(unsigned button)
|
|||
----------------------------------------------------------------------------*/
|
||||
|
||||
#ifdef USE_TOUCHSCREEN
|
||||
int DoubleClickDelay = 1000; /// Time to detect double clicks.
|
||||
int HoldClickDelay = 2000; /// Time to detect hold clicks.
|
||||
int DoubleClickDelay = 1000; /// Time to detect double clicks.
|
||||
int HoldClickDelay = 2000; /// Time to detect hold clicks.
|
||||
#else
|
||||
int DoubleClickDelay = 300; /// Time to detect double clicks.
|
||||
int HoldClickDelay = 1000; /// Time to detect hold clicks.
|
||||
int DoubleClickDelay = 300; /// Time to detect double clicks.
|
||||
int HoldClickDelay = 1000; /// Time to detect hold clicks.
|
||||
#endif
|
||||
|
||||
static enum {
|
||||
InitialMouseState, /// start state
|
||||
ClickedMouseState /// button is clicked
|
||||
} MouseState; /// Current state of mouse
|
||||
InitialMouseState, /// start state
|
||||
ClickedMouseState /// button is clicked
|
||||
} MouseState; /// Current state of mouse
|
||||
|
||||
static int MouseX; /// Last mouse X position
|
||||
static int MouseY; /// Last mouse Y position
|
||||
static unsigned LastMouseButton; /// last mouse button handled
|
||||
static unsigned StartMouseTicks; /// Ticks of first click
|
||||
static unsigned LastMouseTicks; /// Ticks of last mouse event
|
||||
static PixelPos LastMousePos; /// Last mouse position
|
||||
static unsigned LastMouseButton; /// last mouse button handled
|
||||
static unsigned StartMouseTicks; /// Ticks of first click
|
||||
static unsigned LastMouseTicks; /// Ticks of last mouse event
|
||||
|
||||
/**
|
||||
** Called if any mouse button is pressed down
|
||||
|
@ -1348,35 +1349,31 @@ void InputMouseButtonRelease(const EventCallback &callbacks,
|
|||
void InputMouseMove(const EventCallback &callbacks,
|
||||
unsigned ticks, int x, int y)
|
||||
{
|
||||
PixelPos mousePos(x, y);
|
||||
// Don't reset the mouse state unless we really moved
|
||||
#ifdef USE_TOUCHSCREEN
|
||||
const int buff = 32;
|
||||
if (((x - buff) <= MouseX && MouseX <= (x + buff)) == 0
|
||||
|| ((y - buff) <= MouseY && MouseY <= (y + buff)) == 0) {
|
||||
const PixelDiff diff = LastMousePos - mousePos;
|
||||
|
||||
if (abs(diff.x) > buff || abs (diff.y) > buff) {
|
||||
MouseState = InitialMouseState;
|
||||
LastMouseTicks = ticks;
|
||||
// Reset rectangle select cursor state if we moved by a lot
|
||||
// - rectangle select should be a drag, not a tap
|
||||
if (CursorState == CursorStateRectangle
|
||||
&& (((x - buff * 2) <= MouseX && MouseX <= (x + buff * 2)) == 0
|
||||
|| ((y - buff * 2) <= MouseY && MouseY <= (y + buff * 2)) == 0)) {
|
||||
&& (abs(diff.x) > 2 * buff || abs (diff.y) > 2 * buff)) {
|
||||
CursorState = CursorStatePoint;
|
||||
}
|
||||
}
|
||||
if (MouseX != x || MouseY != y) {
|
||||
MouseX = x;
|
||||
MouseY = y;
|
||||
}
|
||||
LastMousePos = mousePos;
|
||||
#else
|
||||
if (MouseX != x || MouseY != y) {
|
||||
if (LastMousePos != mousePos) {
|
||||
MouseState = InitialMouseState;
|
||||
LastMouseTicks = ticks;
|
||||
MouseX = x;
|
||||
MouseY = y;
|
||||
LastMousePos = mousePos;
|
||||
}
|
||||
#endif
|
||||
const PixelPos pos(x, y);
|
||||
callbacks.MouseMoved(pos);
|
||||
callbacks.MouseMoved(mousePos);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1384,7 +1381,6 @@ void InputMouseMove(const EventCallback &callbacks,
|
|||
**
|
||||
** @param callbacks Callback structure for events.
|
||||
** @param ticks Denotes time-stamp of video-system
|
||||
**
|
||||
*/
|
||||
void InputMouseExit(const EventCallback &callbacks, unsigned /* ticks */)
|
||||
{
|
||||
|
|
|
@ -99,7 +99,6 @@ static void DrawMenuButtonArea_Network()
|
|||
UI.NetworkDiplomacyButton.X, UI.NetworkDiplomacyButton.Y,
|
||||
UI.NetworkDiplomacyButton.Text);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -453,21 +452,23 @@ static void DrawUnitInfo_transporter(CUnit &unit)
|
|||
size_t j = 0;
|
||||
|
||||
for (int i = 0; i < unit.InsideCount; ++i, uins = uins->NextContained) {
|
||||
if (uins->Boarded && j < UI.TransportingButtons.size()) {
|
||||
CIcon &icon = *uins->Type->Icon.Icon;
|
||||
int flag = (ButtonAreaUnderCursor == ButtonAreaTransporting && static_cast<size_t>(ButtonUnderCursor) == j) ?
|
||||
(IconActive | (MouseButtons & LeftButton)) : 0;
|
||||
const PixelPos pos(UI.TransportingButtons[j].X, UI.TransportingButtons[j].Y);
|
||||
icon.DrawUnitIcon(*UI.TransportingButtons[j].Style, flag, pos, "");
|
||||
UiDrawLifeBar(*uins, pos.x, pos.y);
|
||||
if (uins->Type->CanCastSpell && uins->Variable[MANA_INDEX].Max) {
|
||||
UiDrawManaBar(*uins, pos.x, pos.y);
|
||||
}
|
||||
if (ButtonAreaUnderCursor == ButtonAreaTransporting && static_cast<size_t>(ButtonUnderCursor) == j) {
|
||||
UI.StatusLine.Set(uins->Type->Name);
|
||||
}
|
||||
++j;
|
||||
if (!uins->Boarded || j >= UI.TransportingButtons.size()) {
|
||||
continue;
|
||||
}
|
||||
CIcon &icon = *uins->Type->Icon.Icon;
|
||||
int flag = (ButtonAreaUnderCursor == ButtonAreaTransporting && static_cast<size_t>(ButtonUnderCursor) == j) ?
|
||||
(IconActive | (MouseButtons & LeftButton)) : 0;
|
||||
const PixelPos pos(UI.TransportingButtons[j].X, UI.TransportingButtons[j].Y);
|
||||
icon.DrawUnitIcon(*UI.TransportingButtons[j].Style, flag, pos, "");
|
||||
UiDrawLifeBar(*uins, pos.x, pos.y);
|
||||
if (uins->Type->CanCastSpell && uins->Variable[MANA_INDEX].Max) {
|
||||
UiDrawManaBar(*uins, pos.x, pos.y);
|
||||
}
|
||||
if (ButtonAreaUnderCursor == ButtonAreaTransporting
|
||||
&& static_cast<size_t>(ButtonUnderCursor) == j) {
|
||||
UI.StatusLine.Set(uins->Type->Name);
|
||||
}
|
||||
++j;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
220
src/ui/mouse.cpp
220
src/ui/mouse.cpp
|
@ -106,17 +106,15 @@ void CancelBuildingMode()
|
|||
|
||||
static bool CanBuildOnArea(const CUnit &unit, const Vec2i &pos)
|
||||
{
|
||||
bool result = true;
|
||||
for (int j = 0; result && j < unit.Type->TileHeight; ++j) {
|
||||
for (int j = 0; j < unit.Type->TileHeight; ++j) {
|
||||
for (int i = 0; i < unit.Type->TileWidth; ++i) {
|
||||
const Vec2i tempPos(i, j);
|
||||
if (!Map.Field(pos + tempPos)->playerInfo.IsExplored(*ThisPlayer)) {
|
||||
result = false;
|
||||
break;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void DoRightButton_ForForeignUnit(CUnit *dest)
|
||||
|
@ -200,7 +198,64 @@ static bool DoRightButton_Transporter(CUnit &unit, CUnit *dest, int flush, int &
|
|||
return false;
|
||||
}
|
||||
|
||||
static bool DoRightButton_Harvest(CUnit &unit, CUnit *dest, const Vec2i &pos, int flush, int &acknowledged)
|
||||
static bool DoRightButton_Harvest_Unit(CUnit &unit, CUnit &dest, int flush, int &acknowledged)
|
||||
{
|
||||
// Return a loaded harvester to deposit
|
||||
if (unit.ResourcesHeld > 0
|
||||
&& dest.Type->CanStore[unit.CurrentResource]
|
||||
&& dest.Player == unit.Player) {
|
||||
dest.Blink = 4;
|
||||
if (!acknowledged) {
|
||||
PlayUnitSound(unit, VoiceAcknowledging);
|
||||
acknowledged = 1;
|
||||
}
|
||||
SendCommandReturnGoods(unit, &dest, flush);
|
||||
return true;
|
||||
}
|
||||
// Go and harvest from a unit
|
||||
const int res = dest.Type->GivesResource;
|
||||
const CUnitType& type = *unit.Type;
|
||||
if (res
|
||||
&& type.ResInfo[res]
|
||||
&& unit.ResourcesHeld < type.ResInfo[res]->ResourceCapacity
|
||||
&& dest.Type->CanHarvest
|
||||
&& (dest.Player == unit.Player || dest.Player->Index == PlayerNumNeutral)) {
|
||||
dest.Blink = 4;
|
||||
if (!acknowledged) {
|
||||
PlayUnitSound(unit, VoiceAcknowledging);
|
||||
acknowledged = 1;
|
||||
}
|
||||
SendCommandResource(unit, dest, flush);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool DoRightButton_Harvest_Pos(CUnit &unit, const Vec2i &pos, int flush, int &acknowledged)
|
||||
{
|
||||
if (!Map.Field(pos)->playerInfo.IsExplored(*unit.Player)) {
|
||||
return false;
|
||||
}
|
||||
const CUnitType& type = *unit.Type;
|
||||
// FIXME: support harvesting more types of terrain.
|
||||
for (int res = 0; res < MaxCosts; ++res) {
|
||||
if (type.ResInfo[res]
|
||||
&& type.ResInfo[res]->TerrainHarvester
|
||||
&& Map.Field(pos)->IsTerrainResourceOnMap(res)
|
||||
&& ((unit.CurrentResource != res)
|
||||
|| (unit.ResourcesHeld < type.ResInfo[res]->ResourceCapacity))) {
|
||||
if (!acknowledged) {
|
||||
PlayUnitSound(unit, VoiceAcknowledging);
|
||||
acknowledged = 1;
|
||||
}
|
||||
SendCommandResourceLoc(unit, pos, flush);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool DoRightButton_Worker(CUnit &unit, CUnit *dest, const Vec2i &pos, int flush, int &acknowledged)
|
||||
{
|
||||
const CUnitType &type = *unit.Type;
|
||||
|
||||
|
@ -220,50 +275,12 @@ static bool DoRightButton_Harvest(CUnit &unit, CUnit *dest, const Vec2i &pos, in
|
|||
// Harvest
|
||||
if (type.Harvester) {
|
||||
if (dest != NULL) {
|
||||
// Return a loaded harvester to deposit
|
||||
if (unit.ResourcesHeld > 0
|
||||
&& dest->Type->CanStore[unit.CurrentResource]
|
||||
&& dest->Player == unit.Player) {
|
||||
dest->Blink = 4;
|
||||
if (!acknowledged) {
|
||||
PlayUnitSound(unit, VoiceAcknowledging);
|
||||
acknowledged = 1;
|
||||
}
|
||||
SendCommandReturnGoods(unit, dest, flush);
|
||||
return true;
|
||||
}
|
||||
// Go and harvest from a unit
|
||||
const int res = dest->Type->GivesResource;
|
||||
if (res
|
||||
&& type.ResInfo[res]
|
||||
&& unit.ResourcesHeld < type.ResInfo[res]->ResourceCapacity
|
||||
&& dest->Type->CanHarvest
|
||||
&& (dest->Player == unit.Player || dest->Player->Index == PlayerNumNeutral)) {
|
||||
dest->Blink = 4;
|
||||
if (!acknowledged) {
|
||||
PlayUnitSound(unit, VoiceAcknowledging);
|
||||
acknowledged = 1;
|
||||
}
|
||||
SendCommandResource(unit, *dest, flush);
|
||||
if (DoRightButton_Harvest_Unit(unit, *dest, flush, acknowledged)) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if (Map.Field(pos)->playerInfo.IsExplored(*unit.Player)) {
|
||||
// FIXME: support harvesting more types of terrain.
|
||||
for (int res = 0; res < MaxCosts; ++res) {
|
||||
if (type.ResInfo[res]
|
||||
&& type.ResInfo[res]->TerrainHarvester
|
||||
&& Map.Field(pos)->IsTerrainResourceOnMap(res)
|
||||
&& ((unit.CurrentResource != res)
|
||||
|| (unit.ResourcesHeld < type.ResInfo[res]->ResourceCapacity))) {
|
||||
if (!acknowledged) {
|
||||
PlayUnitSound(unit, VoiceAcknowledging);
|
||||
acknowledged = 1;
|
||||
}
|
||||
SendCommandResourceLoc(unit, pos, flush);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (DoRightButton_Harvest_Pos(unit, pos, flush, acknowledged)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -291,45 +308,53 @@ static bool DoRightButton_Harvest(CUnit &unit, CUnit *dest, const Vec2i &pos, in
|
|||
return true;
|
||||
}
|
||||
|
||||
static void DoRightButton_Attack(CUnit &unit, CUnit *dest, const Vec2i &pos, int flush, int &acknowledged)
|
||||
static bool DoRightButton_AttackUnit(CUnit &unit, CUnit &dest, const Vec2i &pos, int flush, int &acknowledged)
|
||||
{
|
||||
const CUnitType &type = *unit.Type;
|
||||
const int action = type.MouseAction;
|
||||
|
||||
if (dest != NULL && unit.CurrentAction() != UnitActionBuilt) {
|
||||
if (action == MouseActionSpellCast || unit.IsEnemy(*dest)) {
|
||||
dest->Blink = 4;
|
||||
if (!acknowledged) {
|
||||
PlayUnitSound(unit, VoiceAttack);
|
||||
acknowledged = 1;
|
||||
}
|
||||
if (action == MouseActionSpellCast) {
|
||||
// This is for demolition squads and such
|
||||
Assert(unit.Type->CanCastSpell);
|
||||
size_t spellnum;
|
||||
for (spellnum = 0; !type.CanCastSpell[spellnum] && spellnum < SpellTypeTable.size() ; spellnum++) {
|
||||
}
|
||||
SendCommandSpellCast(unit, pos, dest, spellnum, flush);
|
||||
} else {
|
||||
if (CanTarget(type, *dest->Type)) {
|
||||
SendCommandAttack(unit, pos, dest, flush);
|
||||
} else { // No valid target
|
||||
SendCommandAttack(unit, pos, NoUnitP, flush);
|
||||
}
|
||||
}
|
||||
return;
|
||||
if (action == MouseActionSpellCast || unit.IsEnemy(dest)) {
|
||||
dest.Blink = 4;
|
||||
if (!acknowledged) {
|
||||
PlayUnitSound(unit, VoiceAttack);
|
||||
acknowledged = 1;
|
||||
}
|
||||
if ((dest->Player == unit.Player || unit.IsAllied(*dest)) && dest != &unit) {
|
||||
dest->Blink = 4;
|
||||
if (!acknowledged) {
|
||||
PlayUnitSound(unit, VoiceAcknowledging);
|
||||
acknowledged = 1;
|
||||
if (action == MouseActionSpellCast) {
|
||||
// This is for demolition squads and such
|
||||
Assert(unit.Type->CanCastSpell);
|
||||
size_t spellnum;
|
||||
for (spellnum = 0; !type.CanCastSpell[spellnum] && spellnum < SpellTypeTable.size() ; spellnum++) {
|
||||
}
|
||||
if (dest->Type->CanMove() == false && !dest->Type->Teleporter) {
|
||||
SendCommandMove(unit, pos, flush);
|
||||
} else {
|
||||
SendCommandFollow(unit, *dest, flush);
|
||||
SendCommandSpellCast(unit, pos, &dest, spellnum, flush);
|
||||
} else {
|
||||
if (CanTarget(type, *dest.Type)) {
|
||||
SendCommandAttack(unit, pos, &dest, flush);
|
||||
} else { // No valid target
|
||||
SendCommandAttack(unit, pos, NoUnitP, flush);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if ((dest.Player == unit.Player || unit.IsAllied(dest)) && &dest != &unit) {
|
||||
dest.Blink = 4;
|
||||
if (!acknowledged) {
|
||||
PlayUnitSound(unit, VoiceAcknowledging);
|
||||
acknowledged = 1;
|
||||
}
|
||||
if (!dest.Type->CanMove() && !dest.Type->Teleporter) {
|
||||
SendCommandMove(unit, pos, flush);
|
||||
} else {
|
||||
SendCommandFollow(unit, dest, flush);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void DoRightButton_Attack(CUnit &unit, CUnit *dest, const Vec2i &pos, int flush, int &acknowledged)
|
||||
{
|
||||
if (dest != NULL && unit.CurrentAction() != UnitActionBuilt) {
|
||||
if (DoRightButton_AttackUnit(unit, *dest, pos, flush, acknowledged)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -501,7 +526,7 @@ static void DoRightButton_ForSelectedUnit(CUnit &unit, CUnit *dest, const Vec2i
|
|||
|
||||
// Handle resource workers.
|
||||
if (action == MouseActionHarvest) {
|
||||
DoRightButton_Harvest(unit, dest, pos, flush, acknowledged);
|
||||
DoRightButton_Worker(unit, dest, pos, flush, acknowledged);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -595,10 +620,9 @@ void DoRightButton(const PixelPos &mapPixelPos)
|
|||
/**
|
||||
** Check if the mouse is on a button
|
||||
**
|
||||
** @param x X screen coordinate.
|
||||
** @param y Y screen coordinate.
|
||||
** @param screenPos screen coordinate.
|
||||
**
|
||||
** @return True if mouse is on the button, False otherwise.
|
||||
** @return True if mouse is on the button, False otherwise.
|
||||
*/
|
||||
bool CUIButton::Contains(const PixelPos &screenPos) const
|
||||
{
|
||||
|
@ -855,16 +879,14 @@ static void MouseScrollMap(const PixelPos &pos)
|
|||
/**
|
||||
** Handle movement of the cursor.
|
||||
**
|
||||
** @param mousePos Screen X position.
|
||||
** @param cursorPos Screen X position.
|
||||
*/
|
||||
void UIHandleMouseMove(const PixelPos &cursorPos)
|
||||
{
|
||||
enum _cursor_on_ OldCursorOn;
|
||||
|
||||
OldCursorOn = CursorOn;
|
||||
//
|
||||
// Selecting units.
|
||||
//
|
||||
if (CursorState == CursorStateRectangle) {
|
||||
// Restrict cursor to viewport.
|
||||
UI.SelectedViewport->Restrict(CursorScreenPos.x, CursorScreenPos.y);
|
||||
|
@ -872,9 +894,7 @@ void UIHandleMouseMove(const PixelPos &cursorPos)
|
|||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Move map.
|
||||
//
|
||||
if (GameCursor == UI.Scroll.Cursor) {
|
||||
MouseScrollMap(cursorPos);
|
||||
return;
|
||||
|
@ -884,9 +904,7 @@ void UIHandleMouseMove(const PixelPos &cursorPos)
|
|||
GameCursor = UI.Point.Cursor; // Reset
|
||||
HandleMouseOn(cursorPos);
|
||||
|
||||
//
|
||||
// Make the piemenu "follow" the mouse
|
||||
//
|
||||
if (CursorState == CursorStatePieMenu && CursorOn == CursorOnMap) {
|
||||
clamp(&CursorStartScreenPos.x, CursorScreenPos.x - UI.PieMenu.X[2], CursorScreenPos.x + UI.PieMenu.X[2]);
|
||||
clamp(&CursorStartScreenPos.y, CursorScreenPos.y - UI.PieMenu.Y[4], CursorScreenPos.y + UI.PieMenu.Y[4]);
|
||||
|
@ -902,9 +920,7 @@ void UIHandleMouseMove(const PixelPos &cursorPos)
|
|||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// User may be draging with button pressed.
|
||||
//
|
||||
if (GameMenuButtonClicked || GameDiplomacyButtonClicked) {
|
||||
return;
|
||||
} else {
|
||||
|
@ -992,9 +1008,7 @@ void UIHandleMouseMove(const PixelPos &cursorPos)
|
|||
UnitUnderCursor = NULL;
|
||||
}
|
||||
|
||||
//
|
||||
// Selecting target.
|
||||
//
|
||||
if (CursorState == CursorStateSelect) {
|
||||
if (CursorOn == CursorOnMap || CursorOn == CursorOnMinimap) {
|
||||
if (CustomCursor.length() && CursorByIdent(CustomCursor)) {
|
||||
|
@ -1020,9 +1034,7 @@ void UIHandleMouseMove(const PixelPos &cursorPos)
|
|||
}
|
||||
if (CursorOn == CursorOnMinimap && (MouseButtons & RightButton)) {
|
||||
const Vec2i cursorPos = UI.Minimap.ScreenToTilePos(CursorScreenPos);
|
||||
//
|
||||
// Minimap move viewpoint
|
||||
//
|
||||
UI.SelectedViewport->Center(Map.TilePosToMapPixelPos_Center(cursorPos));
|
||||
}
|
||||
}
|
||||
|
@ -1030,13 +1042,9 @@ void UIHandleMouseMove(const PixelPos &cursorPos)
|
|||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Cursor pointing.
|
||||
//
|
||||
if (CursorOn == CursorOnMap) {
|
||||
//
|
||||
// Map
|
||||
//
|
||||
if (UnitUnderCursor != NULL && !UnitUnderCursor->Type->Decoration
|
||||
&& (UnitUnderCursor->IsVisible(*ThisPlayer) || ReplayRevealMap)) {
|
||||
GameCursor = UI.Glass.Cursor;
|
||||
|
@ -1045,9 +1053,7 @@ void UIHandleMouseMove(const PixelPos &cursorPos)
|
|||
}
|
||||
|
||||
if (CursorOn == CursorOnMinimap && (MouseButtons & LeftButton)) {
|
||||
//
|
||||
// Minimap move viewpoint
|
||||
//
|
||||
const Vec2i cursorPos = UI.Minimap.ScreenToTilePos(CursorScreenPos);
|
||||
|
||||
UI.SelectedViewport->Center(Map.TilePosToMapPixelPos_Center(cursorPos));
|
||||
|
@ -1072,7 +1078,7 @@ static int SendRepair(const Vec2i &tilePos)
|
|||
if (dest && dest->Variable[HP_INDEX].Value < dest->Variable[HP_INDEX].Max
|
||||
&& dest->Type->RepairHP
|
||||
&& (dest->Player == ThisPlayer || ThisPlayer->IsAllied(*dest))) {
|
||||
for (size_t i = 0; i != Selected.size(); ++i) {
|
||||
for (size_t i = 0; i != Selected.size(); ++i) {
|
||||
CUnit *unit = Selected[i];
|
||||
|
||||
if (unit->Type->RepairRange) {
|
||||
|
@ -1754,7 +1760,6 @@ static void UIHandleButtonDown_OnButton(unsigned button)
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
** Called if mouse button pressed down.
|
||||
**
|
||||
|
@ -1766,7 +1771,8 @@ void UIHandleButtonDown(unsigned button)
|
|||
const bool longLeftButton = MouseButtons & ((LeftButton << MouseHoldShift));
|
||||
|
||||
#ifdef USE_TOUCHSCREEN
|
||||
// If we are moving with stylus/finger, left button on touch screen devices is still clicked
|
||||
// If we are moving with stylus/finger,
|
||||
// left button on touch screen devices is still clicked
|
||||
// Ignore handle if left button is long cliked
|
||||
if (longLeftButton) {
|
||||
return;
|
||||
|
@ -1854,8 +1860,8 @@ void UIHandleButtonUp(unsigned button)
|
|||
//
|
||||
if (CursorState == CursorStatePieMenu) {
|
||||
// Little threshold
|
||||
if (CursorStartScreenPos.x < CursorScreenPos.x - 1 || CursorScreenPos.x + 1 < CursorStartScreenPos.x
|
||||
|| CursorStartScreenPos.y < CursorScreenPos.y - 1 || CursorScreenPos.y + 1 < CursorStartScreenPos.y) {
|
||||
if (1 < std::abs(CursorStartScreenPos.x - CursorScreenPos.x)
|
||||
|| 1 < std::abs(CursorStartScreenPos.y - CursorScreenPos.y)) {
|
||||
// there was a move, handle the selected button/pie
|
||||
HandlePieMenuMouseSelection();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue