Editable resize factor in File > Export (fix #3007)

Related to #3008
This commit is contained in:
David Capello 2022-06-15 12:25:31 -03:00
parent af74f8bc84
commit b7d5d4a2c9
6 changed files with 46 additions and 29 deletions

View File

@ -1,4 +1,5 @@
<!-- Aseprite -->
<!-- Copyright (C) 2022 by Igara Studio S.A. -->
<!-- Copyright (C) 2016-2018 by David Capello -->
<gui>
<window id="export_file" text="@.title">
@ -8,19 +9,20 @@
<button id="output_filename_browse" text="..." style="mini_button" />
<label id="resize_label" text="@.resize" />
<combobox id="resize" cell_align="horizontal" cell_hspan="2">
<listitem text="25%" value="0.25" />
<listitem text="50%" value="0.5" />
<listitem text="100%" value="1" />
<listitem text="200%" value="2" />
<listitem text="300%" value="3" />
<listitem text="400%" value="4" />
<listitem text="500%" value="5" />
<listitem text="600%" value="6" />
<listitem text="700%" value="7" />
<listitem text="800%" value="8" />
<listitem text="900%" value="9" />
<listitem text="1000%" value="10" />
<combobox id="resize" editable="true" suffix="%"
cell_align="horizontal" cell_hspan="2">
<listitem text="25" />
<listitem text="50" />
<listitem text="100" />
<listitem text="200" />
<listitem text="300" />
<listitem text="400" />
<listitem text="500" />
<listitem text="600" />
<listitem text="700" />
<listitem text="800" />
<listitem text="900" />
<listitem text="1000" />
</combobox>
<label id="layers_label" text="@.layers" />

View File

@ -345,7 +345,7 @@ void SaveFileCopyAsCommand::onExecute(Context* context)
std::string layers = kAllLayers;
std::string frames = kAllFrames;
bool applyPixelRatio = false;
gfx::PointF scale(params().scale(), params().scale());
double scale = params().scale();
doc::AniDir aniDirValue = params().aniDir();
bool isForTwitter = false;
@ -409,18 +409,20 @@ void SaveFileCopyAsCommand::onExecute(Context* context)
layers = win.layersValue();
frames = win.framesValue();
scale.x = scale.y = win.resizeValue();
scale = win.resizeValue();
applyPixelRatio = win.applyPixelRatio();
aniDirValue = win.aniDirValue();
isForTwitter = win.isForTwitter();
}
#endif
gfx::PointF scaleXY(scale, scale);
// Pixel ratio
if (applyPixelRatio) {
doc::PixelRatio pr = doc->sprite()->pixelRatio();
scale.x *= pr.w;
scale.y *= pr.h;
scaleXY.x *= pr.w;
scaleXY.y *= pr.h;
}
// First of all we'll try to use the "on the fly" scaling, to avoid
@ -428,14 +430,15 @@ void SaveFileCopyAsCommand::onExecute(Context* context)
const undo::UndoState* undoState = nullptr;
bool undoResize = false;
const bool resizeOnTheFly = FileOp::checkIfFormatSupportResizeOnTheFly(outputFilename);
if (!resizeOnTheFly && (scale.x != 1.0 || scale.y != 1.0)) {
if (!resizeOnTheFly && (scaleXY.x != 1.0 ||
scaleXY.y != 1.0)) {
Command* resizeCmd = Commands::instance()->byId(CommandId::SpriteSize());
ASSERT(resizeCmd);
if (resizeCmd) {
int width = doc->sprite()->width();
int height = doc->sprite()->height();
int newWidth = int(double(width) * scale.x);
int newHeight = int(double(height) * scale.y);
int newWidth = int(double(width) * scaleXY.x);
int newHeight = int(double(height) * scaleXY.y);
if (newWidth < 1) newWidth = 1;
if (newHeight < 1) newHeight = 1;
if (width != newWidth || height != newHeight) {
@ -489,7 +492,7 @@ void SaveFileCopyAsCommand::onExecute(Context* context)
MarkAsSaved::Off,
(resizeOnTheFly ? ResizeOnTheFly::On:
ResizeOnTheFly::Off),
scale);
scaleXY);
}
// Undo resize

View File

@ -51,8 +51,7 @@ ExportFileWindow::ExportFileWindow(const Doc* doc)
}
// Default export configuration
resize()->setValue(
base::convert_to<std::string>(m_docPref.saveCopy.resizeScale()));
setResizeScale(m_docPref.saveCopy.resizeScale());
fill_layers_combobox(m_doc->sprite(), layers(), m_docPref.saveCopy.layer());
fill_frames_combobox(m_doc->sprite(), frames(), m_docPref.saveCopy.frameTag());
fill_anidir_combobox(anidir(), m_docPref.saveCopy.aniDir());
@ -113,7 +112,8 @@ std::string ExportFileWindow::outputFilenameValue() const
double ExportFileWindow::resizeValue() const
{
return base::convert_to<double>(resize()->getValue());
double value = resize()->getEntryWidget()->textDouble() / 100.0;
return std::clamp(value, 0.001, 100000000.0);
}
std::string ExportFileWindow::layersValue() const
@ -141,10 +141,9 @@ bool ExportFileWindow::isForTwitter() const
return forTwitter()->isSelected();
}
void ExportFileWindow::setResizeScale(const gfx::PointF& scale)
void ExportFileWindow::setResizeScale(double scale)
{
resize()->setValue(
base::convert_to<std::string>(scale.x)); // TODO support x & y
resize()->setValue(fmt::format("{:.2f}", 100.0 * scale));
}
void ExportFileWindow::setAniDir(const doc::AniDir aniDir)

View File

@ -35,7 +35,7 @@ namespace app {
bool isForTwitter() const;
void setOutputFilename(const std::string& pathAndFilename);
void setResizeScale(const gfx::PointF& scale);
void setResizeScale(const double scale);
void setAniDir(const doc::AniDir aniDir);
obs::signal<std::string()> SelectOutputFile;

View File

@ -231,6 +231,10 @@ Widget* WidgetLoader::convertXmlElementToWidget(const TiXmlElement* elem, Widget
bool editable = bool_attr(elem, "editable", false);
if (editable)
((ComboBox*)widget)->setEditable(true);
const char* suffix = elem->Attribute("suffix");
if (suffix)
((ComboBox*)widget)->getEntryWidget()->setSuffix(suffix);
}
else if (elem_name == "entry" ||
elem_name == "expr") {

View File

@ -327,7 +327,8 @@ void ComboBox::setValue(const std::string& value)
{
if (isEditable()) {
m_entry->setText(value);
m_entry->selectAllText();
if (hasFocus())
m_entry->selectAllText();
}
else {
int index = findItemIndexByValue(value);
@ -536,6 +537,14 @@ bool ComboBoxEntry::onProcessMessage(Message* msg)
return result;
}
case kFocusLeaveMessage:
if (m_comboBox->isEditable() &&
m_comboBox->m_window &&
!View::getView(m_comboBox->m_listbox)->hasMouse()) {
m_comboBox->closeListBox();
}
break;
}
return Entry::onProcessMessage(msg);