Skip to content

Commit

Permalink
Redo the blueprint menu using a generalizable framework
Browse files Browse the repository at this point in the history
See ui/menus/blueprint-menu.lua for an easy way to make menus.

Squashed commit of the following:

commit 26ee4b8
Author: Austin Hicks <[email protected]>
Date:   Mon Feb 17 18:59:40 2025 -0800

    Bunch of small fixes

commit ee79129
Author: Austin Hicks <[email protected]>
Date:   Sun Feb 9 19:36:33 2025 -0800

    WIP

commit 8008364
Author: Austin Hicks <[email protected]>
Date:   Sun Feb 9 17:30:48 2025 -0800

    WIP

commit bf5454d
Author: Austin Hicks <[email protected]>
Date:   Fri Feb 7 20:25:32 2025 -0800

    WIP

commit 566fdeb
Author: Austin Hicks <[email protected]>
Date:   Fri Feb 7 20:25:04 2025 -0800

    WIP
  • Loading branch information
ahicks92 committed Feb 24, 2025
1 parent 5f0287b commit 0417077
Show file tree
Hide file tree
Showing 11 changed files with 778 additions and 23 deletions.
16 changes: 12 additions & 4 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ local fa_circuits = require("scripts.circuit-networks")
local fa_kk = require("scripts.kruise-kontrol-wrapper")
local fa_quickbar = require("scripts.quickbar")
local BeltAnalyzer = require("scripts.ui.belt-analyzer")
local BlueprintsMenu = require("scripts.ui.menus.blueprints-menu")
local FaCommands = require("scripts.fa-commands")
local Filters = require("scripts.filters")
local Consts = require("scripts.consts")
Expand Down Expand Up @@ -1642,7 +1643,7 @@ function menu_cursor_up(pindex)
elseif players[pindex].menu == "roboport_menu" then
fa_bot_logistics.roboport_menu_up(pindex)
elseif players[pindex].menu == "blueprint_menu" then
fa_blueprints.blueprint_menu_up(pindex)
BlueprintsMenu.blueprint_menu_tabs:on_up(pindex)
elseif players[pindex].menu == "blueprint_book_menu" then
fa_blueprints.blueprint_book_menu_up(pindex)
elseif players[pindex].menu == "circuit_network_menu" then
Expand Down Expand Up @@ -1854,7 +1855,7 @@ function menu_cursor_down(pindex)
elseif players[pindex].menu == "roboport_menu" then
fa_bot_logistics.roboport_menu_down(pindex)
elseif players[pindex].menu == "blueprint_menu" then
fa_blueprints.blueprint_menu_down(pindex)
BlueprintsMenu.blueprint_menu_tabs:on_down(pindex)
elseif players[pindex].menu == "blueprint_book_menu" then
fa_blueprints.blueprint_book_menu_down(pindex)
elseif players[pindex].menu == "circuit_network_menu" then
Expand Down Expand Up @@ -3470,7 +3471,7 @@ function close_menu_resets(pindex)
elseif players[pindex].menu == "roboport_menu" then
fa_bot_logistics.roboport_menu_close(pindex)
elseif players[pindex].menu == "blueprint_menu" then
fa_blueprints.blueprint_menu_close(pindex)
BlueprintsMenu.blueprint_menu_tabs:close(pindex, false)
elseif players[pindex].menu == "blueprint_book_menu" then
fa_blueprints.blueprint_book_menu_close(pindex)
elseif players[pindex].menu == "circuit_network_menu" then
Expand Down Expand Up @@ -4658,7 +4659,7 @@ script.on_event("click-menu", function(event)
elseif players[pindex].menu == "roboport_menu" then
fa_bot_logistics.run_roboport_menu(players[pindex].roboport_menu.index, pindex, true)
elseif players[pindex].menu == "blueprint_menu" then
fa_blueprints.run_blueprint_menu(players[pindex].blueprint_menu.index, pindex, true)
BlueprintsMenu.blueprint_menu_tabs:on_click(pindex)
elseif players[pindex].menu == "blueprint_book_menu" then
local bpb_menu = players[pindex].blueprint_book_menu
fa_blueprints.run_blueprint_book_menu(pindex, bpb_menu.index, bpb_menu.list_mode, true, false)
Expand Down Expand Up @@ -4933,6 +4934,7 @@ script.on_event("click-hand-right", function(event)
--Laterdo here: build as ghost
elseif stack.is_blueprint then
fa_blueprints.blueprint_menu_open(pindex)
BlueprintsMenu.blueprint_menu_tabs:open(pindex, {})
elseif stack.is_blueprint_book then
fa_blueprints.blueprint_book_menu_open(pindex, false)
elseif stack.is_deconstruction_item then
Expand Down Expand Up @@ -6020,6 +6022,12 @@ script.on_event(defines.events.on_player_cursor_stack_changed, function(event)
players[pindex].blueprint_height_in_hand = height + 1
end
end

-- As a special case: blueprint menus will end up pointing at the wrong
-- blueprint if not closed here, since the only real unique identifier right
-- now is the player's hand.
if players[pindex].menu == "blueprint_menu" then BlueprintsMenu.blueprint_menu_tabs:close(pindex) end

if players[pindex].menu == "blueprint_menu" or players[pindex].menu == "blueprint_book_menu" then
close_menu_resets(pindex)
end
Expand Down
29 changes: 29 additions & 0 deletions locale/en/ui-blueprints.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[fa]
ui-blueprints-menu-title=Blueprint Configuration
ui-blueprints-menu-limited=Empty Blueprint with Limited Options. Import a blueprint string or select an area for more.
ui-blueprints-menu-basic=Blueprint __1__
ui-blueprints-menu-import=Import a blueprint string to this blueprint
ui-blueprints-menu-description=Description: __1__
ui-blueprints-menu-no-icons=No Icons
ui-blueprints-menu-icons=Icons:
ui-blueprints-menu-count-and-dims=__1__ by __2__ with __3__ entities
ui-blueprints-menu-no-components=No entities in this blueprint
ui-blueprints-menu-components-intro=Contains the following:
ui-blueprints-menu-rename=Rename this blueprint
ui-blueprints-menu-edit-desc=Edit description
ui-blueprints-menu-copy=Create a copy of this blueprint in your inventory
ui-blueprints-menu-delete=Delete this blueprint
ui-blueprints-menu-export=Export this blueprint as a string
ui-blueprints-menu-reselect=Reselect the area for this blueprint


ui-blueprints-import-txtbox=Paste a blueprint string in this textbox and press enter. Press escape to cancel.
ui-blueprints-rename-txtbox=Enter new name and press Enter. Escape to cancel.
ui-blueprints-description-txtbox=Enter description and press enter. Escape to cancel.
ui-blueprints-export-txtbox=Press control plus a control plus c to copy, then escape to close.

ui-blueprints-copy-success=Copied.
ui-blueprints-copy-failed=Unable to copy.
ui-blueprints-deleted=Deleted and menu closed.

ui-blueprints-select-first-point=Select the first point now
2 changes: 2 additions & 0 deletions locale/en/ui-menu.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[fa]
ui-menu-empty=Empty menu.
10 changes: 3 additions & 7 deletions scripts/blueprints.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ end
function mod.get_blueprint_label(stack)
local bp_data = mod.get_bp_data_for_edit(stack)
local label = bp_data.blueprint.label
if label == nil then label = "" end
if label == nil then label = "no name" end
return label
end

Expand Down Expand Up @@ -662,9 +662,8 @@ BLUEPRINT_MENU_LENGTH = 12

function mod.blueprint_menu_open(pindex)
if players[pindex].vanilla_mode then return end
--Set the player menu tracker to this menu
players[pindex].menu = "blueprint_menu"
players[pindex].in_menu = true
-- Opening the ui is one level up, to avoid circular imports.

players[pindex].move_queue = {}

--Set the menu line counter to 0
Expand All @@ -678,9 +677,6 @@ function mod.blueprint_menu_open(pindex)

--Play sound
game.get_player(pindex).play_sound({ path = "Open-Inventory-Sound" })

--Load menu
mod.run_blueprint_menu(players[pindex].blueprint_menu.index, pindex, false)
end

function mod.blueprint_menu_close(pindex, mute_in)
Expand Down
61 changes: 61 additions & 0 deletions scripts/fa-utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1089,4 +1089,65 @@ function mod.closest_point_in_box(point, box)
}
end

local ALL_PROTO_KINDS = {
"item",
"font",
"map_gen_preset",
"style",
"entity",
"fluid",
"tile",
"equipment",
"damage",
"virtual_signal",
"equipment_grid",
"recipe",
"technology",
"decorative",
"particle",
"autoplace_control",
"mod_setting",
"custom_input",
"ammo_category",
"named_noise_expression",
"named_noise_function",
"item_subgroup",
"item_group",
"fuel_category",
"resource_category",
"achievement",
"module_category",
"equipment_category",
"trivial_smoke",
"shortcut",
"recipe_category",
"quality",
"surface_property",
"space_location",
"space_connection",
"custom_event",
"active_trigger",
"asteroid_chunk",
"collision_layer",
"airborne_pollutant",
"burner_usage",
"surface",
"procession",
"procession_layer_inheritance_group",
}

-- In 2.0 all the prototypes got split up into separate attributes of
-- LuaPrototypes, but that's a userdata. This function checks them all for a
-- prototype, because in some cases we don't know what it actually is.
---@param name string
---@return LuaPrototypeBase?
function mod.find_prototype(name)
for _, f in pairs(ALL_PROTO_KINDS) do
local p = prototypes[f]
if p and p[name] then return p[name] end
end

return nil
end

return mod
2 changes: 1 addition & 1 deletion scripts/graphics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ local function sprite_name(sig)
fluid = "fluid",
virtual = "virtual-signal",
}
return typemap[sig.type] .. "." .. sig.name
return typemap[sig.type or "item"] .. "." .. sig.name
end

---@param elem LuaGuiElement
Expand Down
42 changes: 42 additions & 0 deletions scripts/ui/menu-items.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local mod = {}

---@param key string
---@param callback fun(fa.MenuCtx)
---@return fa.MenuRender
function mod.lazy_label(key, callback)
return {
key = key,
label = callback,
click = callback,
}
end

---@param key string
---@param text LocalisedString
---@return fa.MenuRender
function mod.simple_label(key, text)
return mod.lazy_label(key, function(ctx)
ctx.message:fragment(text)
end)
end

--[[
A "button"-like label: the text is static but you can click it to do something.
Remember though! This can change text on each render, so it's often useful for
dynamic labels too (the next render gets a chance to recompute).
]]
---@param key string
---@param label LocalisedString
---@param click_handler fun(fa.MenuCtx)
---@return fa.MenuItemRender
function mod.clickable_label(key, label, click_handler)
return {
key = key,
label = function(ctx)
ctx.message:fragment(label)
end,
click = click_handler,
}
end
return mod
Loading

0 comments on commit 0417077

Please sign in to comment.