Repository navigation
Expand file tree
/
Copy pathSelectionToLayer.lua
More file actions
403 lines (337 loc) · 8.82 KB
/
Copy pathSelectionToLayer.lua
File metadata and controls
403 lines (337 loc) · 8.82 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
-- GOAL: When highlighting a portion of a sprite on a given layer, create a shortcut that will copy the contents of that layer onto a new one.
-- FUNCTIONALITY
--[[https://www.aseprite.org/docs/selecting/
- Should be able to do the described goal.
- Should also implement a way to "cut and paste" that part of the layer onto a new one.
- This already exists in a fashion, as you can copy a selection to a new sprite.
--]]
--[[TODO:
1. https://github.com/aseprite/aseprite/blob/main/src/app/commands/cmd_new_layer.cpp
- This has all the parameters when creating a new layer.
- Check ~ Line 55
2. https://github.com/Windower/Lua/blob/dev/addons/libs/strings.lua
- Implement utility functions for strings.
- Check Lines 198 - 205
--]]
USESLIDER = false
USEDROPDOWN = true
CUTCONTENT = false
SELECTLOC = 'Above'
CLOSECHK = false
-- Get active layer
local layer = app.activeLayer
-- Get layer data
local data = layer.data
-- Get active sprite
local spr = app.activeSprite
-- HELPER FUNCTIONS
-- ==========================================================================
-- Shows the type of an item
function st(v)
print(type(v))
end
function str(v)
if type(v) == 'table' then
for k,v in pairs(v) do
st(v)
str(v)
end
else
st(v)
end
end
-- Prints the contents of a table to the console
-- Stands for "print table"
function pt(t)
for k,v in pairs(t) do
print(tostring(k)..": "..tostring(v))
end
end
-- Prints items in a table if they are of a type
function ptp(t,...)
local types = {...}
local checked = {}
local valid = {'table','number','string','function','userdata','nil','boolean'}
for _, okay in ipairs(valid) do
for i, name in ipairs(types) do
local l = #checked + 1
checked[l] = okay == name and name or nil
end
end
if #checked == 0 then
print("No valid types provided")
return
else
for k,v in pairs(t) do
for _, name in ipairs(checked) do
local tp = type(v)
if tp == name then
print(tostring(k).." is a valid type of "..tp..".")
print("Its value is: "..tostring(v)..'\n')
print('')
end
end
end
end
end
-- Use this if your table contains many tables of its own.
-- Stands for "print table recursively".
function ptr(t)
for k,v in pairs(t) do
if type(k) == 'table' then
print(tostring(k))
pt(v)
else
print(tostring(k)..": "..tostring(v))
end
end
end
-- Returns the string padded with zeroes until the length is len.
function string.zfill(str, len)
return str:lpad('0', len)
end
---@func ptr(t)
---@arg {tbl} t table
---@arg {num} spc number of spaces to add to the front of an item
---@desc Prints tables recursively.
--- Use this if your table contains many tables of its own.
--- Infinite looping is a potential, be careful
function ptrw(t,spc)
local ws = ''
spc = type(spc) == 'number' and spc or 0
ws = srpt(' ', spc)
for k,v in pairs(t) do
if type(k) == 'table' then
print(ws.."TABLE: "..tostring(k))
spc=spc+1
ptr(v,spc)
spc=spc-1
else
print(ws..tostring(k)..": "..tostring(v))
end
end
end
---@func srpt(c, n)
---@arg {str} c character or string
---@arg {num} n number of repetitions
---@desc Creates a string of 'n' length made up of only 'c' characters
---@exam local star10 = srpt('*', 10) -- "**********"
function srpt(c,n)
local r = ""
n = type(n) == 'number' and n or 1
for i = 1, n do
r = r .. tostring(c)
end
return r
end
-- LAME FUNCTIONS
-- ==========================================================================
---@func HistoryTest()
---@desc Can we get Frude Dude to the auditorium?
function HistoryTest()
-- Time circuits must be haywire...
local ___RUFUS = not app.activeSprite
if ___RUFUS then
app.alert("Dude, Missy...")
app.loadfile("missy.gif")
else
TelephoneBoothTimeMachine()
end
end
---@func HistoryTest()
---@desc Bill and Ted's final showdown.
function TelephoneBoothTimeMachine()
return
end
function print_test()
if type(layer) == 'table' then
for k,v in pairs(popup) do
if type(v) == 'table' then
print("Inner table @ "..tostring(k))
pt(v)
print("=======================\n")
else
print(tostring(k)..": "..tostring(v))
end
end
else
local pre = "TYPE:"
local len = #pre
local spc = srpt(" ", len)
local mid = type(v).." | "
local com = pre..spc..mid
local post = tostring(k)..": "..tostring(v)
local final = com..post
print(final)
end
end
-- UTILITY FUNCTIONS
-- ==========================================================================
---@func newLayer(name)
---@arg {str} name name of the layer to create
---@desc Creates a new layer
function newLayer(passedname)
return app.command.newLayer({
name=passedname,
})
end
---@func layer_count()
---@desc Gets the number of layers in the document
---@ret {num}
function layer_count()
return app.activeLayer.stackIndex
end
---@func layer_names()
---@desc Gets the names of layers in the document
---@ret {tbl}
function layer_names()
local names = {}
local len = layer_count()
len = type(len) == 'number' and len or 2
for i=1, len do
local cur = app.site
names[i] = cur.layer.stackIndex
end
return names
end
-- CREATE THE DIALOG
-- ==========================================================================
popup = Dialog("Selection to Layer")
local size = popup.bounds
local w,h = 210,100
-- popup.bounds = Rectangle(1080/2 - w, size.y+h,w+h-h/2,h)
popup.bounds = Rectangle(50, 50,w+h-h/2,h)
popup:entry{
id="newname",
label="New Layer:",
text="Layer",
focus=true
}
-- Create a slider
if USESLIDER then
popup:slider{
id="placement",
label="Location",
}
end
-- Create a Dropdown
if USEDROPDOWN then
-- CHANGING
--[[local layercount = layer_count()
popup:combobox{
id = "placement",
label = "Layer Position",
option = "Above",
options = layer_names(),
onchange = function() popup.id.option = popup.combobox.active end
}]]--
popup:combobox{
id='placement',
label='Layer Location',
option='Above',
options={'Above','Below'},
onchange = function()
-- SELECTLOC = option == 'Above'
SELECTLOC = popup.data.placement == 'Above'
app.alert(SELECTLOC)
-- app.alert(popup.data.placement == 'Above' and 'Should be Above' or 'Should be Below')
end
}
end
-- Make the Separator
popup:separator()
-- Make the layer go
popup:button{
id="creator",
label="Selector",
text="Layer from Selection",
focus=false,
onclick=function()
-- newLayer(popup.creator.text)
-- app.command.TogglePreview()
-- pt(popup.data)
end
}
-- Make the action destructive
popup:check{
id="cutter",
label="Cut Selection",
text="Remove selection from current layer.",
selected=false,
onclick=function()
-- CUTCONTENT = CUTCONTENT == false and true or false
CUTCONTENT = not CUTCONTENT
end
}
-- Close the window on go
popup:check{
id="closeongo",
label="Close",
text="Close window after 'GO!'",
selected=false,
onclick=function()
-- CUTCONTENT = CUTCONTENT == false and true or false
CLOSECHK = not CLOSECHK
end
}
popup:newrow()
-- Make the magic happen.
popup:button{
id="go",
label="Make Layer",
text="GO!",
focus=false,
onclick=function()
-- TODO: Try to get this in app.transaction
-- Check if the selection should be cut or copied.
-- local command = self.cutter.selected and
local command =
CUTCONTENT and
app.command.Cut or
app.command.Copy
-- Perform the action.
command()
-- Debuggery
-- app.alert(CUTCONTENT == app.command.Copy and "Cutting" or "Copying" )
-- app.command.NewLayer({viacopy=true})
-- Position of the layer relative to current.
pt(popup.data)
-- app.command.NewLayer({before=SELECTLOC == popup.data.placement})
app.command.NewLayer({before=not SELECTLOC})
-- Lay it down.
app.command.Paste()
-- Set the name.
app.activeLayer.name = popup.data.newname
-- Close the window if the checkbox is ticked.
if CLOSECHK then
popup:close()
end
--[[Make the new layer completely copied.
app.command.NewLayer{
name = popup.data.newname,
group = false,
reference = false,
ask = false, -- User can enter name here.
fromFile = false,
fromClipboard = true,
viaCut = CUTCONTENT,
viaCopy = not CUTCONTENT,
top = false,
before = SELECTLOC == 'Above'
}]]--
end
}
-- Create the quit button
popup:button{
text="Cancel",
onclick=function()
popup:close()
end
}
-- THE SHOW MUST GO ON!
-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
popup:show{wait=false}
-- WYLD STALYNS
-- ==========================================================================
-- San Dimas High School Football Rules!
-- HistoryTest()