forked from Youka/Macol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpos_shift.lua
247 lines (241 loc) · 6.46 KB
/
pos_shift.lua
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
script_name = "Position shifter"
script_description = "Shifting positions."
script_author = "Youka"
script_version = "1.3"
script_modified = "9th August 2011"
--Configuration
function create_config()
local conf = {
{
class = "label",
x = 0, y = 0, width = 1, height = 1,
label = "\pos("
},
{
class = "floatedit", name = "pos_x",
x = 1, y = 0, width = 1, height = 1,
value = 0.00, hint = "Shift x coordinate of \pos"
},
{
class = "label",
x = 2, y = 0, width = 1, height = 1,
label = ","
},
{
class = "floatedit", name = "pos_y",
x = 3, y = 0, width = 1, height = 1,
value = 0.00, hint = "Shift y coordinate of \pos"
},
{
class = "label",
x = 4, y = 0, width = 1, height = 1,
label = ")"
},
{
class = "label",
x = 0, y = 1, width = 1, height = 1,
label = "\move("
},
{
class = "floatedit", name = "move_x1",
x = 1, y = 1, width = 1, height = 1,
value = 0.00, hint = "Shift first x coordinate of \move"
},
{
class = "label",
x = 2, y = 1, width = 1, height = 1,
label = ","
},
{
class = "floatedit", name = "move_y1",
x = 3, y = 1, width = 1, height = 1,
value = 0.00, hint = "Shift first y coordinate of \move"
},
{
class = "label",
x = 4, y = 1, width = 1, height = 1,
label = ","
},
{
class = "floatedit", name = "move_x2",
x = 5, y = 1, width = 1, height = 1,
value = 0.00, hint = "Shift second x coordinate of \move"
},
{
class = "label",
x = 6, y = 1, width = 1, height = 1,
label = ","
},
{
class = "floatedit", name = "move_y2",
x = 7, y = 1, width = 1, height = 1,
value = 0.00, hint = "Shift second y coordinate of \move"
},
{
class = "label",
x = 8, y = 1, width = 1, height = 1,
label = "(, ?, ?) )"
},
{
class = "label",
x = 0, y = 2, width = 1, height = 1,
label = "\org("
},
{
class = "floatedit", name = "org_x",
x = 1, y = 2, width = 1, height = 1,
value = 0.00, hint = "Shift x coordinate of \org"
},
{
class = "label",
x = 2, y = 2, width = 1, height = 1,
label = ","
},
{
class = "floatedit", name = "org_y",
x = 3, y = 2, width = 1, height = 1,
value = 0.00, hint = "Shift y coordinate of \org"
},
{
class = "label",
x = 4, y = 2, width = 1, height = 1,
label = ")"
},
{
class = "label",
x = 0, y = 3, width = 1, height = 1,
label = "Clips: x:"
},
{
class = "floatedit", name = "clip_x",
x = 1, y = 3, width = 1, height = 1,
value = 0.00, hint = "Shift x coordinate of \pos"
},
{
class = "label",
x = 2, y = 3, width = 1, height = 1,
label = " y:"
},
{
class = "floatedit", name = "clip_y",
x = 3, y = 3, width = 1, height = 1,
value = 0.00, hint = "Shift y coordinate of \pos"
}
}
return conf
end
--Shift positions of selected lines
function pos_shift(subs,sel,config)
for x, i in ipairs(sel) do
local a = subs[i]
--\pos
local function pos_repl(x,y)
x, y = tonumber(x), tonumber(y)
x = x + config.pos_x
y = y + config.pos_y
return string.format("\\pos(%.3f,%.3f)",x,y)
end
a.text = a.text:gsub("\\pos%((%s*%-?[%d%.]+%s*),(%s*%-?[%d%.]+%s*)%)",pos_repl,1)
--\move
local function move_repl(x, y, x2, y2, t)
t = t or ""
x, y, x2, y2 = tonumber(x), tonumber(y), tonumber(x2), tonumber(y2)
x = x + config.move_x1
y = y + config.move_y1
x2 = x2 + config.move_x2
y2 = y2 + config.move_y2
return string.format("\\move(%.3f,%.3f,%.3f,%.3f%s)",x,y,x2,y2,t)
end
a.text = a.text:gsub("\\move%((%s*%-?[%d%.]+%s*),(%s*%-?[%d%.]+%s*),(%s*%-?[%d%.]+%s*),(%s*%-?[%d%.]+%s*)([%d%,%s]-)%)",move_repl,1)
--\org
local function org_repl(x,y)
x, y = tonumber(x), tonumber(y)
x = x + config.pos_x
y = y + config.pos_y
return string.format("\\org(%.3f,%.3f)",x,y)
end
a.text = a.text:gsub("\\org%((%s*%-?[%d%.]+%s*),(%s*%-?[%d%.]+%s*)%)",org_repl,1)
--\(i)clip (rectangle)
local function rec_clip_repl(i, x1, y1, x2, y2)
if not y2 then
y2 = x2
x2 = y1
y1 = x1
x1 = i
i = ""
end
x1, y1, x2, y2 = tonumber(x1), tonumber(y1), tonumber(x2), tonumber(y2)
x1 = x1 + config.clip_x
y1 = y1 + config.clip_y
x2 = x2 + config.clip_x
y2 = y2 + config.clip_y
return string.format("\\%sclip(%.3f,%.3f,%.3f,%.3f)", i, x1, y1, x2, y2)
end
a.text = a.text:gsub("\\(%i?)clip%((%s*%-?[%d%.]+%s*),(%s*%-?[%d%.]+%s*),(%s*%-?[%d%.]+%s*),(%s*%-?[%d%.]+%s*)%)",rec_clip_repl,1)
--\(i)clip (vectors)
local function vec_clip_repl(i, acc, shape)
if not shape then
if not acc then
shape = i
i = ""
acc = ""
else
shape = acc
if i ~= "i" then
acc = i
i = ""
else
acc = ""
end
end
end
local function x_y_adder(x,y)
x = x + config.clip_x
y = y + config.clip_y
return string.format("%d %d", x, y)
end
shape = shape:gsub("(%d+)%s*(%d+)", x_y_adder)
return string.format("\\%sclip(%s%s)", i, acc, shape)
end
a.text = a.text:gsub("\\(%i?)clip%((%s*%d*%s*%,?)([mlbsc%s%d%-]+)%)",vec_clip_repl,1)
--Return changed line
subs[i] = a
end
end
--Initialisation + GUI
function load_macro_pos(subs,sel)
local shift = {"Shift","Cancel"}
local sh, config = aegisub.dialog.display(create_config(subs,meta),shift)
if sh=="Shift" then
pos_shift(subs,sel,config)
aegisub.set_undo_point("\""..script_name.."\"")
end
end
--Test for activation (something to change?)
function test_pos(text)
--\pos?
local p1, p2 = text:match("\\pos%((%s*%-?[%d%.]+%s*),(%s*%-?[%d%.]+%s*)%)")
local tp = tonumber(p1) and tonumber(p2)
--\move (without times)?
local m1, m2, m3, m4 = text:match("\\move%((%s*%-?[%d%.]+%s*),(%s*%-?[%d%.]+%s*),(%s*%-?[%d%.]+%s*),(%s*%-?[%d%.]+%s*)%)")
local tm = tonumber(m1) and tonumber(m2) and tonumber(m3) and tonumber(m4)
--\move (with times)?
local mm1, mm2, mm3, mm4, mm5, mm6 = text:match("\\move%((%s*%-?[%d%.]+%s*),(%s*%-?[%d%.]+%s*),(%s*%-?[%d%.]+%s*),(%s*%-?[%d%.]+%s*),(%s*%d+%s*),(%s*%d+%s*)%)")
local tmm = tonumber(mm1) and tonumber(mm2) and tonumber(mm3) and tonumber(mm4) and tonumber(mm5) and tonumber(mm6)
--\org?
local o1, o2 = text:match("\\org%((%s*%-?[%d%.]+%s*),(%s*%-?[%d%.]+%s*)%)")
local to = tonumber(o1) and tonumber(o2)
--Something in line?
if tp or tm or tmm or to then return true end
return nil
end
function activate_macro_pos(subs, sel)
for x, i in ipairs(sel) do
if not test_pos(subs[i].text) then
return false
end
end
return true
end
--Register macro in aegisub
aegisub.register_macro(script_name,script_description, load_macro_pos, activate_macro_pos)