Skip to content

Commit

Permalink
added rotate 90° editor option
Browse files Browse the repository at this point in the history
  • Loading branch information
Robosturm committed Dec 10, 2023
1 parent 9800cec commit c23d203
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 3 deletions.
8 changes: 8 additions & 0 deletions game/gamemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,18 @@ class GameMap : public QObject, public FileSerializable, public oxygine::Actor,
* @brief rotateX
*/
void rotateX();
/**
* @brief rotateX90
*/
void rotateX90();
/**
* @brief rotateY
*/
void rotateY();
/**
* @brief rotate90Y
*/
void rotateY90();

/**
* @brief getTerrain smart pointer
Expand Down
155 changes: 155 additions & 0 deletions mapsupport/refactorMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,161 @@ void GameMap::rotateX()
Mainapp::getInstance()->continueRendering();
}

void GameMap::rotateX90()
{
Mainapp::getInstance()->pauseRendering();
qint32 currentHeigth = getMapHeight();
qint32 currentWidth = getMapWidth();
// upper left quarter gets rotated to upper right
for (qint32 y = 0; y < currentHeigth / 2; y++)
{
for (qint32 x = 0; x < currentWidth / 2; x++)
{
spTerrain sourceTerrain = m_fields[y][x];
qint32 targetX = currentWidth - x - 1;
qint32 targetY = currentHeigth / 2 - y - 1;
spTerrain targetTerrain = m_fields[targetY][targetX];
targetTerrain->detach();
targetTerrain = Terrain::createTerrain(sourceTerrain->getTerrainID(), targetX, targetY, sourceTerrain->getBaseTerrainID(), this);
m_rowSprites[targetY]->addChild(targetTerrain);
m_fields[targetY][targetX] = targetTerrain;
targetTerrain->setPosition(targetX * m_imagesize, targetY * m_imagesize);

spBuilding pCurrentBuilding = sourceTerrain->getSpBuilding();
if (sourceTerrain->getBuilding() != nullptr)
{
spBuilding pBuilding = MemoryManagement::create<Building>(pCurrentBuilding->getBuildingID(), this);
pBuilding->setOwner(pCurrentBuilding->getOwner());
targetTerrain->setBuilding(pBuilding);
}

spUnit pCurrentUnit = sourceTerrain->getSpUnit();
if (pCurrentUnit.get() != nullptr)
{
spUnit pUnit = MemoryManagement::create<Unit>(pCurrentUnit->getUnitID(), pCurrentUnit->getOwner(), false, this);
targetTerrain->setUnit(pUnit);
pUnit->setAiMode(pCurrentUnit->getAiMode());
}
}
}
// lower right quarter gets rotated to lower left
qint32 startY = GlobalUtils::roundUp(static_cast<float>(currentHeigth) / 2.0f);
qint32 startX = GlobalUtils::roundUp(static_cast<float>(currentWidth) / 2.0f);
for (qint32 y = startY; y < currentHeigth; y++)
{
for (qint32 x = startX; x < currentWidth; x++)
{
spTerrain sourceTerrain = m_fields[y][x];
qint32 targetX = currentWidth - x - 1;
qint32 targetY = currentHeigth - 1 - (y - startY);
spTerrain targetTerrain = m_fields[targetY][targetX];
targetTerrain->detach();
targetTerrain = Terrain::createTerrain(sourceTerrain->getTerrainID(), targetX, targetY, sourceTerrain->getBaseTerrainID(), this);
m_rowSprites[targetY]->addChild(targetTerrain);
m_fields[targetY][targetX] = targetTerrain;
targetTerrain->setPosition(targetX * m_imagesize, targetY * m_imagesize);

spBuilding pCurrentBuilding = sourceTerrain->getSpBuilding();
if (sourceTerrain->getBuilding() != nullptr)
{
spBuilding pBuilding = MemoryManagement::create<Building>(pCurrentBuilding->getBuildingID(), this);
pBuilding->setOwner(pCurrentBuilding->getOwner());
targetTerrain->setBuilding(pBuilding);
}

spUnit pCurrentUnit = sourceTerrain->getSpUnit();
if (pCurrentUnit.get() != nullptr)
{
spUnit pUnit = MemoryManagement::create<Unit>(pCurrentUnit->getUnitID(), pCurrentUnit->getOwner(), false, this);
targetTerrain->setUnit(pUnit);
pUnit->setAiMode(pCurrentUnit->getAiMode());
}
}
}

updateSprites(-1, -1, true);
centerMap(getMapWidth() / 2, getMapHeight() / 2);
Mainapp::getInstance()->continueRendering();
}

void GameMap::rotateY90()
{
Mainapp::getInstance()->pauseRendering();
qint32 currentHeigth = getMapHeight();
qint32 currentWidth = getMapWidth();
// upper left quarter gets rotated to lower left
for (qint32 y = 0; y < currentHeigth / 2; y++)
{
for (qint32 x = 0; x < currentWidth / 2; x++)
{
spTerrain sourceTerrain = m_fields[y][x];
qint32 targetX = currentWidth / 2 - x - 1;
qint32 targetY = currentHeigth - y - 1;
spTerrain targetTerrain = m_fields[targetY][targetX];
targetTerrain->detach();
targetTerrain = Terrain::createTerrain(sourceTerrain->getTerrainID(), targetX, targetY, sourceTerrain->getBaseTerrainID(), this);
m_rowSprites[targetY]->addChild(targetTerrain);
m_fields[targetY][targetX] = targetTerrain;
targetTerrain->setPosition(targetX * m_imagesize, targetY * m_imagesize);

spBuilding pCurrentBuilding = sourceTerrain->getSpBuilding();
if (sourceTerrain->getBuilding() != nullptr)
{
spBuilding pBuilding = MemoryManagement::create<Building>(pCurrentBuilding->getBuildingID(), this);
pBuilding->setOwner(pCurrentBuilding->getOwner());
targetTerrain->setBuilding(pBuilding);
}

spUnit pCurrentUnit = sourceTerrain->getSpUnit();
if (pCurrentUnit.get() != nullptr)
{
spUnit pUnit = MemoryManagement::create<Unit>(pCurrentUnit->getUnitID(), pCurrentUnit->getOwner(), false, this);
targetTerrain->setUnit(pUnit);
pUnit->setAiMode(pCurrentUnit->getAiMode());
}
}
}
// lower right quarter gets rotated to upper right
qint32 startY = GlobalUtils::roundUp(static_cast<float>(currentHeigth) / 2.0f);
qint32 startX = GlobalUtils::roundUp(static_cast<float>(currentWidth) / 2.0f);
for (qint32 y = startY; y < currentHeigth; y++)
{
for (qint32 x = startX; x < currentWidth; x++)
{
spTerrain sourceTerrain = m_fields[y][x];
qint32 targetX = currentWidth - 1 - (x - startX);
qint32 targetY = currentHeigth - y - 1;
spTerrain targetTerrain = m_fields[targetY][targetX];
targetTerrain->detach();
targetTerrain = Terrain::createTerrain(sourceTerrain->getTerrainID(), targetX, targetY, sourceTerrain->getBaseTerrainID(), this);
m_rowSprites[targetY]->addChild(targetTerrain);
m_fields[targetY][targetX] = targetTerrain;
targetTerrain->setPosition(targetX * m_imagesize, targetY * m_imagesize);

spBuilding pCurrentBuilding = sourceTerrain->getSpBuilding();
if (sourceTerrain->getBuilding() != nullptr)
{
spBuilding pBuilding = MemoryManagement::create<Building>(pCurrentBuilding->getBuildingID(), this);
pBuilding->setOwner(pCurrentBuilding->getOwner());
targetTerrain->setBuilding(pBuilding);
}

spUnit pCurrentUnit = sourceTerrain->getSpUnit();
if (pCurrentUnit.get() != nullptr)
{
spUnit pUnit = MemoryManagement::create<Unit>(pCurrentUnit->getUnitID(), pCurrentUnit->getOwner(), false, this);
targetTerrain->setUnit(pUnit);
pUnit->setAiMode(pCurrentUnit->getAiMode());
}
}
}

updateSprites(-1, -1, true);
centerMap(getMapWidth() / 2, getMapHeight() / 2);
Mainapp::getInstance()->continueRendering();
}


void GameMap::flipY()
{
Mainapp::getInstance()->pauseRendering();
Expand Down
24 changes: 21 additions & 3 deletions menue/editormenue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ EditorMenue::EditorMenue()
m_Topbar->addItem(tr("Extend map"), "EXTENDMAP", 1, tr("Extends this map with another map"));
m_Topbar->addItem(tr("Flip map X"), "FLIPX", 1, tr("Flips the map at the x-axis. Flipping the left half of the map. The right half of the map is changed."));
m_Topbar->addItem(tr("Flip map Y"), "FLIPY", 1, tr("Flips the map at the y-axis. Flipping the top half of the map. The bottom half of the map is changed."));
m_Topbar->addItem(tr("Rotate map X"), "ROTATEX", 1, tr("Flips and rotates the map at the x-axis. Using the left half of the map. The right half of the map is changed."));
m_Topbar->addItem(tr("Rotate map Y"), "ROTATEY", 1, tr("Flips and rotates the map at the y-axis. Using the top half of the map. The bottom half of the map is changed."));
m_Topbar->addItem(tr("Rotate map X 180°"), "ROTATEX", 1, tr("Flips and rotates the map at the x-axis. Using the left half of the map. The right half of the map is changed."));
m_Topbar->addItem(tr("Rotate map X 90°"), "ROTATEX90", 1, tr("Rotates the upper left quarter into the upper right quarter of the map and rotates the lower right quarter into the lower left quarter of the map "));
m_Topbar->addItem(tr("Rotate map Y 180°"), "ROTATEY", 1, tr("Flips and rotates the map at the y-axis. Using the top half of the map. The bottom half of the map is changed."));
m_Topbar->addItem(tr("Rotate map Y 90°"), "ROTATEY90", 1, tr("Rotates the upper left quarter into the lower left quarter of the map and rotates the lower right quarter into the upper right quarter of the map "));
m_Topbar->addItem(tr("Random map"), "RANDOMMAP", 1, tr("Creates a new random map."));
m_Topbar->addItem(tr("Toggle grid Strg+G"), "TOGGLEGRID", 1, tr("Shows or hides a grid layout."));
m_Topbar->addItem(tr("Toggle cross Strg+M"), "TOGGLEMIDDLECROSS", 1, tr("Shows or hides the cross marking the middle of the map."));
Expand Down Expand Up @@ -387,7 +389,9 @@ void EditorMenue::clickedTopbar(QString itemID)
MenuItem("FLIPX", &EditorMenue::flipX),
MenuItem("FLIPY", &EditorMenue::flipY),
MenuItem("ROTATEX", &EditorMenue::rotateX),
MenuItem("ROTATEY", &EditorMenue::rotateY),
MenuItem("ROTATEY", &EditorMenue::rotateY),
MenuItem("ROTATEX90", &EditorMenue::rotateX90),
MenuItem("ROTATEY90", &EditorMenue::rotateY90),
MenuItem("RANDOMMAP", &EditorMenue::showRandomMap),
MenuItem("PLACESELECTION", &EditorMenue::changePlaceSelection),
MenuItem("DELETEUNITS", &EditorMenue::deleteUnits),
Expand Down Expand Up @@ -732,6 +736,20 @@ void EditorMenue::rotateY()
pGameMap->rotateY();
}

void EditorMenue::rotateX90()
{
createTempFile();
spGameMap pGameMap = m_pMap;
pGameMap->rotateX90();
}

void EditorMenue::rotateY90()
{
createTempFile();
spGameMap pGameMap = m_pMap;
pGameMap->rotateY90();
}

void EditorMenue::showRandomMap()
{
spDialogRandomMap pDialogRandomMap = MemoryManagement::create<DialogRandomMap>(tr("Do you want to create a random map and discard all current changes?"));
Expand Down
8 changes: 8 additions & 0 deletions menue/editormenue.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ public slots:
* @brief rotateY
*/
void rotateY();
/**
* @brief rotateX90
*/
void rotateX90();
/**
* @brief rotateY90
*/
void rotateY90();
/**
* @brief showRandomMap
*/
Expand Down

0 comments on commit c23d203

Please sign in to comment.