From f5fd927dbfd4f9de494692b7e716e7661efac5f7 Mon Sep 17 00:00:00 2001 From: Helfima Date: Fri, 7 Jul 2023 18:27:33 +0200 Subject: [PATCH] add beacons menu --- .gitignore | 44 +++++++++++++++++++++++ .vscode/launch.json | 5 +-- edition/RecipeEdition.lua | 76 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..639484f7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,44 @@ +#vscode +.vscode/* + +# Compiled Lua sources +luac.out + +# luarocks build files +*.src.rock +*.zip +*.tar.gz + +# Object files +*.o +*.os +*.ko +*.obj +*.elf + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo +*.def +*.exp + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + diff --git a/.vscode/launch.json b/.vscode/launch.json index 6a7d67f0..7784ee3a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,14 +7,12 @@ { "type": "factoriomod", "request": "launch", - "name": "Factorio Mod Debug", - "factorioPath": "E:/Perso/Factorio_Test/Factorio_1.1.x/bin/x64/factorio.exe" + "name": "Factorio Mod Debug" }, { "type": "factoriomod", "request": "launch", "name": "Factorio Mod Debug (Settings & Data)", - "factorioPath": "", "hookSettings": true, "hookData": true }, @@ -22,7 +20,6 @@ "type": "factoriomod", "request": "launch", "name": "Factorio Mod Debug (Profile)", - "factorioPath": "", "hookMode": "profile" } ] diff --git a/edition/RecipeEdition.lua b/edition/RecipeEdition.lua index 9b95266e..f7d633c4 100644 --- a/edition/RecipeEdition.lua +++ b/edition/RecipeEdition.lua @@ -165,7 +165,6 @@ function RecipeEdition:getBeaconTablePanel() table_panel.vertical_centering = false local info_panel = GuiElement.add(table_panel, GuiFlowV(info_name)) info_panel.style.minimal_width = 250 - GuiElement.add(info_panel, GuiLabel("beacon_label"):caption({"helmod_common.beacon"}):style("helmod_label_title_frame")) local module_panel = GuiElement.add(table_panel, GuiFlowV(module_name)) @@ -173,6 +172,19 @@ function RecipeEdition:getBeaconTablePanel() return info_panel, module_panel end +------------------------------------------------------------------------------- +---Get or create beacon selection panel +---@return LuaGuiElement +function RecipeEdition:getBeaconSelectionPanel() + local info_panel, module_panel = self:getBeaconTablePanel() + local selection_name = "beacon_selection" + if info_panel[selection_name] ~= nil and info_panel[selection_name].valid then + return info_panel[selection_name] + end + local selection_panel = GuiElement.add(info_panel, GuiFlowV(selection_name)) + return selection_panel +end + ------------------------------------------------------------------------------- ---Get or create beacon info panel ---@return LuaGuiElement, LuaGuiElement @@ -374,6 +386,28 @@ function RecipeEdition:onEvent(event) Controller:send("on_gui_refresh", event) end + if event.action == "beacon-select" then + User.setParameter("current_beacon_selection", tonumber(event.item4)) + self:update(event) + end + + if event.action == "beacon-add" then + if recipe.beacons == nil then recipe.beacons = {} end + local new_beacon = Model.newBeacon() + table.insert(recipe.beacons, new_beacon) + User.setParameter("current_beacon_selection", #recipe.beacons) + self:update(event) + end + + if event.action == "beacon-remove" then + local current_key = User.getParameter("current_beacon_selection") or 1 + if #recipe.beacons > 1 then + table.remove(recipe.beacons, current_key) + end + User.setParameter("current_beacon_selection", #recipe.beacons) + self:update(event) + end + if event.action == "beacon-tool" then if event.item4 == "default" then User.setDefaultBeacon(recipe) @@ -518,6 +552,7 @@ function RecipeEdition:onUpdate(event) self:updateFactoryInfo(event) self:updateFactoryModulesActive(event) self:updateFactoryModules(event) + self:updateBeaconSelection(event) self:updateBeaconInfoTool(event) self:updateBeaconInfo(event) self:updateBeaconModulesActive(event) @@ -978,6 +1013,37 @@ function RecipeEdition:updateBeaconInfo(event) end end +---Update beacon tool +---@param event LuaEvent +function RecipeEdition:updateBeaconSelection(event) + local selection_panel = self:getBeaconSelectionPanel() + local model, block, recipe = self:getParameterObjects() + + local tool_panel = GuiElement.add(selection_panel, GuiFlowH("tool1")) + tool_panel.style.horizontal_spacing = 5 + + GuiElement.add(tool_panel, GuiLabel("beacon_label"):caption({"helmod_common.beacon"}):style("helmod_label_title_frame")) + if recipe ~= nil then + local selection_panel = GuiElement.add(tool_panel, GuiFlowH("selections")) + selection_panel.style.horizontal_spacing = 2 + selection_panel.style.horizontally_stretchable = true + selection_panel.style.horizontal_align = "right" + selection_panel.style.right_margin = 10 + + local beacons = recipe.beacons or {recipe.beacon} + local current_beacon_selection = User.getParameter("current_beacon_selection") or 1 + for key, beacon in pairs(beacons) do + local style = "helmod_button_menu_sm_bold" + if current_beacon_selection == key then + style = "helmod_button_menu_sm_bold_selected" + end + GuiElement.add(selection_panel, GuiButton(self.classname, "beacon-select", model.id, block.id, recipe.id, key):caption(key):style(style)) + end + + GuiElement.add(selection_panel, GuiButton(self.classname, "beacon-add", model.id, block.id, recipe.id):sprite("menu", defines.sprites.add.black, defines.sprites.add.black):style("helmod_button_menu_sm")) + GuiElement.add(selection_panel, GuiButton(self.classname, "beacon-remove", model.id, block.id, recipe.id):sprite("menu", defines.sprites.remove.black, defines.sprites.remove.black):style("helmod_button_menu_sm")) + end +end ------------------------------------------------------------------------------- ---Update beacon tool ---@param event LuaEvent @@ -985,7 +1051,9 @@ function RecipeEdition:updateBeaconInfoTool(event) local tool_panel, detail_panel = self:getBeaconInfoPanel() local model, block, recipe = self:getParameterObjects() if recipe ~= nil then - local beacon = recipe.beacon + local beacons = recipe.beacons or {recipe.beacon} + local current_beacon_selection = User.getParameter("current_beacon_selection") or 1 + local beacon = beacons[current_beacon_selection] tool_panel.clear() @@ -1032,7 +1100,9 @@ function RecipeEdition:updateBeaconModulesActive(event) local tool_panel, module_panel = self:getBeaconModulePanel() local model, block, recipe = self:getParameterObjects() if recipe ~= nil then - local beacon = recipe.beacon + local beacons = recipe.beacons or {recipe.beacon} + local current_beacon_selection = User.getParameter("current_beacon_selection") or 1 + local beacon = beacons[current_beacon_selection] tool_panel.clear() GuiElement.add(tool_panel, GuiLabel("module_label"):caption({"helmod_recipe-edition-panel.current-modules"}):style("helmod_label_title_frame"))