From b80e85eb41b98c602e08f110f750ed2596151825 Mon Sep 17 00:00:00 2001 From: Monica Moniot Date: Wed, 26 Oct 2022 20:51:23 -0400 Subject: [PATCH] got gui to work --- cybersyn/control.lua | 1 + cybersyn/locale/en/base.cfg | 8 +++ cybersyn/scripts/gui.lua | 98 +++++++++++++++++++++++++++++++++++++ cybersyn/scripts/layout.lua | 25 +++++----- cybersyn/scripts/main.lua | 10 ++-- 5 files changed, 125 insertions(+), 17 deletions(-) create mode 100644 cybersyn/scripts/gui.lua diff --git a/cybersyn/control.lua b/cybersyn/control.lua index 6b7facc..ed1cfee 100644 --- a/cybersyn/control.lua +++ b/cybersyn/control.lua @@ -4,4 +4,5 @@ require("scripts.constants") require("scripts.global") require("scripts.controller") require("scripts.layout") +require("scripts.gui") require("scripts.main") diff --git a/cybersyn/locale/en/base.cfg b/cybersyn/locale/en/base.cfg index 503d985..e432a4d 100644 --- a/cybersyn/locale/en/base.cfg +++ b/cybersyn/locale/en/base.cfg @@ -38,3 +38,11 @@ cybersyn-locked-slots=Locked slots per cargo wagon missing-trains=No trains available to make a delivery from station __2__ to station __1__ lost-train=A train has become lost nonempty-train=A train has parked in a depot while still containing items; it cannot be dispatched until it is empty + +[cybersyn-gui] +operation=Choose combinator type +comb1=Primary controller +comb2=Secondary station control +depot=Depot +wagon-manifest=Wagon +combinator-title=Cybernetic combinator diff --git a/cybersyn/scripts/gui.lua b/cybersyn/scripts/gui.lua new file mode 100644 index 0000000..9c3c493 --- /dev/null +++ b/cybersyn/scripts/gui.lua @@ -0,0 +1,98 @@ +--By Mami +local gui = require("__flib__.gui") + +local RED = "utility/status_not_working" +local GREEN = "utility/status_working" +local YELLOW = "utility/status_yellow" + +local STATUS_SPRITES = {} +STATUS_SPRITES[defines.entity_status.working] = GREEN +STATUS_SPRITES[defines.entity_status.normal] = GREEN +STATUS_SPRITES[defines.entity_status.no_power] = RED +STATUS_SPRITES[defines.entity_status.low_power] = YELLOW +STATUS_SPRITES[defines.entity_status.disabled_by_control_behavior] = RED +STATUS_SPRITES[defines.entity_status.disabled_by_script] = RED +STATUS_SPRITES[defines.entity_status.marked_for_deconstruction] = RED +local STATUS_SPRITES_DEFAULT = RED + +local STATUS_NAMES = {} +STATUS_NAMES[defines.entity_status.working] = "entity-status.working" +STATUS_NAMES[defines.entity_status.normal] = "entity-status.normal" +STATUS_NAMES[defines.entity_status.no_power] = "entity-status.no-power" +STATUS_NAMES[defines.entity_status.low_power] = "entity-status.low-power" +STATUS_NAMES[defines.entity_status.disabled_by_control_behavior] = "entity-status.disabled" +STATUS_NAMES[defines.entity_status.disabled_by_script] = "entity-status.disabled-by-script" +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) + 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={ + on_close = {"test"} + }, children={ + --title bar + {type="flow", ref={"titlebar"}, children={ + {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"} + }} + }}, + {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"}} + }}, + --preview + {type="frame", style="deep_frame_in_shallow_frame", style_mods={minimal_width=0, horizontally_stretchable=true, padding=0}, children={ + {type="entity-preview", style="wide_entity_button", ref={"preview"}}, + }}, + {type="label", caption={"cybersyn-gui.operation"}, style_mods={top_padding=8}}, + {type="drop-down", ref={"operation"}, actions={ + on_selection_state_changed = {"test"} + }, items={ + {"cybersyn-gui.comb1"}, + {"cybersyn-gui.comb2"}, + {"cybersyn-gui.depot"}, + {"cybersyn-gui.wagon-manifest"}, + }}, + }} + }} + }} + }) + + window.preview.entity = entity + window.titlebar.drag_target = window.main_window + window.main_window.force_auto_center() + + player.opened = window.main_window +end + +local function o(event) + local entity = event.entity + if not entity or not entity.valid 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) + end +end + +function register_gui_actions() + gui.hook_events(function(event) + local msg = gui.read_action(event) + if msg then + -- read the action to determine what to do + local hi = 2 + end + end) + script.on_event(defines.events.on_gui_opened, o) +end diff --git a/cybersyn/scripts/layout.lua b/cybersyn/scripts/layout.lua index ecc5cc5..5dafdfa 100644 --- a/cybersyn/scripts/layout.lua +++ b/cybersyn/scripts/layout.lua @@ -127,9 +127,9 @@ local function reset_station_layout(map_data, station, forbidden_entity) local length = 2 local pre_rail = station_rail local layout_pattern = "^" - local layout_min_size = 10000 local type_filter = {"inserter", "pump", "arithmetic-combinator"} local wagon_number = 0 + local pattern_length = 1 for i = 1, 100 do local rail, rail_direction, rail_connection_direction = pre_rail.get_connected_rail({rail_direction = rail_direction_from_station, rail_connection_direction = defines.rail_connection_direction.straight}) if not rail or rail_connection_direction ~= defines.rail_connection_direction.straight or not rail.valid then @@ -203,20 +203,17 @@ local function reset_station_layout(map_data, station, forbidden_entity) --TODO: needs to allow misc wagons as well layout_pattern = layout_pattern..STATION_LAYOUT_NOT_FLUID end + pattern_length = #layout_pattern elseif supports_fluid then layout_pattern = layout_pattern..STATION_LAYOUT_NOT_CARGO + pattern_length = #layout_pattern else layout_pattern = layout_pattern..STATION_LAYOUT_NA end - if layout_min_size <= 0 then - layout_pattern = layout_pattern.."?" - else - layout_min_size = layout_min_size - 1 - end search_area = area.move(search_area, area_delta) end end - layout_pattern = layout_pattern..STATION_LAYOUT_NA.."*$" + layout_pattern = string.sub(layout_pattern, 1, pattern_length)..STATION_LAYOUT_NA.."*$" station.layout_pattern = layout_pattern local accepted_layouts = station.accepted_layouts for id, layout in pairs(map_data.layouts) do @@ -257,7 +254,7 @@ end ---@param map_data MapData ---@param rail LuaEntity ----@param forbidden_entity LuaEntity +---@param forbidden_entity LuaEntity? function update_station_from_rail(map_data, rail, forbidden_entity) --TODO: search further or better? local entity = rail.get_rail_segment_entity(defines.rail_direction.back, false) @@ -278,23 +275,25 @@ function update_station_from_rail(map_data, rail, forbidden_entity) end ---@param map_data MapData ---@param pump LuaEntity -function update_station_from_pump(map_data, pump) +---@param forbidden_entity LuaEntity? +function update_station_from_pump(map_data, pump, forbidden_entity) if pump.pump_rail_target then - update_station_from_rail(map_data, pump.pump_rail_target, pump) + update_station_from_rail(map_data, pump.pump_rail_target, forbidden_entity) end end ---@param map_data MapData ---@param inserter LuaEntity -function update_station_from_inserter(map_data, inserter) +---@param forbidden_entity LuaEntity? +function update_station_from_inserter(map_data, inserter, forbidden_entity) --TODO: check if correct local surface = inserter.surface local rail = surface.find_entity("straight-rail", inserter.pickup_position) if rail then - update_station_from_rail(map_data, rail, inserter) + update_station_from_rail(map_data, rail, forbidden_entity) end rail = surface.find_entity("straight-rail", inserter.drop_position) if rail then - update_station_from_rail(map_data, rail, inserter) + update_station_from_rail(map_data, rail, forbidden_entity) end end diff --git a/cybersyn/scripts/main.lua b/cybersyn/scripts/main.lua index e311246..2d69b9b 100644 --- a/cybersyn/scripts/main.lua +++ b/cybersyn/scripts/main.lua @@ -128,7 +128,7 @@ local function on_combinator_built(map_data, comb) if cur_entity.name == "train-stop" then --NOTE: if there are multiple stops we take the later one stop = cur_entity - elseif cur_entity.name == "rail-straight" then + elseif cur_entity.name == "straight-rail" then rail = cur_entity end end @@ -500,7 +500,7 @@ local function on_built(event) elseif entity.type == "pump" then update_station_from_pump(global, entity) elseif entity.type == "straight-rail" then - update_station_from_rail(global, entity, nil) + update_station_from_rail(global, entity) end end local function on_broken(event) @@ -517,9 +517,9 @@ local function on_broken(event) elseif entity.name == COMBINATOR_NAME then on_combinator_broken(global, entity) elseif entity.type == "inserter" then - update_station_from_inserter(global, entity) + update_station_from_inserter(global, entity, entity) elseif entity.type == "pump" then - update_station_from_pump(global, entity) + update_station_from_pump(global, entity, entity) elseif entity.type == "straight-rail" then update_station_from_rail(global, entity, nil) end @@ -607,6 +607,8 @@ local function register_events() script.on_event(defines.events.on_train_changed_state, on_train_changed) script.on_event(defines.events.on_entity_renamed, on_rename) + + register_gui_actions() end script.on_load(function()