-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathPath Editor.lua
More file actions
211 lines (173 loc) · 5.44 KB
/
Path Editor.lua
File metadata and controls
211 lines (173 loc) · 5.44 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
----------------------------------------------------------------------
-- This example shows how to create a simple path editor,
-- using the canvas widget.
----------------------------------------------------------------------
if app.apiVersion < 21 then
return app.alert("This script requires Aseprite v1.3-rc1")
end
-- This script requires UI
if not app.isUIAvailable then
return
end
local PointRadius = 5
-- Use the theme colors
local RegularColor = app.theme.color["text"]
local HighlightColor = app.theme.color["selected"]
local dialog = Dialog("Path Editor Example")
-- Hold mouse position in a variable
local mouse = Point(0, 0)
local points = {}
local grabbedPoint = nil
local lines = {}
local lineStart = nil
local get_distance = function(a, b)
return math.sqrt((b.x - a.x) ^ 2 + (b.y - a.y) ^ 2)
end
local find_close_point = function(available_points, target_point, distance)
for index, point in ipairs(available_points) do
if get_distance(point, target_point) < distance then
return index, point
end
end
end
local onpaint = function(ev)
local context = ev.context
-- Use the anti-aliasing option from the dialog
context.antialias = dialog.data["antialias"]
-- Draw the beckground of the editor first
context:drawThemeRect("sunken_focused",
Rectangle(0, 0, context.width, context.height))
-- Draw the new line with the highlight color
if lineStart then
context.color = HighlightColor
context:beginPath()
context:moveTo(lineStart.x, lineStart.y)
context:lineTo(mouse.x, mouse.y)
context:stroke()
end
-- Draw all lines
for _, line in ipairs(lines) do
context.color = RegularColor
context:beginPath()
context:moveTo(line.startPoint.x, line.startPoint.y)
context:lineTo(line.endPoint.x, line.endPoint.y)
context:stroke()
end
-- Draw all points
for _, point in ipairs(points) do
context:beginPath()
-- Shift X and Y to draw the shape around the point
local pointShape = Rectangle {
x = point.x - PointRadius,
y = point.y - PointRadius,
width = PointRadius * 2,
height = PointRadius * 2
}
context:roundedRect(pointShape, PointRadius, PointRadius)
local isMouseOver = get_distance(mouse, point) < PointRadius
local isLineStart = point == lineStart
-- Highlight points close to the mouse position and ones that are a start of a new line
if isMouseOver or isLineStart then
context.color = HighlightColor
context:fill()
else
context.color = RegularColor
context:stroke()
end
end
end
local onmouseup = function(ev)
local hoverPointIndex, hoverPoint = find_close_point(points, mouse,
PointRadius)
if ev.button == MouseButton.LEFT then
if lineStart and hoverPoint and hoverPoint ~= lineStart then
local newLine = {startPoint = lineStart, endPoint = hoverPoint}
table.insert(lines, newLine)
end
if ev.ctrlKey then
local newPoint = {x = ev.x, y = ev.y}
table.insert(points, newPoint)
end
grabbedPoint = nil
lineStart = nil
end
if ev.button == MouseButton.RIGHT then
if hoverPoint then
table.remove(points, hoverPointIndex)
local newLines = {}
for _, line in ipairs(lines) do
if line.startPoint ~= hoverPoint and line.endPoint ~= hoverPoint then
table.insert(newLines, line)
end
end
lines = newLines
end
end
dialog:repaint()
end
local onmousemove = function(ev)
-- Save the delta from the previous mouse position, this will be used to move the grabbed point
local delta = Point(ev.x - mouse.x, ev.y - mouse.y)
-- Update the last, saved mouse position
mouse = Point(ev.x, ev.y)
-- Find the first, close point
local _, hoverPoint = find_close_point(points, mouse, PointRadius)
if ev.button == MouseButton.LEFT then
if ev.shiftKey then
if not lineStart and hoverPoint then
lineStart = hoverPoint
end
else
if grabbedPoint then
grabbedPoint.x = grabbedPoint.x + delta.x
grabbedPoint.y = grabbedPoint.y + delta.y
elseif hoverPoint then
grabbedPoint = hoverPoint
end
end
end
-- This could be optimized to only repaint the dialog when something changes
-- For now the dialog is redrawn every time the mouse moves over it
dialog:repaint()
end
dialog
:canvas{
width = 200,
height = 200,
label = "Canvas:",
onpaint = onpaint,
onmouseup = onmouseup,
onmousemove = onmousemove
}
:check{
id = "antialias",
label = "Anti-aliasing:",
onclick = function() dialog:repaint() end
}
:separator{text = "Instructions:"}
:label{
label = "Left-click:",
text = "Move",
enabled = false
}
:label{
label = "Left-click + Ctrl:",
text = "Add",
enabled = false
}
:label{
label = "Left-click + Shift:",
text = "Connect",
enabled = false
}
:label{
label = "Right-click:",
text = "Delete",
enabled = false
}
:separator()
:button{
text = "&Cancel",
focus = true
}
dialog:show{wait = false}