Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ahicks92 committed Feb 10, 2025
1 parent 8008364 commit ee79129
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 26 deletions.
1 change: 1 addition & 0 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4934,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
26 changes: 26 additions & 0 deletions locale/en/ui-blueprints.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +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
8 changes: 2 additions & 6 deletions scripts/blueprints.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ local fa_building_tools = require("scripts.building-tools")
local fa_mining_tools = require("scripts.player-mining-tools")
local fa_graphics = require("scripts.graphics")
local dirs = defines.direction
local BlueprintsMenu = require("scripts.ui.menus.blueprints-menu")

local mod = {}

Expand Down Expand Up @@ -44,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 @@ -663,7 +662,7 @@ BLUEPRINT_MENU_LENGTH = 12

function mod.blueprint_menu_open(pindex)
if players[pindex].vanilla_mode then return end
BlueprintsMenu.blueprint_menu_tabs:open(pindex, {})
-- Opening the ui is one level up, to avoid circular imports.

players[pindex].move_queue = {}

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
3 changes: 2 additions & 1 deletion scripts/ui/menu-items.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function mod.lazy_label(key, callback)
return {
key = key,
label = callback,
clikc = callback,
click = callback,
}
end

Expand All @@ -32,6 +32,7 @@ dynamic labels too (the next render gets a chance to recompute).
---@return fa.MenuItemRender
function mod.clickable_label(key, label, click_handler)
return {
key = key,
label = function(ctx)
ctx.message:fragment(label)
end,
Expand Down
30 changes: 17 additions & 13 deletions scripts/ui/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,18 @@ local Sounds = require("scripts.ui.sounds")

local mod = {}

--[[
Tell the menu itself to do things from click handlers etc.
]]
---@class fa.MenuController
---@field close fun(self)

---@class fa.MenuCtx
---@field pindex number
---@field message_builder fa.MessageBuilder
---@field state any Returned from the state function and maintained in `storage`.
---@field item_state table? Private to the menu item itself.
---@field controller fa.MenuController

---@alias fa.MenuEventCallback fun(fa.MenuCtx)
---@alias fa.MenuEventPredicate fun(fa.MenuCtx): boolean
Expand Down Expand Up @@ -202,18 +209,7 @@ end
--[[
Return a descriptor for a tab containing a menu.
The tab's name will be `menutab-menu_name`. The menu will look into
`parameters` for `parameters.menu_name`. If found, that is the menu's
parameters. For now the only parameter which can be passed is `initial_position
= { key = "foo" }`. For example:
```
{
blueprint_menu = {
initial_position = { key = "read_name" },
}
}
```
The tab's name will be `menutab-menu_name`.
Which would put the cursor in this tab on that menu item. If the tablist is
only one tab, that makes it so that the menu opens to the right place directly;
Expand All @@ -228,11 +224,19 @@ function mod.declare_menu(opts)
---@param ctx fa.MenuTabCtxInternal
---@return fa.MenuCtx
local function build_user_ctx(ctx)
local controller = {}
---@cast controller fa.MenuController

function controller:close()
ctx.force_close = true
end

return {
pindex = ctx.pindex,
message = ctx.message,
state = ctx.state.menu_state,
item_state = nil,
controller = controller,
}
end

Expand Down Expand Up @@ -273,7 +277,6 @@ function mod.declare_menu(opts)
---@param item fa.MenuItemRender
---@return fa.MenuCtx
function build_item_ctx(ctx, item)
print(serpent.line(ctx, { nocode = true }))
local u_ctx = build_user_ctx(ctx)
u_ctx.item_state = ctx.state.item_states[item.key]
if not u_ctx.item_state then
Expand Down Expand Up @@ -352,6 +355,7 @@ function mod.declare_menu(opts)
if not item then return end

item.click(build_item_ctx(ctx, item))
Sounds.play_menu_click(ctx.pindex)
end)
end

Expand Down
Loading

0 comments on commit ee79129

Please sign in to comment.