This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
/
screen.lua
553 lines (442 loc) · 12.8 KB
/
screen.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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
stdout = print
function pprint(...)
stdout(...)
local out = ""
for _,v in ipairs({...}) do
out = out .. (_==1 and "" or " ") .. tostring(v)
end
var.store("print", out)
end
function Pr(n, d, s, ex)
local nc = tonumber(n)
if nc and nc<math.abs(nc) then
return s-ex-(type(n)== "number" and math.abs(n) or (.01*s*math.abs(nc)))
else
return (type(n)=="number" and n or (type(n)=="string" and .01*s*nc or d))
end
end
-- Apply an extension on a class, and return our new frankenstein
function addExtension(oldclass, extension)
local newclass = class(oldclass)
for key, data in pairs(extension) do
newclass[key] = data
end
return newclass
end
clipRectData = {}
function gc_clipRect(gc, what, x, y, w, h)
if what == "set" and clipRectData.current then
clipRectData.old = clipRectData.current
elseif what == "subset" and clipRectData.current then
clipRectData.old = clipRectData.current
x = clipRectData.old.x<x and x or clipRectData.old.x
y = clipRectData.old.y<y and y or clipRectData.old.y
h = clipRectData.old.y+clipRectData.old.h > y+h and h or clipRectData.old.y+clipRectData.old.h-y
w = clipRectData.old.x+clipRectData.old.w > x+w and w or clipRectData.old.x+clipRectData.old.w-x
what = "set"
elseif what == "restore" and clipRectData.old then
--gc:clipRect("reset")
what = "set"
x = clipRectData.old.x
y = clipRectData.old.y
h = clipRectData.old.h
w = clipRectData.old.w
elseif what == "restore" then
what = "reset"
end
gc:clipRect(what, x, y, w, h)
if x and y and w and h then clipRectData.current = {x=x,y=y,w=w,h=h} end
end
------------------------------------------------------------------
-- Screen Class --
------------------------------------------------------------------
Screen = class(Object)
Screens = {}
function scrollScreen(screen, d, callback)
-- print("scrollScreen. number of screens : ", #Screens)
local dir = d or 1
screen.x=dir*kXSize
screen:Animate( {x=0}, 10, callback )
end
function insertScreen(screen, ...)
-- print("insertScreen")
screen:size()
if current_screen() ~= DummyScreen then
current_screen():screenLoseFocus()
local coeff = pushFromBack and 1 or -1
current_screen():Animate( {x=coeff*kXSize}, 10)
end
table.insert(Screens, screen)
platform.window:invalidate()
current_screen():pushed(...)
end
function insertScreen_direct(screen, ...)
-- print("insertScreen_direct")
screen:size()
table.insert(Screens, screen)
platform.window:invalidate()
current_screen():pushed(...)
end
function push_screen(screen, ...)
--print("push_screen")
local args = ...
local theScreen = current_screen()
pushFromBack = false
insertScreen(screen, ...)
scrollScreen(screen, 1, function() remove_screen_previous(theScreen) end)
end
function push_screen_back(screen, ...)
--print("push_screen_back")
local theScreen = current_screen()
pushFromBack = true
insertScreen(screen, ...)
scrollScreen(screen, -1, function() remove_screen_previous(theScreen) end)
end
function push_screen_direct(screen, ...)
-- print("push_screen_direct")
table.insert(Screens, screen)
platform.window:invalidate()
current_screen():pushed(...)
end
function only_screen(screen, ...)
-- print("only_screen")
remove_screen(current_screen())
Screens = {}
push_screen(screen, ...)
platform.window:invalidate()
end
function only_screen_back(screen, ...)
-- print("only_screen_back")
--Screens = {}
push_screen_back(screen, ...)
platform.window:invalidate()
end
function remove_screen_previous(...)
-- print("remove_screen_previous")
platform.window:invalidate()
current_screen():removed(...)
res=table.remove(Screens, #Screens-1)
current_screen():screenGetFocus()
return res
end
function remove_screen(...)
-- print("remove_screen")
platform.window:invalidate()
current_screen():removed(...)
res=table.remove(Screens)
current_screen():screenGetFocus()
return res
end
function current_screen()
return Screens[#Screens] or DummyScreen
end
function Screen:init(xx,yy,ww,hh)
self.yy = yy
self.xx = xx
self.hh = hh
self.ww = ww
self:ext()
self:size(0)
Object.init(self, self.x, self.y, self.w, self.h, 0)
end
function Screen:ext()
end
function Screen:size()
local screenH = platform.window:height()
local screenW = platform.window:width()
if screenH == 0 then screenH=212 end
if screenW == 0 then screenW=318 end
self.x = math.floor(Pr(self.xx, 0, screenW)+.5)
self.y = math.floor(Pr(self.yy, 0, screenH)+.5)
self.w = math.floor(Pr(self.ww, screenW, screenW, 0)+.5)
self.h = math.floor(Pr(self.hh, screenH, screenH, 0)+.5)
end
function Screen:pushed() end
function Screen:removed() end
function Screen:screenLoseFocus() end
function Screen:screenGetFocus() end
function Screen:draw(gc)
--self:size()
self:paint(gc)
end
function Screen:paint(gc) end
function Screen:invalidate()
platform.window:invalidate(self.x ,self.y , self.w, self.h)
end
function Screen:arrowKey() end
function Screen:enterKey() end
function Screen:backspaceKey() end
function Screen:clearKey() end
function Screen:escapeKey() end
function Screen:tabKey() end
function Screen:backtabKey() end
function Screen:charIn(char) end
function Screen:mouseDown() end
function Screen:mouseUp() end
function Screen:mouseMove() end
function Screen:contextMenu() end
function Screen:appended() end
function Screen:resize(w,h) end
function Screen:destroy()
self = nil
end
------------------------------------------------------------------
-- WidgetManager Extension --
------------------------------------------------------------------
WidgetManager = {}
function WidgetManager:ext()
self.widgets = {}
self.focus = 0
end
function WidgetManager:resize(w,h)
if self.x then --already inited
self:size()
end
end
function WidgetManager:appendWidget(widget, xx, yy)
widget.xx = xx
widget.yy = yy
widget.parent = self
widget:size()
table.insert(self.widgets, widget)
widget.pid = #self.widgets
widget:appended(self)
return self
end
function WidgetManager:getWidget()
return self.widgets[self.focus]
end
function WidgetManager:drawWidgets(gc)
for _, widget in pairs(self.widgets) do
widget:size()
widget:draw(gc)
gc:setColorRGB(0,0,0)
end
end
function WidgetManager:postPaint(gc)
end
function WidgetManager:draw(gc)
--self:size()
self:paint(gc)
self:drawWidgets(gc)
self:postPaint(gc)
end
function WidgetManager:loop(n) end
function WidgetManager:stealFocus(n)
local oldfocus=self.focus
if oldfocus~=0 then
local veto = self:getWidget():loseFocus(n)
if veto == -1 then
return -1, oldfocus
end
self:getWidget().hasFocus = false
self.focus = 0
end
return 0, oldfocus
end
function WidgetManager:focusChange() end
function WidgetManager:switchFocus(n, b)
if n~=0 and #self.widgets>0 then
local veto, focus = self:stealFocus(n)
if veto == -1 then
return -1
end
local looped
self.focus = focus + n
if self.focus>#self.widgets then
self.focus = 1
looped = true
elseif self.focus<1 then
self.focus = #self.widgets
looped = true
end
if looped and self.noloop and not b then
self.focus = focus
self:loop(n)
else
self:getWidget().hasFocus = true
self:getWidget():getFocus(n)
end
end
self:focusChange()
end
function WidgetManager:arrowKey(arrow)
if self.focus~=0 then
self:getWidget():arrowKey(arrow)
end
self:invalidate()
end
function WidgetManager:enterKey()
if self.focus~=0 then
self:getWidget():enterKey()
else
if self.widgets and self.widgets[1] then -- ugh, quite a bad hack for the mouseUp at (0,0) when cursor isn't shown (grrr TI) :/
self.widgets[1]:enterKey()
end
end
self:invalidate()
end
function WidgetManager:clearKey()
if self.focus~=0 then
self:getWidget():clearKey()
end
self:invalidate()
end
function WidgetManager:backspaceKey()
if self.focus~=0 then
self:getWidget():backspaceKey()
end
self:invalidate()
end
function WidgetManager:escapeKey()
if self.focus~=0 then
self:getWidget():escapeKey()
end
self:invalidate()
end
function WidgetManager:tabKey()
self:switchFocus(1)
self:invalidate()
end
function WidgetManager:backtabKey()
self:switchFocus(-1)
self:invalidate()
end
function WidgetManager:charIn(char)
if self.focus~=0 then
self:getWidget():charIn(char)
end
self:invalidate()
end
function WidgetManager:getWidgetIn(x, y)
for n, widget in pairs(self.widgets) do
local wox = widget.ox or 0
local woy = widget.oy or 0
if x>=widget.x-wox and y>=widget.y-wox and x<widget.x+widget.w-wox and y<widget.y+widget.h-woy then
return n, widget
end
end
end
function WidgetManager:mouseDown(x, y)
local n, widget = self:getWidgetIn(x, y)
if n then
if self.focus~=0 and self.focus~=n then self:getWidget().hasFocus = false self:getWidget():loseFocus() end
self.focus = n
widget.hasFocus = true
widget:getFocus()
widget:mouseDown(x, y)
self:focusChange()
else
if self.focus~=0 then self:getWidget().hasFocus = false self:getWidget():loseFocus() end
self.focus = 0
end
end
function WidgetManager:mouseUp(x, y)
if self.focus~=0 then
--self:getWidget():mouseUp(x, y)
end
for _, widget in pairs(self.widgets) do
widget:mouseUp(x,y) -- well, mouseUp is a release of a button, so everything previously "clicked" should be released, for every widget, even if the mouse has moved (and thus changed widget)
-- eventually, a better way for this would be to keep track of the last widget active and do it to this one only...
end
self:invalidate()
end
function WidgetManager:mouseMove(x, y)
if self.focus~=0 then
self:getWidget():mouseMove(x, y)
end
end
--------------------------
-- Our new frankenstein --
--------------------------
WScreen = addExtension(Screen, WidgetManager)
--Dialog screen
Dialog = class(WScreen)
function Dialog:init(title,xx,yy,ww,hh)
self.yy = yy
self.xx = xx
self.hh = hh
self.ww = ww
self.title = title
self:size()
self.widgets = {}
self.focus = 0
end
function Dialog:paint(gc)
self.xx = (pww()-self.w)/2
self.yy = (pwh()-self.h)/2
self.x, self.y = self.xx, self.yy
gc:setFont("sansserif","r",10)
gc:setColorRGB(224,224,224)
gc:fillRect(self.x, self.y, self.w, self.h)
for i=1, 14,2 do
gc:setColorRGB(32+i*3, 32+i*4, 32+i*3)
gc:fillRect(self.x, self.y+i, self.w,2)
end
gc:setColorRGB(32+16*3, 32+16*4, 32+16*3)
gc:fillRect(self.x, self.y+15, self.w, 10)
gc:setColorRGB(128,128,128)
gc:drawRect(self.x, self.y, self.w, self.h)
gc:drawRect(self.x-1, self.y-1, self.w+2, self.h+2)
gc:setColorRGB(96,100,96)
gc:fillRect(self.x+self.w+1, self.y, 1, self.h+2)
gc:fillRect(self.x, self.y+self.h+2, self.w+3, 1)
gc:setColorRGB(104,108,104)
gc:fillRect(self.x+self.w+2, self.y+1, 1, self.h+2)
gc:fillRect(self.x+1, self.y+self.h+3, self.w+3, 1)
gc:fillRect(self.x+self.w+3, self.y+2, 1, self.h+2)
gc:fillRect(self.x+2, self.y+self.h+4, self.w+2, 1)
gc:setColorRGB(255,255,255)
gc:drawString(self.title, self.x + 4, self.y+2, "top")
self:postPaint(gc)
end
function Dialog:postPaint() end
---
-- The dummy screen
---
DummyScreen = Screen()
------------------------------------------------------------------
-- Bindings to the on events --
------------------------------------------------------------------
function on.paint(gc)
allWentWell, generalErrMsg = pcall(onpaint, gc)
if not allWentWell and errorHandler then
errorHandler.display = true
errorHandler.errorMessage = generalErrMsg
end
if platform.hw and platform.hw() < 4 and not doNotDisplayIcon then
platform.withGC(on.draw)
end
end
function onpaint(gc)
for _, screen in pairs(Screens) do
screen:draw(gc)
end
if errorHandler.display then
errorPopup(gc)
end
end
function on.resize(w, h)
-- Global Ratio Constants for On-Calc (shouldn't be used often though...)
kXRatio = w/320
kYRatio = h/212
kXSize, kYSize = w, h
for _,screen in pairs(Screens) do
screen:resize(w,h)
end
-- No better place?
toolpalette.enableCopy(true)
toolpalette.enablePaste(true)
end
function on.arrowKey(arrw) current_screen():arrowKey(arrw) end
function on.enterKey() current_screen():enterKey() end
function on.escapeKey() current_screen():escapeKey() end
function on.tabKey() current_screen():tabKey() end
function on.backtabKey() current_screen():backtabKey() end
function on.charIn(ch) current_screen():charIn(ch) end
function on.backspaceKey() current_screen():backspaceKey() end
function on.contextMenu() current_screen():contextMenu() end
function on.mouseDown(x,y) current_screen():mouseDown(x,y) end
function on.mouseUp(x,y) if (x == 0 and y == 0) then current_screen():enterKey() else current_screen():mouseUp(x,y) end end
function on.mouseMove(x,y) current_screen():mouseMove(x,y) end
function on.clearKey() current_screen():clearKey() end