From 682e7152b76915bc73f298f04806c9fd749cf6b4 Mon Sep 17 00:00:00 2001 From: David Capello Date: Sun, 27 Mar 2011 18:15:00 -0300 Subject: [PATCH] Replace UndoHistory::undo_int/data/double methods with new safe-undoers. + Added SetCelFrame/Opacity/Position, SetMaskPosition, SetStockImgType, and SetLayerFlags undoers. + Finally removed the Layer::flags_addr() atrocity. + Removed RawData undoer. + Renamed SetImgType to SetSpriteImgType. --- src/CMakeLists.txt | 9 +++- src/commands/cmd_cel_properties.cpp | 4 +- src/commands/cmd_merge_down_layer.cpp | 7 ++- src/file/ase_format.cpp | 6 +-- src/raster/layer.cpp | 3 +- src/raster/layer.h | 18 ++++--- src/raster/layer_io.cpp | 13 +++-- src/undo/undo_history.h | 9 ---- src/undo/undo_history_backward.cpp | 10 +--- src/undo_transaction.cpp | 31 ++++++----- src/undoers/set_cel_frame.cpp | 49 ++++++++++++++++++ src/undoers/{raw_data.h => set_cel_frame.h} | 20 ++++---- src/undoers/set_cel_opacity.cpp | 49 ++++++++++++++++++ src/undoers/set_cel_opacity.h | 46 +++++++++++++++++ src/undoers/set_cel_position.cpp | 51 +++++++++++++++++++ src/undoers/set_cel_position.h | 46 +++++++++++++++++ src/undoers/set_layer_flags.cpp | 49 ++++++++++++++++++ src/undoers/set_layer_flags.h | 47 +++++++++++++++++ .../{raw_data.cpp => set_mask_position.cpp} | 28 +++++----- src/undoers/set_mask_position.h | 47 +++++++++++++++++ ...set_imgtype.cpp => set_sprite_imgtype.cpp} | 12 ++--- src/undoers/set_sprite_imgtype.h | 45 ++++++++++++++++ src/undoers/set_stock_imgtype.cpp | 49 ++++++++++++++++++ src/undoers/set_stock_imgtype.h | 45 ++++++++++++++++ src/util/celmove.cpp | 10 ++-- src/util/misc.cpp | 7 ++- src/widgets/editor/editor.cpp | 15 +++--- 27 files changed, 621 insertions(+), 104 deletions(-) create mode 100644 src/undoers/set_cel_frame.cpp rename src/undoers/{raw_data.h => set_cel_frame.h} (69%) create mode 100644 src/undoers/set_cel_opacity.cpp create mode 100644 src/undoers/set_cel_opacity.h create mode 100644 src/undoers/set_cel_position.cpp create mode 100644 src/undoers/set_cel_position.h create mode 100644 src/undoers/set_layer_flags.cpp create mode 100644 src/undoers/set_layer_flags.h rename src/undoers/{raw_data.cpp => set_mask_position.cpp} (55%) create mode 100644 src/undoers/set_mask_position.h rename src/undoers/{set_imgtype.cpp => set_sprite_imgtype.cpp} (74%) create mode 100644 src/undoers/set_sprite_imgtype.h create mode 100644 src/undoers/set_stock_imgtype.cpp create mode 100644 src/undoers/set_stock_imgtype.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fcc39e270..d08dcd87a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -241,21 +241,26 @@ add_library(aseprite-library undoers/image_area.cpp undoers/move_layer.cpp undoers/open_group.cpp - undoers/raw_data.cpp undoers/remap_palette.cpp undoers/remove_cel.cpp undoers/remove_image.cpp undoers/remove_layer.cpp undoers/remove_palette.cpp undoers/replace_image.cpp + undoers/set_cel_frame.cpp + undoers/set_cel_opacity.cpp + undoers/set_cel_position.cpp undoers/set_current_frame.cpp undoers/set_current_layer.cpp undoers/set_frame_duration.cpp - undoers/set_imgtype.cpp + undoers/set_layer_flags.cpp undoers/set_layer_name.cpp undoers/set_mask.cpp + undoers/set_mask_position.cpp undoers/set_palette_colors.cpp + undoers/set_sprite_imgtype.cpp undoers/set_sprite_size.cpp + undoers/set_stock_imgtype.cpp undoers/set_total_frames.cpp util/autocrop.cpp util/boundary.cpp diff --git a/src/commands/cmd_cel_properties.cpp b/src/commands/cmd_cel_properties.cpp index 9cbdeb1c4..c55105534 100644 --- a/src/commands/cmd_cel_properties.cpp +++ b/src/commands/cmd_cel_properties.cpp @@ -30,6 +30,7 @@ #include "raster/sprite.h" #include "raster/stock.h" #include "undo/undo_history.h" +#include "undoers/set_cel_opacity.h" #include @@ -138,7 +139,8 @@ void CelPropertiesCommand::onExecute(Context* context) if (undo->isEnabled()) { undo->setLabel("Cel Opacity Change"); undo->setModification(undo::ModifyDocument); - undo->undo_int(cel_writer, &cel_writer->opacity); + + undo->pushUndoer(new undoers::SetCelOpacity(undo->getObjects(), cel_writer)); } /* change cel opacity */ diff --git a/src/commands/cmd_merge_down_layer.cpp b/src/commands/cmd_merge_down_layer.cpp index 043c2704e..8ee6fd6d5 100644 --- a/src/commands/cmd_merge_down_layer.cpp +++ b/src/commands/cmd_merge_down_layer.cpp @@ -29,6 +29,7 @@ #include "raster/sprite.h" #include "raster/stock.h" #include "undo/undo_history.h" +#include "undoers/set_cel_position.h" ////////////////////////////////////////////////////////////////////// // merge_down_layer @@ -162,10 +163,8 @@ void MergeDownLayerCommand::onExecute(Context* context) src_cel->opacity, static_cast(src_layer)->getBlendMode()); - if (undo->isEnabled()) { - undo->undo_int(dst_cel, &dst_cel->x); - undo->undo_int(dst_cel, &dst_cel->y); - } + if (undo->isEnabled()) + undo->pushUndoer(new undoers::SetCelPosition(undo->getObjects(), dst_cel)); cel_set_position(dst_cel, x1, y1); diff --git a/src/file/ase_format.cpp b/src/file/ase_format.cpp index 4c07ac6d5..30eaae81a 100644 --- a/src/file/ase_format.cpp +++ b/src/file/ase_format.cpp @@ -679,7 +679,7 @@ static Layer *ase_file_read_layer_chunk(FILE *f, Sprite *sprite, Layer **previou if (layer) { // flags - *layer->flags_addr() = flags; + layer->setFlags(flags); // name layer->setName(name.c_str()); @@ -703,8 +703,8 @@ static void ase_file_write_layer_chunk(FILE *f, Layer *layer) { ase_file_write_start_chunk(f, ASE_FILE_CHUNK_LAYER); - /* flags */ - fputw(*layer->flags_addr(), f); + // Flags + fputw(layer->getFlags(), f); /* layer type */ fputw(layer->is_image() ? 0: (layer->is_folder() ? 1: -1), f); diff --git a/src/raster/layer.cpp b/src/raster/layer.cpp index 648b837cf..b94e8d3c3 100644 --- a/src/raster/layer.cpp +++ b/src/raster/layer.cpp @@ -208,7 +208,8 @@ void LayerImage::configureAsBackground() ASSERT(getSprite() != NULL); ASSERT(getSprite()->getBackgroundLayer() == NULL); - *flags_addr() |= LAYER_IS_LOCKMOVE | LAYER_IS_BACKGROUND; + set_moveable(false); + set_background(true); setName("Background"); getSprite()->getFolder()->move_layer(this, NULL); diff --git a/src/raster/layer.h b/src/raster/layer.h index be25fbc17..2da685476 100644 --- a/src/raster/layer.h +++ b/src/raster/layer.h @@ -30,14 +30,16 @@ class Layer; class LayerImage; class LayerFolder; -#define LAYER_IS_READABLE 0x0001 -#define LAYER_IS_WRITABLE 0x0002 -#define LAYER_IS_LOCKMOVE 0x0004 -#define LAYER_IS_BACKGROUND 0x0008 - ////////////////////////////////////////////////////////////////////// // Layer class +enum { + LAYER_IS_READABLE = 0x0001, + LAYER_IS_WRITABLE = 0x0002, + LAYER_IS_LOCKMOVE = 0x0004, + LAYER_IS_BACKGROUND = 0x0008, +}; + class Layer : public GfxObj { protected: @@ -69,10 +71,10 @@ public: void set_readable(bool b) { if (b) m_flags |= LAYER_IS_READABLE; else m_flags &= ~LAYER_IS_READABLE; } void set_writable(bool b) { if (b) m_flags |= LAYER_IS_WRITABLE; else m_flags &= ~LAYER_IS_WRITABLE; } - virtual void getCels(CelList& cels) = 0; + uint32_t getFlags() const { return m_flags; } + void setFlags(uint32_t flags) { m_flags = flags; } - // TODO remove these methods (from C backward-compatibility) - unsigned short* flags_addr() { return &m_flags; } + virtual void getCels(CelList& cels) = 0; private: std::string m_name; // layer name diff --git a/src/raster/layer_io.cpp b/src/raster/layer_io.cpp index 25751cc55..e6e2c4a2d 100644 --- a/src/raster/layer_io.cpp +++ b/src/raster/layer_io.cpp @@ -45,7 +45,7 @@ void write_layer(std::ostream& os, LayerSubObjectsSerializer* subObjects, Layer* if (!name.empty()) os.write(name.c_str(), name.size()); // Name - write8(os, *layer->flags_addr()); // Flags + write32(os, layer->getFlags()); // Flags write16(os, layer->getType()); // Type switch (layer->getType()) { @@ -86,9 +86,6 @@ void write_layer(std::ostream& os, LayerSubObjectsSerializer* subObjects, Layer* Layer* read_layer(std::istream& is, LayerSubObjectsSerializer* subObjects, Sprite* sprite) { - int flags, layer_type; - UniquePtr layer; - uint16_t name_length = read16(is); // Name length std::vector name(name_length+1); if (name_length > 0) { @@ -98,8 +95,10 @@ Layer* read_layer(std::istream& is, LayerSubObjectsSerializer* subObjects, Sprit else name[0] = 0; - flags = read8(is); // Flags - layer_type = read16(is); // Type + uint32_t flags = read32(is); // Flags + uint16_t layer_type = read16(is); // Type + + UniquePtr layer; switch (layer_type) { @@ -147,7 +146,7 @@ Layer* read_layer(std::istream& is, LayerSubObjectsSerializer* subObjects, Sprit if (layer != NULL) { layer->setName(&name[0]); - *layer->flags_addr() = flags; + layer->setFlags(flags); } return layer.release(); diff --git a/src/undo/undo_history.h b/src/undo/undo_history.h index 2d88c5ba6..76c480355 100644 --- a/src/undo/undo_history.h +++ b/src/undo/undo_history.h @@ -70,7 +70,6 @@ public: // Backward compatibility methods void undo_open(); void undo_close(); - void undo_data(void* object, void* fieldAddress, int fieldSize); void undo_image(Image *image, int x, int y, int w, int h); void undo_flip(Image *image, int x1, int y1, int x2, int y2, bool horz); void undo_dirty(Image* image, Dirty *dirty); @@ -95,14 +94,6 @@ public: void undo_set_frames(Sprite* sprite); void undo_set_frlen(Sprite* sprite, int frame); - void undo_int(GfxObj* gfxobj, int* value_address) { - undo_data(gfxobj, (void*)(value_address), sizeof(int)); - } - - void undo_double(GfxObj* gfxobj, double* value_address) { - undo_data(gfxobj, (void*)(value_address), sizeof(double)); - } - private: enum Direction { UndoDirection, RedoDirection }; diff --git a/src/undo/undo_history_backward.cpp b/src/undo/undo_history_backward.cpp index 0f2188fea..9dc01d0c6 100644 --- a/src/undo/undo_history_backward.cpp +++ b/src/undo/undo_history_backward.cpp @@ -20,7 +20,6 @@ #include "undoers/image_area.h" #include "undoers/move_layer.h" #include "undoers/open_group.h" -#include "undoers/raw_data.h" #include "undoers/remap_palette.h" #include "undoers/remove_cel.h" #include "undoers/remove_image.h" @@ -30,10 +29,10 @@ #include "undoers/set_current_frame.h" #include "undoers/set_current_layer.h" #include "undoers/set_frame_duration.h" -#include "undoers/set_imgtype.h" #include "undoers/set_layer_name.h" #include "undoers/set_mask.h" #include "undoers/set_palette_colors.h" +#include "undoers/set_sprite_imgtype.h" #include "undoers/set_sprite_size.h" #include "undoers/set_total_frames.h" @@ -49,11 +48,6 @@ void UndoHistory::undo_close() pushUndoer(new undoers::CloseGroup()); } -void UndoHistory::undo_data(void* object, void* fieldAddress, int fieldSize) -{ - pushUndoer(new undoers::RawData(getObjects(), object, fieldAddress, fieldSize)); -} - void UndoHistory::undo_image(Image* image, int x, int y, int w, int h) { pushUndoer(new undoers::ImageArea(getObjects(), image, x, y, w, h)); @@ -148,7 +142,7 @@ void UndoHistory::undo_set_mask(Document* document) void UndoHistory::undo_set_imgtype(Sprite* sprite) { - pushUndoer(new undoers::SetImgType(getObjects(), sprite)); + pushUndoer(new undoers::SetSpriteImgType(getObjects(), sprite)); } void UndoHistory::undo_set_size(Sprite* sprite) diff --git a/src/undo_transaction.cpp b/src/undo_transaction.cpp index 968969deb..4be20d3e7 100644 --- a/src/undo_transaction.cpp +++ b/src/undo_transaction.cpp @@ -33,6 +33,11 @@ #include "raster/sprite.h" #include "raster/stock.h" #include "undo/undo_history.h" +#include "undoers/set_cel_frame.h" +#include "undoers/set_cel_position.h" +#include "undoers/set_layer_flags.h" +#include "undoers/set_mask_position.h" +#include "undoers/set_stock_imgtype.h" UndoTransaction::UndoTransaction(Document* document, const char* label, undo::Modification modification) { @@ -221,9 +226,9 @@ void UndoTransaction::setImgType(int new_imgtype, DitheringMethod dithering_meth if (m_sprite->getImgType() == new_imgtype) return; - /* change imgtype of the stock of images */ + // Change imgtype of the stock of images. if (isEnabled()) - m_undoHistory->undo_int(m_sprite->getStock(), &m_sprite->getStock()->m_imgtype); + m_undoHistory->pushUndoer(new undoers::SetStockImgType(m_undoHistory->getObjects(), m_sprite->getStock())); m_sprite->getStock()->setImgType(new_imgtype); @@ -508,10 +513,8 @@ void UndoTransaction::layerFromBackground() ASSERT(m_sprite->getCurrentLayer()->is_background()); if (isEnabled()) { - m_undoHistory->undo_data( - m_sprite->getCurrentLayer(), - m_sprite->getCurrentLayer()->flags_addr(), - sizeof(*m_sprite->getCurrentLayer()->flags_addr())); + m_undoHistory->pushUndoer(new undoers::SetLayerFlags(m_undoHistory->getObjects(), + m_sprite->getCurrentLayer())); m_undoHistory->undo_set_layer_name(m_sprite->getCurrentLayer()); } @@ -618,7 +621,7 @@ void UndoTransaction::flattenLayers(int bgcolor) void UndoTransaction::configureLayerAsBackground(LayerImage* layer) { if (isEnabled()) { - m_undoHistory->undo_data(layer, layer->flags_addr(), sizeof(*layer->flags_addr())); + m_undoHistory->pushUndoer(new undoers::SetLayerFlags(m_undoHistory->getObjects(), layer)); m_undoHistory->undo_set_layer_name(layer); m_undoHistory->undo_move_layer(layer); } @@ -796,7 +799,7 @@ void UndoTransaction::setCelFramePosition(Cel* cel, int frame) ASSERT(frame >= 0); if (isEnabled()) - m_undoHistory->undo_int(cel, &cel->frame); + m_undoHistory->pushUndoer(new undoers::SetCelFrame(m_undoHistory->getObjects(), cel)); cel->frame = frame; } @@ -805,10 +808,8 @@ void UndoTransaction::setCelPosition(Cel* cel, int x, int y) { ASSERT(cel); - if (isEnabled()) { - m_undoHistory->undo_int(cel, &cel->x); - m_undoHistory->undo_int(cel, &cel->y); - } + if (isEnabled()) + m_undoHistory->pushUndoer(new undoers::SetCelPosition(m_undoHistory->getObjects(), cel)); cel->x = x; cel->y = y; @@ -1071,10 +1072,8 @@ void UndoTransaction::setMaskPosition(int x, int y) { ASSERT(m_document->getMask()); - if (isEnabled()) { - m_undoHistory->undo_int(m_document->getMask(), &m_document->getMask()->x); - m_undoHistory->undo_int(m_document->getMask(), &m_document->getMask()->y); - } + if (isEnabled()) + m_undoHistory->pushUndoer(new undoers::SetMaskPosition(m_undoHistory->getObjects(), m_document)); m_document->getMask()->x = x; m_document->getMask()->y = y; diff --git a/src/undoers/set_cel_frame.cpp b/src/undoers/set_cel_frame.cpp new file mode 100644 index 000000000..88b20d67d --- /dev/null +++ b/src/undoers/set_cel_frame.cpp @@ -0,0 +1,49 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 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 + */ + +#include "config.h" + +#include "undoers/set_cel_frame.h" + +#include "raster/cel.h" +#include "undo/objects_container.h" +#include "undo/undoers_collector.h" + +using namespace undo; +using namespace undoers; + +SetCelFrame::SetCelFrame(ObjectsContainer* objects, Cel* cel) + : m_celId(objects->addObject(cel)) + , m_frame(cel->frame) +{ +} + +void SetCelFrame::dispose() +{ + delete this; +} + +void SetCelFrame::revert(ObjectsContainer* objects, UndoersCollector* redoers) +{ + Cel* cel = objects->getObjectT(m_celId); + + // Push another SetCelFrame as redoer + redoers->pushUndoer(new SetCelFrame(objects, cel)); + + cel->frame = m_frame; +} diff --git a/src/undoers/raw_data.h b/src/undoers/set_cel_frame.h similarity index 69% rename from src/undoers/raw_data.h rename to src/undoers/set_cel_frame.h index 948715eb8..e241a6b78 100644 --- a/src/undoers/raw_data.h +++ b/src/undoers/set_cel_frame.h @@ -16,31 +16,31 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#ifndef UNDOERS_RAW_DATA_H_INCLUDED -#define UNDOERS_RAW_DATA_H_INCLUDED +#ifndef UNDOERS_SET_CEL_FRAME_H_INCLUDED +#define UNDOERS_SET_CEL_FRAME_H_INCLUDED #include "undo/object_id.h" #include "undoers/undoer_base.h" -#include +class Cel; +class Layer; namespace undoers { -class RawData : public UndoerBase +class SetCelFrame : public UndoerBase { public: - RawData(undo::ObjectsContainer* objects, void* object, void* fieldAddress, int fieldSize); + SetCelFrame(undo::ObjectsContainer* objects, Cel* cel); void dispose() OVERRIDE; - int getMemSize() const OVERRIDE { return sizeof(*this) + m_data.size(); } + int getMemSize() const OVERRIDE { return sizeof(*this); } void revert(undo::ObjectsContainer* objects, undo::UndoersCollector* redoers) OVERRIDE; private: - undo::ObjectId m_objectId; - uint32_t m_offset; - std::vector m_data; + undo::ObjectId m_celId; + int m_frame; }; } // namespace undoers -#endif // UNDOERS_RAW_DATA_H_INCLUDED +#endif // UNDOERS_SET_CEL_FRAME_H_INCLUDED diff --git a/src/undoers/set_cel_opacity.cpp b/src/undoers/set_cel_opacity.cpp new file mode 100644 index 000000000..d0127b585 --- /dev/null +++ b/src/undoers/set_cel_opacity.cpp @@ -0,0 +1,49 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 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 + */ + +#include "config.h" + +#include "undoers/set_cel_opacity.h" + +#include "raster/cel.h" +#include "undo/objects_container.h" +#include "undo/undoers_collector.h" + +using namespace undo; +using namespace undoers; + +SetCelOpacity::SetCelOpacity(ObjectsContainer* objects, Cel* cel) + : m_celId(objects->addObject(cel)) + , m_opacity(cel->opacity) +{ +} + +void SetCelOpacity::dispose() +{ + delete this; +} + +void SetCelOpacity::revert(ObjectsContainer* objects, UndoersCollector* redoers) +{ + Cel* cel = objects->getObjectT(m_celId); + + // Push another SetCelOpacity as redoer + redoers->pushUndoer(new SetCelOpacity(objects, cel)); + + cel->opacity = m_opacity; +} diff --git a/src/undoers/set_cel_opacity.h b/src/undoers/set_cel_opacity.h new file mode 100644 index 000000000..9512b7829 --- /dev/null +++ b/src/undoers/set_cel_opacity.h @@ -0,0 +1,46 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 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 UNDOERS_SET_CEL_OPACITY_H_INCLUDED +#define UNDOERS_SET_CEL_OPACITY_H_INCLUDED + +#include "undo/object_id.h" +#include "undoers/undoer_base.h" + +class Cel; +class Layer; + +namespace undoers { + +class SetCelOpacity : public UndoerBase +{ +public: + SetCelOpacity(undo::ObjectsContainer* objects, Cel* cel); + + void dispose() OVERRIDE; + int getMemSize() const OVERRIDE { return sizeof(*this); } + void revert(undo::ObjectsContainer* objects, undo::UndoersCollector* redoers) OVERRIDE; + +private: + undo::ObjectId m_celId; + uint8_t m_opacity; +}; + +} // namespace undoers + +#endif // UNDOERS_SET_CEL_OPACITY_H_INCLUDED diff --git a/src/undoers/set_cel_position.cpp b/src/undoers/set_cel_position.cpp new file mode 100644 index 000000000..8268d9a02 --- /dev/null +++ b/src/undoers/set_cel_position.cpp @@ -0,0 +1,51 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 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 + */ + +#include "config.h" + +#include "undoers/set_cel_position.h" + +#include "raster/cel.h" +#include "undo/objects_container.h" +#include "undo/undoers_collector.h" + +using namespace undo; +using namespace undoers; + +SetCelPosition::SetCelPosition(ObjectsContainer* objects, Cel* cel) + : m_celId(objects->addObject(cel)) + , m_x(cel->x) + , m_y(cel->y) +{ +} + +void SetCelPosition::dispose() +{ + delete this; +} + +void SetCelPosition::revert(ObjectsContainer* objects, UndoersCollector* redoers) +{ + Cel* cel = objects->getObjectT(m_celId); + + // Push another SetCelPosition as redoer + redoers->pushUndoer(new SetCelPosition(objects, cel)); + + cel->x = m_x; + cel->y = m_y; +} diff --git a/src/undoers/set_cel_position.h b/src/undoers/set_cel_position.h new file mode 100644 index 000000000..fc62ca858 --- /dev/null +++ b/src/undoers/set_cel_position.h @@ -0,0 +1,46 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 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 UNDOERS_SET_CEL_POSITION_H_INCLUDED +#define UNDOERS_SET_CEL_POSITION_H_INCLUDED + +#include "undo/object_id.h" +#include "undoers/undoer_base.h" + +class Cel; +class Layer; + +namespace undoers { + +class SetCelPosition : public UndoerBase +{ +public: + SetCelPosition(undo::ObjectsContainer* objects, Cel* cel); + + void dispose() OVERRIDE; + int getMemSize() const OVERRIDE { return sizeof(*this); } + void revert(undo::ObjectsContainer* objects, undo::UndoersCollector* redoers) OVERRIDE; + +private: + undo::ObjectId m_celId; + int m_x, m_y; +}; + +} // namespace undoers + +#endif // UNDOERS_SET_CEL_POSITION_H_INCLUDED diff --git a/src/undoers/set_layer_flags.cpp b/src/undoers/set_layer_flags.cpp new file mode 100644 index 000000000..7836669ab --- /dev/null +++ b/src/undoers/set_layer_flags.cpp @@ -0,0 +1,49 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 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 + */ + +#include "config.h" + +#include "undoers/set_layer_flags.h" + +#include "raster/layer.h" +#include "undo/objects_container.h" +#include "undo/undoers_collector.h" + +using namespace undo; +using namespace undoers; + +SetLayerFlags::SetLayerFlags(ObjectsContainer* objects, Layer* layer) + : m_layerId(objects->addObject(layer)) + , m_flags(layer->getFlags()) +{ +} + +void SetLayerFlags::dispose() +{ + delete this; +} + +void SetLayerFlags::revert(ObjectsContainer* objects, UndoersCollector* redoers) +{ + Layer* layer = objects->getObjectT(m_layerId); + + // Push another SetLayerFlags as redoer + redoers->pushUndoer(new SetLayerFlags(objects, layer)); + + layer->setFlags(m_flags); +} diff --git a/src/undoers/set_layer_flags.h b/src/undoers/set_layer_flags.h new file mode 100644 index 000000000..171165ac6 --- /dev/null +++ b/src/undoers/set_layer_flags.h @@ -0,0 +1,47 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 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 UNDOERS_SET_LAYER_FLAGS_H_INCLUDED +#define UNDOERS_SET_LAYER_FLAGS_H_INCLUDED + +#include "undo/object_id.h" +#include "undoers/undoer_base.h" + +#include + +class Layer; + +namespace undoers { + +class SetLayerFlags : public UndoerBase +{ +public: + SetLayerFlags(undo::ObjectsContainer* objects, Layer* layer); + + void dispose() OVERRIDE; + int getMemSize() const OVERRIDE { return sizeof(*this); } + void revert(undo::ObjectsContainer* objects, undo::UndoersCollector* redoers) OVERRIDE; + +private: + undo::ObjectId m_layerId; + uint32_t m_flags; +}; + +} // namespace undoers + +#endif // UNDOERS_SET_LAYER_FLAGS_H_INCLUDED diff --git a/src/undoers/raw_data.cpp b/src/undoers/set_mask_position.cpp similarity index 55% rename from src/undoers/raw_data.cpp rename to src/undoers/set_mask_position.cpp index 751678b05..93d9fe072 100644 --- a/src/undoers/raw_data.cpp +++ b/src/undoers/set_mask_position.cpp @@ -18,35 +18,35 @@ #include "config.h" -#include "undoers/raw_data.h" +#include "undoers/set_mask_position.h" +#include "document.h" +#include "raster/mask.h" #include "undo/objects_container.h" #include "undo/undoers_collector.h" using namespace undo; using namespace undoers; -RawData::RawData(ObjectsContainer* objects, void* object, void* fieldAddress, int fieldSize) - : m_objectId(objects->addObject(object)) - , m_offset((uint32_t)(((uint8_t*)fieldAddress) - ((uint8_t*)object))) - , m_data(fieldSize) +SetMaskPosition::SetMaskPosition(ObjectsContainer* objects, Document* document) + : m_documentId(objects->addObject(document)) + , m_x(document->getMask()->x) + , m_y(document->getMask()->y) { - memcpy(&m_data[0], fieldAddress, m_data.size()); } -void RawData::dispose() +void SetMaskPosition::dispose() { delete this; } -void RawData::revert(ObjectsContainer* objects, UndoersCollector* redoers) +void SetMaskPosition::revert(ObjectsContainer* objects, UndoersCollector* redoers) { - void* object = objects->getObject(m_objectId); - void* fieldAddress = (void*)(((uint8_t*)object) + m_offset); + Document* document = objects->getObjectT(m_documentId); - // Save the current data - redoers->pushUndoer(new RawData(objects, object, fieldAddress, m_data.size())); + // Push another SetMaskPosition as redoer + redoers->pushUndoer(new SetMaskPosition(objects, document)); - // Copy back the old data - memcpy(fieldAddress, &m_data[0], m_data.size()); + document->getMask()->x = m_x; + document->getMask()->y = m_y; } diff --git a/src/undoers/set_mask_position.h b/src/undoers/set_mask_position.h new file mode 100644 index 000000000..90dc13f8d --- /dev/null +++ b/src/undoers/set_mask_position.h @@ -0,0 +1,47 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 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 UNDOERS_SET_MASK_POSITION_H_INCLUDED +#define UNDOERS_SET_MASK_POSITION_H_INCLUDED + +#include "undo/object_id.h" +#include "undoers/undoer_base.h" + +#include + +class Document; + +namespace undoers { + +class SetMaskPosition : public UndoerBase +{ +public: + SetMaskPosition(undo::ObjectsContainer* objects, Document* document); + + void dispose() OVERRIDE; + int getMemSize() const OVERRIDE { return sizeof(*this); } + void revert(undo::ObjectsContainer* objects, undo::UndoersCollector* redoers) OVERRIDE; + +private: + undo::ObjectId m_documentId; + int m_x, m_y; +}; + +} // namespace undoers + +#endif // UNDOERS_SET_MASK_POSITION_H_INCLUDED diff --git a/src/undoers/set_imgtype.cpp b/src/undoers/set_sprite_imgtype.cpp similarity index 74% rename from src/undoers/set_imgtype.cpp rename to src/undoers/set_sprite_imgtype.cpp index d73efb188..85f059f8c 100644 --- a/src/undoers/set_imgtype.cpp +++ b/src/undoers/set_sprite_imgtype.cpp @@ -18,7 +18,7 @@ #include "config.h" -#include "undoers/set_imgtype.h" +#include "undoers/set_sprite_imgtype.h" #include "raster/sprite.h" #include "undo/objects_container.h" @@ -27,23 +27,23 @@ using namespace undo; using namespace undoers; -SetImgType::SetImgType(ObjectsContainer* objects, Sprite* sprite) +SetSpriteImgType::SetSpriteImgType(ObjectsContainer* objects, Sprite* sprite) : m_spriteId(objects->addObject(sprite)) , m_imgtype(sprite->getImgType()) { } -void SetImgType::dispose() +void SetSpriteImgType::dispose() { delete this; } -void SetImgType::revert(ObjectsContainer* objects, UndoersCollector* redoers) +void SetSpriteImgType::revert(ObjectsContainer* objects, UndoersCollector* redoers) { Sprite* sprite = objects->getObjectT(m_spriteId); - // Push another SetImgType as redoer - redoers->pushUndoer(new SetImgType(objects, sprite)); + // Push another SetSpriteImgType as redoer + redoers->pushUndoer(new SetSpriteImgType(objects, sprite)); sprite->setImgType(m_imgtype); } diff --git a/src/undoers/set_sprite_imgtype.h b/src/undoers/set_sprite_imgtype.h new file mode 100644 index 000000000..2051cdde2 --- /dev/null +++ b/src/undoers/set_sprite_imgtype.h @@ -0,0 +1,45 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 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 UNDOERS_SET_SPRITE_IMGTYPE_H_INCLUDED +#define UNDOERS_SET_SPRITE_IMGTYPE_H_INCLUDED + +#include "undo/object_id.h" +#include "undoers/undoer_base.h" + +class Sprite; + +namespace undoers { + +class SetSpriteImgType : public UndoerBase +{ +public: + SetSpriteImgType(undo::ObjectsContainer* objects, Sprite* sprite); + + void dispose() OVERRIDE; + int getMemSize() const OVERRIDE { return sizeof(*this); } + void revert(undo::ObjectsContainer* objects, undo::UndoersCollector* redoers) OVERRIDE; + +private: + undo::ObjectId m_spriteId; + uint32_t m_imgtype; +}; + +} // namespace undoers + +#endif // UNDOERS_SET_SPRITE_IMGTYPE_H_INCLUDED diff --git a/src/undoers/set_stock_imgtype.cpp b/src/undoers/set_stock_imgtype.cpp new file mode 100644 index 000000000..775b2fa55 --- /dev/null +++ b/src/undoers/set_stock_imgtype.cpp @@ -0,0 +1,49 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 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 + */ + +#include "config.h" + +#include "undoers/set_stock_imgtype.h" + +#include "raster/stock.h" +#include "undo/objects_container.h" +#include "undo/undoers_collector.h" + +using namespace undo; +using namespace undoers; + +SetStockImgType::SetStockImgType(ObjectsContainer* objects, Stock* stock) + : m_stockId(objects->addObject(stock)) + , m_imgtype(stock->getImgType()) +{ +} + +void SetStockImgType::dispose() +{ + delete this; +} + +void SetStockImgType::revert(ObjectsContainer* objects, UndoersCollector* redoers) +{ + Stock* stock = objects->getObjectT(m_stockId); + + // Push another SetStockImgType as redoer + redoers->pushUndoer(new SetStockImgType(objects, stock)); + + stock->setImgType(m_imgtype); +} diff --git a/src/undoers/set_stock_imgtype.h b/src/undoers/set_stock_imgtype.h new file mode 100644 index 000000000..a7452d6fe --- /dev/null +++ b/src/undoers/set_stock_imgtype.h @@ -0,0 +1,45 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 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 UNDOERS_SET_STOCK_IMGTYPE_H_INCLUDED +#define UNDOERS_SET_STOCK_IMGTYPE_H_INCLUDED + +#include "undo/object_id.h" +#include "undoers/undoer_base.h" + +class Stock; + +namespace undoers { + +class SetStockImgType : public UndoerBase +{ +public: + SetStockImgType(undo::ObjectsContainer* objects, Stock* stock); + + void dispose() OVERRIDE; + int getMemSize() const OVERRIDE { return sizeof(*this); } + void revert(undo::ObjectsContainer* objects, undo::UndoersCollector* redoers) OVERRIDE; + +private: + undo::ObjectId m_stockId; + uint32_t m_imgtype; +}; + +} // namespace undoers + +#endif // UNDOERS_SET_STOCK_IMGTYPE_H_INCLUDED diff --git a/src/util/celmove.cpp b/src/util/celmove.cpp index 4bdee27f6..6f04d38d0 100644 --- a/src/util/celmove.cpp +++ b/src/util/celmove.cpp @@ -32,6 +32,9 @@ #include "raster/sprite.h" #include "raster/stock.h" #include "undo/undo_history.h" +#include "undoers/set_cel_frame.h" +#include "undoers/set_cel_opacity.h" +#include "undoers/set_cel_position.h" /* these variables indicate what cel to move (and the sprite's frame indicates where to move it) */ @@ -91,7 +94,7 @@ void move_cel(DocumentWriter& document) if (src_cel != NULL) { if (src_layer == dst_layer) { if (undo->isEnabled()) - undo->undo_int(src_cel, &src_cel->frame); + undo->pushUndoer(new undoers::SetCelFrame(undo->getObjects(), src_cel)); src_cel->frame = dst_frame; } @@ -117,9 +120,8 @@ void move_cel(DocumentWriter& document) if (undo->isEnabled()) { undo->undo_replace_image(sprite->getStock(), src_cel->image); - undo->undo_int(src_cel, &src_cel->x); - undo->undo_int(src_cel, &src_cel->y); - undo->undo_int(src_cel, &src_cel->opacity); + undo->pushUndoer(new undoers::SetCelPosition(undo->getObjects(), src_cel)); + undo->pushUndoer(new undoers::SetCelOpacity(undo->getObjects(), src_cel)); } image_clear(dst_image, app_get_color_to_clear_layer(dst_layer)); diff --git a/src/util/misc.cpp b/src/util/misc.cpp index b01c038f7..c83fc4c0c 100644 --- a/src/util/misc.cpp +++ b/src/util/misc.cpp @@ -33,6 +33,7 @@ #include "modules/palettes.h" #include "raster/raster.h" #include "undo/undo_history.h" +#include "undoers/set_cel_position.h" #include "util/misc.h" #include "widgets/editor/editor.h" #include "widgets/statebar.h" @@ -157,10 +158,8 @@ int interactive_move_layer(int mode, bool use_undo, int (*callback)()) if (use_undo && undo->isEnabled()) { undo->setLabel("Cel Movement"); undo->setModification(undo::ModifyDocument); - undo->undo_open(); - undo->undo_int(cel, &cel->x); - undo->undo_int(cel, &cel->y); - undo->undo_close(); + + undo->pushUndoer(new undoers::SetCelPosition(undo->getObjects(), cel)); } cel_set_position(cel, new_x, new_y); diff --git a/src/widgets/editor/editor.cpp b/src/widgets/editor/editor.cpp index 96a67a1b0..57c92216f 100644 --- a/src/widgets/editor/editor.cpp +++ b/src/widgets/editor/editor.cpp @@ -41,6 +41,7 @@ #include "tools/tool.h" #include "ui_context.h" #include "undo/undo_history.h" +#include "undoers/set_cel_position.h" #include "util/boundary.h" #include "util/misc.h" #include "util/render.h" @@ -1967,16 +1968,16 @@ public: if (undo->isEnabled()) { undo->undo_open(); - if (m_cel->x != m_old_cel_x) { + if (m_cel->x != m_old_cel_x || + m_cel->y != m_old_cel_y) { int x = m_cel->x; - m_cel->x = m_old_cel_x; - undo->undo_int(m_cel, &m_cel->x); - m_cel->x = x; - } - if (m_cel->y != m_old_cel_y) { int y = m_cel->y; m_cel->y = m_old_cel_y; - undo->undo_int(m_cel, &m_cel->y); + m_cel->x = m_old_cel_x; + + undo->pushUndoer(new undoers::SetCelPosition(undo->getObjects(), m_cel)); + + m_cel->x = x; m_cel->y = y; }