| 1 | // Aseprite | 
|---|
| 2 | // Copyright (C) 2019-2020  Igara Studio S.A. | 
|---|
| 3 | // Copyright (C) 2017-2018  David Capello | 
|---|
| 4 | // | 
|---|
| 5 | // This program is distributed under the terms of | 
|---|
| 6 | // the End-User License Agreement for Aseprite. | 
|---|
| 7 |  | 
|---|
| 8 | #ifdef HAVE_CONFIG_H | 
|---|
| 9 | #include "config.h" | 
|---|
| 10 | #endif | 
|---|
| 11 |  | 
|---|
| 12 | #include "app/cmd/set_slice_key.h" | 
|---|
| 13 | #include "app/cmd/set_slice_name.h" | 
|---|
| 14 | #include "app/cmd/set_user_data.h" | 
|---|
| 15 | #include "app/commands/command.h" | 
|---|
| 16 | #include "app/context_access.h" | 
|---|
| 17 | #include "app/tx.h" | 
|---|
| 18 | #include "app/ui/slice_window.h" | 
|---|
| 19 | #include "base/convert_to.h" | 
|---|
| 20 | #include "doc/slice.h" | 
|---|
| 21 | #include "doc/sprite.h" | 
|---|
| 22 |  | 
|---|
| 23 | namespace app { | 
|---|
| 24 |  | 
|---|
| 25 | using namespace ui; | 
|---|
| 26 |  | 
|---|
| 27 | class SlicePropertiesCommand : public Command { | 
|---|
| 28 | public: | 
|---|
| 29 | SlicePropertiesCommand(); | 
|---|
| 30 |  | 
|---|
| 31 | protected: | 
|---|
| 32 | void onLoadParams(const Params& params) override; | 
|---|
| 33 | bool onEnabled(Context* context) override; | 
|---|
| 34 | void onExecute(Context* context) override; | 
|---|
| 35 |  | 
|---|
| 36 | private: | 
|---|
| 37 | std::string m_sliceName; | 
|---|
| 38 | ObjectId m_sliceId; | 
|---|
| 39 | }; | 
|---|
| 40 |  | 
|---|
| 41 | SlicePropertiesCommand::SlicePropertiesCommand() | 
|---|
| 42 | : Command(CommandId::SliceProperties(), CmdUIOnlyFlag) | 
|---|
| 43 | , m_sliceId(NullId) | 
|---|
| 44 | { | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | void SlicePropertiesCommand::onLoadParams(const Params& params) | 
|---|
| 48 | { | 
|---|
| 49 | m_sliceName = params.get( "name"); | 
|---|
| 50 |  | 
|---|
| 51 | std::string id = params.get( "id"); | 
|---|
| 52 | if (!id.empty()) | 
|---|
| 53 | m_sliceId = ObjectId(base::convert_to<doc::ObjectId>(id)); | 
|---|
| 54 | else | 
|---|
| 55 | m_sliceId = NullId; | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | bool SlicePropertiesCommand::onEnabled(Context* context) | 
|---|
| 59 | { | 
|---|
| 60 | return context->checkFlags(ContextFlags::ActiveDocumentIsWritable); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | void SlicePropertiesCommand::onExecute(Context* context) | 
|---|
| 64 | { | 
|---|
| 65 | const ContextReader reader(context); | 
|---|
| 66 | const Sprite* sprite = reader.sprite(); | 
|---|
| 67 | frame_t frame = reader.frame(); | 
|---|
| 68 | SelectedObjects slices; | 
|---|
| 69 |  | 
|---|
| 70 | { | 
|---|
| 71 | Slice* slice = nullptr; | 
|---|
| 72 | if (!m_sliceName.empty()) | 
|---|
| 73 | slice = sprite->slices().getByName(m_sliceName); | 
|---|
| 74 | else if (m_sliceId != NullId) | 
|---|
| 75 | slice = sprite->slices().getById(m_sliceId); | 
|---|
| 76 |  | 
|---|
| 77 | if (slice) | 
|---|
| 78 | slices.insert(slice->id()); | 
|---|
| 79 | else | 
|---|
| 80 | slices = reader.site()->selectedSlices(); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | // Nothing to delete | 
|---|
| 84 | if (slices.empty()) | 
|---|
| 85 | return; | 
|---|
| 86 |  | 
|---|
| 87 | SliceWindow window(sprite, slices, frame); | 
|---|
| 88 | if (!window.show()) | 
|---|
| 89 | return; | 
|---|
| 90 |  | 
|---|
| 91 | { | 
|---|
| 92 | const SliceWindow::Mods mods = window.modifiedFields(); | 
|---|
| 93 | ContextWriter writer(reader); | 
|---|
| 94 | Tx tx(writer.context(), "Slice Properties"); | 
|---|
| 95 |  | 
|---|
| 96 | for (Slice* slice : slices.iterateAs<Slice>()) { | 
|---|
| 97 | // Change name | 
|---|
| 98 | if (mods & SliceWindow::kName) { | 
|---|
| 99 | std::string name = window.nameValue(); | 
|---|
| 100 | if (slice->name() != name) | 
|---|
| 101 | tx(new cmd::SetSliceName(slice, name)); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | // Change user data | 
|---|
| 105 | if ((mods & SliceWindow::kUserData) && | 
|---|
| 106 | slice->userData() != window.userDataValue()) { | 
|---|
| 107 | tx(new cmd::SetUserData(slice, window.userDataValue(), static_cast<Doc*>(sprite->document()))); | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | // Change slice properties | 
|---|
| 111 | const doc::SliceKey* key = slice->getByFrame(frame); | 
|---|
| 112 | if (!key) | 
|---|
| 113 | continue; | 
|---|
| 114 |  | 
|---|
| 115 | SliceKey newKey = *key; | 
|---|
| 116 | gfx::Rect newBounds = newKey.bounds(); | 
|---|
| 117 | gfx::Rect newCenter = newKey.center(); | 
|---|
| 118 | gfx::Point newPivot = newKey.pivot(); | 
|---|
| 119 | if (mods & SliceWindow::kBoundsX) newBounds.x = window.boundsValue().x; | 
|---|
| 120 | if (mods & SliceWindow::kBoundsY) newBounds.y = window.boundsValue().y; | 
|---|
| 121 | if (mods & SliceWindow::kBoundsW) newBounds.w = window.boundsValue().w; | 
|---|
| 122 | if (mods & SliceWindow::kBoundsH) newBounds.h = window.boundsValue().h; | 
|---|
| 123 | if (mods & SliceWindow::kCenterX) newCenter.x = window.centerValue().x; | 
|---|
| 124 | if (mods & SliceWindow::kCenterY) newCenter.y = window.centerValue().y; | 
|---|
| 125 | if (mods & SliceWindow::kCenterW) newCenter.w = window.centerValue().w; | 
|---|
| 126 | if (mods & SliceWindow::kCenterH) newCenter.h = window.centerValue().h; | 
|---|
| 127 | if (mods & SliceWindow::kPivotX) newPivot.x = window.pivotValue().x; | 
|---|
| 128 | if (mods & SliceWindow::kPivotY) newPivot.y = window.pivotValue().y; | 
|---|
| 129 | newKey.setBounds(newBounds); | 
|---|
| 130 | newKey.setCenter(newCenter); | 
|---|
| 131 | newKey.setPivot(newPivot); | 
|---|
| 132 |  | 
|---|
| 133 | if (key->bounds() != newKey.bounds() || | 
|---|
| 134 | key->center() != newKey.center() || | 
|---|
| 135 | key->pivot() != newKey.pivot()) { | 
|---|
| 136 | tx(new cmd::SetSliceKey(slice, frame, newKey)); | 
|---|
| 137 | } | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | tx.commit(); | 
|---|
| 141 | writer.document()->notifyGeneralUpdate(); | 
|---|
| 142 | } | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | Command* CommandFactory::createSlicePropertiesCommand() | 
|---|
| 146 | { | 
|---|
| 147 | return new SlicePropertiesCommand; | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | } // namespace app | 
|---|
| 151 |  | 
|---|