| 1 | // Aseprite |
|---|---|
| 2 | // Copyright (C) 2001-2015 David Capello |
| 3 | // |
| 4 | // This program is distributed under the terms of |
| 5 | // the End-User License Agreement for Aseprite. |
| 6 | |
| 7 | #ifdef HAVE_CONFIG_H |
| 8 | #include "config.h" |
| 9 | #endif |
| 10 | |
| 11 | #include "app/cmd/set_cel_data.h" |
| 12 | |
| 13 | #include "doc/cel.h" |
| 14 | #include "doc/image.h" |
| 15 | #include "doc/image_io.h" |
| 16 | #include "doc/image_ref.h" |
| 17 | #include "doc/layer.h" |
| 18 | #include "doc/sprite.h" |
| 19 | #include "doc/subobjects_io.h" |
| 20 | |
| 21 | namespace app { |
| 22 | namespace cmd { |
| 23 | |
| 24 | using namespace doc; |
| 25 | |
| 26 | SetCelData::SetCelData(Cel* cel, const CelDataRef& newData) |
| 27 | : WithCel(cel) |
| 28 | , m_oldDataId(cel->data()->id()) |
| 29 | , m_oldImageId(cel->image()->id()) |
| 30 | , m_newDataId(newData->id()) |
| 31 | , m_newData(newData) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | void SetCelData::onExecute() |
| 36 | { |
| 37 | Cel* cel = this->cel(); |
| 38 | if (!cel->links()) |
| 39 | createCopy(); |
| 40 | |
| 41 | cel->setDataRef(m_newData); |
| 42 | cel->incrementVersion(); |
| 43 | m_newData.reset(); |
| 44 | } |
| 45 | |
| 46 | void SetCelData::onUndo() |
| 47 | { |
| 48 | Cel* cel = this->cel(); |
| 49 | |
| 50 | if (m_dataCopy) { |
| 51 | ASSERT(!cel->sprite()->getCelDataRef(m_oldDataId)); |
| 52 | m_dataCopy->setId(m_oldDataId); |
| 53 | m_dataCopy->image()->setId(m_oldImageId); |
| 54 | |
| 55 | cel->setDataRef(m_dataCopy); |
| 56 | m_dataCopy.reset(); |
| 57 | } |
| 58 | else { |
| 59 | CelDataRef oldData = cel->sprite()->getCelDataRef(m_oldDataId); |
| 60 | ASSERT(oldData); |
| 61 | cel->setDataRef(oldData); |
| 62 | } |
| 63 | |
| 64 | cel->incrementVersion(); |
| 65 | } |
| 66 | |
| 67 | void SetCelData::onRedo() |
| 68 | { |
| 69 | Cel* cel = this->cel(); |
| 70 | if (!cel->links()) |
| 71 | createCopy(); |
| 72 | |
| 73 | CelDataRef newData = cel->sprite()->getCelDataRef(m_newDataId); |
| 74 | ASSERT(newData); |
| 75 | cel->setDataRef(newData); |
| 76 | cel->incrementVersion(); |
| 77 | } |
| 78 | |
| 79 | void SetCelData::createCopy() |
| 80 | { |
| 81 | Cel* cel = this->cel(); |
| 82 | |
| 83 | ASSERT(!m_dataCopy); |
| 84 | m_dataCopy.reset(new CelData(*cel->data())); |
| 85 | m_dataCopy->setImage( |
| 86 | ImageRef(Image::createCopy(cel->image())), |
| 87 | cel->layer()); |
| 88 | } |
| 89 | |
| 90 | } // namespace cmd |
| 91 | } // namespace app |
| 92 |