| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- /*
- * 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 "CAdvMapInt.h"
- #include "../CGameInfo.h"
- #include "../CMT.h"
- #include "../CPlayerInterface.h"
- #include "../gui/CGuiHandler.h"
- #include "../gui/CursorHandler.h"
- #include "../mapRenderer/mapHandler.h"
- #include "../render/CAnimation.h"
- #include "../render/IImage.h"
- #include "../renderSDL/SDL_Extensions.h"
- #include "../widgets/TextControls.h"
- #include "../mapRenderer/MapView.h"
- #include "../mapRenderer/MapViewController.h"
- #include "../mapRenderer/MapViewModel.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()
- : curHoveredTile(-1, -1, -1)
- , isSwiping(false)
- {
- OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
- pos.x=ADVOPT.advmapX;
- pos.y=ADVOPT.advmapY;
- pos.w=ADVOPT.advmapW;
- pos.h=ADVOPT.advmapH;
- addUsedEvents(LCLICK | RCLICK | MCLICK | HOVER | MOVE);
- renderer = std::make_shared<MapView>( Point(0,0), pos.dimensions() );
- }
- void CTerrainRect::setViewCenter(const int3 &coordinates)
- {
- renderer->getController()->setViewCenter(coordinates);
- }
- void CTerrainRect::setViewCenter(const Point & position, int level)
- {
- renderer->getController()->setViewCenter(position, level);
- }
- void CTerrainRect::deactivate()
- {
- CIntObject::deactivate();
- curHoveredTile = int3(-1,-1,-1); //we lost info about hovered tile when disabling
- }
- void CTerrainRect::clickLeft(tribool down, bool previousState)
- {
- if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
- return;
- if(indeterminate(down))
- return;
- #if defined(VCMI_MOBILE)
- if(adventureInt->swipeEnabled)
- {
- 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;
- }
- 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)
- {
- #if defined(VCMI_MOBILE)
- if(adventureInt->swipeEnabled && isSwiping)
- return;
- #endif
- if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
- return;
- int3 mp = whichTileIsIt();
- if(CGI->mh->isInMap(mp) && down)
- adventureInt->tileRClicked(mp);
- }
- void CTerrainRect::clickMiddle(tribool down, bool previousState)
- {
- handleSwipeStateChange((bool)down == true);
- }
- void CTerrainRect::mouseMoved(const Point & cursorPosition)
- {
- handleHover(cursorPosition);
- if(!adventureInt->swipeEnabled)
- return;
- handleSwipeMove(cursorPosition);
- }
- void CTerrainRect::handleSwipeMove(const Point & cursorPosition)
- {
- #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(std::abs(cursorPosition.x - swipeInitialRealPos.x) > SwipeTouchSlop ||
- std::abs(cursorPosition.y - swipeInitialRealPos.y) > SwipeTouchSlop)
- {
- isSwiping = true;
- }
- }
- if(isSwiping)
- {
- adventureInt->swipeTargetPosition.x = swipeInitialViewPos.x + swipeInitialRealPos.x - cursorPosition.x;
- adventureInt->swipeTargetPosition.y = swipeInitialViewPos.y + swipeInitialRealPos.y - cursorPosition.y;
- adventureInt->swipeMovementRequested = true;
- }
- }
- bool CTerrainRect::handleSwipeStateChange(bool btnPressed)
- {
- if(btnPressed)
- {
- swipeInitialRealPos = Point(GH.getCursorPosition().x, GH.getCursorPosition().y);
- swipeInitialViewPos = getViewCenter();
- return true;
- }
- 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);
- if(!CGI->mh->isInMap(tHovered))
- {
- CCS->curh->set(Cursor::Map::POINTER);
- return;
- }
- if (tHovered != curHoveredTile)
- {
- curHoveredTile = tHovered;
- adventureInt->tileHovered(tHovered);
- }
- }
- void CTerrainRect::hover(bool on)
- {
- if (!on)
- {
- GH.statusbar->clear();
- CCS->curh->set(Cursor::Map::POINTER);
- }
- //Hoverable::hover(on);
- }
- int3 CTerrainRect::whichTileIsIt(const Point &position)
- {
- return renderer->getModel()->getTileAtPoint(position - pos);
- }
- int3 CTerrainRect::whichTileIsIt()
- {
- return whichTileIsIt(GH.getCursorPosition());
- }
- Rect CTerrainRect::visibleTilesArea()
- {
- return renderer->getModel()->getTilesTotalRect();
- }
- void CTerrainRect::fadeFromCurrentView()
- {
- assert(0);//TODO
- }
- void CTerrainRect::setLevel(int level)
- {
- renderer->getController()->setViewCenter(renderer->getModel()->getMapViewCenter(), level);
- }
- void CTerrainRect::moveViewBy(const Point & delta)
- {
- renderer->getController()->setViewCenter(renderer->getModel()->getMapViewCenter() + delta, getLevel());
- }
- Point CTerrainRect::getViewCenter()
- {
- return renderer->getModel()->getMapViewCenter();
- }
- int CTerrainRect::getLevel()
- {
- return renderer->getModel()->getLevel();
- }
- void CTerrainRect::setTileSize(int sizePixels)
- {
- renderer->getController()->setTileSize(Point(sizePixels, sizePixels));
- }
|