-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy-script.lua
147 lines (134 loc) · 2.91 KB
/
my-script.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
local neulo_dlg = nil
local last_pos = nil
local neulo_data = {}
function show_neulo()
neulo_dlg:show {
wait = false,
autoscrollbars = true,
bounds = rect
}
if last_pos ~= nil then
neulo_dlg.bounds = Rectangle{
x=last_pos.x,
y=last_pos.y,
width=neulo_dlg.bounds.width,
height=neulo_dlg.bounds.height
}
last_pos = nil
end
end
function dlg_base(dir)
if neulo_dlg ~= nil then
last_pos = neulo_dlg.bounds
neulo_dlg:close()
end
neulo_data = {rows={}}
local left = dir == "left"
local right = dir == "right"
neulo_dlg = Dialog("Knitting order")
:button {
id = "left",
text = left and "[<-]" or " <- ",
focus = left,
onclick = function()
calculate_order("left")
end
}
:button {
id = "right",
text = right and"[->]" or " -> ",
focus = right,
onclick = function()
calculate_order("right")
end
}
return neulo_dlg
end
function add_color(col, i, count)
neulo_dlg:shades {
id = "col"..i,
label=" "..count,
colors = {col},
onclick = function()
for k, v in pairs(neulo_data.rows) do
if k <= i then
neulo_dlg:modify {
id="col"..k,
label="[x] "..v
}
end
end
end
}
end
function calculate_order(dir)
dlg = dlg_base(dir)
local sel = app.sprite.selection
-- first check if only one row is selected
if sel.bounds.height ~= 1 then
dlg:label{text="Select a single row"}
else
local rows = {}
local b = sel.bounds
local ranges = {
left = {b.width - 1, -1},
right = {0 , 1}
}
local range = ranges[dir]
local offset, mul = table.unpack(range)
local last_color = nil
local pix_count = 0
for i=0, b.width-1, 1 do
local x = offset + i*mul + b.x
local col = app.image:getPixel(
x - app.cel.position.x,
b.y - app.cel.position.y
)
if last_color ~= col then
if pix_count > 0 then
local count = pix_count
rows[i-1] = count
add_color(last_color, i - 1, count)
end
last_color = col
pix_count = 1
else
pix_count = pix_count + 1
end
end
if pix_count > 0 then
rows[b.width]=pix_count
add_color(last_color, b.width, pix_count)
end
neulo_data.rows=rows
end
show_neulo()
end
function init(plugin)
dlg_base()
show_neulo()
plugin:newCommand {
id = "neulo",
title = "Show knitting order",
group = "select_simple",
onclick = function()
dlg_base()
show_neulo()
end
}
end
function count(sel)
local count = 0
local bounds = sel.bounds
for x = bounds.x, bounds.x + bounds.width do
for y = bounds.y, bounds.y + bounds.height do
if sel:contains(x, y) then
count = count + 1
end
end
end
return count
end
function exit(plugin)
neulo_dlg:close()
end