mirror of
https://github.com/aseprite/aseprite.git
synced 2025-03-30 04:20:23 +00:00
Convert UndoStream in a class.
This commit is contained in:
parent
6f50c62ace
commit
03c5d0030a
@ -121,27 +121,7 @@ static void undo_chunk_free(UndoChunk* chunk);
|
|||||||
|
|
||||||
typedef std::list<UndoChunk*> ChunksList;
|
typedef std::list<UndoChunk*> ChunksList;
|
||||||
|
|
||||||
struct UndoStream
|
#include "undo_stream.h"
|
||||||
{
|
|
||||||
Undo* undo;
|
|
||||||
ChunksList chunks;
|
|
||||||
int size;
|
|
||||||
|
|
||||||
UndoStream(Undo* undo)
|
|
||||||
{
|
|
||||||
this->undo = undo;
|
|
||||||
this->size = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
~UndoStream()
|
|
||||||
{
|
|
||||||
ChunksList::iterator it = this->chunks.begin();
|
|
||||||
ChunksList::iterator end = this->chunks.end();
|
|
||||||
for (; it != end; ++it)
|
|
||||||
undo_chunk_free(*it);
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
struct UndoAction
|
struct UndoAction
|
||||||
{
|
{
|
||||||
@ -287,23 +267,25 @@ static Mask* read_raw_mask(ase_uint8* raw_data);
|
|||||||
static ase_uint8* write_raw_mask(ase_uint8* raw_data, Mask* mask);
|
static ase_uint8* write_raw_mask(ase_uint8* raw_data, Mask* mask);
|
||||||
static int get_raw_mask_size(Mask* mask);
|
static int get_raw_mask_size(Mask* mask);
|
||||||
|
|
||||||
/* UndoStream */
|
|
||||||
|
|
||||||
static UndoChunk* undo_stream_pop_chunk(UndoStream* stream, bool tail);
|
|
||||||
static void undo_stream_push_chunk(UndoStream* stream, UndoChunk* chunk);
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
Undo::Undo(Sprite* sprite)
|
Undo::Undo(Sprite* sprite)
|
||||||
: GfxObj(GFXOBJ_UNDO)
|
: GfxObj(GFXOBJ_UNDO)
|
||||||
{
|
{
|
||||||
m_sprite = sprite;
|
m_sprite = sprite;
|
||||||
m_undoStream = new UndoStream(this); // TODO try/catch
|
|
||||||
m_redoStream = new UndoStream(this);
|
|
||||||
m_diffCount = 0;
|
m_diffCount = 0;
|
||||||
m_diffSaved = 0;
|
m_diffSaved = 0;
|
||||||
m_enabled = true;
|
m_enabled = true;
|
||||||
m_label = NULL;
|
m_label = NULL;
|
||||||
|
|
||||||
|
m_undoStream = new UndoStream(this);
|
||||||
|
try {
|
||||||
|
m_redoStream = new UndoStream(this);
|
||||||
|
}
|
||||||
|
catch (...) {
|
||||||
|
delete m_undoStream;
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Undo::~Undo()
|
Undo::~Undo()
|
||||||
@ -324,12 +306,12 @@ void Undo::setEnabled(bool state)
|
|||||||
|
|
||||||
bool Undo::canUndo() const
|
bool Undo::canUndo() const
|
||||||
{
|
{
|
||||||
return !m_undoStream->chunks.empty();
|
return !m_undoStream->empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Undo::canRedo() const
|
bool Undo::canRedo() const
|
||||||
{
|
{
|
||||||
return !m_redoStream->chunks.empty();
|
return !m_redoStream->empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Undo::doUndo()
|
void Undo::doUndo()
|
||||||
@ -344,10 +326,8 @@ void Undo::doRedo()
|
|||||||
|
|
||||||
void Undo::clearRedo()
|
void Undo::clearRedo()
|
||||||
{
|
{
|
||||||
if (!m_redoStream->chunks.empty()) {
|
if (!m_redoStream->empty())
|
||||||
delete m_redoStream;
|
m_redoStream->clear();
|
||||||
m_redoStream = new UndoStream(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* Undo::getLabel()
|
const char* Undo::getLabel()
|
||||||
@ -366,7 +346,7 @@ const char* Undo::getNextUndoLabel() const
|
|||||||
|
|
||||||
ASSERT(canUndo());
|
ASSERT(canUndo());
|
||||||
|
|
||||||
chunk = *m_undoStream->chunks.begin();
|
chunk = *m_undoStream->begin();
|
||||||
return chunk->label;
|
return chunk->label;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -376,7 +356,7 @@ const char* Undo::getNextRedoLabel() const
|
|||||||
|
|
||||||
ASSERT(canRedo());
|
ASSERT(canRedo());
|
||||||
|
|
||||||
chunk = *m_redoStream->chunks.begin();
|
chunk = *m_redoStream->begin();
|
||||||
return chunk->label;
|
return chunk->label;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -400,7 +380,7 @@ void Undo::runUndo(int state)
|
|||||||
int level = 0;
|
int level = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
chunk = undo_stream_pop_chunk(undo_stream, false); // read from head
|
chunk = undo_stream->popChunk(false); // read from head
|
||||||
if (!chunk)
|
if (!chunk)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -428,7 +408,7 @@ void Undo::discardTail()
|
|||||||
int level = 0;
|
int level = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
chunk = undo_stream_pop_chunk(undo_stream, true); // read from tail
|
chunk = undo_stream->popChunk(true); // read from tail
|
||||||
if (!chunk)
|
if (!chunk)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -447,8 +427,8 @@ static int count_undo_groups(UndoStream* undo_stream)
|
|||||||
int groups = 0;
|
int groups = 0;
|
||||||
int level;
|
int level;
|
||||||
|
|
||||||
ChunksList::iterator it = undo_stream->chunks.begin();
|
ChunksList::iterator it = undo_stream->begin();
|
||||||
while (it != undo_stream->chunks.end()) {
|
while (it != undo_stream->end()) {
|
||||||
level = 0;
|
level = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
@ -459,7 +439,7 @@ static int count_undo_groups(UndoStream* undo_stream)
|
|||||||
level++;
|
level++;
|
||||||
else if (chunk->type == UNDO_TYPE_CLOSE)
|
else if (chunk->type == UNDO_TYPE_CLOSE)
|
||||||
level--;
|
level--;
|
||||||
} while (level && (it != undo_stream->chunks.end()));
|
} while (level && (it != undo_stream->end()));
|
||||||
|
|
||||||
if (level == 0)
|
if (level == 0)
|
||||||
groups++;
|
groups++;
|
||||||
@ -473,8 +453,8 @@ static bool out_of_group(UndoStream* undo_stream)
|
|||||||
UndoChunk* chunk;
|
UndoChunk* chunk;
|
||||||
int level = 0;
|
int level = 0;
|
||||||
|
|
||||||
ChunksList::iterator it = undo_stream->chunks.begin();
|
ChunksList::iterator it = undo_stream->begin();
|
||||||
while (it != undo_stream->chunks.end()) {
|
while (it != undo_stream->end()) {
|
||||||
level = 0;
|
level = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
@ -485,7 +465,7 @@ static bool out_of_group(UndoStream* undo_stream)
|
|||||||
level++;
|
level++;
|
||||||
else if (chunk->type == UNDO_TYPE_CLOSE)
|
else if (chunk->type == UNDO_TYPE_CLOSE)
|
||||||
level--;
|
level--;
|
||||||
} while (level && (it != undo_stream->chunks.end()));
|
} while (level && (it != undo_stream->end()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return level == 0;
|
return level == 0;
|
||||||
@ -506,7 +486,7 @@ void Undo::updateUndo()
|
|||||||
int groups = count_undo_groups(m_undoStream);
|
int groups = count_undo_groups(m_undoStream);
|
||||||
|
|
||||||
// "undo" is too big?
|
// "undo" is too big?
|
||||||
while (groups > 1 && m_undoStream->size > undo_size_limit) {
|
while (groups > 1 && m_undoStream->getMemSize() > undo_size_limit) {
|
||||||
discardTail();
|
discardTail();
|
||||||
groups--;
|
groups--;
|
||||||
}
|
}
|
||||||
@ -1788,11 +1768,11 @@ static UndoChunk* undo_chunk_new(UndoStream* stream, int type, int size)
|
|||||||
|
|
||||||
chunk->type = type;
|
chunk->type = type;
|
||||||
chunk->size = size;
|
chunk->size = size;
|
||||||
chunk->label = stream->undo->getLabel() ?
|
chunk->label = stream->getUndo()->getLabel() ?
|
||||||
stream->undo->getLabel():
|
stream->getUndo()->getLabel():
|
||||||
undo_actions[chunk->type].name;
|
undo_actions[chunk->type].name;
|
||||||
|
|
||||||
undo_stream_push_chunk(stream, chunk);
|
stream->pushChunk(chunk);
|
||||||
return chunk;
|
return chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2440,36 +2420,3 @@ static int get_raw_mask_size(Mask* mask)
|
|||||||
|
|
||||||
return 2*4 + (mask->bitmap ? mask->h*size: 0);
|
return 2*4 + (mask->bitmap ? mask->h*size: 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/***********************************************************************
|
|
||||||
|
|
||||||
Helper routines for UndoStream (a serie of UndoChunks)
|
|
||||||
|
|
||||||
***********************************************************************/
|
|
||||||
|
|
||||||
static UndoChunk* undo_stream_pop_chunk(UndoStream* stream, bool tail)
|
|
||||||
{
|
|
||||||
UndoChunk* chunk;
|
|
||||||
ChunksList::iterator it;
|
|
||||||
|
|
||||||
if (!stream->chunks.empty()) {
|
|
||||||
if (!tail)
|
|
||||||
it = stream->chunks.begin();
|
|
||||||
else
|
|
||||||
it = --stream->chunks.end();
|
|
||||||
|
|
||||||
chunk = *it;
|
|
||||||
stream->chunks.erase(it);
|
|
||||||
stream->size -= chunk->size;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
chunk = NULL;
|
|
||||||
|
|
||||||
return chunk;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void undo_stream_push_chunk(UndoStream* stream, UndoChunk* chunk)
|
|
||||||
{
|
|
||||||
stream->chunks.insert(stream->chunks.begin(), chunk);
|
|
||||||
stream->size += chunk->size;
|
|
||||||
}
|
|
||||||
|
@ -34,7 +34,7 @@ class Sprite;
|
|||||||
class Stock;
|
class Stock;
|
||||||
|
|
||||||
struct Dirty;
|
struct Dirty;
|
||||||
struct UndoStream;
|
class UndoStream;
|
||||||
|
|
||||||
class UndoException : public ase_exception
|
class UndoException : public ase_exception
|
||||||
{
|
{
|
||||||
|
96
src/raster/undo_stream.h
Normal file
96
src/raster/undo_stream.h
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
/* ASE - Allegro Sprite Editor
|
||||||
|
* Copyright (C) 2001-2010 David Capello
|
||||||
|
*
|
||||||
|
* This program 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.
|
||||||
|
*
|
||||||
|
* This program 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 this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef RASTER_UNDO_STREAM_H_INCLUDED
|
||||||
|
#define RASTER_UNDO_STREAM_H_INCLUDED
|
||||||
|
|
||||||
|
class UndoStream
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
typedef ChunksList::iterator iterator;
|
||||||
|
|
||||||
|
UndoStream(Undo* undo)
|
||||||
|
{
|
||||||
|
m_undo = undo;
|
||||||
|
m_size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
~UndoStream()
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
Undo* getUndo() const
|
||||||
|
{
|
||||||
|
return m_undo;
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator begin() { return m_chunks.begin(); }
|
||||||
|
iterator end() { return m_chunks.end(); }
|
||||||
|
bool empty() const { return m_chunks.empty(); }
|
||||||
|
|
||||||
|
void clear()
|
||||||
|
{
|
||||||
|
iterator it = begin();
|
||||||
|
iterator end = this->end();
|
||||||
|
for (; it != end; ++it)
|
||||||
|
undo_chunk_free(*it);
|
||||||
|
|
||||||
|
m_size = 0;
|
||||||
|
m_chunks.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
int getMemSize() const
|
||||||
|
{
|
||||||
|
return m_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
UndoChunk* popChunk(bool tail)
|
||||||
|
{
|
||||||
|
UndoChunk* chunk;
|
||||||
|
iterator it;
|
||||||
|
|
||||||
|
if (!empty()) {
|
||||||
|
if (!tail)
|
||||||
|
it = begin();
|
||||||
|
else
|
||||||
|
it = --end();
|
||||||
|
|
||||||
|
chunk = *it;
|
||||||
|
m_chunks.erase(it);
|
||||||
|
m_size -= chunk->size;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
chunk = NULL;
|
||||||
|
|
||||||
|
return chunk;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pushChunk(UndoChunk* chunk)
|
||||||
|
{
|
||||||
|
m_chunks.insert(begin(), chunk);
|
||||||
|
m_size += chunk->size;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Undo* m_undo;
|
||||||
|
ChunksList m_chunks;
|
||||||
|
int m_size;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
Loading…
x
Reference in New Issue
Block a user