-
Notifications
You must be signed in to change notification settings - Fork 25
/
Canvas Shapes Demo.lua
413 lines (376 loc) · 12 KB
/
Canvas Shapes Demo.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
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
404
405
406
407
408
409
410
411
412
413
----------------------------------------------------------------------
-- This example shows how to draw different shapes,
-- using the canvas widget.
----------------------------------------------------------------------
if app.apiVersion < 22 then
return app.alert("This script requires Aseprite v1.3-rc2")
end
-- This script requires UI
if not app.isUIAvailable then
return
end
local RegularColor = app.theme.color["text"]
local DisabledColor = app.theme.color["disabled"]
local Shape = {
None = "",
Line = "Line",
Curve = "Curve",
Triangle = "Triangle",
Rect = "Rectangle",
RoundedRect = "Rounded Rectangle",
Oval = "Oval"
}
local dialog = Dialog("Canvas Shapes Demo")
dialog
:canvas{
id = "canvas",
label = "Canvas:",
width = 160,
height = 160,
onpaint = function(ev)
local gc = ev.context
-- Declare a local method for drawing reference points using the disabled text color
local fillReferencePoint = function(point, name)
local textSize = gc:measureText(name)
gc.color = DisabledColor
gc:fillText(name, point.x - textSize.width / 2,
point.y - textSize.height - 4)
gc:beginPath()
gc:oval(point.x - 4, point.y - 4, 8, 8)
gc:fill()
end
local strokeReferenceRectangle = function(rectangle)
gc.color = DisabledColor
gc:beginPath()
gc:rect(rectangle)
gc:stroke()
end
-- Draw the editor background with a border first
gc:drawThemeRect("sunken_focused", Rectangle(0, 0, gc.width, gc.height))
-- Clip to the area withing the drawn editor border
-- All drawing operations after taht will only draw within the clipping region
-- This assure that no shape will be drawn over the borders
gc:beginPath()
gc:rect(Rectangle(4, 4, gc.width - 8, gc.height - 8))
gc:clip()
local p1 = Point(dialog.data["p1-x"], dialog.data["p1-y"])
local p2 = Point(dialog.data["p2-x"], dialog.data["p2-y"])
local p3 = Point(dialog.data["p3-x"], dialog.data["p3-y"])
local rectangle = Rectangle(p1.x, p1.y, dialog.data["width"],
dialog.data["height"])
local cp1 = Point(dialog.data["cp1-x"], dialog.data["cp1-y"])
local cp2 = Point(dialog.data["cp2-x"], dialog.data["cp2-y"])
-- Each shape has it's own code block for drawing
-- When drawing multiple lines or shapes, `gc:beginPath()` is called to start a new sub-path
-- This allows us to draw each subpath with different parameters - e.g. color, stroke width
if dialog.data.shape == Shape.Line then
-- Draw the points first
fillReferencePoint(p1, "P1")
fillReferencePoint(p2, "P2")
if dialog.data["parallel-line"] then
local px = dialog.data["parallel-line-x"]
local py = dialog.data["parallel-line-y"]
gc.color = DisabledColor
gc:beginPath()
gc:moveTo(p1.x + px, p1.y + py)
gc:lineTo(p2.x + px, p2.y + py)
gc:stroke()
end
if dialog.data["perpendicular-line"] then
-- Calculate the center of the line
local cx = p1.x + ((p2.x - p1.x) / 2)
local cy = p1.y + ((p2.y - p1.y) / 2)
fillReferencePoint(Point(cx, cy), "")
local ppx = cx - p1.x
local ppy = cy - p1.y
-- Draw the perpendicular line
gc.color = DisabledColor
gc:beginPath()
gc:moveTo(cx + ppy, cy - ppx)
gc:lineTo(cx - ppy, cy + ppx)
gc:stroke()
end
-- Draw the line
gc.color = RegularColor
gc.strokeWidth = 2
gc:beginPath()
gc:moveTo(p1.x, p1.y)
gc:lineTo(p2.x, p2.y)
gc:stroke()
elseif dialog.data.shape == Shape.Curve then
-- Draw the points and control points first
fillReferencePoint(p1, "P1")
fillReferencePoint(p2, "P2")
fillReferencePoint(cp1, "CP1")
fillReferencePoint(cp2, "CP2")
-- Draw the lines connecting to the control points
gc.color = DisabledColor
gc:beginPath()
gc:moveTo(p1.x, p1.y)
gc:lineTo(cp1.x, cp1.y)
-- Calling `gc:moveTo` makes the sub-path jump to a new point without creating a line
-- This way we can draw these two separate lines with a single `gc:stroke()` call
gc:moveTo(p2.x, p2.y)
gc:lineTo(cp2.x, cp2.y)
gc:stroke()
-- Draw the curve
gc.color = RegularColor
gc.strokeWidth = 2
gc:beginPath()
gc:moveTo(p1.x, p1.y)
gc:cubicTo(cp1.x, cp1.y, cp2.x, cp2.y, p2.x, p2.y)
gc:stroke()
elseif dialog.data.shape == Shape.Triangle then
-- Draw the points first
fillReferencePoint(p1, "P1")
fillReferencePoint(p2, "P2")
fillReferencePoint(p3, "P3")
-- Draw the triangle
gc.color = RegularColor
gc.strokeWidth = 2
gc:beginPath()
gc:moveTo(p1.x, p1.y)
gc:lineTo(p2.x, p2.y)
gc:lineTo(p3.x, p3.y)
-- Calling `gc:closePath` connects draws a line between the last point (P3) and the first on (P1)
gc:closePath()
gc:stroke()
elseif dialog.data.shape == Shape.Rect then
-- Draw the point first
fillReferencePoint(p1, "P1")
-- Draw the rectangle
gc.color = RegularColor
gc.strokeWidth = 2
gc:beginPath()
gc:rect(rectangle)
gc:stroke()
elseif dialog.data.shape == Shape.RoundedRect then
-- Draw the point first
fillReferencePoint(p1, "P1")
-- Draw the bounding rectangle for reference
strokeReferenceRectangle(rectangle)
-- Draw the rounded rectangle
gc.color = RegularColor
gc.strokeWidth = 2
gc:beginPath()
gc:roundedRect(rectangle, dialog.data["radius-x"],
dialog.data["radius-y"])
gc:stroke()
elseif dialog.data.shape == Shape.Oval then
-- Draw the point first
fillReferencePoint(p1, "P1")
-- Draw the bounding rectangle for reference
strokeReferenceRectangle(rectangle)
-- Draw the oval, using the regular text color
gc.color = RegularColor
gc.strokeWidth = 2
gc:beginPath()
gc:oval(rectangle)
gc:stroke()
end
end
}
:combobox{
id = "shape",
label = "Shape:",
option = Shape.None,
options = {
Shape.None, Shape.Line, Shape.Curve, Shape.Triangle, Shape.Rect,
Shape.RoundedRect, Shape.Oval
},
onchange = function()
local shape = dialog.data.shape
local usesP1 = shape == Shape.Line or shape == Shape.Curve or shape ==
Shape.Triangle or shape == Shape.Rect or shape ==
Shape.RoundedRect or shape == Shape.Oval
local usesP2 = shape == Shape.Line or shape == Shape.Curve or shape ==
Shape.Triangle
local usesP3 = shape == Shape.Triangle
local usesControlPoints = shape == Shape.Curve
local usesWidth = shape == Shape.Rect or shape == Shape.RoundedRect or
shape == Shape.Oval
local usesHeight = shape == Shape.Rect or shape == Shape.RoundedRect or
shape == Shape.Oval
local usesRadius = shape == Shape.RoundedRect
local showParallel = shape == Shape.Line and dialog.data["parallel-line"]
dialog
:modify{id = "p1-x", visible = usesP1}
:modify{id = "p1-y", visible = usesP1}
:modify{id = "p2-x", visible = usesP2}
:modify{id = "p2-y", visible = usesP2}
:modify{id = "p3-x", visible = usesP3}
:modify{id = "p3-y", visible = usesP3}
:modify{id = "cp1-x", visible = usesControlPoints}
:modify{id = "cp1-y", visible = usesControlPoints}
:modify{id = "cp2-x", visible = usesControlPoints}
:modify{id = "cp2-y", visible = usesControlPoints}
:modify{id = "parallel-line", visible = shape == Shape.Line}
:modify{id = "parallel-line-x", visible = shape == showParallel}
:modify{id = "parallel-line-y", visible = shape == showParallel}
:modify{id = "perpendicular-line", visible = shape == Shape.Line}
:modify{id = "width", visible = usesWidth}
:modify{id = "height", visible = usesHeight}
:modify{id = "radius-x", visible = usesRadius}
:modify{id = "radius-y", visible = usesRadius}
if shape ~= Shape.None then dialog:repaint() end
end
}
:slider{
id = "p1-x",
label = "P1 (X/Y):",
min = 0,
max = 160,
value = 80,
visible = false,
onchange = function() dialog:repaint() end
}
:slider{
id = "p1-y",
min = 0,
max = 160,
value = 20,
visible = false,
onchange = function() dialog:repaint() end
}
:slider{
id = "p2-x",
label = "P2 (X/Y):",
min = 0,
max = 160,
value = 20,
visible = false,
onchange = function() dialog:repaint() end
}
:slider{
id = "p2-y",
min = 0,
max = 160,
value = 140,
visible = false,
onchange = function() dialog:repaint() end
}
:slider{
id = "p3-x",
label = "P3 (X/Y):",
min = 0,
max = 160,
value = 140,
visible = false,
onchange = function() dialog:repaint() end
}
:slider{
id = "p3-y",
min = 0,
max = 160,
value = 140,
visible = false,
onchange = function() dialog:repaint() end
}
:check{
id = "parallel-line",
label = "Parallel:",
text = "Show",
selected = false,
visible = false,
onclick = function()
dialog
:modify{id = "parallel-line-x", visible = dialog.data["parallel-line"]}
:modify{id = "parallel-line-y", visible = dialog.data["parallel-line"]}
dialog:repaint()
end
}
:slider{
id = "parallel-line-x",
label = "Shift (X/Y):",
min = -80,
max = 80,
value = 10,
visible = false,
onchange = function() dialog:repaint() end
}
:slider{
id = "parallel-line-y",
min = -80,
max = 80,
value = 10,
visible = false,
onchange = function() dialog:repaint() end
}
:check{
id = "perpendicular-line",
label = "Perpendicular:",
text = "Show",
selected = false,
visible = false,
onclick = function() dialog:repaint() end
}
:slider{
id = "cp1-x",
label = "CP1 (X/Y):",
min = 0,
max = 160,
value = 20,
visible = false,
onchange = function() dialog:repaint() end
}
:slider{
id = "cp1-y",
min = 0,
max = 160,
value = 20,
visible = false,
onchange = function() dialog:repaint() end
}
:slider{
id = "cp2-x",
label = "CP2 (X/Y):",
min = 0,
max = 160,
value = 140,
visible = false,
onchange = function() dialog:repaint() end
}
:slider{
id = "cp2-y",
min = 0,
max = 160,
value = 140,
visible = false,
onchange = function() dialog:repaint() end
}
:slider{
id = "width",
label = "Width:",
min = 0,
max = 160,
value = 120,
visible = false,
onchange = function() dialog:repaint() end
}
:slider{
id = "height",
label = "Height:",
min = 0,
max = 160,
value = 80,
visible = false,
onchange = function() dialog:repaint() end
}
:slider{
id = "radius-x",
label = "Radius (X/Y):",
min = 0,
max = 160,
value = 10,
visible = false,
onchange = function() dialog:repaint() end
}
:slider{
id = "radius-y",
min = 0,
max = 160,
value = 10,
visible = false,
onchange = function() dialog:repaint() end
}
dialog:show()