Add pixel ratio field in Sprite properties

This commit is contained in:
David Capello 2016-05-24 12:25:11 -03:00
parent 37209a0f5b
commit a9c18db7a4
11 changed files with 222 additions and 31 deletions

View File

@ -1,5 +1,5 @@
<!-- ASEPRITE -->
<!-- Copyright (C) 2001-2013, 2015 by David Capello -->
<!-- Copyright (C) 2001-2016 by David Capello -->
<gui>
<window text="Sprite Properties" id="sprite_properties">
<vbox>
@ -16,8 +16,18 @@
<label text="Frames:" />
<label text="" id="frames" />
<separator text="Advanced" horizontal="true" cell_hspan="2" />
<label text="Transparent Color:" />
<hbox id="transparent_color_placeholder" tooltip="Palette entry used as&#10;transparent color in each&#10;layer (only for indexed images)." />
<label text="Pixel Ratio:" />
<combobox id="pixel_ratio" cell_align="horizontal">
<listitem text="Square Pixels (1:1)" value="1:1" />
<listitem text="Double-wide Pixels (2:1)" value="2:1" />
<listitem text="Double-high Pixels (1:2)" value="1:2" />
</combobox>
</grid>
<separator horizontal="true" />
<hbox>

View File

@ -146,6 +146,7 @@ add_library(app-lib
cmd/set_mask_position.cpp
cmd/set_palette.cpp
cmd/set_pixel_format.cpp
cmd/set_pixel_ratio.cpp
cmd/set_sprite_size.cpp
cmd/set_total_frames.cpp
cmd/set_transparent_color.cpp
@ -433,6 +434,7 @@ add_library(app-lib
util/msk_file.cpp
util/new_image_from_mask.cpp
util/pic_file.cpp
util/pixel_ratio.cpp
util/range_utils.cpp
webserver.cpp
widget_loader.cpp

View File

@ -0,0 +1,54 @@
// Aseprite
// Copyright (C) 2016 David Capello
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/cmd/set_pixel_ratio.h"
#include "doc/document.h"
#include "doc/document_event.h"
#include "doc/document_observer.h"
#include "doc/sprite.h"
namespace app {
namespace cmd {
using namespace doc;
SetPixelRatio::SetPixelRatio(Sprite* sprite, PixelRatio pixelRatio)
: WithSprite(sprite)
, m_oldRatio(sprite->pixelRatio())
, m_newRatio(pixelRatio)
{
}
void SetPixelRatio::onExecute()
{
Sprite* spr = sprite();
spr->setPixelRatio(m_newRatio);
spr->incrementVersion();
}
void SetPixelRatio::onUndo()
{
Sprite* spr = sprite();
spr->setPixelRatio(m_oldRatio);
spr->incrementVersion();
}
void SetPixelRatio::onFireNotifications()
{
Sprite* sprite = this->sprite();
DocumentEvent ev(sprite->document());
ev.sprite(sprite);
sprite->document()->notifyObservers<DocumentEvent&>(&DocumentObserver::onSpritePixelRatioChanged, ev);
}
} // namespace cmd
} // namespace app

View File

@ -0,0 +1,47 @@
// Aseprite
// Copyright (C) 2016 David Capello
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
#ifndef APP_CMD_SET_PIXEL_RATIO_H_INCLUDED
#define APP_CMD_SET_PIXEL_RATIO_H_INCLUDED
#pragma once
#include "app/cmd.h"
#include "app/cmd/with_sprite.h"
#include "doc/pixel_ratio.h"
namespace doc {
class Sprite;
}
namespace app {
namespace cmd {
using namespace doc;
class SetPixelRatio : public Cmd
, public WithSprite {
public:
SetPixelRatio(Sprite* sprite, PixelRatio pixelRatio);
protected:
void onExecute() override;
void onUndo() override;
void onFireNotifications() override;
size_t onMemSize() const override {
return sizeof(*this);
}
private:
void setRatio(PixelRatio ratio);
PixelRatio m_oldRatio;
PixelRatio m_newRatio;
};
} // namespace cmd
} // namespace app
#endif

View File

@ -22,9 +22,8 @@
#include "app/ui/workspace.h"
#include "app/ui_context.h"
#include "app/util/clipboard.h"
#include "app/util/pixel_ratio.h"
#include "base/bind.h"
#include "base/convert_to.h"
#include "base/split_string.h"
#include "base/unique_ptr.h"
#include "doc/cel.h"
#include "doc/image.h"
@ -169,19 +168,8 @@ void NewFileCommand::onExecute(Context* context)
base::UniquePtr<Sprite> sprite(Sprite::createBasicSprite(format, w, h, ncolors));
if (window.advancedCheck()->isSelected()) {
std::string value = window.pixelRatio()->getValue();
std::vector<std::string> parts;
base::split_string(value, parts, ":");
PixelRatio pixelRatio(1, 1);
if (parts.size() == 2) {
pixelRatio.w = base::convert_to<int>(parts[0]);
pixelRatio.h = base::convert_to<int>(parts[1]);
pixelRatio.w = MAX(1, pixelRatio.w);
pixelRatio.h = MAX(1, pixelRatio.h);
}
sprite->setPixelRatio(pixelRatio);
sprite->setPixelRatio(
base::convert_to<PixelRatio>(window.pixelRatio()->getValue()));
}
if (sprite->pixelFormat() != IMAGE_GRAYSCALE)

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2001-2015 David Capello
// Copyright (C) 2001-2016 David Capello
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
@ -9,13 +9,15 @@
#include "config.h"
#endif
#include "app/cmd/set_pixel_ratio.h"
#include "app/color.h"
#include "app/commands/command.h"
#include "app/context_access.h"
#include "app/document_api.h"
#include "app/modules/gui.h"
#include "app/ui/color_button.h"
#include "app/transaction.h"
#include "app/ui/color_button.h"
#include "app/util/pixel_ratio.h"
#include "base/bind.h"
#include "base/mem_utils.h"
#include "doc/image.h"
@ -112,6 +114,10 @@ void SpritePropertiesCommand::onExecute(Context* context)
else {
window.transparentColorPlaceholder()->addChild(new Label("(only for indexed images)"));
}
// Pixel ratio
window.pixelRatio()->setValue(
base::convert_to<std::string>(sprite->pixelRatio()));
}
window.remapWindow();
@ -122,21 +128,28 @@ void SpritePropertiesCommand::onExecute(Context* context)
window.openWindowInForeground();
if (window.closer() == window.ok()) {
if (color_button) {
ContextWriter writer(context);
Sprite* sprite(writer.sprite());
ContextWriter writer(context);
Sprite* sprite(writer.sprite());
// If the transparent color index has changed, we update the
// property in the sprite.
int index = color_button->getColor().getIndex();
if (color_t(index) != sprite->transparentColor()) {
Transaction transaction(writer.context(), "Set Transparent Color");
DocumentApi api = writer.document()->getApi(transaction);
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()) {
Transaction transaction(writer.context(), "Change Sprite Properties");
DocumentApi api = writer.document()->getApi(transaction);
if (index != sprite->transparentColor())
api.setSpriteTransparentColor(sprite, index);
transaction.commit();
update_screen_for_document(writer.document());
}
if (pixelRatio != sprite->pixelRatio())
transaction.execute(new cmd::SetPixelRatio(sprite, pixelRatio));
transaction.commit();
update_screen_for_document(writer.document());
}
}

View File

@ -1417,6 +1417,12 @@ void Editor::onExposeSpritePixels(doc::DocumentEvent& ev)
m_state->onExposeSpritePixels(ev.region());
}
void Editor::onSpritePixelRatioChanged(doc::DocumentEvent& ev)
{
m_proj.setPixelRatio(ev.sprite()->pixelRatio());
invalidate();
}
void Editor::setCursor(const gfx::Point& mouseScreenPos)
{
bool used = false;

View File

@ -238,7 +238,10 @@ namespace app {
void onFgColorChange();
void onContextBarBrushChange();
void onShowExtrasChange();
// DocumentObserver impl
void onExposeSpritePixels(doc::DocumentEvent& ev) override;
void onSpritePixelRatioChanged(doc::DocumentEvent& ev) override;
// ActiveToolObserver impl
void onActiveToolChange(tools::Tool* tool) override;

View File

@ -0,0 +1,45 @@
// Aseprite
// Copyright (C) 2016 David Capello
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/util/pixel_ratio.h"
#include "base/split_string.h"
#include <string>
#include <vector>
namespace base {
template<>
doc::PixelRatio convert_to(const std::string& from)
{
std::vector<std::string> parts;
split_string(from, parts, ":");
doc::PixelRatio pixelRatio(1, 1);
if (parts.size() == 2) {
pixelRatio.w = convert_to<int>(parts[0]);
pixelRatio.h = convert_to<int>(parts[1]);
pixelRatio.w = std::max(1, pixelRatio.w);
pixelRatio.h = std::max(1, pixelRatio.h);
}
return pixelRatio;
}
template<>
std::string convert_to(const doc::PixelRatio& from)
{
return (convert_to<std::string>(from.w) + ":" +
convert_to<std::string>(from.h));
}
} // namespace base

View File

@ -0,0 +1,22 @@
// Aseprite
// Copyright (C) 2016 David Capello
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
#ifndef APP_UTIL_PIXEL_RATIO_H_INCLUDED
#define APP_UTIL_PIXEL_RATIO_H_INCLUDED
#pragma once
#include "base/convert_to.h"
#include "doc/pixel_ratio.h"
namespace base {
template<> doc::PixelRatio convert_to(const std::string& from);
template<> std::string convert_to(const doc::PixelRatio& from);
} // namespace base
#endif

View File

@ -1,5 +1,5 @@
// Aseprite Document Library
// Copyright (c) 2001-2015 David Capello
// Copyright (c) 2001-2016 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -39,6 +39,7 @@ namespace doc {
virtual void onSpriteSizeChanged(DocumentEvent& ev) { }
virtual void onSpriteTransparentColorChanged(DocumentEvent& ev) { }
virtual void onSpritePixelRatioChanged(DocumentEvent& ev) { }
virtual void onLayerNameChange(DocumentEvent& ev) { }
virtual void onLayerOpacityChange(DocumentEvent& ev) { }