| 
									
										
										
										
											2015-08-13 20:59:44 +08:00
										 |  |  | // Aseprite
 | 
					
						
							| 
									
										
										
										
											2017-12-01 10:41:45 +08:00
										 |  |  | // Copyright (C) 2001-2017  David Capello
 | 
					
						
							| 
									
										
										
										
											2015-08-13 20:59:44 +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.
 | 
					
						
							| 
									
										
										
										
											2015-08-13 20:59:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef HAVE_CONFIG_H
 | 
					
						
							|  |  |  | #include "config.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "app/app.h"
 | 
					
						
							|  |  |  | #include "app/cmd/copy_cel.h"
 | 
					
						
							|  |  |  | #include "app/commands/command.h"
 | 
					
						
							|  |  |  | #include "app/context_access.h"
 | 
					
						
							|  |  |  | #include "app/modules/gui.h"
 | 
					
						
							|  |  |  | #include "app/transaction.h"
 | 
					
						
							|  |  |  | #include "app/ui/status_bar.h"
 | 
					
						
							|  |  |  | #include "doc/cel.h"
 | 
					
						
							|  |  |  | #include "doc/layer.h"
 | 
					
						
							|  |  |  | #include "doc/sprite.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace app { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class LinkCelsCommand : public Command { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |   LinkCelsCommand(); | 
					
						
							|  |  |  |   Command* clone() const override { return new LinkCelsCommand(*this); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |   bool onEnabled(Context* context) override; | 
					
						
							|  |  |  |   void onExecute(Context* context) override; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LinkCelsCommand::LinkCelsCommand() | 
					
						
							| 
									
										
										
										
											2017-12-02 02:10:21 +08:00
										 |  |  |   : Command(CommandId::LinkCels(), CmdRecordableFlag) | 
					
						
							| 
									
										
										
										
											2015-08-13 20:59:44 +08:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool LinkCelsCommand::onEnabled(Context* context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (context->checkFlags(ContextFlags::ActiveDocumentIsWritable)) { | 
					
						
							| 
									
										
										
										
											2016-06-15 00:06:30 +08:00
										 |  |  |     auto site = context->activeSite(); | 
					
						
							|  |  |  |     return (site.inTimeline() && | 
					
						
							|  |  |  |             site.selectedFrames().size() > 1); | 
					
						
							| 
									
										
										
										
											2015-08-13 20:59:44 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LinkCelsCommand::onExecute(Context* context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   ContextWriter writer(context); | 
					
						
							|  |  |  |   Document* document(writer.document()); | 
					
						
							|  |  |  |   bool nonEditableLayers = false; | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2016-06-15 00:06:30 +08:00
										 |  |  |     auto site = context->activeSite(); | 
					
						
							|  |  |  |     if (!site.inTimeline()) | 
					
						
							| 
									
										
										
										
											2015-08-13 20:59:44 +08:00
										 |  |  |       return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Transaction transaction(writer.context(), friendlyName()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-15 00:06:30 +08:00
										 |  |  |     for (Layer* layer : site.selectedLayers()) { | 
					
						
							| 
									
										
										
										
											2015-08-13 20:59:44 +08:00
										 |  |  |       if (!layer->isImage()) | 
					
						
							|  |  |  |         continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 02:27:38 +08:00
										 |  |  |       if (!layer->isEditableHierarchy()) { | 
					
						
							| 
									
										
										
										
											2015-08-13 20:59:44 +08:00
										 |  |  |         nonEditableLayers = true; | 
					
						
							|  |  |  |         continue; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       LayerImage* layerImage = static_cast<LayerImage*>(layer); | 
					
						
							| 
									
										
										
										
											2016-06-15 00:06:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       for (auto it=site.selectedFrames().begin(), end=site.selectedFrames().end(); | 
					
						
							|  |  |  |            it != end; ++it) { | 
					
						
							|  |  |  |         frame_t frame = *it; | 
					
						
							| 
									
										
										
										
											2015-08-13 20:59:44 +08:00
										 |  |  |         Cel* cel = layerImage->cel(frame); | 
					
						
							|  |  |  |         if (cel) { | 
					
						
							| 
									
										
										
										
											2016-06-15 00:06:30 +08:00
										 |  |  |           for (++it; it != end; ++it) { | 
					
						
							| 
									
										
										
										
											2015-08-13 20:59:44 +08:00
										 |  |  |             transaction.execute( | 
					
						
							|  |  |  |               new cmd::CopyCel( | 
					
						
							|  |  |  |                 layerImage, cel->frame(), | 
					
						
							| 
									
										
										
										
											2016-06-15 00:06:30 +08:00
										 |  |  |                 layerImage, *it, | 
					
						
							| 
									
										
										
										
											2015-08-13 20:59:44 +08:00
										 |  |  |                 true));         // true = force links
 | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     transaction.commit(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (nonEditableLayers) | 
					
						
							|  |  |  |     StatusBar::instance()->showTip(1000, | 
					
						
							|  |  |  |       "There are locked layers"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   update_screen_for_document(document); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Command* CommandFactory::createLinkCelsCommand() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return new LinkCelsCommand; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace app
 |