1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/client/mapView/MapView.cpp

164 lines
4.2 KiB
C++
Raw Normal View History

/*
* 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"
#include "MapViewActions.h"
#include "MapViewCache.h"
#include "MapViewController.h"
#include "MapViewModel.h"
#include "mapHandler.h"
#include "../CGameInfo.h"
#include "../CPlayerInterface.h"
#include "../adventureMap/CAdvMapInt.h"
#include "../gui/CGuiHandler.h"
#include "../render/CAnimation.h"
#include "../render/Canvas.h"
#include "../render/IImage.h"
#include "../renderSDL/SDL_Extensions.h"
#include "../../CCallback.h"
#include "../../lib/CConfigHandler.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
#include "../../lib/mapping/CMap.h"
BasicMapView::~BasicMapView() = default;
std::shared_ptr<MapViewModel> BasicMapView::createModel(const Point & dimensions) const
{
2023-02-15 20:16:21 +02:00
auto result = std::make_shared<MapViewModel>();
2023-02-15 20:16:21 +02:00
result->setLevel(0);
result->setTileSize(Point(32, 32));
result->setViewCenter(Point(0, 0));
2023-02-15 20:16:21 +02:00
result->setViewDimensions(dimensions);
2023-02-15 20:16:21 +02:00
return result;
}
BasicMapView::BasicMapView(const Point & offset, const Point & dimensions)
2023-02-15 20:16:21 +02:00
: model(createModel(dimensions))
, tilesCache(new MapViewCache(model))
, controller(new MapViewController(model, tilesCache))
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
2023-02-15 20:16:21 +02:00
pos += offset;
pos.w = dimensions.x;
pos.h = dimensions.y;
}
void BasicMapView::render(Canvas & target, bool fullUpdate)
{
Canvas targetClipped(target, pos);
tilesCache->update(controller->getContext());
2023-02-21 22:28:48 +02:00
tilesCache->render(controller->getContext(), targetClipped, fullUpdate);
}
void BasicMapView::show(SDL_Surface * to)
2023-02-21 22:28:48 +02:00
{
2023-03-22 15:51:25 +02:00
controller->update(GH.mainFPSmng->getElapsedMilliseconds());
2023-02-21 22:28:48 +02:00
Canvas target(to);
CSDL_Ext::CClipRectGuard guard(to, pos);
render(target, false);
}
void BasicMapView::showAll(SDL_Surface * to)
{
2023-03-22 15:51:25 +02:00
controller->update(0);
2023-02-21 22:28:48 +02:00
Canvas target(to);
CSDL_Ext::CClipRectGuard guard(to, pos);
render(target, true);
}
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());
}
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()
{
isSwiping = false;
}
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)
{
controller->activateSpellViewContext();
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
{
controller->activateWorldViewContext();
controller->setTileSize(Point(tileSize, tileSize));
2023-02-15 20:16:21 +02:00
}
void MapView::onViewMapActivated()
{
controller->activateAdventureContext();
controller->setTileSize(Point(32, 32));
}
PuzzleMapView::PuzzleMapView(const Point & offset, const Point & dimensions, const int3 & tileToCenter)
: BasicMapView(offset, dimensions)
{
controller->setViewCenter(tileToCenter);
controller->activatePuzzleMapContext(tileToCenter);
}