void TileMap::pasteSelectedTiles(sf::Vector2i mousePosition)
{
sf::Vector2f mousePositionWorld = _window.mapPixelToCoords(mousePosition);
if (!isAllowedPosition(mousePositionWorld)) return;
int left = _chunks.x;
int top = _chunks.y;
int bottom = 0;
int right = 0;
int numOfElements = 0;
applyTileMapSelectionIndices(left,top,bottom,right, numOfElements);
if (numOfElements <= 0)
return;
//std::cout << "Got " << numOfElements << " for copy. " << "Left[" << left << "][" << top << "] to Bottom[" << right << "][" << bottom << "]" << std::endl;
//Got Dimensions
int x = static_cast<int>(mousePositionWorld.x / chunksize);
int y = static_cast<int>(mousePositionWorld.y / chunksize);
std::cout << "Paste at <" << x << ":" << y << ">" << std::endl;
const int sizex = 1 + (right - left);
const int sizey = 1 + (bottom - top);
//////////////////////////////////////////////////////////////////////////
// Create temp Vector
//////////////////////////////////////////////////////////////////////////
using DimVec = std::vector < std::vector< Tile*> > ;
std::array< DimVec,count> _tempArray;
for (int layer = 0; layer < Layer::count; layer++)
{
_tempArray[layer].assign(sizex,std::vector<Tile*>(sizey,nullptr));
}
//////////////////////////////////////////////////////////////////////////
// Copy to temp Vector
//////////////////////////////////////////////////////////////////////////
for (int layer = 0; layer < Layer::count; layer++)
{
//minLeft selectedElement to maxRight selectedElement
for (int ix = 0; ix < sizex; ++ix)
{
if (!isAllowedPositionX(left + ix)) break;
//minTop selectedElement to maxBottom selectedElement
for (int iy = 0; iy < sizey; ++iy)
{
if (!isAllowedPositionY(top + iy)) break;
//std::cout << "Accessing <L" << layer << ":" << ix << ":" << iy << ">" << std::endl;
Tile& oldTile = _layeredChunks[layer][left + ix][top + iy];
//is Tile marked for copy?
if (oldTile.isCopyable())
{
_tempArray[layer][ix][iy] = &oldTile;
}
else
{
_tempArray[layer][ix][iy] = nullptr;
}
}
}
}
//////////////////////////////////////////////////////////////////////////
// Paste from temp Vector
//////////////////////////////////////////////////////////////////////////
sf::Vector2f targetWorldPosition;
for (int layer = 0; layer < Layer::count; layer++)
{
//minLeft selectedElement to maxRight selectedElement
for (int ix = 0; ix < sizex; ++ix)
{
if (!isAllowedPositionX(x + ix)) break;
//minTop selectedElement to maxBottom selectedElement
for (int iy = 0; iy < sizey; ++iy)
{
if (!isAllowedPositionY(y + iy)) break;
//std::cout << "Accessing2 <L" << layer << ":" << ix << ":" << iy << ">" << std::endl;
//std::cout << "Copying <L" << layer << ":" << ix << ":" << iy << ">" << std::endl;
Tile* oldTile = _tempArray[layer][ix][iy];
if (oldTile == nullptr) continue;
Tile& newTile = _layeredChunks[layer][x + ix][y + iy];
//Apply copy options
newTile = *oldTile;
//////////////////////////////////////////////////////////////////////////
// Here is the PROBLEM! WorldCoordinates will be not correct if I zoom or move my _mapView
//////////////////////////////////////////////////////////////////////////
//Set converted worldMousePosition (Paste position start in mapView Coords)
sf::Vector2f pasteCoords(x*chunksize,y*chunksize);
//Add target offset (past position + offset) Where should the current tile be displayed?
pasteCoords.x += (ix*chunksize);
pasteCoords.y += (iy*chunksize);
//Set new tile position (Always right until i zoom or move view)
newTile.setPosition(pasteCoords);
newTile.setCopyable(false);
newTile.setSelected(false);
newTile.setVisible(true);
if (_selectionCut)
{
//Apply xcopy options
oldTile->setVisible(false);
oldTile->setCopyable(false);
}
}
}
}
}