mirror of
https://github.com/factoriolib/flib.git
synced 2025-09-04 08:26:22 +00:00
Fix a zillion language server warnings
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
Version: 0.14.0
|
||||
Date: ????
|
||||
Features:
|
||||
- Added `gui-lite.format_handlers` function.
|
||||
- Added `gui-templates` module for building various common and/or annoying GUI components, including technology slots.
|
||||
- Added `technology` module with various runtime technology-related utilities.
|
||||
- Added technology slot styles.
|
||||
|
@@ -2,6 +2,27 @@ local gui = require("__flib__/gui-lite")
|
||||
local mod_gui = require("__core__/lualib/mod-gui")
|
||||
local table = require("__flib__/table")
|
||||
|
||||
--- @class FlibDictionaryGlobal
|
||||
--- @field init_ran boolean
|
||||
--- @field player_language_requests table<uint, DictLangRequest>
|
||||
--- @field player_languages table<uint, string>
|
||||
--- @field raw table<string, Dictionary>
|
||||
--- @field raw_count integer
|
||||
--- @field to_translate string[]
|
||||
--- @field translated table<string, table<string, TranslatedDictionary>>
|
||||
--- @field wip DictWipData?
|
||||
|
||||
--- @class DictWipData
|
||||
--- @field dict string
|
||||
--- @field dicts table<string, RawDictionary>
|
||||
--- @field finished boolean
|
||||
--- @field key string?
|
||||
--- @field language string
|
||||
--- @field received_count integer
|
||||
--- @field requests table<uint, DictTranslationRequest>
|
||||
--- @field request_tick uint
|
||||
--- @field translator LuaPlayer
|
||||
|
||||
--- Utilities for creating dictionaries of localised string translations.
|
||||
--- ```lua
|
||||
--- local flib_dictionary = require("__flib__/dictionary-lite")
|
||||
@@ -12,7 +33,7 @@ local flib_dictionary = {}
|
||||
local request_timeout_ticks = (60 * 5)
|
||||
|
||||
--- @param init_only boolean?
|
||||
--- @return flib_dictionary_global
|
||||
--- @return FlibDictionaryGlobal
|
||||
local function get_data(init_only)
|
||||
if not global.__flib or not global.__flib.dictionary then
|
||||
error("Dictionary module was not properly initialized - ensure that all lifecycle events are handled.")
|
||||
@@ -24,7 +45,7 @@ local function get_data(init_only)
|
||||
return data
|
||||
end
|
||||
|
||||
--- @param data flib_dictionary_global
|
||||
--- @param data FlibDictionaryGlobal
|
||||
--- @param language string
|
||||
--- @return LuaPlayer?
|
||||
local function get_translator(data, language)
|
||||
@@ -44,7 +65,7 @@ local function get_translator(data, language)
|
||||
end
|
||||
end
|
||||
|
||||
--- @param data flib_dictionary_global
|
||||
--- @param data FlibDictionaryGlobal
|
||||
local function update_gui(data)
|
||||
local wip = data.wip
|
||||
for _, player in pairs(game.players) do
|
||||
@@ -67,6 +88,7 @@ local function update_gui(data)
|
||||
type = "frame",
|
||||
name = "pane",
|
||||
style = "inside_shallow_frame_with_padding",
|
||||
--- @diagnostic disable-next-line: missing-fields
|
||||
style_mods = { top_padding = 8 },
|
||||
direction = "vertical",
|
||||
},
|
||||
@@ -79,10 +101,12 @@ local function update_gui(data)
|
||||
type = "flow",
|
||||
name = script.mod_name,
|
||||
style = "centering_horizontal_flow",
|
||||
--- @diagnostic disable-next-line: missing-fields
|
||||
style_mods = { top_margin = 4, horizontal_spacing = 8 },
|
||||
{
|
||||
type = "label",
|
||||
style = "caption_label",
|
||||
--- @diagnostic disable-next-line: missing-fields
|
||||
style_mods = { minimal_width = 130 },
|
||||
caption = { "?", { "mod-name." .. script.mod_name }, script.mod_name },
|
||||
ignored_by_interaction = true,
|
||||
@@ -92,6 +116,7 @@ local function update_gui(data)
|
||||
{
|
||||
type = "progressbar",
|
||||
name = "bar",
|
||||
--- @diagnostic disable-next-line: missing-fields
|
||||
style_mods = { top_margin = 1, width = 100 },
|
||||
ignored_by_interaction = true,
|
||||
},
|
||||
@@ -99,6 +124,7 @@ local function update_gui(data)
|
||||
type = "label",
|
||||
name = "percentage",
|
||||
style = "bold_label",
|
||||
--- @diagnostic disable-next-line: missing-fields
|
||||
style_mods = { width = 24, horizontal_align = "right" },
|
||||
ignored_by_interaction = true,
|
||||
},
|
||||
@@ -124,7 +150,7 @@ local function update_gui(data)
|
||||
end
|
||||
end
|
||||
|
||||
--- @param data flib_dictionary_global
|
||||
--- @param data FlibDictionaryGlobal
|
||||
--- @return boolean success
|
||||
local function request_next_batch(data)
|
||||
local raw = data.raw
|
||||
@@ -175,6 +201,7 @@ local function request_next_batch(data)
|
||||
for i = 1, #ids do
|
||||
wip.requests[ids[i]] = requests[i]
|
||||
end
|
||||
--- @diagnostic disable-next-line: missing-fields
|
||||
wip.request_tick = game.tick
|
||||
|
||||
update_gui(data)
|
||||
@@ -182,7 +209,7 @@ local function request_next_batch(data)
|
||||
return true
|
||||
end
|
||||
|
||||
--- @param data flib_dictionary_global
|
||||
--- @param data FlibDictionaryGlobal
|
||||
local function handle_next_language(data)
|
||||
while not data.wip and #data.to_translate > 0 do
|
||||
local next_language = table.remove(data.to_translate, 1)
|
||||
@@ -191,6 +218,7 @@ local function handle_next_language(data)
|
||||
if translator then
|
||||
-- Start translation
|
||||
local dicts = {}
|
||||
--- @type string?
|
||||
local first_dict
|
||||
for name in pairs(data.raw) do
|
||||
first_dict = first_dict or name
|
||||
@@ -200,7 +228,7 @@ local function handle_next_language(data)
|
||||
if not first_dict then
|
||||
return
|
||||
end
|
||||
--- @class DictWipData
|
||||
--- @type DictWipData
|
||||
data.wip = {
|
||||
dict = first_dict,
|
||||
dicts = dicts,
|
||||
@@ -239,27 +267,23 @@ function flib_dictionary.on_init()
|
||||
if not global.__flib then
|
||||
global.__flib = {}
|
||||
end
|
||||
--- @class flib_dictionary_global
|
||||
--- @type FlibDictionaryGlobal
|
||||
global.__flib.dictionary = {
|
||||
init_ran = false,
|
||||
--- @type table<uint, string>
|
||||
player_languages = {},
|
||||
--- @type table<uint, DictLangRequest>
|
||||
player_language_requests = {},
|
||||
--- @type table<string, Dictionary>
|
||||
player_languages = {},
|
||||
raw = {},
|
||||
raw_count = 0,
|
||||
--- @type string[]
|
||||
to_translate = {},
|
||||
--- @type table<string, table<string, TranslatedDictionary>>
|
||||
translated = {},
|
||||
--- @type DictWipData?
|
||||
wip = nil,
|
||||
}
|
||||
-- Initialize all existing players
|
||||
for player_index, player in pairs(game.players) do
|
||||
if player.connected then
|
||||
flib_dictionary.on_player_joined_game({
|
||||
name = defines.events.on_player_joined_game,
|
||||
tick = game.tick,
|
||||
--- @cast player_index uint
|
||||
player_index = player_index,
|
||||
})
|
||||
|
@@ -70,7 +70,6 @@ function flib_dictionary.new(name, keep_untranslated, initial_contents)
|
||||
for key, value in pairs(initial_contents or {}) do
|
||||
self:add(key, value)
|
||||
end
|
||||
--- @diagnostic disable-next-line
|
||||
raw[name] = { strings = self.strings, keep_untranslated = keep_untranslated }
|
||||
|
||||
return self
|
||||
|
40
gui-lite.lua
40
gui-lite.lua
@@ -14,7 +14,7 @@ local handlers_lookup = {}
|
||||
|
||||
--- Add a new child or children to the given GUI element.
|
||||
--- @param parent LuaGuiElement
|
||||
--- @param def GuiElemDef Can be a single element, or an array of elements.
|
||||
--- @param def GuiElemDef|GuiElemDef[] The element definition, or an array of element definitions.
|
||||
--- @param elems table<string, LuaGuiElement>? Optional initial `elems` table.
|
||||
--- @return table<string, LuaGuiElement> elems Elements with names will be collected into this table.
|
||||
--- @return LuaGuiElement first The element that was created first; the "top level" element.
|
||||
@@ -195,7 +195,24 @@ function flib_gui.handle_events()
|
||||
end
|
||||
end
|
||||
|
||||
--- @param input GuiElemHandler
|
||||
--- Format the given handlers for use in a GUI element's tags. An alternative to using `flib_gui.add` if event handling
|
||||
--- is the only desired feature.
|
||||
---
|
||||
--- ### Example
|
||||
---
|
||||
--- ```lua
|
||||
--- --- @param e EventData.on_gui_click
|
||||
--- local function on_button_clicked(e)
|
||||
--- game.print("You clicked it!")
|
||||
--- end
|
||||
---
|
||||
--- player.gui.screen.add({
|
||||
--- type = "button",
|
||||
--- caption = "Click me!",
|
||||
--- tags = flib_gui.format_handlers({ [defines.events.on_gui_click] = on_button_clicked }),
|
||||
--- })
|
||||
--- ```
|
||||
--- @param input GuiElemHandler|table<defines.events, GuiElemHandler>
|
||||
--- @return Tags
|
||||
function flib_gui.format_handlers(input)
|
||||
local out
|
||||
@@ -212,22 +229,19 @@ end
|
||||
|
||||
--- A GUI element definition. This extends `LuaGuiElement.add_param` with several new attributes.
|
||||
--- Children may be defined in the array portion as an alternative to the `children` subtable.
|
||||
--- @class GuiElemDefClass: LuaGuiElement.add_param
|
||||
--- @field style_mods LuaStyle? Modifications to make to the element's style
|
||||
--- @field elem_mods LuaGuiElement? Modifications to make to the element itself
|
||||
--- @class GuiElemDef: LuaGuiElement.add_param.button|LuaGuiElement.add_param.camera|LuaGuiElement.add_param.checkbox|LuaGuiElement.add_param.choose_elem_button|LuaGuiElement.add_param.drop_down|LuaGuiElement.add_param.flow|LuaGuiElement.add_param.frame|LuaGuiElement.add_param.line|LuaGuiElement.add_param.list_box|LuaGuiElement.add_param.minimap|LuaGuiElement.add_param.progressbar|LuaGuiElement.add_param.radiobutton|LuaGuiElement.add_param.scroll_pane|LuaGuiElement.add_param.slider|LuaGuiElement.add_param.sprite|LuaGuiElement.add_param.sprite_button|LuaGuiElement.add_param.switch|LuaGuiElement.add_param.tab|LuaGuiElement.add_param.table|LuaGuiElement.add_param.text_box|LuaGuiElement.add_param.textfield
|
||||
--- @field style_mods LuaStyle? Modifications to make to the element's style.
|
||||
--- @field elem_mods LuaGuiElement? Modifications to make to the element itself.
|
||||
--- @field drag_target string? Set the element's drag target to the element whose name matches this string. The drag target must be present in the `elems` table.
|
||||
--- @field handler GuiElemHandler? Handler(s) to assign to this element
|
||||
--- @field children GuiElemDef[]? Children to add to this element
|
||||
--- @field handler (GuiElemHandler|table<defines.events, GuiElemHandler>)? Handler(s) to assign to this element. If assigned to a function, that function will be called for any GUI event on this element.
|
||||
--- @field children GuiElemDef[]? Children to add to this element.
|
||||
--- @field tab GuiElemDef? To add a tab, specify `tab` and `content` and leave all other fields unset.
|
||||
--- @field content GuiElemDef? To add a tab, specify `tab` and `content` and leave all other fields unset.
|
||||
|
||||
--- @alias GuiElemDef GuiElemDefClass|GuiElemDef[]
|
||||
|
||||
--- A handler function to invoke when receiving GUI events for this element. Alternatively, separate handlers may be
|
||||
--- specified for different events.
|
||||
--- @alias GuiElemHandler fun(e: GuiEventData)|table<defines.events, fun(e: GuiEventData)>
|
||||
--- A handler function to invoke when receiving GUI events for this element.
|
||||
--- @alias GuiElemHandler fun(e: GuiEventData)
|
||||
|
||||
--- Aggregate type of all possible GUI events.
|
||||
--- @alias GuiEventData EventData.on_gui_checked_state_changed|EventData.on_gui_click|EventData.on_gui_closed|EventData.on_gui_confirmed|EventData.on_gui_elem_changed|EventData.on_gui_location_changed|EventData.on_gui_opened|EventData.on_gui_selected_tab_changed|EventData.on_gui_selection_state_changed|EventData.on_gui_switch_state_changed|EventData.on_gui_text_changed|EventData.on_gui_value_changed
|
||||
--- @alias GuiEventData EventData.on_gui_checked_state_changed|EventData.on_gui_click|EventData.on_gui_closed|EventData.on_gui_confirmed|EventData.on_gui_elem_changed|EventData.on_gui_location_changed|EventData.on_gui_opened|EventData.on_gui_selected_tab_changed|EventData.on_gui_selection_state_changed|EventData.on_gui_switch_state_changed|EventData.on_gui_text_changed|EventData.on_gui_value_changed
|
||||
|
||||
return flib_gui
|
||||
|
@@ -5,11 +5,12 @@ local flib_technology = require("__flib__/technology")
|
||||
|
||||
local flib_gui_templates = {}
|
||||
|
||||
--- Create and return a technology slot.
|
||||
--- @param parent LuaGuiElement
|
||||
--- @param technology LuaTechnology
|
||||
--- @param level uint
|
||||
--- @param research_state TechnologyResearchState
|
||||
--- @param on_click function
|
||||
--- @param on_click GuiElemHandler?
|
||||
--- @return LuaGuiElement
|
||||
function flib_gui_templates.technology_slot(parent, technology, level, research_state, on_click)
|
||||
local progress = flib_technology.get_research_progress(technology, level)
|
||||
|
@@ -6,6 +6,19 @@
|
||||
local flib_gui = require("__flib__/gui-lite")
|
||||
local mod_gui = require("__core__/lualib/mod-gui")
|
||||
|
||||
--- @alias FlibTestGuiMode
|
||||
--- | "all"
|
||||
--- | "active"
|
||||
--- | "completed"
|
||||
|
||||
--- @class FlibTestGui
|
||||
--- @field elems table<string, LuaGuiElement>
|
||||
--- @field player LuaPlayer
|
||||
--- @field completed_count integer
|
||||
--- @field items_left integer
|
||||
--- @field mode FlibTestGuiMode
|
||||
--- @field pinned boolean
|
||||
|
||||
--- @class FlibTestGuiBase
|
||||
local gui = {}
|
||||
|
||||
@@ -37,6 +50,7 @@ function gui.build(player)
|
||||
name = "flib_todo_window",
|
||||
direction = "vertical",
|
||||
-- Use `elem_mods` to make modifications to the GUI element after creation.
|
||||
--- @diagnostic disable-next-line: missing-fields
|
||||
elem_mods = { auto_center = true },
|
||||
-- If `handler` is a function, it will call that function for any GUI event on this element.
|
||||
-- If it is a dictioanry of event -> function, it will call the corresponding function for the corresponding event.
|
||||
@@ -60,6 +74,7 @@ function gui.build(player)
|
||||
type = "frame",
|
||||
style = "inside_shallow_frame",
|
||||
-- Use `style_mods` to make modifications to the element's style.
|
||||
--- @diagnostic disable-next-line: missing-fields
|
||||
style_mods = { width = 500 },
|
||||
direction = "vertical",
|
||||
{
|
||||
@@ -152,7 +167,7 @@ function gui.build(player)
|
||||
-- In a real mod, you would want to initially hide the GUI and not set opened until the player opens it.
|
||||
player.opened = elems.flib_todo_window
|
||||
|
||||
--- @class FlibTestGui
|
||||
--- @type FlibTestGui
|
||||
global.guis[player.index] = {
|
||||
elems = elems,
|
||||
player = player,
|
||||
@@ -176,7 +191,7 @@ end
|
||||
--- @param self FlibTestGui
|
||||
--- @param e EventData.on_gui_checked_state_changed
|
||||
function gui.change_mode(self, e)
|
||||
local mode = e.element.tags.mode --[[@as string]]
|
||||
local mode = e.element.tags.mode --[[@as FlibTestGuiMode]]
|
||||
self.mode = mode
|
||||
self.elems.all_radio.state = mode == "all"
|
||||
self.elems.active_radio.state = mode == "active"
|
||||
@@ -233,6 +248,7 @@ function gui.on_textfield_confirmed(self, e)
|
||||
local todos_flow = self.elems.todos_flow
|
||||
flib_gui.add(todos_flow, {
|
||||
type = "checkbox",
|
||||
--- @diagnostic disable-next-line: missing-fields
|
||||
style_mods = { horizontally_stretchable = true },
|
||||
caption = title,
|
||||
state = false,
|
||||
|
@@ -34,7 +34,7 @@ end
|
||||
function flib_train.rotate_carriage(entity)
|
||||
local disconnected_back = entity.disconnect_rolling_stock(defines.rail_direction.back)
|
||||
local disconnected_front = entity.disconnect_rolling_stock(defines.rail_direction.front)
|
||||
entity.rotate()
|
||||
entity.rotate({})
|
||||
-- Only reconnect the side that was disconnected
|
||||
local reconnected_front = disconnected_front
|
||||
local reconnected_back = disconnected_back
|
||||
|
3
typedefs.lua
Normal file
3
typedefs.lua
Normal file
@@ -0,0 +1,3 @@
|
||||
--- @meta
|
||||
|
||||
default_shadow = {}
|
Reference in New Issue
Block a user