2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2018-10-19 02:29:16 +08:00
|
|
|
// Copyright (C) 2018 Igara Studio S.A.
|
2018-08-21 03:00:59 +08:00
|
|
|
// Copyright (C) 2001-2018 David Capello
|
2015-02-12 23:16:25 +08:00
|
|
|
//
|
2016-08-27 04:02:58 +08:00
|
|
|
// This program is distributed under the terms of
|
|
|
|
// the End-User License Agreement for Aseprite.
|
2012-01-06 06:45:03 +08:00
|
|
|
|
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
|
|
|
|
2018-10-19 02:29:16 +08:00
|
|
|
#include "app/cmd/assign_color_profile.h"
|
|
|
|
#include "app/cmd/convert_color_profile.h"
|
2016-05-24 23:25:11 +08:00
|
|
|
#include "app/cmd/set_pixel_ratio.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "app/color.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/commands/command.h"
|
|
|
|
#include "app/context_access.h"
|
2018-07-07 14:07:16 +08:00
|
|
|
#include "app/doc_api.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/modules/gui.h"
|
2018-10-19 02:29:16 +08:00
|
|
|
#include "app/pref/preferences.h"
|
2018-08-21 03:00:59 +08:00
|
|
|
#include "app/tx.h"
|
2016-05-24 23:25:11 +08:00
|
|
|
#include "app/ui/color_button.h"
|
|
|
|
#include "app/util/pixel_ratio.h"
|
2012-06-16 10:37:59 +08:00
|
|
|
#include "base/bind.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "base/mem_utils.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/image.h"
|
|
|
|
#include "doc/palette.h"
|
|
|
|
#include "doc/sprite.h"
|
2018-10-19 02:29:16 +08:00
|
|
|
#include "fmt/format.h"
|
|
|
|
#include "os/color_space.h"
|
|
|
|
#include "os/system.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "ui/ui.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2015-09-23 03:33:49 +08:00
|
|
|
#include "sprite_properties.xml.h"
|
2015-06-22 22:20:10 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
2012-06-18 09:02:54 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
using namespace ui;
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
class SpritePropertiesCommand : public Command {
|
2012-01-06 06:45:03 +08:00
|
|
|
public:
|
|
|
|
SpritePropertiesCommand();
|
2014-08-15 10:07:47 +08:00
|
|
|
Command* clone() const override { return new SpritePropertiesCommand(*this); }
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
protected:
|
2015-10-01 03:34:43 +08:00
|
|
|
bool onEnabled(Context* context) override;
|
|
|
|
void onExecute(Context* context) override;
|
2012-01-06 06:45:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
SpritePropertiesCommand::SpritePropertiesCommand()
|
2017-12-02 02:10:21 +08:00
|
|
|
: Command(CommandId::SpriteProperties(), CmdUIOnlyFlag)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpritePropertiesCommand::onEnabled(Context* context)
|
|
|
|
{
|
|
|
|
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
|
|
|
|
ContextFlags::HasActiveSprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpritePropertiesCommand::onExecute(Context* context)
|
|
|
|
{
|
2014-04-21 06:53:27 +08:00
|
|
|
std::string imgtype_text;
|
2018-10-19 02:29:16 +08:00
|
|
|
ColorButton* color_button = nullptr;
|
|
|
|
|
|
|
|
// List of available color profiles
|
|
|
|
std::vector<os::ColorSpacePtr> colorSpaces;
|
|
|
|
os::instance()->listColorSpaces(colorSpaces);
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
// Load the window widget
|
2015-06-22 22:20:10 +08:00
|
|
|
app::gen::SpriteProperties window;
|
2018-10-19 02:29:16 +08:00
|
|
|
int selectedColorProfile = -1;
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
// Get sprite properties and fill frame fields
|
|
|
|
{
|
2013-03-12 07:29:45 +08:00
|
|
|
const ContextReader reader(context);
|
2018-07-07 22:54:44 +08:00
|
|
|
const Doc* document(reader.document());
|
2013-03-12 07:29:45 +08:00
|
|
|
const Sprite* sprite(reader.sprite());
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
// Update widgets values
|
2014-07-30 12:28:15 +08:00
|
|
|
switch (sprite->pixelFormat()) {
|
2012-01-06 06:45:03 +08:00
|
|
|
case IMAGE_RGB:
|
|
|
|
imgtype_text = "RGB";
|
|
|
|
break;
|
|
|
|
case IMAGE_GRAYSCALE:
|
|
|
|
imgtype_text = "Grayscale";
|
|
|
|
break;
|
|
|
|
case IMAGE_INDEXED:
|
2018-10-19 02:29:16 +08:00
|
|
|
imgtype_text = fmt::format("Indexed ({0} colors)",
|
|
|
|
sprite->palette(0)->size());
|
2012-01-06 06:45:03 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT(false);
|
|
|
|
imgtype_text = "Unknown";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filename
|
2015-06-22 22:20:10 +08:00
|
|
|
window.name()->setText(document->filename());
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
// Color mode
|
2015-06-22 22:20:10 +08:00
|
|
|
window.type()->setText(imgtype_text.c_str());
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
// Sprite size (width and height)
|
2015-06-22 22:20:10 +08:00
|
|
|
window.size()->setTextf(
|
|
|
|
"%dx%d (%s)",
|
2014-09-21 22:59:39 +08:00
|
|
|
sprite->width(),
|
|
|
|
sprite->height(),
|
|
|
|
base::get_pretty_memory_size(sprite->getMemSize()).c_str());
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
// How many frames
|
2015-06-22 22:20:10 +08:00
|
|
|
window.frames()->setTextf("%d", (int)sprite->totalFrames());
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2014-07-30 12:28:15 +08:00
|
|
|
if (sprite->pixelFormat() == IMAGE_INDEXED) {
|
|
|
|
color_button = new ColorButton(app::Color::fromIndex(sprite->transparentColor()),
|
2016-09-13 04:53:01 +08:00
|
|
|
IMAGE_INDEXED,
|
2017-06-23 05:39:28 +08:00
|
|
|
ColorButtonOptions());
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2015-06-22 22:20:10 +08:00
|
|
|
window.transparentColorPlaceholder()->addChild(color_button);
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
else {
|
2015-06-22 22:20:10 +08:00
|
|
|
window.transparentColorPlaceholder()->addChild(new Label("(only for indexed images)"));
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
2016-05-24 23:25:11 +08:00
|
|
|
|
|
|
|
// Pixel ratio
|
|
|
|
window.pixelRatio()->setValue(
|
|
|
|
base::convert_to<std::string>(sprite->pixelRatio()));
|
2018-10-19 02:29:16 +08:00
|
|
|
|
|
|
|
// Color profile
|
|
|
|
selectedColorProfile = -1;
|
|
|
|
int i = 0;
|
|
|
|
for (auto& cs : colorSpaces) {
|
|
|
|
if (cs->gfxColorSpace()->nearlyEqual(*sprite->colorSpace())) {
|
|
|
|
selectedColorProfile = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
if (selectedColorProfile < 0) {
|
|
|
|
colorSpaces.push_back(os::instance()->createColorSpace(sprite->colorSpace()));
|
|
|
|
selectedColorProfile = colorSpaces.size()-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& cs : colorSpaces)
|
|
|
|
window.colorProfile()->addItem(cs->gfxColorSpace()->name());
|
|
|
|
window.colorProfile()->setSelectedItemIndex(selectedColorProfile);
|
|
|
|
|
|
|
|
auto updateButtons =
|
|
|
|
[&] {
|
|
|
|
bool enabled = (selectedColorProfile != window.colorProfile()->getSelectedItemIndex());
|
|
|
|
window.assignColorProfile()->setEnabled(enabled);
|
|
|
|
window.convertColorProfile()->setEnabled(enabled);
|
|
|
|
window.ok()->setEnabled(!enabled);
|
|
|
|
};
|
|
|
|
|
|
|
|
window.assignColorProfile()->setEnabled(false);
|
|
|
|
window.convertColorProfile()->setEnabled(false);
|
|
|
|
window.colorProfile()->Change.connect(updateButtons);
|
|
|
|
|
|
|
|
window.assignColorProfile()->Click.connect(
|
|
|
|
[&](Event&){
|
|
|
|
selectedColorProfile = window.colorProfile()->getSelectedItemIndex();
|
|
|
|
|
|
|
|
ContextWriter writer(context);
|
|
|
|
Sprite* sprite(writer.sprite());
|
|
|
|
Tx tx(writer.context(), "Assign Color Profile");
|
|
|
|
tx(new cmd::AssignColorProfile(
|
|
|
|
sprite, colorSpaces[selectedColorProfile]->gfxColorSpace()));
|
|
|
|
tx.commit();
|
|
|
|
|
|
|
|
updateButtons();
|
|
|
|
});
|
|
|
|
window.convertColorProfile()->Click.connect(
|
|
|
|
[&](Event&){
|
|
|
|
selectedColorProfile = window.colorProfile()->getSelectedItemIndex();
|
|
|
|
|
|
|
|
ContextWriter writer(context);
|
|
|
|
Sprite* sprite(writer.sprite());
|
|
|
|
Tx tx(writer.context(), "Convert Color Profile");
|
|
|
|
tx(new cmd::ConvertColorProfile(
|
|
|
|
sprite, colorSpaces[selectedColorProfile]->gfxColorSpace()));
|
|
|
|
tx.commit();
|
|
|
|
|
|
|
|
updateButtons();
|
|
|
|
});
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-06-22 22:20:10 +08:00
|
|
|
window.remapWindow();
|
|
|
|
window.centerWindow();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2015-06-22 22:20:10 +08:00
|
|
|
load_window_pos(&window, "SpriteProperties");
|
|
|
|
window.setVisible(true);
|
|
|
|
window.openWindowInForeground();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2015-12-05 01:54:15 +08:00
|
|
|
if (window.closer() == window.ok()) {
|
2016-05-24 23:25:11 +08:00
|
|
|
ContextWriter writer(context);
|
|
|
|
Sprite* sprite(writer.sprite());
|
|
|
|
|
|
|
|
color_t index = (color_button ? color_button->getColor().getIndex():
|
|
|
|
sprite->transparentColor());
|
|
|
|
PixelRatio pixelRatio =
|
|
|
|
base::convert_to<PixelRatio>(window.pixelRatio()->getValue());
|
|
|
|
|
|
|
|
if (index != sprite->transparentColor() ||
|
|
|
|
pixelRatio != sprite->pixelRatio()) {
|
2018-08-21 03:00:59 +08:00
|
|
|
Tx tx(writer.context(), "Change Sprite Properties");
|
|
|
|
DocApi api = writer.document()->getApi(tx);
|
2016-05-24 23:25:11 +08:00
|
|
|
|
|
|
|
if (index != sprite->transparentColor())
|
2014-05-03 07:00:26 +08:00
|
|
|
api.setSpriteTransparentColor(sprite, index);
|
|
|
|
|
2016-05-24 23:25:11 +08:00
|
|
|
if (pixelRatio != sprite->pixelRatio())
|
2018-08-21 03:00:59 +08:00
|
|
|
tx(new cmd::SetPixelRatio(sprite, pixelRatio));
|
2016-05-24 23:25:11 +08:00
|
|
|
|
2018-08-21 03:00:59 +08:00
|
|
|
tx.commit();
|
2016-05-24 23:25:11 +08:00
|
|
|
|
|
|
|
update_screen_for_document(writer.document());
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-22 22:20:10 +08:00
|
|
|
save_window_pos(&window, "SpriteProperties");
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Command* CommandFactory::createSpritePropertiesCommand()
|
|
|
|
{
|
|
|
|
return new SpritePropertiesCommand;
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|