2018-03-16 07:34:01 +08:00
|
|
|
// Aseprite
|
2024-03-13 02:13:35 +08:00
|
|
|
// Copyright (C) 2019-2024 Igara Studio S.A.
|
2018-03-16 07:34:01 +08:00
|
|
|
// Copyright (C) 2018 David Capello
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed under the terms of
|
|
|
|
|
// the End-User License Agreement for Aseprite.
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "app/ui/export_file_window.h"
|
|
|
|
|
|
2018-07-07 22:54:44 +08:00
|
|
|
#include "app/doc.h"
|
2018-06-27 21:00:19 +08:00
|
|
|
#include "app/file/file.h"
|
2018-03-16 22:26:13 +08:00
|
|
|
#include "app/i18n/strings.h"
|
2018-07-07 13:47:42 +08:00
|
|
|
#include "app/site.h"
|
2018-03-16 07:34:01 +08:00
|
|
|
#include "app/ui/layer_frame_comboboxes.h"
|
2018-03-16 19:59:34 +08:00
|
|
|
#include "app/ui_context.h"
|
2018-03-16 07:34:01 +08:00
|
|
|
#include "base/convert_to.h"
|
|
|
|
|
#include "base/fs.h"
|
2018-06-27 21:00:19 +08:00
|
|
|
#include "base/string.h"
|
2018-03-16 19:59:34 +08:00
|
|
|
#include "doc/selected_frames.h"
|
2019-10-02 01:55:08 +08:00
|
|
|
#include "doc/tag.h"
|
2018-03-16 22:26:13 +08:00
|
|
|
#include "fmt/format.h"
|
2018-06-27 21:00:19 +08:00
|
|
|
#include "ui/alert.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
2018-03-16 07:34:01 +08:00
|
|
|
|
|
|
|
|
namespace app {
|
|
|
|
|
|
2018-07-07 22:54:44 +08:00
|
|
|
ExportFileWindow::ExportFileWindow(const Doc* doc)
|
2018-03-16 07:34:01 +08:00
|
|
|
: m_doc(doc)
|
|
|
|
|
, m_docPref(Preferences::instance().document(doc))
|
2018-03-16 22:26:13 +08:00
|
|
|
, m_preferredResize(1)
|
2018-03-16 07:34:01 +08:00
|
|
|
{
|
|
|
|
|
// Is a default output filename in the preferences?
|
|
|
|
|
if (!m_docPref.saveCopy.filename().empty()) {
|
2018-03-16 21:08:52 +08:00
|
|
|
setOutputFilename(m_docPref.saveCopy.filename());
|
2018-03-16 07:34:01 +08:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
std::string newFn = base::replace_extension(
|
|
|
|
|
doc->filename(),
|
2018-06-27 21:00:19 +08:00
|
|
|
defaultExtension());
|
2018-03-16 07:34:01 +08:00
|
|
|
if (newFn == doc->filename()) {
|
|
|
|
|
newFn = base::join_path(
|
|
|
|
|
base::get_file_path(newFn),
|
|
|
|
|
base::get_file_title(newFn) + "-export." + base::get_file_extension(newFn));
|
|
|
|
|
}
|
2018-03-16 21:08:52 +08:00
|
|
|
setOutputFilename(newFn);
|
2018-03-16 07:34:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Default export configuration
|
2022-06-15 23:25:31 +08:00
|
|
|
setResizeScale(m_docPref.saveCopy.resizeScale());
|
2022-08-12 22:56:52 +08:00
|
|
|
fill_area_combobox(m_doc->sprite(), area(), m_docPref.saveCopy.area());
|
2022-09-06 04:19:25 +08:00
|
|
|
fill_layers_combobox(m_doc->sprite(), layers(), m_docPref.saveCopy.layer(), m_docPref.saveCopy.layerIndex());
|
2018-03-16 07:34:01 +08:00
|
|
|
fill_frames_combobox(m_doc->sprite(), frames(), m_docPref.saveCopy.frameTag());
|
2018-03-16 19:59:34 +08:00
|
|
|
fill_anidir_combobox(anidir(), m_docPref.saveCopy.aniDir());
|
2024-03-13 02:13:35 +08:00
|
|
|
|
|
|
|
|
if (doc->sprite()->hasPixelRatio()) {
|
|
|
|
|
pixelRatio()->setSelected(m_docPref.saveCopy.applyPixelRatio());
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// Hide "Apply pixel ratio" checkbox when there is no pixel aspect
|
|
|
|
|
// ratio to apply.
|
|
|
|
|
pixelRatio()->setSelected(false);
|
|
|
|
|
pixelRatio()->setVisible(false);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-16 22:03:50 +08:00
|
|
|
forTwitter()->setSelected(m_docPref.saveCopy.forTwitter());
|
2018-03-16 22:26:13 +08:00
|
|
|
adjustResize()->setVisible(false);
|
2023-12-02 02:40:20 +08:00
|
|
|
playSubtags()->setSelected(m_docPref.saveCopy.playSubtags());
|
2019-04-02 01:31:27 +08:00
|
|
|
// Here we don't call updateAniDir() because it's already filled and
|
|
|
|
|
// set by the function fill_anidir_combobox(). So if the user
|
|
|
|
|
// exported a tag with a specific AniDir, we want to keep the option
|
|
|
|
|
// in the preference (instead of the tag's AniDir).
|
|
|
|
|
//updateAniDir();
|
2023-12-02 02:40:20 +08:00
|
|
|
updatePlaySubtags();
|
2019-04-02 01:31:27 +08:00
|
|
|
|
2018-03-16 22:26:13 +08:00
|
|
|
updateAdjustResizeButton();
|
2018-03-16 19:59:34 +08:00
|
|
|
|
2018-03-16 21:08:52 +08:00
|
|
|
outputFilename()->Change.connect(
|
2020-07-04 08:51:46 +08:00
|
|
|
[this]{
|
|
|
|
|
m_outputFilename = outputFilename()->text();
|
|
|
|
|
onOutputFilenameEntryChange();
|
|
|
|
|
});
|
2018-03-16 21:08:52 +08:00
|
|
|
outputFilenameBrowse()->Click.connect(
|
2020-07-04 08:51:46 +08:00
|
|
|
[this]{
|
|
|
|
|
std::string fn = SelectOutputFile();
|
|
|
|
|
if (!fn.empty()) {
|
|
|
|
|
setOutputFilename(fn);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
resize()->Change.connect([this]{ updateAdjustResizeButton(); });
|
2023-12-02 02:40:20 +08:00
|
|
|
frames()->Change.connect([this]{
|
|
|
|
|
updateAniDir();
|
|
|
|
|
updatePlaySubtags();
|
|
|
|
|
});
|
2020-07-04 08:51:46 +08:00
|
|
|
forTwitter()->Click.connect([this]{ updateAdjustResizeButton(); });
|
|
|
|
|
adjustResize()->Click.connect([this]{ onAdjustResize(); });
|
|
|
|
|
ok()->Click.connect([this]{ onOK(); });
|
2018-03-16 07:34:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ExportFileWindow::show()
|
|
|
|
|
{
|
|
|
|
|
openWindowInForeground();
|
|
|
|
|
return (closer() == ok());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExportFileWindow::savePref()
|
|
|
|
|
{
|
|
|
|
|
m_docPref.saveCopy.filename(outputFilenameValue());
|
|
|
|
|
m_docPref.saveCopy.resizeScale(resizeValue());
|
2022-08-12 22:56:52 +08:00
|
|
|
m_docPref.saveCopy.area(areaValue());
|
2018-03-16 07:34:01 +08:00
|
|
|
m_docPref.saveCopy.layer(layersValue());
|
2022-09-06 04:19:25 +08:00
|
|
|
m_docPref.saveCopy.layerIndex(layersIndex());
|
2019-03-26 23:20:54 +08:00
|
|
|
m_docPref.saveCopy.aniDir(aniDirValue());
|
2018-03-16 07:34:01 +08:00
|
|
|
m_docPref.saveCopy.frameTag(framesValue());
|
|
|
|
|
m_docPref.saveCopy.applyPixelRatio(applyPixelRatio());
|
2018-03-16 22:03:50 +08:00
|
|
|
m_docPref.saveCopy.forTwitter(isForTwitter());
|
2023-12-02 02:40:20 +08:00
|
|
|
m_docPref.saveCopy.playSubtags(isPlaySubtags());
|
2018-03-16 07:34:01 +08:00
|
|
|
}
|
|
|
|
|
|
2018-03-16 21:08:52 +08:00
|
|
|
std::string ExportFileWindow::outputFilenameValue() const
|
|
|
|
|
{
|
|
|
|
|
return base::join_path(m_outputPath,
|
|
|
|
|
m_outputFilename);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-16 07:34:01 +08:00
|
|
|
double ExportFileWindow::resizeValue() const
|
|
|
|
|
{
|
2022-06-15 23:25:31 +08:00
|
|
|
double value = resize()->getEntryWidget()->textDouble() / 100.0;
|
|
|
|
|
return std::clamp(value, 0.001, 100000000.0);
|
2018-03-16 07:34:01 +08:00
|
|
|
}
|
|
|
|
|
|
2022-08-12 22:56:52 +08:00
|
|
|
std::string ExportFileWindow::areaValue() const
|
|
|
|
|
{
|
|
|
|
|
return area()->getValue();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-16 07:34:01 +08:00
|
|
|
std::string ExportFileWindow::layersValue() const
|
|
|
|
|
{
|
|
|
|
|
return layers()->getValue();
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 01:45:42 +08:00
|
|
|
int ExportFileWindow::layersIndex() const
|
|
|
|
|
{
|
2022-08-31 04:33:52 +08:00
|
|
|
int i = layers()->getSelectedItemIndex() - kLayersComboboxExtraInitialItems;
|
2022-08-31 01:45:42 +08:00
|
|
|
return i < 0 ? -1 : i;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-16 07:34:01 +08:00
|
|
|
std::string ExportFileWindow::framesValue() const
|
|
|
|
|
{
|
|
|
|
|
return frames()->getValue();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-16 19:59:34 +08:00
|
|
|
doc::AniDir ExportFileWindow::aniDirValue() const
|
|
|
|
|
{
|
|
|
|
|
return (doc::AniDir)anidir()->getSelectedItemIndex();
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 02:40:20 +08:00
|
|
|
bool ExportFileWindow::isPlaySubtags() const
|
|
|
|
|
{
|
|
|
|
|
return playSubtags()->isSelected() && framesValue() != kSelectedFrames;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-16 07:34:01 +08:00
|
|
|
bool ExportFileWindow::applyPixelRatio() const
|
|
|
|
|
{
|
|
|
|
|
return pixelRatio()->isSelected();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-16 22:03:50 +08:00
|
|
|
bool ExportFileWindow::isForTwitter() const
|
|
|
|
|
{
|
|
|
|
|
return forTwitter()->isSelected();
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 23:25:31 +08:00
|
|
|
void ExportFileWindow::setResizeScale(double scale)
|
2022-06-15 10:19:39 +08:00
|
|
|
{
|
2022-06-15 23:25:31 +08:00
|
|
|
resize()->setValue(fmt::format("{:.2f}", 100.0 * scale));
|
2022-06-15 10:19:39 +08:00
|
|
|
}
|
|
|
|
|
|
2022-08-13 07:11:25 +08:00
|
|
|
void ExportFileWindow::setArea(const std::string& areaValue)
|
|
|
|
|
{
|
|
|
|
|
area()->setValue(areaValue);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 04:04:12 +08:00
|
|
|
void ExportFileWindow::setAniDir(const doc::AniDir aniDir)
|
|
|
|
|
{
|
|
|
|
|
anidir()->setSelectedItemIndex(int(aniDir));
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-16 21:08:52 +08:00
|
|
|
void ExportFileWindow::setOutputFilename(const std::string& pathAndFilename)
|
|
|
|
|
{
|
2024-06-04 04:23:36 +08:00
|
|
|
if (base::get_file_path(m_doc->filename()).empty()) {
|
|
|
|
|
m_outputPath = base::get_file_path(pathAndFilename);
|
|
|
|
|
m_outputFilename = base::get_file_name(pathAndFilename);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
m_outputPath = base::get_file_path(m_doc->filename());
|
|
|
|
|
m_outputFilename = base::get_relative_path(pathAndFilename, base::get_file_path(m_doc->filename()));
|
2024-08-22 00:50:29 +08:00
|
|
|
|
|
|
|
|
// Cannot find a relative path (e.g. we selected other drive)
|
|
|
|
|
if (m_outputFilename == pathAndFilename) {
|
|
|
|
|
m_outputPath = base::get_file_path(pathAndFilename);
|
|
|
|
|
m_outputFilename = base::get_file_name(pathAndFilename);
|
|
|
|
|
}
|
2024-06-04 04:23:36 +08:00
|
|
|
}
|
2018-03-16 21:08:52 +08:00
|
|
|
|
|
|
|
|
updateOutputFilenameEntry();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExportFileWindow::updateOutputFilenameEntry()
|
|
|
|
|
{
|
|
|
|
|
outputFilename()->setText(m_outputFilename);
|
|
|
|
|
onOutputFilenameEntryChange();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExportFileWindow::onOutputFilenameEntryChange()
|
2018-03-16 09:42:53 +08:00
|
|
|
{
|
2018-03-16 20:37:21 +08:00
|
|
|
ok()->setEnabled(!m_outputFilename.empty());
|
2018-03-16 09:42:53 +08:00
|
|
|
}
|
|
|
|
|
|
2018-03-16 19:59:34 +08:00
|
|
|
void ExportFileWindow::updateAniDir()
|
|
|
|
|
{
|
|
|
|
|
std::string framesValue = this->framesValue();
|
|
|
|
|
if (!framesValue.empty() &&
|
|
|
|
|
framesValue != kAllFrames &&
|
|
|
|
|
framesValue != kSelectedFrames) {
|
|
|
|
|
SelectedFrames selFrames;
|
2019-10-02 01:55:08 +08:00
|
|
|
Tag* tag = calculate_selected_frames(
|
2018-03-16 19:59:34 +08:00
|
|
|
UIContext::instance()->activeSite(), framesValue, selFrames);
|
2019-10-02 01:55:08 +08:00
|
|
|
if (tag)
|
|
|
|
|
anidir()->setSelectedItemIndex(int(tag->aniDir()));
|
2018-03-16 19:59:34 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
anidir()->setSelectedItemIndex(int(doc::AniDir::FORWARD));
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 02:40:20 +08:00
|
|
|
void ExportFileWindow::updatePlaySubtags()
|
|
|
|
|
{
|
|
|
|
|
std::string framesValue = this->framesValue();
|
2024-03-13 02:54:05 +08:00
|
|
|
playSubtags()->setVisible(framesValue != kSelectedFrames &&
|
|
|
|
|
// We hide the option if there is no tag
|
|
|
|
|
!m_doc->sprite()->tags().empty());
|
2023-12-02 02:40:20 +08:00
|
|
|
layout();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-16 22:26:13 +08:00
|
|
|
void ExportFileWindow::updateAdjustResizeButton()
|
|
|
|
|
{
|
|
|
|
|
// Calculate a better size for Twitter
|
|
|
|
|
m_preferredResize = 1;
|
|
|
|
|
while (m_preferredResize < 10 &&
|
|
|
|
|
(m_doc->width()*m_preferredResize < 240 ||
|
|
|
|
|
m_doc->height()*m_preferredResize < 240)) {
|
|
|
|
|
++m_preferredResize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bool newState =
|
|
|
|
|
forTwitter()->isSelected() &&
|
|
|
|
|
((int)resizeValue() < m_preferredResize);
|
|
|
|
|
|
|
|
|
|
if (adjustResize()->isVisible() != newState) {
|
|
|
|
|
adjustResize()->setVisible(newState);
|
2024-06-21 07:14:29 +08:00
|
|
|
if (newState) {
|
|
|
|
|
adjustResize()->setText(
|
|
|
|
|
Strings::export_file_adjust_resize(100 * m_preferredResize));
|
|
|
|
|
}
|
2018-03-16 22:26:13 +08:00
|
|
|
adjustResize()->parent()->layout();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExportFileWindow::onAdjustResize()
|
|
|
|
|
{
|
2022-08-08 21:29:36 +08:00
|
|
|
resize()->setValue(fmt::format("{:.2f}", 100.0 * m_preferredResize));
|
2018-03-16 22:26:13 +08:00
|
|
|
|
|
|
|
|
adjustResize()->setVisible(false);
|
|
|
|
|
adjustResize()->parent()->layout();
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-27 21:00:19 +08:00
|
|
|
void ExportFileWindow::onOK()
|
|
|
|
|
{
|
|
|
|
|
base::paths exts = get_writable_extensions();
|
|
|
|
|
std::string ext = base::string_to_lower(
|
|
|
|
|
base::get_file_extension(m_outputFilename));
|
|
|
|
|
|
|
|
|
|
// Add default extension to output filename
|
|
|
|
|
if (std::find(exts.begin(), exts.end(), ext) == exts.end()) {
|
|
|
|
|
if (ext.empty()) {
|
|
|
|
|
m_outputFilename =
|
|
|
|
|
base::replace_extension(m_outputFilename,
|
|
|
|
|
defaultExtension());
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
ui::Alert::show(
|
2024-06-21 07:14:29 +08:00
|
|
|
Strings::alerts_unknown_output_file_format_error(ext));
|
2018-06-27 21:00:19 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closeWindow(ok());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string ExportFileWindow::defaultExtension() const
|
|
|
|
|
{
|
|
|
|
|
auto& pref = Preferences::instance();
|
|
|
|
|
if (m_doc->sprite()->totalFrames() > 1)
|
|
|
|
|
return pref.exportFile.animationDefaultExtension();
|
|
|
|
|
else
|
|
|
|
|
return pref.exportFile.imageDefaultExtension();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-16 07:34:01 +08:00
|
|
|
} // namespace app
|