/* * 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(std::floor(static_cast(absolutePosition.x) / getSingleTileSize().x)), static_cast(std::floor(static_cast(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()); }