Editor: made "Clean up game tiles" undoable

This commit is contained in:
Corantin H 2024-01-23 19:03:53 +01:00
parent a2c8869025
commit af51fcccbb
4 changed files with 26 additions and 13 deletions

View file

@ -476,24 +476,24 @@ void CEditorActionBulk::Redo()
// --------- // ---------
CEditorActionAutoMap::CEditorActionAutoMap(CEditor *pEditor, int GroupIndex, int LayerIndex, const EditorTileStateChangeHistory<STileStateChange> &Changes) : CEditorActionTileChanges::CEditorActionTileChanges(CEditor *pEditor, int GroupIndex, int LayerIndex, const char *pAction, const EditorTileStateChangeHistory<STileStateChange> &Changes) :
CEditorActionLayerBase(pEditor, GroupIndex, LayerIndex), m_Changes(Changes) CEditorActionLayerBase(pEditor, GroupIndex, LayerIndex), m_Changes(Changes)
{ {
ComputeInfos(); ComputeInfos();
str_format(m_aDisplayText, sizeof(m_aDisplayText), "Auto map (x%d)", m_TotalChanges); str_format(m_aDisplayText, sizeof(m_aDisplayText), "%s (x%d)", pAction, m_TotalChanges);
} }
void CEditorActionAutoMap::Undo() void CEditorActionTileChanges::Undo()
{ {
Apply(true); Apply(true);
} }
void CEditorActionAutoMap::Redo() void CEditorActionTileChanges::Redo()
{ {
Apply(false); Apply(false);
} }
void CEditorActionAutoMap::Apply(bool Undo) void CEditorActionTileChanges::Apply(bool Undo)
{ {
auto &Map = m_pEditor->m_Map; auto &Map = m_pEditor->m_Map;
std::shared_ptr<CLayerTiles> pLayerTiles = std::static_pointer_cast<CLayerTiles>(m_pLayer); std::shared_ptr<CLayerTiles> pLayerTiles = std::static_pointer_cast<CLayerTiles>(m_pLayer);
@ -512,7 +512,7 @@ void CEditorActionAutoMap::Apply(bool Undo)
Map.OnModify(); Map.OnModify();
} }
void CEditorActionAutoMap::ComputeInfos() void CEditorActionTileChanges::ComputeInfos()
{ {
m_TotalChanges = 0; m_TotalChanges = 0;
for(auto &Line : m_Changes) for(auto &Line : m_Changes)

View file

@ -155,10 +155,10 @@ private:
// //
class CEditorActionAutoMap : public CEditorActionLayerBase class CEditorActionTileChanges : public CEditorActionLayerBase
{ {
public: public:
CEditorActionAutoMap(CEditor *pEditor, int GroupIndex, int LayerIndex, const EditorTileStateChangeHistory<STileStateChange> &Changes); CEditorActionTileChanges(CEditor *pEditor, int GroupIndex, int LayerIndex, const char *pAction, const EditorTileStateChangeHistory<STileStateChange> &Changes);
void Undo() override; void Undo() override;
void Redo() override; void Redo() override;

View file

@ -924,7 +924,7 @@ CUI::EPopupMenuFunctionResult CLayerTiles::RenderProperties(CUIRect *pToolBox)
if(!m_TilesHistory.empty()) // Sometimes pressing that button causes the automap to run so we should be able to undo that if(!m_TilesHistory.empty()) // Sometimes pressing that button causes the automap to run so we should be able to undo that
{ {
// record undo // record undo
m_pEditor->m_EditorHistory.RecordAction(std::make_shared<CEditorActionAutoMap>(m_pEditor, m_pEditor->m_SelectedGroup, m_pEditor->m_vSelectedLayers[0], m_TilesHistory)); m_pEditor->m_EditorHistory.RecordAction(std::make_shared<CEditorActionTileChanges>(m_pEditor, m_pEditor->m_SelectedGroup, m_pEditor->m_vSelectedLayers[0], "Auto map", m_TilesHistory));
ClearHistory(); ClearHistory();
} }
} }
@ -935,7 +935,7 @@ CUI::EPopupMenuFunctionResult CLayerTiles::RenderProperties(CUIRect *pToolBox)
{ {
m_pEditor->m_Map.m_vpImages[m_Image]->m_AutoMapper.Proceed(this, m_AutoMapperConfig, m_Seed); m_pEditor->m_Map.m_vpImages[m_Image]->m_AutoMapper.Proceed(this, m_AutoMapperConfig, m_Seed);
// record undo // record undo
m_pEditor->m_EditorHistory.RecordAction(std::make_shared<CEditorActionAutoMap>(m_pEditor, m_pEditor->m_SelectedGroup, m_pEditor->m_vSelectedLayers[0], m_TilesHistory)); m_pEditor->m_EditorHistory.RecordAction(std::make_shared<CEditorActionTileChanges>(m_pEditor, m_pEditor->m_SelectedGroup, m_pEditor->m_vSelectedLayers[0], "Auto map", m_TilesHistory));
ClearHistory(); ClearHistory();
return CUI::POPUP_CLOSE_CURRENT; return CUI::POPUP_CLOSE_CURRENT;
} }

View file

@ -405,10 +405,14 @@ CUI::EPopupMenuFunctionResult CEditor::PopupGroup(void *pContext, CUIRect View,
{ {
// gather all tile layers // gather all tile layers
std::vector<std::shared_ptr<CLayerTiles>> vpLayers; std::vector<std::shared_ptr<CLayerTiles>> vpLayers;
for(auto &pLayer : pEditor->m_Map.m_pGameGroup->m_vpLayers) int GameLayerIndex;
for(int LayerIndex = 0; LayerIndex < (int)pEditor->m_Map.m_pGameGroup->m_vpLayers.size(); LayerIndex++)
{ {
auto &pLayer = pEditor->m_Map.m_pGameGroup->m_vpLayers.at(LayerIndex);
if(pLayer != pEditor->m_Map.m_pGameLayer && pLayer->m_Type == LAYERTYPE_TILES) if(pLayer != pEditor->m_Map.m_pGameLayer && pLayer->m_Type == LAYERTYPE_TILES)
vpLayers.push_back(std::static_pointer_cast<CLayerTiles>(pLayer)); vpLayers.push_back(std::static_pointer_cast<CLayerTiles>(pLayer));
else if(pLayer == pEditor->m_Map.m_pGameLayer)
GameLayerIndex = LayerIndex;
} }
// search for unneeded game tiles // search for unneeded game tiles
@ -430,14 +434,23 @@ CUI::EPopupMenuFunctionResult CEditor::PopupGroup(void *pContext, CUIRect View,
} }
} }
if(!Found) CTile Tile = pGameLayer->GetTile(x, y);
if(!Found && Tile.m_Index != TILE_AIR)
{ {
pGameLayer->m_pTiles[y * pGameLayer->m_Width + x].m_Index = TILE_AIR; Tile.m_Index = TILE_AIR;
pGameLayer->SetTile(x, y, Tile);
pEditor->m_Map.OnModify(); pEditor->m_Map.OnModify();
} }
} }
} }
if(!pGameLayer->m_TilesHistory.empty())
{
// record undo
pEditor->m_EditorHistory.RecordAction(std::make_shared<CEditorActionTileChanges>(pEditor, pEditor->m_SelectedGroup, GameLayerIndex, "Clean up game tiles", pGameLayer->m_TilesHistory));
pGameLayer->ClearHistory();
}
return CUI::POPUP_CLOSE_CURRENT; return CUI::POPUP_CLOSE_CURRENT;
} }
} }