From 0bddf2611c8433bbf89eb69335be6c8c30eed9d6 Mon Sep 17 00:00:00 2001 From: David Capello Date: Sun, 31 Jul 2011 22:55:52 -0300 Subject: [PATCH] Add "Import Sprite Sheet" command (part of issue #4). --- data/gui.xml | 2 + src/CMakeLists.txt | 1 + src/commands/cmd_import_sprite_sheet.cpp | 329 +++++++++++++++++++++++ src/commands/commands_list.h | 1 + 4 files changed, 333 insertions(+) create mode 100644 src/commands/cmd_import_sprite_sheet.cpp diff --git a/data/gui.xml b/data/gui.xml index 1c1af8bc2b..706b5d6315 100644 --- a/data/gui.xml +++ b/data/gui.xml @@ -176,6 +176,8 @@ + + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 982fc31215..a86da0e608 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -146,6 +146,7 @@ add_library(aseprite-library commands/cmd_goto_frame.cpp commands/cmd_goto_layer.cpp commands/cmd_grid.cpp + commands/cmd_import_sprite_sheet.cpp commands/cmd_invert_mask.cpp commands/cmd_launch.cpp commands/cmd_layer_from_background.cpp diff --git a/src/commands/cmd_import_sprite_sheet.cpp b/src/commands/cmd_import_sprite_sheet.cpp new file mode 100644 index 0000000000..a6780172ad --- /dev/null +++ b/src/commands/cmd_import_sprite_sheet.cpp @@ -0,0 +1,329 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 David Capello + * + * 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 "base/bind.h" +#include "commands/command.h" +#include "commands/commands.h" +#include "commands/params.h" +#include "dialogs/filesel.h" +#include "document_wrappers.h" +#include "gui/gui.h" +#include "ini_file.h" +#include "modules/editors.h" +#include "modules/gui.h" +#include "modules/palettes.h" +#include "raster/cel.h" +#include "raster/image.h" +#include "raster/layer.h" +#include "raster/palette.h" +#include "raster/sprite.h" +#include "raster/stock.h" +#include "undo_transaction.h" +#include "widgets/drop_down_button.h" +#include "widgets/editor/editor.h" +#include "widgets/editor/editor_decorator.h" +#include "widgets/editor/select_box_state.h" +#include "widgets/editor/standby_state.h" + +#include + +////////////////////////////////////////////////////////////////////// +// ImportSpriteSheetFrame + +class ImportSpriteSheetFrame : public Frame, + public SelectBoxDelegate +{ +public: + ImportSpriteSheetFrame(Context* context) + : Frame(false, "Import Sprite Sheet") + , m_context(context) + , m_document(NULL) + , m_grid(4, false) + , m_selectFile("Select File") + , m_x(4, "0") + , m_y(4, "0") + , m_width(4, "16") + , m_height(4, "16") + , m_import("Import") + , m_cancel("Cancel") + , m_editor(NULL) + , m_fileOpened(false) + { + addChild(&m_grid); + m_grid.addChildInCell(&m_selectFile, 4, 1, 0); + m_grid.addChildInCell(new Label("X"), 1, 1, 0); + m_grid.addChildInCell(&m_x, 1, 1, 0); + m_grid.addChildInCell(new Label("Width"), 1, 1, 0); + m_grid.addChildInCell(&m_width, 1, 1, 0); + m_grid.addChildInCell(new Label("Y"), 1, 1, 0); + m_grid.addChildInCell(&m_y, 1, 1, 0); + m_grid.addChildInCell(new Label("Height"), 1, 1, 0); + m_grid.addChildInCell(&m_height, 1, 1, 0); + m_grid.addChildInCell(&m_import, 4, 1, 0); + m_grid.addChildInCell(&m_cancel, 4, 1, 0); + + m_selectFile.Click.connect(&ImportSpriteSheetFrame::onSelectFile, this); + m_selectFile.DropDownClick.connect(&ImportSpriteSheetFrame::onDropDown, this); + m_import.Click.connect(Bind(&ImportSpriteSheetFrame::onImport, this)); + m_cancel.Click.connect(Bind(&ImportSpriteSheetFrame::onCancel, this)); + } + + ~ImportSpriteSheetFrame() + { + releaseEditor(); + } + +protected: + + void onSelectFile() + { + Document* oldActiveDocument = m_context->getActiveDocument(); + Command* openFile = CommandsModule::instance()->getCommandByName(CommandId::OpenFile); + Params params; + params.set("filename", ""); + openFile->loadParams(¶ms); + openFile->execute(m_context); + + // The user have selected another document. + if (oldActiveDocument != m_context->getActiveDocument()) { + selectActiveDocument(); + m_fileOpened = true; + } + } + + void onDropDown() + { + SharedPtr menu(new Menu()); + MenuItem* item = new MenuItem("Use Current Sprite"); + item->Click.connect(&ImportSpriteSheetFrame::onUseCurrentSprite, this); + + if (m_editor || current_editor->getDocument() == NULL) + item->setEnabled(false); + + menu->addChild(item); + + menu->showPopup(m_selectFile.rc->x1, m_selectFile.rc->y2); + } + + void onUseCurrentSprite() + { + selectActiveDocument(); + m_fileOpened = false; + } + + void onImport() + { + // The user don't select a sheet yet. + if (!m_document) { + Alert::show("Import Sprite Sheet<