1 | // Aseprite |
---|---|
2 | // Copyright (C) 2019 Igara Studio S.A. |
3 | // Copyright (C) 2001-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/commands/command.h" |
13 | #include "app/context_access.h" |
14 | #include "app/doc_api.h" |
15 | #include "app/i18n/strings.h" |
16 | #include "app/modules/gui.h" |
17 | #include "app/tx.h" |
18 | #include "app/ui/color_bar.h" |
19 | #include "app/util/autocrop.h" |
20 | #include "doc/image.h" |
21 | #include "doc/layer.h" |
22 | #include "doc/mask.h" |
23 | #include "doc/sprite.h" |
24 | |
25 | namespace app { |
26 | |
27 | class CropSpriteCommand : public Command { |
28 | public: |
29 | CropSpriteCommand(); |
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 | gfx::Rect m_bounds; |
38 | }; |
39 | |
40 | CropSpriteCommand::CropSpriteCommand() |
41 | : Command(CommandId::CropSprite(), CmdRecordableFlag) |
42 | { |
43 | } |
44 | |
45 | void CropSpriteCommand::onLoadParams(const Params& params) |
46 | { |
47 | m_bounds = gfx::Rect(0, 0, 0, 0); |
48 | if (params.has_param("x")) m_bounds.x = params.get_as<int>( "x"); |
49 | if (params.has_param("y")) m_bounds.y = params.get_as<int>( "y"); |
50 | if (params.has_param("width")) m_bounds.w = params.get_as<int>( "width"); |
51 | if (params.has_param("height")) m_bounds.h = params.get_as<int>( "height"); |
52 | } |
53 | |
54 | bool CropSpriteCommand::onEnabled(Context* context) |
55 | { |
56 | return |
57 | context->checkFlags( |
58 | ContextFlags::ActiveDocumentIsWritable | |
59 | (m_bounds.isEmpty() ? ContextFlags::HasVisibleMask: 0)); |
60 | } |
61 | |
62 | void CropSpriteCommand::onExecute(Context* context) |
63 | { |
64 | ContextWriter writer(context); |
65 | Doc* document(writer.document()); |
66 | Sprite* sprite(writer.sprite()); |
67 | |
68 | gfx::Rect bounds; |
69 | if (m_bounds.isEmpty()) |
70 | bounds = document->mask()->bounds(); |
71 | else |
72 | bounds = m_bounds; |
73 | |
74 | { |
75 | Tx tx(writer.context(), "Sprite Crop"); |
76 | document->getApi(tx).cropSprite(sprite, bounds); |
77 | tx.commit(); |
78 | } |
79 | |
80 | #ifdef ENABLE_UI |
81 | if (context->isUIAvailable()) |
82 | update_screen_for_document(document); |
83 | #endif |
84 | } |
85 | |
86 | class AutocropSpriteCommand : public Command { |
87 | public: |
88 | AutocropSpriteCommand(); |
89 | |
90 | protected: |
91 | void onLoadParams(const Params& params) override; |
92 | bool onEnabled(Context* context) override; |
93 | void onExecute(Context* context) override; |
94 | std::string onGetFriendlyName() const override; |
95 | |
96 | private: |
97 | bool m_byGrid = false; |
98 | }; |
99 | |
100 | AutocropSpriteCommand::AutocropSpriteCommand() |
101 | : Command(CommandId::AutocropSprite(), CmdRecordableFlag) |
102 | { |
103 | } |
104 | |
105 | void AutocropSpriteCommand::onLoadParams(const app::Params& params) |
106 | { |
107 | m_byGrid = params.get_as<bool>("byGrid"); |
108 | } |
109 | |
110 | bool AutocropSpriteCommand::onEnabled(Context* context) |
111 | { |
112 | return context->checkFlags(ContextFlags::ActiveDocumentIsWritable | |
113 | ContextFlags::HasActiveSprite); |
114 | } |
115 | |
116 | void AutocropSpriteCommand::onExecute(Context* context) |
117 | { |
118 | ContextWriter writer(context); |
119 | Doc* document(writer.document()); |
120 | Sprite* sprite(writer.sprite()); |
121 | { |
122 | Tx tx(writer.context(), onGetFriendlyName()); |
123 | document->getApi(tx).trimSprite(sprite, m_byGrid); |
124 | tx.commit(); |
125 | } |
126 | |
127 | #ifdef ENABLE_UI |
128 | if (context->isUIAvailable()) |
129 | update_screen_for_document(document); |
130 | #endif |
131 | } |
132 | |
133 | std::string AutocropSpriteCommand::onGetFriendlyName() const |
134 | { |
135 | if (m_byGrid) |
136 | return Strings::commands_AutocropSprite_ByGrid(); |
137 | else |
138 | return Strings::commands_AutocropSprite(); |
139 | } |
140 | |
141 | Command* CommandFactory::createCropSpriteCommand() |
142 | { |
143 | return new CropSpriteCommand; |
144 | } |
145 | |
146 | Command* CommandFactory::createAutocropSpriteCommand() |
147 | { |
148 | return new AutocropSpriteCommand; |
149 | } |
150 | |
151 | } // namespace app |
152 |