1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-28 23:06:24 +02:00
vcmi/client/adventureMap/CTerrainRect.cpp

416 lines
11 KiB
C++
Raw Normal View History

/*
2023-02-01 16:42:03 +02:00
* CTerrainRect.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "CTerrainRect.h"
#include "mapHandler.h"
#include "CAdvMapInt.h"
#include "../CGameInfo.h"
#include "../CPlayerInterface.h"
#include "../gui/CursorHandler.h"
#include "../gui/CGuiHandler.h"
#include "../render/CAnimation.h"
#include "../render/CFadeAnimation.h"
#include "../render/IImage.h"
#include "../renderSDL/SDL_Extensions.h"
#include "../widgets/TextControls.h"
#include "../CMT.h"
#include "../../CCallback.h"
#include "../../lib/CConfigHandler.h"
#include "../../lib/mapping/CMap.h"
#include "../../lib/CPathfinder.h"
#include <SDL_surface.h>
#define ADVOPT (conf.go()->ac)
CTerrainRect::CTerrainRect()
: fadeSurface(nullptr),
2016-11-27 18:00:16 +02:00
lastRedrawStatus(EMapAnimRedrawStatus::OK),
fadeAnim(std::make_shared<CFadeAnimation>()),
curHoveredTile(-1,-1,-1),
currentPath(nullptr)
{
tilesw=(ADVOPT.advmapW+31)/32;
tilesh=(ADVOPT.advmapH+31)/32;
pos.x=ADVOPT.advmapX;
pos.y=ADVOPT.advmapY;
pos.w=ADVOPT.advmapW;
pos.h=ADVOPT.advmapH;
moveX = moveY = 0;
addUsedEvents(LCLICK | RCLICK | MCLICK | HOVER | MOVE);
}
CTerrainRect::~CTerrainRect()
{
if(fadeSurface)
SDL_FreeSurface(fadeSurface);
}
void CTerrainRect::deactivate()
{
CIntObject::deactivate();
curHoveredTile = int3(-1,-1,-1); //we lost info about hovered tile when disabling
2011-02-23 20:21:51 +02:00
}
void CTerrainRect::clickLeft(tribool down, bool previousState)
{
if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
2015-01-13 21:57:41 +02:00
return;
if(indeterminate(down))
return;
2023-02-27 12:00:13 +02:00
#if defined(VCMI_MOBILE)
if(adventureInt->swipeEnabled)
{
2019-01-24 17:34:58 +02:00
if(handleSwipeStateChange((bool)down == true))
{
return; // if swipe is enabled, we don't process "down" events and wait for "up" (to make sure this wasn't a swiping gesture)
}
}
else
{
#endif
if(down == false)
return;
2023-02-27 12:00:13 +02:00
#if defined(VCMI_MOBILE)
}
#endif
int3 mp = whichTileIsIt();
if(mp.x < 0 || mp.y < 0 || mp.x >= LOCPLINT->cb->getMapSize().x || mp.y >= LOCPLINT->cb->getMapSize().y)
return;
adventureInt->tileLClicked(mp);
}
void CTerrainRect::clickRight(tribool down, bool previousState)
{
2023-02-27 12:00:13 +02:00
#if defined(VCMI_MOBILE)
if(adventureInt->swipeEnabled && isSwiping)
return;
#endif
if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
2015-01-13 21:57:41 +02:00
return;
int3 mp = whichTileIsIt();
if(CGI->mh->map->isInTheMap(mp) && down)
2011-05-26 01:44:02 +03:00
adventureInt->tileRClicked(mp);
}
2011-05-26 01:44:02 +03:00
void CTerrainRect::clickMiddle(tribool down, bool previousState)
{
2019-01-24 17:30:09 +02:00
handleSwipeStateChange((bool)down == true);
}
void CTerrainRect::mouseMoved(const Point & cursorPosition)
{
handleHover(cursorPosition);
if(!adventureInt->swipeEnabled)
return;
handleSwipeMove(cursorPosition);
}
void CTerrainRect::handleSwipeMove(const Point & cursorPosition)
{
2023-02-27 12:00:13 +02:00
#if defined(VCMI_MOBILE)
if(!GH.isMouseButtonPressed() || GH.multifinger) // any "button" is enough on mobile
return;
#else
if(!GH.isMouseButtonPressed(MouseButton::MIDDLE)) // swipe only works with middle mouse on other platforms
return;
#endif
if(!isSwiping)
{
// try to distinguish if this touch was meant to be a swipe or just fat-fingering press
if(abs(cursorPosition.x - swipeInitialRealPos.x) > SwipeTouchSlop ||
abs(cursorPosition.y - swipeInitialRealPos.y) > SwipeTouchSlop)
{
isSwiping = true;
}
}
if(isSwiping)
{
adventureInt->swipeTargetPosition.x =
swipeInitialMapPos.x + static_cast<si32>(swipeInitialRealPos.x - cursorPosition.x) / 32;
adventureInt->swipeTargetPosition.y =
swipeInitialMapPos.y + static_cast<si32>(swipeInitialRealPos.y - cursorPosition.y) / 32;
adventureInt->swipeMovementRequested = true;
}
}
bool CTerrainRect::handleSwipeStateChange(bool btnPressed)
{
if(btnPressed)
{
swipeInitialRealPos = Point(GH.getCursorPosition().x, GH.getCursorPosition().y);
swipeInitialMapPos = int3(adventureInt->position);
return true;
}
else if(isSwiping) // only accept this touch if it wasn't a swipe
{
isSwiping = false;
return true;
}
return false;
}
void CTerrainRect::handleHover(const Point & cursorPosition)
{
int3 tHovered = whichTileIsIt(cursorPosition.x, cursorPosition.y);
int3 pom = adventureInt->verifyPos(tHovered);
if(tHovered != pom) //tile outside the map
{
CCS->curh->set(Cursor::Map::POINTER);
return;
}
if (pom != curHoveredTile)
{
curHoveredTile = pom;
adventureInt->tileHovered(pom);
}
}
2007-10-07 17:51:09 +03:00
void CTerrainRect::hover(bool on)
{
if (!on)
{
2023-02-10 23:29:13 +02:00
GH.statusbar->clear();
CCS->curh->set(Cursor::Map::POINTER);
}
//Hoverable::hover(on);
2007-10-07 17:51:09 +03:00
}
void CTerrainRect::showPath(const Rect & extRect, SDL_Surface * to)
{
2011-05-21 11:44:02 +03:00
const static int pns[9][9] = {
{16, 17, 18, 7, -1, 19, 6, 5, -1},
{ 8, 9, 18, 7, -1, 19, 6, -1, 20},
{ 8, 1, 10, 7, -1, 19, -1, 21, 20},
{24, 17, 18, 15, -1, -1, 6, 5, 4},
{-1, -1, -1, -1, -1, -1, -1, -1, -1},
{ 8, 1, 2, -1, -1, 11, 22, 21, 20},
{24, 17, -1, 23, -1, 3, 14, 5, 4},
{24, -1, 2, 23, -1, 3, 22, 13, 4},
{-1, 1, 2, 23, -1, 3, 22, 21, 12}
}; //table of magic values TODO meaning, change variable name
for (int i = 0; i < -1 + (int)currentPath->nodes.size(); ++i)
{
const int3 &curPos = currentPath->nodes[i].coord, &nextPos = currentPath->nodes[i+1].coord;
if(curPos.z != adventureInt->position.z)
continue;
int pn=-1;//number of picture
if (i==0) //last tile
{
int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
if (x<0 || y<0 || x>pos.w || y>pos.h)
continue;
pn=0;
}
else
{
const int3 &prevPos = currentPath->nodes[i-1].coord;
std::vector<CGPathNode> & cv = currentPath->nodes;
/* Vector directions
* 0 1 2
* \ | /
* 3 - 4 - 5
* / | \
* 6 7 8
*For example:
* |
* |__\
* /
* is id1=7, id2=5 (pns[7][5])
*/
bool pathContinuous = curPos.areNeighbours(nextPos) && curPos.areNeighbours(prevPos);
if(pathContinuous && cv[i].action != CGPathNode::EMBARK && cv[i].action != CGPathNode::DISEMBARK)
{
int id1=(curPos.x-nextPos.x+1)+3*(curPos.y-nextPos.y+1); //Direction of entering vector
int id2=(cv[i-1].coord.x-curPos.x+1)+3*(cv[i-1].coord.y-curPos.y+1); //Direction of exiting vector
pn=pns[id1][id2];
}
else //path discontinuity or sea/land transition (eg. when moving through Subterranean Gate or Boat)
{
pn = 0;
}
}
if (currentPath->nodes[i].turns)
pn+=25;
if (pn>=0)
{
2018-03-30 13:02:04 +02:00
const auto arrow = graphics->heroMoveArrows->getImage(pn);
2016-10-18 09:31:31 +02:00
int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
if (x< -32 || y< -32 || x>pos.w || y>pos.h)
continue;
2016-10-18 09:31:31 +02:00
int hvx = (x + arrow->width()) - (pos.x + pos.w),
hvy = (y + arrow->height()) - (pos.y + pos.h);
Rect prevClip;
CSDL_Ext::getClipRect(to, prevClip);
CSDL_Ext::setClipRect(to, extRect); //preventing blitting outside of that rect
if(ADVOPT.smoothMove) //version for smooth hero move, with pos shifts
{
if (hvx<0 && hvy<0)
{
2016-10-18 09:31:31 +02:00
arrow->draw(to, x + moveX, y + moveY);
}
else if(hvx<0)
{
Rect srcRect (Point(0, 0), Point(arrow->height() - hvy, arrow->width()));
2016-10-18 09:31:31 +02:00
arrow->draw(to, x + moveX, y + moveY, &srcRect);
}
else if (hvy<0)
{
Rect srcRect (Point(0, 0), Point(arrow->height(), arrow->width() - hvx));
2016-10-18 09:31:31 +02:00
arrow->draw(to, x + moveX, y + moveY, &srcRect);
}
else
{
Rect srcRect (Point(0, 0), Point(arrow->height() - hvy, arrow->width() - hvx));
2016-10-18 09:31:31 +02:00
arrow->draw(to, x + moveX, y + moveY, &srcRect);
}
}
else //standard version
{
if (hvx<0 && hvy<0)
{
2016-10-18 09:31:31 +02:00
arrow->draw(to, x, y);
}
else if(hvx<0)
{
Rect srcRect (Point(0, 0), Point(arrow->height() - hvy, arrow->width()));
2016-10-18 09:31:31 +02:00
arrow->draw(to, x, y, &srcRect);
}
else if (hvy<0)
{
Rect srcRect (Point(0, 0), Point(arrow->height(), arrow->width() - hvx));
2016-10-18 09:31:31 +02:00
arrow->draw(to, x, y, &srcRect);
}
else
{
Rect srcRect (Point(0, 0), Point(arrow->height() - hvy, arrow->width() - hvx));
2016-10-18 09:31:31 +02:00
arrow->draw(to, x, y, &srcRect);
}
}
CSDL_Ext::setClipRect(to, prevClip);
}
} //for (int i=0;i<currentPath->nodes.size()-1;i++)
}
void CTerrainRect::show(SDL_Surface * to)
{
2015-01-13 21:57:41 +02:00
if (adventureInt->mode == EAdvMapMode::NORMAL)
{
MapDrawingInfo info(adventureInt->position, LOCPLINT->cb->getVisibilityMap(), pos);
info.otherheroAnim = true;
info.anim = adventureInt->anim;
info.heroAnim = adventureInt->heroAnim;
2015-01-13 21:57:41 +02:00
if (ADVOPT.smoothMove)
info.movement = int3(moveX, moveY, 0);
lastRedrawStatus = CGI->mh->drawTerrainRectNew(to, &info);
if (fadeAnim->isFading())
{
Rect r(pos);
fadeAnim->update();
fadeAnim->draw(to, r.topLeft());
}
2015-01-13 21:57:41 +02:00
if (currentPath/* && adventureInt->position.z==currentPath->startPos().z*/) //drawing path
{
showPath(pos, to);
2015-01-13 21:57:41 +02:00
}
}
}
void CTerrainRect::showAll(SDL_Surface * to)
{
// world view map is static and doesn't need redraw every frame
if (adventureInt->mode == EAdvMapMode::WORLD_VIEW)
{
MapDrawingInfo info(adventureInt->position, LOCPLINT->cb->getVisibilityMap(), pos, adventureInt->worldViewIcons);
info.scaled = true;
info.scale = adventureInt->worldViewScale;
adventureInt->worldViewOptions.adjustDrawingInfo(info);
CGI->mh->drawTerrainRectNew(to, &info);
}
}
void CTerrainRect::showAnim(SDL_Surface * to)
{
if (!needsAnimUpdate())
return;
if (fadeAnim->isFading() || lastRedrawStatus == EMapAnimRedrawStatus::REDRAW_REQUESTED)
show(to);
}
int3 CTerrainRect::whichTileIsIt(const int x, const int y)
2007-10-07 17:51:09 +03:00
{
int3 ret;
ret.x = adventureInt->position.x + ((x-CGI->mh->offsetX-pos.x)/32);
ret.y = adventureInt->position.y + ((y-CGI->mh->offsetY-pos.y)/32);
ret.z = adventureInt->position.z;
2007-10-07 17:51:09 +03:00
return ret;
}
2007-10-07 17:51:09 +03:00
int3 CTerrainRect::whichTileIsIt()
{
return whichTileIsIt(GH.getCursorPosition().x, GH.getCursorPosition().y);
2007-10-07 17:51:09 +03:00
}
2023-02-10 23:29:13 +02:00
Rect CTerrainRect::visibleTilesArea()
2015-01-13 21:57:41 +02:00
{
switch (adventureInt->mode)
{
default:
2017-08-11 13:38:10 +02:00
logGlobal->error("Unknown map mode %d", (int)adventureInt->mode);
2023-02-10 23:29:13 +02:00
return Rect();
2015-01-13 21:57:41 +02:00
case EAdvMapMode::NORMAL:
2023-02-10 23:29:13 +02:00
return Rect(adventureInt->position.x, adventureInt->position.y, tilesw, tilesh);
2015-01-13 21:57:41 +02:00
case EAdvMapMode::WORLD_VIEW:
2023-02-10 23:29:13 +02:00
return Rect(adventureInt->position.x, adventureInt->position.y, tilesw / adventureInt->worldViewScale, tilesh / adventureInt->worldViewScale);
2015-01-13 21:57:41 +02:00
}
}
void CTerrainRect::fadeFromCurrentView()
{
if (!ADVOPT.screenFading)
return;
if (adventureInt->mode == EAdvMapMode::WORLD_VIEW)
return;
if (!fadeSurface)
fadeSurface = CSDL_Ext::newSurface(pos.w, pos.h);
CSDL_Ext::blitSurface(screen, fadeSurface, Point(0,0));
2015-02-02 18:42:42 +02:00
fadeAnim->init(CFadeAnimation::EMode::OUT, fadeSurface);
}
bool CTerrainRect::needsAnimUpdate()
{
return fadeAnim->isFading() || lastRedrawStatus == EMapAnimRedrawStatus::REDRAW_REQUESTED;
}