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 Jul 27, 2022
2 parents ca462c2 + e65e558 commit 11b5a80
Show file tree
Hide file tree
Showing 23 changed files with 858 additions and 26 deletions.
5 changes: 5 additions & 0 deletions src/ass_dialogue.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
// POSSIBILITY OF SUCH DAMAGE.
//
// Aegisub Project http://www.aegisub.org/
#pragma once

#include "ass_entry.h"
#include "ass_override.h"
#include "fold_controller.h"

#include <libaegisub/ass/time.h>

Expand Down Expand Up @@ -124,6 +126,9 @@ struct AssDialogueBase {

int Row = -1;

/// Data describing line folds starting or ending at this line
FoldInfo Fold;

/// Is this a comment line?
bool Comment = false;
/// Layer number
Expand Down
2 changes: 2 additions & 0 deletions src/ass_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ int AssFile::Commit(wxString const& desc, int type, int amend_id, AssDialogue *s
event.Row = i++;
}

AnnouncePreCommit(type, single_line);

PushState({desc, &amend_id, single_line});

AnnounceCommit(type, single_line);
Expand Down
14 changes: 14 additions & 0 deletions src/ass_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
//
// Aegisub Project http://www.aegisub.org/

#pragma once

#include "ass_entry.h"

#include <libaegisub/fs_fwd.h>
Expand All @@ -52,6 +54,13 @@ struct ExtradataEntry {
std::string value;
};

// Both start and end are inclusive
struct LineFold {
int start;
int end;
bool collapsed;
};

struct AssFileCommit {
wxString const& message;
int *commit_id;
Expand All @@ -76,11 +85,13 @@ 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;
public:
/// The lines in the file
Expand Down Expand Up @@ -166,8 +177,11 @@ class AssFile {
COMMIT_DIAG_FULL = COMMIT_DIAG_META | COMMIT_DIAG_TIME | COMMIT_DIAG_TEXT,
/// Extradata entries were added/modified/removed
COMMIT_EXTRADATA = 0x100,
/// Folds were added or removed
COMMIT_FOLD = 0x200,
};

DEFINE_SIGNAL_ADDERS(AnnouncePreCommit, AddPreCommitListener)
DEFINE_SIGNAL_ADDERS(AnnounceCommit, AddCommitListener)
DEFINE_SIGNAL_ADDERS(PushState, AddUndoManager)

Expand Down
28 changes: 27 additions & 1 deletion src/ass_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

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

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

Expand All @@ -58,6 +60,7 @@ 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 @@ -80,6 +83,29 @@ 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
16 changes: 16 additions & 0 deletions src/auto4_lua_assfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#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 @@ -100,6 +101,16 @@ namespace {
return ret;
}

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

using namespace Automation4;
template<int (LuaAssFile::*closure)(lua_State *)>
int closure_wrapper(lua_State *L)
Expand Down Expand Up @@ -181,6 +192,10 @@ 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 @@ -301,6 +316,7 @@ 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");
get_userdata_field(L, "_foldinfo", "dialogue", &dia->Fold);

std::vector<uint32_t> new_ids;

Expand Down
Loading

0 comments on commit 11b5a80

Please sign in to comment.