mirror of
https://github.com/factoriolib/flib.git
synced 2025-09-04 08:26:22 +00:00
Move custom types under the flib
namespace
This commit is contained in:
@@ -6,23 +6,23 @@ local gui = require("__flib__.gui")
|
||||
local mod_gui = require("__core__.lualib.mod-gui")
|
||||
local table = require("__flib__.table")
|
||||
|
||||
--- @class FlibDictionaryStorage
|
||||
--- @class flib.DictionaryStorage
|
||||
--- @field init_ran boolean
|
||||
--- @field raw table<string, Dictionary>
|
||||
--- @field raw table<string, flib.Dictionary>
|
||||
--- @field raw_count integer
|
||||
--- @field to_translate string[]
|
||||
--- @field translated table<string, table<string, TranslatedDictionary>?>
|
||||
--- @field wip DictWipData?
|
||||
--- @field translated table<string, table<string, flib.TranslatedDictionary>?>
|
||||
--- @field wip flib.DictionaryWipData?
|
||||
|
||||
--- @class DictWipData
|
||||
--- @class flib.DictionaryWipData
|
||||
--- @field dict string
|
||||
--- @field dicts table<string, RawDictionary>
|
||||
--- @field finished boolean
|
||||
--- @field key string?
|
||||
--- @field last_batch_end DictTranslationRequest?
|
||||
--- @field last_batch_end flib.DictionaryTranslationRequest?
|
||||
--- @field language string
|
||||
--- @field received_count integer
|
||||
--- @field requests table<uint, DictTranslationRequest>
|
||||
--- @field requests table<uint, flib.DictionaryTranslationRequest>
|
||||
--- @field request_tick uint
|
||||
--- @field translator LuaPlayer
|
||||
|
||||
@@ -36,7 +36,7 @@ local flib_dictionary = {}
|
||||
local request_timeout_ticks = (60 * 5)
|
||||
|
||||
--- @param init_only boolean?
|
||||
--- @return FlibDictionaryStorage
|
||||
--- @return flib.DictionaryStorage
|
||||
local function get_data(init_only)
|
||||
if not storage.__flib or not storage.__flib.dictionary then
|
||||
error("Dictionary module was not properly initialized - ensure that all lifecycle events are handled.")
|
||||
@@ -58,7 +58,7 @@ local function get_translator(language)
|
||||
end
|
||||
end
|
||||
|
||||
--- @param data FlibDictionaryStorage
|
||||
--- @param data flib.DictionaryStorage
|
||||
local function update_gui(data)
|
||||
local wip = data.wip
|
||||
for _, player in pairs(game.players) do
|
||||
@@ -141,11 +141,11 @@ local function update_gui(data)
|
||||
end
|
||||
end
|
||||
|
||||
--- @param data FlibDictionaryStorage
|
||||
--- @param data flib.DictionaryStorage
|
||||
--- @return boolean success
|
||||
local function request_next_batch(data)
|
||||
local raw = data.raw
|
||||
local wip = data.wip --[[@as DictWipData]]
|
||||
local wip = data.wip --[[@as flib.DictionaryWipData]]
|
||||
if wip.finished then
|
||||
wip.last_batch_end = nil
|
||||
return false
|
||||
@@ -203,7 +203,7 @@ local function request_next_batch(data)
|
||||
return true
|
||||
end
|
||||
|
||||
--- @param data FlibDictionaryStorage
|
||||
--- @param data flib.DictionaryStorage
|
||||
local function handle_next_language(data)
|
||||
if not next(data.raw) then
|
||||
-- This can happen if handle_next_language is called during on_init or on_configuration_changed
|
||||
@@ -219,7 +219,7 @@ local function handle_next_language(data)
|
||||
for name in pairs(data.raw) do
|
||||
dicts[name] = {}
|
||||
end
|
||||
--- @type DictWipData
|
||||
--- @type flib.DictionaryWipData
|
||||
data.wip = {
|
||||
dict = next(data.raw),
|
||||
dicts = dicts,
|
||||
@@ -228,7 +228,7 @@ local function handle_next_language(data)
|
||||
key = nil,
|
||||
language = next_language,
|
||||
received_count = 0,
|
||||
--- @type table<uint, DictTranslationRequest>
|
||||
--- @type table<uint, flib.DictionaryTranslationRequest>
|
||||
requests = {},
|
||||
request_tick = 0,
|
||||
translator = translator,
|
||||
@@ -243,7 +243,7 @@ end
|
||||
|
||||
flib_dictionary.on_player_dictionaries_ready = script.generate_event_name()
|
||||
--- Called when a player's dictionaries are ready to be used. Handling this event is not required.
|
||||
--- @class EventData.on_player_dictionaries_ready: EventData
|
||||
--- @class flib.on_player_dictionaries_ready: EventData
|
||||
--- @field player_index uint
|
||||
|
||||
-- Lifecycle handlers
|
||||
@@ -252,7 +252,7 @@ function flib_dictionary.on_init()
|
||||
if not storage.__flib then
|
||||
storage.__flib = {}
|
||||
end
|
||||
--- @type FlibDictionaryStorage
|
||||
--- @type flib.DictionaryStorage
|
||||
storage.__flib.dictionary = {
|
||||
init_ran = false,
|
||||
player_language_requests = {},
|
||||
@@ -395,7 +395,7 @@ flib_dictionary.events = {
|
||||
|
||||
--- Create a new dictionary. The name must be unique.
|
||||
--- @param name string
|
||||
--- @param initial_strings Dictionary?
|
||||
--- @param initial_strings flib.Dictionary?
|
||||
function flib_dictionary.new(name, initial_strings)
|
||||
local data = get_data(true)
|
||||
local raw = data.raw
|
||||
@@ -426,7 +426,7 @@ end
|
||||
|
||||
--- Get all dictionaries for the player. Will return `nil` if the player's language has not finished translating.
|
||||
--- @param player_index uint
|
||||
--- @return table<string, TranslatedDictionary>?
|
||||
--- @return table<string, flib.TranslatedDictionary>?
|
||||
function flib_dictionary.get_all(player_index)
|
||||
local player = game.get_player(player_index)
|
||||
if not player then
|
||||
@@ -438,7 +438,7 @@ end
|
||||
--- Get the specified dictionary for the player. Will return `nil` if the dictionary has not finished translating.
|
||||
--- @param player_index uint
|
||||
--- @param dict_name string
|
||||
--- @return TranslatedDictionary?
|
||||
--- @return flib.TranslatedDictionary?
|
||||
function flib_dictionary.get(player_index, dict_name)
|
||||
local data = get_data()
|
||||
if not data.raw[dict_name] then
|
||||
@@ -448,20 +448,20 @@ function flib_dictionary.get(player_index, dict_name)
|
||||
return language_dicts[dict_name]
|
||||
end
|
||||
|
||||
--- @class DictLangRequest
|
||||
--- @class flib.DictionaryLanguageRequest
|
||||
--- @field player LuaPlayer
|
||||
--- @field tick uint
|
||||
|
||||
--- @class DictTranslationRequest
|
||||
--- @class flib.DictionaryTranslationRequest
|
||||
--- @field language string
|
||||
--- @field dict string
|
||||
--- @field key string
|
||||
|
||||
--- Localised strings identified by an internal key. Keys must be unique and language-agnostic.
|
||||
--- @alias Dictionary table<string, LocalisedString>
|
||||
--- @alias flib.Dictionary table<string, LocalisedString>
|
||||
|
||||
--- Translations are identified by their internal key. If the translation failed, then it will not be present. Locale
|
||||
--- fallback groups can be used if every key needs a guaranteed translation.
|
||||
--- @alias TranslatedDictionary table<string, string>
|
||||
--- @alias flib.TranslatedDictionary table<string, string>
|
||||
|
||||
return flib_dictionary
|
||||
|
@@ -14,7 +14,7 @@ local flib_gui_templates = {}
|
||||
--- @param technology LuaTechnology
|
||||
--- @param level uint
|
||||
--- @param research_state TechnologyResearchState
|
||||
--- @param on_click GuiElemHandler?
|
||||
--- @param on_click flib.GuiElemHandler?
|
||||
--- @param tags Tags?
|
||||
--- @param index uint?
|
||||
--- @return LuaGuiElement
|
||||
|
28
gui.lua
28
gui.lua
@@ -11,14 +11,14 @@ local flib_gui = {}
|
||||
|
||||
local handler_tag_key = "__" .. script.mod_name .. "_handler"
|
||||
|
||||
--- @type table<GuiElemHandler, string>
|
||||
--- @type table<flib.GuiElemHandler, string>
|
||||
local handlers = {}
|
||||
--- @type table<string, GuiElemHandler>
|
||||
--- @type table<string, flib.GuiElemHandler>
|
||||
local handlers_lookup = {}
|
||||
|
||||
--- Add a new child or children to the given GUI element.
|
||||
--- @param parent LuaGuiElement
|
||||
--- @param def GuiElemDef|GuiElemDef[] The element definition, or an array of element definitions.
|
||||
--- @param def flib.GuiElemDef|flib.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.
|
||||
@@ -129,8 +129,8 @@ end
|
||||
--- Add the given handler functions to the registry for use with `flib_gui.add`. Each handler must have a unique name. If a
|
||||
--- `wrapper` function is provided, it will be called instead, and will receive the event data and handler. The wrapper
|
||||
--- can be used to execute logic or gather data common to all handler functions for this GUI.
|
||||
--- @param new_handlers table<string, fun(e: GuiEventData)>
|
||||
--- @param wrapper fun(e: GuiEventData, handler: function)?
|
||||
--- @param new_handlers table<string, fun(e: flib.GuiEventData)>
|
||||
--- @param wrapper fun(e: flib.GuiEventData, handler: function)?
|
||||
--- @param prefix string?
|
||||
function flib_gui.add_handlers(new_handlers, wrapper, prefix)
|
||||
for name, handler in pairs(new_handlers) do
|
||||
@@ -155,7 +155,7 @@ end
|
||||
|
||||
--- Dispatch the handler associated with this event and GUI element. The handler must have been added using
|
||||
--- `flib_gui.add_handlers`.
|
||||
--- @param e GuiEventData
|
||||
--- @param e flib.GuiEventData
|
||||
--- @return boolean handled True if an event handler was called.
|
||||
function flib_gui.dispatch(e)
|
||||
local elem = e.element
|
||||
@@ -218,7 +218,7 @@ end
|
||||
---
|
||||
--- flib_gui.handle_events({ on_button_clicked = on_button_clicked })
|
||||
--- ```
|
||||
--- @param input GuiElemHandler|table<defines.events, GuiElemHandler?>
|
||||
--- @param input flib.GuiElemHandler|table<defines.events, flib.GuiElemHandler?>
|
||||
--- @param existing Tags?
|
||||
--- @return Tags
|
||||
function flib_gui.format_handlers(input, existing)
|
||||
@@ -240,19 +240,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 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
|
||||
--- @class flib.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|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.
|
||||
--- @field handler (flib.GuiElemHandler|table<defines.events, flib.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 flib.GuiElemDef[]? Children to add to this element.
|
||||
--- @field tab flib.GuiElemDef? To add a tab, specify `tab` and `content` and leave all other fields unset.
|
||||
--- @field content flib.GuiElemDef? To add a tab, specify `tab` and `content` and leave all other fields unset.
|
||||
|
||||
--- A handler function to invoke when receiving GUI events for this element.
|
||||
--- @alias GuiElemHandler fun(e: GuiEventData)
|
||||
--- @alias flib.GuiElemHandler fun(e: flib.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 flib.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
|
||||
|
@@ -16,7 +16,7 @@ function on_tick_n.init()
|
||||
if not storage.__flib then
|
||||
storage.__flib = {}
|
||||
end
|
||||
--- @type table<number, Tasks>
|
||||
--- @type table<number, flib.OnTickNTasks>
|
||||
storage.__flib.on_tick_n = {}
|
||||
end
|
||||
|
||||
@@ -24,7 +24,7 @@ end
|
||||
---
|
||||
--- Must be called **during** `on_tick`.
|
||||
--- @param tick number
|
||||
--- @return Tasks?
|
||||
--- @return flib.OnTickNTasks?
|
||||
function on_tick_n.retrieve(tick)
|
||||
-- Failsafe for rare cases where on_tick can fire before on_init
|
||||
if not storage.__flib or not storage.__flib.on_tick_n then
|
||||
@@ -40,7 +40,7 @@ end
|
||||
--- Add a task to execute on the given tick.
|
||||
--- @param tick number
|
||||
--- @param task any The data representing this task. This can be anything except for a `function`.
|
||||
--- @return TaskIdent ident An identifier for the task. Save this if you might remove the task before execution.
|
||||
--- @return flib.OnTickNTaskID ident An identifier for the task. Save this if you might remove the task before execution.
|
||||
function on_tick_n.add(tick, task)
|
||||
local list = storage.__flib.on_tick_n
|
||||
local tick_list = list[tick]
|
||||
@@ -55,7 +55,7 @@ function on_tick_n.add(tick, task)
|
||||
end
|
||||
|
||||
--- Remove a scheduled task.
|
||||
--- @param ident TaskIdent The identifier object for the task, as returned from `on-tick-n.add`.
|
||||
--- @param ident flib.OnTickNTaskID The identifier object for the task, as returned from `on-tick-n.add`.
|
||||
function on_tick_n.remove(ident)
|
||||
local tick_list = storage.__flib.on_tick_n[ident.tick]
|
||||
if not tick_list or not tick_list[ident.index] then
|
||||
@@ -68,7 +68,7 @@ function on_tick_n.remove(ident)
|
||||
end
|
||||
|
||||
--- A unique identifier for a previously added task, used in `on-tick-n.remove`.
|
||||
--- @class TaskIdent
|
||||
--- @class flib.OnTickNTaskID
|
||||
--- @field tick number The tick this task is scheduled for.
|
||||
--- @field index number The tasks' index in the tick's `Tasks` table.
|
||||
|
||||
@@ -93,6 +93,6 @@ end
|
||||
--- end
|
||||
--- end)
|
||||
--- ```
|
||||
--- @alias Tasks table<number, any>
|
||||
--- @alias flib.OnTickNTasks table<number, any>
|
||||
|
||||
return on_tick_n
|
||||
|
22
queue.lua
22
queue.lua
@@ -11,17 +11,17 @@ end
|
||||
--- @class flib_queue
|
||||
local flib_queue = {}
|
||||
|
||||
---@class Queue<T>: { [integer]: T, first: integer, last: integer }
|
||||
---@class flib.Queue<T>: { [integer]: T, first: integer, last: integer }
|
||||
|
||||
--- Create a new queue.
|
||||
--- @return Queue
|
||||
--- @return flib.Queue
|
||||
function flib_queue.new()
|
||||
return { first = 0, last = -1 }
|
||||
end
|
||||
|
||||
--- Push an element into the front of the queue.
|
||||
--- @generic T
|
||||
--- @param self Queue<T>
|
||||
--- @param self flib.Queue<T>
|
||||
--- @param value T
|
||||
function flib_queue.push_front(self, value)
|
||||
local first = self.first - 1
|
||||
@@ -31,7 +31,7 @@ end
|
||||
|
||||
--- Push an element into the back of the queue.
|
||||
--- @generic T
|
||||
--- @param self Queue<T>
|
||||
--- @param self flib.Queue<T>
|
||||
--- @param value `T`
|
||||
function flib_queue.push_back(self, value)
|
||||
local last = self.last + 1
|
||||
@@ -41,7 +41,7 @@ end
|
||||
|
||||
--- Retrieve an element from the front of the queue.
|
||||
--- @generic T
|
||||
--- @param self Queue<T>
|
||||
--- @param self flib.Queue<T>
|
||||
--- @return T?
|
||||
function flib_queue.pop_front(self)
|
||||
local first = self.first
|
||||
@@ -56,7 +56,7 @@ end
|
||||
|
||||
--- Retrieve an element from the back of the queue.
|
||||
--- @generic T
|
||||
--- @param self Queue<T>
|
||||
--- @param self flib.Queue<T>
|
||||
--- @return T?
|
||||
function flib_queue.pop_back(self)
|
||||
local last = self.last
|
||||
@@ -85,8 +85,8 @@ end
|
||||
--- end
|
||||
--- ```
|
||||
--- @generic T
|
||||
--- @param self Queue<T>
|
||||
--- @return fun(self: Queue<T>, index: integer): T
|
||||
--- @param self flib.Queue<T>
|
||||
--- @return fun(self: flib.Queue<T>, index: integer): T
|
||||
function flib_queue.iter(self)
|
||||
local i = self.first - 1
|
||||
return function()
|
||||
@@ -113,8 +113,8 @@ end
|
||||
--- end
|
||||
--- ```
|
||||
--- @generic T
|
||||
--- @param self Queue<T>
|
||||
--- @return fun(self: Queue<T>, index: integer): T
|
||||
--- @param self flib.Queue<T>
|
||||
--- @return fun(self: flib.Queue<T>, index: integer): T
|
||||
function flib_queue.iter_rev(self)
|
||||
local i = self.last + 1
|
||||
return function()
|
||||
@@ -127,7 +127,7 @@ end
|
||||
|
||||
--- Get the length of the queue.
|
||||
--- @generic T
|
||||
--- @param self Queue<T>
|
||||
--- @param self flib.Queue<T>
|
||||
--- @return number
|
||||
function flib_queue.length(self)
|
||||
return math.abs(self.last - self.first + 1)
|
||||
|
22
table.lua
22
table.lua
@@ -21,8 +21,8 @@ end
|
||||
--- Shallow copy an array's values into a new array.
|
||||
---
|
||||
--- This function is optimized specifically for arrays, and should be used in place of `table.shallow_copy` for arrays.
|
||||
--- @param arr Array
|
||||
--- @return Array
|
||||
--- @param arr flib.Array
|
||||
--- @return flib.Array
|
||||
function flib_table.array_copy(arr)
|
||||
local new_arr = {}
|
||||
for i = 1, #arr do
|
||||
@@ -32,8 +32,8 @@ function flib_table.array_copy(arr)
|
||||
end
|
||||
|
||||
--- Merge all of the given arrays into a single array.
|
||||
--- @param arrays Array An array of arrays to merge.
|
||||
--- @return Array
|
||||
--- @param arrays flib.Array An array of arrays to merge.
|
||||
--- @return flib.Array
|
||||
function flib_table.array_merge(arrays)
|
||||
local output = {}
|
||||
local i = 0
|
||||
@@ -154,7 +154,7 @@ end
|
||||
--- log(tbl.foo) -- logs "baz"
|
||||
--- log(tbl.set) -- logs "3"
|
||||
--- ```
|
||||
--- @param tables Array An array of tables to merge.
|
||||
--- @param tables flib.Array An array of tables to merge.
|
||||
--- @return table
|
||||
function flib_table.deep_merge(tables)
|
||||
local output = {}
|
||||
@@ -413,7 +413,7 @@ end
|
||||
---
|
||||
--- This function utilizes [insertion sort](https://en.wikipedia.org/wiki/Insertion_sort), which is _extremely_ inefficient with large data sets. However, you can spread the sorting over multiple ticks, reducing the performance impact. Only use this function if `table.sort` is too slow.
|
||||
--- @generic V
|
||||
--- @param arr Array<V>
|
||||
--- @param arr flib.Array<V>
|
||||
--- @param from_index number? The index to start iteration at (inclusive). Pass `nil` or a number less than `2` to begin at the start of the array.
|
||||
--- @param iterations number The number of iterations to perform. Higher is more performance-heavy. This number should be adjusted based on the performance impact of the custom `comp` function (if any) and the size of the array.
|
||||
--- @param comp fun(a: V, b: V) A comparison function for sorting. Must return truthy if `a < b`.
|
||||
@@ -523,10 +523,10 @@ flib_table.size = _ENV.table_size
|
||||
--- log(serpent.line(arr)) -- {10, 20, 30, 40, 50, 60, 70, 80, 90} (unchanged)
|
||||
--- ```
|
||||
--- @generic V
|
||||
--- @param arr Array<V>
|
||||
--- @param arr flib.Array<V>
|
||||
--- @param start number? default: `1`
|
||||
--- @param stop number? Stop at this index. If zero or negative, will stop `n` items from the end of the array (default: `#arr`).
|
||||
--- @return Array<V> A new array with the copied values.
|
||||
--- @return flib.Array<V> A new array with the copied values.
|
||||
function flib_table.slice(arr, start, stop)
|
||||
local output = {}
|
||||
local n = #arr
|
||||
@@ -559,10 +559,10 @@ end
|
||||
--- log(serpent.line(arr)) -- {10, 20, 80, 90} (values were removed)
|
||||
--- ```
|
||||
--- @generic V
|
||||
--- @param arr Array<V>
|
||||
--- @param arr flib.Array<V>
|
||||
--- @param start number default: `1`
|
||||
--- @param stop number? Stop at this index. If zero or negative, will stop `n` items from the end of the array (default: `#arr`).
|
||||
--- @return Array<V> A new array with the extracted values.
|
||||
--- @return flib.Array<V> A new array with the extracted values.
|
||||
function flib_table.splice(arr, start, stop)
|
||||
local output = {}
|
||||
local n = #arr
|
||||
@@ -583,6 +583,6 @@ function flib_table.splice(arr, start, stop)
|
||||
return output
|
||||
end
|
||||
|
||||
--- @class Array<T>: { [integer]: T }
|
||||
--- @class flib.Array<T>: { [integer]: T }
|
||||
|
||||
return flib_table
|
||||
|
@@ -5,17 +5,17 @@ local flib_dictionary = require("__flib__.dictionary-lite")
|
||||
-- of the things I need to search, prefixed by their type (i.e. `fluid/crude-oil`, `item/iron-plate`, etc).
|
||||
local function build_dictionaries()
|
||||
for type, prototypes in pairs({
|
||||
entity = game.entity_prototypes,
|
||||
equipment = game.equipment_prototypes,
|
||||
equipment_category = game.equipment_category_prototypes,
|
||||
fluid = game.fluid_prototypes,
|
||||
fuel_category = game.fuel_category_prototypes,
|
||||
item = game.item_prototypes,
|
||||
item_group = game.item_group_prototypes,
|
||||
recipe = game.recipe_prototypes,
|
||||
recipe_category = game.recipe_category_prototypes,
|
||||
resource_category = game.resource_category_prototypes,
|
||||
technology = game.technology_prototypes,
|
||||
entity = prototypes.entity,
|
||||
equipment = prototypes.equipment,
|
||||
equipment_category = prototypes.equipment_category,
|
||||
fluid = prototypes.fluid,
|
||||
fuel_category = prototypes.fuel_category,
|
||||
item = prototypes.item,
|
||||
item_group = prototypes.item_group,
|
||||
recipe = prototypes.recipe,
|
||||
recipe_category = prototypes.recipe_category,
|
||||
resource_category = prototypes.resource_category,
|
||||
technology = prototypes.technology,
|
||||
}) do
|
||||
flib_dictionary.new(type)
|
||||
for name, prototype in pairs(prototypes) do
|
||||
@@ -43,6 +43,7 @@ script.on_configuration_changed(function()
|
||||
end)
|
||||
|
||||
-- `on_player_dictionaries_ready` is raised when a given player's dictionaries are... ready. Shocking!
|
||||
--- @param e flib.on_player_dictionaries_ready
|
||||
script.on_event(flib_dictionary.on_player_dictionaries_ready, function(e)
|
||||
-- Alternatively, you can get specific dictionaries with `flib_dictionary.get(e.player_index, "item")` et al.
|
||||
-- For the aforementioned "search" dictionary, I do not handle this event, and instead call `get()` during my
|
||||
|
@@ -6,20 +6,20 @@
|
||||
local flib_gui = require("__flib__.gui-lite")
|
||||
local mod_gui = require("__core__.lualib.mod-gui")
|
||||
|
||||
--- @alias FlibTestGuiMode
|
||||
--- @alias flib_test.TestGuiMode
|
||||
--- | "all"
|
||||
--- | "active"
|
||||
--- | "completed"
|
||||
|
||||
--- @class FlibTestGui
|
||||
--- @class flib_test.TestGui
|
||||
--- @field elems table<string, LuaGuiElement>
|
||||
--- @field player LuaPlayer
|
||||
--- @field completed_count integer
|
||||
--- @field items_left integer
|
||||
--- @field mode FlibTestGuiMode
|
||||
--- @field mode flib_test.TestGuiMode
|
||||
--- @field pinned boolean
|
||||
|
||||
--- @class FlibTestGuiBase
|
||||
--- @class flib.TestGuiBase
|
||||
local gui = {}
|
||||
|
||||
--- @param name string
|
||||
@@ -167,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
|
||||
|
||||
--- @type FlibTestGui
|
||||
--- @type flib_test.TestGui
|
||||
storage.guis[player.index] = {
|
||||
elems = elems,
|
||||
player = player,
|
||||
@@ -188,10 +188,10 @@ function gui.on_textfield_text_changed(_, e)
|
||||
end
|
||||
end
|
||||
|
||||
--- @param self FlibTestGui
|
||||
--- @param self flib_test.TestGui
|
||||
--- @param e EventData.on_gui_checked_state_changed
|
||||
function gui.change_mode(self, e)
|
||||
local mode = e.element.tags.mode --[[@as FlibTestGuiMode]]
|
||||
local mode = e.element.tags.mode --[[@as flib_test.TestGuiMode]]
|
||||
self.mode = mode
|
||||
self.elems.all_radio.state = mode == "all"
|
||||
self.elems.active_radio.state = mode == "active"
|
||||
@@ -202,7 +202,7 @@ function gui.change_mode(self, e)
|
||||
end
|
||||
end
|
||||
|
||||
--- @param self FlibTestGui
|
||||
--- @param self flib_test.TestGui
|
||||
function gui.clear_completed(self)
|
||||
for _, checkbox in pairs(self.elems.todos_flow.children) do
|
||||
if checkbox.state then
|
||||
@@ -213,12 +213,12 @@ function gui.clear_completed(self)
|
||||
gui.update_footer(self)
|
||||
end
|
||||
|
||||
--- @param self FlibTestGui
|
||||
--- @param self flib_test.TestGui
|
||||
function gui.hide(self)
|
||||
self.elems.flib_todo_window.visible = false
|
||||
end
|
||||
|
||||
--- @param self FlibTestGui
|
||||
--- @param self flib_test.TestGui
|
||||
--- @param e EventData.on_gui_checked_state_changed
|
||||
function gui.on_todo_toggled(self, e)
|
||||
local checkbox = e.element
|
||||
@@ -237,7 +237,7 @@ function gui.on_todo_toggled(self, e)
|
||||
gui.update_footer(self)
|
||||
end
|
||||
|
||||
--- @param self FlibTestGui
|
||||
--- @param self flib_test.TestGui
|
||||
--- @param e EventData.on_gui_confirmed
|
||||
function gui.on_textfield_confirmed(self, e)
|
||||
local title = e.element.text
|
||||
@@ -263,7 +263,7 @@ function gui.on_textfield_confirmed(self, e)
|
||||
gui.update_footer(self)
|
||||
end
|
||||
|
||||
--- @param self FlibTestGui
|
||||
--- @param self flib_test.TestGui
|
||||
function gui.on_window_closed(self)
|
||||
-- Don't close when enabling the pin
|
||||
if self.pinned then
|
||||
@@ -272,7 +272,7 @@ function gui.on_window_closed(self)
|
||||
gui.hide(self)
|
||||
end
|
||||
|
||||
--- @param self FlibTestGui
|
||||
--- @param self flib_test.TestGui
|
||||
function gui.show(self)
|
||||
self.elems.flib_todo_window.visible = true
|
||||
self.elems.textfield.focus()
|
||||
@@ -281,7 +281,7 @@ function gui.show(self)
|
||||
end
|
||||
end
|
||||
|
||||
--- @param self FlibTestGui
|
||||
--- @param self flib_test.TestGui
|
||||
function gui.toggle_pinned(self)
|
||||
-- "Pinning" the GUI will remove it from player.opened, allowing it to coexist with other windows.
|
||||
-- I highly recommend implementing this for your GUIs. flib includes the requisite sprites and locale for the button.
|
||||
@@ -301,7 +301,7 @@ function gui.toggle_pinned(self)
|
||||
end
|
||||
end
|
||||
|
||||
--- @param self FlibTestGui
|
||||
--- @param self flib_test.TestGui
|
||||
function gui.toggle_visible(self)
|
||||
if self.elems.flib_todo_window.visible then
|
||||
gui.hide(self)
|
||||
@@ -310,7 +310,7 @@ function gui.toggle_visible(self)
|
||||
end
|
||||
end
|
||||
|
||||
--- @param self FlibTestGui
|
||||
--- @param self flib_test.TestGui
|
||||
function gui.update_footer(self)
|
||||
self.elems.count_label.caption = self.items_left .. " items left"
|
||||
self.elems.clear_completed_button.enabled = self.completed_count > 0
|
||||
|
Reference in New Issue
Block a user