finished gui

This commit is contained in:
Monica Moniot
2022-10-27 11:24:05 -04:00
parent b80e85eb41
commit 85efc33183
4 changed files with 94 additions and 39 deletions

View File

@@ -2,6 +2,7 @@
---@class MapData
---@field public total_ticks uint
---@field public layout_top_id uint
---@field public to_comb {[uint]: LuaEntity}
---@field public to_output {[uint]: LuaEntity}
---@field public to_stop {[uint]: LuaEntity}
---@field public stations {[uint]: Station}
@@ -56,6 +57,7 @@ mod_settings.r_threshold = settings.global["cybersyn-request-threshold"].value
mod_settings.p_threshold = settings.global["cybersyn-provide-threshold"].value
global.total_ticks = 0
global.to_comb = {}
global.to_output = {}
global.to_stop = {}
global.stations = {}

View File

@@ -1,5 +1,6 @@
--By Mami
local gui = require("__flib__.gui")
local flib_gui = require("__flib__.gui")
local flib_event = require("__flib__.event")
local RED = "utility/status_not_working"
local GREEN = "utility/status_working"
@@ -25,11 +26,24 @@ STATUS_NAMES[defines.entity_status.disabled_by_script] = "entity-status.disabled
STATUS_NAMES[defines.entity_status.marked_for_deconstruction] = "entity-status.marked-for-deconstruction"
STATUS_NAMES_DEFAULT = "entity-status.disabled"
---@param entity LuaEntity
function gui_opened(entity, player)
---@param comb LuaEntity
---@param player LuaPlayer
function gui_opened(comb, player)
local rootgui = player.gui.screen
local window = gui.build(rootgui, {
{type="frame", direction="vertical", ref={"main_window"}, name=COMBINATOR_NAME, tags={unit_number=entity.unit_number}, actions={
local selected_index = 0
local control = comb.get_or_create_control_behavior().parameters
if control.operation == OPERATION_PRIMARY_IO then
selected_index = 1
elseif control.operation == OPERATION_SECONDARY_IO then
selected_index = 2
elseif control.operation == OPERATION_DEPOT then
selected_index = 3
elseif control.operation == OPERATION_WAGON_MANIFEST then
selected_index = 4
end
local window = flib_gui.build(rootgui, {
{type="frame", direction="vertical", ref={"main_window"}, name=COMBINATOR_NAME, actions={
on_close = {"test"}
}, children={
--title bar
@@ -37,15 +51,15 @@ function gui_opened(entity, player)
{type="label", style="frame_title", caption={"cybersyn-gui.combinator-title"}, elem_mods={ignored_by_interaction=true}},
{type="empty-widget", style="flib_titlebar_drag_handle", elem_mods={ignored_by_interaction=true}},
{type="sprite-button", style="frame_action_button", mouse_button_filter={"left"}, sprite="utility/close_white", hovered_sprite="utility/close_black", name=COMBINATOR_NAME, actions={
on_click = {"test"}
on_click = {"close", comb.unit_number}
}}
}},
{type="frame", style="inside_shallow_frame_with_padding", style_mods={padding=8}, children={
{type="flow", direction="vertical", style_mods={horizontal_align="left"}, children={
--status
{type="flow", style = "status_flow", direction = "horizontal", style_mods={vertical_align="center", horizontally_stretchable=true}, children={
{type="sprite", sprite=STATUS_SPRITES[entity.status] or STATUS_SPRITES_DEFAULT, style="status_image", ref={"status_icon"}, style_mods={stretch_image_to_widget_size=true}},
{type="label", caption={STATUS_NAMES[entity.status] or STATUS_NAMES_DEFAULT}, ref={"status_label"}}
{type="sprite", sprite=STATUS_SPRITES[comb.status] or STATUS_SPRITES_DEFAULT, style="status_image", ref={"status_icon"}, style_mods={stretch_image_to_widget_size=true}},
{type="label", caption={STATUS_NAMES[comb.status] or STATUS_NAMES_DEFAULT}, ref={"status_label"}}
}},
--preview
{type="frame", style="deep_frame_in_shallow_frame", style_mods={minimal_width=0, horizontally_stretchable=true, padding=0}, children={
@@ -53,8 +67,8 @@ function gui_opened(entity, player)
}},
{type="label", caption={"cybersyn-gui.operation"}, style_mods={top_padding=8}},
{type="drop-down", ref={"operation"}, actions={
on_selection_state_changed = {"test"}
}, items={
on_selection_state_changed = {"drop-down", comb.unit_number}
}, selected_index=selected_index, items={
{"cybersyn-gui.comb1"},
{"cybersyn-gui.comb2"},
{"cybersyn-gui.depot"},
@@ -65,34 +79,71 @@ function gui_opened(entity, player)
}}
})
window.preview.entity = entity
window.preview.entity = comb
window.titlebar.drag_target = window.main_window
window.main_window.force_auto_center()
player.opened = window.main_window
end
local function o(event)
local function on_gui_opened(event)
local entity = event.entity
if not entity or not entity.valid then return end
if not entity or not entity.valid or entity.name ~= COMBINATOR_NAME then return end
local player = game.get_player(event.player_index)
if not player then return end
gui_opened(entity, player)
end
local function on_gui_closed(event)
if not event.element or event.element.name ~= COMBINATOR_NAME then return end
local player = game.get_player(event.player_index)
if not player then return end
local rootgui = player.gui.screen
if rootgui[COMBINATOR_NAME] then
--rootgui[COMBINATOR_NAME].destroy()
else
gui_opened(entity, player)
rootgui[COMBINATOR_NAME].destroy()
--TODO: play close sound to player
end
end
function register_gui_actions()
gui.hook_events(function(event)
local msg = gui.read_action(event)
flib_gui.hook_events(function(event)
local msg = flib_gui.read_action(event)
if msg then
local player = game.get_player(event.player_index)
if not player then return end
local rootgui = player.gui.screen
-- read the action to determine what to do
local hi = 2
if msg[1] == "close" then
if rootgui[COMBINATOR_NAME] then
rootgui[COMBINATOR_NAME].destroy()
--TODO: play close sound to player
end
elseif msg[1] == "drop-down" then
local element = event.element
if not element then return end
local comb = global.to_comb[msg[2]]
if not comb or not comb.valid then return end
local a = comb.get_or_create_control_behavior()
local control = a.parameters
if element.selected_index == 1 then
control.operation = OPERATION_PRIMARY_IO
elseif element.selected_index == 2 then
control.operation = OPERATION_SECONDARY_IO
elseif element.selected_index == 3 then
control.operation = OPERATION_DEPOT
elseif element.selected_index == 4 then
control.operation = OPERATION_WAGON_MANIFEST
else
return
end
a.parameters = control
on_combinator_updated(global, comb)
end
end
end)
script.on_event(defines.events.on_gui_opened, o)
flib_event.register(defines.events.on_gui_opened, on_gui_opened)
flib_event.register(defines.events.on_gui_closed, on_gui_closed)
end

View File

@@ -107,7 +107,7 @@ local function reset_station_layout(map_data, station, forbidden_entity)
direction_filter = {defines.direction.east, defines.direction.west}
is_ver = true
elseif station_direction == defines.direction.east then
search_area = {left_top = {y = middle_y - reach, x = middle_x}, right_bottom = {y = middle_y + reach, x = middle_x - 6}}
search_area = {left_top = {y = middle_y - reach, x = middle_x - 6}, right_bottom = {y = middle_y + reach, x = middle_x}}
area_delta = {x = -7, y = 0}
direction_filter = {defines.direction.north, defines.direction.south}
is_ver = false
@@ -117,7 +117,7 @@ local function reset_station_layout(map_data, station, forbidden_entity)
direction_filter = {defines.direction.east, defines.direction.west}
is_ver = true
elseif station_direction == defines.direction.west then
search_area = {left_top = {y = middle_y - reach, x = middle_x + 6}, right_bottom = {y = middle_y + reach, x = middle_x}}
search_area = {left_top = {y = middle_y - reach, x = middle_x}, right_bottom = {y = middle_y + reach, x = middle_x + 6}}
area_delta = {x = 7, y = 0}
direction_filter = {defines.direction.north, defines.direction.south}
is_ver = false

View File

@@ -1,4 +1,5 @@
--By Mami
local flib_event = require("__flib__.event")
---@param map_data MapData
---@param train Train
@@ -151,6 +152,7 @@ local function on_combinator_built(map_data, comb)
wire = defines.wire_type.red,
})
map_data.to_comb[comb.unit_number] = comb
map_data.to_output[comb.unit_number] = out
map_data.to_stop[comb.unit_number] = stop
@@ -229,12 +231,13 @@ local function on_combinator_broken(map_data, comb)
if out and out.valid then
out.destroy()
end
map_data.to_comb[comb.unit_number] = nil
map_data.to_output[comb.unit_number] = nil
map_data.to_stop[comb.unit_number] = nil
end
---@param map_data MapData
---@param comb LuaEntity
local function on_combinator_updated(map_data, comb)
function on_combinator_updated(map_data, comb)
--NOTE: this is the lazy way to implement updates and puts strong restrictions on data validity on on_combinator_broken
on_combinator_broken(map_data, comb)
on_combinator_built(map_data, comb)
@@ -588,40 +591,39 @@ local filter_broken = {
}
local function register_events()
--NOTE: I have no idea if this correctly registers all events once in all situations
script.on_event(defines.events.on_built_entity, on_built, filter_built)
script.on_event(defines.events.on_robot_built_entity, on_built, filter_built)
script.on_event({defines.events.script_raised_built, defines.events.script_raised_revive, defines.events.on_entity_cloned}, on_built)
flib_event.register(defines.events.on_built_entity, on_built, filter_built)
flib_event.register(defines.events.on_robot_built_entity, on_built, filter_built)
flib_event.register({defines.events.script_raised_built, defines.events.script_raised_revive, defines.events.on_entity_cloned}, on_built)
script.on_event(defines.events.on_pre_player_mined_item, on_broken, filter_broken)
script.on_event(defines.events.on_robot_pre_mined, on_broken, filter_broken)
script.on_event(defines.events.on_entity_died, on_broken, filter_broken)
script.on_event(defines.events.script_raised_destroy, on_broken)
flib_event.register(defines.events.on_pre_player_mined_item, on_broken, filter_broken)
flib_event.register(defines.events.on_robot_pre_mined, on_broken, filter_broken)
flib_event.register(defines.events.on_entity_died, on_broken, filter_broken)
flib_event.register(defines.events.script_raised_destroy, on_broken)
script.on_event({defines.events.on_pre_surface_deleted, defines.events.on_pre_surface_cleared}, on_surface_removed)
flib_event.register({defines.events.on_pre_surface_deleted, defines.events.on_pre_surface_cleared}, on_surface_removed)
local nth_tick = math.ceil(60/mod_settings.tps);
script.on_nth_tick(nil)
script.on_nth_tick(nth_tick, on_tick)
flib_event.on_nth_tick(nth_tick, on_tick)
script.on_event(defines.events.on_train_created, on_train_built)
script.on_event(defines.events.on_train_changed_state, on_train_changed)
flib_event.register(defines.events.on_train_created, on_train_built)
flib_event.register(defines.events.on_train_changed_state, on_train_changed)
script.on_event(defines.events.on_entity_renamed, on_rename)
flib_event.register(defines.events.on_entity_renamed, on_rename)
register_gui_actions()
end
script.on_load(function()
flib_event.on_load(function()
register_events()
end)
script.on_init(function()
flib_event.on_init(function()
--TODO: we are not checking changed cargo capacities
--find_and_add_all_stations(global)
register_events()
end)
script.on_configuration_changed(function(data)
flib_event.on_configuration_changed(function(data)
--TODO: we are not checking changed cargo capacities
--find_and_add_all_stations(global)
register_events()