123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492 |
- //----------------------------------------------------------------------------
- //
- // Action.cpp - implementation file for the abstract action object, and
- // the mgr that holds action objects. (otherwise known as the
- // undo manager )
- //
- //---------------------------------------------------------------------------//
- // Copyright (C) Microsoft Corporation. All rights reserved. //
- //===========================================================================//
- #include "terrain.h"
- #include "Action.h"
- #include "TerrTxm.h"
- ActionUndoMgr* ActionUndoMgr::instance = NULL;
- //************************************************************************
- // Function: c'tor
- // ParamsIn: none
- // ParamsOut: none
- // Returns: nothing
- // Description:
- //************************************************************************
- ActionUndoMgr::ActionUndoMgr()
- {
- m_CurrentPos = -1;
- m_PosOfLastSave = -1;
- gosASSERT( !instance );
- instance = this;
- }
- //************************************************************************
- // Function: d'tor
- // ParamsIn: none
- // ParamsOut: none
- // Returns: nothing
- // Description: empties list
- //************************************************************************
- ActionUndoMgr::~ActionUndoMgr()
- {
- Reset();
- }
- //***********************************************************************
- // Function: AddAction
- // ParamsIn: Action to add
- // ParamsOut: none
- // Returns: void
- // Descripition: adds the passed in action to the undo list
- //***********************************************************************
- void ActionUndoMgr::AddAction( Action* pAction )
- {
- gosASSERT( pAction );
- if ( m_listUndoActions.Count() )
- {
- if (m_PosOfLastSave > m_CurrentPos)
- {
- m_PosOfLastSave = -1;
- }
- ACTION_LIST::EIterator iter = m_listUndoActions.End();
- for ( int i = m_listUndoActions.Count() - 1; i > m_CurrentPos; -- i )
- {
- delete (*iter);
- iter--;
- m_listUndoActions.DeleteTail();
- }
- }
- m_listUndoActions.Append( pAction );
- m_CurrentPos = m_listUndoActions.Count() - 1;
- }// fun AddAction
- //***********************************************************************
- // Function: EmptyUndoList
- // ParamsIn: none
- // ParamsOut: none
- // Returns: void
- // Descripition:clears out the undo list
- //***********************************************************************
- void ActionUndoMgr::EmptyUndoList()
- {
-
- for (ACTION_LIST::EIterator pos = m_listUndoActions.Begin(); !pos.IsDone(); pos++ )
- {
- delete (*pos);
- }
- m_listUndoActions.Clear();
- m_CurrentPos = -1;
- m_PosOfLastSave = -1;
- }
- //***********************************************************************
- // Function: GetRedoString
- // ParamsIn: none
- // ParamsOut: none
- // Returns: string to put in the Undo prompt
- // Descripition: this is the string that should go in the redo prompt
- //***********************************************************************
- const char* ActionUndoMgr::GetRedoString()
- {
- const char* strRet = NULL;
- if ( HaveRedo() )
- {
-
- ACTION_POS tmp = m_CurrentPos;
- tmp++;
- ACTION_LIST::EIterator iter = m_listUndoActions.Iterator( tmp );
- return (*iter)->getDescription();
- }
- return strRet;
- }
- //***********************************************************************
- // Function: GetUndoString
- // ParamsIn: none
- // ParamsOut: none
- // Returns: string to put in the Udo prompt
- // Descripition: this is the string that should go in the undo prompt
- //***********************************************************************
- const char* ActionUndoMgr::GetUndoString()
- {
- const char* strRet = NULL;
- if ( HaveUndo() )
- {
- ACTION_LIST::EIterator iter = m_listUndoActions.Iterator( m_CurrentPos );
- return (*iter)->getDescription();
- }
- return strRet;
- }
- //***********************************************************************
- // Function: HaveRedo
- // ParamsIn: none
- // ParamsOut: none
- // Returns: whether there is a redo action to perform
- // Descripition: call to see wherther you can perform a redo
- //***********************************************************************
- bool ActionUndoMgr::HaveRedo() const
- {
- return m_CurrentPos + 1 != m_listUndoActions.Count();
- }
- //***********************************************************************
- // Function: HaveUndo
- // ParamsIn: none
- // ParamsOut: none
- // Returns: whether there is undo action to perform
- // Descripition: call to see whether you can perform an undo action
- //***********************************************************************
- bool ActionUndoMgr::HaveUndo() const
- {
- return m_CurrentPos != -1;
- }
- //************************************************************************
- // Function: Redo
- // ParamsIn: none
- // ParamsOut; none
- // Returns: success of operation
- // Description: gets the action at the front of the redo list, performs
- // it and adds it to the undo list
- //************************************************************************
- bool ActionUndoMgr::Redo()
- {
- gosASSERT( HaveRedo() );
- m_CurrentPos++;
- ACTION_LIST::EIterator iter = m_listUndoActions.Iterator( m_CurrentPos );
- return (*iter)->redo();
- }
- //************************************************************************
- // Function: Reset
- // ParamsIn: none
- // ParamsOut: none
- // Returns: nothing
- // Description: Empties all of the actions from the do and undo lists
- //************************************************************************
- void ActionUndoMgr::Reset()
- {
- EmptyUndoList();
- }
- //************************************************************************
- // Function: Undo
- // ParamsIn: none
- // ParamsOut; none
- // Returns: success of operation
- // Description: gets the action at the front of the undo list, performs
- // it and adds it to the redo list
- //************************************************************************
- bool ActionUndoMgr::Undo()
- {
- bool bRetVal = false;
- gosASSERT( HaveUndo() );
- ACTION_LIST::EIterator iter = m_listUndoActions.Iterator( m_CurrentPos );
- bRetVal = (*iter)->undo();
- m_CurrentPos --;
- return bRetVal;
- }
- //************************************************************************
- // Function: NoteThatASaveHasJustOccurred
- // ParamsIn: none
- // ParamsOut; none
- // Returns:
- // Description:
- //************************************************************************
- void ActionUndoMgr::NoteThatASaveHasJustOccurred()
- {
- m_PosOfLastSave = m_CurrentPos;
- }
- //************************************************************************
- // Function: ThereHasBeenANetChangeFromWhenLastSaved
- // ParamsIn: none
- // ParamsOut; none
- // Returns:
- // Description:
- //************************************************************************
- bool ActionUndoMgr::ThereHasBeenANetChangeFromWhenLastSaved()
- {
- if (m_PosOfLastSave == m_CurrentPos)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- //-----------------------------------------------------------------------
- // Function: ActionPaintTile::Redo
- // ParamsIn: none
- // ParamsOut: none
- // Returns: success of operation
- // Description: redoes a smart paint operation
- ////-----------------------------------------------------------------------
- bool ActionPaintTile::redo()
- {
- return doRedo();
- }
- bool ActionPaintTile::doRedo()
- {
- for ( VERTEX_INFO_LIST::EIterator iter = vertexInfoList.Begin();
- !iter.IsDone(); iter++ )
- {
- // get current values
- int terrain = land->getTerrain( (*iter).row, (*iter).column );
- int texture = land->getTexture( (*iter).row, (*iter).column );
- float elv = land->getTerrainElevation( (*iter).row, (*iter).column );
- Overlays overlay;
- unsigned long offset;
- // reset to old values
- land->terrainTextures->getOverlayInfoFromHandle( (*iter).textureData, overlay, offset );
- land->setOverlay( (*iter).row, (*iter).column, overlay, offset );
- land->setTerrain( (*iter).row, (*iter).column, (*iter).terrainData );
- land->setVertexHeight( (*iter).row * land->realVerticesMapSide + (*iter).column, (*iter).elevation );
- // save current valuds
- (*iter).terrainData = terrain;
- (*iter).textureData = texture;
- (*iter).elevation = elv;
- }
- return true;
- }
- //-----------------------------------------------------------------------
- // Function: Undo
- // ParamsIn: none
- // ParamsOut: none
- // Returns: nothing
- // Description: undos a smart paint operation
- ////-----------------------------------------------------------------------
- bool ActionPaintTile::undo()
- {
- // actually, undo does the same thing as redo.
- return doRedo();
- }
- //-----------------------------------------------------------------------
- // Function: AddTileInfo
- // ParamsIn: info to be added to the undo list
- // ParamsOut: none
- // Returns: nothing
- // Description: this function checks to make sure that the object isn't
- // already in the list before adding it.
- ////-----------------------------------------------------------------------
- void ActionPaintTile::addVertexInfo( VertexInfo& info )
- {
- for( VERTEX_INFO_LIST::EIterator iter = vertexInfoList.Begin();
- !iter.IsDone(); iter++ )
- {
- if ( info.row == (*iter).row && info.column == (*iter).column )
- return;
- }
- vertexInfoList.Append( info );
- }
- //-----------------------------------------------------------------------
- // Function: AddChangedVertexInfo
- // ParamsIn: none
- // ParamsOut: none
- // Returns: nothing
- // Description:
- ////-----------------------------------------------------------------------
- void ActionPaintTile::addChangedVertexInfo( int row, int column )
- {
- // get the info and add it
- for( VERTEX_INFO_LIST::EIterator iter = vertexInfoList.Begin();
- !iter.IsDone(); iter++ )
- {
- if ( row == (*iter).row && column == (*iter).column )
- return;
- }
- // if we made it here, it isn't in there already
- VertexInfo info( row, column );
- vertexInfoList.Append( info );
- }
- bool ActionPaintTile::getOldHeight( int row, int column, float& height )
- {
- for( VERTEX_INFO_LIST::EIterator iter = vertexInfoList.Begin();
- !iter.IsDone(); iter++ )
- {
- if ( row == (*iter).row && column == (*iter).column )
- {
- height = (*iter).elevation;
- return true;
- }
- }
- return false;
- }
- VertexInfo::VertexInfo( long newRow, long newColumn )
- {
- gosASSERT( newRow > -1 && newColumn > -1 );
- gosASSERT( newRow < land->realVerticesMapSide && newColumn < land->realVerticesMapSide );
- row = newRow;
- column = newColumn;
- elevation = land->getTerrainElevation( row, column );
- terrainData = land->getTerrain( row, column );
- textureData = land->getTexture( row, column );
- }
- #ifndef EDITOROBJECTMGR_H
- #include "EditorObjectMgr.h"
- #endif
- ModifyBuildingAction::~ModifyBuildingAction()
- {
- for ( OBJ_INFO_PTR_LIST::EIterator iter = buildingCopyPtrs.Begin(); !iter.IsDone(); iter++)
- {
- delete (*iter);
- }
- }
- bool ModifyBuildingAction::doRedo()
- {
- bool bRetVal = true;
- OBJ_INFO_PTR_LIST::EIterator iter2 = buildingPtrs.Begin();
- OBJ_APPEAR_LIST::EIterator iter3 = buildingAppearanceCopies.Begin();
- OBJ_ID_LIST::EIterator iter4 = buildingIDs.Begin();
- for ( OBJ_INFO_PTR_LIST::EIterator iter = buildingCopyPtrs.Begin();
- !iter.IsDone(); iter++)
- {
- //EditorObject *pBuilding = (*iter2);
- EditorObject *pBuilding = EditorObjectMgr::instance()->getObjectAtLocation((*iter4).x, (*iter4).y);
- if (pBuilding)
- {
- EditorObject *pBuildingSwap = (*iter)->Clone();
- ObjectAppearance AppearanceSwap = (*iter3);
- (*iter)->CastAndCopy(*pBuilding);
- (*iter3) = (*(pBuilding->appearance()));
- (*pBuilding).CastAndCopy(*pBuildingSwap);
- (*(pBuilding->appearance())) = AppearanceSwap;
- delete pBuildingSwap;
- {
- /*this is just to make sure the visuals are up-to-date*/
- bool d = pBuilding->getDamage();
- pBuilding->setDamage(!d);
- pBuilding->setDamage(d);
- }
- long row, column;
- pBuilding->getCells(row, column);
- EditorObjectMgr::instance()->moveBuilding(pBuilding, row, column);
- (*iter4).x = pBuilding->getPosition().x;
- (*iter4).y = pBuilding->getPosition().y;
- }
- else
- {
- gosASSERT(false);
- }
- iter2++;
- iter3++;
- iter4++;
- }
- return bRetVal;
- }
- bool ModifyBuildingAction::redo()
- {
- return doRedo();
- }
- bool ModifyBuildingAction::undo()
- {
- // actually, undo does the same thing as redo.
- return doRedo();
- }
- void ModifyBuildingAction::addBuildingInfo(EditorObject& info)
- {
- if ((0 < buildingPtrs.Count()) && (OBJ_INFO_PTR_LIST::INVALID_ITERATOR != buildingPtrs.Find(&info)))
- {
- return;
- }
- // if we made it here, it isn't in there already
- EditorObject *pInfoCopy = info.Clone();
- buildingCopyPtrs.Append( pInfoCopy );
- buildingAppearanceCopies.Append(*(info.appearance()));
- buildingPtrs.Append( &info );
- CObjectID id;
- id.x = info.getPosition().x;
- id.y = info.getPosition().y;
- buildingIDs.Append(id);
- }
- void ModifyBuildingAction::updateNotedObjectPositions()
- {
- OBJ_ID_LIST::EIterator iter4 = buildingIDs.Begin();
- for ( OBJ_INFO_PTR_LIST::EIterator iter = buildingPtrs.Begin(); !iter.IsDone(); iter++)
- {
- EditorObject *pBuilding = (*iter);
- if (pBuilding)
- {
- (*iter4).x = pBuilding->getPosition().x;
- (*iter4).y = pBuilding->getPosition().y;
- }
- else
- {
- gosASSERT(false);
- }
- iter4++;
- }
- }
|