forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathandroidbutton.lcb
More file actions
282 lines (221 loc) · 9.51 KB
/
Copy pathandroidbutton.lcb
File metadata and controls
282 lines (221 loc) · 9.51 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
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
/*
Copyright (C) 2017 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/>. */
/**
Name: Android Native Button
Description:
This widget is a native button on Android.
Name: enabled
Syntax:
set the enabled of <widget> to {true | false}
get the enabled of <widget>
Description:
Use the <enabled> property to enable or disable the native button. When
disabled, the button has a greyed out appearance and does not accept
clicks or touches.
*/
widget com.livecode.widget.native.android.button
use com.livecode.foreign
use com.livecode.java
use com.livecode.widget
use com.livecode.canvas
use com.livecode.engine
use com.livecode.library.widgetutils
metadata version is "1.0.0"
metadata author is "LiveCode"
metadata title is "Android Native Button"
metadata svgicon is "M25.34,0H2.1A2.1,2.1,0,0,0,0,2.1V13.19a2.1,2.1,0,0,0,2.1,2.1H25.34a2.1,2.1,0,0,0,2.1-2.1V2.1A2.1,2.1,0,0,0,25.34,0Zm-15,9.49a.76.76,0,0,1-1.51.15.85.85,0,0,1,0-.15q0-1.58,0-3.16a.76.76,0,0,1,1.51-.12.91.91,0,0,1,0,.13q0,.78,0,1.57T10.35,9.49Zm7.08,1.11a.8.8,0,0,1-.4.71.78.78,0,0,1-.42.12h-.49c-.05,0-.05,0-.05.05,0,.54,0,1.08,0,1.62a.76.76,0,0,1-1.51.13.75.75,0,0,1,0-.14q0-.81,0-1.62s0,0,0,0h-.94s0,0,0,0c0,.55,0,1.09,0,1.64a.76.76,0,0,1-1.51.12.76.76,0,0,1,0-.14q0-.81,0-1.62s0,0,0,0h-.5a.82.82,0,0,1-.81-.64.8.8,0,0,1,0-.18q0-2.44,0-4.88s0,0,0,0h6.71s0,0,0,0Q17.43,8.17,17.43,10.6Zm0-5.18H10.68s0,0,0,0a2.93,2.93,0,0,1,.14-.86,3,3,0,0,1,.49-.92,3.27,3.27,0,0,1,.89-.79l.18-.1s0,0,0,0l-.51-.92a.11.11,0,0,1,0-.16.11.11,0,0,1,.16,0l.17.3.35.64s0,0,.06,0a3.5,3.5,0,0,1,.93-.26,3.67,3.67,0,0,1,1.89.24h0c.07,0,.07,0,.1,0l.5-.9a.11.11,0,0,1,.12-.07.11.11,0,0,1,.09.08.12.12,0,0,1,0,.09l-.51.91s0,0,0,.07a3.23,3.23,0,0,1,1.35,1.34,2.91,2.91,0,0,1,.33,1.08c0,.09,0,.17,0,.26S17.43,5.42,17.41,5.42Zm1.82,2.49q0,.79,0,1.59a.76.76,0,0,1-1.51.11.85.85,0,0,1,0-.12q0-1.58,0-3.15a.76.76,0,0,1,1.51-.15.88.88,0,0,1,0,.16Q19.24,7.12,19.24,7.91Zm-3.35-4a.28.28,0,1,1-.28-.28A.29.29,0,0,1,15.89,3.89Zm-3.14,0a.28.28,0,1,1-.28-.28A.28.28,0,0,1,12.75,3.89Z"
/**
Syntax:
set the label of <widget> to <pLabel>
get the label of <widget>
Summary: The label displayed by the button.
Value (string): The string to use as the button label
Example:
set the label of widget "Android Button" to "Click me!"
Description:
The <label> property is the label displayed by the button.
*/
property label get mLabel set SetLabel
metadata label.editor is "com.livecode.pi.string"
metadata label.default is ""
/**
Syntax:
set the labelColor of <widget> to <pLabel>
get the labelColor of <widget>
Summary: The color of the text of the button label
Value (string): A comma delimited string representing a color in RGB / RGBA format
Example:
set the labelColor of widget "Android Button" to "255,0,0,128"
Description:
Use the <labeColor> property to change the color of the label text of
the button.
*/
property labelColor get mColor set SetTextColor
metadata labelColor.label is "Text Color"
metadata labelColor.default is "0,0,0"
metadata labelColor.editor is "com.livecode.pi.colorwithalpha"
metadata labelColor.section is "Colors"
__safe foreign handler _JNI_GetAndroidEngine() returns JObject \
binds to "java:com.runrev.android.Engine>getEngine()Lcom/runrev/android/Engine;!static"
__safe foreign handler _JNI_GetEngineContext(in pEngine as JObject) returns JObject \
binds to "java:android.view.View>getContext()Landroid/content/Context;"
// Handlers for creating and attaching view
__safe foreign handler _JNI_CreateButton(in pContext as JObject) returns JObject \
binds to "java:android.widget.Button>new(Landroid/content/Context;)?ui"
__safe foreign handler _JNI_AddButtonView(in pParentView as JObject, in pChildView as JObject) returns nothing \
binds to "java:android.view.ViewGroup>addView(Landroid/view/View;)V?ui"
// Handlers for adding click listener
handler type ClickCallback(in pView as JObject) returns nothing
__safe foreign handler _JNI_OnClickListener(in pHandler as ClickCallback) returns JObject \
binds to "java:android.view.View$OnClickListener>interface()"
__safe foreign handler _JNI_SetOnClickListener(in pButton as JObject, in pListener as JObject) returns nothing \
binds to "java:android.view.View>setOnClickListener(Landroid/view/View$OnClickListener;)V?ui"
// Property setters
__safe foreign handler _JNI_SetTextViewText(in pView as JObject, in pValue as JString) returns nothing \
binds to "java:android.widget.TextView>setText(Ljava/lang/CharSequence;)V?ui"
__safe foreign handler _JNI_SetTextViewTextColor(in pView as JObject, in pValue as JInt) returns nothing \
binds to "java:android.widget.TextView>setTextColor(I)V?ui"
__safe foreign handler _JNI_GetColorFromARGB(in pA as JInt, in pR as JInt, in pG as JInt, in pB as JInt) returns JInt \
binds to "java:android.graphics.Color>argb(IIII)I!static"
__safe foreign handler _JNI_SetTextViewEnabled(in pView as JObject, in pValue as JBoolean) returns nothing \
binds to "java:android.view.View>setEnabled(Z)V?ui"
private variable mLabel as String
private variable mColor as String
private variable mButton as optional JObject
private variable mOpen as Boolean
private handler IsAndroid() returns Boolean
return the operating system is "android"
end handler
public handler OnCreate()
put "" into mLabel
put "0,0,0" into mColor
end handler
public handler OnSave(out rProperties as Array)
put mLabel into rProperties["label"]
put mColor into rProperties["color"]
end handler
public handler OnLoad(in pProperties as Array)
put pProperties["label"] into mLabel
put pProperties["color"] into mColor
end handler
private handler InitButtonView()
// Create an android button using the Engine Context
variable tEngine as JObject
put _JNI_GetAndroidEngine() into tEngine
variable tContext as JObject
put _JNI_GetEngineContext(tEngine) into tContext
put _JNI_CreateButton(tContext) into mButton
// put my native window into tParent
variable tParent as Pointer
MCWidgetGetMyStackNativeView(tParent)
// wrap the parent pointer
variable tParentObj as JObject
put PointerToJObject(tParent) into tParentObj
// add the view
_JNI_AddButtonView(tParentObj, mButton)
// get the pointer from the view and set the native layer
variable tButtonPointer as Pointer
put PointerFromJObject(mButton) into tButtonPointer
set my native layer to tButtonPointer
// add an OnClick listener
variable tListener as JObject
put _JNI_OnClickListener(ButtonClicked) into tListener
_JNI_SetOnClickListener(mButton, tListener)
// Update label, text size and color, and enabled state
updateProperties()
end handler
private handler FinalizeButtonView()
set my native layer to nothing
put nothing into mButton
end handler
private handler updateProperties()
SetEnabled(my enabled)
SetLabel(mLabel)
SetTextColor(mColor)
end handler
public handler OnOpen()
if IsAndroid() then
InitButtonView()
end if
end handler
public handler OnClose()
if IsAndroid() then
FinalizeButtonView()
end if
end handler
public handler OnParentPropertyChanged()
// Ensure enabled state is up to date
SetEnabled(my enabled)
end handler
public handler ButtonClicked(in pView as JObject) returns nothing
post "mouseUp"
// Wake the engine to deal with the posted signal
MCEngineRunloopBreakWait()
end handler
public handler OnClick()
post "mouseUp"
end handler
public handler OnPaint()
if IsAndroid() then
return
end if
variable tLabel as String
if mLabel is empty then
put my name into tLabel
else
put mLabel into tLabel
end if
paintPlaceholderImage(this canvas, my bounds, placeholderIcon("android"), \
tLabel, my font, stringToColor(mColor))
end handler
handler SetEnabled(in pValue as Boolean)
if mButton is not nothing then
_JNI_SetTextViewEnabled(mButton, pValue)
end if
redraw all
end handler
public handler SetLabel(in pLabel as String)
put pLabel into mLabel
if mButton is not nothing then
_JNI_SetTextViewText(mButton, StringToJString(mLabel))
end if
redraw all
end handler
public handler SetTextColor(in pColor as String)
put pColor into mColor
if mButton is not nothing then
SetStringAndroidColor(pColor)
end if
redraw all
end handler
private handler ColorComponentToInt(in pComponent as Real) returns Integer
multiply pComponent by 255
round pComponent
return pComponent
end handler
private handler SetStringAndroidColor(in pString as String)
variable tColor as Color
put stringToColor(pString) into tColor
variable tA as Integer
variable tR as Integer
variable tG as Integer
variable tB as Integer
put ColorComponentToInt(the alpha of tColor) into tA
put ColorComponentToInt(the red of tColor) into tR
put ColorComponentToInt(the green of tColor) into tG
put ColorComponentToInt(the blue of tColor) into tB
variable tColorInt as Integer
put _JNI_GetColorFromARGB(tA,tR,tG,tB) into tColorInt
_JNI_SetTextViewTextColor(mButton, tColorInt)
end handler
end widget