| 
									
										
										
										
											2016-03-19 22:33:05 +08:00
										 |  |  | // Aseprite
 | 
					
						
							| 
									
										
										
										
											2017-12-01 01:51:13 +08:00
										 |  |  | // Copyright (C) 2015-2017  David Capello
 | 
					
						
							| 
									
										
										
										
											2016-03-19 22:33:05 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2016-08-27 04:02:58 +08:00
										 |  |  | // This program is distributed under the terms of
 | 
					
						
							|  |  |  | // the End-User License Agreement for Aseprite.
 | 
					
						
							| 
									
										
										
										
											2016-03-19 22:33:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef HAVE_CONFIG_H
 | 
					
						
							|  |  |  | #include "config.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "app/cmd/set_mask.h"
 | 
					
						
							|  |  |  | #include "app/commands/command.h"
 | 
					
						
							|  |  |  | #include "app/context_access.h"
 | 
					
						
							|  |  |  | #include "app/document.h"
 | 
					
						
							| 
									
										
										
										
											2017-12-01 10:41:45 +08:00
										 |  |  | #include "app/i18n/strings.h"
 | 
					
						
							| 
									
										
										
										
											2016-03-19 22:33:05 +08:00
										 |  |  | #include "app/modules/editors.h"
 | 
					
						
							|  |  |  | #include "app/modules/gui.h"
 | 
					
						
							|  |  |  | #include "app/pref/preferences.h"
 | 
					
						
							|  |  |  | #include "app/snap_to_grid.h"
 | 
					
						
							|  |  |  | #include "app/transaction.h"
 | 
					
						
							|  |  |  | #include "app/ui/editor/editor.h"
 | 
					
						
							|  |  |  | #include "doc/mask.h"
 | 
					
						
							| 
									
										
										
										
											2017-12-01 10:41:45 +08:00
										 |  |  | #include "fmt/format.h"
 | 
					
						
							| 
									
										
										
										
											2016-03-19 22:33:05 +08:00
										 |  |  | #include "ui/system.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace app { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace doc; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SelectTileCommand : public Command { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |   SelectTileCommand(); | 
					
						
							|  |  |  |   Command* clone() const override { return new SelectTileCommand(*this); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2016-03-19 23:09:03 +08:00
										 |  |  |   void onLoadParams(const Params& params) override; | 
					
						
							|  |  |  |   bool onEnabled(Context* ctx) override; | 
					
						
							|  |  |  |   void onExecute(Context* ctx) override; | 
					
						
							|  |  |  |   std::string onGetFriendlyName() const override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2016-04-05 05:46:48 +08:00
										 |  |  |   gen::SelectionMode m_mode; | 
					
						
							| 
									
										
										
										
											2016-03-19 22:33:05 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SelectTileCommand::SelectTileCommand() | 
					
						
							| 
									
										
										
										
											2017-12-01 10:41:45 +08:00
										 |  |  |   : Command("SelectTile", CmdRecordableFlag) | 
					
						
							| 
									
										
										
										
											2016-04-05 05:46:48 +08:00
										 |  |  |   , m_mode(gen::SelectionMode::DEFAULT) | 
					
						
							| 
									
										
										
										
											2016-03-19 23:09:03 +08:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SelectTileCommand::onLoadParams(const Params& params) | 
					
						
							| 
									
										
										
										
											2016-03-19 22:33:05 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-19 23:09:03 +08:00
										 |  |  |   std::string mode = params.get("mode"); | 
					
						
							|  |  |  |   if (mode == "add") | 
					
						
							| 
									
										
										
										
											2016-04-05 05:46:48 +08:00
										 |  |  |     m_mode = gen::SelectionMode::ADD; | 
					
						
							| 
									
										
										
										
											2016-03-19 23:09:03 +08:00
										 |  |  |   else if (mode == "subtract") | 
					
						
							| 
									
										
										
										
											2016-04-05 05:46:48 +08:00
										 |  |  |     m_mode = gen::SelectionMode::SUBTRACT; | 
					
						
							| 
									
										
										
										
											2016-03-19 23:09:03 +08:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2016-04-05 05:46:48 +08:00
										 |  |  |     m_mode = gen::SelectionMode::DEFAULT; | 
					
						
							| 
									
										
										
										
											2016-03-19 22:33:05 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 23:09:03 +08:00
										 |  |  | bool SelectTileCommand::onEnabled(Context* ctx) | 
					
						
							| 
									
										
										
										
											2016-03-19 22:33:05 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-19 23:09:03 +08:00
										 |  |  |   return ctx->checkFlags(ContextFlags::ActiveDocumentIsWritable); | 
					
						
							| 
									
										
										
										
											2016-03-19 22:33:05 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SelectTileCommand::onExecute(Context* ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (!current_editor || | 
					
						
							|  |  |  |       !current_editor->hasMouse()) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Lock sprite
 | 
					
						
							|  |  |  |   ContextWriter writer(ctx); | 
					
						
							|  |  |  |   Document* doc(writer.document()); | 
					
						
							|  |  |  |   auto& docPref = Preferences::instance().document(doc); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   base::UniquePtr<Mask> mask(new Mask()); | 
					
						
							| 
									
										
										
										
											2016-03-19 23:09:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-05 05:46:48 +08:00
										 |  |  |   if (m_mode != gen::SelectionMode::DEFAULT) | 
					
						
							| 
									
										
										
										
											2016-03-19 23:09:03 +08:00
										 |  |  |     mask->copyFrom(doc->mask()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 22:33:05 +08:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2016-03-19 23:09:03 +08:00
										 |  |  |     gfx::Rect gridBounds = docPref.grid.bounds(); | 
					
						
							| 
									
										
										
										
											2016-03-19 22:33:05 +08:00
										 |  |  |     gfx::Point pos = current_editor->screenToEditor(ui::get_mouse_position()); | 
					
						
							|  |  |  |     pos = snap_to_grid(gridBounds, pos, PreferSnapTo::BoxOrigin); | 
					
						
							| 
									
										
										
										
											2016-03-19 23:09:03 +08:00
										 |  |  |     gridBounds.setOrigin(pos); | 
					
						
							| 
									
										
										
										
											2016-03-19 22:33:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-05 05:46:48 +08:00
										 |  |  |     if (m_mode != gen::SelectionMode::SUBTRACT) | 
					
						
							| 
									
										
										
										
											2016-03-19 23:09:03 +08:00
										 |  |  |       mask->add(gridBounds); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       mask->subtract(gridBounds); | 
					
						
							| 
									
										
										
										
											2016-03-19 22:33:05 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Set the new mask
 | 
					
						
							|  |  |  |   Transaction transaction(writer.context(), | 
					
						
							| 
									
										
										
										
											2017-12-01 10:41:45 +08:00
										 |  |  |                           friendlyName(), | 
					
						
							| 
									
										
										
										
											2016-03-19 22:33:05 +08:00
										 |  |  |                           DoesntModifyDocument); | 
					
						
							|  |  |  |   transaction.execute(new cmd::SetMask(doc, mask)); | 
					
						
							|  |  |  |   transaction.commit(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   doc->generateMaskBoundaries(); | 
					
						
							|  |  |  |   update_screen_for_document(doc); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 23:09:03 +08:00
										 |  |  | std::string SelectTileCommand::onGetFriendlyName() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-12-01 10:41:45 +08:00
										 |  |  |   std::string text; | 
					
						
							| 
									
										
										
										
											2016-03-19 23:09:03 +08:00
										 |  |  |   switch (m_mode) { | 
					
						
							| 
									
										
										
										
											2017-12-01 10:41:45 +08:00
										 |  |  |     case gen::SelectionMode::ADD: | 
					
						
							|  |  |  |       text = Strings::commands_SelectTile_Add(); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     case gen::SelectionMode::SUBTRACT: | 
					
						
							|  |  |  |       text = Strings::commands_SelectTile_Subtract(); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |       text = getBaseFriendlyName();; | 
					
						
							|  |  |  |       break; | 
					
						
							| 
									
										
										
										
											2016-03-19 23:09:03 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   return text; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 22:33:05 +08:00
										 |  |  | Command* CommandFactory::createSelectTileCommand() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return new SelectTileCommand; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace app
 |