| 
									
										
										
										
											2013-08-09 08:01:20 +08:00
										 |  |  | /* Aseprite
 | 
					
						
							| 
									
										
										
										
											2013-01-27 23:13:13 +08:00
										 |  |  |  * Copyright (C) 2001-2013  David Capello | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation; either version 2 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software | 
					
						
							|  |  |  |  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | #ifdef HAVE_CONFIG_H
 | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | #include "config.h"
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | #include "app/app.h"
 | 
					
						
							|  |  |  | #include "app/commands/command.h"
 | 
					
						
							|  |  |  | #include "app/context_access.h"
 | 
					
						
							| 
									
										
										
										
											2012-06-16 10:37:59 +08:00
										 |  |  | #include "app/find_widget.h"
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | #include "app/ini_file.h"
 | 
					
						
							| 
									
										
										
										
											2012-06-16 10:37:59 +08:00
										 |  |  | #include "app/load_widget.h"
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | #include "app/modules/editors.h"
 | 
					
						
							|  |  |  | #include "app/ui_context.h"
 | 
					
						
							| 
									
										
										
										
											2013-10-15 06:58:11 +08:00
										 |  |  | #include "base/path.h"
 | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | #include "raster/sprite.h"
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | #include "ui/ui.h"
 | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-10 04:36:45 +08:00
										 |  |  | #include <cstdio>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | namespace app { | 
					
						
							| 
									
										
										
										
											2012-06-18 09:02:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | using namespace ui; | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | class DuplicateSpriteCommand : public Command { | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | public: | 
					
						
							|  |  |  |   DuplicateSpriteCommand(); | 
					
						
							|  |  |  |   Command* clone() { return new DuplicateSpriteCommand(*this); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |   bool onEnabled(Context* context); | 
					
						
							|  |  |  |   void onExecute(Context* context); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DuplicateSpriteCommand::DuplicateSpriteCommand() | 
					
						
							|  |  |  |   : Command("DuplicateSprite", | 
					
						
							|  |  |  |             "Duplicate Sprite", | 
					
						
							|  |  |  |             CmdUIOnlyFlag) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool DuplicateSpriteCommand::onEnabled(Context* context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return context->checkFlags(ContextFlags::ActiveDocumentIsReadable); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DuplicateSpriteCommand::onExecute(Context* context) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-06-16 11:05:01 +08:00
										 |  |  |   Widget* src_name, *dst_name, *flatten; | 
					
						
							| 
									
										
										
										
											2013-03-12 07:29:45 +08:00
										 |  |  |   const ContextReader reader(context); | 
					
						
							|  |  |  |   const Document* document = reader.document(); | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* load the window widget */ | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  |   base::UniquePtr<Window> window(app::load_widget<Window>("duplicate_sprite.xml", "duplicate_sprite")); | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   src_name = window->findChild("src_name"); | 
					
						
							|  |  |  |   dst_name = window->findChild("dst_name"); | 
					
						
							|  |  |  |   flatten = window->findChild("flatten"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-15 06:58:11 +08:00
										 |  |  |   base::string fn = document->getFilename(); | 
					
						
							|  |  |  |   src_name->setText(base::get_file_name(fn)); | 
					
						
							|  |  |  |   dst_name->setText(base::get_file_title(fn) + " Copy." + base::get_file_extension(fn)); | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (get_config_bool("DuplicateSprite", "Flatten", false)) | 
					
						
							|  |  |  |     flatten->setSelected(true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-12 07:29:45 +08:00
										 |  |  |   // Open the window
 | 
					
						
							| 
									
										
										
										
											2012-07-09 10:24:42 +08:00
										 |  |  |   window->openWindowInForeground(); | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-11 23:43:25 +08:00
										 |  |  |   if (window->getKiller() == window->findChild("ok")) { | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  |     set_config_bool("DuplicateSprite", "Flatten", flatten->isSelected()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-12 07:29:45 +08:00
										 |  |  |     // Make a copy of the document
 | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  |     Document* docCopy; | 
					
						
							|  |  |  |     if (flatten->isSelected()) | 
					
						
							|  |  |  |       docCopy = document->duplicate(DuplicateWithFlattenLayers); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       docCopy = document->duplicate(DuplicateExactCopy); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-15 06:58:11 +08:00
										 |  |  |     docCopy->setFilename(dst_name->getText().c_str()); | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     context->addDocument(docCopy); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Command* CommandFactory::createDuplicateSpriteCommand() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return new DuplicateSpriteCommand; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | } // namespace app
 |