Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 03f1f97

Browse files
author
livecodeali
committed
[[ Widgets ]] Factor out common handlers into utility library
1 parent f391d2b commit 03f1f97

12 files changed

Lines changed: 330 additions & 552 deletions

File tree

extensions/extensions.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
'sources':
2020
[
21+
'modules/widget-utils/widget-utils.lcb',
22+
2123
'libraries/canvas/canvas.lcb',
2224
'libraries/iconsvg/iconsvg.lcb',
2325
'libraries/json/json.lcb',
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Utility module added
2+
3+
A module, **com.livecode.widgetutils**, has been written to provide support for functions
4+
commonly needed by widgets.
5+
6+
The functions are:
7+
- `constrainPathToRect`: Scales and translates a Path value to fit within a rectangle
8+
- `intToString`: Formats an integer as a string
9+
- `stripZeroes`: Removes any superfluous zeros and decimal places from a string representation of a number.
10+
- `colorToString`: Converts a value of type Color to an RGB or RGBA string representing the color
11+
- `stringToColor`: Converts a comma-delimited string representing an RGB or RGBA color to a value of type Color.
12+
13+
See the documentation for more details on the individual handlers and their syntax.
Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
/*
2+
Copyright (C) 2015 LiveCode Ltd.
3+
4+
This file is part of LiveCode.
5+
6+
LiveCode is free software; you can redistribute it and/or modify it under
7+
the terms of the GNU General Public License v3 as published by the Free
8+
Software Foundation.
9+
10+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
11+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
17+
18+
/*
19+
A library of utility handlers for functions commonly needed by widgets.
20+
*/
21+
22+
module com.livecode.widgetutils
23+
24+
use com.livecode.canvas
25+
use com.livecode.engine
26+
27+
metadata version is "1.0.0"
28+
metadata author is "LiveCode"
29+
metadata title is "Widget Utilities"
30+
31+
/*
32+
Summary: Scales and translates a path to fit within a rectangle
33+
34+
Parameters:
35+
pTargetRect: The rectangle to fit the path within
36+
xPath: The path to transform
37+
38+
Example:
39+
use com.livecode.library.iconSVG
40+
41+
public handler OnPaint() returns nothing
42+
-- get the 'shopping cart' icon from the icon library
43+
variable tPathString as String
44+
put iconSVGPathFromName("shopping cart") into tPathString
45+
46+
-- create the path
47+
variable tPath as Path
48+
put path tPathString into tPath
49+
50+
-- scale the path to fit within the bounds of the widget
51+
constrainPathToRect(my bounds, tPath)
52+
53+
-- fill the path
54+
fill tPath on this canvas
55+
end handler
56+
57+
Description:
58+
Scales and transforms <xPath> so that it fits within <pTargetRect> whilst maintaining its
59+
aspect ratio.
60+
*/
61+
public handler constrainPathToRect(in pTargetRect as Rectangle, inout xPath as Path)
62+
63+
// Scale the icon
64+
variable tBounds
65+
put the bounding box of xPath into tBounds
66+
67+
// Scale appropriately
68+
variable tXScale as Real
69+
variable tYScale as Real
70+
71+
put the width of pTargetRect / the width of tBounds into tXScale
72+
put the height of pTargetRect / the height of tBounds into tYScale
73+
74+
if tXScale > tYScale then
75+
put tYScale into tXScale
76+
else
77+
put tXScale into tYScale
78+
end if
79+
80+
scale xPath by [tXScale, tYScale]
81+
82+
variable tXTranslate as Real
83+
variable tYTranslate as Real
84+
85+
put the bounding box of xPath into tBounds
86+
87+
put the left of pTargetRect - the left of tBounds into tXTranslate
88+
put the top of pTargetRect - the top of tBounds into tYTranslate
89+
90+
variable tXDiff as Real
91+
variable tYDiff as Real
92+
93+
put the width of pTargetRect - the width of tBounds into tXDiff
94+
put the height of pTargetRect - the height of tBounds into tYDiff
95+
96+
// align center
97+
divide tXDiff by 2
98+
divide tYDiff by 2
99+
100+
translate xPath by [tXTranslate + tXDiff, tYTranslate + tYDiff]
101+
end handler
102+
103+
/*
104+
Summary: Formats an integer as a string
105+
106+
Parameters:
107+
pInteger: The integer to format
108+
109+
Example:
110+
log 5 formatted as string -- logs 5.000000
111+
log intToString(5) -- logs 5
112+
113+
Description:
114+
<intToString> formats an integer as a string, removing the decimal place and any zeros
115+
thereafter.
116+
*/
117+
public handler intToString(in pNumber as Number) returns String
118+
variable tFormatted as String
119+
put pNumber formatted as string into tFormatted
120+
121+
variable tDotIndex as Number
122+
put the index of "." in tFormatted into tDotIndex
123+
124+
if tDotIndex is 0 then
125+
return tFormatted
126+
end if
127+
128+
return char 1 to tDotIndex - 1 of tFormatted
129+
end handler
130+
131+
/*
132+
Summary: Removes any superfluous zeros and decimal places.
133+
134+
Parameters:
135+
pString: The string to remove zeros from
136+
137+
Example:
138+
log stripZeros("5.0000000") -- logs 5
139+
log stripZeros("5.432100") -- logs 5.4321
140+
141+
Description:
142+
Use <stripZeros> to remove any superfluous zeros and decimal places from <pString>
143+
which have been added by the
144+
145+
`tNumber formatted as string`
146+
147+
syntax.
148+
*/
149+
public handler stripZeros(in pString as String) returns String
150+
if "." is in pString then
151+
repeat while ((the last char of pString) is in ".0")
152+
if the last char of pString is "." then
153+
delete the last char of pString
154+
exit repeat
155+
else
156+
delete the last char of pString
157+
end if
158+
end repeat
159+
end if
160+
161+
return pString
162+
end handler
163+
164+
private handler colorComponentToString(in pComponent as Number)
165+
return intToString(the rounded of (pComponent * 255))
166+
end handler
167+
168+
/*
169+
Summary: Converts a color to a string representing the color
170+
171+
Parameters:
172+
pColor: A value of type com.livecode.canvas.Color
173+
pIncludeAlpha: Whether to include the alpha value in the string or not
174+
175+
Example:
176+
property widgetColor get getColor
177+
metadata widgetColor.editor is "com.livecode.pi.colorwithalpha"
178+
179+
private variable mColor as Color
180+
181+
public handler OnSave(out rProperties as Array)
182+
put colorToString(mColor, true) into rProperties["color with alpha"]
183+
end handler
184+
185+
public handler getColor() returns String
186+
-- the editor used for the widgetColor property expects the alpha value to be included
187+
return colorToString(mColor, true)
188+
end handler
189+
190+
Example:
191+
log colorToString(color [0.4,0.4,0.4,0.4]) -- logs "102,102,102,102"
192+
193+
Description:
194+
Use the <colorToString> handler when logging colors, returning them to LiveCode script
195+
via a property getter, or serialising them in the widget's stored properties array for saving.
196+
*/
197+
public handler colorToString(in pColor as Color, in pIncludeAlpha as Boolean) returns String
198+
variable tComponents as List
199+
variable tString as String
200+
put [] into tComponents
201+
push colorComponentToString(the red of pColor) onto tComponents
202+
push colorComponentToString(the green of pColor) onto tComponents
203+
push colorComponentToString(the blue of pColor) onto tComponents
204+
205+
if pIncludeAlpha then
206+
push colorComponentToString(the alpha of pColor) onto tComponents
207+
end if
208+
209+
combine tComponents with "," into tString
210+
return tString
211+
end handler
212+
213+
private handler stringToColorComponent(in pComponent as String)
214+
return (pComponent parsed as number) / 255
215+
end handler
216+
217+
/*
218+
Summary: Converts a string to a color
219+
220+
Parameters:
221+
pColor: A comma delimited string representing a color in RGB / RGBA format
222+
223+
Example:
224+
property widgetColor get getColor set setColor
225+
metadata widgetColor.editor is "com.livecode.pi.colorwithalpha"
226+
227+
private variable mColor as Color
228+
229+
public handler OnLoad(in pProperties as Array)
230+
setColor(pProperties["color"])
231+
end handler
232+
233+
public handler setColor(in pColor as String)
234+
put stringToColor(pColor) into mColor
235+
redraw all
236+
end handler
237+
238+
Description:
239+
Use the <stringToColor> handler when receiving colors from LiveCode script
240+
via a property setter, or from the widget's stored properties array when loading.
241+
*/
242+
public handler stringToColor(in pString as String) returns Color
243+
variable tRed as Real
244+
variable tGreen as Real
245+
variable tBlue as Real
246+
variable tAlpha as Real
247+
248+
variable tComponentList as List
249+
split pString by "," into tComponentList
250+
251+
variable tComponentCount
252+
put the number of elements in tComponentList into tComponentCount
253+
if tComponentCount is not 3 and tComponentCount is not 4 then
254+
// Invalid number of components detected
255+
throw "Invalid color"
256+
end if
257+
258+
variable tColor as List
259+
put [] into tColor
260+
variable tColorComponent as String
261+
repeat for each element tColorComponent in tComponentList
262+
push stringToColorComponent(tColorComponent) onto tColor
263+
end repeat
264+
265+
if tComponentCount is 3 then
266+
push 1 onto tColor
267+
end if
268+
269+
return color tColor
270+
end handler
271+
272+
end module

extensions/widgets/colorswatch/colorswatch.lcb

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ widget com.livecode.widget.colorSwatch
2525
use com.livecode.canvas
2626
use com.livecode.widget
2727
use com.livecode.engine
28+
use com.livecode.widgetutils
29+
2830
--
2931

3032
-- adding metadata to ensure the extension displays correctly in LiveCode
@@ -211,7 +213,7 @@ public handler getColor() returns String
211213
if mColor is nothing then
212214
return ""
213215
else
214-
return colorToString(mColor)
216+
return colorToString(mColor, true)
215217
end if
216218
end handler
217219
----------
@@ -227,62 +229,6 @@ public handler getPaint(pType) returns Paint
227229

228230
return solid paint with stringToColor("255,100,200")
229231
end handler
230-
----------
231-
232-
233-
----------
234-
-- this handler converts a string of numbers to an RGBA color
235-
private handler stringToColor(in pString as String) returns Color
236-
variable tRed as Real
237-
variable tGreen as Real
238-
variable tBlue as Real
239-
variable tAlpha as Real
240-
241-
variable tComponentList as List
242-
split pString by "," into tComponentList
243-
244-
variable tComponentCount
245-
put the number of elements in tComponentList into tComponentCount
246-
if tComponentCount is not 3 and tComponentCount is not 4 then
247-
// Invalid number of components detected
248-
throw "Invalid color"
249-
end if
250-
251-
put (element 1 of tComponentList) parsed as number into tRed
252-
put (element 2 of tComponentList) parsed as number into tGreen
253-
put (element 3 of tComponentList) parsed as number into tBlue
254-
255-
if tComponentCount is 4 then
256-
put (element 4 of tComponentList) parsed as number into tAlpha
257-
else
258-
put 255 into tAlpha
259-
end if
260232

261-
return color [ tRed/255, tGreen/255, tBlue/255, tAlpha/255 ]
262-
end handler
263-
----------
264-
265-
----------
266-
-- this handler converts an RGBA color to a string
267-
private handler colorToString(in pColor as Color) returns String
268-
return stripZeros((the rounded of ((the red of pColor) * 255)) formatted as string) & "," & stripZeros((the rounded of ((the green of pColor) * 255)) formatted as string) & "," & stripZeros((the rounded of ((the blue of pColor) * 255)) formatted as string) & "," & stripZeros((the rounded of ((the alpha of pColor) * 255)) formatted as string)
269-
end handler
270-
----------
271-
272-
-- this handler strips the zeros when an integer is formatted as a string
273-
private handler stripZeros(in pString as String) returns String
274-
if pString contains "." then
275-
repeat while ((the last char of pString) is in ".0")
276-
if the last char of pString is "." then
277-
delete the last char of pString
278-
exit repeat
279-
else
280-
delete the last char of pString
281-
end if
282-
end repeat
283-
end if
284-
return pString
285-
end handler
286-
--
287233

288234
end widget

0 commit comments

Comments
 (0)