This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
forked from livecode/livecode
/
widget-utils.lcb
349 lines (267 loc) · 9.24 KB
/
widget-utils.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
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
/*
Copyright (C) 2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
/**
A library of utility handlers for functions commonly needed by widgets.
*/
module com.livecode.library.widgetutils
use com.livecode.canvas
use com.livecode.engine
metadata version is "1.0.0"
metadata author is "LiveCode"
metadata title is "Widget Utilities"
/**
Summary: Scales and translates a path to fit within a rectangle
Parameters:
pTargetRect: The rectangle to fit the path within
xPath: The path to transform
Example:
use com.livecode.library.iconSVG
public handler OnPaint() returns nothing
-- get the 'shopping cart' icon from the icon library
variable tPathString as String
put iconSVGPathFromName("shopping cart") into tPathString
-- create the path
variable tPath as Path
put path tPathString into tPath
-- scale the path to fit within the bounds of the widget
constrainPathToRect(my bounds, tPath)
-- fill the path
fill tPath on this canvas
end handler
Description:
Scales and transforms <xPath> so that it fits within <pTargetRect> whilst maintaining its
aspect ratio.
*/
public handler constrainPathToRect(in pTargetRect as Rectangle, inout xPath as Path)
// Scale the icon
variable tBounds
put the bounding box of xPath into tBounds
// Scale appropriately
variable tXScale as Real
variable tYScale as Real
put the width of pTargetRect / the width of tBounds into tXScale
put the height of pTargetRect / the height of tBounds into tYScale
if tXScale > tYScale then
put tYScale into tXScale
else
put tXScale into tYScale
end if
scale xPath by [tXScale, tYScale]
variable tXTranslate as Real
variable tYTranslate as Real
put the bounding box of xPath into tBounds
put the left of pTargetRect - the left of tBounds into tXTranslate
put the top of pTargetRect - the top of tBounds into tYTranslate
variable tXDiff as Real
variable tYDiff as Real
put the width of pTargetRect - the width of tBounds into tXDiff
put the height of pTargetRect - the height of tBounds into tYDiff
// align center
divide tXDiff by 2
divide tYDiff by 2
translate xPath by [tXTranslate + tXDiff, tYTranslate + tYDiff]
end handler
/**
Summary: Formats an integer as a string
Parameters:
pNumber: The integer to format
Example:
log 5 formatted as string -- logs 5.000000
log intToString(5) -- logs 5
Description:
<intToString> formats an integer as a string, removing the decimal place and any zeros
thereafter.
*/
public handler intToString(in pNumber as Number) returns String
variable tFormatted as String
put pNumber formatted as string into tFormatted
variable tDotIndex as Number
put the index of "." in tFormatted into tDotIndex
if tDotIndex is 0 then
return tFormatted
end if
return char 1 to tDotIndex - 1 of tFormatted
end handler
/**
Summary: Removes any superfluous zeros and decimal places.
Parameters:
pString: The string to remove zeros from
Example:
log stripZeros("5.0000000") -- logs 5
log stripZeros("5.432100") -- logs 5.4321
Description:
Use <stripZeros> to remove any superfluous zeros and decimal places from <pString>
which have been added by the
`tNumber formatted as string`
syntax.
*/
public handler stripZeros(in pString as String) returns String
if "." is in pString then
repeat while ((the last char of pString) is in ".0")
if the last char of pString is "." then
delete the last char of pString
exit repeat
else
delete the last char of pString
end if
end repeat
end if
return pString
end handler
private handler colorComponentToString(in pComponent as Number)
return intToString(the rounded of (pComponent * 255))
end handler
/**
Summary: Converts a color to a string representing the color
Parameters:
pColor: A value of type com.livecode.canvas.Color
pIncludeAlpha: Whether to include the alpha value in the string or not
Example:
property widgetColor get getColor
metadata widgetColor.editor is "com.livecode.pi.colorwithalpha"
private variable mColor as Color
public handler OnSave(out rProperties as Array)
put colorToString(mColor, true) into rProperties["color with alpha"]
end handler
public handler getColor() returns String
-- the editor used for the widgetColor property expects the alpha value to be included
return colorToString(mColor, true)
end handler
Example:
log colorToString(color [0.4,0.4,0.4,0.4]) -- logs "102,102,102,102"
Description:
Use the <colorToString> handler when logging colors, returning them to LiveCode script
via a property getter, or serialising them in the widget's stored properties array for saving.
*/
public handler colorToString(in pColor as Color, in pIncludeAlpha as Boolean) returns String
variable tComponents as List
variable tString as String
put [] into tComponents
push colorComponentToString(the red of pColor) onto tComponents
push colorComponentToString(the green of pColor) onto tComponents
push colorComponentToString(the blue of pColor) onto tComponents
if pIncludeAlpha then
push colorComponentToString(the alpha of pColor) onto tComponents
end if
combine tComponents with "," into tString
return tString
end handler
private handler stringToColorComponent(in pComponent as String)
return (pComponent parsed as number) / 255
end handler
/**
Summary: Converts a string to a color
Parameters:
pString: A comma delimited string representing a color in RGB / RGBA format
Example:
property widgetColor get getColor set setColor
metadata widgetColor.editor is "com.livecode.pi.colorwithalpha"
private variable mColor as Color
public handler OnLoad(in pProperties as Array)
setColor(pProperties["color"])
end handler
public handler setColor(in pColor as String)
put stringToColor(pColor) into mColor
redraw all
end handler
Description:
Use the <stringToColor> handler when receiving colors from LiveCode script
via a property setter, or from the widget's stored properties array when loading.
*/
public handler stringToColor(in pString as String) returns Color
variable tComponentList as List
split pString by "," into tComponentList
variable tColor as List
put [] into tColor
variable tColorComponent as String
repeat for each element tColorComponent in tComponentList
push stringToColorComponent(tColorComponent) onto tColor
end repeat
return color tColor
end handler
/**
Summary: Get the canonical name of the current "native" mobile theme
Example:
variable tNativeTheme as String
put getNativeThemeName() into tNativeTheme
if tNativeTheme is "android" then
-- Draw Android themed UI
else
-- Draw iOS themed UI
end if
Description:
Returns the name of the current theme that should be used when the theme is
"native".
Currently, this will return either "iOS" or "Android".
*/
public handler getNativeThemeName() returns String
variable tOS as String
put the operating system into tOS
if tOS is in ["ios","mac"] then
return "iOS"
else
return "Android"
end if
end handler
/**
Summary: Hit test to determine if a point is within or on a path
Example:
variable tPath as Path
put circle path centered at point [my width / 2, my height / 2] with radius (my width/2) into tPath
if hitTestPath(the mouse position, tPath, 1, false) then
-- mouse is on the circle perimiter
else if hitTestPath(the mouse position, tPath, 0, true) then
-- mouse is within the circle
else
-- mouse is somewhere else
end if
Description:
Determine if a point is within or on a path
Parameters:
pPoint (Point): A point to test against the path
pPath (Path): A path to test
pStrokeWidth (Number): If greater than 0 the stroke width will be used
to determine the hit area. If 0 and pWithin is false a stroke width of 1
will be used.
pWithin (Boolean): If true the handler will return true if the point is
within the filled path. If false the handler will return true if the
point is on the stroked path.
*/
public handler hitTestPath(in pPoint as Point, in pPath as Path, in pStrokeWidth as Number, in pWithin as Boolean) \
returns Boolean
-- create a 1 x 1 canvas
variable tCanvas as Canvas
put a new canvas with size [1,1] into tCanvas
-- ensure we either get pixels drawn with our color or not drawn
set the antialias of tCanvas to false
-- translate the path by the point
translate pPath by [-(the x of pPoint), -(the y of pPoint)]
-- draw the path to the canvas
set the paint of tCanvas to solid paint with color [0.0, 0.0, 0.0, 1.0]
if pStrokeWidth is 0 and not pWithin then
put 1 into pStrokeWidth
end if
if pWithin then
fill pPath on tCanvas
end if
if pStrokeWidth > 0 then
set the stroke width of tCanvas to pStrokeWidth
stroke pPath on tCanvas
end if
-- get the pixel data of the canvas
variable tData as Data
put the pixel data of tCanvas into tData
-- return whether pixel is opaque
return the first byte of tData is the byte with code 255
end handler
end module