* move class CUndoRedo from bteditdoc.cpp to new file undoredo.cpp

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@2363 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2007-08-08 20:39:07 +00:00
parent 243bc7ddb8
commit ba7d1d5078
9 changed files with 345 additions and 288 deletions

View File

@ -242,6 +242,10 @@ SOURCE=.\tiletypes.cpp
# End Source File
# Begin Source File
SOURCE=.\undoredo.cpp
# End Source File
# Begin Source File
SOURCE=.\wfview.cpp
# End Source File
# End Group

View File

@ -29,6 +29,7 @@
#include "tiletypes.h"
#include "debugprint.hpp"
#include "assert.h"
#include "undoredo.h"
extern DWORD g_Flags[MAXTILES];
extern CUndoRedo *g_UndoRedo;

View File

@ -44,6 +44,7 @@
#include "playermap.h"
#include "gateway.hpp"
#include "pasteprefs.h"
#include "undoredo.h"
#include <string>
@ -4668,266 +4669,6 @@ void CBTEditDoc::OnRedo()
Update3DView();
}
CUndoRedo::CUndoRedo(CHeightMap *HeightMap,UDWORD StackSize)
{
m_HeightMap = HeightMap;
m_StackSize = StackSize;
m_Stack = new UndoRecord[StackSize];
for(UDWORD i=0; i<StackSize; i++) {
memset(&m_Stack[i],0,sizeof(UndoRecord));
}
m_StackPointer = -1;
m_GroupRefCount = 0;
m_GroupActive = FALSE;
m_GroupCounter = 0;
}
CUndoRedo::~CUndoRedo(void)
{
for(UDWORD i=0; i<m_StackSize; i++) {
CleanUndoRecord(&m_Stack[i]);
}
delete m_Stack;
}
BOOL CUndoRedo::PushUndo(UndoRecord *UndoRec)
{
// Get the next record.
m_StackPointer++;
if(m_StackPointer >= m_StackSize) {
m_StackPointer = 0;
}
// If it's already in use then clean it.
if(m_Stack[m_StackPointer].Flags) {
CleanUndoRecord(&m_Stack[m_StackPointer]);
}
// Copy the undo data into it.
m_Stack[m_StackPointer] = *UndoRec;
// And mark it as new.
m_Stack[m_StackPointer].Flags = UF_DONE;
if(m_GroupActive) {
if(m_GroupCounter > 0) {
m_Stack[m_StackPointer].Group = UF_INGROUP;
} else {
m_Stack[m_StackPointer].Group = UF_BEGINGROUP;
}
} else {
m_Stack[m_StackPointer].Group = UF_NOGROUP;
}
if(m_GroupActive) {
m_GroupCounter++;
}
return TRUE;
}
void CUndoRedo::BeginGroup(void)
{
if(m_GroupRefCount == 0) {
m_GroupActive = TRUE;
m_GroupCounter = 0;
}
m_GroupRefCount++;
}
void CUndoRedo::EndGroup(void)
{
m_GroupRefCount--;
if(m_GroupRefCount == 0) {
m_Stack[m_StackPointer].Group = UF_ENDGROUP;
m_GroupActive = FALSE;
m_GroupCounter = 0;
}
}
BOOL CUndoRedo::PopUndo(UndoRecord *UndoRec,BOOL *IsGroup)
{
// If the current record is new.
if(m_Stack[m_StackPointer].Flags == UF_DONE) {
*IsGroup = (m_Stack[m_StackPointer].Group == UF_INGROUP) || (m_Stack[m_StackPointer].Group == UF_ENDGROUP);
// Return the record.
*UndoRec = m_Stack[m_StackPointer];
// Mark it as undone.
m_Stack[m_StackPointer].Flags = UF_UNDONE;
// Get previous record.
m_StackPointer--;
if(m_StackPointer < 0) {
m_StackPointer = m_StackSize-1;
}
return TRUE;
}
return FALSE;
}
BOOL CUndoRedo::GetRedo(UndoRecord *UndoRec,BOOL *IsGroup)
{
SWORD Tmp = m_StackPointer;
// Get next record.
m_StackPointer++;
if(m_StackPointer >= m_StackSize) {
m_StackPointer = 0;
}
if(m_Stack[m_StackPointer].Flags == UF_UNDONE) {
*IsGroup = (m_Stack[m_StackPointer].Group == UF_INGROUP) || (m_Stack[m_StackPointer].Group == UF_BEGINGROUP);
// Return the record.
*UndoRec = m_Stack[m_StackPointer];
// Mark it as done.
m_Stack[m_StackPointer].Flags = UF_DONE;
return TRUE;
}
m_StackPointer = Tmp;
return FALSE;
}
void CUndoRedo::CleanUndoRecord(UndoRecord *UndoRec)
{
if(UndoRec->Tile) delete UndoRec->Tile;
memset(UndoRec,0,sizeof(UndoRecord));
}
BOOL CUndoRedo::AddUndo(CTile *Tile)
{
UndoRecord UndoRec;
memset(&UndoRec,0,sizeof(UndoRecord));
UndoRec.TilePointer = Tile;
UndoRec.Tile = new CTile;
*UndoRec.Tile = *Tile;
PushUndo(&UndoRec);
return TRUE;
}
void CUndoRedo::DumpRedo(void)
{
static char *FlagNames[]={
"UF_FREE",
"UF_DONE",
"UF_UNDONE",
"UF_NOGROUP",
"UF_BEGINGROUP",
"UF_INGROUP",
"UF_ENDGROUP",
};
SWORD StackPointer = m_StackPointer;
StackPointer++;
if(StackPointer >= m_StackSize) {
StackPointer = 0;
}
while(m_Stack[StackPointer].Flags == UF_UNDONE) {
DebugPrint("sp %d : Flags %s Group %s\n",StackPointer,
FlagNames[m_Stack[StackPointer].Flags],
FlagNames[m_Stack[StackPointer].Group]);
// Get the next record.
StackPointer++;
if(StackPointer >= m_StackSize) {
StackPointer = 0;
}
}
}
void CUndoRedo::DumpUndo(void)
{
static char *FlagNames[]={
"UF_FREE",
"UF_DONE",
"UF_UNDONE",
"UF_NOGROUP",
"UF_BEGINGROUP",
"UF_INGROUP",
"UF_ENDGROUP",
};
SWORD StackPointer = m_StackPointer;
while(m_Stack[StackPointer].Flags == UF_DONE) {
DebugPrint("sp %d : Flags %s Group %s\n",StackPointer,
FlagNames[m_Stack[StackPointer].Flags],
FlagNames[m_Stack[StackPointer].Group]);
// Get previous record.
StackPointer--;
if(StackPointer < 0) {
StackPointer = m_StackSize-1;
}
}
}
BOOL CUndoRedo::Undo(void)
{
UndoRecord UndoRec;
BOOL IsGroup;
do {
if(!PopUndo(&UndoRec,&IsGroup)) {
return FALSE;
}
if(UndoRec.TilePointer) {
CTile Temp = *UndoRec.TilePointer;
*UndoRec.TilePointer = *UndoRec.Tile;
*UndoRec.Tile = Temp;
}
} while(IsGroup);
return TRUE;
}
BOOL CUndoRedo::Redo(void)
{
UndoRecord UndoRec;
BOOL IsGroup;
do {
if(!GetRedo(&UndoRec,&IsGroup)) {
return FALSE;
}
if(UndoRec.TilePointer) {
CTile Temp = *UndoRec.TilePointer;
*UndoRec.TilePointer = *UndoRec.Tile;
*UndoRec.Tile = Temp;
}
} while(IsGroup);
return TRUE;
}
void CBTEditDoc::OnViewGouraudshading()
{
if(m_ShadeMode != SM_GOURAUD) {

View File

@ -39,7 +39,6 @@
#include "chnkio.h"
#include "tiletypes.h"
#include "undoredo.h"
#include "brushprop.h"
#define EBVIEW_YOFFSET 32

View File

@ -36,6 +36,7 @@
#include "tiletypes.h"
#include "objectproperties.h"
#include "keyhandler.hpp"
#include "undoredo.h"
#define KEY_SEAUP 'U'
#define KEY_SEADOWN 'J'

View File

@ -26,6 +26,7 @@
#define __GRDLAND_INCLUDED__
#include "chnkio.h"
#include "typedefs.h"
class CGrdTileIO : public CChnkIO {
public:

View File

@ -0,0 +1,302 @@
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2007 Warzone Resurrection Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
$Revision$
$Id$
$HeadURL$
*/
#include "undoredo.h"
#include "debugprint.hpp"
CUndoRedo::CUndoRedo(CHeightMap* HeightMap, UDWORD StackSize) :
_HeightMap(HeightMap),
_StackSize(StackSize),
_StackPointer(-1),
_Stack(new UndoRecord[StackSize]),
_GroupRefCount(0),
_GroupActive(false),
_GroupCounter(0)
{
memset(_Stack, 0, sizeof(UndoRecord) * StackSize);
}
CUndoRedo::~CUndoRedo()
{
for(UDWORD i = 0; i < _StackSize; ++i)
{
CleanUndoRecord(_Stack[i]);
}
delete [] _Stack;
}
bool CUndoRedo::PushUndo(UndoRecord* UndoRec)
{
// Get the next record.
++_StackPointer;
if(_StackPointer >= _StackSize)
{
_StackPointer = 0;
}
// If it's already in use then clean it.
if(_Stack[_StackPointer].Flags)
{
CleanUndoRecord(_Stack[_StackPointer]);
}
// Copy the undo data into it.
_Stack[_StackPointer] = *UndoRec;
// And mark it as new.
_Stack[_StackPointer].Flags = UF_DONE;
if (_GroupActive)
{
if(_GroupCounter > 0)
{
_Stack[_StackPointer].Group = UF_INGROUP;
}
else
{
_Stack[_StackPointer].Group = UF_BEGINGROUP;
}
}
else
{
_Stack[_StackPointer].Group = UF_NOGROUP;
}
if (_GroupActive)
_GroupCounter++;
return true;
}
void CUndoRedo::BeginGroup()
{
if (_GroupRefCount == 0)
{
_GroupActive = true;
_GroupCounter = 0;
}
_GroupRefCount++;
}
void CUndoRedo::EndGroup()
{
--_GroupRefCount;
if (_GroupRefCount == 0)
{
_Stack[_StackPointer].Group = UF_ENDGROUP;
_GroupActive = false;
_GroupCounter = 0;
}
}
bool CUndoRedo::PopUndo(UndoRecord* UndoRec, bool* IsGroup)
{
// If the current record is new.
if (_Stack[_StackPointer].Flags == UF_DONE)
{
*IsGroup = (_Stack[_StackPointer].Group == UF_INGROUP) || (_Stack[_StackPointer].Group == UF_ENDGROUP);
// Return the record.
*UndoRec = _Stack[_StackPointer];
// Mark it as undone.
_Stack[_StackPointer].Flags = UF_UNDONE;
// Get previous record.
--_StackPointer;
if (_StackPointer < 0)
{
_StackPointer = _StackSize - 1;
}
return true;
}
return false;
}
bool CUndoRedo::GetRedo(UndoRecord* UndoRec, bool* IsGroup)
{
SWORD Tmp = _StackPointer;
// Get next record.
++_StackPointer;
if (_StackPointer >= _StackSize)
{
_StackPointer = 0;
}
if (_Stack[_StackPointer].Flags == UF_UNDONE)
{
*IsGroup = (_Stack[_StackPointer].Group == UF_INGROUP) || (_Stack[_StackPointer].Group == UF_BEGINGROUP);
// Return the record.
*UndoRec = _Stack[_StackPointer];
// Mark it as done.
_Stack[_StackPointer].Flags = UF_DONE;
return true;
}
_StackPointer = Tmp;
return false;
}
void CUndoRedo::CleanUndoRecord(UndoRecord& UndoRec)
{
if (UndoRec.Tile)
delete UndoRec.Tile;
memset(&UndoRec, 0, sizeof(UndoRecord));
}
bool CUndoRedo::AddUndo(CTile *Tile)
{
UndoRecord UndoRec;
memset(&UndoRec,0,sizeof(UndoRecord));
UndoRec.TilePointer = Tile;
UndoRec.Tile = new CTile;
*UndoRec.Tile = *Tile;
PushUndo(&UndoRec);
return true;
}
void CUndoRedo::DumpRedo()
{
static char *FlagNames[]={
"UF_FREE",
"UF_DONE",
"UF_UNDONE",
"UF_NOGROUP",
"UF_BEGINGROUP",
"UF_INGROUP",
"UF_ENDGROUP",
};
SWORD StackPointer = _StackPointer;
++StackPointer;
if (StackPointer >= _StackSize)
StackPointer = 0;
while (_Stack[StackPointer].Flags == UF_UNDONE)
{
DebugPrint("sp %d : Flags %s Group %s\n",StackPointer,
FlagNames[_Stack[StackPointer].Flags],
FlagNames[_Stack[StackPointer].Group]);
// Get the next record.
++StackPointer;
if (StackPointer >= _StackSize)
StackPointer = 0;
}
}
void CUndoRedo::DumpUndo()
{
static char *FlagNames[]={
"UF_FREE",
"UF_DONE",
"UF_UNDONE",
"UF_NOGROUP",
"UF_BEGINGROUP",
"UF_INGROUP",
"UF_ENDGROUP",
};
SWORD StackPointer = _StackPointer;
while (_Stack[StackPointer].Flags == UF_DONE)
{
DebugPrint("sp %d : Flags %s Group %s\n",StackPointer,
FlagNames[_Stack[StackPointer].Flags],
FlagNames[_Stack[StackPointer].Group]);
// Get previous record.
--StackPointer;
if (StackPointer < 0)
StackPointer = _StackSize - 1;
}
}
bool CUndoRedo::Undo()
{
UndoRecord UndoRec;
bool IsGroup;
do {
if(!PopUndo(&UndoRec,&IsGroup)) {
return false;
}
if(UndoRec.TilePointer) {
CTile Temp = *UndoRec.TilePointer;
*UndoRec.TilePointer = *UndoRec.Tile;
*UndoRec.Tile = Temp;
}
} while(IsGroup);
return true;
}
bool CUndoRedo::Redo()
{
UndoRecord UndoRec;
bool IsGroup;
do {
if(!GetRedo(&UndoRec,&IsGroup)) {
return false;
}
if(UndoRec.TilePointer) {
CTile Temp = *UndoRec.TilePointer;
*UndoRec.TilePointer = *UndoRec.Tile;
*UndoRec.Tile = Temp;
}
} while(IsGroup);
return true;
}

View File

@ -26,6 +26,7 @@
#define _INCLUDE_UNDOREDO_
#define UNDO_STACKSIZE 8192
#include "heightmap.h"
typedef enum {
UF_FREE,
@ -40,37 +41,43 @@ typedef enum {
struct UndoRecord {
UNDO_FLAGS Flags;
UNDO_FLAGS Group;
CTile *TilePointer; // Pointer to undo location.
CTile *Tile; // Undo data.
CTile* TilePointer; // Pointer to undo location.
CTile* Tile; // Undo data.
};
class CUndoRedo {
protected:
CHeightMap *m_HeightMap;
UWORD m_StackSize;
SWORD m_StackPointer;
UndoRecord *m_Stack;
SWORD m_GroupRefCount;
BOOL m_GroupActive;
SWORD m_GroupCounter;
class CUndoRedo
{
public:
CUndoRedo(CHeightMap* HeightMap, UDWORD StackSize);
~CUndoRedo();
public:
CUndoRedo(CHeightMap *HeightMap,UDWORD StackSize);
~CUndoRedo(void);
void BeginGroup(void);
BOOL AddUndo(CTile *Tile);
BOOL AddUndo(DWORD ToolType,int Selected);
void EndGroup(void);
BOOL Undo(void);
BOOL Redo(void);
void DumpUndo(void);
void DumpRedo(void);
void BeginGroup();
void EndGroup();
protected:
BOOL PushUndo(UndoRecord *UndoRec);
BOOL PopUndo(UndoRecord *UndoRec,BOOL *IsGroup);
BOOL GetRedo(UndoRecord *UndoRec,BOOL *IsGroup);
void CleanUndoRecord(UndoRecord *UndoRec);
bool AddUndo(CTile* Tile);
bool AddUndo(DWORD ToolType, int Selected);
bool Undo();
bool Redo();
void DumpUndo();
void DumpRedo();
private:
bool PushUndo(UndoRecord* UndoRec);
bool PopUndo(UndoRecord* UndoRec, bool* IsGroup);
bool GetRedo(UndoRecord* UndoRec, bool* IsGroup);
void CleanUndoRecord(UndoRecord& UndoRec);
private:
CHeightMap* _HeightMap;
UWORD _StackSize;
SWORD _StackPointer;
UndoRecord* _Stack;
SWORD _GroupRefCount;
bool _GroupActive;
SWORD _GroupCounter;
};
#endif

View File

@ -35,6 +35,7 @@
//#include "DebugWin.h"
#include "objectproperties.h"
#include "keyhandler.hpp"
#include "undoredo.h"
#define KEY_ROTATETILE 'R'
#define KEY_ROTATETILE2 'E'