2023-02-14 23:49:12 +02:00
|
|
|
/*
|
|
|
|
* MapView.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 "MapView.h"
|
|
|
|
|
2023-02-23 19:46:41 +02:00
|
|
|
#include "MapViewActions.h"
|
2023-02-20 22:16:50 +02:00
|
|
|
#include "MapViewCache.h"
|
|
|
|
#include "MapViewController.h"
|
2023-02-23 19:46:41 +02:00
|
|
|
#include "MapViewModel.h"
|
2023-02-14 23:49:12 +02:00
|
|
|
#include "mapHandler.h"
|
|
|
|
|
|
|
|
#include "../CGameInfo.h"
|
|
|
|
#include "../CPlayerInterface.h"
|
2023-05-08 14:18:34 +02:00
|
|
|
#include "../adventureMap/AdventureMapInterface.h"
|
2023-02-14 23:49:12 +02:00
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../render/CAnimation.h"
|
|
|
|
#include "../render/Canvas.h"
|
2023-02-20 19:56:35 +02:00
|
|
|
#include "../render/IImage.h"
|
2023-02-14 23:49:12 +02:00
|
|
|
#include "../renderSDL/SDL_Extensions.h"
|
|
|
|
|
|
|
|
#include "../../CCallback.h"
|
|
|
|
|
|
|
|
#include "../../lib/CConfigHandler.h"
|
|
|
|
#include "../../lib/mapObjects/CGHeroInstance.h"
|
|
|
|
#include "../../lib/mapping/CMap.h"
|
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
BasicMapView::~BasicMapView() = default;
|
2023-02-14 23:49:12 +02:00
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
std::shared_ptr<MapViewModel> BasicMapView::createModel(const Point & dimensions) const
|
2023-02-14 23:49:12 +02:00
|
|
|
{
|
2023-02-15 20:16:21 +02:00
|
|
|
auto result = std::make_shared<MapViewModel>();
|
2023-02-14 23:49:12 +02:00
|
|
|
|
2023-02-15 20:16:21 +02:00
|
|
|
result->setLevel(0);
|
2023-02-18 17:37:09 +02:00
|
|
|
result->setTileSize(Point(32, 32));
|
|
|
|
result->setViewCenter(Point(0, 0));
|
2023-02-15 20:16:21 +02:00
|
|
|
result->setViewDimensions(dimensions);
|
2023-02-14 23:49:12 +02:00
|
|
|
|
2023-02-15 20:16:21 +02:00
|
|
|
return result;
|
2023-02-14 23:49:12 +02:00
|
|
|
}
|
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
BasicMapView::BasicMapView(const Point & offset, const Point & dimensions)
|
2023-02-15 20:16:21 +02:00
|
|
|
: model(createModel(dimensions))
|
2023-02-18 17:37:09 +02:00
|
|
|
, tilesCache(new MapViewCache(model))
|
2023-02-26 18:17:53 +02:00
|
|
|
, controller(new MapViewController(model, tilesCache))
|
2023-02-14 23:49:12 +02:00
|
|
|
{
|
2023-02-23 19:46:41 +02:00
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
|
2023-02-15 20:16:21 +02:00
|
|
|
pos += offset;
|
|
|
|
pos.w = dimensions.x;
|
|
|
|
pos.h = dimensions.y;
|
2023-05-13 16:24:18 +02:00
|
|
|
|
|
|
|
addUsedEvents(TIME);
|
2023-02-14 23:49:12 +02:00
|
|
|
}
|
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
void BasicMapView::render(Canvas & target, bool fullUpdate)
|
2023-02-14 23:49:12 +02:00
|
|
|
{
|
|
|
|
Canvas targetClipped(target, pos);
|
2023-02-21 19:21:50 +02:00
|
|
|
tilesCache->update(controller->getContext());
|
2023-02-21 22:28:48 +02:00
|
|
|
tilesCache->render(controller->getContext(), targetClipped, fullUpdate);
|
|
|
|
}
|
|
|
|
|
2023-05-13 16:24:18 +02:00
|
|
|
void BasicMapView::tick(uint32_t msPassed)
|
2023-02-21 22:28:48 +02:00
|
|
|
{
|
2023-05-13 16:24:18 +02:00
|
|
|
controller->tick(msPassed);
|
|
|
|
}
|
2023-03-22 15:51:25 +02:00
|
|
|
|
2023-05-13 16:24:18 +02:00
|
|
|
void BasicMapView::show(SDL_Surface * to)
|
|
|
|
{
|
2023-02-21 22:28:48 +02:00
|
|
|
Canvas target(to);
|
|
|
|
CSDL_Ext::CClipRectGuard guard(to, pos);
|
|
|
|
render(target, false);
|
2023-03-28 21:37:53 +02:00
|
|
|
|
2023-05-13 16:24:18 +02:00
|
|
|
controller->afterRender();
|
2023-02-14 23:49:12 +02:00
|
|
|
}
|
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
void BasicMapView::showAll(SDL_Surface * to)
|
2023-02-14 23:49:12 +02:00
|
|
|
{
|
2023-02-21 22:28:48 +02:00
|
|
|
Canvas target(to);
|
|
|
|
CSDL_Ext::CClipRectGuard guard(to, pos);
|
|
|
|
render(target, true);
|
2023-02-14 23:49:12 +02:00
|
|
|
}
|
|
|
|
|
2023-02-26 18:17:53 +02:00
|
|
|
void MapView::show(SDL_Surface * to)
|
|
|
|
{
|
|
|
|
actions->setContext(controller->getContext());
|
|
|
|
BasicMapView::show(to);
|
|
|
|
}
|
|
|
|
|
|
|
|
MapView::MapView(const Point & offset, const Point & dimensions)
|
|
|
|
: BasicMapView(offset, dimensions)
|
|
|
|
, isSwiping(false)
|
|
|
|
{
|
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
|
|
|
|
actions = std::make_shared<MapViewActions>(*this, model);
|
|
|
|
actions->setContext(controller->getContext());
|
|
|
|
}
|
|
|
|
|
2023-02-23 19:46:41 +02:00
|
|
|
void MapView::onMapLevelSwitched()
|
|
|
|
{
|
|
|
|
if(LOCPLINT->cb->getMapSize().z > 1)
|
|
|
|
{
|
|
|
|
if(model->getLevel() == 0)
|
|
|
|
controller->setViewCenter(model->getMapViewCenter(), 1);
|
|
|
|
else
|
|
|
|
controller->setViewCenter(model->getMapViewCenter(), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::onMapScrolled(const Point & distance)
|
|
|
|
{
|
|
|
|
if(!isSwiping)
|
|
|
|
controller->setViewCenter(model->getMapViewCenter() + distance, model->getLevel());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::onMapSwiped(const Point & viewPosition)
|
|
|
|
{
|
|
|
|
isSwiping = true;
|
|
|
|
controller->setViewCenter(viewPosition, model->getLevel());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::onMapSwipeEnded()
|
|
|
|
{
|
2023-03-02 19:24:32 +02:00
|
|
|
isSwiping = false;
|
2023-02-23 19:46:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::onCenteredTile(const int3 & tile)
|
|
|
|
{
|
|
|
|
controller->setViewCenter(tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::onCenteredObject(const CGObjectInstance * target)
|
|
|
|
{
|
|
|
|
controller->setViewCenter(target->getSightCenter());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::onViewSpellActivated(uint32_t tileSize, const std::vector<ObjectPosInfo> & objectPositions, bool showTerrain)
|
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
controller->activateSpellViewContext();
|
2023-02-23 19:46:41 +02:00
|
|
|
controller->setTileSize(Point(tileSize, tileSize));
|
|
|
|
controller->setOverlayVisibility(objectPositions);
|
|
|
|
controller->setTerrainVisibility(showTerrain);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapView::onViewWorldActivated(uint32_t tileSize)
|
2023-02-15 20:16:21 +02:00
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
controller->activateWorldViewContext();
|
2023-02-23 19:46:41 +02:00
|
|
|
controller->setTileSize(Point(tileSize, tileSize));
|
2023-02-15 20:16:21 +02:00
|
|
|
}
|
|
|
|
|
2023-05-16 22:42:51 +02:00
|
|
|
void MapView::onMapZoomLevelChanged(double zoomFactor)
|
|
|
|
{
|
|
|
|
controller->modifyTileSize(zoomFactor);
|
|
|
|
}
|
|
|
|
|
2023-02-23 19:46:41 +02:00
|
|
|
void MapView::onViewMapActivated()
|
2023-02-18 17:37:09 +02:00
|
|
|
{
|
2023-02-26 18:17:53 +02:00
|
|
|
controller->activateAdventureContext();
|
2023-02-23 19:46:41 +02:00
|
|
|
controller->setTileSize(Point(32, 32));
|
2023-02-26 18:17:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PuzzleMapView::PuzzleMapView(const Point & offset, const Point & dimensions, const int3 & tileToCenter)
|
|
|
|
: BasicMapView(offset, dimensions)
|
|
|
|
{
|
|
|
|
controller->activatePuzzleMapContext(tileToCenter);
|
2023-04-24 16:16:55 +02:00
|
|
|
controller->setViewCenter(tileToCenter);
|
|
|
|
|
2023-02-18 17:37:09 +02:00
|
|
|
}
|