mirror of
https://github.com/vcmi/vcmi.git
synced 2025-06-15 00:05:02 +02:00
Renamed directory
This commit is contained in:
117
client/mapView/MapViewModel.cpp
Normal file
117
client/mapView/MapViewModel.cpp
Normal file
@ -0,0 +1,117 @@
|
||||
/*
|
||||
* MapViewModel.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 "MapViewModel.h"
|
||||
|
||||
#include "../../lib/int3.h"
|
||||
|
||||
void MapViewModel::setTileSize(const Point & newValue)
|
||||
{
|
||||
tileSize = newValue;
|
||||
}
|
||||
|
||||
void MapViewModel::setViewCenter(const Point & newValue)
|
||||
{
|
||||
viewCenter = newValue;
|
||||
}
|
||||
|
||||
void MapViewModel::setViewDimensions(const Point & newValue)
|
||||
{
|
||||
viewDimensions = newValue;
|
||||
}
|
||||
|
||||
void MapViewModel::setLevel(int newLevel)
|
||||
{
|
||||
mapLevel = newLevel;
|
||||
}
|
||||
|
||||
Point MapViewModel::getSingleTileSize() const
|
||||
{
|
||||
return tileSize;
|
||||
}
|
||||
|
||||
Point MapViewModel::getMapViewCenter() const
|
||||
{
|
||||
return viewCenter;
|
||||
}
|
||||
|
||||
Point MapViewModel::getPixelsVisibleDimensions() const
|
||||
{
|
||||
return viewDimensions;
|
||||
}
|
||||
|
||||
int MapViewModel::getLevel() const
|
||||
{
|
||||
return mapLevel;
|
||||
}
|
||||
|
||||
Point MapViewModel::getTilesVisibleDimensions() const
|
||||
{
|
||||
// total number of potentially visible tiles is:
|
||||
// 1) number of completely visible tiles
|
||||
// 2) additional tile that might be partially visible from left/top size
|
||||
// 3) additional tile that might be partially visible from right/bottom size
|
||||
return {
|
||||
getPixelsVisibleDimensions().x / getSingleTileSize().x + 2,
|
||||
getPixelsVisibleDimensions().y / getSingleTileSize().y + 2,
|
||||
};
|
||||
}
|
||||
|
||||
Rect MapViewModel::getTilesTotalRect() const
|
||||
{
|
||||
return Rect(
|
||||
Point(getTileAtPoint(Point(0,0))),
|
||||
getTilesVisibleDimensions()
|
||||
);
|
||||
}
|
||||
|
||||
int3 MapViewModel::getTileAtPoint(const Point & position) const
|
||||
{
|
||||
Point topLeftOffset = getMapViewCenter() - getPixelsVisibleDimensions() / 2;
|
||||
|
||||
Point absolutePosition = position + topLeftOffset;
|
||||
|
||||
// NOTE: using division via double in order to use std::floor
|
||||
// which rounds to negative infinity and not towards zero (like integer division)
|
||||
return {
|
||||
static_cast<int>(std::floor(static_cast<double>(absolutePosition.x) / getSingleTileSize().x)),
|
||||
static_cast<int>(std::floor(static_cast<double>(absolutePosition.y) / getSingleTileSize().y)),
|
||||
getLevel()
|
||||
};
|
||||
}
|
||||
|
||||
Point MapViewModel::getCacheDimensionsPixels() const
|
||||
{
|
||||
return getTilesVisibleDimensions() * getSingleTileSize();
|
||||
}
|
||||
|
||||
Rect MapViewModel::getCacheTileArea(const int3 & coordinates) const
|
||||
{
|
||||
assert(mapLevel == coordinates.z);
|
||||
assert(getTilesVisibleDimensions().x + coordinates.x >= 0);
|
||||
assert(getTilesVisibleDimensions().y + coordinates.y >= 0);
|
||||
|
||||
Point tileIndex{
|
||||
(getTilesVisibleDimensions().x + coordinates.x) % getTilesVisibleDimensions().x,
|
||||
(getTilesVisibleDimensions().y + coordinates.y) % getTilesVisibleDimensions().y
|
||||
};
|
||||
|
||||
return Rect(tileIndex * tileSize, tileSize);
|
||||
}
|
||||
|
||||
Rect MapViewModel::getTargetTileArea(const int3 & coordinates) const
|
||||
{
|
||||
Point topLeftOffset = getMapViewCenter() - getPixelsVisibleDimensions() / 2;
|
||||
Point tilePosAbsolute = Point(coordinates) * tileSize;
|
||||
Point tilePosRelative = tilePosAbsolute - topLeftOffset;
|
||||
|
||||
return Rect(tilePosRelative, tileSize);
|
||||
}
|
Reference in New Issue
Block a user