From 283d883d3750bd26406ad57d4cdeff53b833fa5a Mon Sep 17 00:00:00 2001 From: David Capello Date: Thu, 17 Apr 2014 01:29:58 -0300 Subject: [PATCH] Add SetPaletteCommand --- src/app/CMakeLists.txt | 1 + src/app/commands/cmd_load_palette.cpp | 29 +++------- src/app/commands/cmd_set_palette.cpp | 80 +++++++++++++++++++++++++++ src/app/commands/cmd_set_palette.h | 48 ++++++++++++++++ src/app/commands/commands_list.h | 1 + 5 files changed, 137 insertions(+), 22 deletions(-) create mode 100644 src/app/commands/cmd_set_palette.cpp create mode 100644 src/app/commands/cmd_set_palette.h diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index c59be24aa..6d05f824c 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -75,6 +75,7 @@ add_library(app-lib commands/cmd_save_mask.cpp commands/cmd_save_palette.cpp commands/cmd_scroll.cpp + commands/cmd_set_palette.cpp commands/cmd_sprite_editor.cpp commands/cmd_sprite_properties.cpp commands/cmd_sprite_size.cpp diff --git a/src/app/commands/cmd_load_palette.cpp b/src/app/commands/cmd_load_palette.cpp index f2d790e3f..ea5f666cb 100644 --- a/src/app/commands/cmd_load_palette.cpp +++ b/src/app/commands/cmd_load_palette.cpp @@ -20,18 +20,15 @@ #include "config.h" #endif +#include "app/commands/cmd_set_palette.h" #include "app/commands/command.h" -#include "app/context_access.h" -#include "app/document_api.h" +#include "app/commands/commands.h" +#include "app/context.h" #include "app/file_selector.h" -#include "app/ini_file.h" -#include "app/modules/palettes.h" -#include "app/undo_transaction.h" #include "base/compiler_specific.h" #include "base/unique_ptr.h" #include "raster/palette.h" #include "ui/alert.h" -#include "ui/manager.h" namespace app { @@ -55,30 +52,18 @@ LoadPaletteCommand::LoadPaletteCommand() void LoadPaletteCommand::onExecute(Context* context) { - ContextWriter writer(context); - base::string filename = app::show_file_selector("Load Palette", "", "png,pcx,bmp,tga,lbm,col,gpl"); if (!filename.empty()) { base::UniquePtr palette(raster::Palette::load(filename.c_str())); if (!palette) { Alert::show("Error<getApi() - .setPalette(writer.sprite(), writer.frame(), palette); - undoTransaction.commit(); - } else { - set_default_palette(palette); - set_config_string("GfxMode", "Palette", filename.c_str()); + SetPaletteCommand* cmd = static_cast( + CommandsModule::instance()->getCommandByName(CommandId::SetPalette)); + cmd->setPalette(palette); + context->executeCommand(cmd); } - - // Set the palette calling the hooks - set_current_palette(palette, false); - - // Redraw the entire screen - ui::Manager::getDefault()->invalidate(); } } diff --git a/src/app/commands/cmd_set_palette.cpp b/src/app/commands/cmd_set_palette.cpp new file mode 100644 index 000000000..e4385c618 --- /dev/null +++ b/src/app/commands/cmd_set_palette.cpp @@ -0,0 +1,80 @@ +/* Aseprite + * Copyright (C) 2001-2014 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 + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "app/commands/cmd_set_palette.h" +#include "app/context_access.h" +#include "app/document_api.h" +#include "app/file_selector.h" +#include "app/ini_file.h" +#include "app/modules/palettes.h" +#include "app/undo_transaction.h" +#include "base/compiler_specific.h" +#include "base/unique_ptr.h" +#include "raster/palette.h" +#include "ui/alert.h" +#include "ui/manager.h" + +namespace app { + +using namespace ui; + +SetPaletteCommand::SetPaletteCommand() + : Command("SetPalette", + "Set Palette", + CmdRecordableFlag) + , m_palette(NULL) +{ +} + +void SetPaletteCommand::onExecute(Context* context) +{ + ASSERT(m_palette); + if (!m_palette) + return; + + ContextWriter writer(context); + + if (writer.document()) { + UndoTransaction undoTransaction(writer.context(), "Set Palette"); + writer.document()->getApi() + .setPalette(writer.sprite(), writer.frame(), m_palette); + undoTransaction.commit(); + } + // Set default palette + else { + set_default_palette(m_palette); + set_config_string("GfxMode", "Palette", m_palette->getFilename().c_str()); + } + + // Set the palette calling the hooks + set_current_palette(m_palette, false); + + // Redraw the entire screen + ui::Manager::getDefault()->invalidate(); +} + +Command* CommandFactory::createSetPaletteCommand() +{ + return new SetPaletteCommand; +} + +} // namespace app diff --git a/src/app/commands/cmd_set_palette.h b/src/app/commands/cmd_set_palette.h new file mode 100644 index 000000000..571ba9c79 --- /dev/null +++ b/src/app/commands/cmd_set_palette.h @@ -0,0 +1,48 @@ +/* Aseprite + * Copyright (C) 2001-2014 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 + */ + +#ifndef APP_COMMANDS_CMD_SET_PALETTE_H_INCLUDED +#define APP_COMMANDS_CMD_SET_PALETTE_H_INCLUDED +#pragma once + +#include "app/commands/command.h" +#include "base/compiler_specific.h" + +namespace raster { + class Palette; +} + +namespace app { + + class SetPaletteCommand : public Command { + public: + SetPaletteCommand(); + Command* clone() const OVERRIDE { return new SetPaletteCommand(*this); } + + void setPalette(raster::Palette* palette) { m_palette = palette; } + + protected: + virtual void onExecute(Context* context) OVERRIDE; + + private: + raster::Palette* m_palette; + }; + +} // namespace app + +#endif diff --git a/src/app/commands/commands_list.h b/src/app/commands/commands_list.h index da9fcf8a6..59ade8827 100644 --- a/src/app/commands/commands_list.h +++ b/src/app/commands/commands_list.h @@ -97,6 +97,7 @@ FOR_EACH_COMMAND(SaveFileCopyAs) FOR_EACH_COMMAND(SaveMask) FOR_EACH_COMMAND(SavePalette) FOR_EACH_COMMAND(Scroll) +FOR_EACH_COMMAND(SetPalette) FOR_EACH_COMMAND(ShowGrid) FOR_EACH_COMMAND(SnapToGrid) FOR_EACH_COMMAND(SplitEditorHorizontally)