z_mirror/StatsGui

git clone https://git.raiguard.me/z_mirror/StatsGui

Caleb HeuerHide the stats GUI when entering the train GUI57acba4

trunk
2.1 KiB76 linesraw
1local sensors = require("scripts.sensors")
2
3local stats_gui = {}
4
5function stats_gui.build(player, player_table)
6  local single_line = player_table.settings.single_line
7  local style = "statsgui_frame"
8  if player_table.settings.adjust_for_clock then
9    style = style .. "_clock"
10  end
11  if not player_table.settings.adjust_for_ups then
12    style = style .. "_no_ups"
13  end
14
15  local window = player.gui.screen.add({
16    type = "frame",
17    style = style,
18    direction = single_line and "horizontal" or "vertical",
19    ignored_by_interaction = true,
20    visible = player.controller_type ~= defines.controllers.cutscene,
21  })
22
23  player_table.stats_window = window
24
25  stats_gui.update(player, player_table)
26end
27
28function stats_gui.destroy(player_table)
29  local stats_window = player_table.stats_window
30  if stats_window and stats_window.valid then
31    stats_window.destroy()
32    player_table.stats_window = nil
33  end
34end
35
36--- @param player LuaPlayer
37function stats_gui.update(player, player_table)
38  local window = player_table.stats_window
39  if not window or not window.valid then
40    stats_gui.build(player, player_table)
41    window = player_table.stats_window
42  end
43  local children = window.children
44
45  local i = 0
46  for _, sensor in pairs(sensors) do
47    local caption = sensor(player)
48    if caption then
49      i = i + 1
50      local label = children[i]
51      if label then
52        label.caption = caption
53      else
54        window.add({
55          type = "label",
56          style = "statsgui_label",
57          caption = caption,
58        })
59      end
60    end
61  end
62  -- remove extra children
63  for j = i + 1, #children do
64    children[j].destroy()
65  end
66
67  local in_remote = player.controller_type == defines.controllers.remote
68  local in_train_gui = player.opened_gui_type == defines.gui_type.entity and player.opened.type == "locomotive"
69  local location = window.location
70  location.y = in_remote and (36 * player.display_scale) or 0
71  window.location = location
72  window.style.width = (player.display_resolution.width / player.display_scale)
73  window.visible = not in_train_gui
74end
75
76return stats_gui