forked from TypesettingTools/Myaamori-Aegisub-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myaa.Bounce.moon
225 lines (197 loc) · 8.35 KB
/
myaa.Bounce.moon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
export script_name = "Bounce"
export script_description = "Make text bounce"
export script_version = "0.0.2"
export script_author = "Myaamori"
export script_namespace = "myaa.Bounce"
DependencyControl = require 'l0.DependencyControl'
depctrl = DependencyControl {
{
{"l0.Functional", version: "0.6.0", url: "https://github.com/TypesettingTools/Functional",
feed: "https://raw.githubusercontent.com/TypesettingTools/Functional/master/DependencyControl.json"}
{"a-mo.LineCollection", version: "1.3.0", url: "https://github.com/TypesettingTools/Aegisub-Motion",
feed: "https://raw.githubusercontent.com/TypesettingTools/Aegisub-Motion/DepCtrl/DependencyControl.json"},
{"a-mo.Line", version: "1.5.3", url: "https://github.com/TypesettingTools/Aegisub-Motion",
feed: "https://raw.githubusercontent.com/TypesettingTools/Aegisub-Motion/DepCtrl/DependencyControl.json"},
{"l0.ASSFoundation", version: "0.5.0", url: "https://github.com/TypesettingTools/ASSFoundation",
feed: "https://raw.githubusercontent.com/TypesettingTools/ASSFoundation/master/DependencyControl.json"},
{"a-mo.ConfigHandler", version: "1.1.4", url: "https://github.com/TypesettingTools/Aegisub-Motion",
feed: "https://raw.githubusercontent.com/TypesettingTools/Aegisub-Motion/DepCtrl/DependencyControl.json"},
}
}
F, LineCollection, Line, ASS, ConfigHandler = depctrl\requireModules!
findall = (s, c) ->
indices = {}
for i = 1, #s
if s\sub(i, i) == c
table.insert indices, i
return indices
process = (sub, sel, result) ->
lines = LineCollection sub, sel
lines_to_delete = {}
distance = result.characterDistance
column_distance = result.columnDistance
offset_size = if result.spaceEven then 0 else 1
delay = result.delay
anim_size = result.maximumOffset
anim_stop = result.finalOffset
anim_duration = result.duration
vertical = result.layout == "Vertical"
direction = if result.invertDirection then -1 else 1
split_char = result.split
hide_char = result.hideCharacters
lines_added = 0
lines\runCallback ((lines, line, i) ->
ass = ASS\parse line
line\getPropertiesFromStyle!
str = ass\copy!\stripTags!\getString!
for i, wordline in ipairs ass\splitAtIndexes findall(str, split_char), 4, false
wordass = wordline.ASS
if i > 1
wordass.sections[2].value = wordass.sections[2].value\sub(2)
lwidth, lheight = 0, 0
for j, charline in ipairs wordass\splitAtIntervals 1, 4, false
charass = charline.ASS
pos = charass\getPosition!
if vertical
pos\add column_distance * (i - 1), distance * (j - 1) + lheight * offset_size
else
pos\add distance * (j - 1) + lwidth * offset_size, column_distance * (i - 1)
charass\replaceTags {pos}
charass\commit!
cwidth, cheight = charass\getTextExtents!
lwidth += cwidth
lheight += cheight
start_frame = aegisub.frame_from_ms charline.start_time
end_frame = aegisub.frame_from_ms charline.end_time
duration = end_frame - start_frame
charline\tokenizeTransforms!
for frame = start_frame, end_frame - 1
n = frame - start_frame
p = n / duration * math.pi
cur_time = aegisub.ms_from_frame(frame) - charline.start_time
copy = Line charline, lines
copy.start_time = aegisub.ms_from_frame frame
copy.end_time = aegisub.ms_from_frame (frame + 1)
copy\interpolateTransforms cur_time
cur_shift = math.max(0, math.min((2 * anim_size - anim_stop) / (2 * anim_size),
(cur_time - delay * (j - 1)) / anim_duration))
if not hide_char or cur_shift > 0
copyass = ASS\parse copy
pos = copyass\getPosition!
shift = direction * math.sin(cur_shift * math.pi) * anim_size
if vertical
pos\add shift, 0
else
pos\add 0, shift
copyass\replaceTags {pos}
copyass\commit!
lines_added += 1
lines\addLine copy, nil, true, line.number + lines_added
line.comment = true
), true
lines\replaceLines!
lines\insertLines!
return lines\getSelection!
dialog = {
main: {
layoutLabel: {
class: "label", label: "Text layout: ",
x: 0, y: 0, width: 1, height: 1
},
layout: {
class: "dropdown",
value: "Vertical", config: true, items: {"Vertical", "Horizontal"},
x: 1, y: 0, width: 1, height: 1, hint: "Whether to lay out the text vertically or horizontally"
},
invertDirection: {
class: "checkbox", label: "Invert animation direction",
value: false, config: true,
x: 0, y: 1, width: 2, height: 1, hint: "Inverts the direction of the animation"
},
hideCharacters: {
class: "checkbox", label: "Hide characters until animation starts",
value: false, config: true,
x: 0, y: 2, width: 2, height: 1, hint: "Hides each character until the animation for that character starts"
},
spaceEven: {
class: "checkbox", label: "Space out characters evenly",
value: true, config: true,
x: 0, y: 3, width: 2, height: 1, hint: "Spaces out characters evenly, ignoring their width/height; if false, characters are additionally offset by their height (vertical layout) or their width (horizontal layout)"
},
characterDistanceLabel: {
class: "label", label: "Character distance:",
x: 0, y: 4, width: 1, height: 1
},
characterDistance: {
class: "intedit",
value: 40, config: true,
x: 1, y: 4, width: 1, height: 1, hint: "Distance between characters on the same row/column (pixels)"
},
columnDistanceLabel: {
class: "label", label: "Column/row distance:",
x: 0, y: 5, width: 1, height: 1
},
columnDistance: {
class: "intedit",
value: 100, config: true,
x: 1, y: 5, width: 1, height: 1, hint: "Distance between rows/columns of characters (pixels)"
},
maximumOffsetLabel: {
class: "label", label: "Maximum offset:",
x: 0, y: 6, width: 1, height: 1
},
maximumOffset: {
class: "intedit",
value: 100, config: true, min: 0,
x: 1, y: 6, width: 1, height: 1, hint: "The maximum offset (peak) of the animation (pixels)"
},
finalOffsetLabel: {
class: "label", label: "Final offset:",
x: 0, y: 7, width: 1, height: 1
},
finalOffset: {
class: "intedit",
value: 30, config: true, min: 0,
x: 1, y: 7, width: 1, height: 1, hint: "The final offset at the end of the animation (pixels)"
},
durationLabel: {
class: "label", label: "Animation duration:",
x: 0, y: 8, width: 1, height: 1
},
duration: {
class: "intedit",
value: 200, config: true, min: 10,
x: 1, y: 8, width: 1, height: 1, hint: "The duration of the animation per character (ms)"
},
delayLabel: {
class: "label", label: "Animation delay:",
x: 0, y: 9, width: 1, height: 1
},
delay: {
class: "intedit",
value: 100, config: true,
x: 1, y: 9, width: 1, height: 1, hint: "The delay between the start of the animation for different characters (ms)"
},
splitLabel: {
class: "label", label: "Split character:",
x: 0, y: 10, width: 1, height: 1
},
split: {
class: "edit",
value: "|", config: true,
x: 1, y: 10, width: 1, height: 1, hint: "The character to split the text into multiple rows/columns at"
}
}
}
show_dialog = (sub, sel) ->
options = ConfigHandler dialog, depctrl.configFile, false, script_version, depctrl.configDig
options\read!
options\updateInterface "main"
button, result = aegisub.dialog.display dialog.main
if button
options\updateConfiguration result, "main"
options\write!
return process sub, sel, result
depctrl\registerMacros {
{"Make bouncy", "", show_dialog}
}