1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
/*
* 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::getSingleTileSizeUpperLimit() const
{
// arbitrary-seleted upscaling limit
return Point(256, 256);
}
Point MapViewModel::getSingleTileSizeLowerLimit() const
{
// arbitrary-seleted downscaling limit
return Point(4, 4);
}
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 getPixelsVisibleDimensions() + getSingleTileSizeUpperLimit() * 2;
}
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 * getSingleTileSize(), getSingleTileSize());
}
Rect MapViewModel::getTargetTileArea(const int3 & coordinates) const
{
Point topLeftOffset = getMapViewCenter() - getPixelsVisibleDimensions() / 2;
Point tilePosAbsolute = Point(coordinates) * getSingleTileSize();
Point tilePosRelative = tilePosAbsolute - topLeftOffset;
return Rect(tilePosRelative, getSingleTileSize());
}
|