mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-27 03:25:23 +09:00
Refactor some of the Guis from the legacy plugin (#399)
* Fix bugs in core and add default args to Gui defs * Refactor production Gui * Refactor landfill blueprint button * Fix more bugs in core * Consistent naming of new guis * Refactor module inserter gui * Refactor surveillance gui * Add shorthand for data from arguments * Make element names consistent * Add types * Change how table rows work * Refactor player stats gui * Refactor quick actions gui * Refactor research milestones gui * Refactor player bonus gui * Refactor science production gui * Refactor autofill gui * Cleanup use of aligned flow * Rename "Gui.element" to "Gui.define" * Rename Gui types * Rename property_from_arg * Add guide for making guis * Add full reference document * Add condensed reference * Apply style guide to refactored guis * Bug fixes
This commit is contained in:
83
exp_scenario/module/gui/elements.lua
Normal file
83
exp_scenario/module/gui/elements.lua
Normal file
@@ -0,0 +1,83 @@
|
||||
--[[-- Gui - Elements
|
||||
A collection of standalone elements that are reused between GUIs
|
||||
]]
|
||||
|
||||
local Gui = require("modules/exp_gui")
|
||||
|
||||
--- @class ExpGui_Elements
|
||||
local Elements = {}
|
||||
|
||||
--- To help with caching and avoid context changes the player list from the previous update is remembered
|
||||
--- @type (string?)[]
|
||||
local _player_names = {}
|
||||
|
||||
--- Dropdown which allows selecting an online player
|
||||
--- @class ExpGui_Elements.online_player_dropdown: ExpElement
|
||||
--- @overload fun(parent: LuaGuiElement): LuaGuiElement
|
||||
Elements.online_player_dropdown = Gui.define("player_dropdown")
|
||||
:track_all_elements()
|
||||
:draw(function(def, parent)
|
||||
return parent.add{
|
||||
type = "drop-down",
|
||||
items = _player_names,
|
||||
selected_index = #_player_names > 0 and 1 or nil,
|
||||
}
|
||||
end)
|
||||
:style{
|
||||
height = 24,
|
||||
} --[[ @as any ]]
|
||||
|
||||
--- Get the selected player name from a online player dropdown
|
||||
--- @param online_player_dropdown LuaGuiElement
|
||||
--- @return string
|
||||
function Elements.online_player_dropdown.get_selected_name(online_player_dropdown)
|
||||
local name = _player_names[online_player_dropdown.selected_index]
|
||||
if not name then
|
||||
online_player_dropdown.selected_index = 1
|
||||
name = _player_names[1] --- @cast name -nil
|
||||
end
|
||||
return name
|
||||
end
|
||||
|
||||
--- Get the selected player from a online player dropdown
|
||||
--- @param online_player_dropdown LuaGuiElement
|
||||
--- @return LuaPlayer
|
||||
function Elements.online_player_dropdown.get_selected(online_player_dropdown)
|
||||
local name = _player_names[online_player_dropdown.selected_index]
|
||||
if not name then
|
||||
online_player_dropdown.selected_index = 1
|
||||
name = _player_names[1] --- @cast name -nil
|
||||
end
|
||||
return assert(game.get_player(name))
|
||||
end
|
||||
|
||||
|
||||
--- Get the number of players in the dropdown
|
||||
--- @return number
|
||||
function Elements.online_player_dropdown.get_player_count()
|
||||
return #_player_names
|
||||
end
|
||||
|
||||
--- Update all player dropdowns to match the currently online players
|
||||
--- We don't split join and leave because the order would be inconsistent between players and cause desyncs
|
||||
function Elements.online_player_dropdown.refresh_online()
|
||||
_player_names[#_player_names] = nil -- Nil last element to account for player leave
|
||||
|
||||
for i, player in pairs(game.connected_players) do
|
||||
_player_names[i] = player.name
|
||||
end
|
||||
|
||||
for _, online_player_dropdown in Elements.online_player_dropdown:online_elements() do
|
||||
online_player_dropdown.items = _player_names
|
||||
end
|
||||
end
|
||||
|
||||
local e = defines.events
|
||||
|
||||
--- @package
|
||||
Elements.events = {
|
||||
[e.on_player_joined_game] = Elements.online_player_dropdown.refresh_online,
|
||||
[e.on_player_left_game] = Elements.online_player_dropdown.refresh_online,
|
||||
}
|
||||
|
||||
return Elements
|
||||
Reference in New Issue
Block a user