-
Notifications
You must be signed in to change notification settings - Fork 0
/
gidcomps.lua
522 lines (450 loc) · 15 KB
/
gidcomps.lua
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
--[[
This code is MIT licensed, see https://opensource.org/licenses/MIT
Copyright 2018 Amnon David
]]
-----------------------------------------------------------------------------
function dumpTable(table, depth)
depth = depth or 1
if not table then
return 'nil'
end
for k,v in pairs(table) do
if (type(v) == "table") then
print(string.rep(" ", depth)..k..":")
dumpTable(v, depth+1)
else
print(string.rep(" ", depth)..k..": ",v)
end
end
end
local function FontCache()
local public = {}
local font_cache = {}
local font_cache_size = 0
public.getFont = function(font_filename, font_size)
if not font_cache[font_filename] then
font_cache[font_filename] = {}
end
if not font_cache[font_filename][font_size] then
font_cache[font_filename][font_size] = TTFont.new(font_filename, font_size)
font_cache_size = font_cache_size + 1
print("font_cache_size="..font_cache_size)
end
return font_cache[font_filename][font_size]
end
return public
end
if not g_fontcache then
g_fontcache = FontCache()
end
function RButton(text, ax, ay, w, h, params)
local f = 0.42
local linew = 2
local fillc = 'ffff44'
local linec = '000000'
local textc = '000000'
local focus_fillc = '2222aa'
local focus_linec = 'aaffaa'
local focus_textc = 'aaffaa'
local font_file = nil
local priv = {}
local text_offset_x = 0
local text_offset_y = 0
priv.str2col = function(str)
local def = '000000'
if not str then
str = def
end
if #str ~= 6 then
str = def
end
local res = tonumber(str, 16)
if not res then
res = tonumber(def, 16)
end
return res
end
if params then
f = params.roundness or f
linew = params.line_width or linew
fillc = params.fill_color or fillc
linec = params.line_color or linec
textc = params.text_color or textc
focus_fillc = params.focus_fill_color or focus_fillc
focus_linec = params.focus_line_color or focus_linec
focus_textc = params.focus_text_color or focus_textc
font_file = params.font_file
text_offset_x = params.text_offset_x or 0
text_offset_y = params.text_offset_y or 0
end
local fill_color = priv.str2col(fillc)
local line_color = priv.str2col(linec)
local text_color = priv.str2col(textc)
local focus_fillcol = priv.str2col(focus_fillc)
local focus_linecol = priv.str2col(focus_linec)
local focus_textcol = priv.str2col(focus_textc)
local focus = false
local textfield = nil
local handler = nil
local context = nil
local fixedfont = false
local myShape = Shape.new()
priv.drawButton = function(fc, lc)
myShape:clear()
myShape:setFillStyle(Shape.SOLID, fc)
myShape:setLineStyle(linew, lc)
myShape:beginPath()
local r = f*h/2
local r1x = ax + w/2 - r
local r1y = ay - h/2 + r
local step = math.pi / 52
myShape:moveTo(r1x,r1y-r)
for a = -math.pi/2, 0, step do
local x = r1x + r * math.cos(a)
local y = r1y + r * math.sin(a)
myShape:lineTo(x,y)
end
local r2x = ax + w/2 - r
local r2y = ay + h/2 - r
myShape:lineTo(r2x+r,r2y)
for a = 0, math.pi/2, step do
local x = r2x + r * math.cos(a)
local y = r2y + r * math.sin(a)
myShape:lineTo(x,y)
end
local r3x = ax - w/2 + r
local r3y = ay + h/2 - r
myShape:lineTo(r3x,r3y+r)
for a = math.pi/2, math.pi, step do
local x = r3x + r * math.cos(a)
local y = r3y + r * math.sin(a)
myShape:lineTo(x,y)
end
local r4x = ax - w/2 + r
local r4y = ay - h/2 + r
myShape:lineTo(r4x-r,r4y)
for a = math.pi, math.pi*1.5, step do
local x = r4x + r * math.cos(a)
local y = r4y + r * math.sin(a)
myShape:lineTo(x,y)
end
myShape:lineTo(r1x,r1y-r)
myShape:endPath()
end
priv.drawButton(fill_color, line_color)
local font_size = nil
local padding = 0.08*w -- Make total x-padding 8 percent of width
local padx = 0
local pady = 0
local save_lineh = 0
if params.font_size then
fixedfont = true
font_size = params.font_size
local font = g_fontcache.getFont(font_file, font_size)
local tf = TextField.new(font, text)
local lineh = tf:getHeight()
local linew = tf:getWidth()
padx = w - linew
pady = h - lineh
save_lineh = lineh
else
for size=8,60 do
font_size = size
local font = g_fontcache.getFont(font_file, font_size)
local tf = TextField.new(font, text)
local lineh = tf:getHeight()
local linew = tf:getWidth()
if lineh > h-padding or linew > w-padding then
break
end
padx = w - linew
pady = h - lineh
save_lineh = lineh
end
-- TTFont.new exhausts open file handles before garbage is collected resulting in
-- errors of type: "Vera.ttf: No such file or directory.", a message which has little
-- relation to the actual problem...
collectgarbage("collect")
font_size = font_size - 1
end
myShape.drawText = function()
local whichcol = text_color
if focus then
whichcol = focus_textcol
end
if textfield then
myShape:removeChild(textfield)
end
local font = g_fontcache.getFont(font_file, font_size)
textfield = TextField.new(font, text)
myShape:addChild(textfield)
textfield:setTextColor(whichcol)
local textposx = ax - w/2 + padx/2 + text_offset_x
local textposy = ay + save_lineh/2 + text_offset_y
textfield:setPosition(textposx, textposy)
--textfield:setLayout({flags = FontBase.TLF_LEFT})
end
myShape.getFontSize = function()
return font_size
end
myShape.getText = function()
return text
end
myShape.updateFocusColors = function()
if focus then
priv.drawButton(focus_fillcol, focus_linecol)
else
priv.drawButton(fill_color, line_color)
end
myShape.drawText()
end
myShape.setFontSize = function(size)
if fixedfont then
return
end
local font = g_fontcache.getFont(font_file, size)
local tf = TextField.new(font, text)
local lineh = tf:getHeight()
local linew = tf:getWidth()
if lineh > h-padding or linew > w-padding then
print("Font too large")
return false
end
padx = w - linew
pady = h - lineh
save_lineh = lineh
font_size = size
end
myShape.setHandler = function( func, ctx )
if type(func) ~= "function" then
return
end
handler = func
context = ctx
end
function myShape:onMouseDown(event)
if myShape:hitTestPoint(event.x, event.y) then
focus = true
myShape.updateFocusColors()
event:stopPropagation()
end
end
function myShape:onMouseUp(event)
if not focus then
return
end
if myShape:hitTestPoint(event.x, event.y) then
focus = false
myShape.updateFocusColors()
if handler then
handler(context)
end
event:stopPropagation()
end
end
function myShape:onMouseMove(event)
if not focus then
return
end
if not myShape:hitTestPoint(event.x, event.y) then
focus = false
myShape.updateFocusColors()
end
event:stopPropagation()
end
myShape:addEventListener(Event.MOUSE_DOWN, myShape.onMouseDown, myShape)
myShape:addEventListener(Event.MOUSE_MOVE, myShape.onMouseMove, myShape)
myShape:addEventListener(Event.MOUSE_UP, myShape.onMouseUp, myShape)
return myShape
end
-----------------------------------------------------------------------------
function ButtonGrid(width, height, rows, cols, padding)
local priv = {}
local public = {}
local grid = {}
local buttons = {}
local handler = nil
local font_file = nil
priv.makegrid = function(rows, cols)
for i = 1, rows do
grid[i] = {}
for j = 1, cols do
grid[i][j] = {}
end
end
end
priv.makegrid(rows, cols)
priv.btnEvtHandler = function(context)
if not handler then
print("no handler set for button event")
return
end
handler(context)
end
public.setBtnParams = function( params )
btn_params = params
end
public.setHandler = function( func )
if type(func) ~= "function" then
return
end
handler = func
end
public.addText = function(row, col, text, params)
params = params or {}
params.btn_params = params.btn_params or {}
bparams = params.btn_params
bparams.fill_color = bparams.fill_color or 'ffffff'
bparams.line_color = bparams.line_color or 'ffffff'
bparams.text_color = bparams.text_color or '000000'
bparams.focus_fill_color = bparams.fill_color
bparams.focus_line_color = bparams.line_color
bparams.focus_text_color = bparams.text_color
bparams.font_file = 'Vera.ttf'
public.addButton(row, col, text, nil, params)
end
-- Add a button to the grid
-- row: Which row in the grid the button should be placed in
-- col: Which column in the grid the button should be placed in
-- text: Text of the button
-- btnCallback: function to be called when thi specific button is clicked
-- params (optional): A table of parameters
-- btn_params: A table of button parameters. Same structure as that passed to RButton
-- disp_params: Position parameters
-- xspan: how many cells should button cover on x axis (default is 1)
-- yspan: how many cells should button cover on y axis (default is 1)
-- optfont_group: a string identifying the group of fonts this texts size should sync with
public.addButton = function(row, col, text, btnCallback, params)
local xspan = 1
local yspan = 1
local optfont_group = '___'
local bparams = btn_params
if params then
bparams = params.btn_params or {}
-- save original properties that were not set
if btn_params then
for k,v in pairs(btn_params) do
if not bparams[k] then
bparams[k] = v
end
end
end
if params.disp_params then
local dp = params.disp_params
xspan = dp.xspan or xspan
yspan = dp.yspan or yspan
optfont_group = dp.optfont_group or optfont_group
end
end
btnCallback = btnCallback or priv.btnEvtHandler
if row > #grid then
return false
end
local grow = grid[row]
if col > #grow then
return false
end
local xleft = cols - col + 1
local yleft = rows - row + 1
if xspan < 1 then xspan = 1 end
if yspan < 1 then yspan = 1 end
if xspan > xleft then xspan = xleft end
if yspan > yleft then yspan = yleft end
local cellx = width / cols
local celly = height / rows
local btn_w = xspan * cellx
local btn_h = yspan * celly
local x = (col-1) * cellx + btn_w/2
local y = (row-1) * celly + btn_h/2
local btn = RButton(text, x, y, btn_w-padding*cellx, btn_h-padding*celly, bparams)
btn.optfont_group = optfont_group
btn.setHandler(btnCallback, {text=text, row=row, col=col})
table.insert(buttons, btn)
end
public.render = function(parent)
priv.parent = parent
if #buttons < 1 then
return
end
local optfont_group = buttons[1].optfont_group
local max_for_group = {}
max_for_group[optfont_group] = buttons[1].getFontSize()
for i=2,#buttons do
local btn = buttons[i]
local fs = btn.getFontSize()
optfont_group = btn.optfont_group
if not max_for_group[optfont_group] then
max_for_group[optfont_group] = fs
elseif max_for_group[optfont_group] > fs then
max_for_group[optfont_group] = fs
end
end
for i=1,#buttons do
local btn = buttons[i]
btn.setFontSize(max_for_group[btn.optfont_group])
parent:addChild(btn)
btn.drawText()
print('text:"'..btn.getText()..'" fontsize:'..btn.getFontSize())
end
end
public.clear = function()
for i=1,#buttons do
priv.parent:removeChild(buttons[i])
buttons[i] = nil
end
end
return public
end
-----------------------------------------------------------------------------
local g_view_manager = nil
function ViewManager()
if g_view_manager then
return g_view_manager
end
local private = {}
local public = {} -- interface seen by creators of ViewManager
local views = {}
private.leave = function(from, whereTo, params)
vfrom = views[from]
vfrom.view.onLeave(vfrom.vstage)
stage:removeChild(vfrom.vstage)
vto = views[whereTo]
if not vto then
print( "Error: "..whereTo.." view is nil. Check if added via addView." )
return
end
stage:addChild(vto.vstage)
vto.view.onStart(vto.vstage, params)
end
private.View = function(name)
local i_view = {} -- interface seen by users of View
i_view.onStart = function()
print( "Error: view "..name.." did not define onStart function")
end
i_view.onLeave = function()
print( "view "..name.." did not define onLeave function")
end
i_view.leave = function(whereTo, params)
private.leave(name, whereTo, params)
end
return i_view
end
public.addView = function(viewname)
local view = private.View(viewname, private)
views[viewname] = {view = view, vstage = Sprite.new()}
return view
end
public.start = function(viewname)
local viewinfo = views[viewname]
if viewinfo == nil then
print("Error: No view associated with "..viewname)
return
end
stage:addChild(viewinfo.vstage)
viewinfo.view.onStart(viewinfo.vstage, nil)
end
print("creating new g_view_manager")
g_view_manager = public
return public
end
-----------------------------------------------------------------------------