mirror of
https://github.com/aseprite/aseprite.git
synced 2025-03-17 13:20:45 +00:00
Add {tagframe} to filename format (fix #894)
This commit is contained in:
parent
172e03b5e3
commit
6d3623ec26
@ -435,7 +435,9 @@ FileOp* FileOp::createSaveDocumentOperation(const Context* context,
|
||||
.filename(fn)
|
||||
.innerTagName(innerTag ? innerTag->name(): "")
|
||||
.outerTagName(outerTag ? outerTag->name(): "")
|
||||
.frame(start_from+frame);
|
||||
.frame(start_from+frame)
|
||||
.tagFrame(innerTag ? frame-innerTag->fromFrame():
|
||||
start_from+frame);
|
||||
|
||||
std::string frame_fn =
|
||||
filename_formatter(fn_format, fnInfo);
|
||||
|
@ -20,6 +20,33 @@
|
||||
|
||||
namespace app {
|
||||
|
||||
static bool replace_frame(const char* frameKey, // E.g. = "{frame"
|
||||
int frameBase,
|
||||
std::string& str)
|
||||
{
|
||||
size_t i = str.find(frameKey);
|
||||
if (i != std::string::npos) {
|
||||
int keyLen = std::strlen(frameKey);
|
||||
|
||||
size_t j = str.find("}", i+keyLen);
|
||||
if (j != std::string::npos) {
|
||||
std::string from = str.substr(i, j - i + 1);
|
||||
if (frameBase >= 0) {
|
||||
std::vector<char> to(32);
|
||||
int offset = std::strtol(from.c_str()+keyLen, NULL, 10);
|
||||
|
||||
std::sprintf(&to[0], "%0*d", (int(j)-int(i+keyLen)), frameBase + offset);
|
||||
base::replace_string(str, from, &to[0]);
|
||||
}
|
||||
else
|
||||
base::replace_string(str, from, "");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string filename_formatter(
|
||||
const std::string& format,
|
||||
FilenameInfo& info,
|
||||
@ -42,22 +69,8 @@ std::string filename_formatter(
|
||||
base::replace_string(output, "{outertag}", info.outerTagName());
|
||||
|
||||
if (replaceFrame) {
|
||||
size_t i = output.find("{frame");
|
||||
if (i != std::string::npos) {
|
||||
size_t j = output.find("}", i+6);
|
||||
if (j != std::string::npos) {
|
||||
std::string from = output.substr(i, j - i + 1);
|
||||
if (info.frame() >= 0) {
|
||||
std::vector<char> to(32);
|
||||
int offset = std::strtol(from.c_str()+6, NULL, 10);
|
||||
|
||||
std::sprintf(&to[0], "%0*d", (int(j)-int(i+6)), info.frame() + offset);
|
||||
base::replace_string(output, from, &to[0]);
|
||||
}
|
||||
else
|
||||
base::replace_string(output, from, "");
|
||||
}
|
||||
}
|
||||
replace_frame("{frame", info.frame(), output);
|
||||
replace_frame("{tagframe", info.tagFrame(), output);
|
||||
}
|
||||
|
||||
return output;
|
||||
@ -87,7 +100,9 @@ std::string add_frame_format(
|
||||
std::string output = format;
|
||||
|
||||
size_t i = output.find("{frame");
|
||||
if (i == std::string::npos) {
|
||||
size_t j = output.find("{tagframe");
|
||||
if (i == std::string::npos &&
|
||||
j == std::string::npos) {
|
||||
output =
|
||||
base::join_path(
|
||||
base::get_file_path(format),
|
||||
|
@ -15,13 +15,14 @@ namespace app {
|
||||
|
||||
class FilenameInfo {
|
||||
public:
|
||||
FilenameInfo() : m_frame(-1) { }
|
||||
FilenameInfo() : m_frame(-1), m_tagFrame(-1) { }
|
||||
|
||||
const std::string& filename() const { return m_filename; }
|
||||
const std::string& layerName() const { return m_layerName; }
|
||||
const std::string& innerTagName() const { return m_innerTagName; }
|
||||
const std::string& outerTagName() const { return m_outerTagName; }
|
||||
int frame() const { return m_frame; }
|
||||
int tagFrame() const { return m_tagFrame; }
|
||||
|
||||
FilenameInfo& filename(const std::string& value) {
|
||||
m_filename = value;
|
||||
@ -48,12 +49,18 @@ namespace app {
|
||||
return *this;
|
||||
}
|
||||
|
||||
FilenameInfo& tagFrame(int value) {
|
||||
m_tagFrame = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_filename;
|
||||
std::string m_layerName;
|
||||
std::string m_innerTagName;
|
||||
std::string m_outerTagName;
|
||||
int m_frame;
|
||||
int m_tagFrame;
|
||||
};
|
||||
|
||||
std::string filename_formatter(
|
||||
|
@ -165,3 +165,24 @@ TEST(AddFrameFormat, Tests)
|
||||
"{path}/{title}{frame1}.{extension}",
|
||||
"{frame001}"));
|
||||
}
|
||||
|
||||
TEST(FilenameFormatter, WithTagFrame)
|
||||
{
|
||||
EXPECT_EQ(
|
||||
"./file_2_0.png",
|
||||
filename_formatter(
|
||||
"{path}/{title}_{frame}_{tagframe}.{extension}",
|
||||
FilenameInfo().filename("./file.png").frame(2).tagFrame(0)));
|
||||
|
||||
EXPECT_EQ(
|
||||
"./file_2_1.png",
|
||||
filename_formatter(
|
||||
"{path}/{title}_{frame}_{tagframe1}.{extension}",
|
||||
FilenameInfo().filename("./file.png").frame(2).tagFrame(0)));
|
||||
|
||||
EXPECT_EQ(
|
||||
"./file_2_25.png",
|
||||
filename_formatter(
|
||||
"{path}/{title}_{frame}_{tagframe24}.{extension}",
|
||||
FilenameInfo().filename("./file.png").frame(2).tagFrame(1)));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user