forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtiletypes.lua
More file actions
1792 lines (1608 loc) · 63.9 KB
/
tiletypes.lua
File metadata and controls
1792 lines (1608 loc) · 63.9 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
--@ module = true
local argparse = require('argparse')
local gui = require('gui')
local guidm = require('gui.dwarfmode')
local plugin = require('plugins.tiletypes')
local textures = require('gui.textures')
local utils = require('utils')
local widgets = require('gui.widgets')
local UI_AREA = {r=2, t=18, w=38, h=31}
local POPUP_UI_AREA = {r=41, t=18, w=30, h=22}
local CONFIG_BUTTON = {
{ tile= dfhack.pen.parse{fg=COLOR_CYAN, tile=curry(textures.tp_control_panel, 7) or nil, ch=string.byte('[')} },
{ tile= dfhack.pen.parse{tile=curry(textures.tp_control_panel, 10) or nil, ch=15} }, -- gear/masterwork symbol
{ tile= dfhack.pen.parse{fg=COLOR_CYAN, tile=curry(textures.tp_control_panel, 8) or nil, ch=string.byte(']')} }
}
local OTHER_LABEL_FORMAT = { first= string.char(15).."(", last= ")"}
local UI_COLORS = {
SELECTED= COLOR_GREEN,
SELECTED2= COLOR_CYAN,
SELECTED_BORDER= COLOR_LIGHTGREEN,
DESELECTED= COLOR_GRAY,
DESELECTED2= COLOR_DARKGRAY,
DESELECTED_BORDER= COLOR_GRAY,
HIGHLIGHTED= COLOR_WHITE,
HIGHLIGHTED2= COLOR_DARKGRAY,
HIGHLIGHTED_BORDER= COLOR_YELLOW,
VALUE_NONE= COLOR_GRAY,
VALUE= COLOR_YELLOW,
VALID_OPTION= COLOR_WHITE,
INVALID_OPTION= COLOR_RED,
}
local TILESET = dfhack.textures.loadTileset(dfhack.getHackPath()..'/data/art/tiletypes.png', 8, 12, true)
local TILESET_STRIDE = 16
local DEFAULT_OPTIONS = {
{ value= -1, char1= " ", char2= " ", offset= 97, pen = COLOR_GRAY },
{ value= 1, char1= "+", char2= "+", offset= 101, pen = COLOR_LIGHTGREEN },
{ value= 0, char1= "X", char2= "X", offset= 105, pen = COLOR_RED },
}
local MORE_OPTIONS = {
{ key= "hidden", label= "Hidden", values= DEFAULT_OPTIONS },
{ key= "light", label= "Light", values= DEFAULT_OPTIONS },
{ key= "subterranean", label= "Subterranean", values= DEFAULT_OPTIONS },
{ key= "skyview", label= "Skyview", values= DEFAULT_OPTIONS },
{ key= "aquifer", label= "Aquifer", values= {
{ value= -1, char1= " ", char2= " ", offset= 97, pen = COLOR_GRAY },
{ value= 1, char1= 247, char2= " ", offset= 109, pen = COLOR_LIGHTBLUE },
{ value= 2, char1= 247, char2= 247, offset= 157, pen = COLOR_BLUE },
{ value= 0, char1= "X", char2= "X", offset= 105, pen = COLOR_RED },
}
},
{ key= "autocorrect", label= "Autocorrect", values= {
{ value= 1, char1= "+", char2= "+", offset= 101, pen = COLOR_LIGHTGREEN },
{ value= 0, char1= "X", char2= "X", offset= 105, pen = COLOR_RED },
}
},
}
local MODE_LIST = {
{ label= "Paint" , value= "paint" , pen= COLOR_YELLOW },
{ label= "Replace" , value= "replace", pen= COLOR_LIGHTGREEN },
{ label= "Fill" , value= "fill" , pen= COLOR_GREEN },
{ label= "Remove" , value= "remove" , pen= COLOR_RED },
}
function isEmptyTile(pos)
if pos and dfhack.maps.isValidTilePos(pos) then
local tiletype = dfhack.maps.getTileType(pos)
return tiletype and (
df.tiletype.attrs[tiletype].shape == df.tiletype_shape.NONE
or df.tiletype.attrs[tiletype].material == df.tiletype_material.AIR
)
end
return true
end
local MODE_SETTINGS = {
["paint"] = {
idx= 1, config= true , char1= 219, char2= 219, offset= 1, selected_offset = 49,
description= "Paint tiles",
validator= function(pos) return true end
},
["replace"] = {
idx= 2, config= true , char1= 8, char2= 7, offset= 5, selected_offset = 53,
description= "Replace non-empty tiles",
validator= function(pos) return not isEmptyTile(pos) end
},
["fill"] = {
idx= 3, config= true , char1= 7, char2= 8, offset= 9, selected_offset = 57,
description= "Fill in empty tiles",
validator= function(pos) return isEmptyTile(pos) end
},
["remove"] = {
idx= 4, config= false, char1= 177, char2= 177, offset= 13, selected_offset = 61,
description= "Remove selected tiles",
validator= function(pos) return true end
},
}
CYCLE_VALUES = {
shape = {
[df.tiletype_shape.NONE] = true,
[df.tiletype_shape.EMPTY] = true,
[df.tiletype_shape.FLOOR] = true,
[df.tiletype_shape.WALL] = true,
other = df.tiletype_shape.STAIR_UPDOWN
},
material = {
[df.tiletype_material.NONE] = true,
[df.tiletype_material.AIR] = true,
[df.tiletype_material.SOIL] = true,
[df.tiletype_material.STONE] = true,
other = df.tiletype_material.LAVA_STONE
},
special = {
[df.tiletype_special.NONE] = true,
[df.tiletype_special.NORMAL] = true,
[df.tiletype_special.SMOOTH] = true,
other = df.tiletype_special.WORN_1
},
}
HIDDEN_VALUES = {
shape = {
[df.tiletype_shape.BRANCH] = true,
[df.tiletype_shape.TRUNK_BRANCH] = true,
[df.tiletype_shape.TWIG] = true,
[df.tiletype_shape.SAPLING] = true,
[df.tiletype_shape.SHRUB] = true,
[df.tiletype_shape.ENDLESS_PIT] = true
},
material = {
[df.tiletype_material.FEATURE] = true,
[df.tiletype_material.MINERAL] = true,
[df.tiletype_material.CONSTRUCTION] = true,
[df.tiletype_material.PLANT] = true,
[df.tiletype_material.TREE] = true,
[df.tiletype_material.MUSHROOM] = true,
[df.tiletype_material.ROOT] = true,
[df.tiletype_material.CAMPFIRE] = true,
[df.tiletype_material.DRIFTWOOD] = true,
[df.tiletype_material.UNDERWORLD_GATE] = true,
[df.tiletype_material.HFS] = true
},
special = {
[df.tiletype_special.DEAD] = true,
[df.tiletype_special.SMOOTH_DEAD] = true
}
}
---@class TileType
---@field shape? df.tiletype_shape
---@field material? df.tiletype_material
---@field special? df.tiletype_special
---@field variant? df.tiletype_variant
---@field dig? integer Only for filters
---@field hidden? integer
---@field light? integer
---@field subterranean? integer
---@field skyview? integer
---@field aquifer? integer
---@field autocorrect? integer
---@field stone_material? integer
---@field vein_type? df.inclusion_type
---@param pos df.coord
---@param target TileType
---@return boolean
function setTile(pos, target)
local toValidEnumValue = function(value, enum, default)
return value ~= nil and enum[value] and value or default
end
local toValidOptionValue = function(value)
return value == nil and -1 or value
end
local tiletype = {
shape = toValidEnumValue(target.shape, df.tiletype_shape, df.tiletype_shape.NONE),
material = toValidEnumValue(target.material, df.tiletype_material, df.tiletype_material.NONE),
special = toValidEnumValue(target.special, df.tiletype_special, df.tiletype_special.NONE),
variant = toValidEnumValue(target.variant, df.tiletype_variant, df.tiletype_variant.NONE),
hidden = toValidOptionValue(target.hidden),
light = toValidOptionValue(target.light),
subterranean = toValidOptionValue(target.subterranean),
skyview = toValidOptionValue(target.skyview),
aquifer = toValidOptionValue(target.aquifer),
autocorrect = target.autocorrect == nil and 0 or target.autocorrect,
}
tiletype.stone_material = tiletype.material == df.tiletype_material.STONE and target.stone_material or -1
tiletype.vein_type = tiletype.material ~= df.tiletype_material.STONE and -1 or toValidEnumValue(target.vein_type, df.inclusion_type, df.inclusion_type.CLUSTER)
return plugin.tiletypes_setTile(pos, tiletype)
end
--#region GUI
--#region UI Utilities
---@type widgets.LabelToken
local EMPTY_TOKEN = { text=' ', hpen=dfhack.pen.make(COLOR_RESET), width=1 }
---@class InlineButtonLabelSpec
---@field left_specs? widgets.ButtonLabelSpec
---@field right_specs? widgets.ButtonLabelSpec
---@field width? integer
---@field height? integer
---@field spacing? integer
---@nodiscard
---@param spec InlineButtonLabelSpec
---@return widgets.LabelToken[]
function makeInlineButtonLabelText(spec)
spec.left_specs = safe_index(spec, "left_specs", "chars") and spec.left_specs or {chars={}}
spec.right_specs = safe_index(spec, "right_specs", "chars") and spec.right_specs or {chars={}}
spec.width = spec.width or -1
spec.height = spec.height or -1
spec.spacing = spec.spacing or -1
local getSpecWidth = function(value)
local width = 0
for _,v in pairs(value.chars) do
width = math.max(width, #v)
end
return width
end
local left_width = getSpecWidth(spec.left_specs)
local right_width = getSpecWidth(spec.right_specs)
spec.width = spec.width >= 0 and spec.width or (left_width + right_width + math.max(spec.spacing, 0))
local left_height = #spec.left_specs.chars
local right_height = #spec.right_specs.chars
spec.height = spec.height >= 0 and spec.height or math.max(left_height, right_height)
local left_tokens = widgets.makeButtonLabelText(spec.left_specs)
local right_tokens = widgets.makeButtonLabelText(spec.right_specs)
local centerHeight = function(tokens, height)
local height_spacing = (spec.height - height) // 2
for i=1, height_spacing do
table.insert(tokens, 1, NEWLINE)
end
height_spacing = spec.height - height - height_spacing
for i=1, height_spacing do
table.insert(tokens, NEWLINE)
end
end
centerHeight(left_tokens, left_height)
centerHeight(right_tokens, right_height)
local right_start = spec.spacing >= 0 and (left_width + spec.spacing + 1) or math.max(left_width, spec.width - right_width)
local label_tokens = {}
local left_cursor = 1
local right_cursor = 1
for y=1, spec.height do
for x=1, spec.width do
local token = nil
if x <= left_width then
token = left_tokens[left_cursor]
token = token ~= NEWLINE and token or nil
if token then
left_cursor = left_cursor + 1
end
elseif x >= right_start then
token = right_tokens[right_cursor]
token = token ~= NEWLINE and token or nil
if token then
right_cursor = right_cursor + 1
end
end
table.insert(label_tokens, token or EMPTY_TOKEN)
end
if y ~= spec.height then
-- Move the cursors to the token following the next NEWLINE
while left_tokens[left_cursor - 1] ~= NEWLINE do
left_cursor = left_cursor + 1
end
while right_tokens[right_cursor - 1] ~= NEWLINE do
right_cursor = right_cursor + 1
end
end
table.insert(label_tokens, NEWLINE)
end
return label_tokens
end
-- Rect data class
---@class Rect.attrs
---@field x1 number
---@field y1 number
---@field x2 number
---@field y2 number
---@class Rect.attrs.partial: Rect.attrs
---@class Rect: Rect.attrs
---@field ATTRS Rect.attrs|fun(attributes: Rect.attrs.partial)
---@overload fun(init_table: Rect.attrs.partial): self
Rect = defclass(Rect)
Rect.ATTRS {
x1 = -1,
y1 = -1,
x2 = -1,
y2 = -1,
}
---@param pos df.coord2d
---@return boolean
function Rect:contains(pos)
return pos.x <= self.x2
and pos.x >= self.x1
and pos.y <= self.y2
and pos.y >= self.y1
end
---@param overlap_rect Rect
---@return boolean
function Rect:isOverlapping(overlap_rect)
return overlap_rect.x1 <= self.x2
and overlap_rect.x2 >= self.x1
and overlap_rect.y1 <= self.y2
and overlap_rect.y2 >= self.y1
end
---@param clip_rect Rect
---@return Rect[]
function Rect:clip(clip_rect)
local output = {}
-- If there is any overlap with the screen rect
if self:isOverlapping(clip_rect) then
local temp_rect = Rect(self)
-- Get rect to the left of the clip rect
if temp_rect.x1 <= clip_rect.x1 then
table.insert(output, Rect{
x1= temp_rect.x1,
x2= math.min(temp_rect.x2, clip_rect.x1),
y1= temp_rect.y1,
y2= temp_rect.y2
})
temp_rect.x1 = clip_rect.x1
end
-- Get rect to the right of the clip rect
if temp_rect.x2 >= clip_rect.x2 then
table.insert(output, Rect{
x1= math.max(temp_rect.x1, clip_rect.x2),
x2= temp_rect.x2,
y1= temp_rect.y1,
y2= temp_rect.y2
})
temp_rect.x2 = clip_rect.x2
end
-- Get rect above the clip rect
if temp_rect.y1 <= clip_rect.y1 then
table.insert(output, Rect{
x1= temp_rect.x1,
x2= temp_rect.x2,
y1= temp_rect.y1,
y2= math.min(temp_rect.y2, clip_rect.y1)
})
temp_rect.y1 = clip_rect.y1
end
-- Get rect below the clip rect
if temp_rect.y2 >= clip_rect.y2 then
table.insert(output, Rect{
x1= temp_rect.x1,
x2= temp_rect.x2,
y1= math.max(temp_rect.y1, clip_rect.y2),
y2= temp_rect.y2
})
temp_rect.y2 = clip_rect.y2
end
else
-- No overlap
table.insert(output, self)
end
return output
end
---@return Rect
function Rect:screenToTile()
local view_dims = dfhack.gui.getDwarfmodeViewDims()
local tile_view_size = xy2pos(view_dims.map_x2 - view_dims.map_x1 + 1, view_dims.map_y2 - view_dims.map_y1 + 1)
local display_view_size = xy2pos(df.global.init.display.grid_x, df.global.init.display.grid_y)
local display_to_tile_ratio = xy2pos(tile_view_size.x / display_view_size.x, tile_view_size.y / display_view_size.y)
return Rect{
x1= self.x1 * display_to_tile_ratio.x - 1,
x2= self.x2 * display_to_tile_ratio.x + 1,
y1= self.y1 * display_to_tile_ratio.y - 1,
y2= self.y2 * display_to_tile_ratio.y + 1
}
end
---@return Rect
function Rect:tileToScreen()
local view_dims = dfhack.gui.getDwarfmodeViewDims()
local tile_view_size = xy2pos(view_dims.map_x2 - view_dims.map_x1 + 1, view_dims.map_y2 - view_dims.map_y1 + 1)
local display_view_size = xy2pos(df.global.init.display.grid_x, df.global.init.display.grid_y)
local display_to_tile_ratio = xy2pos(tile_view_size.x / display_view_size.x, tile_view_size.y / display_view_size.y)
return Rect{
x1= (self.x1 + 1) / display_to_tile_ratio.x,
x2= (self.x2 - 1) / display_to_tile_ratio.x,
y1= (self.y1 + 1) / display_to_tile_ratio.y,
y2= (self.y2 - 1) / display_to_tile_ratio.y
}
end
-- Draws a list of rects with associated pens, without overlapping any of the given screen rects
---@param draw_queue { pen: dfhack.pen, rect: Rect }[]
---@param screen_rect_list Rect[]
function drawOutsideOfScreenRectList(draw_queue, screen_rect_list)
local cur_draw_queue = draw_queue
for _,screen_rect in pairs(screen_rect_list) do
local screen_tile_rect = screen_rect:screenToTile()
local new_draw_queue = {}
for _,draw_rect in pairs(cur_draw_queue) do
for _,clipped in pairs(draw_rect.rect:clip(screen_tile_rect)) do
table.insert(new_draw_queue, { pen= draw_rect.pen, rect= clipped })
end
end
cur_draw_queue = new_draw_queue
end
for _,draw_rect in pairs(cur_draw_queue) do
dfhack.screen.fillRect(draw_rect.pen, draw_rect.rect.x1, draw_rect.rect.y1, draw_rect.rect.x2, draw_rect.rect.y2, true)
end
end
-- Box data class
---@class Box.attrs
---@class Box.attrs.partial: Box.attrs
---@class Box: Box.attrs
---@field ATTRS Box.attrs|fun(attributes: Box.attrs.partial)
---@field valid boolean
---@field min df.coord
---@field max df.coord
---@overload fun(init_table: Box.attrs.partial): self
Box = defclass(Box)
Box.ATTRS {}
function Box:init(points)
self.valid = true
self.min = nil
self.max = nil
for _,value in pairs(points) do
if dfhack.maps.isValidTilePos(value) then
self.min = xyz2pos(
self.min and math.min(self.min.x, value.x) or value.x,
self.min and math.min(self.min.y, value.y) or value.y,
self.min and math.min(self.min.z, value.z) or value.z
)
self.max = xyz2pos(
self.max and math.max(self.max.x, value.x) or value.x,
self.max and math.max(self.max.y, value.y) or value.y,
self.max and math.max(self.max.z, value.z) or value.z
)
else
self.valid = false
break
end
end
self.valid = self.valid and self.min and self.max
and dfhack.maps.isValidTilePos(self.min.x, self.min.y, self.min.z)
and dfhack.maps.isValidTilePos(self.max.x, self.max.y, self.max.z)
if not self.valid then
self.min = xyz2pos(-1, -1, -1)
self.max = xyz2pos(-1, -1, -1)
end
end
function Box:iterate(callback)
if not self.valid then return end
for z = self.min.z, self.max.z do
for y = self.min.y, self.max.y do
for x = self.min.x, self.max.x do
callback(xyz2pos(x, y, z))
end
end
end
end
function Box:draw(tile_map, avoid_rect, ascii_fill)
if not self.valid or df.global.window_z < self.min.z or df.global.window_z > self.max.z then return end
local screen_min = xy2pos(
self.min.x - df.global.window_x,
self.min.y - df.global.window_y
)
local screen_max = xy2pos(
self.max.x - df.global.window_x,
self.max.y - df.global.window_y
)
local draw_queue = {}
if self.min.x == self.max.x and self.min.y == self.max.y then
-- Single point
draw_queue = {
{
pen= tile_map.pens[tile_map.getPenKey{n=true, e=true, s=true, w=true}],
rect= Rect{ x1= screen_min.x, x2= screen_min.x, y1= screen_min.y, y2= screen_min.y }
}
}
elseif self.min.x == self.max.x then
-- Vertical line
draw_queue = {
-- Line
{
pen= tile_map.pens[tile_map.getPenKey{n=false, e=true, s=false, w=true}],
rect= Rect{ x1= screen_min.x, x2= screen_min.x, y1= screen_min.y, y2= screen_max.y }
},
-- Top nub
{
pen= tile_map.pens[tile_map.getPenKey{n=true, e=true, s=false, w=true}],
rect= Rect{ x1= screen_min.x, x2= screen_min.x, y1= screen_min.y, y2= screen_min.y }
},
-- Bottom nub
{
pen= tile_map.pens[tile_map.getPenKey{n=false, e=true, s=true, w=true}],
rect= Rect{ x1= screen_min.x, x2= screen_min.x, y1= screen_max.y, y2= screen_max.y }
}
}
elseif self.min.y == self.max.y then
-- Horizontal line
draw_queue = {
-- Line
{
pen= tile_map.pens[tile_map.getPenKey{n=true, e=false, s=true, w=false}],
rect= Rect{ x1= screen_min.x, x2= screen_max.x, y1= screen_min.y, y2= screen_min.y }
},
-- Left nub
{
pen= tile_map.pens[tile_map.getPenKey{n=true, e=false, s=true, w=true}],
rect= Rect{ x1= screen_min.x, x2= screen_min.x, y1= screen_min.y, y2= screen_min.y }
},
-- Right nub
{
pen= tile_map.pens[tile_map.getPenKey{n=true, e=true, s=true, w=false}],
rect= Rect{ x1= screen_max.x, x2= screen_max.x, y1= screen_min.y, y2= screen_min.y }
}
}
else
-- Rectangle
draw_queue = {
-- North Edge
{
pen= tile_map.pens[tile_map.getPenKey{n=true, e=false, s=false, w=false}],
rect= Rect{ x1= screen_min.x, x2= screen_max.x, y1= screen_min.y, y2= screen_min.y }
},
-- East Edge
{
pen= tile_map.pens[tile_map.getPenKey{n=false, e=true, s=false, w=false}],
rect= Rect{ x1= screen_max.x, x2= screen_max.x, y1= screen_min.y, y2= screen_max.y }
},
-- South Edge
{
pen= tile_map.pens[tile_map.getPenKey{n=false, e=false, s=true, w=false}],
rect= Rect{ x1= screen_min.x, x2= screen_max.x, y1= screen_max.y, y2= screen_max.y }
},
-- West Edge
{
pen= tile_map.pens[tile_map.getPenKey{n=false, e=false, s=false, w=true}],
rect= Rect{ x1= screen_min.x, x2= screen_min.x, y1= screen_min.y, y2= screen_max.y }
},
-- NW Corner
{
pen= tile_map.pens[tile_map.getPenKey{n=true, e=false, s=false, w=true}],
rect= Rect{ x1= screen_min.x, x2= screen_min.x, y1= screen_min.y, y2= screen_min.y }
},
-- NE Corner
{
pen= tile_map.pens[tile_map.getPenKey{n=true, e=true, s=false, w=false}],
rect= Rect{ x1= screen_max.x, x2= screen_max.x, y1= screen_min.y, y2= screen_min.y }
},
-- SE Corner
{
pen= tile_map.pens[tile_map.getPenKey{n=false, e=true, s=true, w=false}],
rect= Rect{ x1= screen_max.x, x2= screen_max.x, y1= screen_max.y, y2= screen_max.y }
},
-- SW Corner
{
pen= tile_map.pens[tile_map.getPenKey{n=false, e=false, s=true, w=true}],
rect= Rect{ x1= screen_min.x, x2= screen_min.x, y1= screen_max.y, y2= screen_max.y }
},
}
if dfhack.screen.inGraphicsMode() or ascii_fill then
-- Fill inside
table.insert(draw_queue, 1, {
pen= tile_map.pens[tile_map.getPenKey{n=false, e=false, s=false, w=false}],
rect= Rect{ x1= screen_min.x + 1, x2= screen_max.x - 1, y1= screen_min.y + 1, y2= screen_max.y - 1 }
})
end
end
if avoid_rect and not dfhack.screen.inGraphicsMode() then
-- If in ASCII and an avoid_rect was specified
-- Draw the queue, avoiding the avoid_rect
drawOutsideOfScreenRectList(draw_queue, { avoid_rect })
else
-- Draw the queue
for _,draw_rect in pairs(draw_queue) do
dfhack.screen.fillRect(draw_rect.pen, math.floor(draw_rect.rect.x1), math.floor(draw_rect.rect.y1), math.floor(draw_rect.rect.x2), math.floor(draw_rect.rect.y2), true)
end
end
end
--================================--
--|| BoxSelection ||--
--================================--
-- Allows for selecting a box
---@class BoxTileMap
---@field getPenKey fun(nesw: { n: boolean, e: boolean, s: boolean, w: boolean }): any
---@field createPens? fun(): { key: dfhack.pen }
---@field pens? { key: dfhack.pen }
local TILE_MAP = {
getPenKey= function(nesw)
local out = 0
for _,v in ipairs({nesw.n, nesw.e, nesw.s, nesw.w}) do
out = (out << 1) | (v and 1 or 0)
end
return out
end
}
TILE_MAP.createPens= function()
return {
[TILE_MAP.getPenKey{ n=false, e=false, s=false, w=false }] = dfhack.pen.parse{tile=dfhack.screen.findGraphicsTile("CURSORS", 1, 2), fg=COLOR_GREEN, ch='X'}, -- INSIDE
[TILE_MAP.getPenKey{ n=true, e=false, s=false, w=true }] = dfhack.pen.parse{tile=dfhack.screen.findGraphicsTile("CURSORS", 0, 1), fg=COLOR_GREEN, ch='X'}, -- NW
[TILE_MAP.getPenKey{ n=true, e=false, s=false, w=false }] = dfhack.pen.parse{tile=dfhack.screen.findGraphicsTile("CURSORS", 1, 1), fg=COLOR_GREEN, ch='X'}, -- NORTH
[TILE_MAP.getPenKey{ n=true, e=true, s=false, w=false }] = dfhack.pen.parse{tile=dfhack.screen.findGraphicsTile("CURSORS", 2, 1), fg=COLOR_GREEN, ch='X'}, -- NE
[TILE_MAP.getPenKey{ n=false, e=false, s=false, w=true }] = dfhack.pen.parse{tile=dfhack.screen.findGraphicsTile("CURSORS", 0, 2), fg=COLOR_GREEN, ch='X'}, -- WEST
[TILE_MAP.getPenKey{ n=false, e=true, s=false, w=false }] = dfhack.pen.parse{tile=dfhack.screen.findGraphicsTile("CURSORS", 2, 2), fg=COLOR_GREEN, ch='X'}, -- EAST
[TILE_MAP.getPenKey{ n=false, e=false, s=true, w=true }] = dfhack.pen.parse{tile=dfhack.screen.findGraphicsTile("CURSORS", 0, 3), fg=COLOR_GREEN, ch='X'}, -- SW
[TILE_MAP.getPenKey{ n=false, e=false, s=true, w=false }] = dfhack.pen.parse{tile=dfhack.screen.findGraphicsTile("CURSORS", 1, 3), fg=COLOR_GREEN, ch='X'}, -- SOUTH
[TILE_MAP.getPenKey{ n=false, e=true, s=true, w=false }] = dfhack.pen.parse{tile=dfhack.screen.findGraphicsTile("CURSORS", 2, 3), fg=COLOR_GREEN, ch='X'}, -- SE
[TILE_MAP.getPenKey{ n=true, e=true, s=false, w=true }] = dfhack.pen.parse{tile=dfhack.screen.findGraphicsTile("CURSORS", 3, 2), fg=COLOR_GREEN, ch='X'}, -- N_NUB
[TILE_MAP.getPenKey{ n=true, e=true, s=true, w=false }] = dfhack.pen.parse{tile=dfhack.screen.findGraphicsTile("CURSORS", 5, 1), fg=COLOR_GREEN, ch='X'}, -- E_NUB
[TILE_MAP.getPenKey{ n=true, e=false, s=true, w=true }] = dfhack.pen.parse{tile=dfhack.screen.findGraphicsTile("CURSORS", 3, 1), fg=COLOR_GREEN, ch='X'}, -- W_NUB
[TILE_MAP.getPenKey{ n=false, e=true, s=true, w=true }] = dfhack.pen.parse{tile=dfhack.screen.findGraphicsTile("CURSORS", 4, 2), fg=COLOR_GREEN, ch='X'}, -- S_NUB
[TILE_MAP.getPenKey{ n=false, e=true, s=false, w=true }] = dfhack.pen.parse{tile=dfhack.screen.findGraphicsTile("CURSORS", 3, 3), fg=COLOR_GREEN, ch='X'}, -- VERT_NS
[TILE_MAP.getPenKey{ n=true, e=false, s=true, w=false }] = dfhack.pen.parse{tile=dfhack.screen.findGraphicsTile("CURSORS", 4, 1), fg=COLOR_GREEN, ch='X'}, -- VERT_EW
[TILE_MAP.getPenKey{ n=true, e=true, s=true, w=true }] = dfhack.pen.parse{tile=dfhack.screen.findGraphicsTile("CURSORS", 4, 3), fg=COLOR_GREEN, ch='X'}, -- POINT
}
end
---@class BoxSelection.attrs: widgets.Window.attrs
---@field tooltip_enabled? boolean,
---@field screen? gui.Screen
---@field tile_map? BoxTileMap
---@field avoid_view? gui.View|fun():gui.View
---@field on_confirm? fun(box: Box)
---@field flat boolean
---@field ascii_fill boolean
---@class BoxSelection.attrs.partial: BoxSelection.attrs
---@class BoxSelection: widgets.Window, BoxSelection.attrs
---@field box? Box
---@field first_point? df.coord
---@field last_point? df.coord
BoxSelection = defclass(BoxSelection, widgets.Window)
BoxSelection.ATTRS {
tooltip_enabled=true,
screen=DEFAULT_NIL,
tile_map=TILE_MAP,
avoid_view=DEFAULT_NIL,
on_confirm=DEFAULT_NIL,
flat=false,
ascii_fill=false,
}
function BoxSelection:init()
self.frame = { w=0, h=0 }
self.box=nil
self.first_point=nil
self.last_point=nil
if self.tile_map then
if self.tile_map.createPens then
self.tile_map.pens = self.tile_map.createPens()
end
else
error("No tile map provided")
end
-- Set the cursor to the center of the screen
guidm.setCursorPos(guidm.Viewport.get():getCenter())
-- Show cursor
df.global.game.main_interface.main_designation_selected = df.main_designation_type.TOGGLE_ENGRAVING
if self.tooltip_enabled then
if self.screen then
self.dimensions_tooltip = widgets.DimensionsTooltip{
get_anchor_pos_fn=function()
if self.first_point and self.flat then
return xyz2pos(self.first_point.x, self.first_point.y, df.global.window_z)
end
return self.first_point
end,
}
self.screen:addviews{
self.dimensions_tooltip
}
else
error("No screen provided to BoxSelection, unable to display DimensionsTooltip")
end
end
end
function BoxSelection:confirm()
if self.first_point and self.last_point
and dfhack.maps.isValidTilePos(self.first_point)
and dfhack.maps.isValidTilePos(self.last_point)
then
self.box = Box{
self.first_point,
self.last_point
}
if self.on_confirm then
self.on_confirm(self.box)
end
end
end
function BoxSelection:clear()
self.box = nil
self.first_point = nil
self.last_point = nil
end
function BoxSelection:onInput(keys)
if BoxSelection.super.onInput(self, keys) then
return true
end
if keys.LEAVESCREEN or keys._MOUSE_R then
if self.last_point then
self.box = nil
self.last_point = nil
return true
elseif self.first_point then
self.first_point = nil
return true
end
return false
end
local mousePos = dfhack.gui.getMousePos(true)
local cursorPos = guidm.getCursorPos()
if cursorPos and keys.SELECT then
cursorPos.x = math.max(math.min(cursorPos.x, df.global.world.map.x_count - 1), 0)
cursorPos.y = math.max(math.min(cursorPos.y, df.global.world.map.y_count - 1), 0)
if self.first_point and not self.last_point then
if not self.flat or cursorPos.z == self.first_point.z then
self.last_point = cursorPos
self:confirm()
end
elseif dfhack.maps.isValidTilePos(cursorPos) then
self.first_point = self.first_point or cursorPos
end
return true
end
local avoid_view = utils.getval(self.avoid_view)
-- Get the position of the mouse in coordinates local to avoid_view, if it's specified
local mouseFramePos = avoid_view and avoid_view:getMouseFramePos()
if keys._MOUSE_L and not mouseFramePos then
-- If left click and the mouse is not in the avoid_view
self.useCursor = false
if self.first_point and not self.last_point then
if not self.flat or mousePos.z == self.first_point.z then
local inBoundsMouse = xyz2pos(
math.max(math.min(mousePos.x, df.global.world.map.x_count - 1), 0),
math.max(math.min(mousePos.y, df.global.world.map.y_count - 1), 0),
mousePos.z
)
self.last_point = inBoundsMouse
self:confirm()
end
elseif dfhack.maps.isValidTilePos(mousePos) then
self.first_point = self.first_point or mousePos
end
return true
end
-- Switch to the cursor if the cursor was moved (Excluding up and down a Z level)
local filteredKeys = utils.clone(keys)
filteredKeys["CURSOR_DOWN_Z"] = nil
filteredKeys["CURSOR_UP_Z"] = nil
self.useCursor = (self.useCursor or guidm.getMapKey(filteredKeys)) and df.global.d_init.feature.flags.KEYBOARD_CURSOR
return false
end
function BoxSelection:onRenderFrame(dc, rect)
-- Switch to cursor if the mouse is offscreen, or if it hasn't moved
self.useCursor = (self.useCursor or (self.lastMousePos and (self.lastMousePos.x < 0 or self.lastMousePos.y < 0)))
and self.lastMousePos.x == df.global.gps.mouse_x and self.lastMousePos.y == df.global.gps.mouse_y
self.lastMousePos = xy2pos(df.global.gps.mouse_x, df.global.gps.mouse_y)
if self.tooltip_enabled and self.screen and self.dimensions_tooltip then
self.dimensions_tooltip.visible = not self.useCursor
end
if not self.tile_map then return end
local box = self.box
if not box then
local selectedPos = dfhack.gui.getMousePos(true)
if self.useCursor or not selectedPos then
selectedPos = guidm.getCursorPos() or xyz2pos(nil)
selectedPos.x = math.max(math.min(selectedPos.x, df.global.world.map.x_count - 1), 0)
selectedPos.y = math.max(math.min(selectedPos.y, df.global.world.map.y_count - 1), 0)
end
if self.flat and self.first_point then
selectedPos.z = self.first_point.z
end
local inBoundsMouse = xyz2pos(
math.max(math.min(selectedPos.x, df.global.world.map.x_count - 1), 0),
math.max(math.min(selectedPos.y, df.global.world.map.y_count - 1), 0),
selectedPos.z
)
box = Box {
self.first_point or selectedPos,
self.last_point or (self.first_point and inBoundsMouse or selectedPos)
}
end
if box then
local avoid_view = utils.getval(self.avoid_view)
box:draw(self.tile_map, avoid_view and Rect(avoid_view.frame_rect), self.ascii_fill)
end
-- Don't call super.onRenderFrame, since this widget should not be drawn
end
function BoxSelection:hideCursor()
-- Hide cursor
df.global.game.main_interface.main_designation_selected = df.main_designation_type.NONE
end
--================================--
--|| SelectDialog ||--
--================================--
-- Popup for selecting an item from a list, with a search bar
---@class CategoryChoice
---@field text string|widgets.LabelToken[]
---@field category string?
---@field key string?
---@field item_list (widgets.ListChoice|CategoryChoice)[]
local ARROW = string.char(26)
SelectDialogWindow = defclass(SelectDialogWindow, widgets.Window)
SelectDialogWindow.ATTRS{
prompt = "Type or select a item from this list",
base_category = "Any item",
frame={w=40, h=28},
frame_style = gui.FRAME_PANEL,
frame_inset = 1,
frame_title = "Select Item",
item_list = DEFAULT_NIL, ---@type (widgets.ListChoice|CategoryChoice)[]
on_select = DEFAULT_NIL,
on_cancel = DEFAULT_NIL,
on_close = DEFAULT_NIL,
}
function SelectDialogWindow:init()
self.back = widgets.HotkeyLabel{
frame = { r = 0, b = 0 },
auto_width = true,
visible = false,
label = "Back",
key="LEAVESCREEN",
on_activate = self:callback('onGoBack')
}
self.list = widgets.FilteredList{
not_found_label = 'No matching items',
frame = { l = 0, r = 0, t = 4, b = 2 },
icon_width = 2,
on_submit = self:callback('onSubmitItem'),
}
self:addviews{
widgets.Label{
text = {
self.prompt, '\n\n',
'Category: ', { text = self:cb_getfield('category_str'), pen = COLOR_CYAN }
},
text_pen = COLOR_WHITE,
frame = { l = 0, t = 0 },
},
self.back,
self.list,
widgets.HotkeyLabel{
frame = { l = 0, b = 0 },
auto_width = true,
label = "Select",
key="SELECT",
disabled = function() return not self.list:canSubmit() end,
on_activate = function() self.list:submit() end
}
}
self:initCategory(self.base_category, self.item_list)
end
function SelectDialogWindow:onDismiss()
if self.on_close then
self.on_close()
end
end
function SelectDialogWindow:initCategory(name, item_list)
local choices = {}
for _,value in pairs(item_list) do
self:addItem(choices, value)
end
self:pushCategory(name, choices)
end
function SelectDialogWindow:addItem(choices, item)
if not item or not item.text then return end
if item.item_list then
table.insert(choices, {
icon = ARROW, text = item.text, key = item.key,
cb = function() self:initCategory(item.category or item.text, item.item_list) end
})
else
table.insert(choices, {
text = item.text,
value = item.value or item.text
})
end
end
function SelectDialogWindow:pushCategory(name, choices)
if not self.back_stack then
self.back_stack = {}
self.back.visible = false
else
table.insert(self.back_stack, {
category_str = self.category_str,
all_choices = self.list:getChoices(),
edit_text = self.list:getFilter(),
selected = self.list:getSelected(),
})
self.back.visible = true
end
self.category_str = name
self.list:setChoices(choices, 1)
end
function SelectDialogWindow:onGoBack()
local save = table.remove(self.back_stack)
self.back.visible = (#self.back_stack > 0)
self.category_str = save.category_str
self.list:setChoices(save.all_choices)
self.list:setFilter(save.edit_text, save.selected)
end
function SelectDialogWindow:submitItem(value)
self.parent_view:dismiss()
if self.on_select then