Skip to content

Commit

Permalink
Merge branch 'folding' into feature
Browse files Browse the repository at this point in the history
  • Loading branch information
arch1t3cht committed Dec 11, 2022
2 parents 8390c77 + ddfcbde commit ac7635e
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 162 deletions.
33 changes: 33 additions & 0 deletions src/ass_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,39 @@ uint32_t AssFile::AddExtradata(std::string const& key, std::string const& value)
return next_extradata_id++; // return old value, then post-increment
}

void AssFile::SetExtradataValue(AssDialogue& line, std::string const& key, std::string const& value, bool del) {
std::vector<uint32_t> id_list = line.ExtradataIds;
std::vector<bool> to_erase(id_list.size());
bool dirty = false;
bool found = false;

std::vector<ExtradataEntry> entry_list = GetExtradata(id_list);
for (int i = entry_list.size() - 1; i >= 0; i--) {
if (entry_list[i].key == key) {
if (!del && entry_list[i].value == value) {
found = true;
} else {
to_erase[i] = true;
dirty = true;
}
}
}

// The key is already set, we don't need to change anything
if (found && !dirty)
return;

for (int i = id_list.size() - 1; i >= 0; i--) {
if (to_erase[i])
id_list.erase(id_list.begin() + i, id_list.begin() + i + 1);
}

if (!del && !found)
id_list.push_back(AddExtradata(key, value));

line.ExtradataIds = id_list;
}

namespace {
struct extradata_id_cmp {
bool operator()(ExtradataEntry const& e, uint32_t id) {
Expand Down
9 changes: 7 additions & 2 deletions src/ass_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@ struct ProjectProperties {
int active_row = 0;
int ar_mode = 0;
int video_position = 0;
std::vector<LineFold> folds;
};

class AssFile {
/// A set of changes has been committed to the file (AssFile::COMMITType)
agi::signal::Signal<int, const AssDialogue*> AnnounceCommit;
agi::signal::Signal<int, const AssDialogue*> AnnouncePreCommit;
agi::signal::Signal<AssFileCommit> PushState;

void SetExtradataValue(AssDialogue& line, std::string const& key, std::string const& value, bool del);
public:
/// The lines in the file
std::vector<AssInfo> Info;
Expand Down Expand Up @@ -144,6 +145,10 @@ class AssFile {
uint32_t AddExtradata(std::string const& key, std::string const& value);
/// Fetch all extradata entries from a list of IDs
std::vector<ExtradataEntry> GetExtradata(std::vector<uint32_t> const& id_list) const;
/// Set an extradata kex:value pair for a dialogue line, clearing previous values for this key if necessary
void SetExtradataValue(AssDialogue& line, std::string const& key, std::string const& value) { SetExtradataValue(line, key, value, false); };
/// Delete any extradata values for the given key
void DeleteExtradataValue(AssDialogue& line, std::string const& key) { SetExtradataValue(line, key, "", true); };
/// Remove unreferenced extradata entries
void CleanExtradata();

Expand Down Expand Up @@ -178,7 +183,7 @@ class AssFile {
/// Extradata entries were added/modified/removed
COMMIT_EXTRADATA = 0x100,
/// Folds were added or removed
COMMIT_FOLD = 0x200,
COMMIT_FOLD = COMMIT_EXTRADATA,
};

DEFINE_SIGNAL_ADDERS(AnnouncePreCommit, AddPreCommitListener)
Expand Down
28 changes: 1 addition & 27 deletions src/ass_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

#include <libaegisub/ass/uuencode.h>
#include <libaegisub/make_unique.h>
#include <libaegisub/split.h>
#include <libaegisub/util.h>

#include <algorithm>
Expand All @@ -40,8 +39,7 @@ class AssParser::HeaderToProperty {
using field = boost::variant<
std::string ProjectProperties::*,
int ProjectProperties::*,
double ProjectProperties::*,
std::vector<LineFold> ProjectProperties::*
double ProjectProperties::*
>;
std::unordered_map<std::string, field> fields;

Expand All @@ -60,7 +58,6 @@ class AssParser::HeaderToProperty {
{"Video Zoom Percent", &ProjectProperties::video_zoom},
{"Scroll Position", &ProjectProperties::scroll_position},
{"Active Line", &ProjectProperties::active_row},
{"Line Folds", &ProjectProperties::folds},
{"Video Position", &ProjectProperties::video_position},
{"Video AR Mode", &ProjectProperties::ar_mode},
{"Video AR Value", &ProjectProperties::ar_value},
Expand All @@ -83,29 +80,6 @@ class AssParser::HeaderToProperty {
void operator()(std::string ProjectProperties::*f) const { obj.*f = value; }
void operator()(int ProjectProperties::*f) const { try_parse(value, &(obj.*f)); }
void operator()(double ProjectProperties::*f) const { try_parse(value, &(obj.*f)); }
void operator()(std::vector<LineFold> ProjectProperties::*f) const {
std::vector<LineFold> folds;

for (auto foldstr : agi::Split(value, ',')) {
LineFold fold;
std::vector<std::string> parsed;
agi::Split(parsed, foldstr, ':');
if (parsed.size() != 3) {
continue;
}

int collapsed;
try_parse(parsed[0], &fold.start);
try_parse(parsed[1], &fold.end);
try_parse(parsed[2], &collapsed);
fold.collapsed = !!collapsed;

if (fold.start >= 0 && fold.end > fold.start) {
folds.push_back(fold);
}
}
obj.*f = folds;
}
} visitor {target->Properties, value};
boost::apply_visitor(visitor, it->second);
return true;
Expand Down
24 changes: 0 additions & 24 deletions src/auto4_lua_assfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#include "ass_karaoke.h"
#include "ass_style.h"
#include "compat.h"
#include "fold_controller.h"

#include <libaegisub/exception.h>
#include <libaegisub/log.h>
Expand Down Expand Up @@ -101,22 +100,6 @@ namespace {
return ret;
}

template<typename T>
bool get_userdata_field(lua_State *L, const char *name, const char *line_class, T *target, bool required)
{
lua_getfield(L, -1, name);
if (!lua_isuserdata(L, -1)) {
if (!required) {
lua_pop(L, 1);
return false;
}
throw bad_field("userdata", name, line_class);
}
*target = *static_cast<T *>(lua_touserdata(L, -1));
lua_pop(L, 1);
return true;
}

using namespace Automation4;
template<int (LuaAssFile::*closure)(lua_State *)>
int closure_wrapper(lua_State *L)
Expand Down Expand Up @@ -198,10 +181,6 @@ namespace Automation4 {

set_field(L, "text", dia->Text);

// preserve the folds
*static_cast<FoldInfo*>(lua_newuserdata(L, sizeof(FoldInfo))) = dia->Fold;
lua_setfield(L, -2, "_foldinfo");

// create extradata table
lua_newtable(L);
for (auto const& ed : ass->GetExtradata(dia->ExtradataIds)) {
Expand Down Expand Up @@ -322,9 +301,6 @@ namespace Automation4 {
dia->Margin[2] = get_int_field(L, "margin_t", "dialogue");
dia->Effect = get_string_field(L, "effect", "dialogue");
dia->Text = get_string_field(L, "text", "dialogue");
if (!get_userdata_field(L, "_foldinfo", "dialogue", &dia->Fold, false)) {
dia->Fold = FoldInfo();
}

std::vector<uint32_t> new_ids;

Expand Down
Loading

0 comments on commit ac7635e

Please sign in to comment.