-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSeleccionPictogramas.bas
262 lines (220 loc) · 6.82 KB
/
SeleccionPictogramas.bas
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
B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Activity
Version=8
@EndOfDesignText@
' Proyecto desarrollado por Javier Prieto Martínez
' Información, código fuente, documentación, etc. en http://blog.ganso.org/proyectos/pictorario
' Este código fuente se ofrece con una licencia Creative Commons de tipo Reconocimiento-NoComercial-CompartirIgual 3.0 España (CC BY-NC-SA 3.0 ES)
#Region Activity Attributes
#FullScreen: True
#IncludeTitle: False
#End Region
Sub Process_Globals
End Sub
Sub Globals
Private BarraBusqueda As EditText
Private BotonBuscar As Label
Private Buscar As Panel
Private ListadoPictogramas As ScrollView
Private Titulo As Label
Private BotonCancelar As Button
'Texto por defecto para la barra de búsqueda
Dim TextoBuscarPorDefecto="Buscar pictograma por texto" As String
'Máximo número de Líneas y Columnas a mostrar
Dim NumColumnas As Int = 3
Dim NumLineas As Int = 20
'Mosaico de iconos
Dim BotonIcono(NumLineas,NumColumnas) As Button
'Tamaño de cada icono
Dim TamIcono As Int
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("SeleccionarPictograma")
BotonBuscar.SetBackgroundImage(LoadBitmap(File.DirAssets,"buscar.png"))
BarraBusqueda.Text=TextoBuscarPorDefecto
TamIcono=ListadoPictogramas.Width/3
DibujaIconos
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub BotonIcono_Click
Dim BotonPulsado As Button
BotonPulsado=Sender
CallSubDelayed2(ConfigurarSecuencia,"PictogramaElegido",BotonPulsado.Tag)
Activity.Finish
End Sub
Sub BotonBuscar_Click
BuscarTexto
End Sub
Sub BarraBusqueda_FocusChanged (TieneFoco As Boolean)
'Si pulsamos sobre el texto por defecto, lo vacía
If TieneFoco==True And BarraBusqueda.Text==TextoBuscarPorDefecto Then
BarraBusqueda.Text=""
Activity.Invalidate
End If
End Sub
Sub BarraBusqueda_EnterPressed
BuscarTexto
End Sub
Sub BuscarTexto
Dim id(100) As Int
Dim URL(100) As String
'Dim URLencontrada As String
Dim i,x,y As Int
If BarraBusqueda.Text<>TextoBuscarPorDefecto Then 'Comprueba que hemos cambiado el texto por defecto
Dim im As IME
im.Initialize("")
im.HideKeyboard 'Ocultamos el teclado
ProgressDialogShow("Buscando pictogramas")
'Busca en la web de ARASAAC la cadena
Dim Web As HttpJob
Web.Initialize("buscapictogramas",Me)
Dim su As StringUtils
Web.Download("https://api.arasaac.org/api/pictograms/es/search/"&su.EncodeUrl(BarraBusqueda.Text,"UTF8").Replace("+","%20"))
Wait For (Web) JobDone(j As HttpJob)
Dim encontrados As Int = 0
If j.Success Then
Dim jp As JSONParser
jp.Initialize(j.GetString)
Dim PictogramasEncontrados As List = jp.NextArray
For Each Pictograma As Map In PictogramasEncontrados
id(encontrados)=Pictograma.Get("_id")
encontrados=encontrados+1
Next
Else
Msgbox("La búsqueda no ha producido resultados."&CRLF&CRLF&"Pruebe otros términos, y compruebe que la conexión a Internet está activa."&CRLF&CRLF&"Las búsquedas no funcionan si no hay conexión a la red.","NO SE ENCUENTRAN PICTOGRAMAS")
End If
j.Release
Web.Release
ProgressDialogShow("Descargando imágenes")
'Es necesario hacer dos bloques independientes para detectar URLs y luego descargar para que funcione correctamente el anidamiento de WAIT FORs
For i=0 To encontrados-1
If File.Exists(Starter.DirPictogramas,id(i)&".png")==False Then
'Wait For(EncontrarURLporId(id(i))) Complete (URLencontrada As String)
'URL(i)=URLencontrada
'Se descarga directamente la versión de 500px de la URL de pictogramas pregenerados
URL(i)="https://static.arasaac.org/pictograms/"&id(i)&"/"&id(i)&"_500.png"
End If
Next
For i=0 To encontrados-1
If File.Exists(Starter.DirPictogramas,id(i)&".png")==False Then
Wait For (DescargarPictograma(id(i),URL(i))) Complete (Resultado As Int)
End If
Next
ProgressDialogShow("Preparando listado")
For x=0 To NumColumnas-1
For y=0 To NumLineas-1
BotonIcono(y,x).Visible=False 'Oculta todos los botones
Next
Next
x=0
y=0
For i=0 To encontrados-1 'Muestra los nuevos iconos
If y<NumLineas Then
BotonIcono(y,x).SetBackgroundImage(LoadBitmap(File.Combine(File.DirInternal,"/pictogramas"),id(i)&".png"))
BotonIcono(y,x).Tag=id(i)
BotonIcono(y,x).Visible=True
ListadoPictogramas.Panel.Height=(y+1)*TamIcono
x=x+1
If (x=NumColumnas) Then
x=0
y=y+1
End If
End If
Next
ProgressDialogHide
End If
End Sub
Sub DescargarPictograma(id As Int,URL As String) As ResumableSub
'Descarga un pictograma conociendo su ID y su URL
Dim Web3 As HttpJob
Web3.Initialize("bajapictograma",Me)
Web3.Download(URL)
Wait For (Web3) JobDone(j3 As HttpJob)
If j3.Success Then
Dim Fichero As OutputStream
Fichero = File.OpenOutput(Starter.DirPictogramas,id&".png",False)
File.Copy2(Web3.GetInputStream, Fichero)
Fichero.Close
End If
j3.Release
Web3.Release
Return 0
End Sub
'Ya no es necesaria (se accede a static.arasaac.org, ya que todos los pictogramas están siempre pregenerados)
'Sub EncontrarURLporId(id As Int) As ResumableSub
' 'Encuentra la URL del fichero de imagen de un ID determinado
'
' Dim URL As String
' Dim Web2 As HttpJob
'
' Web2.Initialize("encuentaid",Me)
' Web2.Download("https://api.arasaac.org/api/pictograms/"&id&"?url=true")
'
' Wait For (Web2) JobDone(j2 As HttpJob)
' If j2.Success Then
' Dim jp As JSONParser
' jp.Initialize(j2.GetString)
' Dim m As Map
' m=jp.NextObject
' URL=m.Get("image")
' End If
' j2.Release
' Web2.Release
'
' Return URL
'End Sub
Sub BotonCancelar_Click
CallSubDelayed2(ConfigurarSecuencia,"PictogramaElegido",-1) '-1 indica que se ha cancelado
Activity.Finish
End Sub
Sub DibujaIconos
Dim x,y As Int
For x=0 To NumColumnas-1
For y=0 To NumLineas-1
BotonIcono(y,x).Initialize("BotonIcono")
ListadoPictogramas.Panel.AddView(BotonIcono(y,x),x*TamIcono,y*TamIcono,TamIcono,TamIcono)
BotonIcono(y,x).Visible=False
Next
Next
ListadoPictogramas.Panel.Height=0 'Ningún icono visible
RellenarIconos
End Sub
Sub RellenarIconos
'Rellena la vista inicial de iconos
Dim fileList As List
Dim i,x,y,NumFicheros,id As Int
Dim NombreFich As String
fileList = File.ListFiles(Starter.DirPictogramas)
fileList.Sort(False)
NumFicheros=fileList.Size
If NumFicheros>(NumLineas*NumColumnas) Then
NumFicheros=NumLineas*NumColumnas
End If
x=0
y=0
For i=0 To NumFicheros-1
If y<NumLineas Then
NombreFich=fileList.Get(i)
BotonIcono(y,x).SetBackgroundImage(LoadBitmap(Starter.DirPictogramas,NombreFich))
id=Regex.Replace("[^0-9]",NombreFich,"")
BotonIcono(y,x).Tag=id
BotonIcono(y,x).Visible=True
ListadoPictogramas.Panel.Height=(y+1)*TamIcono
x=x+1
If (x=NumColumnas) Then
x=0
y=y+1
End If
End If
Next
End Sub
Sub Activity_KeyPress (KeyCode As Int)
If KeyCode = KeyCodes.KEYCODE_BACK Then 'Al pulsar atrás...
Sleep(0) 'No hace nada
End If
End Sub