mirror of
https://github.com/factoriolib/flib.git
synced 2025-09-04 08:26:22 +00:00
Format all files with stylua
This commit is contained in:
7
.kakrc
Normal file
7
.kakrc
Normal file
@@ -0,0 +1,7 @@
|
||||
hook global WinSetOption filetype=lua %{
|
||||
hook window BufWritePre .* format
|
||||
}
|
||||
|
||||
hook global WinSetOption filetype=(factorio-changelog|ini) %{
|
||||
hook window BufWritePost .* spell
|
||||
}
|
73
area.lua
73
area.lua
@@ -10,7 +10,6 @@
|
||||
---
|
||||
local flib_area = {}
|
||||
|
||||
|
||||
--- Expands an area to its outer tile edges.
|
||||
--- @param self BoundingBox
|
||||
function flib_area.ceil(self)
|
||||
@@ -19,11 +18,11 @@ function flib_area.ceil(self)
|
||||
end
|
||||
self.left_top = {
|
||||
x = math.floor(self.left_top.x),
|
||||
y = math.floor(self.left_top.y)
|
||||
y = math.floor(self.left_top.y),
|
||||
}
|
||||
self.right_bottom = {
|
||||
x = math.ceil(self.right_bottom.x),
|
||||
y = math.ceil(self.right_bottom.y)
|
||||
y = math.ceil(self.right_bottom.y),
|
||||
}
|
||||
|
||||
return self
|
||||
@@ -38,7 +37,7 @@ function flib_area.center(self)
|
||||
end
|
||||
return {
|
||||
x = self.left_top.x + (flib_area.width(self) / 2),
|
||||
y = self.left_top.y + (flib_area.height(self) / 2)
|
||||
y = self.left_top.y + (flib_area.height(self) / 2),
|
||||
}
|
||||
end
|
||||
|
||||
@@ -56,11 +55,11 @@ function flib_area.center_on(self, center_point)
|
||||
|
||||
self.left_top = {
|
||||
x = center_point.x - (width / 2),
|
||||
y = center_point.y - (height / 2)
|
||||
y = center_point.y - (height / 2),
|
||||
}
|
||||
self.right_bottom = {
|
||||
x = center_point.x + (width / 2),
|
||||
y = center_point.y + (height / 2)
|
||||
y = center_point.y + (height / 2),
|
||||
}
|
||||
|
||||
return self
|
||||
@@ -76,11 +75,11 @@ function flib_area.contains_area(self, other_area)
|
||||
end
|
||||
|
||||
return (
|
||||
self.left_top.x <= other_area.left_top.x
|
||||
and self.left_top.y <= other_area.left_top.y
|
||||
and self.right_bottom.x >= other_area.right_bottom.x
|
||||
and self.right_bottom.y >= other_area.right_bottom.y
|
||||
)
|
||||
self.left_top.x <= other_area.left_top.x
|
||||
and self.left_top.y <= other_area.left_top.y
|
||||
and self.right_bottom.x >= other_area.right_bottom.x
|
||||
and self.right_bottom.y >= other_area.right_bottom.y
|
||||
)
|
||||
end
|
||||
|
||||
--- Checks if the area contains the given position.
|
||||
@@ -93,11 +92,11 @@ function flib_area.contains_position(self, position)
|
||||
end
|
||||
|
||||
return (
|
||||
self.left_top.x <= position.x
|
||||
and self.right_bottom.x >= position.x
|
||||
and self.left_top.y <= position.y
|
||||
and self.right_bottom.y >= position.y
|
||||
)
|
||||
self.left_top.x <= position.x
|
||||
and self.right_bottom.x >= position.x
|
||||
and self.left_top.y <= position.y
|
||||
and self.right_bottom.y >= position.y
|
||||
)
|
||||
end
|
||||
|
||||
--- Adds left_bottom and right_top keys to the area.
|
||||
@@ -112,11 +111,11 @@ function flib_area.corners(self)
|
||||
|
||||
self.left_bottom = {
|
||||
x = self.left_top.x,
|
||||
y = self.right_bottom.y
|
||||
y = self.right_bottom.y,
|
||||
}
|
||||
self.right_top = {
|
||||
x = self.right_bottom.x,
|
||||
y = self.left_top.y
|
||||
y = self.left_top.y,
|
||||
}
|
||||
|
||||
return self
|
||||
@@ -165,11 +164,11 @@ function flib_area.expand_to_contain_area(self, other_area)
|
||||
|
||||
self.left_top = {
|
||||
x = self.left_top.x < other_area.left_top.x and self.left_top.x or other_area.left_top.x,
|
||||
y = self.left_top.y < other_area.left_top.y and self.left_top.y or other_area.left_top.y
|
||||
y = self.left_top.y < other_area.left_top.y and self.left_top.y or other_area.left_top.y,
|
||||
}
|
||||
self.right_bottom = {
|
||||
x = self.right_bottom.x > other_area.right_bottom.x and self.right_bottom.x or other_area.right_bottom.x,
|
||||
y = self.right_bottom.y > other_area.right_bottom.y and self.right_bottom.y or other_area.right_bottom.y
|
||||
y = self.right_bottom.y > other_area.right_bottom.y and self.right_bottom.y or other_area.right_bottom.y,
|
||||
}
|
||||
|
||||
return self
|
||||
@@ -206,11 +205,11 @@ function flib_area.floor(self)
|
||||
|
||||
self.left_top = {
|
||||
x = math.ceil(self.left_top.x),
|
||||
y = math.ceil(self.left_top.y)
|
||||
y = math.ceil(self.left_top.y),
|
||||
}
|
||||
self.right_bottom = {
|
||||
x = math.floor(self.right_bottom.x),
|
||||
y = math.floor(self.right_bottom.y)
|
||||
y = math.floor(self.right_bottom.y),
|
||||
}
|
||||
|
||||
return self
|
||||
@@ -247,15 +246,15 @@ function flib_area.from_position(position, snap)
|
||||
end
|
||||
|
||||
if snap then
|
||||
local floored_position = {x = math.floor(position.x), y = math.floor(position.y)}
|
||||
local floored_position = { x = math.floor(position.x), y = math.floor(position.y) }
|
||||
return {
|
||||
left_top = {x = floored_position.x, y = floored_position.y},
|
||||
right_bottom = {x = floored_position.x + 1, y = floored_position.y + 1}
|
||||
left_top = { x = floored_position.x, y = floored_position.y },
|
||||
right_bottom = { x = floored_position.x + 1, y = floored_position.y + 1 },
|
||||
}
|
||||
else
|
||||
return {
|
||||
left_top = {x = position.x - 0.5, y = position.y - 0.5},
|
||||
right_bottom = {x = position.x + 0.5, y = position.y + 0.5}
|
||||
left_top = { x = position.x - 0.5, y = position.y - 0.5 },
|
||||
right_bottom = { x = position.x + 0.5, y = position.y + 0.5 },
|
||||
}
|
||||
end
|
||||
end
|
||||
@@ -269,8 +268,8 @@ end
|
||||
--- @return BoundingBox
|
||||
function flib_area.from_shorthand(area)
|
||||
return {
|
||||
left_top = {x = area[1][1], y = area[1][2]},
|
||||
right_bottom = {x = area[2][1], y = area[2][2]},
|
||||
left_top = { x = area[1][1], y = area[1][2] },
|
||||
right_bottom = { x = area[2][1], y = area[2][2] },
|
||||
}
|
||||
end
|
||||
|
||||
@@ -312,7 +311,7 @@ function flib_area.iterate(self, step)
|
||||
return function()
|
||||
if first then
|
||||
first = false
|
||||
return {x = x, y = y}
|
||||
return { x = x, y = y }
|
||||
end
|
||||
|
||||
local new_x = x + step
|
||||
@@ -328,7 +327,7 @@ function flib_area.iterate(self, step)
|
||||
end
|
||||
end
|
||||
|
||||
return {x = x, y = y}
|
||||
return { x = x, y = y }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -359,7 +358,7 @@ function flib_area.load(area)
|
||||
if not area.left_top then
|
||||
area = flib_area.from_shorthand(area)
|
||||
end
|
||||
return setmetatable(area, {__index = flib_area})
|
||||
return setmetatable(area, { __index = flib_area })
|
||||
end
|
||||
|
||||
--- Moves the area by the given delta.
|
||||
@@ -409,12 +408,12 @@ function flib_area.strip(self)
|
||||
return {
|
||||
left_top = {
|
||||
x = self.left_top.x,
|
||||
y = self.left_top.y
|
||||
y = self.left_top.y,
|
||||
},
|
||||
right_bottom = {
|
||||
x = self.right_bottom.x,
|
||||
y = self.right_bottom.y
|
||||
}
|
||||
y = self.right_bottom.y,
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
@@ -432,8 +431,8 @@ end
|
||||
--- @return BoundingBox
|
||||
function flib_area.to_shorthand(self)
|
||||
return {
|
||||
{self.left_top.x, self.left_top.y},
|
||||
{self.right_bottom.x, self.right_bottom.y},
|
||||
{ self.left_top.x, self.left_top.y },
|
||||
{ self.right_bottom.x, self.right_bottom.y },
|
||||
}
|
||||
end
|
||||
|
||||
|
@@ -23,7 +23,7 @@ function flib_data_util.copy_prototype(prototype, new_name, remove_icon)
|
||||
p.result = new_name
|
||||
end
|
||||
if p.results then
|
||||
for _,result in pairs(p.results) do
|
||||
for _, result in pairs(p.results) do
|
||||
if result.name == prototype.name then
|
||||
result.name = new_name
|
||||
end
|
||||
@@ -47,7 +47,7 @@ end
|
||||
--- @return IconSpecification[]|nil
|
||||
function flib_data_util.create_icons(prototype, new_layers)
|
||||
if new_layers then
|
||||
for _,new_layer in pairs(new_layers) do
|
||||
for _, new_layer in pairs(new_layers) do
|
||||
if not new_layer.icon or not new_layer.icon_size then
|
||||
return nil
|
||||
end
|
||||
@@ -55,10 +55,10 @@ function flib_data_util.create_icons(prototype, new_layers)
|
||||
end
|
||||
|
||||
if prototype.icons then
|
||||
local icons ={}
|
||||
local icons = {}
|
||||
for _, v in pairs(prototype.icons) do
|
||||
-- Over define as much as possible to minimize weirdness: https://forums.factorio.com/viewtopic.php?f=25&t=81980
|
||||
icons[#icons+1] = {
|
||||
icons[#icons + 1] = {
|
||||
icon = v.icon,
|
||||
icon_size = v.icon_size or prototype.icon_size or 32,
|
||||
icon_mipmaps = v.icon_mipmaps or prototype.icon_mipmaps or 0,
|
||||
@@ -69,55 +69,52 @@ function flib_data_util.create_icons(prototype, new_layers)
|
||||
end
|
||||
if new_layers then
|
||||
for _, new_layer in pairs(new_layers) do
|
||||
icons[#icons+1] = new_layer
|
||||
icons[#icons + 1] = new_layer
|
||||
end
|
||||
end
|
||||
return icons
|
||||
|
||||
elseif prototype.icon then
|
||||
local icons =
|
||||
{
|
||||
local icons = {
|
||||
{
|
||||
icon = prototype.icon,
|
||||
icon_size = prototype.icon_size,
|
||||
icon_mipmaps = prototype.icon_mipmaps,
|
||||
tint = {r=1, g=1, b=1, a=1}
|
||||
tint = { r = 1, g = 1, b = 1, a = 1 },
|
||||
},
|
||||
}
|
||||
if new_layers then
|
||||
for _, new_layer in pairs(new_layers) do
|
||||
icons[#icons+1] = new_layer
|
||||
icons[#icons + 1] = new_layer
|
||||
end
|
||||
end
|
||||
return icons
|
||||
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
local exponent_multipliers = {
|
||||
['y'] = 0.000000000000000000000001,
|
||||
['z'] = 0.000000000000000000001,
|
||||
['a'] = 0.000000000000000001,
|
||||
['f'] = 0.000000000000001,
|
||||
['p'] = 0.000000000001,
|
||||
['n'] = 0.000000001,
|
||||
['u'] = 0.000001, -- μ is invalid
|
||||
['m'] = 0.001,
|
||||
['c'] = 0.01,
|
||||
['d'] = 0.1,
|
||||
[''] = 1,
|
||||
['da'] = 10,
|
||||
['h'] = 100,
|
||||
['k'] = 1000,
|
||||
['M'] = 1000000,
|
||||
['G'] = 1000000000,
|
||||
['T'] = 1000000000000,
|
||||
['P'] = 1000000000000000,
|
||||
['E'] = 1000000000000000000,
|
||||
['Z'] = 1000000000000000000000,
|
||||
['Y'] = 1000000000000000000000000,
|
||||
["y"] = 0.000000000000000000000001,
|
||||
["z"] = 0.000000000000000000001,
|
||||
["a"] = 0.000000000000000001,
|
||||
["f"] = 0.000000000000001,
|
||||
["p"] = 0.000000000001,
|
||||
["n"] = 0.000000001,
|
||||
["u"] = 0.000001, -- μ is invalid
|
||||
["m"] = 0.001,
|
||||
["c"] = 0.01,
|
||||
["d"] = 0.1,
|
||||
[""] = 1,
|
||||
["da"] = 10,
|
||||
["h"] = 100,
|
||||
["k"] = 1000,
|
||||
["M"] = 1000000,
|
||||
["G"] = 1000000000,
|
||||
["T"] = 1000000000000,
|
||||
["P"] = 1000000000000000,
|
||||
["E"] = 1000000000000000000,
|
||||
["Z"] = 1000000000000000000000,
|
||||
["Y"] = 1000000000000000000000000,
|
||||
}
|
||||
|
||||
--- Converts an energy string to base unit value + suffix.
|
||||
@@ -154,10 +151,10 @@ function flib_data_util.build_sprite(name, position, filename, size, mipmap_coun
|
||||
position = position,
|
||||
size = size,
|
||||
mipmap_count = mipmap_count,
|
||||
flags = {"icon"}
|
||||
flags = { "icon" },
|
||||
}
|
||||
if mods then
|
||||
for k,v in pairs(mods) do
|
||||
for k, v in pairs(mods) do
|
||||
def[k] = v
|
||||
end
|
||||
end
|
||||
|
101
dictionary.lua
101
dictionary.lua
@@ -14,7 +14,7 @@ local raw = {}
|
||||
local use_local_storage = false
|
||||
|
||||
local function key_value(key, value)
|
||||
return key..inner_separator..value..separator
|
||||
return key .. inner_separator .. value .. separator
|
||||
end
|
||||
|
||||
--- @class RawDictionary
|
||||
@@ -27,7 +27,7 @@ local RawDictionary = {}
|
||||
--- @param internal string An unique, language-agnostic identifier for this translation.
|
||||
--- @param translation LocalisedString
|
||||
function RawDictionary:add(internal, translation)
|
||||
local to_add = {"", internal, inner_separator, translation, separator}
|
||||
local to_add = { "", internal, inner_separator, translation, separator }
|
||||
|
||||
local ref = self.ref
|
||||
local i = self.i + 1
|
||||
@@ -37,7 +37,7 @@ function RawDictionary:add(internal, translation)
|
||||
else
|
||||
local r_i = self.r_i + 1
|
||||
if r_i <= max_depth then
|
||||
local new_level = {"", to_add}
|
||||
local new_level = { "", to_add }
|
||||
ref[i] = new_level
|
||||
self.ref = new_level
|
||||
self.i = 2
|
||||
@@ -45,7 +45,7 @@ function RawDictionary:add(internal, translation)
|
||||
else
|
||||
local s_i = self.s_i + 1
|
||||
self.s_i = s_i
|
||||
local new_set = {"", to_add}
|
||||
local new_set = { "", to_add }
|
||||
self.ref = new_set
|
||||
self.strings[s_i] = new_set
|
||||
self.i = 2
|
||||
@@ -63,30 +63,26 @@ end
|
||||
--- @return RawDictionary
|
||||
function flib_dictionary.new(name, keep_untranslated, initial_contents)
|
||||
if raw[name] then
|
||||
error("Dictionary with the name `"..name.."` already exists.")
|
||||
error("Dictionary with the name `" .. name .. "` already exists.")
|
||||
end
|
||||
|
||||
local initial_string = {""}
|
||||
local self = setmetatable(
|
||||
{
|
||||
-- Indices
|
||||
i = 1,
|
||||
r_i = 1,
|
||||
s_i = 1,
|
||||
-- Internal
|
||||
ref = initial_string,
|
||||
strings = {initial_string},
|
||||
-- Meta
|
||||
name = name,
|
||||
},
|
||||
{__index = RawDictionary}
|
||||
)
|
||||
local initial_string = { "" }
|
||||
local self = setmetatable({
|
||||
-- Indices
|
||||
i = 1,
|
||||
r_i = 1,
|
||||
s_i = 1,
|
||||
-- Internal
|
||||
ref = initial_string,
|
||||
strings = { initial_string },
|
||||
-- Meta
|
||||
name = name,
|
||||
}, { __index = RawDictionary })
|
||||
|
||||
for key, value in pairs(initial_contents or {}) do
|
||||
self:add(key, value)
|
||||
end
|
||||
raw[name] = {strings = self.strings, keep_untranslated = keep_untranslated}
|
||||
|
||||
raw[name] = { strings = self.strings, keep_untranslated = keep_untranslated }
|
||||
|
||||
return self
|
||||
end
|
||||
@@ -103,7 +99,7 @@ function flib_dictionary.init()
|
||||
in_process = {},
|
||||
players = {},
|
||||
raw = {},
|
||||
translated = {}
|
||||
translated = {},
|
||||
}
|
||||
if use_local_storage then
|
||||
raw = {}
|
||||
@@ -139,7 +135,9 @@ function flib_dictionary.translate(player)
|
||||
error("Player must be connected to the game before this function can be called!")
|
||||
end
|
||||
local player_data = global.__flib.dictionary.players[player.index]
|
||||
if player_data then return end
|
||||
if player_data then
|
||||
return
|
||||
end
|
||||
|
||||
global.__flib.dictionary.players[player.index] = {
|
||||
player = player,
|
||||
@@ -147,7 +145,7 @@ function flib_dictionary.translate(player)
|
||||
requested_tick = game.tick,
|
||||
}
|
||||
|
||||
player.request_translation({"", "FLIB_LOCALE_IDENTIFIER", separator, {"locale-identifier"}})
|
||||
player.request_translation({ "", "FLIB_LOCALE_IDENTIFIER", separator, { "locale-identifier" } })
|
||||
end
|
||||
|
||||
local function request_translation(player_data)
|
||||
@@ -168,14 +166,14 @@ local function request_translation(player_data)
|
||||
end
|
||||
end
|
||||
|
||||
player_data.player.request_translation{
|
||||
player_data.player.request_translation({
|
||||
"",
|
||||
key_value("FLIB_DICTIONARY_MOD", script.mod_name),
|
||||
key_value("FLIB_DICTIONARY_NAME", player_data.dictionary),
|
||||
key_value("FLIB_DICTIONARY_LANGUAGE", player_data.language),
|
||||
key_value("FLIB_DICTIONARY_STRING_INDEX", player_data.i),
|
||||
string,
|
||||
}
|
||||
})
|
||||
|
||||
player_data.requested_tick = game.tick
|
||||
end
|
||||
@@ -195,7 +193,7 @@ function flib_dictionary.check_skipped()
|
||||
-- is saved will not be returned when that save is loaded
|
||||
if (player_data.requested_tick or 0) + translation_timeout <= tick then
|
||||
if player_data.status == "get_language" then
|
||||
player_data.player.request_translation({"", "FLIB_LOCALE_IDENTIFIER", separator, {"locale-identifier"}})
|
||||
player_data.player.request_translation({ "", "FLIB_LOCALE_IDENTIFIER", separator, { "locale-identifier" } })
|
||||
end
|
||||
if player_data.status == "translating" then
|
||||
request_translation(player_data)
|
||||
@@ -204,17 +202,16 @@ function flib_dictionary.check_skipped()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--- Escape match special characters
|
||||
local function match_literal(s)
|
||||
return string.gsub(s, "%-", "%%-")
|
||||
return string.gsub(s, "%-", "%%-")
|
||||
end
|
||||
|
||||
local dictionary_match_string = key_value("^FLIB_DICTIONARY_MOD", match_literal(script.mod_name))
|
||||
..key_value("FLIB_DICTIONARY_NAME", "(.-)")
|
||||
..key_value("FLIB_DICTIONARY_LANGUAGE", "(.-)")
|
||||
..key_value("FLIB_DICTIONARY_STRING_INDEX", "(%d-)")
|
||||
.."(.*)$"
|
||||
.. key_value("FLIB_DICTIONARY_NAME", "(.-)")
|
||||
.. key_value("FLIB_DICTIONARY_LANGUAGE", "(.-)")
|
||||
.. key_value("FLIB_DICTIONARY_STRING_INDEX", "(%d-)")
|
||||
.. "(.*)$"
|
||||
|
||||
--- Processes a returned translation batch, then request the next batch or return the finished dictionaries.
|
||||
---
|
||||
@@ -222,7 +219,9 @@ local dictionary_match_string = key_value("^FLIB_DICTIONARY_MOD", match_literal(
|
||||
--- @param event_data on_string_translated
|
||||
--- @return TranslationFinishedOutput?
|
||||
function flib_dictionary.process_translation(event_data)
|
||||
if not event_data.translated then return end
|
||||
if not event_data.translated then
|
||||
return
|
||||
end
|
||||
local script_data = global.__flib.dictionary
|
||||
if string.find(event_data.result, "FLIB_DICTIONARY_NAME") then
|
||||
local _, _, dict_name, dict_lang, string_index, translation = string.find(
|
||||
@@ -233,17 +232,21 @@ function flib_dictionary.process_translation(event_data)
|
||||
if dict_name and dict_lang and string_index and translation then
|
||||
local language_data = script_data.in_process[dict_lang]
|
||||
-- In some cases, this can fire before on_configuration_changed
|
||||
if not language_data then return end
|
||||
if not language_data then
|
||||
return
|
||||
end
|
||||
local dictionary = language_data.dictionaries[dict_name]
|
||||
if not dictionary then return end
|
||||
if not dictionary then
|
||||
return
|
||||
end
|
||||
local dict_data = raw[dict_name]
|
||||
local player_data = script_data.players[event_data.player_index]
|
||||
|
||||
-- If this number does not match, this is a duplicate, so ignore it
|
||||
if tonumber(string_index) == player_data.i then
|
||||
-- Extract current string's translations
|
||||
for str in string.gmatch(translation, "(.-)"..separator) do
|
||||
local _, _, key, value = string.find(str, "^(.-)"..inner_separator.."(.-)$")
|
||||
for str in string.gmatch(translation, "(.-)" .. separator) do
|
||||
local _, _, key, value = string.find(str, "^(.-)" .. inner_separator .. "(.-)$")
|
||||
if key then
|
||||
-- If `keep_untranslated` is true, then use the key as the value if it failed
|
||||
local failed = string.find(value, "Unknown key:")
|
||||
@@ -269,16 +272,18 @@ function flib_dictionary.process_translation(event_data)
|
||||
for _, player_index in pairs(language_data.players) do
|
||||
script_data.players[player_index] = nil
|
||||
end
|
||||
return {dictionaries = language_data.dictionaries, language = dict_lang, players = language_data.players}
|
||||
return { dictionaries = language_data.dictionaries, language = dict_lang, players = language_data.players }
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif string.find(event_data.result, "^FLIB_LOCALE_IDENTIFIER") then
|
||||
local _, _, language = string.find(event_data.result, "^FLIB_LOCALE_IDENTIFIER"..separator.."(.*)$")
|
||||
local _, _, language = string.find(event_data.result, "^FLIB_LOCALE_IDENTIFIER" .. separator .. "(.*)$")
|
||||
if language then
|
||||
local player_data = script_data.players[event_data.player_index]
|
||||
-- Handle a duplicate
|
||||
if not player_data or player_data.status == "translating" then return end
|
||||
if not player_data or player_data.status == "translating" then
|
||||
return
|
||||
end
|
||||
|
||||
player_data.language = language
|
||||
|
||||
@@ -286,7 +291,7 @@ function flib_dictionary.process_translation(event_data)
|
||||
local dictionaries = script_data.translated[language]
|
||||
if dictionaries then
|
||||
script_data.players[event_data.player_index] = nil
|
||||
return {dictionaries = dictionaries, language = language, players = {event_data.player_index}}
|
||||
return { dictionaries = dictionaries, language = language, players = { event_data.player_index } }
|
||||
end
|
||||
local in_process = script_data.in_process[language]
|
||||
if in_process then
|
||||
@@ -302,8 +307,10 @@ function flib_dictionary.process_translation(event_data)
|
||||
|
||||
-- Add language to in process data
|
||||
script_data.in_process[language] = {
|
||||
dictionaries = table.map(raw, function(_) return {} end),
|
||||
players = {event_data.player_index}
|
||||
dictionaries = table.map(raw, function(_)
|
||||
return {}
|
||||
end),
|
||||
players = { event_data.player_index },
|
||||
}
|
||||
|
||||
-- Start translating
|
||||
@@ -325,7 +332,9 @@ function flib_dictionary.cancel_translation(player_index)
|
||||
if player_data then
|
||||
if player_data.status == "translating" then
|
||||
local in_process = script_data.in_process[player_data.language]
|
||||
if not in_process then error("Dafuq?") end
|
||||
if not in_process then
|
||||
error("Dafuq?")
|
||||
end
|
||||
if #in_process.players > 1 then
|
||||
-- Copy progress to another player with the same language
|
||||
local first_player = in_process.players[1]
|
||||
|
@@ -83,7 +83,7 @@ function flib_direction.to_vector(direction, distance)
|
||||
elseif direction == flib_direction.northwest then
|
||||
x, y = x - distance, y - distance
|
||||
end
|
||||
return {x = x, y = y}
|
||||
return { x = x, y = y }
|
||||
end
|
||||
|
||||
--- Returns a two-dimensional vector from a cardinal direction.
|
||||
@@ -96,13 +96,13 @@ end
|
||||
-- @treturn Concepts.Position
|
||||
function flib_direction.to_vector_2d(direction, longitudinal, orthogonal)
|
||||
if direction == defines.direction.north then
|
||||
return {x=orthogonal, y=-longitudinal}
|
||||
return { x = orthogonal, y = -longitudinal }
|
||||
elseif direction == defines.direction.south then
|
||||
return {x=-orthogonal, y=longitudinal}
|
||||
return { x = -orthogonal, y = longitudinal }
|
||||
elseif direction == defines.direction.east then
|
||||
return {x=longitudinal, y=orthogonal}
|
||||
return { x = longitudinal, y = orthogonal }
|
||||
elseif direction == defines.direction.west then
|
||||
return {x=-longitudinal, y=-orthogonal}
|
||||
return { x = -longitudinal, y = -orthogonal }
|
||||
end
|
||||
end
|
||||
|
||||
|
@@ -11,12 +11,12 @@ local dictionary = require("__flib__.dictionary")
|
||||
local migration = require("__flib__.migration")
|
||||
|
||||
local function create_demo_dictionaries()
|
||||
for _, type in pairs{"entity", "fluid", "item", "recipe", "technology", "tile"} do
|
||||
for _, type in pairs({ "entity", "fluid", "item", "recipe", "technology", "tile" }) do
|
||||
-- If the object's name doesn't have a translation, use its internal name as the translation
|
||||
local Names = dictionary.new(type.."_names", true)
|
||||
local Names = dictionary.new(type .. "_names", true)
|
||||
-- If a description doesn't exist, it won't exist in the resulting dictionary either
|
||||
local Descriptions = dictionary.new(type.."_descriptions")
|
||||
for name, prototype in pairs(game[type.."_prototypes"]) do
|
||||
local Descriptions = dictionary.new(type .. "_descriptions")
|
||||
for name, prototype in pairs(game[type .. "_prototypes"]) do
|
||||
Names:add(name, prototype.localised_name)
|
||||
Descriptions:add(name, prototype.localised_description)
|
||||
end
|
||||
@@ -72,7 +72,7 @@ event.on_string_translated(function(e)
|
||||
if language_data then
|
||||
for _, player_index in pairs(language_data.players) do
|
||||
global.player_dictionaries[player_index] = language_data.dictionaries
|
||||
game.print("Player "..player_index.." now has dictionaries for their language!")
|
||||
game.print("Player " .. player_index .. " now has dictionaries for their language!")
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
@@ -1,61 +1,66 @@
|
||||
local event = require("__flib__.event")
|
||||
|
||||
-- LuaBootstrap events are usable simply by replacing `script` with `event`
|
||||
event.on_init(function() log("on init!") end)
|
||||
event.on_load(function() log("on load!") end)
|
||||
event.on_configuration_changed(function(e) log("on configuration changed!") end)
|
||||
event.on_nth_tick(60, function(e) log("one second has passed!") end)
|
||||
event.on_init(function()
|
||||
log("on init!")
|
||||
end)
|
||||
event.on_load(function()
|
||||
log("on load!")
|
||||
end)
|
||||
event.on_configuration_changed(function(e)
|
||||
log("on configuration changed!")
|
||||
end)
|
||||
event.on_nth_tick(60, function(e)
|
||||
log("one second has passed!")
|
||||
end)
|
||||
|
||||
-- syntax shortcuts - when registering to a single `defines.events` event, follow this syntax:
|
||||
-- `script.on_event(defines.events.on_player_created, handler, filters)` -> `event.on_player_created(handler, filters)`
|
||||
event.on_player_created(function(e) log("player "..game.get_player(e.player_index).name.." created!") end)
|
||||
event.on_player_created(function(e)
|
||||
log("player " .. game.get_player(e.player_index).name .. " created!")
|
||||
end)
|
||||
|
||||
-- event.register - register to custom-input events, or multiple events simultaneously
|
||||
-- mostly equivalent to `script.on_event` for these cases
|
||||
event.register("my-custom-input", function(e) log("custom input pressed!") end)
|
||||
event.register({defines.events.on_player_left_game, defines.events.on_player_removed}, function(e)
|
||||
event.register("my-custom-input", function(e)
|
||||
log("custom input pressed!")
|
||||
end)
|
||||
event.register({ defines.events.on_player_left_game, defines.events.on_player_removed }, function(e)
|
||||
log("player left or was removed")
|
||||
end)
|
||||
|
||||
-- bonus feature: `event.register` can register custom-inputs and `defines.events` events simultaneously
|
||||
-- trying to do this with `script.on_event` will error
|
||||
event.register({"my-other-custom-input", defines.events.on_lua_shortcut}, function(e)
|
||||
event.register({ "my-other-custom-input", defines.events.on_lua_shortcut }, function(e)
|
||||
log("input or shortcut pressed!")
|
||||
end)
|
||||
|
||||
-- bonus feature: you can add event filters to multiple events simultaneously, assuming the filters are compatible with
|
||||
-- all of the events
|
||||
-- trying to do this with `script.on_event` will error
|
||||
event.register(
|
||||
{defines.events.on_built_entity, defines.events.on_robot_built_entity},
|
||||
function(e)
|
||||
log("entity built!")
|
||||
end,
|
||||
{
|
||||
{filter="type", type="transport-belt"},
|
||||
{filter="type", type="underground-belt"},
|
||||
{filter="type", type="splitter"},
|
||||
{filter="type", type="loader"},
|
||||
{filter="type", type="loader-1x1"}
|
||||
}
|
||||
)
|
||||
event.register({ defines.events.on_built_entity, defines.events.on_robot_built_entity }, function(e)
|
||||
log("entity built!")
|
||||
end, {
|
||||
{ filter = "type", type = "transport-belt" },
|
||||
{ filter = "type", type = "underground-belt" },
|
||||
{ filter = "type", type = "splitter" },
|
||||
{ filter = "type", type = "loader" },
|
||||
{ filter = "type", type = "loader-1x1" },
|
||||
})
|
||||
|
||||
-- these functions are simple name changes - they are functionally identical
|
||||
local registration_number = event.register_on_entity_destroyed(...)
|
||||
local my_event_id = event.generate_id()
|
||||
local handler = event.get_handler(defines.events.on_tick)
|
||||
event.raise(my_event_id, {my_data="foo"})
|
||||
event.raise(my_event_id, { my_data = "foo" })
|
||||
local order = event.get_order()
|
||||
local filters = event.get_filters(defines.events.on_built_entity)
|
||||
|
||||
-- `event.set_filters` has the bonus ability to add compatible filters to multiple events simultaneously
|
||||
event.set_filters(
|
||||
{defines.events.on_built_entity, defines.events.on_robot_built_entity},
|
||||
{
|
||||
{filter="type", type="transport-belt"},
|
||||
{filter="type", type="underground-belt"},
|
||||
{filter="type", type="splitter"},
|
||||
{filter="type", type="loader"},
|
||||
{filter="type", type="loader-1x1"}
|
||||
}
|
||||
)
|
||||
event.set_filters({ defines.events.on_built_entity, defines.events.on_robot_built_entity }, {
|
||||
{ filter = "type", type = "transport-belt" },
|
||||
{ filter = "type", type = "underground-belt" },
|
||||
{ filter = "type", type = "splitter" },
|
||||
{ filter = "type", type = "loader" },
|
||||
{ filter = "type", type = "loader-1x1" },
|
||||
})
|
||||
|
@@ -21,11 +21,11 @@ local table = require("__flib__.table")
|
||||
|
||||
local todo_gui = {}
|
||||
|
||||
local view_modes = table.invert{
|
||||
local view_modes = table.invert({
|
||||
"all",
|
||||
"active",
|
||||
"completed"
|
||||
}
|
||||
"completed",
|
||||
})
|
||||
|
||||
-- ROOT
|
||||
|
||||
@@ -62,43 +62,39 @@ local function update_todos(gui_data)
|
||||
i = i + 1
|
||||
local child = children[i]
|
||||
if child then
|
||||
gui.update(
|
||||
child,
|
||||
gui.update(child, {
|
||||
{
|
||||
{
|
||||
elem_mods = {caption = todo.text, state = todo.completed},
|
||||
tags = {todo_id = id}
|
||||
},
|
||||
{},
|
||||
{tags = {todo_id = id}}
|
||||
}
|
||||
)
|
||||
elem_mods = { caption = todo.text, state = todo.completed },
|
||||
tags = { todo_id = id },
|
||||
},
|
||||
{},
|
||||
{ tags = { todo_id = id } },
|
||||
})
|
||||
else
|
||||
gui.add(
|
||||
todos_flow,
|
||||
{type = "flow", style_mods = {vertical_align = "center"},
|
||||
{
|
||||
type = "checkbox",
|
||||
caption = todo.text,
|
||||
state = todo.completed,
|
||||
actions = {
|
||||
on_click = "toggle_completed"
|
||||
},
|
||||
tags = {todo_id = id}
|
||||
gui.add(todos_flow, {
|
||||
type = "flow",
|
||||
style_mods = { vertical_align = "center" },
|
||||
{
|
||||
type = "checkbox",
|
||||
caption = todo.text,
|
||||
state = todo.completed,
|
||||
actions = {
|
||||
on_click = "toggle_completed",
|
||||
},
|
||||
{type = "empty-widget", style = "flib_horizontal_pusher"},
|
||||
{
|
||||
type = "sprite-button",
|
||||
style = "tool_button_red",
|
||||
sprite = "utility/trash",
|
||||
tooltip = "Delete",
|
||||
actions = {
|
||||
on_click = "delete_todo"
|
||||
},
|
||||
tags = {todo_id = id}
|
||||
}
|
||||
}
|
||||
)
|
||||
tags = { todo_id = id },
|
||||
},
|
||||
{ type = "empty-widget", style = "flib_horizontal_pusher" },
|
||||
{
|
||||
type = "sprite-button",
|
||||
style = "tool_button_red",
|
||||
sprite = "utility/trash",
|
||||
tooltip = "Delete",
|
||||
actions = {
|
||||
on_click = "delete_todo",
|
||||
},
|
||||
tags = { todo_id = id },
|
||||
},
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -118,7 +114,7 @@ local function update_todos(gui_data)
|
||||
refs.subfooter_frame.visible = false
|
||||
end
|
||||
|
||||
refs.subfooter_flow.items_left_label.caption = active_count.." items left"
|
||||
refs.subfooter_flow.items_left_label.caption = active_count .. " items left"
|
||||
refs.subfooter_flow.clear_completed_button.enabled = completed_count > 0
|
||||
end
|
||||
|
||||
@@ -127,61 +123,68 @@ function todo_gui.build(player, player_table)
|
||||
{
|
||||
type = "frame",
|
||||
direction = "vertical",
|
||||
ref = {"window"},
|
||||
ref = { "window" },
|
||||
actions = {
|
||||
on_closed = "close"
|
||||
on_closed = "close",
|
||||
},
|
||||
{type = "flow", ref = {"titlebar_flow"}, children = {
|
||||
{type ="label", style = "frame_title", caption = "TodoMVC", ignored_by_interaction = true},
|
||||
{type = "empty-widget", style = "flib_titlebar_drag_handle", ignored_by_interaction = true},
|
||||
{
|
||||
type = "sprite-button",
|
||||
style = "frame_action_button",
|
||||
sprite = "utility/close_white",
|
||||
hovered_sprite = "utility/close_black",
|
||||
clicked_sprite = "utility/close_black",
|
||||
mouse_button_filter = {"left"},
|
||||
actions = {
|
||||
on_click = "close"
|
||||
}
|
||||
}
|
||||
}},
|
||||
{type = "frame", style = "inside_shallow_frame", direction = "vertical",
|
||||
{
|
||||
type = "flow",
|
||||
ref = { "titlebar_flow" },
|
||||
children = {
|
||||
{ type = "label", style = "frame_title", caption = "TodoMVC", ignored_by_interaction = true },
|
||||
{ type = "empty-widget", style = "flib_titlebar_drag_handle", ignored_by_interaction = true },
|
||||
{
|
||||
type = "sprite-button",
|
||||
style = "frame_action_button",
|
||||
sprite = "utility/close_white",
|
||||
hovered_sprite = "utility/close_black",
|
||||
clicked_sprite = "utility/close_black",
|
||||
mouse_button_filter = { "left" },
|
||||
actions = {
|
||||
on_click = "close",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
type = "frame",
|
||||
style = "inside_shallow_frame",
|
||||
direction = "vertical",
|
||||
{
|
||||
type = "textfield",
|
||||
style_mods = {width = 500, margin = 12},
|
||||
ref = {"textfield"},
|
||||
style_mods = { width = 500, margin = 12 },
|
||||
ref = { "textfield" },
|
||||
actions = {
|
||||
on_confirmed = "add_todo"
|
||||
}
|
||||
on_confirmed = "add_todo",
|
||||
},
|
||||
},
|
||||
{
|
||||
type = "flow",
|
||||
style_mods = {left_margin = 12, right_margin = 12, bottom_margin = 12},
|
||||
style_mods = { left_margin = 12, right_margin = 12, bottom_margin = 12 },
|
||||
direction = "vertical",
|
||||
elem_mods = {visible = false},
|
||||
ref = {"todos_flow"}
|
||||
elem_mods = { visible = false },
|
||||
ref = { "todos_flow" },
|
||||
},
|
||||
{
|
||||
type = "frame",
|
||||
style = "subfooter_frame",
|
||||
elem_mods = {visible = false},
|
||||
ref = {"subfooter_frame"},
|
||||
elem_mods = { visible = false },
|
||||
ref = { "subfooter_frame" },
|
||||
{
|
||||
type = "flow",
|
||||
style_mods = {vertical_align = "center", left_margin = 8},
|
||||
ref = {"subfooter_flow"},
|
||||
{type = "label", name = "items_left_label", caption = "0 items left"},
|
||||
{type = "empty-widget", style = "flib_horizontal_pusher"},
|
||||
style_mods = { vertical_align = "center", left_margin = 8 },
|
||||
ref = { "subfooter_flow" },
|
||||
{ type = "label", name = "items_left_label", caption = "0 items left" },
|
||||
{ type = "empty-widget", style = "flib_horizontal_pusher" },
|
||||
{
|
||||
type = "radiobutton",
|
||||
name = "all_radiobutton",
|
||||
caption = "All",
|
||||
state = true,
|
||||
actions = {
|
||||
on_checked_state_changed = "change_mode"
|
||||
on_checked_state_changed = "change_mode",
|
||||
},
|
||||
tags = {mode = view_modes.all}
|
||||
tags = { mode = view_modes.all },
|
||||
},
|
||||
{
|
||||
type = "radiobutton",
|
||||
@@ -189,9 +192,9 @@ function todo_gui.build(player, player_table)
|
||||
caption = "Active",
|
||||
state = false,
|
||||
actions = {
|
||||
on_checked_state_changed = "change_mode"
|
||||
on_checked_state_changed = "change_mode",
|
||||
},
|
||||
tags = {mode = view_modes.active}
|
||||
tags = { mode = view_modes.active },
|
||||
},
|
||||
{
|
||||
type = "radiobutton",
|
||||
@@ -199,24 +202,24 @@ function todo_gui.build(player, player_table)
|
||||
caption = "Completed",
|
||||
state = false,
|
||||
actions = {
|
||||
on_checked_state_changed = "change_mode"
|
||||
on_checked_state_changed = "change_mode",
|
||||
},
|
||||
tags = {mode = view_modes.completed}
|
||||
tags = { mode = view_modes.completed },
|
||||
},
|
||||
{type = "empty-widget", style = "flib_horizontal_pusher"},
|
||||
{ type = "empty-widget", style = "flib_horizontal_pusher" },
|
||||
{
|
||||
type = "button",
|
||||
name = "clear_completed_button",
|
||||
caption = "Clear completed",
|
||||
elem_mods = {enabled = false},
|
||||
elem_mods = { enabled = false },
|
||||
actions = {
|
||||
on_click = "delete_completed_todos"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
on_click = "delete_completed_todos",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
refs.titlebar_flow.drag_target = refs.window
|
||||
@@ -229,8 +232,8 @@ function todo_gui.build(player, player_table)
|
||||
mode = view_modes.all,
|
||||
next_id = 1,
|
||||
todos = {},
|
||||
visible = false
|
||||
}
|
||||
visible = false,
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
@@ -265,7 +268,7 @@ local function add_todo(e)
|
||||
|
||||
state.todos[state.next_id] = {
|
||||
completed = false,
|
||||
text = todo_text
|
||||
text = todo_text,
|
||||
}
|
||||
|
||||
state.next_id = state.next_id + 1
|
||||
@@ -327,7 +330,7 @@ todo_gui.actions = {
|
||||
toggle_completed = toggle_todo_completed,
|
||||
delete_todo = delete_todo,
|
||||
delete_completed_todos = delete_completed_todos,
|
||||
change_mode = change_view_mode
|
||||
change_mode = change_view_mode,
|
||||
}
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------------------
|
||||
@@ -351,8 +354,8 @@ event.on_player_created(function(e)
|
||||
style = mod_gui.button_style,
|
||||
caption = "TodoMVC",
|
||||
actions = {
|
||||
on_click = "toggle_todo_gui"
|
||||
}
|
||||
on_click = "toggle_todo_gui",
|
||||
},
|
||||
})
|
||||
|
||||
todo_gui.build(player, player_table)
|
||||
|
@@ -13,7 +13,7 @@ local migrations = {
|
||||
end,
|
||||
["1.1.0"] = function()
|
||||
-- logic specific to changes made in 1.1.0
|
||||
end
|
||||
end,
|
||||
}
|
||||
|
||||
event.on_configuration_changed(function(e)
|
||||
@@ -24,4 +24,4 @@ event.on_configuration_changed(function(e)
|
||||
end
|
||||
-- if the result is false, that means that this mod was just added, so migrations shouldn't be performed
|
||||
-- if you have code that absolutely must run despite this, put it here
|
||||
end)
|
||||
end)
|
||||
|
@@ -83,7 +83,7 @@ flib_event.on_nth_tick = script.on_nth_tick
|
||||
-- event.register({"my-input", defines.events.on_lua_shortcut}, nil)
|
||||
function flib_event.register(ids, handler, filters)
|
||||
if type(ids) ~= "table" then
|
||||
ids = {ids}
|
||||
ids = { ids }
|
||||
end
|
||||
for i = 1, #ids do
|
||||
-- the game doesn't like you passing filters to events that don't support them, even if they're `nil`
|
||||
@@ -125,7 +125,7 @@ flib_event.generate_id = script.generate_event_name
|
||||
-- @function get_handler
|
||||
-- @tparam EventId id
|
||||
-- @treturn function The registered handler, or `nil` if one isn't registered.
|
||||
flib_event.get_handler = script.get_event_handler
|
||||
flib_event.get_handler = script.get_event_handler
|
||||
|
||||
-- TODO Nexela link EventData to https://lua-api.factorio.com/latest/events.html
|
||||
|
||||
@@ -161,7 +161,7 @@ flib_event.get_order = script.get_event_order
|
||||
-- event.set_filters(defines.events.on_robot_built_entity, nil)
|
||||
function flib_event.set_filters(ids, filters)
|
||||
if type(ids) ~= "table" then
|
||||
ids = {ids}
|
||||
ids = { ids }
|
||||
end
|
||||
for i = 1, #ids do
|
||||
script.set_event_filter(ids[i], filters)
|
||||
@@ -186,4 +186,4 @@ flib_event.get_filter = script.get_event_filter
|
||||
-- <li>A @{string} corresponding to a custom-input name.</li>
|
||||
-- </ul>
|
||||
|
||||
return flib_event
|
||||
return flib_event
|
||||
|
40
gui.lua
40
gui.lua
@@ -48,13 +48,19 @@ end
|
||||
-- end)
|
||||
function flib_gui.read_action(event_data)
|
||||
local elem = event_data.element
|
||||
if not elem or not elem.valid then return end
|
||||
if not elem or not elem.valid then
|
||||
return
|
||||
end
|
||||
|
||||
local mod_tags = elem.tags[mod_name]
|
||||
if not mod_tags then return end
|
||||
if not mod_tags then
|
||||
return
|
||||
end
|
||||
|
||||
local elem_actions = mod_tags.flib
|
||||
if not elem_actions then return end
|
||||
if not elem_actions then
|
||||
return
|
||||
end
|
||||
|
||||
local event_name = event_id_to_string_mapping[event_data.name]
|
||||
local msg = elem_actions[event_name]
|
||||
@@ -68,7 +74,9 @@ end
|
||||
local function recursive_build(parent, structure, refs)
|
||||
-- If the structure has no type, just ignore it
|
||||
-- This is to make it possible to pass unit types `{}` to represent "no element" without breaking things
|
||||
if not structure.type then return end
|
||||
if not structure.type then
|
||||
return
|
||||
end
|
||||
|
||||
-- Prepare tags
|
||||
local original_tags = structure.tags
|
||||
@@ -76,7 +84,7 @@ local function recursive_build(parent, structure, refs)
|
||||
local actions = structure.actions
|
||||
local tags_flib = tags.flib
|
||||
tags.flib = actions
|
||||
structure.tags = {[mod_name] = tags}
|
||||
structure.tags = { [mod_name] = tags }
|
||||
|
||||
-- Make the game not convert these into a property tree for no reason
|
||||
structure.actions = nil
|
||||
@@ -168,11 +176,7 @@ end
|
||||
function flib_gui.build(parent, structures)
|
||||
local refs = {}
|
||||
for i = 1, #structures do
|
||||
recursive_build(
|
||||
parent,
|
||||
structures[i],
|
||||
refs
|
||||
)
|
||||
recursive_build(parent, structures[i], refs)
|
||||
end
|
||||
return refs
|
||||
end
|
||||
@@ -189,14 +193,10 @@ function flib_gui.add(parent, structure)
|
||||
-- Just in case they had a ref in the structure already, extract it
|
||||
local previous_ref = structure.ref
|
||||
-- Put in a known ref that we can use later
|
||||
structure.ref = {"FLIB_ADD_ROOT"}
|
||||
structure.ref = { "FLIB_ADD_ROOT" }
|
||||
-- Build the element
|
||||
local refs = {}
|
||||
recursive_build(
|
||||
parent,
|
||||
structure,
|
||||
refs
|
||||
)
|
||||
recursive_build(parent, structure, refs)
|
||||
-- Restore the previous ref
|
||||
structure.ref = previous_ref
|
||||
-- Return the element
|
||||
@@ -377,10 +377,14 @@ function flib_gui.get_action(elem, event_name)
|
||||
local elem_tags = elem.tags
|
||||
local existing = elem_tags[mod_name]
|
||||
|
||||
if not existing then return end
|
||||
if not existing then
|
||||
return
|
||||
end
|
||||
|
||||
local actions = existing.flib
|
||||
if not actions then return end
|
||||
if not actions then
|
||||
return
|
||||
end
|
||||
|
||||
return actions[event_name]
|
||||
end
|
||||
|
6
math.lua
6
math.lua
@@ -81,7 +81,7 @@ end
|
||||
-- @tparam number num_decimals
|
||||
-- @treturn number
|
||||
function flib_math.round_to(num, num_decimals)
|
||||
local mult = 10^(num_decimals)
|
||||
local mult = 10 ^ num_decimals
|
||||
return flib_math.floor(num * mult + 0.5) / mult
|
||||
end
|
||||
|
||||
@@ -91,7 +91,7 @@ end
|
||||
-- @tparam number num_decimals
|
||||
-- @treturn number
|
||||
function flib_math.ceil_to(num, num_decimals)
|
||||
local mult = 10^(num_decimals)
|
||||
local mult = 10 ^ num_decimals
|
||||
return flib_math.ceil(num * mult) / mult
|
||||
end
|
||||
|
||||
@@ -101,7 +101,7 @@ end
|
||||
-- @tparam number num_decimals
|
||||
-- @treturn number
|
||||
function flib_math.floor_to(num, num_decimals)
|
||||
local mult = 10^(num_decimals)
|
||||
local mult = 10 ^ num_decimals
|
||||
return flib_math.floor(num * mult) / mult
|
||||
end
|
||||
|
||||
|
8
misc.lua
8
misc.lua
@@ -16,7 +16,7 @@ function flib_misc.get_distance(pos1, pos2)
|
||||
local y1 = pos1.y or pos1[2]
|
||||
local x2 = pos2.x or pos2[1]
|
||||
local y2 = pos2.y or pos2[2]
|
||||
return math.sqrt((x1-x2)^2 + (y1-y2)^2)
|
||||
return math.sqrt((x1 - x2) ^ 2 + (y1 - y2) ^ 2)
|
||||
end
|
||||
|
||||
--- Calculate the squared distance in tiles between two positions.
|
||||
@@ -28,7 +28,7 @@ function flib_misc.get_distance_squared(pos1, pos2)
|
||||
local y1 = pos1.y or pos1[2]
|
||||
local x2 = pos2.x or pos2[1]
|
||||
local y2 = pos2.y or pos2[2]
|
||||
return (x1-x2)^2 + (y1-y2)^2
|
||||
return (x1 - x2) ^ 2 + (y1 - y2) ^ 2
|
||||
end
|
||||
|
||||
--- Convert given tick or game.tick into "[hh:]mm:ss" format.
|
||||
@@ -76,12 +76,12 @@ function flib_misc.delineate_number(number, delimiter)
|
||||
local formatted = before
|
||||
local k
|
||||
while true do
|
||||
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", "%1"..delimiter.."%2")
|
||||
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", "%1" .. delimiter .. "%2")
|
||||
if k == 0 then
|
||||
break
|
||||
end
|
||||
end
|
||||
return formatted..after
|
||||
return formatted .. after
|
||||
end
|
||||
|
||||
return flib_misc
|
||||
|
@@ -20,7 +20,9 @@ end
|
||||
-- @treturn Tasks|nil The tasks to execute on this tick, or `nil`.
|
||||
function on_tick_n.retrieve(tick)
|
||||
-- Failsafe for rare cases where on_tick can fire before on_init
|
||||
if not global.__flib or not global.__flib.on_tick_n then return end
|
||||
if not global.__flib or not global.__flib.on_tick_n then
|
||||
return
|
||||
end
|
||||
local actions = global.__flib.on_tick_n[tick]
|
||||
if actions then
|
||||
global.__flib.on_tick_n[tick] = nil
|
||||
@@ -38,10 +40,10 @@ function on_tick_n.add(tick, task)
|
||||
if tick_list then
|
||||
local index = #tick_list + 1
|
||||
tick_list[index] = task
|
||||
return {index = index, tick = tick}
|
||||
return { index = index, tick = tick }
|
||||
else
|
||||
list[tick] = {task}
|
||||
return {index = 1, tick = tick}
|
||||
list[tick] = { task }
|
||||
return { index = 1, tick = tick }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -49,7 +51,9 @@ end
|
||||
-- @tparam TaskIdent ident The identifier object for the task, as returned from @{on-tick-n.add}.
|
||||
function on_tick_n.remove(ident)
|
||||
local tick_list = global.__flib.on_tick_n[ident.tick]
|
||||
if not tick_list or not tick_list[ident.index] then return false end
|
||||
if not tick_list or not tick_list[ident.index] then
|
||||
return false
|
||||
end
|
||||
|
||||
tick_list[ident.index] = nil
|
||||
|
||||
|
@@ -49,4 +49,4 @@ function flib_orientation.add(orientation1, orientation2)
|
||||
return (orientation1 + orientation2) % 1
|
||||
end
|
||||
|
||||
return flib_orientation
|
||||
return flib_orientation
|
||||
|
@@ -1,25 +1,53 @@
|
||||
-- INDICATOR SPRITES
|
||||
|
||||
local indicators = {}
|
||||
for i, color in ipairs{"black", "white", "red", "orange", "yellow", "green", "cyan", "blue", "purple", "pink"} do
|
||||
for i, color in ipairs({ "black", "white", "red", "orange", "yellow", "green", "cyan", "blue", "purple", "pink" }) do
|
||||
indicators[i] = {
|
||||
type = "sprite",
|
||||
name = "flib_indicator_"..color,
|
||||
name = "flib_indicator_" .. color,
|
||||
filename = "__flib__/graphics/indicators.png",
|
||||
y = (i - 1) * 32,
|
||||
size = 32,
|
||||
flags = {"icon"}
|
||||
flags = { "icon" },
|
||||
}
|
||||
end
|
||||
data:extend(indicators)
|
||||
|
||||
local fab = "__flib__/graphics/frame-action-icons.png"
|
||||
|
||||
data:extend{
|
||||
{type = "sprite", name = "flib_pin_black", filename = fab, position = {0, 0}, size = 32, flags = {"icon"}},
|
||||
{type = "sprite", name = "flib_pin_white", filename = fab, position = {32, 0}, size = 32, flags = {"icon"}},
|
||||
{type = "sprite", name = "flib_pin_disabled", filename = fab, position = {64, 0}, size = 32, flags = {"icon"}},
|
||||
{type = "sprite", name = "flib_settings_black", filename = fab, position = {0, 32}, size = 32, flags = {"icon"}},
|
||||
{type = "sprite", name = "flib_settings_white", filename = fab, position = {32, 32}, size = 32, flags = {"icon"}},
|
||||
{type = "sprite", name = "flib_settings_disabled", filename = fab, position = {64, 32}, size = 32, flags = {"icon"}},
|
||||
}
|
||||
data:extend({
|
||||
{ type = "sprite", name = "flib_pin_black", filename = fab, position = { 0, 0 }, size = 32, flags = { "icon" } },
|
||||
{ type = "sprite", name = "flib_pin_white", filename = fab, position = { 32, 0 }, size = 32, flags = { "icon" } },
|
||||
{
|
||||
type = "sprite",
|
||||
name = "flib_pin_disabled",
|
||||
filename = fab,
|
||||
position = { 64, 0 },
|
||||
size = 32,
|
||||
flags = { "icon" },
|
||||
},
|
||||
{
|
||||
type = "sprite",
|
||||
name = "flib_settings_black",
|
||||
filename = fab,
|
||||
position = { 0, 32 },
|
||||
size = 32,
|
||||
flags = { "icon" },
|
||||
},
|
||||
{
|
||||
type = "sprite",
|
||||
name = "flib_settings_white",
|
||||
filename = fab,
|
||||
position = { 32, 32 },
|
||||
size = 32,
|
||||
flags = { "icon" },
|
||||
},
|
||||
{
|
||||
type = "sprite",
|
||||
name = "flib_settings_disabled",
|
||||
filename = fab,
|
||||
position = { 64, 32 },
|
||||
size = 32,
|
||||
flags = { "icon" },
|
||||
},
|
||||
})
|
||||
|
@@ -13,17 +13,16 @@ local function gen_slot(x, y, default_offset)
|
||||
parent = "slot",
|
||||
size = 40,
|
||||
default_graphical_set = {
|
||||
base = {border = 4, position={x + default_offset , y}, size = 80, filename = slot_tileset},
|
||||
base = { border = 4, position = { x + default_offset, y }, size = 80, filename = slot_tileset },
|
||||
},
|
||||
hovered_graphical_set = {
|
||||
base = {border = 4, position={x + 80, y}, size = 80, filename = slot_tileset},
|
||||
base = { border = 4, position = { x + 80, y }, size = 80, filename = slot_tileset },
|
||||
},
|
||||
clicked_graphical_set = {
|
||||
},
|
||||
base = {border = 4, position={x + 160, y}, size = 80, filename = slot_tileset},
|
||||
clicked_graphical_set = {},
|
||||
base = { border = 4, position = { x + 160, y }, size = 80, filename = slot_tileset },
|
||||
disabled_graphical_set = { -- identical to default graphical set
|
||||
base = {border = 4, position={x + default_offset, y}, size = 80, filename = slot_tileset},
|
||||
}
|
||||
base = { border = 4, position = { x + default_offset, y }, size = 80, filename = slot_tileset },
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
@@ -34,22 +33,22 @@ local function gen_slot_button(x, y, default_offset, glow)
|
||||
parent = "slot_button",
|
||||
size = 40,
|
||||
default_graphical_set = {
|
||||
base = {border = 4, position={x + default_offset , y}, size = 80, filename = slot_tileset},
|
||||
base = { border = 4, position = { x + default_offset, y }, size = 80, filename = slot_tileset },
|
||||
shadow = offset_by_2_rounded_corners_glow(default_dirt_color),
|
||||
},
|
||||
hovered_graphical_set = {
|
||||
base = {border = 4, position={x + 80, y}, size = 80, filename = slot_tileset},
|
||||
base = { border = 4, position = { x + 80, y }, size = 80, filename = slot_tileset },
|
||||
shadow = offset_by_2_rounded_corners_glow(default_dirt_color),
|
||||
glow = offset_by_2_rounded_corners_glow(glow)
|
||||
glow = offset_by_2_rounded_corners_glow(glow),
|
||||
},
|
||||
clicked_graphical_set = {
|
||||
base = {border = 4, position={x + 160, y}, size = 80, filename = slot_tileset},
|
||||
base = { border = 4, position = { x + 160, y }, size = 80, filename = slot_tileset },
|
||||
shadow = offset_by_2_rounded_corners_glow(default_dirt_color),
|
||||
},
|
||||
disabled_graphical_set = { -- identical to default graphical set
|
||||
base = {border = 4, position={x + default_offset, y}, size = 80, filename = slot_tileset},
|
||||
base = { border = 4, position = { x + default_offset, y }, size = 80, filename = slot_tileset },
|
||||
shadow = offset_by_2_rounded_corners_glow(default_dirt_color),
|
||||
}
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
@@ -60,44 +59,44 @@ local function gen_standalone_slot_button(x, y, default_offset)
|
||||
parent = "slot_button",
|
||||
size = 40,
|
||||
default_graphical_set = {
|
||||
base = {border = 4, position={x + default_offset , y}, size = 80, filename = slot_tileset},
|
||||
shadow = offset_by_4_rounded_corners_subpanel_inset
|
||||
base = { border = 4, position = { x + default_offset, y }, size = 80, filename = slot_tileset },
|
||||
shadow = offset_by_4_rounded_corners_subpanel_inset,
|
||||
},
|
||||
hovered_graphical_set = {
|
||||
base = {border = 4, position={x + 80, y}, size = 80, filename = slot_tileset},
|
||||
shadow = offset_by_4_rounded_corners_subpanel_inset
|
||||
base = { border = 4, position = { x + 80, y }, size = 80, filename = slot_tileset },
|
||||
shadow = offset_by_4_rounded_corners_subpanel_inset,
|
||||
},
|
||||
clicked_graphical_set = {
|
||||
base = {border = 4, position={x + 160, y}, size = 80, filename = slot_tileset},
|
||||
shadow = offset_by_4_rounded_corners_subpanel_inset
|
||||
base = { border = 4, position = { x + 160, y }, size = 80, filename = slot_tileset },
|
||||
shadow = offset_by_4_rounded_corners_subpanel_inset,
|
||||
},
|
||||
disabled_graphical_set = { -- identical to default graphical set
|
||||
base = {border = 4, position={x + default_offset, y}, size = 80, filename = slot_tileset},
|
||||
shadow = offset_by_4_rounded_corners_subpanel_inset
|
||||
}
|
||||
base = { border = 4, position = { x + default_offset, y }, size = 80, filename = slot_tileset },
|
||||
shadow = offset_by_4_rounded_corners_subpanel_inset,
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
local slot_data = {
|
||||
{name="default", y=0, glow=default_glow_color},
|
||||
{name="grey", y=80, glow=default_glow_color},
|
||||
{name="red", y=160, glow={230, 135, 135}},
|
||||
{name="orange", y=240, glow={216, 169, 122}},
|
||||
{name="yellow", y=320, glow={230, 218, 135}},
|
||||
{name="green", y=400, glow={153, 230, 135}},
|
||||
{name="cyan", y=480, glow={135, 230, 230}},
|
||||
{name="blue", y=560, glow={135, 186, 230}},
|
||||
{name="purple", y=640, glow={188, 135, 230}},
|
||||
{name="pink", y=720, glow={230, 135, 230}}
|
||||
{ name = "default", y = 0, glow = default_glow_color },
|
||||
{ name = "grey", y = 80, glow = default_glow_color },
|
||||
{ name = "red", y = 160, glow = { 230, 135, 135 } },
|
||||
{ name = "orange", y = 240, glow = { 216, 169, 122 } },
|
||||
{ name = "yellow", y = 320, glow = { 230, 218, 135 } },
|
||||
{ name = "green", y = 400, glow = { 153, 230, 135 } },
|
||||
{ name = "cyan", y = 480, glow = { 135, 230, 230 } },
|
||||
{ name = "blue", y = 560, glow = { 135, 186, 230 } },
|
||||
{ name = "purple", y = 640, glow = { 188, 135, 230 } },
|
||||
{ name = "pink", y = 720, glow = { 230, 135, 230 } },
|
||||
}
|
||||
|
||||
for _, data in pairs(slot_data) do
|
||||
styles["flib_slot_"..data.name] = gen_slot(0, data.y)
|
||||
styles["flib_selected_slot_"..data.name] = gen_slot(0, data.y, 80)
|
||||
styles["flib_slot_button_"..data.name] = gen_slot_button(240, data.y, 0, data.glow)
|
||||
styles["flib_selected_slot_button_"..data.name] = gen_slot_button(240, data.y, 80, data.glow)
|
||||
styles["flib_standalone_slot_button_"..data.name] = gen_standalone_slot_button(240, data.y)
|
||||
styles["flib_selected_standalone_slot_button_"..data.name] = gen_standalone_slot_button(240, data.y, 80)
|
||||
styles["flib_slot_" .. data.name] = gen_slot(0, data.y)
|
||||
styles["flib_selected_slot_" .. data.name] = gen_slot(0, data.y, 80)
|
||||
styles["flib_slot_button_" .. data.name] = gen_slot_button(240, data.y, 0, data.glow)
|
||||
styles["flib_selected_slot_button_" .. data.name] = gen_slot_button(240, data.y, 80, data.glow)
|
||||
styles["flib_standalone_slot_button_" .. data.name] = gen_standalone_slot_button(240, data.y)
|
||||
styles["flib_selected_standalone_slot_button_" .. data.name] = gen_standalone_slot_button(240, data.y, 80)
|
||||
end
|
||||
|
||||
-- BUTTON STYLES
|
||||
@@ -107,19 +106,19 @@ styles.flib_selected_frame_action_button = {
|
||||
parent = "frame_action_button",
|
||||
default_font_color = button_hovered_font_color,
|
||||
default_graphical_set = {
|
||||
base = {position = {225, 17}, corner_size = 8},
|
||||
shadow = {position = {440, 24}, corner_size = 8, draw_type = "outer"},
|
||||
base = { position = { 225, 17 }, corner_size = 8 },
|
||||
shadow = { position = { 440, 24 }, corner_size = 8, draw_type = "outer" },
|
||||
},
|
||||
hovered_font_color = button_hovered_font_color,
|
||||
hovered_graphical_set = {
|
||||
base = {position = {369, 17}, corner_size = 8},
|
||||
shadow = {position = {440, 24}, corner_size = 8, draw_type = "outer"},
|
||||
base = { position = { 369, 17 }, corner_size = 8 },
|
||||
shadow = { position = { 440, 24 }, corner_size = 8, draw_type = "outer" },
|
||||
},
|
||||
clicked_font_color = button_hovered_font_color,
|
||||
clicked_graphical_set = {
|
||||
base = {position = {352, 17}, corner_size = 8},
|
||||
shadow = {position = {440, 24}, corner_size = 8, draw_type = "outer"},
|
||||
}
|
||||
base = { position = { 352, 17 }, corner_size = 8 },
|
||||
shadow = { position = { 440, 24 }, corner_size = 8, draw_type = "outer" },
|
||||
},
|
||||
}
|
||||
|
||||
local btn = styles.button
|
||||
@@ -132,32 +131,32 @@ styles.flib_selected_tool_button = {
|
||||
hovered_font_color = btn.selected_hovered_font_color,
|
||||
hovered_graphical_set = btn.selected_hovered_graphical_set,
|
||||
clicked_font_color = btn.selected_clicked_font_color,
|
||||
clicked_graphical_set = btn.selected_clicked_graphical_set
|
||||
clicked_graphical_set = btn.selected_clicked_graphical_set,
|
||||
}
|
||||
|
||||
styles.flib_tool_button_light_green = {
|
||||
type = "button_style",
|
||||
parent = "item_and_count_select_confirm",
|
||||
padding = 2,
|
||||
top_margin = 0
|
||||
top_margin = 0,
|
||||
}
|
||||
|
||||
styles.flib_tool_button_dark_red = {
|
||||
type = "button_style",
|
||||
parent = "tool_button",
|
||||
default_graphical_set = {
|
||||
base = {filename = data_util.dark_red_button_tileset, position = {0, 0}, corner_size = 8},
|
||||
shadow = default_dirt
|
||||
base = { filename = data_util.dark_red_button_tileset, position = { 0, 0 }, corner_size = 8 },
|
||||
shadow = default_dirt,
|
||||
},
|
||||
hovered_graphical_set = {
|
||||
base = {filename = data_util.dark_red_button_tileset, position = {17, 0}, corner_size = 8},
|
||||
base = { filename = data_util.dark_red_button_tileset, position = { 17, 0 }, corner_size = 8 },
|
||||
shadow = default_dirt,
|
||||
glow = default_glow({236, 130, 130, 127}, 0.5)
|
||||
glow = default_glow({ 236, 130, 130, 127 }, 0.5),
|
||||
},
|
||||
clicked_graphical_set = {
|
||||
base = {filename = data_util.dark_red_button_tileset, position = {34, 0}, corner_size = 8},
|
||||
shadow = default_dirt
|
||||
}
|
||||
base = { filename = data_util.dark_red_button_tileset, position = { 34, 0 }, corner_size = 8 },
|
||||
shadow = default_dirt,
|
||||
},
|
||||
}
|
||||
|
||||
-- EMPTY-WIDGET STYLES
|
||||
@@ -166,24 +165,24 @@ styles.flib_dialog_footer_drag_handle = {
|
||||
type = "empty_widget_style",
|
||||
parent = "draggable_space",
|
||||
height = 32,
|
||||
horizontally_stretchable = "on"
|
||||
horizontally_stretchable = "on",
|
||||
}
|
||||
|
||||
styles.flib_dialog_footer_drag_handle_no_right = {
|
||||
type = "empty_widget_style",
|
||||
parent = "flib_dialog_footer_drag_handle",
|
||||
right_margin = 0
|
||||
right_margin = 0,
|
||||
}
|
||||
|
||||
styles.flib_dialog_titlebar_drag_handle = {
|
||||
type = "empty_widget_style",
|
||||
parent = "flib_titlebar_drag_handle",
|
||||
right_margin = 0
|
||||
right_margin = 0,
|
||||
}
|
||||
|
||||
styles.flib_horizontal_pusher = {
|
||||
type = "empty_widget_style",
|
||||
horizontally_stretchable = "on"
|
||||
horizontally_stretchable = "on",
|
||||
}
|
||||
|
||||
styles.flib_titlebar_drag_handle = {
|
||||
@@ -192,24 +191,24 @@ styles.flib_titlebar_drag_handle = {
|
||||
left_margin = 4,
|
||||
right_margin = 4,
|
||||
height = 24,
|
||||
horizontally_stretchable = "on"
|
||||
horizontally_stretchable = "on",
|
||||
}
|
||||
|
||||
styles.flib_vertical_pusher = {
|
||||
type = "empty_widget_style",
|
||||
vertically_stretchable = "on"
|
||||
vertically_stretchable = "on",
|
||||
}
|
||||
|
||||
-- FLOW STYLES
|
||||
|
||||
styles.flib_indicator_flow = {
|
||||
type = "horizontal_flow_style",
|
||||
vertical_align = "center"
|
||||
vertical_align = "center",
|
||||
}
|
||||
|
||||
styles.flib_titlebar_flow = {
|
||||
type = "horizontal_flow_style",
|
||||
horizontal_spacing = 8
|
||||
horizontal_spacing = 8,
|
||||
}
|
||||
|
||||
-- FRAME STYLES
|
||||
@@ -220,16 +219,17 @@ styles.flib_shallow_frame_in_shallow_frame = {
|
||||
padding = 0,
|
||||
graphical_set = {
|
||||
base = {
|
||||
position = {85, 0}, corner_size = 8,
|
||||
center = {position = {76, 8}, size = {1, 1}},
|
||||
draw_type = "outer"
|
||||
position = { 85, 0 },
|
||||
corner_size = 8,
|
||||
center = { position = { 76, 8 }, size = { 1, 1 } },
|
||||
draw_type = "outer",
|
||||
},
|
||||
shadow = default_inner_shadow
|
||||
shadow = default_inner_shadow,
|
||||
},
|
||||
vertical_flow_style = {
|
||||
type = "vertical_flow_style",
|
||||
vertical_spacing = 0
|
||||
}
|
||||
vertical_spacing = 0,
|
||||
},
|
||||
}
|
||||
|
||||
-- IMAGE STYLES
|
||||
@@ -237,7 +237,7 @@ styles.flib_shallow_frame_in_shallow_frame = {
|
||||
styles.flib_indicator = {
|
||||
type = "image_style",
|
||||
size = 16,
|
||||
stretch_image_to_widget_size = true
|
||||
stretch_image_to_widget_size = true,
|
||||
}
|
||||
|
||||
-- SCROLL-PANE STYLES
|
||||
@@ -247,8 +247,8 @@ styles.flib_naked_scroll_pane = {
|
||||
extra_padding_when_activated = 0,
|
||||
padding = 12,
|
||||
graphical_set = {
|
||||
shadow = default_inner_shadow
|
||||
}
|
||||
shadow = default_inner_shadow,
|
||||
},
|
||||
}
|
||||
|
||||
styles.flib_naked_scroll_pane_under_tabs = {
|
||||
@@ -256,24 +256,24 @@ styles.flib_naked_scroll_pane_under_tabs = {
|
||||
parent = "flib_naked_scroll_pane",
|
||||
graphical_set = {
|
||||
base = {
|
||||
top = {position = {93, 0}, size = {1, 8}},
|
||||
draw_type = "outer"
|
||||
top = { position = { 93, 0 }, size = { 1, 8 } },
|
||||
draw_type = "outer",
|
||||
},
|
||||
shadow = default_inner_shadow
|
||||
}
|
||||
shadow = default_inner_shadow,
|
||||
},
|
||||
}
|
||||
|
||||
styles.flib_naked_scroll_pane_no_padding = {
|
||||
type = "scroll_pane_style",
|
||||
parent = "flib_naked_scroll_pane",
|
||||
padding = 0
|
||||
padding = 0,
|
||||
}
|
||||
|
||||
styles.flib_shallow_scroll_pane = {
|
||||
type = "scroll_pane_style",
|
||||
padding = 0,
|
||||
graphical_set = {
|
||||
base = {position = {85, 0}, corner_size = 8, draw_type = "outer"},
|
||||
base = { position = { 85, 0 }, corner_size = 8, draw_type = "outer" },
|
||||
shadow = default_inner_shadow,
|
||||
},
|
||||
}
|
||||
@@ -291,8 +291,8 @@ styles.flib_tabbed_pane_with_no_padding = {
|
||||
graphical_set = {
|
||||
base = {
|
||||
-- Same as tabbed_pane_graphical_set - but without bottom
|
||||
top = {position = {76, 0}, size = {1, 8}},
|
||||
center = {position = {76, 8}, size = {1, 1}}
|
||||
top = { position = { 76, 0 }, size = { 1, 8 } },
|
||||
center = { position = { 76, 8 }, size = { 1, 1 } },
|
||||
},
|
||||
shadow = top_shadow,
|
||||
},
|
||||
@@ -303,11 +303,11 @@ styles.flib_tabbed_pane_with_no_padding = {
|
||||
|
||||
styles.flib_widthless_textfield = {
|
||||
type = "textbox_style",
|
||||
width = 0
|
||||
width = 0,
|
||||
}
|
||||
|
||||
styles.flib_widthless_invalid_textfield = {
|
||||
type = "textbox_style",
|
||||
parent = "invalid_value_textfield",
|
||||
width = 0
|
||||
width = 0,
|
||||
}
|
||||
|
@@ -20,7 +20,7 @@ end
|
||||
-- MyQueue:push_right("My string")
|
||||
-- local len = MyQueue:length() -- 1
|
||||
function flib_queue.load(tbl)
|
||||
return setmetatable(tbl, {__index = flib_queue})
|
||||
return setmetatable(tbl, { __index = flib_queue })
|
||||
end
|
||||
|
||||
--- Push an element onto the beginning of the queue.
|
||||
|
@@ -23,4 +23,4 @@ end
|
||||
|
||||
build_reverse_defines(flib_reverse_defines, defines)
|
||||
|
||||
return flib_reverse_defines
|
||||
return flib_reverse_defines
|
||||
|
@@ -1,4 +1,4 @@
|
||||
data:extend{
|
||||
data:extend({
|
||||
{
|
||||
type = "int-setting",
|
||||
name = "flib-dictionary-levels-per-batch",
|
||||
@@ -15,4 +15,4 @@ data:extend{
|
||||
minimum_value = 1,
|
||||
hidden = true,
|
||||
},
|
||||
}
|
||||
})
|
||||
|
2
stylua.toml
Normal file
2
stylua.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
indent_type = "Spaces"
|
||||
indent_width = 2
|
32
table.lua
32
table.lua
@@ -50,16 +50,24 @@ end
|
||||
-- @tparam table tbl2
|
||||
-- @treturn boolean If the tables are the same.
|
||||
function flib_table.deep_compare(tbl1, tbl2)
|
||||
if tbl1 == tbl2 then return true end
|
||||
if tbl1 == tbl2 then
|
||||
return true
|
||||
end
|
||||
for k, v in pairs(tbl1) do
|
||||
if type(v) == "table" and type(tbl2[k]) == "table" then
|
||||
if not flib_table.deep_compare( v, tbl2[k] ) then return false end
|
||||
if not flib_table.deep_compare(v, tbl2[k]) then
|
||||
return false
|
||||
end
|
||||
else
|
||||
if v ~= tbl2[k] then return false end
|
||||
if v ~= tbl2[k] then
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
for k in pairs(tbl2) do
|
||||
if tbl1[k] == nil then return false end
|
||||
if tbl1[k] == nil then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
@@ -74,7 +82,7 @@ function flib_table.deep_copy(tbl)
|
||||
local function _copy(object)
|
||||
if type(object) ~= "table" then
|
||||
return object
|
||||
-- don't copy factorio rich objects
|
||||
-- don't copy factorio rich objects
|
||||
elseif object.__self then
|
||||
return object
|
||||
elseif lookup_table[object] then
|
||||
@@ -113,7 +121,7 @@ function flib_table.deep_merge(tables)
|
||||
for k, v in pairs(tbl) do
|
||||
if type(v) == "table" then
|
||||
if type(output[k] or false) == "table" then
|
||||
output[k] = flib_table.deep_merge{output[k], v}
|
||||
output[k] = flib_table.deep_merge({ output[k], v })
|
||||
else
|
||||
output[k] = flib_table.deep_copy(v)
|
||||
end
|
||||
@@ -234,7 +242,9 @@ function flib_table.for_n_of(tbl, from_k, n, callback, _next)
|
||||
if delete then
|
||||
delete = from_k
|
||||
end
|
||||
if abort then break end
|
||||
if abort then
|
||||
break
|
||||
end
|
||||
else
|
||||
return from_k, result, true
|
||||
end
|
||||
@@ -325,7 +335,9 @@ function flib_table.map(tbl, mapper)
|
||||
return output
|
||||
end
|
||||
|
||||
local function default_comp(a, b) return a < b end
|
||||
local function default_comp(a, b)
|
||||
return a < b
|
||||
end
|
||||
--- Partially sort an array.
|
||||
--
|
||||
-- This function utilizes [insertion sort](https://en.wikipedia.org/wiki/Insertion_sort), which is _extremely_
|
||||
@@ -345,7 +357,9 @@ function flib_table.partial_sort(arr, from_index, iterations, comp)
|
||||
|
||||
for j = start_index, end_index do
|
||||
local key = arr[j]
|
||||
if not key then return nil end
|
||||
if not key then
|
||||
return nil
|
||||
end
|
||||
local i = j - 1
|
||||
|
||||
while i > 0 and comp(key, arr[i]) do
|
||||
|
21
train.lua
21
train.lua
@@ -45,7 +45,7 @@ function flib_train.rotate_carriage(entity)
|
||||
reconnected_back = entity.connect_rolling_stock(defines.rail_direction.front)
|
||||
end
|
||||
if disconnected_front then
|
||||
reconnected_front= entity.connect_rolling_stock(defines.rail_direction.back)
|
||||
reconnected_front = entity.connect_rolling_stock(defines.rail_direction.back)
|
||||
end
|
||||
|
||||
if disconnected_front and not reconnected_front then
|
||||
@@ -68,11 +68,11 @@ function flib_train.get_composition_string(train)
|
||||
local string_table = {}
|
||||
local count_wagons, count_loco_front, count_loco_back, i = 0, 0, 0, 0
|
||||
local locos_front = train.locomotives.front_movers
|
||||
for _,carriage in pairs(carriages) do
|
||||
for _, carriage in pairs(carriages) do
|
||||
i = i + 1
|
||||
if carriage.type == "locomotive" then
|
||||
local faces_forward = false
|
||||
for _,loco in pairs(locos_front) do
|
||||
for _, loco in pairs(locos_front) do
|
||||
if carriage.unit_number == loco.unit_number then
|
||||
faces_forward = true
|
||||
break
|
||||
@@ -99,12 +99,13 @@ function flib_train.get_composition_string(train)
|
||||
string_table[i] = "?"
|
||||
end
|
||||
end
|
||||
return table.concat(string_table), {
|
||||
total = i,
|
||||
wagons = count_wagons,
|
||||
front_movers = count_loco_front,
|
||||
back_movers = count_loco_back
|
||||
}
|
||||
return table.concat(string_table),
|
||||
{
|
||||
total = i,
|
||||
wagons = count_wagons,
|
||||
front_movers = count_loco_front,
|
||||
back_movers = count_loco_back,
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -133,4 +134,4 @@ return flib_train
|
||||
-- @tfield number total The total number of rolling stocks in the train.
|
||||
-- @tfield number wagons The number of wagons in the train.
|
||||
-- @tfield number front_movers The number of front-facing locomotives in the train.
|
||||
-- @tfield number back_movers The number of back-facing locomotives in the train.
|
||||
-- @tfield number back_movers The number of back-facing locomotives in the train.
|
||||
|
@@ -17,16 +17,20 @@ function flib_translation.init()
|
||||
end
|
||||
global.__flib.translation = {
|
||||
players = {},
|
||||
translating_players_count = 0
|
||||
translating_players_count = 0,
|
||||
}
|
||||
end
|
||||
|
||||
function flib_translation.iterate_batch(event_data)
|
||||
local __translation = global.__flib.translation
|
||||
if __translation.translating_players_count == 0 then return end
|
||||
if __translation.translating_players_count == 0 then
|
||||
return
|
||||
end
|
||||
local translations_per_tick = settings.global["flib-translations-per-tick"].value
|
||||
local iterations = math.ceil(translations_per_tick / __translation.translating_players_count)
|
||||
if iterations < 1 then iterations = 1 end
|
||||
if iterations < 1 then
|
||||
iterations = 1
|
||||
end
|
||||
local current_tick = event_data.tick
|
||||
|
||||
for player_index, player_table in pairs(__translation.players) do
|
||||
@@ -49,14 +53,14 @@ function flib_translation.iterate_batch(event_data)
|
||||
if translation_data then
|
||||
local dictionary_names = translation_data.names[string_data.dictionary]
|
||||
if dictionary_names then
|
||||
dictionary_names[#dictionary_names+1] = string_data.internal
|
||||
dictionary_names[#dictionary_names + 1] = string_data.internal
|
||||
else
|
||||
translation_data.names[string_data.dictionary] = {string_data.internal}
|
||||
translation_data.names[string_data.dictionary] = { string_data.internal }
|
||||
end
|
||||
else
|
||||
translate_strings[serialised] = {
|
||||
string = string_data.localised,
|
||||
names = {[string_data.dictionary]={string_data.internal}}
|
||||
names = { [string_data.dictionary] = { string_data.internal } },
|
||||
}
|
||||
translate_strings.__size = translate_strings.__size + 1
|
||||
end
|
||||
@@ -101,10 +105,16 @@ end
|
||||
|
||||
function flib_translation.process_result(event_data)
|
||||
local __translation = global.__flib.translation
|
||||
if __translation.translating_players_count == 0 then return end
|
||||
if __translation.translating_players_count == 0 then
|
||||
return
|
||||
end
|
||||
local player_table = __translation.players[event_data.player_index]
|
||||
if not player_table then return end
|
||||
if player_table.state == "sort" then return end
|
||||
if not player_table then
|
||||
return
|
||||
end
|
||||
if player_table.state == "sort" then
|
||||
return
|
||||
end
|
||||
|
||||
local serialised = flib_translation.serialise_localised_string(event_data.localised_string)
|
||||
local translate_strings = player_table.translate.strings
|
||||
@@ -132,13 +142,13 @@ function flib_translation.add_requests(player_index, strings)
|
||||
if player_table.sort then
|
||||
local strings_to_sort = player_table.sort.strings
|
||||
for i = 1, #strings do
|
||||
strings_to_sort[#strings_to_sort+1] = strings[i]
|
||||
strings_to_sort[#strings_to_sort + 1] = strings[i]
|
||||
end
|
||||
player_table.sort.next_index = 1
|
||||
else
|
||||
player_table.sort = {
|
||||
strings = table.shallow_copy(strings),
|
||||
next_index = 1
|
||||
next_index = 1,
|
||||
}
|
||||
end
|
||||
player_table.translate.next_key = nil
|
||||
@@ -148,15 +158,15 @@ function flib_translation.add_requests(player_index, strings)
|
||||
-- sort
|
||||
sort = {
|
||||
strings = table.shallow_copy(strings),
|
||||
next_index = 1
|
||||
next_index = 1,
|
||||
},
|
||||
-- translate
|
||||
translate = {
|
||||
strings = {__size = 0},
|
||||
next_key = nil
|
||||
strings = { __size = 0 },
|
||||
next_key = nil,
|
||||
},
|
||||
-- wait
|
||||
wait_tick = nil
|
||||
wait_tick = nil,
|
||||
}
|
||||
__translation.translating_players_count = __translation.translating_players_count + 1
|
||||
end
|
||||
@@ -166,7 +176,7 @@ function flib_translation.cancel(player_index)
|
||||
local __translation = global.__flib.translation
|
||||
local player_table = __translation.players[player_index]
|
||||
if not player_table then
|
||||
log("Tried to cancel translations for player ["..player_index.."] when no translations were running!")
|
||||
log("Tried to cancel translations for player [" .. player_index .. "] when no translations were running!")
|
||||
return
|
||||
end
|
||||
__translation.players[player_index] = nil
|
||||
@@ -182,21 +192,23 @@ function flib_translation.translating_players_count()
|
||||
end
|
||||
|
||||
function flib_translation.serialise_localised_string(localised_string)
|
||||
if type(localised_string) == "string" then return localised_string end
|
||||
if type(localised_string) == "string" then
|
||||
return localised_string
|
||||
end
|
||||
local output = "{"
|
||||
local first = true
|
||||
for _, v in pairs(localised_string) do
|
||||
if not first then
|
||||
output = output..","
|
||||
output = output .. ","
|
||||
end
|
||||
if type(v) == "table" then
|
||||
output = output..flib_translation.serialise_localised_string(v)
|
||||
output = output .. flib_translation.serialise_localised_string(v)
|
||||
else
|
||||
output = output.."\""..tostring(v).."\""
|
||||
output = output .. '"' .. tostring(v) .. '"'
|
||||
end
|
||||
first = false
|
||||
end
|
||||
output = output.."}"
|
||||
output = output .. "}"
|
||||
return output
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user