forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.lua
More file actions
1400 lines (1230 loc) · 40.5 KB
/
gui.lua
File metadata and controls
1400 lines (1230 loc) · 40.5 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
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Viewscreen implementation utility collection.
local _ENV = mkmodule('gui')
local textures = require('gui.textures')
local utils = require('utils')
local dscreen = dfhack.screen
local getval = utils.getval
---@class dfhack.pen
---@field ch? integer|string
---@field fg? dfhack.color
---@field bg? dfhack.color
---@field bold? boolean
---@field tile? integer|fun(): integer
---@field tile_color? boolean
---@field tile_fg? dfhack.color
---@field tile_bg? dfhack.color
---@field keep_lower? boolean
---@field write_to_lower? boolean
---@field top_of_text? boolean
---@field bottom_of_text? boolean
local to_pen = dfhack.pen.parse
local function getInteriorTexpos()
if not dfhack.internal.getAddress('init') then return end
if dfhack.screen.inGraphicsMode() then
return df.global.init.texpos_border_interior
else
return df.global.init.classic_texpos_border_interior
end
end
CLEAR_PEN = to_pen{tile=getInteriorTexpos(), ch=32, fg=0, bg=0, write_to_lower=true}
TRANSPARENT_PEN = to_pen{tile=0, ch=0}
KEEP_LOWER_PEN = to_pen{ch=32, fg=0, bg=0, keep_lower=true}
local function set_and_get_undo(field, is_set)
local prev_value = df.global.enabler[field]
df.global.enabler[field] = is_set and 1 or 0
return function() df.global.enabler[field] = prev_value end
end
local MOUSE_KEYS = {
_MOUSE_L = curry(set_and_get_undo, 'mouse_lbut'),
_MOUSE_R = curry(set_and_get_undo, 'mouse_rbut'),
_MOUSE_M = curry(set_and_get_undo, 'mouse_mbut'),
_MOUSE_L_DOWN = curry(set_and_get_undo, 'mouse_lbut_down'),
_MOUSE_R_DOWN = curry(set_and_get_undo, 'mouse_rbut_down'),
_MOUSE_M_DOWN = curry(set_and_get_undo, 'mouse_mbut_down'),
}
local FAKE_INPUT_KEYS = copyall(MOUSE_KEYS)
FAKE_INPUT_KEYS._STRING = true
function simulateInput(screen,...)
local keys, enabled_mouse_keys = {}, {}
local function push_key(arg)
local kv = arg
if type(arg) == 'string' then
kv = df.interface_key[arg]
if kv == nil and not FAKE_INPUT_KEYS[arg] then
error('Invalid keycode: '..arg)
end
if MOUSE_KEYS[arg] then
df.global.enabler.tracking_on = 1
enabled_mouse_keys[arg] = true
end
end
if type(kv) == 'number' then
keys[#keys+1] = kv
end
end
for i = 1,select('#',...) do
local arg = select(i,...)
if arg ~= nil then
local t = type(arg)
if type(arg) == 'table' then
for k,v in pairs(arg) do
if v == true then
push_key(k)
elseif k ~= '_STRING' then
push_key(v)
end
end
else
push_key(arg)
end
end
end
local undo_fns = {}
for mk, fn in pairs(MOUSE_KEYS) do
if type(fn) == 'function' then
table.insert(undo_fns, fn(enabled_mouse_keys[mk]))
end
end
dscreen._doSimulateInput(screen, keys)
for _, undo_fn in ipairs(undo_fns) do
undo_fn()
end
end
---@param x1 integer
---@param y1 integer
---@param x2 integer
---@param y2 integer
---@return gui.dimension
function mkdims_xy(x1,y1,x2,y2)
return { x1=x1, y1=y1, x2=x2, y2=y2, width=x2-x1+1, height=y2-y1+1 }
end
---@param x1 integer
---@param y1 integer
---@param w integer
---@param h integer
---@return gui.dimension
function mkdims_wh(x1,y1,w,h)
return { x1=x1, y1=y1, x2=x1+w-1, y2=y1+h-1, width=w, height=h }
end
---@return gui.dimension
function get_interface_rect()
local l, t = 0, 0
local w, h = dscreen.getWindowSize()
local interface_pct = df.global.init.display.max_interface_percentage
if interface_pct < 100 then
local interface_width = math.max(114, w * interface_pct / 100)
l = math.ceil((w - interface_width) / 2)
w = math.floor(interface_width)
end
return mkdims_wh(l, t, w, h)
end
---@return widgets.Widget.frame
function get_interface_frame()
local interface_rect = get_interface_rect()
return {t=0, l=interface_rect.x1, w=interface_rect.width, h=interface_rect.height}
end
---@param rect gui.dimension
---@param x integer
---@param y integer
---@return boolean
function is_in_rect(rect,x,y)
return x and y and x >= rect.x1 and x <= rect.x2 and y >= rect.y1 and y <= rect.y2
end
local function align_coord(gap,align,lv,rv)
if gap <= 0 then
return 0
end
if not align then
if rv and not lv then
align = 1.0
elseif lv and not rv then
align = 0.0
else
align = 0.5
end
end
return math.floor(gap*align)
end
function compute_frame_rect(wavail,havail,spec,xgap,ygap)
if not spec then
return mkdims_wh(0,0,wavail,havail)
end
local sw = wavail - (spec.l or 0) - (spec.r or 0)
local sh = havail - (spec.t or 0) - (spec.b or 0)
local rqw = math.min(sw, (spec.w or sw)+xgap)
local rqh = math.min(sh, (spec.h or sh)+ygap)
local ax = align_coord(sw - rqw, spec.xalign, spec.l, spec.r)
local ay = align_coord(sh - rqh, spec.yalign, spec.t, spec.b)
local rect = mkdims_wh((spec.l or 0) + ax, (spec.t or 0) + ay, rqw, rqh)
rect.wgap = sw - rqw
rect.hgap = sh - rqh
return rect
end
function parse_inset(inset)
local l,r,t,b
if type(inset) == 'table' then
l,r = inset.l or inset.x or 0, inset.r or inset.x or 0
t,b = inset.t or inset.y or 0, inset.b or inset.y or 0
else
l = inset or 0
t,r,b = l,l,l
end
return l,t,r,b
end
function inset_frame(rect, inset, gap)
if not rect then return mkdims_wh(0, 0, 0, 0) end
gap = gap or 0
local l,t,r,b = parse_inset(inset)
return mkdims_xy(rect.x1+l+gap, rect.y1+t+gap, rect.x2-r-gap, rect.y2-b-gap)
end
function compute_frame_body(wavail, havail, spec, inset, gap, inner_frame)
gap = gap or 0
local l,t,r,b = parse_inset(inset)
local xgap,ygap = 0,0
if inner_frame then
xgap,ygap = gap*2+l+r, gap*2+t+b
end
local rect = compute_frame_rect(wavail, havail, spec, xgap, ygap)
local body = mkdims_xy(rect.x1+l+gap, rect.y1+t+gap, rect.x2-r-gap, rect.y2-b-gap)
return rect, body
end
function blink_visible(delay)
return math.floor(dfhack.getTickCount()/delay) % 2 == 0
end
function getKeyDisplay(code)
if type(code) == 'string' then
code = df.interface_key[code]
end
return dscreen.getKeyDisplay(code)
end
-----------------------------------
-- Clipped view rectangle object --
-----------------------------------
---@class gui.dimension
---@field x1 integer
---@field y1 integer
---@field x2 integer
---@field y2 integer
---@field width integer
---@field height integer
---@class gui.ViewRect.attrs
---@field clip_x1 integer
---@field clip_y1 integer
---@field clip_x2 integer
---@field clip_y2 integer
---@field x1 integer
---@field y1 integer
---@field x2 integer
---@field y2 integer
---@field width integer
---@field height integer
---@class gui.ViewRect.attrs.partial: gui.ViewRect.attrs
---@class gui.ViewRect.initTable: gui.ViewRect.attrs.partial
---@field rect? gui.dimension
---@field clip_rect? gui.dimension
---@field view_rect? gui.ViewRect
---@field clip_view? gui.ViewRect
---@class gui.ViewRect: dfhack.class, gui.ViewRect.attrs
---@field super dfhack.class
---@field ATTRS gui.ViewRect.attrs|fun(attributes: gui.ViewRect.attrs.partial)
---@overload fun(init_table: gui.ViewRect.initTable): self
ViewRect = defclass(ViewRect, nil)
---@param self gui.ViewRect
---@param args gui.ViewRect.initTable
function ViewRect:init(args)
if args.view_rect then
self:assign(args.view_rect)
else
local rect = args.rect or mkdims_wh(0,0,dscreen.getWindowSize())
local crect = args.clip_rect or rect
self:assign{
x1 = rect.x1, clip_x1 = crect.x1,
y1 = rect.y1, clip_y1 = crect.y1,
x2 = rect.x2, clip_x2 = crect.x2,
y2 = rect.y2, clip_y2 = crect.y2,
width = rect.x2-rect.x1+1,
height = rect.y2-rect.y1+1,
}
end
if args.clip_view then
local cr = args.clip_view
self:assign{
clip_x1 = math.max(self.clip_x1, cr.clip_x1),
clip_y1 = math.max(self.clip_y1, cr.clip_y1),
clip_x2 = math.min(self.clip_x2, cr.clip_x2),
clip_y2 = math.min(self.clip_y2, cr.clip_y2),
}
end
end
-- Returns true if the clip area is empty, i.e. no painting is possible.
---@return boolean
function ViewRect:isDefunct()
return (self.clip_x1 > self.clip_x2 or self.clip_y1 > self.clip_y2)
end
---@param x integer
---@param y integer
---@return boolean
function ViewRect:inClipGlobalXY(x,y)
return x >= self.clip_x1 and x <= self.clip_x2
and y >= self.clip_y1 and y <= self.clip_y2
end
---@param x integer
---@param y integer
---@return boolean
function ViewRect:inClipLocalXY(x,y)
return (x+self.x1) >= self.clip_x1 and (x+self.x1) <= self.clip_x2
and (y+self.y1) >= self.clip_y1 and (y+self.y1) <= self.clip_y2
end
---@param x integer
---@param y integer
---@return integer x_local
---@return integer y_local
function ViewRect:localXY(x,y)
return x-self.x1, y-self.y1
end
---@param x integer
---@param y integer
---@return integer x_global
---@return integer y_global
function ViewRect:globalXY(x,y)
return x+self.x1, y+self.y1
end
---@nodiscard
---@param x integer
---@param y integer
---@param w integer
---@param h integer
---@return gui.ViewRect
---@overload fun(x: gui.dimension): gui.ViewRect
function ViewRect:viewport(x,y,w,h)
if type(x) == 'table' then
x,y,w,h = x.x1, x.y1, x.width, x.height
end
local x1,y1 = self.x1+x, self.y1+y
local x2,y2 = x1+w-1, y1+h-1
local vp = {
-- Logical viewport
x1 = x1, y1 = y1, x2 = x2, y2 = y2,
width = w, height = h,
-- Actual clipping rect
clip_x1 = math.max(self.clip_x1, x1),
clip_y1 = math.max(self.clip_y1, y1),
clip_x2 = math.min(self.clip_x2, x2),
clip_y2 = math.min(self.clip_y2, y2),
}
return mkinstance(ViewRect, vp)
end
----------------------------
-- Clipped painter object --
----------------------------
---@class gui.Painter.attrs: gui.ViewRect.attrs
---@field cur_pen dfhack.pen
---@field cur_key_pen dfhack.pen
---@field to_map boolean
---@field x integer
---@field y integer
---@class gui.Painter.attrs.partial: gui.Painter.attrs
---@class gui.Painter.initTable: gui.Painter.attrs.partial
---@field pen? dfhack.pen|dfhack.color
---@field key_pen? dfhack.pen|dfhack.color
---@class gui.Painter: gui.ViewRect, gui.Painter.attrs
---@field super gui.ViewRect
---@field ATTRS gui.Painter.attrs|fun(attributes: gui.Painter.attrs.partial)
---@overload fun(attributes: gui.Painter.initTable): self
Painter = defclass(Painter, ViewRect)
---@param self gui.Painter
---@param args gui.Painter.initTable
function Painter:init(args)
self.x = self.x1
self.y = self.y1
self.cur_pen = to_pen(args.pen or COLOR_GREY)
self.cur_key_pen = to_pen(args.key_pen or COLOR_LIGHTGREEN)
self.to_map = false
end
---@param rect gui.dimension
---@param pen dfhack.pen
---@return gui.Painter
function Painter.new(rect, pen)
return Painter{ rect = rect, pen = pen }
end
---@param view_rect gui.dimension
---@param pen dfhack.pen
---@return gui.Painter
function Painter.new_view(view_rect, pen)
return Painter{ view_rect = view_rect, pen = pen }
end
---@param x1 integer
---@param y1 integer
---@param x2 integer
---@param y2 integer
---@param pen integer
---@return gui.Painter
function Painter.new_xy(x1,y1,x2,y2,pen)
return Painter{ rect = mkdims_xy(x1,y1,x2,y2), pen = pen }
end
---@param x integer
---@param y integer
---@param w integer
---@param h integer
---@param pen dfhack.pen
---@return gui.Painter
function Painter.new_wh(x,y,w,h,pen)
return Painter{ rect = mkdims_wh(x,y,w,h), pen = pen }
end
---@return boolean
function Painter:isValidPos()
return self:inClipGlobalXY(self.x, self.y)
end
---@param x integer
---@param y integer
---@param w integer
---@param h integer
---@return gui.Painter
function Painter:viewport(x,y,w,h)
local vp = ViewRect.viewport(self,x,y,w,h)
vp.cur_pen = self.cur_pen
vp.cur_key_pen = self.cur_key_pen
return mkinstance(Painter, vp):seek(0,0)
end
---@return integer x
---@return integer y
function Painter:cursor()
return self.x - self.x1, self.y - self.y1
end
---@return integer
function Painter:cursorX()
return self.x - self.x1
end
---@return integer
function Painter:cursorY()
return self.y - self.y1
end
---@param x? integer
---@param y? integer
---@return self
function Painter:seek(x,y)
if x then self.x = self.x1 + x end
if y then self.y = self.y1 + y end
return self
end
---@param dx? integer
---@param dy? integer
---@return self
function Painter:advance(dx,dy)
if dx then self.x = self.x + dx end
if dy then self.y = self.y + dy end
return self
end
---@param dx? integer
---@return self
function Painter:newline(dx)
self.y = self.y + 1
self.x = self.x1 + (dx or 0)
return self
end
---@param pen dfhack.pen
---@param ... any
---@return self
---@overload fun(pen: dfhack.color, ...: any): self
function Painter:pen(pen,...)
self.cur_pen = to_pen(self.cur_pen, pen, ...)
return self
end
---@param fg dfhack.color
---@param bold? boolean
---@param bg? dfhack.color
---@return self
function Painter:color(fg,bold,bg)
self.cur_pen = to_pen(self.cur_pen, fg, bg, bold)
return self
end
---@param pen dfhack.pen
---@param ... any
---@return self
---@overload fun(pen: dfhack.color, ...: any): self
function Painter:key_pen(pen,...)
self.cur_key_pen = to_pen(self.cur_key_pen, pen, ...)
return self
end
---@param to_map boolean If set to true, the painter will paint to the fortress/adventure map buffer and not the UI buffer.
---@return self
function Painter:map(to_map)
self.to_map = to_map
return self
end
---@return self
function Painter:clear()
dscreen.fillRect(CLEAR_PEN, self.clip_x1, self.clip_y1, self.clip_x2, self.clip_y2)
return self
end
---@param x1 integer
---@param y1 integer
---@param x2 integer
---@param y2 integer
---@param pen dfhack.pen|dfhack.color
---@param bg? dfhack.color
---@param bold? boolean
---@return self
---@overload fun(rect: gui.dimension, pen: dfhack.pen, bg?: dfhack.color, bold?: boolean): self
function Painter:fill(x1,y1,x2,y2,pen,bg,bold)
if type(x1) == 'table' then
x1, y1, x2, y2, pen, bg, bold = x1.x1, x1.y1, x1.x2, x1.y2, y1, x2, y2
end
x1 = math.max(x1+self.x1,self.clip_x1)
y1 = math.max(y1+self.y1,self.clip_y1)
x2 = math.min(x2+self.x1,self.clip_x2)
y2 = math.min(y2+self.y1,self.clip_y2)
dscreen.fillRect(to_pen(self.cur_pen,pen,bg,bold),x1,y1,x2,y2,self.to_map)
return self
end
---@param char? string
---@param pen? dfhack.pen
---@param ... any
---@return self
function Painter:char(char,pen,...)
if self:isValidPos() then
dscreen.paintTile(to_pen(self.cur_pen, pen, ...), self.x, self.y, char, nil, self.to_map)
end
return self:advance(1, nil)
end
---@param char? string
---@param tile? integer
---@param pen? dfhack.pen
---@param ... any
---@return self
function Painter:tile(char,tile,pen,...)
if self:isValidPos() then
dscreen.paintTile(to_pen(self.cur_pen, pen, ...), self.x, self.y, char, tile, self.to_map)
end
return self:advance(1, nil)
end
---@param text string
---@param pen? dfhack.pen
---@param ... any
---@return self
function Painter:string(text,pen,...)
if self.y >= self.clip_y1 and self.y <= self.clip_y2 then
local dx = 0
if self.x < self.clip_x1 then
dx = self.clip_x1 - self.x
end
local len = #text
if self.x + len - 1 > self.clip_x2 then
len = self.clip_x2 - self.x + 1
end
if len > dx then
dscreen.paintString(
to_pen(self.cur_pen, pen, ...),
self.x+dx, self.y,
string.sub(text,dx+1,len),
self.to_map
)
end
end
return self:advance(#text, nil)
end
---@param keycode string
---@param pen? dfhack.pen
---@param ... any
---@return self
function Painter:key(keycode,pen,...)
return self:string(
getKeyDisplay(keycode),
to_pen(self.cur_key_pen, pen, ...)
)
end
---@param keycode string
---@param text string
---@param ... any
---@return self
function Painter:key_string(keycode, text, ...)
return self:key(keycode):string(': '):string(text, ...)
end
--------------------------
-- Abstract view object --
--------------------------
---@class gui.View.focus_group
---@field cur? gui.View[]
---@field [integer] gui.View
---@class gui.View.attrs
---@field subviews gui.View[]
---@field parent_view? gui.View
---@field focus_group gui.View.focus_group
---@field focus boolean
---@field active boolean|fun(): boolean
---@field visible boolean|fun(): boolean
---@field view_id? string
---@field on_focus? function
---@field on_unfocus? function
---@field frame_parent_rect gui.ViewRect
---@field frame_rect gui.ViewRect
---@field frame_body gui.ViewRect
---@class gui.View.attrs.partial: gui.ViewAttrs
---@class gui.View: dfhack.class, gui.View.attrs
---@field super dfhack.class
---@field ATTRS gui.View.attrs|fun(attributes: gui.View.attrs.partial)
---@overload fun(init_table: gui.View.attrs.partial): self
View = defclass(View)
View.ATTRS {
active = true,
visible = true,
view_id = DEFAULT_NIL,
on_focus = DEFAULT_NIL,
on_unfocus = DEFAULT_NIL,
}
function View:init(args)
self.subviews = {}
self.focus_group = {self}
self.focus = false
end
local function inherit_focus_group(view, focus_group)
for _,child in ipairs(view.subviews) do
inherit_focus_group(child, focus_group)
end
view.focus_group = focus_group
end
---@param self gui.View
---@param list gui.View[]
function View:addviews(list)
if not list then return end
local sv = self.subviews
for _,obj in ipairs(list) do
-- absorb the focus groups of new children
for _,focus_obj in ipairs(obj.focus_group) do
table.insert(self.focus_group, focus_obj)
end
-- if the child's focus group has a focus owner, absorb it if we don't
-- already have one. otherwise keep the focus owner we have and clear
-- the focus of the child.
if obj.focus_group.cur then
if not self.focus_group.cur then
self.focus_group.cur = obj.focus_group.cur
else
obj.focus_group.cur:setFocus(false)
end
end
-- overwrite the child's focus_group hierarchy with ours
inherit_focus_group(obj, self.focus_group)
-- if we don't have a focus owner, give it to the new child if they want
if not self.focus_group.cur and obj:getPreferredFocusState() then
obj:setFocus(true)
end
-- set ourselves as the parent view of the new child
obj.parent_view = self
-- add the subview to our child list
table.insert(sv, obj)
local id = obj.view_id
if id and type(id) ~= 'number' and sv[id] == nil then
sv[id] = obj
end
end
for _,dir in ipairs(list) do
for id,obj in pairs(dir.subviews) do
if id and type(id) ~= 'number' and sv[id] == nil then
sv[id] = obj
end
end
end
end
-- should be overridden by widgets that care about capturing keyboard focus
-- (e.g. widgets.EditField)
function View:getPreferredFocusState()
return false
end
---@param self gui.View
---@param focus boolean
function View:setFocus(focus)
if focus then
if self.focus then return end -- nothing to do if we already have focus
if self.focus_group.cur then
-- steal focus from current owner
self.focus_group.cur:setFocus(false)
end
self.focus_group.cur = self
self.focus = true
if self.on_focus then
self.on_focus()
end
elseif self.focus then
self.focus = false
self.focus_group.cur = nil
if self.on_unfocus then
self.on_unfocus()
end
end
end
---@param self gui.View
---@return integer width
---@return integer height
function View:getWindowSize()
local rect = self.frame_body
return rect.width, rect.height
end
---@param self gui.View
---@param view_rect? gui.ViewRect
---@return integer x
---@return integer y
function View:getMousePos(view_rect)
local rect = view_rect or self.frame_body
local x,y = dscreen.getMousePos()
if rect and x and rect:inClipGlobalXY(x,y) then
return rect:localXY(x,y)
end
end
function View:getMouseFramePos()
if not self.frame_rect or not self.frame_parent_rect then return end
return self:getMousePos(ViewRect{
rect=mkdims_wh(
self.frame_rect.x1+self.frame_parent_rect.x1,
self.frame_rect.y1+self.frame_parent_rect.y1,
self.frame_rect.width,
self.frame_rect.height
)
})
end
function View:computeFrame(parent_rect)
return mkdims_wh(0,0,parent_rect.width,parent_rect.height)
end
function View:updateSubviewLayout(frame_body)
for _,child in ipairs(self.subviews) do
child:updateLayout(frame_body)
end
end
function View:updateLayout(parent_rect)
if not parent_rect then
parent_rect = self.frame_parent_rect
else
self.frame_parent_rect = parent_rect
end
self:invoke_before('preUpdateLayout', parent_rect)
local frame_rect,body_rect = self:computeFrame(parent_rect)
self.frame_rect = frame_rect
self.frame_body = parent_rect:viewport(body_rect or frame_rect)
self:invoke_after('postComputeFrame', self.frame_body)
self:updateSubviewLayout(self.frame_body)
self:invoke_after('postUpdateLayout', self.frame_body)
end
function View:renderSubviews(dc)
for _,child in ipairs(self.subviews) do
if getval(child.visible) then
child:render(dc)
end
end
end
function View:render(dc)
self:onRenderFrame(dc, self.frame_rect)
local sub_dc = Painter{
view_rect = self.frame_body,
clip_view = dc
}
self:onRenderBody(sub_dc)
self:renderSubviews(sub_dc)
end
function View:onRenderFrame(dc,rect)
end
function View:onRenderBody(dc)
end
-- Returns whether we should invoke the focus owner's onInput() function from
-- the given view's inputToSubviews() function. That is, returns true if:
-- - the view is not itself the focus owner since that would be an infinite loop
-- - the view is not a descendent of the focus owner (same as above)
-- - the focus owner and all of its ancestors are visible and active, since if
-- the focus owner is not (directly or transitively) visible or active, then
-- it shouldn't be getting input.
local function should_send_input_to_focus_owner(view, focus_owner)
local iter = view
while iter do
if iter == focus_owner then
return false
end
iter = iter.parent_view
end
iter = focus_owner
while iter do
if not getval(iter.visible) or not getval(iter.active) then
return false
end
iter = iter.parent_view
end
return true
end
function View:inputToSubviews(keys)
local children = self.subviews
-- give focus owner first dibs on the input
local focus_owner = self.focus_group.cur
if focus_owner and should_send_input_to_focus_owner(self, focus_owner) and
focus_owner:onInput(keys) then
return true
end
for i=#children,1,-1 do
local child = children[i]
if getval(child.visible) and getval(child.active)
and child ~= focus_owner and child:onInput(keys) then
return true
end
end
return false
end
function View:onInput(keys)
return self:inputToSubviews(keys)
end
------------------------
-- Base screen object --
------------------------
---@class gui.Screen.attrs: gui.View.attrs
---@field text_input_mode boolean
---@field request_full_screen_refresh boolean
---@field focus_path string
---@class gui.Screen.attrs.partial: gui.Screen.attrs
---@class gui.Screen: gui.View, gui.Screen.attrs
---@field super gui.View
---@field ATTRS gui.Screen.attrs|fun(attributes: gui.Screen.attrs.partial)
---@overload fun(init_table: gui.Screen.attrs.partial): self
Screen = defclass(Screen, View)
Screen.text_input_mode = false
Screen.request_full_screen_refresh = false
function Screen:postinit()
self:onResize(dscreen.getWindowSize())
end
Screen.isDismissed = dscreen.isDismissed
---@return boolean
function Screen:isShown()
return self._native ~= nil
end
---@return boolean
function Screen:isActive()
return self:isShown() and not self:isDismissed()
end
function Screen:invalidate()
dscreen.invalidate()
end
function Screen:renderParent()
if self._native and self._native.parent then
self._native.parent:render(dfhack.getTickCount())
else
dscreen.clear()
end
if Screen.request_full_screen_refresh then
df.global.gps.force_full_display_count = 1
Screen.request_full_screen_refresh = false
end
end
function Screen:sendInputToParent(...)
if self._native and self._native.parent then
simulateInput(self._native.parent, ...)
end
end
function Screen:show(parent)
if self._native then
error("This screen is already on display")
end
self:onAboutToShow(parent or dfhack.gui.getCurViewscreen(true))
-- if we're autodetecting the parent, refresh the parent handle in case the
-- original parent has been dismissed by onAboutToShow()'s actions
parent = parent or dfhack.gui.getCurViewscreen(true)
if not dscreen.show(self, parent.child) then
error('Could not show screen')
end
return self
end
function Screen:onAboutToShow(parent)
end
function Screen:onShow()
self:onResize(dscreen.getWindowSize())
end
function Screen:dismiss()
if self._native then
dscreen.dismiss(self)
end
-- don't leave artifacts behind on the parent screen when we disappear
Screen.request_full_screen_refresh = true
end
function Screen:onDismiss()
end
function Screen:onDestroy()
end
function Screen:onResize(w,h)
self:updateLayout(ViewRect{ rect = mkdims_wh(0,0,w,h) })
end
function Screen:onRender()
self:render(Painter.new())
end
-----------------------------
-- Z-order swapping screen --
-----------------------------
DEFAULT_INITIAL_PAUSE = true
---@class gui.ZScreen.attrs
---@field defocusable boolean
---@field initial_pause boolean
---@field defocused boolean
---@field force_pause boolean
---@field pass_pause boolean
---@field pass_movement_keys boolean
---@field pass_mouse_clicks boolean
---@class gui.ZScreen.attrs.partial: gui.ZScreen.attrs
---@class gui.ZScreen.initTable: gui.ZScreen.attrs.partial