| 
									
										
										
										
											2009-06-01 00:02:32 +08:00
										 |  |  | /* ASE - Allegro Sprite Editor
 | 
					
						
							| 
									
										
										
										
											2010-02-02 05:25:40 +08:00
										 |  |  |  * Copyright (C) 2001-2010  David Capello | 
					
						
							| 
									
										
										
										
											2009-06-01 00:02:32 +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 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "config.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <allegro/unicode.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "jinete/jinete.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-09 09:34:06 +08:00
										 |  |  | #include "commands/command.h"
 | 
					
						
							| 
									
										
										
										
											2009-06-01 00:02:32 +08:00
										 |  |  | #include "modules/gui.h"
 | 
					
						
							|  |  |  | #include "raster/image.h"
 | 
					
						
							|  |  |  | #include "raster/mask.h"
 | 
					
						
							|  |  |  | #include "raster/sprite.h"
 | 
					
						
							| 
									
										
										
										
											2009-10-09 09:34:06 +08:00
										 |  |  | #include "sprite_wrappers.h"
 | 
					
						
							| 
									
										
										
										
											2009-06-11 23:11:11 +08:00
										 |  |  | #include "undoable.h"
 | 
					
						
							| 
									
										
										
										
											2010-08-24 05:11:47 +08:00
										 |  |  | #include "widgets/color_bar.h"
 | 
					
						
							| 
									
										
										
										
											2010-08-25 11:26:37 +08:00
										 |  |  | #include "app/color_utils.h"
 | 
					
						
							| 
									
										
										
										
											2009-06-01 00:02:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-09 09:34:06 +08:00
										 |  |  | class CanvasSizeCommand : public Command | 
					
						
							| 
									
										
										
										
											2009-06-01 00:02:32 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-09 09:34:06 +08:00
										 |  |  |   int m_left, m_right, m_top, m_bottom; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |   CanvasSizeCommand(); | 
					
						
							|  |  |  |   Command* clone() const { return new CanvasSizeCommand(*this); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2010-07-31 23:23:45 +08:00
										 |  |  |   bool onEnabled(Context* context); | 
					
						
							|  |  |  |   void onExecute(Context* context); | 
					
						
							| 
									
										
										
										
											2009-10-09 09:34:06 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CanvasSizeCommand::CanvasSizeCommand() | 
					
						
							|  |  |  |   : Command("canvas_size", | 
					
						
							|  |  |  | 	    "Canvas Size", | 
					
						
							|  |  |  | 	    CmdRecordableFlag) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   m_left = m_right = m_top = m_bottom = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-31 23:23:45 +08:00
										 |  |  | bool CanvasSizeCommand::onEnabled(Context* context) | 
					
						
							| 
									
										
										
										
											2009-10-09 09:34:06 +08:00
										 |  |  | { | 
					
						
							|  |  |  |   const CurrentSpriteReader sprite(context); | 
					
						
							| 
									
										
										
										
											2009-06-01 10:59:15 +08:00
										 |  |  |   return sprite != NULL; | 
					
						
							| 
									
										
										
										
											2009-06-01 00:02:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-31 23:23:45 +08:00
										 |  |  | void CanvasSizeCommand::onExecute(Context* context) | 
					
						
							| 
									
										
										
										
											2009-06-01 00:02:32 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-09 09:34:06 +08:00
										 |  |  |   CurrentSpriteWriter sprite(context); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (context->is_ui_available()) { | 
					
						
							|  |  |  |     JWidget left, top, right, bottom, ok; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // load the window widget
 | 
					
						
							| 
									
										
										
										
											2010-04-07 09:14:33 +08:00
										 |  |  |     FramePtr window(load_widget("canvas_size.xml", "canvas_size")); | 
					
						
							| 
									
										
										
										
											2009-10-09 09:34:06 +08:00
										 |  |  |     get_widgets(window, | 
					
						
							|  |  |  | 		"left", &left, | 
					
						
							|  |  |  | 		"top", &top, | 
					
						
							|  |  |  | 		"right", &right, | 
					
						
							|  |  |  | 		"bottom", &bottom, | 
					
						
							|  |  |  | 		"ok", &ok, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-26 08:38:05 +08:00
										 |  |  |     window->remap_window(); | 
					
						
							|  |  |  |     window->center_window(); | 
					
						
							| 
									
										
										
										
											2009-10-09 09:34:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-26 08:38:05 +08:00
										 |  |  |     left->setTextf("%d", m_left); | 
					
						
							|  |  |  |     right->setTextf("%d", m_right); | 
					
						
							|  |  |  |     top->setTextf("%d", m_top); | 
					
						
							|  |  |  |     bottom->setTextf("%d", m_bottom); | 
					
						
							| 
									
										
										
										
											2009-10-09 09:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     load_window_pos(window, "CanvasSize"); | 
					
						
							| 
									
										
										
										
											2010-07-04 23:03:14 +08:00
										 |  |  |     window->setVisible(true); | 
					
						
							| 
									
										
										
										
											2010-01-26 08:38:05 +08:00
										 |  |  |     window->open_window_fg(); | 
					
						
							| 
									
										
										
										
											2009-10-09 09:34:06 +08:00
										 |  |  |     save_window_pos(window, "CanvasSize"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-26 08:38:05 +08:00
										 |  |  |     if (window->get_killer() != ok) | 
					
						
							| 
									
										
										
										
											2009-10-09 09:34:06 +08:00
										 |  |  |       return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-26 08:38:05 +08:00
										 |  |  |     m_left = left->getTextInt(); | 
					
						
							|  |  |  |     m_right = right->getTextInt(); | 
					
						
							|  |  |  |     m_top = top->getTextInt(); | 
					
						
							|  |  |  |     m_bottom = bottom->getTextInt(); | 
					
						
							| 
									
										
										
										
											2009-10-09 09:34:06 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // resize canvas
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   int x1 = -m_left; | 
					
						
							|  |  |  |   int y1 = -m_top; | 
					
						
							| 
									
										
										
										
											2010-03-31 08:43:18 +08:00
										 |  |  |   int x2 = sprite->getWidth() + m_right; | 
					
						
							|  |  |  |   int y2 = sprite->getHeight() + m_bottom; | 
					
						
							| 
									
										
										
										
											2009-10-09 09:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (x2 <= x1) x2 = x1+1; | 
					
						
							|  |  |  |   if (y2 <= y1) y2 = y1+1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     Undoable undoable(sprite, "Canvas Size"); | 
					
						
							| 
									
										
										
										
											2010-08-25 11:26:37 +08:00
										 |  |  |     int bgcolor = color_utils::color_for_image(context->getSettings()->getBgColor(), sprite->getImgType()); | 
					
						
							|  |  |  |     bgcolor = color_utils::fixup_color_for_background(sprite->getImgType(), bgcolor); | 
					
						
							| 
									
										
										
										
											2010-04-11 04:01:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-19 21:33:07 +08:00
										 |  |  |     undoable.cropSprite(x1, y1, x2-x1, y2-y1, bgcolor); | 
					
						
							| 
									
										
										
										
											2009-10-09 09:34:06 +08:00
										 |  |  |     undoable.commit(); | 
					
						
							| 
									
										
										
										
											2009-06-01 00:02:32 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-03-31 08:43:18 +08:00
										 |  |  |   sprite->generateMaskBoundaries(); | 
					
						
							| 
									
										
										
										
											2009-10-09 09:34:06 +08:00
										 |  |  |   update_screen_for_sprite(sprite); | 
					
						
							| 
									
										
										
										
											2009-06-01 00:02:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-09 09:34:06 +08:00
										 |  |  | //////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // CommandFactory
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Command* CommandFactory::create_canvas_size_command() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return new CanvasSizeCommand; | 
					
						
							|  |  |  | } |