-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathplanneroverlay.lua
More file actions
1383 lines (1264 loc) · 50.6 KB
/
planneroverlay.lua
File metadata and controls
1383 lines (1264 loc) · 50.6 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
local _ENV = mkmodule('plugins.buildingplan.planneroverlay')
local itemselection = require('plugins.buildingplan.itemselection')
local filterselection = require('plugins.buildingplan.filterselection')
local gui = require('gui')
local guidm = require('gui.dwarfmode')
local json = require('json')
local overlay = require('plugins.overlay')
local pens = require('plugins.buildingplan.pens')
local utils = require('utils')
local widgets = require('gui.widgets')
require('dfhack.buildings')
config = config or json.open('dfhack-config/buildingplan.json')
local uibs = df.global.buildreq
reset_counts_flag = false
editing_filters_flag = false
local function get_cur_filters()
return dfhack.buildings.getFiltersByType({}, uibs.building_type,
uibs.building_subtype, uibs.custom_type)
end
local function is_choosing_area()
return uibs.selection_pos.x >= 0
end
-- TODO: reuse data in quickfort database
local function get_selection_size_limits()
local btype = uibs.building_type
if btype == df.building_type.Bridge
or btype == df.building_type.FarmPlot
or btype == df.building_type.RoadPaved
or btype == df.building_type.RoadDirt then
return {w=31, h=31}
elseif btype == df.building_type.AxleHorizontal then
return uibs.direction == 1 and {w=1, h=31} or {w=31, h=1}
elseif btype == df.building_type.Rollers then
return (uibs.direction == 1 or uibs.direction == 3) and {w=31, h=1} or {w=1, h=31}
end
end
local function get_selected_bounds(selection_pos, pos)
selection_pos = selection_pos or uibs.selection_pos
if not is_choosing_area() then return end
pos = pos or uibs.pos
local bounds = {
x1=math.min(selection_pos.x, pos.x),
x2=math.max(selection_pos.x, pos.x),
y1=math.min(selection_pos.y, pos.y),
y2=math.max(selection_pos.y, pos.y),
z1=math.min(selection_pos.z, pos.z),
z2=math.max(selection_pos.z, pos.z),
}
-- clamp to map edges
bounds = {
x1=math.max(0, bounds.x1),
x2=math.min(df.global.world.map.x_count-1, bounds.x2),
y1=math.max(0, bounds.y1),
y2=math.min(df.global.world.map.y_count-1, bounds.y2),
z1=math.max(0, bounds.z1),
z2=math.min(df.global.world.map.z_count-1, bounds.z2),
}
local limits = get_selection_size_limits()
if limits then
-- clamp to building type area limit
bounds = {
x1=math.max(selection_pos.x - (limits.w-1), bounds.x1),
x2=math.min(selection_pos.x + (limits.w-1), bounds.x2),
y1=math.max(selection_pos.y - (limits.h-1), bounds.y1),
y2=math.min(selection_pos.y + (limits.h-1), bounds.y2),
z1=bounds.z1,
z2=bounds.z2,
}
end
return bounds
end
local function get_cur_area_dims(bounds)
if not bounds and not is_choosing_area() then return 1, 1, 1 end
bounds = bounds or get_selected_bounds()
if not bounds then return 1, 1, 1 end
return bounds.x2 - bounds.x1 + 1,
bounds.y2 - bounds.y1 + 1,
bounds.z2 - bounds.z1 + 1
end
local function get_selected_volume(bounds)
local w, h, depth = get_cur_area_dims(bounds)
return w * h * depth
end
local function is_pressure_plate()
return uibs.building_type == df.building_type.Trap
and uibs.building_subtype == df.trap_type.PressurePlate
end
local function is_weapon_trap()
return uibs.building_type == df.building_type.Trap
and uibs.building_subtype == df.trap_type.WeaponTrap
end
local function is_spike_trap()
return uibs.building_type == df.building_type.Weapon
end
local function is_weapon_or_spike_trap()
return is_weapon_trap() or is_spike_trap()
end
local function is_construction()
return uibs.building_type == df.building_type.Construction
end
local function is_siege_engine()
return uibs.building_type == df.building_type.SiegeEngine
end
local function tile_is_construction(pos)
local tt = dfhack.maps.getTileType(pos)
if not tt then return false end
if df.tiletype.attrs[tt].material ~= df.tiletype_material.CONSTRUCTION then
return false
end
local construction = df.construction.find(pos)
return construction and not construction.flags.top_of_wall
end
local ONE_BY_ONE = xy2pos(1, 1)
local function can_reconstruct(bounds)
return get_selected_volume(bounds) == 1 or require('plugins.buildingplan').getGlobalSettings().reconstruct
end
local function can_place_construction(reconstruct, pos)
return dfhack.buildings.checkFreeTiles(pos, ONE_BY_ONE) and (reconstruct or not tile_is_construction(pos))
end
local function is_interior(bounds, x, y)
return x ~= bounds.x1 and x ~= bounds.x2 and
y ~= bounds.y1 and y ~= bounds.y2
end
-- adjusted from CycleHotkeyLabel on the planner panel
local weapon_quantity = 1
local function get_quantity(filter, hollow, bounds)
if is_pressure_plate() then
local flags = uibs.plate_info.flags
return (flags.units and 1 or 0) + (flags.water and 1 or 0) +
(flags.magma and 1 or 0) + (flags.track and 1 or 0)
elseif (is_weapon_trap() and filter.vector_id == df.job_item_vector_id.ANY_WEAPON) or is_spike_trap() then
return weapon_quantity
end
local quantity = filter.quantity or 1
bounds = bounds or get_selected_bounds()
local dimx, dimy, dimz = get_cur_area_dims(bounds)
if quantity < 1 then
return (((dimx * dimy) // 4) + 1) * dimz
end
if bounds and is_construction() then
local reconstruct = can_reconstruct(bounds)
local count = 0
for z = bounds.z1, bounds.z2 do
for y = bounds.y1, bounds.y2 do
for x = bounds.x1, bounds.x2 do
if hollow and is_interior(bounds, x, y) then goto continue end
if can_place_construction(reconstruct, xyz2pos(x, y, z)) then
count = count + 1
end
::continue::
end
end
end
return quantity * count
end
return quantity * get_selected_volume(bounds)
end
local function cur_building_has_no_area()
if uibs.building_type == df.building_type.Construction then return false end
local filters = dfhack.buildings.getFiltersByType({},
uibs.building_type, uibs.building_subtype, uibs.custom_type)
-- this works because all variable-size buildings have either no item
-- filters or a quantity of -1 for their first (and only) item
return filters and filters[1] and (not filters[1].quantity or filters[1].quantity > 0)
end
local function is_tutorial_open()
local help = df.global.game.main_interface.help
return help.open and
help.context == df.help_context_type.START_TUTORIAL_WORKSHOPS_AND_TASKS
end
local function is_plannable()
return not is_tutorial_open() and
get_cur_filters() and
not (is_construction() and
uibs.building_subtype == df.construction_type.TrackNSEW)
end
local function is_slab()
return uibs.building_type == df.building_type.Slab
end
local function is_cage()
return uibs.building_type == df.building_type.Cage
end
local function is_stairs()
return is_construction()
and uibs.building_subtype == df.construction_type.UpDownStair
end
local function is_single_level_stairs()
if not is_stairs() then return false end
local _, _, dimz = get_cur_area_dims()
return dimz == 1
end
local function is_multi_level_stairs()
if not is_stairs() then return false end
local _, _, dimz = get_cur_area_dims()
return dimz > 1
end
local direction_panel_frame = {t=4, h=13, w=46, r=28}
local direction_panel_types = utils.invert{
df.building_type.Bridge,
df.building_type.ScrewPump,
df.building_type.WaterWheel,
df.building_type.AxleHorizontal,
df.building_type.Rollers,
df.building_type.SiegeEngine,
}
local function has_direction_panel()
return direction_panel_types[uibs.building_type]
or (uibs.building_type == df.building_type.Trap
and uibs.building_subtype == df.trap_type.TrackStop)
end
local pressure_plate_panel_frame = {t=4, h=37, w=46, r=28}
local function has_pressure_plate_panel()
return is_pressure_plate()
end
local function is_over_options_panel()
local frame = nil
if has_direction_panel() then
frame = direction_panel_frame
elseif has_pressure_plate_panel() then
frame = pressure_plate_panel_frame
else
return false
end
local v = widgets.Widget{frame=frame}
local rect = gui.mkdims_wh(0, 0, dfhack.screen.getWindowSize())
v:updateLayout(gui.ViewRect{rect=rect})
return v:getMousePos()
end
local function compress(str, len)
if #str <= len then
return str
else
local no_vowels = str:gsub('[aeiou]','')
if #no_vowels <= len then
return no_vowels
else
return no_vowels:sub(1,len-3)..'...'
end
end
end
local function filter_string(mats, cats, length)
local enabled_mat_names = {}
local enabled_cat_names = {}
for name, props in pairs(mats) do
local enabled = props.enabled == 'true' and cats[props.category]
if enabled then table.insert(enabled_mat_names, name) end
end
if #enabled_mat_names == 1 then
return '['..compress(enabled_mat_names[1], length)..']'
elseif #enabled_mat_names > 1 then
for cat, _ in pairs(cats) do
if cat ~= 'unset' and cats[cat] then
table.insert(enabled_cat_names, cat)
end
end
if #enabled_cat_names == 1 then
return '[' .. enabled_cat_names[1]:gsub("^%l", string.upper) .. ']'
else
return '['..#enabled_cat_names..' mat. categories]'
end
else
-- can result from selecting wood and then toggling "fire safe" etc.
return '[impossible filter]'
end
end
--------------------------------
-- ItemLine
--
-- number of characters for item filter summary (excluding surrounding [ ])
local item_filter_chars = 17
ItemLine = defclass(ItemLine, widgets.Panel)
ItemLine.ATTRS{
idx=DEFAULT_NIL,
is_selected_fn=DEFAULT_NIL,
is_hollow_fn=DEFAULT_NIL,
on_select=DEFAULT_NIL,
on_filter=DEFAULT_NIL,
on_clear_filter=DEFAULT_NIL,
}
function ItemLine:init()
self.frame.h = 2
self.visible = function() return #get_cur_filters() >= self.idx end
self:addviews{
widgets.Label{
view_id='item_symbol',
frame={t=0, l=0},
text=string.char(16), -- this is the "►" character
text_pen=COLOR_YELLOW,
auto_width=true,
visible=self.is_selected_fn,
},
widgets.Label{
view_id='item_desc',
frame={t=0, l=2},
text={
{text=self:callback('get_item_line_text'),
pen=function() return gui.invert_color(COLOR_WHITE, self.is_selected_fn()) end},
},
},
widgets.Label{
view_id='item_filter',
frame={t=0, l=28},
text={
{text=self:callback('get_filter_text'),
width=item_filter_chars+2,
rjustify=true,
pen=function() return
self:is_impossible() and COLOR_RED or
gui.invert_color(COLOR_LIGHTCYAN, self.is_selected_fn()) end},
},
auto_width=true,
on_click=function() self.on_filter(self.idx) end,
},
widgets.Label{
frame={t=0, l=47},
text='[clear]',
text_pen=COLOR_LIGHTRED,
auto_width=true,
visible=self:callback('has_filter'),
on_click=function() self.on_clear_filter(self.idx) end,
},
widgets.Label{
frame={t=1, l=2},
text={
{gap=2, text=function() return self.note end,
pen=function() return self.note_pen end},
},
},
}
end
function ItemLine:reset()
self.desc = nil
self.available = nil
end
function ItemLine:onInput(keys)
if keys._MOUSE_L and self:getMousePos() then
self.on_select(self.idx)
end
return ItemLine.super.onInput(self, keys)
end
function ItemLine:get_item_line_text()
local idx = self.idx
local filter = get_cur_filters()[idx]
local quantity = get_quantity(filter, self.is_hollow_fn())
local buildingplan = require('plugins.buildingplan')
self.desc = self.desc or buildingplan.get_desc(filter)
self.available = self.available or buildingplan.countAvailableItems(
uibs.building_type, uibs.building_subtype, uibs.custom_type, idx - 1)
if self.available >= quantity then
self.note_pen = COLOR_GREEN
self.note = (' %d available now'):format(self.available)
elseif self.available >= 0 then
self.note_pen = COLOR_BROWN
self.note = (' Will link next (need to make %d)'):format(quantity - self.available)
else
self.note_pen = COLOR_BROWN
self.note = (' Will link later (need to make %d)'):format(-self.available + quantity)
end
self.note = string.char(192) .. self.note -- character 192 is "└"
return ('%d %s%s'):format(quantity, self.desc, quantity == 1 and '' or 's')
end
function ItemLine:has_filter()
return require('plugins.buildingplan').hasFilter(
uibs.building_type, uibs.building_subtype, uibs.custom_type, self.idx-1)
end
function ItemLine:get_filter_text()
local buildingplan = require('plugins.buildingplan')
if not buildingplan.hasFilter(
uibs.building_type, uibs.building_subtype, uibs.custom_type, self.idx - 1)
then
return '[any material]'
end
local mats = buildingplan.getMaterialFilter(
uibs.building_type, uibs.building_subtype, uibs.custom_type, self.idx - 1)
local cats = buildingplan.getMaterialMaskFilter(
uibs.building_type, uibs.building_subtype, uibs.custom_type, self.idx - 1)
return filter_string(mats, cats, item_filter_chars)
end
-- short circuit version of the '[impossible filter]' case above
function ItemLine:is_impossible()
local buildingplan = require('plugins.buildingplan')
local mats = buildingplan.getMaterialFilter(
uibs.building_type, uibs.building_subtype, uibs.custom_type, self.idx-1)
local cats = buildingplan.getMaterialMaskFilter(
uibs.building_type, uibs.building_subtype, uibs.custom_type, self.idx - 1)
for _,props in pairs(mats) do
local enabled = props.enabled == 'true' and cats[props.category]
if enabled then return false end
end
return true
end
function ItemLine:reduce_quantity(used_quantity)
if not self.available then return end
local filter = get_cur_filters()[self.idx]
used_quantity = used_quantity or get_quantity(filter, self.is_hollow_fn())
self.available = self.available - used_quantity
end
local function get_placement_errors()
local out = ''
for _,str in ipairs(uibs.errors) do
if #out > 0 then out = out .. NEWLINE end
out = out .. str.value
end
return out
end
--------------------------------
-- QuickFilter
--
-- Used to store a table of the following format:
-- table<string, { label: string, mats: string[] }>
-- string: quick filter slot (must be strings because of the way persistence works)
-- label: string representation of the filter
-- mats: list of material names allowed by the filter
BUILDINGPLAN_FILTERS_KEY = "buildingplan/quick-filters"
-- old saves may use numbers as keys, which we convert to string keys on load
dfhack.onStateChange[BUILDINGPLAN_FILTERS_KEY] = function(sc)
if sc ~= SC_MAP_LOADED or df.global.gamemode ~= df.game_mode.DWARF then
return
end
local saved_filters = dfhack.persistent.getSiteData(BUILDINGPLAN_FILTERS_KEY, {})
local new_filters = {}
for k, v in pairs(saved_filters) do
if type(k) == 'number' then
new_filters[tostring(k)] = v
elseif type(k) == 'string' then
new_filters[k] = v
end
end
dfhack.persistent.saveSiteData(BUILDINGPLAN_FILTERS_KEY, new_filters)
end
QuickFilter = defclass(QuickFilter, widgets.Panel)
QuickFilter.ATTRS{
idx=DEFAULT_NIL,
on_click_fn=DEFAULT_NIL,
is_selected_fn=DEFAULT_NIL
}
function QuickFilter:init()
local label = ('%d.'):format(self.idx)
self.frame.w = 27
self.renaming = false
self:addviews {
widgets.Label {
frame = { t = 0, l = 0 },
text = string.char(16), -- this is the "►" character
text_pen = COLOR_YELLOW,
auto_width = true,
visible = self.is_selected_fn,
},
widgets.Label { frame = { t = 0, l = 2 }, text = label },
widgets.Label {
frame = { t = 0, l = 5, w = item_filter_chars + 2 },
text = { { text = self:callback('get_label_text'), pen = function() return COLOR_CYAN end } },
visible = function() return self.renaming == false end,
on_click = self:callback("on_click"),
},
widgets.EditField {
view_id = 'edit_field',
frame = { t = 0, l = 5, w = item_filter_chars + 2 },
text = "",
visible = function() return self.renaming == true end,
on_submit = function(text) self:submit_name(text) end,
},
widgets.Label {
frame = { t = 0, r = 0, w = 3 },
text = "[x]",
text_pen = COLOR_LIGHTRED,
visible = self:callback("slot_used"),
on_click = self:callback("clear")
}
}
end
function QuickFilter:onInput(keys)
if keys.LEAVESCREEN or keys._MOUSE_R then
if self.renaming then
self.subviews.edit_field:setFocus(false)
self.renaming = false
editing_filters_flag = false
return true
else
return false
end
end
return QuickFilter.super.onInput(self, keys)
end
function QuickFilter:on_click()
if dfhack.internal.getModifiers().shift and
self:slot_used() and not editing_filters_flag
then
self.subviews.edit_field:setText(self:get_label_text())
self.renaming = true
editing_filters_flag = true
self.subviews.edit_field:setFocus(true)
else
self.on_click_fn(self.idx) -- save/apply filter based on selected ItemLine
end
end
function QuickFilter:slot_used()
local quick_filters = dfhack.persistent.getSiteData(BUILDINGPLAN_FILTERS_KEY, {})
return quick_filters[self.idx] ~= nil
end
function QuickFilter:clear()
local quick_filters = dfhack.persistent.getSiteData(BUILDINGPLAN_FILTERS_KEY, {})
quick_filters[self.idx] = nil
dfhack.persistent.saveSiteData(BUILDINGPLAN_FILTERS_KEY, quick_filters)
end
function QuickFilter:get_label_text()
local quick_filters = dfhack.persistent.getSiteData(BUILDINGPLAN_FILTERS_KEY, {})
local set = quick_filters[self.idx]
if not set then
return "empty"
else
return set.label
end
end
function QuickFilter:submit_name(text)
local quick_filters = dfhack.persistent.getSiteData(BUILDINGPLAN_FILTERS_KEY, {})
quick_filters[self.idx].label = compress(text, item_filter_chars+2)
dfhack.persistent.saveSiteData(BUILDINGPLAN_FILTERS_KEY, quick_filters)
self.renaming = false
editing_filters_flag = false
end
--------------------------------
-- PlannerOverlay
--
PlannerOverlay = defclass(PlannerOverlay, overlay.OverlayWidget)
PlannerOverlay.ATTRS{
desc='Shows the building planner interface panel when building buildings.',
default_pos={x=5,y=9},
default_enabled=true,
viewscreens='dwarfmode/Building/Placement',
frame={w=56, h=32},
}
function PlannerOverlay:init()
self.selected = 1
self.state = ensure_key(config.data, 'planner')
self.selected_favorite = '1'
local main_panel = widgets.Panel{
view_id='main',
frame={t=1, l=0, r=0, h=14},
frame_style=gui.FRAME_INTERIOR_MEDIUM,
frame_background=gui.CLEAR_PEN,
visible=self:callback('is_not_minimized'),
}
local minimized_panel = widgets.Panel{
frame={t=0, r=1, w=20, h=1},
subviews={
widgets.Label{
frame={t=0, r=3, h=1},
text={
{text=' show Planner ', pen=pens.MINI_TEXT_PEN, hpen=pens.MINI_TEXT_HPEN},
{text='['..string.char(31)..']', pen=pens.MINI_BUTT_PEN, hpen=pens.MINI_BUTT_HPEN},
},
visible=self:callback('is_minimized'),
on_click=self:callback('toggle_minimized'),
},
widgets.Label{
frame={t=0, r=3, h=1},
text={
{text=' hide Planner ', pen=pens.MINI_TEXT_PEN, hpen=pens.MINI_TEXT_HPEN},
{text='['..string.char(30)..']', pen=pens.MINI_BUTT_PEN, hpen=pens.MINI_BUTT_HPEN},
},
visible=self:callback('is_not_minimized'),
on_click=self:callback('toggle_minimized'),
},
widgets.HelpButton{
frame={t=0, r=0},
command='buildingplan',
}
},
}
local function make_is_selected_fn(idx)
return function() return self.selected == idx end
end
local function on_select_fn(idx)
self.selected = idx
end
local function is_hollow_fn()
return self.subviews.hollow:getOptionValue()
end
local buildingplan = require('plugins.buildingplan')
main_panel:addviews{
widgets.Label{
frame={},
auto_width=true,
text='No items required.',
visible=function() return #get_cur_filters() == 0 end,
},
ItemLine{view_id='item1', frame={t=0, l=0, r=0}, idx=1,
is_selected_fn=make_is_selected_fn(1), is_hollow_fn=is_hollow_fn,
on_select=on_select_fn, on_filter=self:callback('set_filter'),
on_clear_filter=self:callback('clear_filter')},
ItemLine{view_id='item2', frame={t=2, l=0, r=0}, idx=2,
is_selected_fn=make_is_selected_fn(2), is_hollow_fn=is_hollow_fn,
on_select=on_select_fn, on_filter=self:callback('set_filter'),
on_clear_filter=self:callback('clear_filter')},
ItemLine{view_id='item3', frame={t=4, l=0, r=0}, idx=3,
is_selected_fn=make_is_selected_fn(3), is_hollow_fn=is_hollow_fn,
on_select=on_select_fn, on_filter=self:callback('set_filter'),
on_clear_filter=self:callback('clear_filter')},
ItemLine{view_id='item4', frame={t=6, l=0, r=0}, idx=4,
is_selected_fn=make_is_selected_fn(4), is_hollow_fn=is_hollow_fn,
on_select=on_select_fn, on_filter=self:callback('set_filter'),
on_clear_filter=self:callback('clear_filter')},
widgets.CycleHotkeyLabel{
view_id='hollow',
frame={b=4, l=1, w=21},
key='CUSTOM_H',
label='Hollow area:',
visible=is_construction,
options={
{label='No', value=false},
{label='Yes', value=true, pen=COLOR_GREEN},
},
},
widgets.CycleHotkeyLabel{
view_id='stairs_top_subtype',
frame={b=7, l=1, w=30},
key='CUSTOM_R',
label='Top stair type: ',
visible=is_multi_level_stairs,
options={
{label='Auto', value='auto'},
{label='UpDown', value=df.construction_type.UpDownStair},
{label='Down', value=df.construction_type.DownStair},
},
},
widgets.CycleHotkeyLabel {
view_id='stairs_bottom_subtype',
frame={b=6, l=1, w=30},
key='CUSTOM_B',
label='Bottom Stair Type:',
visible=is_multi_level_stairs,
options={
{label='Auto', value='auto'},
{label='UpDown', value=df.construction_type.UpDownStair},
{label='Up', value=df.construction_type.UpStair},
},
},
widgets.CycleHotkeyLabel{
view_id='stairs_only_subtype',
frame={b=7, l=1, w=30},
key='CUSTOM_R',
label='Single level stair:',
visible=is_single_level_stairs,
options={
{label='Up', value=df.construction_type.UpStair},
{label='UpDown', value=df.construction_type.UpDownStair},
{label='Down', value=df.construction_type.DownStair},
},
},
widgets.CycleHotkeyLabel { -- TODO: this thing also needs a slider
view_id='weapons',
frame={b=4, l=1, w=28},
key='CUSTOM_T',
key_back='CUSTOM_SHIFT_T',
label='Number of weapons:',
visible=is_weapon_or_spike_trap,
options=utils.tabulate(function(i) return {label='('..i..')', value=i, pen=COLOR_YELLOW} end, 1, 10),
on_change=function(val) weapon_quantity = val end,
},
widgets.ToggleHotkeyLabel {
view_id='engraved',
frame={b=4, l=1, w=22},
key='CUSTOM_T',
label='Engraved only:',
visible=is_slab,
on_change=function(val)
buildingplan.setSpecial(uibs.building_type, uibs.building_subtype, uibs.custom_type, 'engraved', val)
end,
},
widgets.ToggleHotkeyLabel {
view_id='empty',
frame={b=4, l=1, w=22},
key='CUSTOM_T',
label='Empty only:',
visible=is_cage,
on_change=function(val)
buildingplan.setSpecial(uibs.building_type, uibs.building_subtype, uibs.custom_type, 'empty', val)
end,
},
widgets.Panel{
visible=function() return #get_cur_filters() > 0 end,
subviews={
widgets.HotkeyLabel{
frame={b=2, l=1, w=22},
key='CUSTOM_F',
label=function()
return buildingplan.hasFilter(uibs.building_type, uibs.building_subtype, uibs.custom_type, self.selected - 1)
and 'Edit filter' or 'Set filter'
end,
on_activate=function() self:set_filter(self.selected) end,
},
widgets.HotkeyLabel{
frame={b=1, l=1, w=22},
key='CUSTOM_CTRL_D',
label='Delete filter',
on_activate=function() self:clear_filter(self.selected) end,
enabled=function()
return buildingplan.hasFilter(uibs.building_type, uibs.building_subtype, uibs.custom_type, self.selected - 1)
end
},
widgets.CycleHotkeyLabel{
view_id='show_favorites',
frame={b=0, l=1, w=22},
key='CUSTOM_CTRL_F',
label="",
option_gap=0,
options={
{ label='Show favorites', value = false },
{ label='Hide favorites', value = true },
},
initial_option=false,
on_change=function(new,_) self:show_hide_favorites(new) end,
},
widgets.CycleHotkeyLabel{
view_id='choose',
frame={b=0, l=24},
key='CUSTOM_Z',
label='Choose items:',
label_below=true,
options={
{label='With filters', value=0},
{
label=function()
local automaterial = itemselection.get_automaterial_selection(uibs.building_type)
return ('Last used (%s)'):format(automaterial or 'pick manually')
end,
value=2,
},
{label='Manually', value=1},
},
initial_option=0,
on_change=function(choose)
buildingplan.setChooseItems(uibs.building_type, uibs.building_subtype, uibs.custom_type, choose)
end,
},
widgets.CycleHotkeyLabel{
view_id='safety',
frame={b=2, l=24, w=25},
key='CUSTOM_G',
label='Building safety:',
options={
{label='Any', value=0},
{label='Magma', value=2, pen=COLOR_RED},
{label='Fire', value=1, pen=COLOR_LIGHTRED},
},
initial_option=0,
on_change=function(heat)
buildingplan.setHeatSafetyFilter(uibs.building_type, uibs.building_subtype, uibs.custom_type, heat)
end,
},
},
},
}
local divider_widget = widgets.Divider{
frame={t=10, l=0, r=0, h=1},
frame_style=gui.FRAME_INTERIOR_MEDIUM,
visible=self:callback('is_not_minimized'),
}
local error_panel = widgets.ResizingPanel{
view_id='errors',
frame={t=15, l=0, r=0},
frame_style=gui.BOLD_FRAME,
frame_background=gui.CLEAR_PEN,
visible=self:callback('is_not_minimized'),
}
error_panel:addviews{
widgets.WrappedLabel{
frame={t=0, l=1, r=0},
text_pen=COLOR_LIGHTRED,
text_to_wrap=get_placement_errors,
visible=function() return #uibs.errors > 0 end,
},
widgets.Label{
frame={t=0, l=1, r=0},
text_pen=COLOR_GREEN,
text='OK to build',
visible=function() return #uibs.errors == 0 end,
},
}
local prev_next_selector = widgets.Panel{
frame={h=1},
auto_width=true,
subviews={
widgets.HotkeyLabel{
frame={t=0, l=1, w=9},
key='CUSTOM_SHIFT_Q',
key_sep='\0',
label=': Prev/',
on_activate=function() self.selected = ((self.selected - 2) % #get_cur_filters()) + 1 end,
},
widgets.HotkeyLabel{
frame={t=0, l=2, w=1},
key='CUSTOM_Q',
on_activate=function() self.selected = (self.selected % #get_cur_filters()) + 1 end,
},
widgets.Label{
frame={t=0,l=10},
text='next item',
on_click=function() self.selected = (self.selected % #get_cur_filters()) + 1 end,
},
},
visible=function() return #get_cur_filters() > 1 end,
}
local black_bar = widgets.Panel{
frame={t=0, l=1, w=37, h=1},
frame_inset=0,
frame_background=gui.CLEAR_PEN,
visible=self:callback('is_not_minimized'),
subviews={
prev_next_selector,
},
}
local function make_is_selected_filter(idx)
return function () return self.selected_favorite == idx end
end
local favorites_panel = widgets.Panel{
view_id='favorites',
frame={t=15, l=0, r=0, h=11},
frame_style=gui.FRAME_INTERIOR_MEDIUM,
frame_background=gui.CLEAR_PEN,
visible=self:callback('show_favorites'),
subviews={
QuickFilter{idx='1', frame={t=0,l=0},
on_click_fn=self:callback("save_restore_filter"),
is_selected_fn=make_is_selected_filter('1') },
QuickFilter{idx='2', frame={t=1,l=0},
on_click_fn=self:callback("save_restore_filter"),
is_selected_fn=make_is_selected_filter('2') },
QuickFilter{idx='3', frame={t=2,l=0},
on_click_fn=self:callback("save_restore_filter"),
is_selected_fn=make_is_selected_filter('3') },
QuickFilter{idx='4', frame={t=3,l=0},
on_click_fn=self:callback("save_restore_filter"),
is_selected_fn=make_is_selected_filter('4') },
QuickFilter{idx='5', frame={t=4,l=0},
on_click_fn=self:callback("save_restore_filter"),
is_selected_fn=make_is_selected_filter('5') },
QuickFilter{idx='6', frame={t=0,l=27},
on_click_fn=self:callback("save_restore_filter"),
is_selected_fn=make_is_selected_filter('6') },
QuickFilter{idx='7', frame={t=1,l=27},
on_click_fn=self:callback("save_restore_filter"),
is_selected_fn=make_is_selected_filter('7') },
QuickFilter{idx='8', frame={t=2,l=27},
on_click_fn=self:callback("save_restore_filter"),
is_selected_fn=make_is_selected_filter('8') },
QuickFilter{idx='9', frame={t=3,l=27},
on_click_fn=self:callback("save_restore_filter"),
is_selected_fn=make_is_selected_filter('9') },
QuickFilter{idx='0', frame={t=4,l=27},
on_click_fn=self:callback("save_restore_filter"),
is_selected_fn=make_is_selected_filter('0') },
widgets.CycleHotkeyLabel {
view_id='slot_select',
frame={b=2, l=2},
key='CUSTOM_X',
key_back='CUSTOM_SHIFT_X',
label='next/previous slot',
auto_width=true,
options=utils.tabulate(function(i) return {label="", value=tostring(i)} end, 0, 9),
initial_option='1',
on_change=function(val) self.selected_favorite = val end,
},
widgets.HotkeyLabel{
frame={b=2, l=28},
label="set/apply selected",
key='CUSTOM_Y',
on_activate=function () self:save_restore_filter(self.selected_favorite) end,
},
widgets.TooltipLabel {
frame={b=0, l=2},
show_tooltip=true,
text="Shift+click to edit the label of a favorite",
},
}
}
self:addviews{
black_bar,
minimized_panel,
main_panel,
divider_widget,
error_panel,
favorites_panel
}
end
function PlannerOverlay:show_favorites()
return not self.state.minimized and self.subviews.show_favorites:getOptionValue()
end
function PlannerOverlay:show_hide_favorites(new)
local errors_frame = {t=15+(new and 11 or 0), l=0, r=0}
self.subviews.errors.frame = errors_frame
self:updateLayout()
end
function PlannerOverlay:save_restore_filter(slot)
self.selected_favorite = slot
local buildingplan = require('plugins.buildingplan')
local quick_filters = dfhack.persistent.getSiteData(BUILDINGPLAN_FILTERS_KEY, {})
if quick_filters[slot] then -- restore saved filter
buildingplan.setMaterialFilter(
uibs.building_type, uibs.building_subtype, uibs.custom_type, self.selected - 1,
quick_filters[slot].mats
)
else -- save current filter
if not buildingplan.hasFilter(
uibs.building_type, uibs.building_subtype, uibs.custom_type, self.selected - 1)
then return end