-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.lua
More file actions
507 lines (424 loc) · 14.4 KB
/
Copy pathgui.lua
File metadata and controls
507 lines (424 loc) · 14.4 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
-- GUI management for ∞ Space Platform Automation
-- Screen GUI with Remote View visibility control
local naming = require("naming")
local gui = {}
local PANEL_NAME = "spa_main_panel"
local TOGGLE_BUTTON_NAME = "spa_toggle_button"
--- Get all space locations for dropdown
--- @param player LuaPlayer The player
--- @return table Array of space location names
local function get_space_location_dropdown_items(player)
local items = {}
local mapping = {}
for name, proto in pairs(prototypes.space_location) do
local display_name = proto.localised_name or name
table.insert(items, display_name)
mapping[#items] = name
end
return items, mapping
end
--- Get force platforms for copy dropdown
--- @param force LuaForce The force
--- @param player LuaPlayer|nil Optional player for debug logging
--- @return table Array of platform names
local function get_platform_dropdown_items(force, player)
local items = {"-- No Copy --"} -- Plain string for consistency
local mapping = {[1] = nil}
local player_data = player and storage.player_data[player.index]
local debug = player_data and player_data.debug_logging
if debug then
player.print("[∞ SPA DEBUG] Loading platforms for dropdown...")
end
local total_count = 0
local valid_count = 0
for _, platform in pairs(force.platforms) do
total_count = total_count + 1
if platform and platform.valid then
table.insert(items, platform.name)
mapping[#items] = platform.index
valid_count = valid_count + 1
if debug then
player.print("[∞ SPA DEBUG] Added platform: '" .. platform.name .. "' (index: " .. platform.index .. ")")
end
end
end
if debug then
player.print("[∞ SPA DEBUG] Platform scan complete: " .. total_count .. " total, " .. valid_count .. " valid, " .. (#items - 1) .. " in dropdown")
end
return items, mapping
end
--- Blueprint a platform and store it for reuse
--- @param player LuaPlayer The player
--- @param platform_index uint The platform to blueprint
--- @return boolean success Whether blueprint succeeded
local function cache_platform_blueprint(player, platform_index)
-- Find the platform
local source_platform = nil
for _, p in pairs(player.force.platforms) do
if p.index == platform_index then
source_platform = p
break
end
end
if not source_platform or not source_platform.valid then
return false
end
local source_surface = source_platform.surface
if not source_surface or not source_surface.valid then
player.print("[∞ Space Platform Automation] Error: Source platform surface not ready")
return false
end
-- Calculate bounding box
local entities = source_surface.find_entities()
if #entities == 0 then
storage.player_data[player.index].cached_blueprint_string = nil
return true
end
local min_x, min_y = math.huge, math.huge
local max_x, max_y = -math.huge, -math.huge
for _, entity in pairs(entities) do
if entity.valid then
local pos = entity.position
min_x = math.min(min_x, pos.x - 2)
min_y = math.min(min_y, pos.y - 2)
max_x = math.max(max_x, pos.x + 2)
max_y = math.max(max_y, pos.y + 2)
end
end
-- Create and export blueprint
local inventory = game.create_inventory(1)
if not inventory then return false end
inventory.insert({name = "blueprint"})
local blueprint = inventory[1]
if not blueprint or not blueprint.valid_for_read then
inventory.destroy()
return false
end
blueprint.create_blueprint{
surface = source_surface,
force = source_platform.force,
area = {{min_x, min_y}, {max_x, max_y}},
always_include_tiles = true,
include_entities = true,
include_modules = true,
include_station_names = true,
include_trains = true,
include_fuel = true
}
-- Export to string for storage
local blueprint_string = blueprint.export_stack()
inventory.destroy()
storage.player_data[player.index].cached_blueprint_string = blueprint_string
local player_data = storage.player_data[player.index]
if player_data and player_data.debug_logging then
player.print("[∞ Space Platform Automation] Platform blueprinted successfully")
end
return true
end
--- Get status text for display
--- @param player LuaPlayer The player
--- @return LocalisedString Status text
function gui.get_status_text(player)
local player_data = storage.player_data[player.index]
if not player_data.copy_platform_index then
return {"space-platform-automation.status-no-platform"}
end
if not player_data.enabled then
return {"space-platform-automation.status-disabled"}
end
-- Count pending platforms
local pending_count = 0
for _, pending in pairs(storage.pending_platforms) do
if pending.force_index == player.force.index and pending.needs_copy then
pending_count = pending_count + 1
end
end
if pending_count > 0 then
return {"space-platform-automation.status-pending", pending_count}
end
return {"space-platform-automation.status-ready"}
end
--- Create toggle button in top bar
--- @param player LuaPlayer The player
function gui.create_toggle_button(player)
if player.gui.top[TOGGLE_BUTTON_NAME] then
return
end
player.gui.top.add{
type = "sprite-button",
name = TOGGLE_BUTTON_NAME,
sprite = "item/space-platform-starter-pack",
style = "slot_button",
tooltip = {"space-platform-automation.toggle-tooltip"}
}
end
--- Create the main screen panel GUI (floating, shown in Remote View)
--- @param player LuaPlayer The player
function gui.create_main_panel(player)
-- Clean up existing
if player.gui.screen[PANEL_NAME] then
player.gui.screen[PANEL_NAME].destroy()
end
local player_data = storage.player_data[player.index]
if player_data.debug_logging then
player.print("[∞ SPA DEBUG] Creating main panel...")
end
-- Create main frame as floating screen GUI
local frame = player.gui.screen.add{
type = "frame",
name = PANEL_NAME,
direction = "vertical",
caption = {"space-platform-automation.panel-title"}
}
-- Position to right side of screen
frame.location = {x = 370, y = 100}
-- Start closed (respects panel_manually_closed preference)
frame.visible = false
-- Enable Auto-Creation section (moved to top, bold)
local enable_flow = frame.add{
type = "flow",
direction = "horizontal"
}
enable_flow.style.vertical_align = "center"
local enable_label = enable_flow.add{
type = "label",
caption = {"space-platform-automation.panel-enabled"},
style = "bold_label"
}
-- Colored indicator sprite-button
local indicator_sprite = player_data.enabled and "utility/check_mark_green" or "utility/not_available"
enable_flow.add{
type = "sprite-button",
name = "spa_enable_indicator",
sprite = indicator_sprite,
style = "slot_button"
}
-- Debug logging toggle (smaller, less prominent)
frame.add{
type = "checkbox",
name = "spa_debug_checkbox",
caption = {"space-platform-automation.panel-debug"},
state = player_data.debug_logging or false
}
frame.add{type = "line"}
-- Platform copy dropdown
local copy_flow = frame.add{
type = "flow",
direction = "horizontal"
}
copy_flow.add{
type = "label",
caption = {"space-platform-automation.panel-copy"}
}
local platform_items, platform_mapping = get_platform_dropdown_items(player.force, player)
storage.player_data[player.index].platform_mapping = platform_mapping
copy_flow.add{
type = "drop-down",
name = "spa_copy_platform_dropdown",
items = platform_items,
selected_index = 1
}
if player_data.debug_logging then
player.print("[∞ SPA DEBUG] Dropdown created with " .. #platform_items .. " items")
end
-- Planet dropdown (shortened)
local planet_flow = frame.add{
type = "flow",
direction = "horizontal"
}
planet_flow.add{
type = "label",
caption = {"space-platform-automation.panel-planet"}
}
local planet_items, planet_mapping = get_space_location_dropdown_items(player)
storage.player_data[player.index].planet_mapping = planet_mapping
local planet_dropdown = planet_flow.add{
type = "drop-down",
name = "spa_planet_dropdown",
items = planet_items,
selected_index = 1
}
planet_dropdown.style.width = 150
frame.add{type = "line"}
-- Naming section
frame.add{
type = "label",
caption = {"space-platform-automation.panel-naming-section"},
style = "caption_label"
}
local naming_flow = frame.add{
type = "flow",
direction = "vertical"
}
naming_flow.add{
type = "label",
caption = {"space-platform-automation.panel-naming-pattern"}
}
local pattern_field = naming_flow.add{
type = "textfield",
name = "spa_naming_pattern",
text = player_data.custom_name_pattern or "Platform-{counter}"
}
pattern_field.style.width = 250
frame.add{type = "line"}
-- Status
frame.add{
type = "label",
name = "spa_status_label",
caption = gui.get_status_text(player)
}
-- Description at bottom
local description = frame.add{
type = "label",
caption = {"space-platform-automation.panel-description"}
}
description.style.font_color = {r = 0.7, g = 0.7, b = 0.7}
end
--- Toggle panel visibility
--- @param player LuaPlayer The player
function gui.toggle_panel(player)
local panel = player.gui.screen[PANEL_NAME]
local player_data = storage.player_data[player.index]
if panel then
if panel.visible then
-- Close the panel
panel.visible = false
player_data.panel_manually_closed = true
else
-- Recreate the panel to refresh dropdown with new platforms
gui.create_main_panel(player)
player_data.panel_manually_closed = false
end
else
gui.create_main_panel(player)
player_data.panel_manually_closed = false
end
end
--- Refresh panel
--- @param player LuaPlayer The player
function gui.refresh_panel(player)
if player.gui.screen[PANEL_NAME] then
gui.create_main_panel(player)
end
end
--- Update panel visibility based on controller type
--- @param player LuaPlayer The player
function gui.update_panel_visibility(player)
local panel = player.gui.screen[PANEL_NAME]
local button = player.gui.top[TOGGLE_BUTTON_NAME]
local player_data = storage.player_data[player.index]
local is_remote = (player.controller_type == defines.controllers.remote)
-- Update toggle button visibility (only show in remote view)
if button then
button.visible = is_remote
end
-- Update panel visibility
if panel then
if is_remote then
-- In remote view: respect user preference
panel.visible = not player_data.panel_manually_closed
else
-- Not in remote view: always force closed
panel.visible = false
end
end
end
--- Update status label only
--- @param player LuaPlayer The player
function gui.update_status(player)
local panel = player.gui.screen[PANEL_NAME]
if panel then
local status_label = panel["spa_status_label"]
if status_label then
status_label.caption = gui.get_status_text(player)
end
end
end
--- Update enable indicator sprite
--- @param player LuaPlayer The player
function gui.update_enable_indicator(player)
local panel = player.gui.screen[PANEL_NAME]
local player_data = storage.player_data[player.index]
if panel then
-- Navigate through the flow to find the sprite-button
local enable_flow = panel.children[1] -- First element is the enable flow
if enable_flow and enable_flow.valid and enable_flow.children[2] then
local indicator = enable_flow.children[2] -- Second element is the sprite-button
if indicator and indicator.name == "spa_enable_indicator" then
indicator.sprite = player_data.enabled and "utility/check_mark_green" or "utility/not_available"
end
end
end
end
--- Initialize GUI for player
--- @param player LuaPlayer The player
function gui.initialize_player(player)
gui.create_toggle_button(player)
gui.create_main_panel(player)
end
--- Handle GUI click events
--- @param event EventData The event
function gui.on_gui_click(event)
local player = game.players[event.player_index]
if not player or not player.valid then return end
local element = event.element
if not element or not element.valid then return end
if element.name == TOGGLE_BUTTON_NAME then
gui.toggle_panel(player)
elseif element.name == "spa_enable_indicator" then
-- Toggle enabled state
local player_data = storage.player_data[player.index]
player_data.enabled = not player_data.enabled
-- Update indicator sprite and status
gui.update_enable_indicator(player)
gui.update_status(player)
elseif element.name == "spa_debug_checkbox" then
-- Toggle debug logging
storage.player_data[player.index].debug_logging = element.state
end
end
--- Handle dropdown selection changes
--- @param event EventData The event
function gui.on_gui_selection_state_changed(event)
local player = game.players[event.player_index]
if not player or not player.valid then return end
local element = event.element
if not element or not element.valid then return end
local player_data = storage.player_data[player.index]
if element.name == "spa_planet_dropdown" then
-- Regenerate mapping on-demand for robustness
local _, mapping = get_space_location_dropdown_items(player)
if mapping and mapping[element.selected_index] then
player_data.target_planet = mapping[element.selected_index]
end
elseif element.name == "spa_copy_platform_dropdown" then
-- Regenerate mapping on-demand for robustness
local _, mapping = get_platform_dropdown_items(player.force, player)
if mapping and mapping[element.selected_index] then
player_data.copy_platform_index = mapping[element.selected_index]
-- Blueprint the source platform immediately
cache_platform_blueprint(player, player_data.copy_platform_index)
else
player_data.copy_platform_index = nil
player_data.cached_blueprint_string = nil -- Clear cached blueprint
end
-- Just update status, don't rebuild entire panel
gui.update_status(player)
end
end
--- Handle text changed events
--- @param event EventData The event
function gui.on_gui_text_changed(event)
local player = game.players[event.player_index]
if not player or not player.valid then return end
local element = event.element
if not element or not element.valid then return end
if element.name == "spa_naming_pattern" then
local pattern = element.text
if pattern == "" then
pattern = nil
end
storage.player_data[player.index].custom_name_pattern = pattern
end
end
return gui