/
rounded_corners.lcb
261 lines (203 loc) · 7.46 KB
/
rounded_corners.lcb
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
/*
This widget rounds corners.
*/
widget community.livecode.trevordevore.roundedcorners
use com.livecode.canvas
use com.livecode.widget
use com.livecode.math
metadata title is "Rounded Corners"
metadata author is "Trevor DeVore"
metadata version is "1.0.0"
metadata preferredSize is "150,150"
metadata svgicon is "M0.000000 15.000000C-0.000001 6.715731 6.715729 0.000000 15.000000 0.000000L113.000000 0.000000C121.284271 0.000000 128.000000 6.715729 128.000000 15.000000L128.000000 17.000000C128.000000 25.284271 121.284271 32.000000 113.000000 32.000000L15.000000 32.000000C6.715728 32.000000-0.000001 25.284271 0.000000 16.999998L0.000000 15.000000Z"
-- public
private variable mRadius as Number
private variable mRoundTopLeft as Boolean
private variable mRoundTopRight as Boolean
private variable mRoundBottomRight as Boolean
private variable mRoundBottomLeft as Boolean
private variable mImagePath as String
private variable mResizeQuality as String
-- private
private variable mPath as Path
private variable mImage as optional Image
private variable mPaint as optional Paint
property filename get mImagePath set setImagePath
property "radius" get mRadius set setRadius
property roundTopLeft get mRoundTopLeft set setRoundTopLeft
property roundTopRight get mRoundTopRight set setRoundTopRight
property roundBottomRight get mRoundBottomRight set setRoundBottomRight
property roundBottomLeft get mRoundBottomLeft set setRoundBottomLeft
property formattedWidth get getImageWidth
property formattedHeight get getImageHeight
property resizeQuality get getResizeQuality set setResizeQuality
metadata filename.editor is "com.livecode.pi.file"
metadata resizeQuality.editor is "com.livecode.pi.enum"
metadata resizeQuality.options is "none,low,medium,high"
metadata resizeQuality.default is "medium"
metadata backgroundColor.editor is "com.livecode.pi.color"
metadata backgroundColor.default is "255,0,0"
metadata backgroundcolor.section is "Colors"
private handler setRadius(in pRadius as Number)
put max(pRadius, 0) into mRadius
calculateShape()
redraw all
end handler
private handler getResizeQuality() returns String
return mResizeQuality
end handler
private handler setResizeQuality(in pQuality as String)
if not ["none","low","medium","high"] contains [pQuality] then
put "medium" into pQuality
end if
put pQuality into mResizeQuality
end handler
private handler setImagePath(in pPath as String)
put pPath into mImagePath
loadImage()
redraw all
end handler
private handler getImageWidth() returns Integer
if mImage is nothing then
return 0
else
return the width of mImage
end if
end handler
private handler getImageHeight() returns Integer
if mImage is nothing then
return 0
else
return the height of mImage
end if
end handler
private handler setRoundTopLeft(in pBoolean as Boolean)
put pBoolean into mRoundTopLeft
calculateShape()
redraw all
end handler
private handler setRoundTopRight(in pBoolean as Boolean)
put pBoolean into mRoundTopRight
calculateShape()
redraw all
end handler
private handler setRoundBottomRight(in pBoolean as Boolean)
put pBoolean into mRoundBottomRight
calculateShape()
redraw all
end handler
private handler setRoundBottomLeft(in pBoolean as Boolean)
put pBoolean into mRoundBottomLeft
calculateShape()
redraw all
end handler
public handler OnCreate()
put 15 into mRadius
put the empty path into mPath
put "" into mImagePath
put true into mRoundTopLeft
put true into mRoundTopRight
put true into mRoundBottomRight
put true into mRoundBottomLeft
put "medium" into mResizeQuality
put nothing into mImage
put nothing into mPaint
end handler
public handler OnSave(out rProperties as Array)
put the empty array into rProperties
put mRadius into rProperties["radius"]
put mImagePath into rProperties["filename"]
put mRoundTopLeft into rProperties["roundTopLeft"]
put mRoundTopRight into rProperties["roundTopRight"]
put mRoundBottomRight into rProperties["roundBottomRight"]
put mRoundBottomLeft into rProperties["roundBottomLeft"]
put mResizeQuality into rProperties["resizeQuality"]
end handler
public handler OnLoad(in pProperties as Array)
put pProperties["radius"] into mRadius
put pProperties["filename"] into mImagePath
put pProperties["roundTopLeft"] into mRoundTopLeft
put pProperties["roundTopRight"] into mRoundTopRight
put pProperties["roundBottomRight"] into mRoundBottomRight
put pProperties["roundBottomLeft"] into mRoundBottomLeft
put pProperties["resizeQuality"] into mResizeQuality
end handler
public handler OnOpen()
calculateShape()
loadImage()
end handler
public handler OnGeometryChanged()
calculateShape()
end handler
public handler OnMouseUp()
if the mouse position is within my bounds then
post "mouseUp" to my script object with [the click button formatted as string] -- formatint needed until dp-2
else
post "mouseRelease" to my script object with [the click button formatted as string] -- formatint needed until dp-2
end if
end handler
public handler OnMouseCancel()
post "mouseRelease" to my script object with [the click button formatted as string] -- formatint needed until dp-2
end handler
public handler OnMouseDown()
post "mouseDown" to my script object with [the click button formatted as string]
end handler
private handler loadImage() returns nothing
put nothing into mImage
if mImagePath is not empty then
put image from file mImagePath into mImage
end if
end handler
private handler createImagePaint() returns nothing
if mImage is not nothing then
put pattern with mImage into mPaint
scale mPaint by [my width/the width of mImage, my height/the height of mImage]
end if
end handler
private handler calculateShape() returns nothing
variable tRadius as Number
put mRadius into tRadius
put the empty path into mPath
// Make sure radius doesn't exceed what is allowed based on widget height
put min(tRadius, my height / 2) into tRadius
// Begin a new subpath
if mRoundTopLeft then
move to point [0, tRadius] on mPath
arc through point [0, 0] to point [tRadius, 0] with radius tRadius on mPath
else
move to point [0, 0] on mPath
end if
if mRoundTopRight then
arc through point [my width, 0] to point [my width, tRadius] with radius tRadius on mPath
else
line to point [my width, 0] on mPath
end if
if mRoundBottomRight then
arc through point [my width, my height] to point [my width - tRadius, my height] with radius tRadius on mPath
else
line to point [my width, my height] on mPath
end if
if mRoundBottomLeft then
arc through point [0, my height] to point [0, tRadius] with radius tRadius on mPath
else
line to point [0, my height] on mPath
line to point [0, tRadius] on mPath
end if
// Close the path with a line back to the starting point
close path on mPath
end handler
----------
-- called whenever LiveCode needs to redraw the widget
public handler OnPaint()
if mImage is not nothing then
set the image resize quality of this canvas to mResizeQuality
createImagePaint()
else
put my background paint into mPaint
end if
set the paint of this canvas to mPaint
fill mPath on this canvas
end handler
public handler OnClose()
end handler
end widget