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 bf5454d commit 8008364
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
3 changes: 3 additions & 0 deletions locale/en/ui-blueprints.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[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.
18 changes: 18 additions & 0 deletions scripts/ui/menu-items.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,22 @@ function mod.simple_label(key, 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 {
label = function(ctx)
ctx.message:fragment(label)
end,
click = click_handler,
}
end
return mod
8 changes: 6 additions & 2 deletions scripts/ui/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,14 @@ local mod = {}
---@field click fa.MenuEventCallback Can push to message_builder, but doesn't have to.
---@field enabled fa.MenuEventPredicate

-- Parameter to the callback for initial state.
---@class fa.MenuStateCtx
---@field pindex number

---@class fa.MenuSpec
---@field tab_name string
---@field title LocalisedString?
---@field state_callback fun(any): any
---@field state_callback fun(fa.MenuStateCtx): any?
---@field render_callback fun(fa.MenuCtx): fa.MenuRender

---@class fa.MenuRender
Expand Down Expand Up @@ -252,7 +256,7 @@ function mod.declare_menu(opts)
ctx.state = {
item_states = {},
prev_items = {},
menu_state = opts.state_callback(ctx.state),
menu_state = opts.state_callback({ pindex = ctx.pindex }),
position_key = nil,
}

Expand Down
34 changes: 27 additions & 7 deletions scripts/ui/menus/blueprints-menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,42 @@ local Menu = require("scripts.ui.menu")
local MenuItems = require("scripts.ui.menu-items")
local TabList = require("scripts.ui.tab-list")
local Functools = require("scripts.functools")
local Graphics = require("scripts.graphics")

local mod = {}

local function state()
local function state(ctx)
-- We don't use this; it relies on the blueprint in hand.
return {}
end

---@type fun(fa.MenuCtx): fa.MenuRender
---@type fun(fa.MenuCtx): fa.MenuRender?
local function render(ctx)
local p = game.get_player(pindex)
if not p then return nil end

---@type LuaItemStack
local bp = p.cursor_stack
if not bp.is_blueprint then return nil end

local menu_items = {}

if not bp.is_blueprint_setup() then
table.insert(menu_items, MenuItems.make_label("blueprint-not-imported", { "fa.ui-blueprints-menu-limited" }))
end

table.insert(
menu_items,
MenuItems.clickable_label("import", { "fa.ui-blueprints-menu-import" }, function(ctx)
-- For now, this shells out to legacy UI: close ourselves, and let it
-- do the work.
state.players[pindex].blueprint_menu.edit_import = true
local frame = Graphics.create_text_field_frame(pindex, "blueprint-edit-import")
end)
)

return {
items = {
MenuItems.simple_label("label1", "hello"),
MenuItems.simple_label("key2", "label 2"),
MenuItems.simple_label("key3", "more labels"),
},
items = menu_items,
}
end

Expand Down

0 comments on commit 8008364

Please sign in to comment.