Paste
Pasted as Lua by ownly ( 1 year ago )
local ____lualib = require("lualib_bundle")
local __TS__ArrayIndexOf = ____lualib.__TS__ArrayIndexOf
local __TS__ParseInt = ____lualib.__TS__ParseInt
local __TS__ArraySplice = ____lualib.__TS__ArraySplice
local makeButton, updateCountButton, tick, tickPlayer, getPlayerInfo
local updateOffset
function updateOffset(playerIndex)
local player = game.players[playerIndex]
local frame = player.gui.screen.ModulesUI
if not frame then return end
local playerInfo = getPlayerInfo(playerIndex)
local offset = 0
if frame.optionsContainer.visible then
offset = offset - 56
end
local countButtons = 0
for _,button in pairs(frame.buttonsTable.children) do
if button.visible then
countButtons = countButtons + 1
end
end
offset = offset - math.ceil(countButtons/5)*40
offset = offset * player.display_scale
if frame.location.y >= player.display_resolution.height/2 then
frame.location = {x = frame.location.x, y = frame.location.y - (playerInfo.oldOffset or 0) + offset}
end
playerInfo.oldOffset = offset
end
function makeButton(parent, item, quality)
local t = parent.add({type = "empty-widget", style = "ModulesUISlot"})
t.add({
type = "sprite-button",
sprite = "item/" .. item.name,
name = (("ModulesUI_" .. item.name) .. "_") .. (quality or "common"),
tooltip = "0",
elem_tooltip = {type = "item-with-quality", name = item.name, quality = quality},
tags = {item = item.name, quality = quality, tier = item.tier}
})
if quality then
local f = t.add({type = "flow", ignored_by_interaction = true, style = "ModulesUISlot_QualityFlow"})
local f2 = f.add({type = "sprite", sprite = "quality/" .. quality, ignored_by_interaction = true, style = "ModulesUISlot_QualityFlow_Indicator"})
end
end
function updateCountButton(logistics, platform, button, hideBelowCount, hiddenTiers)
local tags = button.tags
if __TS__ArrayIndexOf(hiddenTiers, tags.tier) >= 0 then
button.parent.visible = false
return
end
local count = logistics and logistics.get_item_count({name = tags.item, quality = tags.quality}) or platform.hub.get_inventory(defines.inventory.hub_main).get_item_count({name = tags.item, quality = tags.quality})
if count >= hideBelowCount then
local spriteButton = button
spriteButton.number = count
spriteButton.tooltip = "Available: " .. tostring(count)
button.parent.visible = true
else
button.parent.visible = false
end
end
function tick()
for k, player in pairs(game.players) do
tickPlayer(player)
end
end
function tickPlayer(player)
if player.gui.screen.ModulesUI then
local playerInfo = getPlayerInfo(player.index)
local ____table = player.gui.screen.ModulesUI.buttonsTable
local logistics = player.force.find_logistic_network_by_position(player.position, player.surface)
local platform = player.surface.platform
if logistics or platform then
for _, v in pairs(____table.children) do
local element = v
updateCountButton(
logistics,
platform,
element.children[1],
playerInfo.HideBelowCount,
playerInfo.HiddenTiers or ({})
)
end
else
for _, v in pairs(____table.children) do
local element = v
local tags = element.children[1].tags
if tags.item then
local spriteButton = element.children[1]
spriteButton.number = 0
element.visible = false
end
end
end
updateOffset(player.index)
end
end
function getPlayerInfo(playerIndex)
if storage.Players == nil then
storage.Players = {}
end
if storage.Players[playerIndex] == nil then
storage.Players[playerIndex] = {Position = {x = 200, y = 200}, HideBelowCount = 1}
end
return storage.Players[playerIndex]
end
local tiers = {}
local items = {}
for _, v in pairs(prototypes.item) do
if v.type == "module" then
items[#items + 1] = {name = v.name, category = v.category, tier = v.tier}
if __TS__ArrayIndexOf(tiers, v.tier) == -1 then
tiers[#tiers + 1] = v.tier
end
end
end
local qualities = {}
for _, v in pairs(prototypes.quality) do
if v.name ~= "quality-unknown" then
qualities[#qualities + 1] = v.name
end
end
script.on_configuration_changed(function(ev)
for _, player in pairs(game.players) do
if player.gui.screen.ModulesUI then
player.gui.screen.ModulesUI.destroy()
local playerInfo = getPlayerInfo(player.index)
playerInfo.oldOffset = 0
end
end
end)
script.on_event(
defines.events.on_lua_shortcut,
function(data)
if data.prototype_name ~= "ModulesUI-shortcut" then
return
end
local player = game.get_player(data.player_index)
local playerInfo = getPlayerInfo(data.player_index)
local screenElement = player.gui.screen
if screenElement.ModulesUI then
screenElement.ModulesUI.destroy()
local playerInfo = getPlayerInfo(player.index)
playerInfo.oldOffset = 0
else
local mainFrame = screenElement.add({type = "frame", name = "ModulesUI", style = "ModulesUIFrame", direction = "vertical"})
mainFrame.location = playerInfo.Position
local header = mainFrame.add({type = "flow"})
header.add({type = "label", caption = "Modules"})
local drag = header.add({type = "empty-widget", style = "ModulesUIHeader"})
drag.drag_target = mainFrame
local filterButton = header.add({
type = "sprite-button",
auto_toggle = true,
name = "ShowFilter",
style = "frame_action_button",
sprite = "utility/expand_dots"
})
filterButton.style.size = 16
local closeButton = header.add({type = "sprite-button", name = "CloseWindow", style = "frame_action_button", sprite = "utility/close"})
closeButton.style.size = 16
local optionsContainer = mainFrame.add({type = "flow", direction = "vertical", name = "optionsContainer"})
optionsContainer.visible = false
local hideBelowContainer = optionsContainer.add({type = "flow", direction = "horizontal", name = "hideBelowContainer"})
hideBelowContainer.add({type = "label", caption = "Minimum quantity"})
local p = hideBelowContainer.add({
type = "textfield",
numeric = true,
text = tostring(playerInfo.HideBelowCount),
name = "hideBelowCount",
caption = "Hide below"
})
p.style.width = 36
local tierSelectionContainer = optionsContainer.add({type = "flow", direction = "horizontal", name = "tierSelectionContainer"})
if #tiers > 0 then
tierSelectionContainer.add({type = "label", caption = "Show tiers"})
for ____, tier in ipairs(tiers) do
tierSelectionContainer.add({
type = "checkbox",
state = __TS__ArrayIndexOf(playerInfo.HiddenTiers or ({}), tier) == -1,
caption = tostring(tier),
tags = {tier = tier}
})
end
end
local ____table = mainFrame.add({type = "table", name = "buttonsTable", column_count = 5, style = "filter_slot_table"})
for ____, item in ipairs(items) do
for ____, quality in ipairs(qualities) do
makeButton(____table, item, quality)
end
end
tick()
end
end
)
script.on_nth_tick(
60,
function(data)
tick()
end
)
script.on_event(
defines.events.on_player_changed_position,
function(ev)
local player = game.get_player(ev.player_index)
tickPlayer(player)
end
)
script.on_event(
defines.events.on_player_changed_surface,
function(ev)
local player = game.get_player(ev.player_index)
tickPlayer(player)
end
)
script.on_event(
defines.events.on_gui_text_changed,
function(ev)
if ev.element.get_mod() ~= "ModulesUI" or ev.element.name ~= "hideBelowCount" then
return
end
local playerInfo = getPlayerInfo(ev.player_index)
playerInfo.HideBelowCount = __TS__ParseInt(ev.element.text)
local player = game.get_player(ev.player_index)
tickPlayer(player)
end
)
script.on_event(
defines.events.on_gui_location_changed,
function(ev)
if ev.element.get_mod() ~= "ModulesUI" then
return
end
local playerInfo = getPlayerInfo(ev.player_index)
if ev.element.location.y >= game.players[ev.player_index].display_resolution.height/2 then
playerInfo.Position = {x = ev.element.location.x, y = ev.element.location.y - playerInfo.oldOffset}
else
playerInfo.Position =ev.element.location
end
end
)
script.on_event(
defines.events.on_gui_click,
function(ev)
if ev.element.get_mod() ~= "ModulesUI" then
return
end
local player = game.get_player(ev.player_index)
if ev.element.name == "ShowFilter" then
local elem = ev.element
player.gui.screen.ModulesUI.optionsContainer.visible = elem.toggled
updateOffset(ev.player_index)
elseif ev.element.name == "CloseWindow" then
player.gui.screen.ModulesUI.destroy()
local playerInfo = getPlayerInfo(player.index)
playerInfo.oldOffset = 0
else
local ____opt_2 = ev.element.parent
if (____opt_2 and ____opt_2.name) == "tierSelectionContainer" and ev.element.tags.tier then
local checkbox = ev.element
local tier = ev.element.tags.tier
local playerInfo = getPlayerInfo(ev.player_index)
if playerInfo.HiddenTiers == nil then
playerInfo.HiddenTiers = {}
end
local index = __TS__ArrayIndexOf(playerInfo.HiddenTiers, tier)
if not checkbox.state and index == -1 then
local ____playerInfo_HiddenTiers_4 = playerInfo.HiddenTiers
____playerInfo_HiddenTiers_4[#____playerInfo_HiddenTiers_4 + 1] = tier
elseif checkbox.state and index >= 0 then
__TS__ArraySplice(playerInfo.HiddenTiers, index, 1)
end
tickPlayer(player)
else
local ____opt_7 = ev.element.parent
local ____opt_5 = ____opt_7 and ____opt_7.parent
if (____opt_5 and ____opt_5.name) == "buttonsTable" then
local item = ev.element.tags.item
local quality = ev.element.tags.quality
if item then
player.cursor_ghost = {name = item, quality = quality}
end
end
end
end
end
)
Revise this Paste
Parent: 128513