This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
evoGUI.lua
290 lines (238 loc) · 9.89 KB
/
evoGUI.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
require "value_sensors.day_time"
require "value_sensors.evolution_factor"
require "value_sensors.kill_count"
require "value_sensors.play_time"
require "value_sensors.player_locations"
require "value_sensors.pollution_around_player"
require "value_sensors.remote_sensor"
require "settingsGUI"
require "remote"
if not evogui then evogui = {} end
function evogui.mod_init()
if not global.settings then global.settings = {} end
if not global.settings.update_delay then global.settings.update_delay = 60 end
for _, player in pairs(game.players) do
evogui.create_player_globals(player)
evogui.create_sensor_display(player)
end
end
local function mod_update_0_4_205()
-- 0.4.204 to 0.4.205: Factorio 0.15.22 introduced a bug wherein a
-- GUI element with more than 4 characters in its name, which was
-- shorter than the_mod_name + 4 characters, would get deleted on
-- load. At this time, the EvoGUI root element switched from
-- gui.top.evoGUI to gui.top.evogui_root.
--
-- We need to clean up the leftovers for people updating EvoGUI
-- from any other version of Factorio now.
for _, player in pairs(game.players) do
if player.gui.top.evoGUI then
player.gui.top.evoGUI.destroy()
end
end
end
function evogui.mod_update(data)
if data.mod_changes then
if data.mod_changes["{{MOD_NAME}}"] then
-- TODO: If a more major migration ever needs doing, do that here.
-- Otherwise, just falling back to mod_init should work fine.
evogui.mod_init()
mod_update_0_4_205()
end
evogui.validate_sensors(data.mod_changes)
end
end
function evogui.on_gui_click(event)
if string.starts_with(event.element.name, "evogui_settings_gui_") then
evogui.on_settings_click(event)
elseif event.element.name == "evoGUI_toggle_popup" then
evogui.evoGUI_toggle_popup(event)
elseif string.starts_with(event.element.name, "evogui_sensor_") then
for _, sensor in pairs(evogui.value_sensors) do
-- if the gui element name matches 'evogui_sensor_' + sensor_name, send it the on_click event.
if string.starts_with(event.element.name, "evogui_sensor_" .. sensor.name) then
sensor:on_click(event)
break
end
end
end
end
-- Iterate through all value_sensors, if any are associated with a mod_name that
-- has been removed, remove the sensor from the list of value_sensors.
function evogui.validate_sensors(mod_changes)
for i = #evogui.value_sensors, 1, -1 do
local sensor = evogui.value_sensors[i]
if sensor.mod_name and mod_changes[sensor.mod_name] then
-- mod removed, remove sensor from ui
if mod_changes[sensor.mod_name].new_version == nil then
evogui.hide_sensor(sensor)
table.remove(evogui.value_sensors, i)
end
end
end
end
function evogui.hide_sensor(sensor)
for player_name, data in pairs(global.evogui) do
if data.always_visible then
data.always_visible[sensor["name"]] = false
end
end
for _, player in pairs(game.players) do
local player_settings = global.evogui[player.name]
local sensor_flow = player.gui.top.evogui_root.sensor_flow
evogui.update_av(player, sensor_flow.always_visible)
end
end
function evogui.new_player(event)
local player = game.players[event.player_index]
evogui.create_player_globals(player)
evogui.create_sensor_display(player)
end
function evogui.update_gui(event)
if (event.tick % global.settings.update_delay) ~= 0 then return end
for player_index, player in pairs(game.players) do
local player_settings = global.evogui[player.name]
-- saves converted from SP with no username to MP won't raise evogui.new_player
-- so we have to check here, as well.
if not player_settings then
evogui.new_player({player_index = player_index})
player_settings = global.evogui[player.name]
elseif not player.gui.top.evogui_root then
evogui.create_sensor_display(player)
end
local sensor_flow = player.gui.top.evogui_root.sensor_flow
evogui.update_av(player, sensor_flow.always_visible)
if player_settings.popup_open then
evogui.update_ip(player, sensor_flow.in_popup)
end
end
end
function evogui.create_player_globals(player)
if not global.evogui then global.evogui = {} end
if not global.evogui[player.name] then global.evogui[player.name] = {} end
local player_settings = global.evogui[player.name]
if not player_settings.version then player_settings.version = "" end
if not player_settings.always_visible then
player_settings.always_visible = {
["evolution_factor"] = true,
["play_time"] = true,
}
end
if not player_settings.in_popup then
player_settings.in_popup = {
["day_time"] = true,
}
end
if not player_settings.popup_open then player_settings.popup_open = false end
if not player_settings.sensor_settings then
player_settings.sensor_settings = {}
end
if not player_settings.sensor_settings['player_locations'] then
player_settings.sensor_settings['player_locations'] = {
['show_player_index'] = false,
['show_position'] = false,
['show_surface'] = false,
['show_direction'] = true,
['show_offline'] = false,
}
elseif player_settings.sensor_settings['player_locations'].show_offline == nil then
-- 0.4.3 new feature (783e3d68)
player_settings.sensor_settings['player_locations'].show_offline = false
end
if not player_settings.sensor_settings['day_time'] then
player_settings.sensor_settings['day_time'] = {
['show_day_number'] = false,
['minute_rounding'] = true,
}
end
if not player_settings.sensor_settings['evolution_factor'] then
player_settings.sensor_settings['evolution_factor'] = {
['extra_precision'] = false,
}
end
if not player_settings.sensor_settings['play_time'] then
player_settings.sensor_settings['play_time'] = {
['show_days'] = true,
['show_seconds'] = true,
}
end
end
function evogui.create_sensor_display(player)
local root = player.gui.top.evogui_root
local destroyed = false
if root then
player.gui.top.evogui_root.destroy()
destroyed = true
end
if not root or destroyed then
local root = player.gui.top.add{type="frame",
name="evogui_root",
direction="horizontal",
style="EvoGUI_outer_frame_no_border"}
local action_buttons = root.add{type="flow",
name="action_buttons",
direction="vertical",
style="EvoGUI_cramped_flow_v"}
action_buttons.add{type="button",
name="evoGUI_toggle_popup",
style="EvoGUI_expando_closed"}
if global.evogui[player.name].popup_open then
action_buttons.evoGUI_toggle_popup.style = "EvoGUI_expando_open"
end
action_buttons.add{type="button",
name="evogui_settings_gui_settings_open",
style="EvoGUI_settings"}
local sensor_flow = root.add{type="flow",
name="sensor_flow",
direction="vertical",
style="EvoGUI_cramped_flow_v"}
sensor_flow.add{type="flow",
name="always_visible",
direction="vertical",
style="EvoGUI_cramped_flow_v"}
sensor_flow.add{type="flow",
name="in_popup",
direction="vertical",
style="EvoGUI_cramped_flow_v"}
end
end
local function update_sensors(element, sensor_list, active_sensors)
for _, sensor in ipairs(sensor_list) do
if active_sensors[sensor.name] then
local status, err = pcall(sensor.create_ui, sensor, element)
if err then error({"err_specific", sensor.name, "create_ui", err}) end
status, err = pcall(sensor.update_ui, sensor, element)
if err then error({"err_specific", sensor.name, "update_ui", err}) end
else
local status, err = pcall(sensor.delete_ui, sensor, element)
if err then error({"err_specific", sensor.name, "delete_ui", err}) end
end
end
end
function evogui.update_av(player, element)
local always_visible = global.evogui[player.name].always_visible
update_sensors(element, evogui.value_sensors, always_visible)
end
function evogui.update_ip(player, element)
if not global.evogui[player.name].popup_open then return end
local in_popup = global.evogui[player.name].in_popup
update_sensors(element, evogui.value_sensors, in_popup)
end
function evogui.evoGUI_toggle_popup(event)
local player = game.players[event.player_index]
local player_settings = global.evogui[player.name]
local root = player.gui.top.evogui_root
if player_settings.popup_open then
-- close it
player_settings.popup_open = false
for _, childname in ipairs(root.sensor_flow.in_popup.children_names) do
root.sensor_flow.in_popup[childname].destroy()
end
root.action_buttons.evoGUI_toggle_popup.style = "EvoGUI_expando_closed"
else
-- open it
player_settings.popup_open = true
evogui.update_ip(player, root.sensor_flow.in_popup)
root.action_buttons.evoGUI_toggle_popup.style = "EvoGUI_expando_open"
end
end