| 1 | // Aseprite |
|---|---|
| 2 | // Copyright (C) 2001-2016 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/unlink_cel.h" |
| 12 | |
| 13 | #include "doc/cel.h" |
| 14 | #include "doc/image.h" |
| 15 | #include "doc/layer.h" |
| 16 | #include "doc/sprite.h" |
| 17 | |
| 18 | namespace app { |
| 19 | namespace cmd { |
| 20 | |
| 21 | using namespace doc; |
| 22 | |
| 23 | UnlinkCel::UnlinkCel(Cel* cel) |
| 24 | : WithCel(cel) |
| 25 | , m_newImageId(0) |
| 26 | , m_oldCelDataId(cel->dataRef()->id()) |
| 27 | , m_newCelDataId(0) |
| 28 | { |
| 29 | ASSERT(cel->links()); |
| 30 | } |
| 31 | |
| 32 | void UnlinkCel::onExecute() |
| 33 | { |
| 34 | Cel* cel = this->cel(); |
| 35 | CelDataRef oldCelData = cel->sprite()->getCelDataRef(m_oldCelDataId); |
| 36 | ASSERT(oldCelData); |
| 37 | |
| 38 | ImageRef imgCopy(Image::createCopy(oldCelData->image())); |
| 39 | CelDataRef celDataCopy(new CelData(*oldCelData)); |
| 40 | celDataCopy->setImage(imgCopy, cel->layer()); |
| 41 | celDataCopy->setUserData(oldCelData->userData()); |
| 42 | |
| 43 | if (m_newImageId) { |
| 44 | imgCopy->setId(m_newImageId); |
| 45 | celDataCopy->setId(m_newCelDataId); |
| 46 | } |
| 47 | else { |
| 48 | m_newImageId = imgCopy->id(); |
| 49 | m_newCelDataId = celDataCopy->id(); |
| 50 | } |
| 51 | |
| 52 | cel->setDataRef(celDataCopy); |
| 53 | cel->incrementVersion(); |
| 54 | } |
| 55 | |
| 56 | void UnlinkCel::onUndo() |
| 57 | { |
| 58 | Cel* cel = this->cel(); |
| 59 | CelDataRef oldCelData = cel->sprite()->getCelDataRef(m_oldCelDataId); |
| 60 | ASSERT(oldCelData); |
| 61 | |
| 62 | cel->setDataRef(oldCelData); |
| 63 | cel->incrementVersion(); |
| 64 | } |
| 65 | |
| 66 | } // namespace cmd |
| 67 | } // namespace app |
| 68 |