Add SetPaletteCommand

This commit is contained in:
David Capello 2014-04-17 01:29:58 -03:00
parent 2b7bd3e831
commit 283d883d37
5 changed files with 137 additions and 22 deletions

View File

@ -75,6 +75,7 @@ add_library(app-lib
commands/cmd_save_mask.cpp commands/cmd_save_mask.cpp
commands/cmd_save_palette.cpp commands/cmd_save_palette.cpp
commands/cmd_scroll.cpp commands/cmd_scroll.cpp
commands/cmd_set_palette.cpp
commands/cmd_sprite_editor.cpp commands/cmd_sprite_editor.cpp
commands/cmd_sprite_properties.cpp commands/cmd_sprite_properties.cpp
commands/cmd_sprite_size.cpp commands/cmd_sprite_size.cpp

View File

@ -20,18 +20,15 @@
#include "config.h" #include "config.h"
#endif #endif
#include "app/commands/cmd_set_palette.h"
#include "app/commands/command.h" #include "app/commands/command.h"
#include "app/context_access.h" #include "app/commands/commands.h"
#include "app/document_api.h" #include "app/context.h"
#include "app/file_selector.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/compiler_specific.h"
#include "base/unique_ptr.h" #include "base/unique_ptr.h"
#include "raster/palette.h" #include "raster/palette.h"
#include "ui/alert.h" #include "ui/alert.h"
#include "ui/manager.h"
namespace app { namespace app {
@ -55,30 +52,18 @@ LoadPaletteCommand::LoadPaletteCommand()
void LoadPaletteCommand::onExecute(Context* context) void LoadPaletteCommand::onExecute(Context* context)
{ {
ContextWriter writer(context);
base::string filename = app::show_file_selector("Load Palette", "", "png,pcx,bmp,tga,lbm,col,gpl"); base::string filename = app::show_file_selector("Load Palette", "", "png,pcx,bmp,tga,lbm,col,gpl");
if (!filename.empty()) { if (!filename.empty()) {
base::UniquePtr<raster::Palette> palette(raster::Palette::load(filename.c_str())); base::UniquePtr<raster::Palette> palette(raster::Palette::load(filename.c_str()));
if (!palette) { if (!palette) {
Alert::show("Error<<Loading palette file||&Close"); Alert::show("Error<<Loading palette file||&Close");
} }
else if (writer.document()) {
UndoTransaction undoTransaction(writer.context(), "Load Palette");
writer.document()->getApi()
.setPalette(writer.sprite(), writer.frame(), palette);
undoTransaction.commit();
}
else { else {
set_default_palette(palette); SetPaletteCommand* cmd = static_cast<SetPaletteCommand*>(
set_config_string("GfxMode", "Palette", filename.c_str()); 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();
} }
} }

View File

@ -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

View File

@ -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

View File

@ -97,6 +97,7 @@ FOR_EACH_COMMAND(SaveFileCopyAs)
FOR_EACH_COMMAND(SaveMask) FOR_EACH_COMMAND(SaveMask)
FOR_EACH_COMMAND(SavePalette) FOR_EACH_COMMAND(SavePalette)
FOR_EACH_COMMAND(Scroll) FOR_EACH_COMMAND(Scroll)
FOR_EACH_COMMAND(SetPalette)
FOR_EACH_COMMAND(ShowGrid) FOR_EACH_COMMAND(ShowGrid)
FOR_EACH_COMMAND(SnapToGrid) FOR_EACH_COMMAND(SnapToGrid)
FOR_EACH_COMMAND(SplitEditorHorizontally) FOR_EACH_COMMAND(SplitEditorHorizontally)