Repository navigation
Expand file tree
/
Copy pathcontrol.lua
More file actions
1285 lines (1137 loc) · 37.6 KB
/
Copy pathcontrol.lua
File metadata and controls
1285 lines (1137 loc) · 37.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 PLACEHOLDER = "{count}"
local UPDATE_INTERVAL = 10
local BLUEPRINT_TAG = "improved-display-panel"
local GUI_ROOT = "improved_display_panel_root"
local GUI_REFRESH = "improved_display_panel_refresh"
local GUI_TOGGLE = "improved_display_panel_toggle"
local GUI_TARGET = "improved_display_panel_target"
local GUI_SIGNAL = "improved_display_panel_signal"
local GUI_MOVE_SIGNAL = "improved_display_panel_move_signal"
local GUI_REMOVE_SIGNAL = "improved_display_panel_remove_signal"
local GUI_PANEL_TOGGLE = "improved_display_panel_expand_collapse"
local SPECIAL_SIGNALS = {
["signal-anything"] = true,
["signal-each"] = true,
["signal-everything"] = true,
}
-- GUI-open state is deliberately not persisted. Factorio closes entity GUIs when
-- a save is loaded, while the panel templates themselves live in `storage`.
local opened_panel_by_player = {}
local editors_by_panel = {}
local generator_by_player = {}
local gui_expanded_by_player = {}
local gui_location_by_player = {}
local function state()
storage.improved_display_panel = storage.improved_display_panel or {}
local mod_state = storage.improved_display_panel
mod_state.panels = mod_state.panels or {}
return mod_state
end
local function is_display_panel(entity)
return entity and entity.valid and entity.type == "display-panel" and entity.unit_number ~= nil
end
local function contains_placeholder(text)
return type(text) == "string"
and (string.find(text, PLACEHOLDER, 1, true) ~= nil
or string.find(text, "{count%d+}") ~= nil)
end
local function copy_templates(templates)
local result = {}
for index, template in pairs(templates or {}) do
if type(index) == "number" and type(template) == "string" and contains_placeholder(template) then
result[index] = template
end
end
return result
end
local function get_behavior(entity)
if not is_display_panel(entity) then
return nil
end
local behavior = entity.get_control_behavior()
if behavior and behavior.valid and behavior.type == defines.control_behavior.type.display_panel then
return behavior
end
return nil
end
local function has_templates(templates)
return templates and next(templates) ~= nil
end
local function unregister_panel(entity, clear_editors)
if entity and entity.valid and entity.unit_number then
state().panels[entity.unit_number] = nil
if clear_editors then
editors_by_panel[entity.unit_number] = nil
end
end
end
local function ensure_panel_info(entity)
local panels = state().panels
local unit_number = entity.unit_number
local info = panels[unit_number]
if not info then
info = {
entity = entity,
templates = {},
rendered = {},
}
panels[unit_number] = info
else
info.entity = entity
info.templates = info.templates or {}
info.rendered = info.rendered or {}
end
return info
end
local function concrete_condition_signal(record)
local condition = record and record.condition
local signal = condition and condition.first_signal
if not signal or not signal.name then
return nil, "missing"
end
if SPECIAL_SIGNALS[signal.name] then
return nil, "special"
end
return signal, nil
end
local function read_signal_count(entity, behavior, signal)
local selection = behavior.input_networks
local read_red = selection == nil or selection.red ~= false
local read_green = selection == nil or selection.green ~= false
if read_red and read_green then
return entity.get_signal(
signal,
defines.wire_connector_id.circuit_red,
defines.wire_connector_id.circuit_green
)
elseif read_red then
return entity.get_signal(signal, defines.wire_connector_id.circuit_red)
elseif read_green then
return entity.get_signal(signal, defines.wire_connector_id.circuit_green)
end
return 0
end
local function value_for_record(entity, behavior, record)
local signal, problem = concrete_condition_signal(record)
if not signal then
return nil, problem
end
return read_signal_count(entity, behavior, signal), nil
end
local SIGNAL_TYPE_BY_TAG = {
item = "item",
fluid = "fluid",
["virtual-signal"] = "virtual",
entity = "entity",
recipe = "recipe",
["space-location"] = "space-location",
["asteroid-chunk"] = "asteroid-chunk",
quality = "quality",
}
local TAG_BY_SIGNAL_TYPE = {
item = "item",
fluid = "fluid",
virtual = "virtual-signal",
entity = "entity",
recipe = "recipe",
["space-location"] = "space-location",
["asteroid-chunk"] = "asteroid-chunk",
quality = "quality",
}
local function inline_signals(template)
local signals = {}
if type(template) ~= "string" then
return signals
end
for tag_type, tag in string.gmatch(template, "%[([%a%-]+)=([^%]]+)%]") do
local signal_type = SIGNAL_TYPE_BY_TAG[tag_type]
if signal_type then
local name = string.match(tag, "^%s*([^,%s]+)")
local quality = string.match(tag, ",%s*quality%s*=%s*([^,%s]+)")
if name then
signals[#signals + 1] = {type = signal_type, name = name, quality = quality}
end
end
end
return signals
end
local function normalize_signal(signal)
if not signal or not signal.name then
return nil
end
local signal_type = signal.type
if signal_type == nil then
if prototypes.item[signal.name] then
signal_type = "item"
elseif prototypes.fluid[signal.name] then
signal_type = "fluid"
elseif prototypes.virtual_signal[signal.name] then
signal_type = "virtual"
end
end
if not TAG_BY_SIGNAL_TYPE[signal_type] then
return nil
end
return {
type = signal_type,
name = signal.name,
quality = signal.quality,
}
end
local function signal_exists(signal)
if not signal or not signal.name or SPECIAL_SIGNALS[signal.name] then
return false
end
local prototype
if signal.type == "item" then
prototype = prototypes.item[signal.name]
elseif signal.type == "fluid" then
prototype = prototypes.fluid[signal.name]
elseif signal.type == "virtual" then
prototype = prototypes.virtual_signal[signal.name]
elseif signal.type == "entity" then
prototype = prototypes.entity[signal.name]
elseif signal.type == "recipe" then
prototype = prototypes.recipe[signal.name]
elseif signal.type == "space-location" then
prototype = prototypes.space_location[signal.name]
elseif signal.type == "asteroid-chunk" then
prototype = prototypes.asteroid_chunk[signal.name]
elseif signal.type == "quality" then
prototype = prototypes.quality[signal.name]
end
return prototype ~= nil
and (signal.quality == nil or prototypes.quality[signal.quality] ~= nil)
end
local function has_numbered_inline_signals(text)
return type(text) == "string"
and string.find(text, "{count%d+}") ~= nil
and #inline_signals(text) > 0
end
local function render_template(template, default_value, item_value)
local signals = inline_signals(template)
local rendered = string.gsub(template, "%{count(%d+)%}", function(raw_index)
local signal = signals[tonumber(raw_index)]
local value = signal and signal.name and item_value(signal) or nil
return value == nil and "?" or tostring(value)
end)
local replacement = default_value == nil and "?" or tostring(default_value)
return (string.gsub(rendered, "%{count%}", replacement))
end
local function remove_placeholders(template)
local text = string.gsub(template, "[ \t]*%{count%d+%}", "")
return (string.gsub(text, "[ \t]*%{count%}", ""))
end
local function render_record(entity, behavior, record, template)
local default_value = value_for_record(entity, behavior, record)
return render_template(template, default_value, function(raw_signal)
local signal = normalize_signal(raw_signal)
if not signal_exists(signal) then
return nil
end
return read_signal_count(entity, behavior, signal)
end)
end
local function discover_literal_templates(entity)
local behavior = get_behavior(entity)
if not behavior then
return nil
end
local discovered = {}
for index = 1, behavior.records_count do
local record = behavior.get_record(index)
if contains_placeholder(record.text) then
discovered[index] = record.text
end
end
if not has_templates(discovered) then
return nil
end
local info = ensure_panel_info(entity)
for index, template in pairs(discovered) do
info.templates[index] = template
info.rendered[index] = nil
end
return info
end
local function reconcile_record(info, index, record)
local template = info.templates[index]
if not template then
return nil
end
local current_text = record.text
local last_rendered = info.rendered[index]
if current_text == template or (last_rendered and current_text == last_rendered) then
return template
end
-- Another script may have changed a record while its GUI was closed. A new
-- placeholder becomes the new template; ordinary text disables management.
if contains_placeholder(current_text) then
info.templates[index] = current_text
info.rendered[index] = nil
return current_text
end
info.templates[index] = nil
info.rendered[index] = nil
return nil
end
local function update_panel(entity)
if not is_display_panel(entity) then
return
end
local unit_number = entity.unit_number
if editors_by_panel[unit_number] and next(editors_by_panel[unit_number]) then
return
end
local info = state().panels[unit_number]
local behavior = get_behavior(entity)
if not info or not behavior then
return
end
for index, _ in pairs(info.templates) do
if index > behavior.records_count then
info.templates[index] = nil
info.rendered[index] = nil
else
local record = behavior.get_record(index)
local template = reconcile_record(info, index, record)
if template then
local rendered = render_record(entity, behavior, record, template)
if record.text ~= rendered then
record.text = rendered
behavior.set_record(index, record)
end
info.rendered[index] = rendered
end
end
end
if not has_templates(info.templates) then
unregister_panel(entity)
end
end
local function restore_templates_for_editing(entity)
local info = state().panels[entity.unit_number] or discover_literal_templates(entity)
local behavior = get_behavior(entity)
if not info or not behavior then
return
end
for index, _ in pairs(info.templates) do
if index <= behavior.records_count then
local record = behavior.get_record(index)
local template = reconcile_record(info, index, record)
if template and record.text ~= template then
record.text = template
behavior.set_record(index, record)
end
else
info.templates[index] = nil
info.rendered[index] = nil
end
end
if not has_templates(info.templates) then
unregister_panel(entity)
end
end
local function capture_templates_after_editing(entity)
local behavior = get_behavior(entity)
if not behavior then
unregister_panel(entity)
return nil
end
local templates = {}
for index = 1, behavior.records_count do
local record = behavior.get_record(index)
if contains_placeholder(record.text) then
templates[index] = record.text
end
end
if not has_templates(templates) then
unregister_panel(entity)
return nil
end
local info = ensure_panel_info(entity)
info.templates = templates
info.rendered = {}
return info
end
local function count_managed_records(entity)
local info = state().panels[entity.unit_number]
local count = 0
if info then
for _ in pairs(info.templates) do
count = count + 1
end
end
return count
end
local function generator_signals_from_record(behavior, index)
if index < 1 or index > behavior.records_count then
return {}
end
return inline_signals(behavior.get_record(index).text)
end
local function generator_for(player_index, entity, behavior)
local generator = generator_by_player[player_index]
if not generator or generator.unit_number ~= entity.unit_number then
generator = {
unit_number = entity.unit_number,
target_index = 1,
signals = generator_signals_from_record(behavior, 1),
}
generator_by_player[player_index] = generator
end
local maximum = math.max(behavior.records_count, 1)
generator.target_index = math.max(1, math.min(generator.target_index or 1, maximum))
generator.signals = generator.signals or {}
if generator.moving_index and generator.moving_index > #generator.signals then
generator.moving_index = nil
end
return generator
end
local function signal_tag(signal)
local tag_type = TAG_BY_SIGNAL_TYPE[signal.type]
local tag = "[" .. tag_type .. "=" .. signal.name
if signal.quality and signal.quality ~= "normal" then
tag = tag .. ",quality=" .. signal.quality
end
return tag .. "]"
end
local function build_generated_template(raw_signals)
local rows = {}
local signals = {}
for _, raw_signal in ipairs(raw_signals) do
local signal = normalize_signal(raw_signal)
if signal_exists(signal) then
signals[#signals + 1] = signal
rows[#rows + 1] = signal_tag(signal) .. ": {count" .. #signals .. "}"
end
end
return table.concat(rows, "\n"), signals
end
local function generate_record_template(entity, generator)
local behavior = get_behavior(entity)
if not behavior then
return false
end
local template, signals = build_generated_template(generator.signals)
if template == "" then
return false
end
local target_index = math.max(1, math.min(generator.target_index or 1,
math.max(behavior.records_count, 1)))
if behavior.records_count == 0 then
local added = behavior.add_record({
text = template,
icon = signals[1],
condition = {
first_signal = {type = "virtual", name = "signal-anything"},
comparator = ">",
constant = 0,
},
})
if not added then
return false
end
target_index = behavior.records_count
else
local record = behavior.get_record(target_index)
record.text = template
if not record.icon or not record.icon.name then
record.icon = signals[1]
end
if not record.condition or not record.condition.first_signal
or not record.condition.first_signal.name then
record.condition = {
first_signal = {type = "virtual", name = "signal-anything"},
comparator = ">",
constant = 0,
}
end
behavior.set_record(target_index, record)
end
local info = ensure_panel_info(entity)
info.templates[target_index] = template
info.rendered[target_index] = nil
generator.target_index = target_index
generator.signals = signals
return true
end
local function destroy_gui(player)
local screen_root = player.gui.screen[GUI_ROOT]
if screen_root and screen_root.valid then
local location = screen_root.location
gui_location_by_player[player.index] = {x = location.x, y = location.y}
screen_root.destroy()
end
local relative_root = player.gui.relative[GUI_ROOT]
if relative_root and relative_root.valid then
relative_root.destroy()
end
end
local function default_gui_location(player)
local scale = player.display_scale > 0 and player.display_scale or 1
local width = player.display_resolution.width / scale
local height = player.display_resolution.height / scale
local x = math.floor(width / 2 + 300)
if x + 420 > width - 8 then
x = math.max(8, math.floor(width - 428))
end
return {x = x, y = math.max(8, math.floor((height - 700) / 2))}
end
local function build_gui(player, entity)
destroy_gui(player)
if not is_display_panel(entity) then
return
end
local behavior = get_behavior(entity)
if not behavior then
return
end
local anchor = {
gui = defines.relative_gui_type.display_panel_gui,
position = defines.relative_gui_position.right,
}
if not gui_expanded_by_player[player.index] then
local collapsed_root = player.gui.relative.add({
type = "frame",
name = GUI_ROOT,
direction = "horizontal",
anchor = anchor,
})
collapsed_root.style.padding = 0
local expand_button = collapsed_root.add({
type = "sprite-button",
name = GUI_PANEL_TOGGLE,
sprite = "improved-display-panel-open-icon",
style = "tool_button_blue",
tooltip = {"improved-display-panel.gui-expand"},
})
expand_button.style.width = 40
expand_button.style.height = 40
return
end
local root = player.gui.screen.add({
type = "frame",
name = GUI_ROOT,
direction = "vertical",
})
root.location = gui_location_by_player[player.index] or default_gui_location(player)
root.style.width = 420
root.style.minimal_width = 420
root.style.maximal_width = 420
root.style.vertically_stretchable = false
local title_flow = root.add({
type = "flow",
direction = "horizontal",
style = "frame_header_flow",
})
title_flow.add({
type = "label",
caption = {"improved-display-panel.gui-title"},
style = "frame_title",
})
local title_filler = title_flow.add({
type = "empty-widget",
style = "draggable_space_header",
})
title_filler.style.height = 24
title_filler.style.horizontally_stretchable = true
title_filler.drag_target = root
title_flow.add({
type = "sprite-button",
name = GUI_PANEL_TOGGLE,
sprite = "utility/close",
style = "frame_action_button",
tooltip = {"improved-display-panel.gui-collapse"},
})
-- This screen-level frame is independent from the native display-panel layout,
-- so its natural height cannot stretch the native message rows.
local content = root.add({
type = "frame",
direction = "vertical",
style = "inside_shallow_frame",
})
content.style.horizontally_stretchable = true
local introduction = content.add({type = "flow", direction = "vertical"})
introduction.style.padding = 12
introduction.style.vertical_spacing = 4
local description = introduction.add({
type = "label",
caption = {"improved-display-panel.gui-description"},
})
description.style.single_line = false
description.style.maximal_width = 372
description.style.horizontally_stretchable = true
local generator = generator_for(player.index, entity, behavior)
local generator_header = content.add({
type = "frame",
direction = "horizontal",
style = "subheader_frame",
})
generator_header.style.horizontally_stretchable = true
generator_header.add({
type = "label",
caption = {"improved-display-panel.gui-generator-title"},
style = "subheader_caption_label",
})
local generator_body = content.add({type = "flow", direction = "vertical"})
generator_body.style.padding = 12
generator_body.style.vertical_spacing = 8
local target_flow = generator_body.add({type = "flow", direction = "horizontal"})
target_flow.style.vertical_align = "center"
target_flow.style.horizontal_spacing = 8
local target_label = target_flow.add({
type = "label",
caption = {"improved-display-panel.gui-target-message"},
})
target_label.style.minimal_width = 118
local target_items = {}
for index = 1, math.max(behavior.records_count, 1) do
target_items[index] = "#" .. index
end
local target_drop_down = target_flow.add({
type = "drop-down",
name = GUI_TARGET,
items = target_items,
selected_index = generator.target_index,
})
target_drop_down.style.width = 78
local target_spacer = target_flow.add({type = "empty-widget"})
target_spacer.style.horizontally_stretchable = true
target_flow.add({
type = "button",
name = GUI_REFRESH,
caption = {"improved-display-panel.gui-refresh"},
tooltip = {"improved-display-panel.gui-refresh-tooltip"},
})
generator_body.add({
type = "label",
caption = {"improved-display-panel.gui-signals-title"},
style = "semibold_label",
})
local signal_pane = generator_body.add({
type = "scroll-pane",
direction = "vertical",
style = "naked_scroll_pane",
})
signal_pane.style.maximal_height = 160
signal_pane.style.horizontally_stretchable = true
local signal_rows = signal_pane.add({type = "flow", direction = "vertical"})
signal_rows.style.vertical_spacing = 4
signal_rows.style.horizontally_stretchable = true
for index = 1, #generator.signals + 1 do
local row_cell = signal_rows.add({
type = "frame",
direction = "horizontal",
style = "shallow_frame",
})
row_cell.style.padding = 1
row_cell.style.horizontally_stretchable = true
row_cell.style.height = 34
local row = row_cell.add({type = "flow", direction = "horizontal"})
row.style.vertical_align = "center"
row.style.horizontal_spacing = 4
row.style.horizontally_stretchable = true
row.style.height = 32
local label = row.add({type = "label", caption = "#" .. index})
label.style.minimal_width = 22
label.style.left_margin = 3
local signal_button = row.add({
type = "choose-elem-button",
name = GUI_SIGNAL,
style = "slot_button",
elem_type = "signal",
signal = generator.signals[index],
tooltip = {"improved-display-panel.gui-generator-signal-tooltip"},
tags = {signal_index = index},
})
signal_button.style.width = 32
signal_button.style.height = 32
local row_spacer = row.add({type = "empty-widget"})
row_spacer.style.horizontally_stretchable = true
if generator.signals[index] then
local move_button = row.add({
type = "button",
name = GUI_MOVE_SIGNAL,
style = "improved_display_panel_drag_handle",
tooltip = {"improved-display-panel.gui-reorder-signal"},
tags = {signal_index = index},
})
move_button.toggled = generator.moving_index == index
local remove_button = row.add({
type = "sprite-button",
name = GUI_REMOVE_SIGNAL,
sprite = "utility/trash",
style = "tool_button_red",
tooltip = {"improved-display-panel.gui-remove-signal"},
tags = {signal_index = index},
})
remove_button.style.width = 28
remove_button.style.height = 28
remove_button.style.right_margin = 2
else
local move_spacer = row.add({type = "empty-widget"})
move_spacer.style.width = 28
local remove_spacer = row.add({type = "empty-widget"})
remove_spacer.style.width = 30
end
end
local summary = generator_body.add({type = "flow", direction = "horizontal"})
summary.style.vertical_align = "center"
summary.add({
type = "label",
caption = {
"improved-display-panel.gui-managed-count",
count_managed_records(entity),
},
})
local summary_spacer = summary.add({type = "empty-widget"})
summary_spacer.style.horizontally_stretchable = true
local records_header = content.add({
type = "frame",
direction = "horizontal",
style = "subheader_frame",
})
records_header.style.horizontally_stretchable = true
records_header.add({
type = "label",
caption = {"improved-display-panel.gui-records-title"},
style = "subheader_caption_label",
})
local records_body = content.add({type = "flow", direction = "vertical"})
records_body.style.padding = 12
records_body.style.vertical_spacing = 8
if behavior.records_count == 0 then
local empty = records_body.add({
type = "label",
caption = {"improved-display-panel.gui-no-records"},
})
empty.style.single_line = false
empty.style.maximal_width = 372
return
end
local pane = records_body.add({type = "flow", direction = "vertical"})
pane.style.vertical_spacing = 8
pane.style.horizontally_stretchable = true
local info = state().panels[entity.unit_number]
for index = 1, behavior.records_count do
local record = behavior.get_record(index)
local _, problem = concrete_condition_signal(record)
local condition_signal = record.condition and record.condition.first_signal or nil
local numbered_supported = has_numbered_inline_signals(record.text)
or (info and has_numbered_inline_signals(info.templates[index]))
local supported = problem == nil or numbered_supported
local managed = (info and info.templates[index] ~= nil) or contains_placeholder(record.text)
local row = pane.add({type = "flow", direction = "horizontal"})
row.style.vertical_align = "center"
row.style.horizontal_spacing = 8
row.style.height = 32
local index_label = row.add({type = "label", caption = "#" .. index})
index_label.style.minimal_width = 28
local signal_button = row.add({
type = "choose-elem-button",
style = "slot_button",
elem_type = "signal",
signal = condition_signal,
locked = true,
tooltip = not supported and {"improved-display-panel.gui-unsupported-signal"} or nil,
})
signal_button.style.width = 32
signal_button.style.height = 32
local toggle = row.add({
type = "checkbox",
name = GUI_TOGGLE,
state = managed,
enabled = supported,
caption = {"improved-display-panel.gui-show-count"},
tooltip = not supported and {"improved-display-panel.gui-unsupported-signal"}
or {"improved-display-panel.gui-show-count-tooltip"},
tags = {record_index = index},
})
toggle.style.horizontally_stretchable = true
-- Keep a reference in scope so Factorio validates creation before the next row.
if not signal_button.valid then
return
end
end
end
local function mark_editor_open(player_index, entity)
local previous = opened_panel_by_player[player_index]
if previous and previous.valid and previous.unit_number ~= entity.unit_number then
local previous_editors = editors_by_panel[previous.unit_number]
if previous_editors then
previous_editors[player_index] = nil
end
end
opened_panel_by_player[player_index] = entity
local editors = editors_by_panel[entity.unit_number] or {}
editors[player_index] = true
editors_by_panel[entity.unit_number] = editors
end
local function release_editor(player_index)
local entity = opened_panel_by_player[player_index]
opened_panel_by_player[player_index] = nil
generator_by_player[player_index] = nil
gui_expanded_by_player[player_index] = nil
if not entity or not entity.valid or not entity.unit_number then
return nil, false
end
local editors = editors_by_panel[entity.unit_number]
if editors then
editors[player_index] = nil
if not next(editors) then
editors_by_panel[entity.unit_number] = nil
return entity, true
end
end
return entity, false
end
local function enable_record(entity, index)
local behavior = get_behavior(entity)
if not behavior or index < 1 or index > behavior.records_count then
return
end
local record = behavior.get_record(index)
local signal = concrete_condition_signal(record)
-- Factorio stores the text of a newly added, untouched message as nil.
local template = record.text or ""
if not contains_placeholder(template) then
if not signal then
return
end
template = template == "" and PLACEHOLDER or (template .. " " .. PLACEHOLDER)
record.text = template
behavior.set_record(index, record)
elseif not signal and not has_numbered_inline_signals(template) then
return
end
local info = ensure_panel_info(entity)
info.templates[index] = template
info.rendered[index] = nil
end
local function disable_record(entity, index)
local behavior = get_behavior(entity)
if not behavior or index < 1 or index > behavior.records_count then
return
end
local info = state().panels[entity.unit_number]
local record = behavior.get_record(index)
local template = (info and info.templates[index]) or record.text
if contains_placeholder(template) then
record.text = remove_placeholders(template)
behavior.set_record(index, record)
end
if info then
info.templates[index] = nil
info.rendered[index] = nil
if not has_templates(info.templates) then
unregister_panel(entity)
end
end
end
local function payload_templates(tags)
local payload = tags and tags[BLUEPRINT_TAG]
if type(payload) ~= "table" or payload.version ~= 1 or type(payload.templates) ~= "table" then
return nil
end
local result = {}
for raw_index, template in pairs(payload.templates) do
local index = tonumber(raw_index)
if index and index >= 1 and index == math.floor(index)
and type(template) == "string" and contains_placeholder(template) then
result[index] = template
end
end
return has_templates(result) and result or nil
end
local function apply_blueprint_tags(entity, tags)
if not is_display_panel(entity) then
return false
end
local templates = payload_templates(tags)
if not templates then
return false
end
local info = ensure_panel_info(entity)
info.templates = templates
info.rendered = {}
update_panel(entity)
return true
end
local function setup_blueprint(event)
local blueprint = event.record
if not blueprint and event.stack and event.stack.valid_for_read and event.stack.is_blueprint then
blueprint = event.stack
end
if not blueprint or not blueprint.valid then
return
end
local mapping = event.mapping.get()
for blueprint_index, entity in pairs(mapping) do
if is_display_panel(entity) then
local info = state().panels[entity.unit_number] or discover_literal_templates(entity)
if info and has_templates(info.templates) then
blueprint.set_blueprint_entity_tag(blueprint_index, BLUEPRINT_TAG, {
version = 1,
templates = copy_templates(info.templates),
})
else
blueprint.set_blueprint_entity_tag(blueprint_index, BLUEPRINT_TAG, nil)
end
end
end
end
local function scan_existing_panels()
local panels = state().panels
for unit_number, info in pairs(panels) do
if not info.entity or not info.entity.valid or info.entity.type ~= "display-panel" then
panels[unit_number] = nil
else
info.templates = copy_templates(info.templates)
info.rendered = info.rendered or {}