forked from Choumiko/SmartTrains
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.lua
1321 lines (1179 loc) · 59.1 KB
/
gui.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
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
function page_count(item_count, items_per_page)
return math.floor((item_count - 1) / (items_per_page)) + 1
end
function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
GUI = {
styleprefix = "st_",
defaultStyles = {
label = "label",
button = "button",
checkbox = "checkbox"
},
windows = {
root = "stGui",
settings = {"settings"},
trainInfo = {"trainInfo"}},
stationMapping = "stationMapping",
position = "left",
create_or_update = function(player_index)
local player = game.players[player_index]
if player.valid then
local main = GUI.buildGui(player)
if player.opened then
GUI.showSettingsButton(main)
if player.opened.type == "locomotive" then
GUI.showTrainInfoWindow(main, player_index)
elseif player.opened.type == "train-stop" then
if not main.frameMapping or not main.frameMapping.valid then
GUI.add(main, { type = "frame", name = "frameMapping", caption = "Station mapping", direction = "vertical", style = "st_frame" })
end
GUI.showStationMapping(player_index)
end
GUI.showTrainLinesWindow(main, player_index)
else
GUI.destroy(player_index)
end
end
end,
buildGui = function (player)
GUI.destroyGui(player.gui[GUI.position][GUI.windows.root])
local stGui = GUI.add(player.gui[GUI.position], {type="frame", name="stGui", direction="vertical", style="outer_frame"})
return GUI.add(stGui, {type="table", name="rows", column_count=1})
end,
showSettingsButton = function(parent)
local gui = parent
if gui.toggleSTSettings ~= nil then
gui.toggleSTSettings.destroy()
end
GUI.addButton(gui, {name="toggleSTSettings", caption = {"text-st-settings"}})
end,
destroyGui = function (guiA)
if guiA ~= nil and guiA.valid then
guiA.destroy()
end
end,
destroy = function(player_index)
local player
if type(player_index) == "number" then
player = game.players[player_index]
else
player = player_index
end
if player.valid then
GUI.destroyGui(player.gui[GUI.position][GUI.windows.root])
global.guiData[player.index] = nil
if global.playerRules[player_index] then
global.playerRules[player_index].page = 1
end
end
end,
add = function(parent, e, bind)
local type = e.type
if not e.style and (type == "button" or type == "label") then
e.style = "st_"..type
end
if bind then
if type == "checkbox" then
e.state = global[bind]
end
end
if type == "checkbox" and not (e.state == true or e.state == false) then
e.state = false
end
--insert this element into a frame for padding purposes
if e.left_padding then
local name = nil
if e.name then
name = "padding_frame__"..e.name
end
local top = ""
if e.top_padding then
top ="top_"
end
local frame = parent.add({type="frame", name=name, style="st_frame_padding_"..top.."left_"..e.left_padding})
e.left_padding = nil
e.top_padding = nil
return frame.add(e)
end
return parent.add(e)
end,
addButton = function(parent, e, bind)
e.type="button"
if not e.style then
e.style = "st_button_style"
end
return GUI.add(parent, e, bind)
end,
addLabel = function(parent, e_, bind)
local e = e_
if type(e) == "string" or type(e) == "number" or (type(e) == "table" and e[1]) then
e = {caption=e}
end
e.type="label"
return GUI.add(parent,e,bind)
end,
addTextfield = function(parent, e, bind)
e.type="textfield"
return GUI.add(parent, e, bind)
end,
addPlaceHolder = function(parent, count)
local c = count or 1
for _=1,c do
GUI.add(parent, {type="label", caption=""})
end
end,
globalSettingsWindow = function(index, parent)
local gui = parent or game.players[index].gui[GUI.position].stGui.rows
if gui.globalSettings == nil then
gui.add({type = "frame", name="globalSettings", direction="vertical", caption={"text-st-global-settings"}})
local refueling = gui.globalSettings.add({type = "frame", name="frm_refueling", direction="vertical", style = "st_inner_frame", caption = {"stg-refueling"}})
local coal_min = fuel_value_to_coal(global.settings.refuel.rangeMin)
local coal_max = fuel_value_to_coal(global.settings.refuel.rangeMax)
local tbl = refueling.add{type="table", name="tbl", column_count=4}
GUI.addLabel(tbl, {"stg-refuel-below1"})
GUI.addTextfield(tbl, {name="refuelRangeMin", style="st_textfield_small", text = global.settings.refuel.rangeMin})
GUI.addLabel(tbl, {"", {"stg-MJ"}, " ("..coal_min.." ", game.item_prototypes["coal"].localised_name, ")",{"stg-refuel-below2"}})
local r = GUI.add(tbl, {type="flow", name="row1", direction="horizontal"})
GUI.addTextfield(r, {name="refuelRangeMax", style="st_textfield_small", text = global.settings.refuel.rangeMax})
GUI.addLabel(r, {"", {"stg-MJ"}, " ("..coal_max.." ", game.item_prototypes["coal"].localised_name,")"})
GUI.addLabel(tbl, {"stg-max-refuel-time"})
GUI.addTextfield(tbl, {name="refuelTime", style="st_textfield_small", text = global.settings.refuel.time / 60})
GUI.addLabel(tbl, {"stg-refuel-station"})
GUI.addTextfield(tbl, {name="refuelStation", style="st_textfield_medium", text = global.settings.refuel.station})
GUI.addButton(refueling, {name="setRefuel", caption="Set Refuel", tooltip="Set refuel for all trains"})
local intervals = gui.globalSettings.add({type = "frame", name="frm_intervals", direction="horizontal", style = "st_inner_frame", caption = {"stg-intervals"}})
tbl = intervals.add{type="table", name="tbl", column_count=2, style="st_table"}
GUI.addLabel(tbl, {"stg-intervals-write"}).style.left_padding = 10
GUI.addTextfield(tbl, {name="intervals_write", style="st_textfield_small", text = global.settings.intervals.write})
GUI.addLabel(tbl, {"",{"stg-tracked-trains"}, " ", TrainList.getCount()})
local noStations, uniqueStations = 0,0
local force = game.players[index].force.name
for _,station in pairs(global.stationCount[force]) do
if station > 0 then
noStations = noStations + station
uniqueStations = uniqueStations + 1
end
end
GUI.addLabel(tbl,{"", {"lbl-stations"}, ": ", uniqueStations, "/", noStations})
GUI.addButton(tbl, {name="globalSettingsSave", caption="Save"})
end
end,
showTrainInfoWindow = function(parent, player_index)
local gui = parent
if gui.trainSettings ~= nil then
gui.trainSettings.destroy()
end
local t = TrainList.getTrainInfoFromUI(player_index)
if not t then
return
end
--log(serpent.line(trainInfo))
--log(serpent.line({kUI=keyUI, trainkey=trainKey, t=t}))
local trainLine = t.line and global.trainLines[t.line] or false
local useMapping = trainLine and trainLine.settings.useMapping or false
gui = GUI.add(gui, {type="frame", name="trainSettings", caption={"", {"lbl-train"}, ": ", t.name, " (", t:getType(),")"}, direction="vertical", style="st_frame"})
local line = "-"
local dated = " "
if trainLine then
line = trainLine.name
if trainLine.changed ~= t.lineVersion and
t.lineVersion >= 0 then dated = {"lbl-outdated"} end
end
local tableRows = GUI.add(gui, {type="table", name="rows", column_count=1})
local checkboxes = GUI.add(tableRows, {type="table", name="checkboxes", column_count=2})
if not trainLine then
GUI.add(checkboxes, {type="checkbox", name="btn_refuel__"..t.ID, caption={"lbl-refuel"}, state=t.settings.autoRefuel})
end
local tbl = GUI.add(tableRows, {type="table", name="line", column_count=2})
GUI.addLabel(tbl, {"", {"lbl-active-line"}, ": ", line})
GUI.addLabel(tbl, {"", " ", dated})
local lineKey = ""
local records = t.train.schedule and t.train.schedule.records
local rules
if trainLine then
records = trainLine.records
rules = trainLine.rules
lineKey = "__"..trainLine.name
end
local tbl1 = GUI.add(tableRows, {type="table", name="tbl1", column_count=3, style="st_table"})
local spp = global.settings.stationsPerPage
local page = global.playerPage[player_index].schedule or 1
if records and #records > 0 then
GUI.addLabel(tbl1, {"lbl-station"})
GUI.addLabel(tbl1, {"lbl-leave-when"})
GUI.addPlaceHolder(tbl1)
local start = (page-1) * spp + 1
local max = start + spp - 1
if max > #records then max = #records end
for i=start, max do
local s = records[i]
local chunks = {}
local chunk = {}
GUI.addLabel(tbl1, i.." ".. (s.station or "Temporary"))
if s.wait_conditions then
for c_index, condition in pairs(s.wait_conditions) do
if c_index > 1 then
table.insert(chunk, {"", " ", condition.compare_type == "or" and {"lbl-or"} or "&", " "})
end
if condition.type == "time" then
table.insert(chunk, {"lbl-time", math.floor(condition.ticks/60)})
elseif condition.type == "circuit" then
table.insert(chunk, {"lbl-wait-for-circuit"})
elseif condition.type == "item_count" or condition.type == "fluid_count" then
if condition.condition then
if condition.condition.first_signal then
table.insert(chunk, {"", "[", condition.condition.first_signal.type, "=", condition.condition.first_signal.name, "] ", condition.condition.comparator, " ", condition.condition.constant})
else
table.insert(chunk, {"", "? ", condition.condition.comparator, "", condition.condition.constant})
end
else
table.insert(chunk, {"", "? "})
end
else
table.insert(chunk, {"lbl-"..condition.type})
end
end
end
--local time = (trainLine and rules[i] and rules[i].keepWaiting) and {"lbl-forever"} or (s.time_to_wait > 12010 and {"lbl-forever"}) or math.floor(s.time_to_wait/60)
table.insert(chunks, chunk)
if rules then
local rule = rules[i]
if rule.jumpToCircuit or rule.jumpTo then
table.insert(chunk, ": ")
end
if rule.jumpToCircuit then
table.insert(chunk, {"lbl-jump-to-signal"})
if rule.jumpTo then
table.insert(chunk, " or ")
end
end
if rule.jumpTo and not useMapping and rule.jumpTo <= #records then
table.insert(chunks, {{"lbl-jump-to"}, "", rule.jumpTo})
elseif rule.jumpTo and useMapping and global.stationMap[game.players[player_index].force.name][rule.jumpTo] then
local mappedID = t:get_first_matching_station(rule.jumpTo, i)
if mappedID and mappedID>0 then
table.insert(chunks, {{"lbl-jump-to"}, "", mappedID , " (mapped #", rule.jumpTo, ')'})
else
table.insert(chunks, {"invalid #", " (mapped #", rule.jumpTo, ')'}) --TODO: localisation
end
elseif rule.jumpTo then
table.insert(chunks, {"invalid #"}) --TODO: localisation
end
end
local text = {""}
local c = 1
for _, chunk_ in pairs(chunks) do
for _, bit in pairs(chunk_) do
if c == 19 then
table.insert(text, " ...")
goto continue
end
table.insert(text, bit)
c = c + 1
end
end
::continue::
local lbl_conditions = GUI.addLabel(tbl1, text)
lbl_conditions.style.maximal_width = 250
GUI.addPlaceHolder(tbl1)
end
end
local btns = GUI.add(tableRows,{type="table", name="btns", column_count=2})
local pages = GUI.add(btns, {type="flow", name="pages", direction="horizontal", style = "st_flow"})
if records and #records > spp then
if page > 1 then
GUI.addButton(pages, {name = "firstPageTrain", caption = "|<", style = "st_page_button"})
GUI.addButton(pages, {name="prevPageTrain__"..page, caption="<", style="st_page_button"})
else
GUI.addButton(pages, {caption = "|<", style = "st_page_button_disabled"})
GUI.addButton(pages, {name="asdfFoo", caption="<", style="st_page_button_disabled"})
end
GUI.addButton(pages, {caption=page.."/"..math.ceil(#records/spp), style="st_page_button_disabled"})
if math.ceil(#records/spp) >= page+1 then
GUI.addButton(pages, {name="nextPageTrain__"..page, caption=">", style="st_page_button"})
GUI.addButton(pages, {name = "lastPageTrain", caption = ">|", style = "st_page_button"})
else
GUI.addButton(pages, {name="asdfFoo2", caption= ">", style="st_page_button_disabled"})
GUI.addButton(pages, {caption = ">|", style = "st_page_button_disabled"})
end
else
GUI.addPlaceHolder(pages)
end
GUI.addPlaceHolder(btns)
local buttonFlow = GUI.add(btns, {type = "flow", name = "btnFlow", direction = "horizontal"})
GUI.addButton(buttonFlow, {name="saveAsLine__"..t.ID..lineKey, caption={"lbl-save-as-line"}})
local line_ = GUI.addTextfield(buttonFlow, {name="saveAslineName", text="", style="st_textfield_big"})
if trainLine then
line_.text = trainLine.name
end
end,
showTrainLinesWindow = function(gui, player_index)
local trainInfo = TrainList.getTrainInfoFromUI(player_index)
local trainKey = trainInfo and trainInfo.ID or 0
if gui.trainLines ~= nil then
gui.trainLines.destroy()
end
local page = global.playerPage[player_index].line or 1
local c_lines=0
local trainCount = {}
for l,_ in pairs(global.trainLines) do
trainCount[l] = 0
c_lines = c_lines+1
end
if global.trainLines and c_lines > 0 then
trainKey = trainKey or 0
gui = GUI.add(gui, {type="frame", name="trainLines", caption={"lbl-trainlines"}, direction="vertical", style="st_frame"})
local tbl = GUI.add(gui, {type="table", name="tbl1", column_count=8})
GUI.addLabel(tbl, {"lbl-trainline"})
GUI.addLabel(tbl, {"lbl-1st-station"})
GUI.addLabel(tbl, {"lbl-number-stations"})
GUI.addLabel(tbl, {"lbl-number-trains"})
if trainKey > 0 then
GUI.addLabel(tbl, {"lbl-active"})
else
GUI.addPlaceHolder(tbl)
end
GUI.addLabel(tbl, {"lbl-marked"})
GUI.addLabel(tbl,{"lbl-refuel"})
GUI.addPlaceHolder(tbl)
local dirty = 0
local spp = global.settings.linesPerPage
local start = (page-1) * spp + 1
local max = start + spp - 1
for _,t in pairs(global.trains) do
if t.line and trainCount[t.line] then
trainCount[t.line] = trainCount[t.line] + 1
end
end
local c_trains
for i, l in pairsByKeys(global.trainLines, sortByName) do
dirty= dirty+1
if dirty >= start and dirty <= max then
c_trains = trainCount[l.name] or 0
--log(serpent.block(l,{comment=false}))
GUI.addLabel(tbl, l.name)
GUI.addLabel(tbl, l.records[1].station)
GUI.addLabel(tbl, #l.records)
GUI.addLabel(tbl, c_trains)
if trainKey > 0 then
GUI.add(tbl, {type="checkbox", name="activeLine__"..i.."__"..trainKey, state=(i==trainInfo.line), style="st_checkbox"})
else
GUI.addPlaceHolder(tbl)
end
GUI.add(tbl, {type="checkbox", name="markedDelete__"..i.."__"..trainKey, state=false})
GUI.add(tbl,{type="checkbox", name="lineRefuel__"..i.."__"..trainKey, state=l.settings.autoRefuel})
GUI.addButton(tbl, {name="editRules__"..i, caption={"lbl-rules"}, style="st_button_style_bold"})
end
end
local buttonFlow = GUI.add(gui,{name="buttonFlow", type="flow"})
local pageButtons = GUI.add(buttonFlow, {name="btns", type="flow", style = "st_flow"})
if dirty > spp then
if page > 1 then
GUI.addButton(pageButtons, {name="prevPageLine__"..page, caption="<", style="st_page_button"})
else
GUI.addButton(pageButtons, {caption="<", style="st_page_button_disabled"})
end
GUI.addButton(pageButtons, {caption=page.."/"..math.ceil(dirty/spp), style="st_page_button_disabled"})
if max < c_lines then
GUI.addButton(pageButtons, {name="nextPageLine__"..page, caption=">",style="st_page_button"})
else
GUI.addButton(pageButtons, {caption=">", style="st_page_button_disabled"})
end
end
if dirty == 0 then gui.destroy() end
GUI.addButton(buttonFlow, {name="deleteLines", caption={"lbl-delete-marked"}})
GUI.addButton(buttonFlow,{name="renameLine", caption={"lbl-rename"}})
GUI.addTextfield(buttonFlow,{name="newName", text="", style="st_textfield_big"})
end
end,
showStationMapping = function(player_index)
local player = game.players[player_index]
local gui = player.gui[GUI.position].stGui.rows.frameMapping
if gui.stationMapping ~= nil then
gui.stationMapping.destroy()
end
if gui.buttonFlow ~= nil then
gui.buttonFlow.destroy()
end
local guiData = global.guiData[player_index]
guiData.mapping = guiData.mapping or {}
local page = global.playerPage[player_index].mapping or 1
local c=0
for _, count in pairs(global.stationCount[player.force.name]) do
if count then -- count could be 0 for stations that are still in a line but have no actual/existing station anymore
c = c+1
end
end
local dirty = 1
local spp = global.settings.mappingsPerPage
local start = (page-1) * spp + 1
local max = start + spp - 1
local tbl = GUI.add( gui, { type = "table", name = "stationMapping", column_count = 5 } )
local c1 = 1
for name, count in pairsByKeys(global.stationCount[player.force.name], sortByName) do
if dirty >= start and dirty <= max then
GUI.add( tbl, { type = "label", name = "station_map_label_" .. c1, caption = name } )
local text = global.guiData[player_index].mapping[name] or ""
GUI.add( tbl, { type = "textfield", name = "station_map_" .. c1, style = "st_textfield_small", text = text } )
if c1 % 2 == 1 then
GUI.add( tbl, { type = "label", caption = " "})
end
c1 = c1 + 1
end
dirty= dirty+1
end
local buttonFlow = GUI.add(gui,{name="buttonFlow", type="flow"})
local btns = GUI.add(buttonFlow, {name="pageButtons", type="flow", style = "st_flow"})
if dirty > spp then
if page > 1 then
GUI.addButton(btns, {name="firstPageMapping", caption="|<", style="st_page_button"})
GUI.addButton(btns, {name="prevPageMapping__"..page, caption="<", style="st_page_button"})
else
GUI.addButton(btns, {caption="|<", style="st_page_button_disabled"})
GUI.addButton(btns, {caption="<", style="st_page_button_disabled"})
end
GUI.addButton(btns, {caption=page.."/"..math.ceil(dirty/spp), style="st_page_button_disabled"})
if max < c then
GUI.addButton(btns, {name="nextPageMapping__"..page, caption=">",style="st_page_button"})
GUI.addButton(btns,{name="lastPageMapping", caption=">|", style="st_page_button"})
else
GUI.addButton(btns, {caption=">", style="st_page_button_disabled"})
GUI.addButton(btns,{caption=">|", style="st_page_button_disabled"})
end
end
GUI.addButton( buttonFlow, { caption = "Save", name = "saveMapping" } )
end,
showDynamicRules = function(index, line, rule_button, stay_opened)
--debugDump({i=index,line=line,station=stationKey, tr=trainKey}, true)
local gui = game.players[index].gui[GUI.position].stGui
local guiData = global.guiData[index]
if gui.dynamicRules ~= nil then
gui.dynamicRules.destroy()
if guiData.line == line then
if guiData.rules_button and guiData.rules_button.valid then
guiData.rules_button.style = "st_button_style_bold"
end
if not stay_opened then
--log("not stay open")
guiData.line = false
guiData.line_number = false
guiData.use_mapping = false
return
end
end
end
if line and global.trainLines[line] then
local records = guiData.records
local rules = guiData.rules
if rule_button and rule_button.valid then
rule_button.style = "st_selected_button"
guiData.rules_button = rule_button
end
guiData.line = line
local lineName = global.trainLines[line].name
local line_number = guiData.line_number
local use_mapping = guiData.use_mapping
gui = GUI.add(gui, {type="frame", name="dynamicRules", direction="vertical", style="st_frame"})
gui = GUI.add(gui, {type="frame", name="frm", direction="vertical", style="st_inner_frame"})
local flow = GUI.add(gui, {type="flow", name="rulesFlow", direction="horizontal"})
GUI.addLabel(flow, {name="line", caption="Line: "..lineName}) --TODO localisation
GUI.addLabel(flow, {name="line_number", caption="#: "})
GUI.addTextfield( flow, {name="lineNumber__"..line, style="st_textfield_small", text=line_number})
GUI.add( flow, { type = "checkbox", name = "useMapping__" .. line, style = "st_checkbox", caption = "use station mapping", state = use_mapping } ) --TODO localisation
local tbl = GUI.add(gui,{type="table", name="tbltophdr", column_count=3, style="st_table"})
GUI.addPlaceHolder(tbl,1)
GUI.add(tbl, {name="tophdr_flow1", type="flow", direction="horizontal"})
local top_hdr2 = GUI.add(tbl, {name="tophdr_flow2", type="flow", direction="horizontal"})
GUI.addLabel(top_hdr2, {caption=" "})
GUI.addLabel(top_hdr2, {caption={"lbl-go-to-header"}, style="st_label_bold"})
GUI.addPlaceHolder(tbl,1)
GUI.add(tbl, {name="tophdr_flow3", type="flow", direction="horizontal"})
local test_table2 = GUI.add(tbl, {name="tophdr_flow4", type="flow", direction="horizontal"})
GUI.addLabel(test_table2, {caption={"lbl-jump-to-signal-header"}, style="st_label_bold"})
GUI.addLabel(test_table2, {caption=" "})
GUI.addLabel(test_table2, {caption={"lbl-jump-to-header"}, style="st_label_bold"})
GUI.addLabel(test_table2, {caption=" "})
local page = global.playerRules[index].page or 1
local upper = page*global.settings.rulesPerPage
local lower = page*global.settings.rulesPerPage-global.settings.rulesPerPage
for i,s in pairs(records) do
if i>lower and i<=upper then
local rule = rules[i]
local states = {
jumpToCircuit = rule and rule.jumpToCircuit or false,
jumpTo = (rule and rule.jumpTo) and rule.jumpTo or "",
}
GUI.addLabel(tbl, {caption="#" .. i .. " " .. s.station, style="st_label_bold"})
GUI.add(tbl, {name="rules_flow_a"..i, type="flow", direction="horizontal"})
local record1 = GUI.add(tbl, {name="rules_flow_b"..i, type="flow", direction="horizontal"})
GUI.add(record1,{type="checkbox", name="jumpToCircuit__"..i, style="st_checkbox", state=states.jumpToCircuit, left_padding=15})
GUI.addLabel(record1, {caption=" "})
GUI.addTextfield(record1, {name="jumpTo__"..i, text=states.jumpTo, style="st_textfield_small", left_padding=8})
end
end
local buttonFlow = GUI.add(gui,{name="buttonFlow", type="flow"})
local pageButtons = GUI.add(buttonFlow, {name="pageButtons", type="flow", style = "st_flow"})
local firstPage = GUI.addButton(pageButtons,{name="firstPageRule", caption="|<", style="st_page_button"})
local prevPage = GUI.addButton(pageButtons,{name="prevPageRule", caption="<", style="st_page_button"})
if page == 1 then
firstPage.style = "st_page_button_disabled"
prevPage.style = "st_page_button_disabled"
end
local maxPage = page_count(#records, global.settings.rulesPerPage)
GUI.addButton(pageButtons,{name="rule_page_number", caption=page.."/"..maxPage, style="st_disabled_button_bold"})
local nextPage = GUI.addButton(pageButtons, {name="nextPageRule", caption=">", style="st_page_button"})
local lastPage = GUI.addButton(pageButtons, {name = "lastPageRule", caption = ">|", style = "st_page_button"})
if page == maxPage then
nextPage.style = "st_page_button_disabled"
lastPage.style = "st_page_button_disabled"
end
GUI.addButton(buttonFlow, {name="saveRules__"..line, caption="Save", style="st_button_style_bold"})
end
end,
find_relative = function(e, name_, option2, flow)
local name = name_..option2
return e.parent.parent.parent["rules_flow_"..flow..option2]["padding_frame__"..name][name]
end,
get_station_options = function(e, option2)
return {
jumpToCircuit = GUI.find_relative(e, "jumpToCircuit__", option2, "b"),
jumpTo = GUI.find_relative(e, "jumpTo__", option2, "b"),
}
end,
get_mapping_from_gui = function(player)
local tbl = player.gui[GUI.position].stGui.rows.frameMapping.stationMapping
--local tbl = element.parent.parent.stationMapping
local mappings = tbl.children_names
for _, name in pairs(mappings) do
if string.starts_with(name, "station_map_label_") then
local i = tonumber(name:match("station_map_label_*(%w*)"))
local station = tostring(tbl[name].caption)
local text = trim(tbl["station_map_" .. i].text)
if text ~= "" then
text = tonumber(text)
else
text = false
end
if text == nil then
-- no valid number, restore from saved mapping
text = global.stationMapping[player.force.name][station]
player.print("Invalid mapping for "..station..", '" .. tbl["station_map_" .. i].text .. "' is not a number") --TODO localisation
end
global.guiData[player.index].mapping[station] = text or false
end
end
end,
save_station_options = function(opts, index, option2)
local rules = global.guiData[index].rules[tonumber(option2)] or {}
rules.jumpToCircuit = opts.jumpToCircuit.state
rules.jumpTo = opts.jumpTo.text
global.guiData[index].rules[tonumber(option2)] = rules
end,
}
function sanitizeName(name_)
local name = string.gsub(name_, "_", " ")
name = string.gsub(name, "^%s", "")
name = string.gsub(name, "%s$", "")
local pattern = "(%w+)__([%w%s%-%#%!%$]*)_*([%w%s%-%#%!%$]*)_*(%w*)"
local element = "activeLine__"..name.."__".."something"
local t1,t2,t3,_ = element:match(pattern)
if t1 == "activeLine" and t2 == name and t3 == "something" then --TODO something?? really..
return name
else
return false
end
end
function sanitizeNumber(number, default)
number = tonumber(number)
return (number ~= "" and number) or default
end
function sanitize_rules(player, line, rules, page)
--local page = global.playerRules[player.index].page or 1
local upper = page*global.settings.rulesPerPage
local lower = page*global.settings.rulesPerPage-global.settings.rulesPerPage
local gui = player.gui[GUI.position].stGui.dynamicRules.frm.tbltophdr
for i, rule in pairs(rules) do
if i>lower and i<=upper and gui["rules_flow_b"..i] then
rule.jumpTo = sanitizeNumber(gui["rules_flow_b"..i]["padding_frame__jumpTo__"..i]["jumpTo__"..i].text, false) or false
rule.station = tostring(global.trainLines[line].records[i].station)
end
end
return rules
end
on_gui_checked_state_changed = {
on_gui_checked_state_changed = function(event)
-- log("on checked: " .. serpent.block(event.element.name))
-- log("state: " .. serpent.block(event.element.state))
local elementName = event.element.name
local status, err = pcall(function()
local player = game.players[event.player_index]
local refresh = false
local element = event.element
if on_gui_checked_state_changed[element.name] then
log("Element name:" .. element.name)
refresh = on_gui_checked_state_changed[element.name](player)
else
local option1, option2, option3, _ = event.element.name:match("(%w+)__([%w%s%-%#%!%$]*)_*([%w%s%-%#%!%$]*)_*(%w*)")
--log("Options: " .. serpent.line(option1) .. " " .. serpent.line(option2) .. " " .. serpent.line(option3))
if on_gui_checked_state_changed[option1] then
refresh = on_gui_checked_state_changed[option1](player, option2, option3, element)
end
end
if refresh then
GUI.create_or_update(event.player_index)
end
end)
if not status then
pauseError(err, {"on_gui_click", elementName})
end
end,
jumpToCircuit = function(player, option2, _, element)
local opts = GUI.get_station_options(element, option2)
log("get_station_options")
log(serpent.block(opts))
GUI.save_station_options(opts, player.index, option2)
end,
refuel = function(_, option2, _, element)
option2 = tonumber(option2)
global.trains[option2].settings.autoRefuel = element.state--not global.trains[option2].settings.autoRefuel
end,
activeLine = function(player, option2, option3)
local trainKey = tonumber(option3)
local li = option2
local t = global.trains[trainKey]
if t.line ~= li then
t.line = li
t.lineVersion = -1
if t.train.speed == 0 then
if not t:updateLine() then
t.scheduleUpdate = game.tick + 60
insertInTable(global.scheduleUpdate, t.scheduleUpdate, t)
end
end
else
t.line = false
local schedule = t.train.schedule
t.train.schedule = schedule
end
t.lineVersion = -1
GUI.create_or_update(player.index)
end,
lineRefuel = function(_, option2, option3)
local line = option2
local trainKey = tonumber(option3)
local t = global.trains[trainKey]
if line and global.trainLines[line] then
line = global.trainLines[line]
line.settings.autoRefuel = not line.settings.autoRefuel
line.changed = game.tick
if t and t.line and t.line == line.name then
t.settings.autoRefuel = line.settings.autoRefuel
t.lineVersion = line.changed
end
end
return true
end,
}
on_gui_click = {
add_trains_to_update = function(line, newConditions)
if not newConditions then return end
for i, train in pairs(global.trains) do
if train and train.line and train.line == line and train.train.valid
and train.train.state == defines.train_state.wait_station and not train.opened then
if newConditions[train.train.schedule.current] then
local schedule = train.train.schedule
schedule.records[train.train.schedule.current].wait_conditions = newConditions[train.train.schedule.current]
train.train.schedule = schedule
end
end
end
end,
on_gui_click = function(event)
if event.element.type == "checkbox" then
return
end
local elementName = event.element.name
local status, err = pcall(function()
local player = game.players[event.player_index]
local refresh = false
local element = event.element
if on_gui_click[element.name] then
refresh = on_gui_click[element.name](player)
else
local option1, option2, option3, _ = event.element.name:match("(%w+)__([%w%s%-%#%!%$]*)_*([%w%s%-%#%!%$]*)_*(%w*)")
if on_gui_click[option1] then
refresh = on_gui_click[option1](player, option2, option3, element)
end
end
if refresh then
GUI.create_or_update(event.player_index)
end
end)
if not status then
pauseError(err, {"on_gui_click", elementName})
end
end,
toggleSTSettings = function(player)
if player.gui[GUI.position].stGui.rows.globalSettings == nil then
GUI.globalSettingsWindow(player.index)
GUI.destroyGui(player.gui[GUI.position].stGui.rows.frameMapping)
GUI.destroyGui(player.gui[GUI.position].stGui.rows.toggleSTSettings)
GUI.destroyGui(player.gui[GUI.position].stGui.rows.dynamicRules)
GUI.destroyGui(player.gui[GUI.position].stGui.rows.trainSettings)
GUI.destroyGui(player.gui[GUI.position].stGui.rows.trainLines)
return false
else
player.gui[GUI.position].stGui.rows.toggleSTSettings.destroy()
return true
end
end,
globalSettingsSave = function(player)
local settings = player.gui[GUI.position].stGui.rows.globalSettings
local refueling = settings.frm_refueling.tbl
local time = sanitizeNumber(refueling.refuelTime.text, global.settings.refuel.time/60)*60
local min = sanitizeNumber(refueling.refuelRangeMin.text, global.settings.refuel.rangeMin)
local max = sanitizeNumber(refueling.row1.refuelRangeMax.text, global.settings.refuel.rangeMax)
local station = refueling.refuelStation.text
global.settings.refuel = {time=time, rangeMin = min, rangeMax = max, station = station}
local intervals = settings.frm_intervals.tbl
local i_inactivity = 120 --sanitizeNumber(intervals.intervals_inactivity.text, global.settings.intervals.inactivity)
local i_write = sanitizeNumber(intervals.intervals_write.text, global.settings.intervals.write)
if i_write < 1 then i_write = 1 end
global.settings.intervals = {write = i_write, inactivity = i_inactivity}
return true
end,
deleteLines = function(player)
local group = player.gui[GUI.position].stGui.rows.trainLines.tbl1
local trainKey
for _, child in pairs(group.children_names) do
local pattern = "(markedDelete)__([%w%s%-%#%!%$]*)_*(%d*)"
local del, line, trainkey = child:match(pattern)
if del and group[child].state == true then
trainKey = tonumber(trainkey)
if trainKey > 0 then
if global.trains[trainKey] and global.trains[trainKey].line == line then
global.trains[trainKey].line = false
end
end
if global.trainLines[line] then
global.trainLines[line] = nil
end
end
end
global.playerPage[player.index].line = 1
update_station_numbers()
return true
end,
prepareRulesWindow = function(player)
local guiData = global.guiData[player.index]
local line = guiData.line
local maxPage = page_count(#global.trainLines[line].records, global.settings.rulesPerPage)
local page = global.playerRules[player.index].page
local rulesFlow = player.gui[GUI.position].stGui.dynamicRules.frm.rulesFlow
local textfield = rulesFlow["lineNumber__"..line]
guiData.line_number = math.floor(sanitizeNumber(textfield.text,0))
local use_mapping = rulesFlow["useMapping__" .. line].state
guiData.use_mapping = use_mapping
guiData.rules = sanitize_rules(player,line,guiData.rules, page)
return guiData, page, maxPage, line
end,
nextPageRule = function(player)
local guiData, page, maxPage, line = on_gui_click.prepareRulesWindow(player)
page = page < maxPage and page + 1 or page
global.playerRules[player.index].page = page
GUI.showDynamicRules(player.index, line, guiData.rules_button, true)
return false
end,
prevPageRule = function(player)
local guiData, page, maxPage, line = on_gui_click.prepareRulesWindow(player)
page = page > 1 and page - 1 or 1
global.playerRules[player.index].page = page
GUI.showDynamicRules(player.index, line, guiData.rules_button, true)
return false
end,
firstPageRule = function(player)
local guiData, page, maxPage, line = on_gui_click.prepareRulesWindow(player)
page = 1
global.playerRules[player.index].page = page
GUI.showDynamicRules(player.index, line, guiData.rules_button, true)
return false
end,
lastPageRule = function(player)
local guiData, page, maxPage, line = on_gui_click.prepareRulesWindow(player)
page = maxPage
global.playerRules[player.index].page = page
GUI.showDynamicRules(player.index, line, guiData.rules_button, true)
return false
end,
renameLine = function(player)
local group = player.gui[GUI.position].stGui.rows.trainLines.tbl1
local rename
local count=0
local newName = player.gui[GUI.position].stGui.rows.trainLines.buttonFlow.newName.text
for _, child in pairs(group.children_names) do
local pattern = "(markedDelete)__([%w%s%-%#%!%$]*)_*(%d*)"
local del, line, _ = child:match(pattern)
if del and group[child].state == true then
count = count+1
rename = line
end
end
if count == 1 then
newName = sanitizeName(newName)
if newName ~= false then
if newName ~= "" and not global.trainLines[newName] then
global.trainLines[newName] = table.deepcopy(global.trainLines[rename])
global.trainLines[newName].name = newName
global.trainLines[rename] = nil
for _,t in pairs(global.trains) do
if t.line == rename then
t.line = newName
end
end
end
else
debugDump("Invalid name, only letters, numbers, space, -,#,!,$ are allowed",true) --TODO localisation
end
update_station_numbers()
return true
end
update_station_numbers()
return false
end,
editRules = function(player, option2, _, element)
--GUI.destroyGui(player.gui[GUI.position].stGui.settings.toggleSTSettings)
GUI.destroyGui(player.gui[GUI.position].stGui.rows.trainSettings)
local guiData = global.guiData[player.index]
if guiData.rules_button and guiData.rules_button.valid then
guiData.rules_button.style = "st_button_style_bold"
end
--log("write rules to guiData")
local line = global.trainLines[option2]
guiData.line_number = line.settings.number
guiData.use_mapping = line.settings.useMapping
guiData.records = table.deepcopy(line.records)
guiData.rules = table.deepcopy(line.rules)
global.playerRules[player.index].page = 1
GUI.showDynamicRules(player.index, option2, element)
end,
saveRules = function(player, option2)
local line = option2
local trainline = global.trainLines[line]
--local gui = player.gui[GUI.position].stGui.dynamicRules.frm.tbl
local rulesFlow = player.gui[GUI.position].stGui.dynamicRules.frm.rulesFlow
local textfield = rulesFlow["lineNumber__"..line]
trainline.settings.number = math.floor(sanitizeNumber(textfield.text,0))
local use_mapping = rulesFlow["useMapping__" .. line].state
trainline.settings.useMapping = use_mapping
trainline.rules = table.deepcopy(sanitize_rules(player, line, global.guiData[player.index].rules, global.playerRules[player.index].page))
trainline.changed = game.tick
global.guiData[player.index] = {}
global.playerRules[player.index].page = 1
debugDump("Saved line "..line.." with "..#global.trainLines[line].records.." stations",true)
GUI.destroyGui(player.gui[GUI.position].stGui.dynamicRules)
return true
end,
saveAsLine = function(player, option2)
local name = player.gui[GUI.position].stGui.rows.trainSettings.rows.btns.btnFlow.saveAslineName.text
name = sanitizeName(name)
if name ~= false then
option2 = tonumber(option2)
local t = global.trains[option2]
local is_copy = t.line and t.line ~= name