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"
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "app/color.h"
|
|
|
|
#include "app/color_utils.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/commands/command.h"
|
|
|
|
#include "app/console.h"
|
|
|
|
#include "app/document.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/modules/palettes.h"
|
|
|
|
#include "app/ui/color_bar.h"
|
|
|
|
#include "app/ui/workspace.h"
|
|
|
|
#include "app/ui_context.h"
|
|
|
|
#include "app/util/clipboard.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "base/unique_ptr.h"
|
2013-03-12 07:29:45 +08:00
|
|
|
#include "raster/cel.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "raster/image.h"
|
|
|
|
#include "raster/layer.h"
|
|
|
|
#include "raster/palette.h"
|
2013-11-10 06:59:05 +08:00
|
|
|
#include "raster/primitives.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "raster/sprite.h"
|
2013-03-12 07:29:45 +08:00
|
|
|
#include "raster/stock.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "ui/ui.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
#include <allegro/config.h>
|
|
|
|
#include <allegro/unicode.h>
|
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
using namespace ui;
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
class NewFileCommand : public Command {
|
2012-01-06 06:45:03 +08:00
|
|
|
public:
|
|
|
|
NewFileCommand();
|
|
|
|
Command* clone() { return new NewFileCommand(*this); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void onExecute(Context* context);
|
|
|
|
};
|
|
|
|
|
|
|
|
static int _sprite_counter = 0;
|
|
|
|
|
|
|
|
NewFileCommand::NewFileCommand()
|
|
|
|
: Command("NewFile",
|
|
|
|
"New File",
|
|
|
|
CmdRecordableFlag)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows the "New Sprite" dialog.
|
|
|
|
*/
|
|
|
|
void NewFileCommand::onExecute(Context* context)
|
|
|
|
{
|
2012-02-13 10:21:06 +08:00
|
|
|
PixelFormat format;
|
|
|
|
int w, h, bg, ncolors;
|
2012-01-06 06:45:03 +08:00
|
|
|
char buf[1024];
|
2013-01-07 01:45:43 +08:00
|
|
|
app::Color bg_table[] = {
|
|
|
|
app::Color::fromMask(),
|
|
|
|
app::Color::fromRgb(0, 0, 0),
|
|
|
|
app::Color::fromRgb(255, 255, 255),
|
|
|
|
app::Color::fromRgb(255, 0, 255),
|
2012-07-10 00:20:58 +08:00
|
|
|
ColorBar::instance()->getBgColor()
|
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>("new_sprite.xml", "new_sprite"));
|
2012-06-16 10:37:59 +08:00
|
|
|
Widget* width = app::find_widget<Widget>(window, "width");
|
|
|
|
Widget* height = app::find_widget<Widget>(window, "height");
|
|
|
|
Widget* radio1 = app::find_widget<Widget>(window, "radio1");
|
|
|
|
Widget* radio2 = app::find_widget<Widget>(window, "radio2");
|
|
|
|
Widget* radio3 = app::find_widget<Widget>(window, "radio3");
|
|
|
|
Widget* colors = app::find_widget<Widget>(window, "colors");
|
|
|
|
ListBox* bg_box = app::find_widget<ListBox>(window, "bg_box");
|
|
|
|
Widget* ok = app::find_widget<Widget>(window, "ok_button");
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
// Default values: Indexed, 320x240, Background color
|
2012-02-13 10:21:06 +08:00
|
|
|
format = static_cast<PixelFormat>(get_config_int("NewSprite", "Type", IMAGE_INDEXED));
|
|
|
|
// Invalid format in config file.
|
|
|
|
if (format != IMAGE_RGB && format != IMAGE_INDEXED && format != IMAGE_GRAYSCALE) {
|
|
|
|
format = IMAGE_INDEXED;
|
|
|
|
}
|
2012-01-06 06:45:03 +08:00
|
|
|
w = get_config_int("NewSprite", "Width", 320);
|
|
|
|
h = get_config_int("NewSprite", "Height", 240);
|
|
|
|
bg = get_config_int("NewSprite", "Background", 4); // Default = Background color
|
|
|
|
ncolors = get_config_int("NewSprite", "Colors", 256);
|
|
|
|
|
2012-02-12 04:06:35 +08:00
|
|
|
// If the clipboard contains an image, we can show the size of the
|
|
|
|
// clipboard as default image size.
|
|
|
|
gfx::Size clipboardSize;
|
2013-08-06 08:20:19 +08:00
|
|
|
if (clipboard::get_image_size(clipboardSize)) {
|
2012-02-12 04:06:35 +08:00
|
|
|
w = clipboardSize.w;
|
|
|
|
h = clipboardSize.h;
|
|
|
|
}
|
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
width->setTextf("%d", MAX(1, w));
|
|
|
|
height->setTextf("%d", MAX(1, h));
|
|
|
|
colors->setTextf("%d", MID(2, ncolors, 256));
|
|
|
|
|
|
|
|
// Select image-type
|
2012-02-13 10:21:06 +08:00
|
|
|
switch (format) {
|
2012-01-06 06:45:03 +08:00
|
|
|
case IMAGE_RGB: radio1->setSelected(true); break;
|
|
|
|
case IMAGE_GRAYSCALE: radio2->setSelected(true); break;
|
|
|
|
case IMAGE_INDEXED: radio3->setSelected(true); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select background color
|
2012-04-06 10:19:32 +08:00
|
|
|
bg_box->selectIndex(bg);
|
2012-01-06 06:45:03 +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() == ok) {
|
2012-01-06 06:45:03 +08:00
|
|
|
bool ok = false;
|
|
|
|
|
|
|
|
// Get the options
|
2012-02-13 10:21:06 +08:00
|
|
|
if (radio1->isSelected()) format = IMAGE_RGB;
|
|
|
|
else if (radio2->isSelected()) format = IMAGE_GRAYSCALE;
|
|
|
|
else if (radio3->isSelected()) format = IMAGE_INDEXED;
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
w = width->getTextInt();
|
|
|
|
h = height->getTextInt();
|
|
|
|
ncolors = colors->getTextInt();
|
2012-04-06 10:19:32 +08:00
|
|
|
bg = bg_box->getSelectedIndex();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2012-05-20 03:18:20 +08:00
|
|
|
w = MID(1, w, 65535);
|
|
|
|
h = MID(1, h, 65535);
|
2012-01-06 06:45:03 +08:00
|
|
|
ncolors = MID(2, ncolors, 256);
|
|
|
|
|
|
|
|
// Select the color
|
2013-01-07 01:45:43 +08:00
|
|
|
app::Color color = app::Color::fromMask();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
if (bg >= 0 && bg <= 4) {
|
|
|
|
color = bg_table[bg];
|
|
|
|
ok = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ok) {
|
|
|
|
// Save the configuration
|
2012-02-13 10:21:06 +08:00
|
|
|
set_config_int("NewSprite", "Type", format);
|
2012-01-06 06:45:03 +08:00
|
|
|
set_config_int("NewSprite", "Width", w);
|
|
|
|
set_config_int("NewSprite", "Height", h);
|
|
|
|
set_config_int("NewSprite", "Background", bg);
|
|
|
|
|
|
|
|
// Create the new sprite
|
2012-02-13 10:21:06 +08:00
|
|
|
ASSERT(format == IMAGE_RGB || format == IMAGE_GRAYSCALE || format == IMAGE_INDEXED);
|
2012-05-20 03:18:20 +08:00
|
|
|
ASSERT(w > 0 && h > 0);
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
base::UniquePtr<Document> document(
|
2012-02-13 10:21:06 +08:00
|
|
|
Document::createBasicDocument(format, w, h,
|
|
|
|
(format == IMAGE_INDEXED ? ncolors: 256)));
|
2012-01-06 06:45:03 +08:00
|
|
|
Sprite* sprite(document->getSprite());
|
|
|
|
|
2013-03-12 07:29:45 +08:00
|
|
|
get_default_palette()->copyColorsTo(sprite->getPalette(FrameNumber(0)));
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
usprintf(buf, "Sprite-%04d", ++_sprite_counter);
|
|
|
|
document->setFilename(buf);
|
|
|
|
|
|
|
|
// If the background color isn't transparent, we have to
|
|
|
|
// convert the `Layer 1' in a `Background'
|
2013-01-07 01:45:43 +08:00
|
|
|
if (color.getType() != app::Color::MaskType) {
|
2012-01-06 06:45:03 +08:00
|
|
|
Sprite* sprite = document->getSprite();
|
2013-03-12 07:29:45 +08:00
|
|
|
Layer* layer = sprite->getFolder()->getFirstLayer();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-03-12 07:29:45 +08:00
|
|
|
if (layer && layer->isImage()) {
|
|
|
|
LayerImage* layerImage = static_cast<LayerImage*>(layer);
|
|
|
|
layerImage->configureAsBackground();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-03-12 07:29:45 +08:00
|
|
|
Image* image = sprite->getStock()->getImage(layerImage->getCel(FrameNumber(0))->getImage());
|
2013-11-10 06:59:05 +08:00
|
|
|
raster::clear_image(image, color_utils::color_for_image(color, format));
|
2013-03-12 07:29:45 +08:00
|
|
|
}
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show the sprite to the user
|
2013-01-21 05:40:37 +08:00
|
|
|
context->addDocument(document.release());
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Command* CommandFactory::createNewFileCommand()
|
|
|
|
{
|
|
|
|
return new NewFileCommand;
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|