Start converting GUI modules

This commit is contained in:
Cooldude2606
2025-01-15 22:32:49 +00:00
parent 88a47e3edc
commit bd6781b3a0
14 changed files with 88 additions and 144 deletions

View File

@@ -31,9 +31,12 @@ ExpGui.get_top_flow = mod_gui.get_button_flow
ExpGui.get_left_flow = mod_gui.get_frame_flow ExpGui.get_left_flow = mod_gui.get_frame_flow
--- Get a player from an element or gui event --- Get a player from an element or gui event
--- @param input LuaGuiElement | { player_index: uint } --- @param input LuaGuiElement | { player_index: uint } | { element: LuaGuiElement }
--- @return LuaPlayer --- @return LuaPlayer
function ExpGui.get_player(input) function ExpGui.get_player(input)
if type(input) == "table" and not input.player_index then
return assert(game.get_player(input.element.player_index))
end
return assert(game.get_player(input.player_index)) return assert(game.get_player(input.player_index))
end end

View File

@@ -11,6 +11,12 @@ local elements = {} --- @type table<string, ExpElement>
local buttons_with_left_element = {} --- @type table<string, ExpElement> local buttons_with_left_element = {} --- @type table<string, ExpElement>
local left_elements_with_button = {} --- @type table<string, ExpElement> local left_elements_with_button = {} --- @type table<string, ExpElement>
ExpGui.on_toolbar_button_toggled = script.generate_event_name()
--- @class EventData.on_toolbar_button_toggled: EventData
--- @field element LuaGuiElement
--- @field state boolean
--- Set the style of a toolbar button --- Set the style of a toolbar button
--- @param element LuaGuiElement --- @param element LuaGuiElement
--- @param state boolean? --- @param state boolean?
@@ -137,6 +143,10 @@ function ExpGui.create_toolbar_button(options)
local player = ExpGui.get_player(event) local player = ExpGui.get_player(event)
ExpGui.set_left_element_visible(left_element, player, state) ExpGui.set_left_element_visible(left_element, player, state)
end end
script.raise_event(ExpGui.on_toolbar_button_toggled, {
element = event.element,
state = state,
})
end) end)
end end

View File

@@ -7,8 +7,7 @@
"control.lua" "control.lua"
], ],
"require": [ "require": [
"core_elements.lua", "core_elements.lua"
"test.lua"
], ],
"dependencies": { "dependencies": {
"clusterio": "*", "clusterio": "*",

View File

@@ -6,7 +6,7 @@ local GuiIter = require("modules/exp_gui/iter")
--- @class ExpGui_ExpElement --- @class ExpGui_ExpElement
local ExpElement = { local ExpElement = {
_elements = {}, _elements = {}, --- @type table<string, LuaGuiElement>
} }
ExpElement.events = {} ExpElement.events = {}
@@ -51,7 +51,7 @@ ExpElement._prototype = {
ExpElement._metatable = { ExpElement._metatable = {
__call = nil, -- ExpElement._prototype.create __call = nil, -- ExpElement._prototype.create
__index = ExpElement._prototype, __index = ExpElement._prototype,
__class = "ExpGui", __class = "ExpElement",
} }
--- Used to signal that the property should be the same as the define name --- Used to signal that the property should be the same as the define name

View File

@@ -1,16 +0,0 @@
local ExpGui = require("modules/exp_gui")
local frame = ExpGui.element("test")
:draw{
type = "frame",
caption = "Hello, World",
}
ExpGui.add_left_element(frame, true)
ExpGui.create_toolbar_button{
name = "test-button",
left_element = frame,
caption = "Test",
}

View File

@@ -43,25 +43,25 @@ return {
"modules.data.language", "modules.data.language",
--- GUI --- GUI
"modules.gui.readme", --"modules.gui.readme",
"modules.gui.rocket-info", --"modules.gui.rocket-info",
"modules.gui.science-info", --"modules.gui.science-info",
"modules.gui.autofill", --"modules.gui.autofill",
"modules.gui.warp-list", --"modules.gui.task-list",
"modules.gui.task-list", --"modules.gui.warp-list",
"modules.gui.player-list", --"modules.gui.player-list",
"modules.gui.server-ups", --"modules.gui.server-ups",
"modules.gui.bonus", --"modules.gui.bonus",
"modules.gui.vlayer", --"modules.gui.vlayer",
"modules.gui.research", --"modules.gui.research",
"modules.gui.module", --"modules.gui.module",
"modules.gui.landfill", --"modules.gui.landfill",
"modules.gui.tool", --"modules.gui.tool",
"modules.gui.production", --"modules.gui.production",
"modules.gui.playerdata", --"modules.gui.playerdata",
"modules.gui.surveillance", --"modules.gui.surveillance",
"modules.graftorio.require", -- graftorio --"modules.graftorio.require", -- graftorio
"modules.gui.toolbar", -- must be loaded last to register toolbar handlers --"modules.gui.toolbar", -- must be loaded last to register toolbar handlers
--- Config Files --- Config Files
"config.expcore.permission_groups", -- loads some predefined permission groups "config.expcore.permission_groups", -- loads some predefined permission groups

View File

@@ -7,6 +7,7 @@
], ],
"dependencies": { "dependencies": {
"clusterio": "*", "clusterio": "*",
"exp_util": "*" "exp_util": "*",
"exp_gui": "*"
} }
} }

View File

@@ -1,10 +1,11 @@
--- Makes trees which are marked for decon "decay" quickly to allow faster building --- Makes trees which are marked for decon "decay" quickly to allow faster building
-- @addon Tree-Decon -- @addon Tree-Decon
local Event = require("modules/exp_legacy/utils/event") --- @dep utils.event
local Storage = require("modules/exp_util/storage") local Storage = require("modules/exp_util/storage")
local Gui = require("modules/exp_gui")
local Event = require("modules/exp_legacy/utils/event") --- @dep utils.event
local Roles = require("modules.exp_legacy.expcore.roles") --- @dep expcore.roles local Roles = require("modules.exp_legacy.expcore.roles") --- @dep expcore.roles
local Gui = require("modules.exp_legacy.expcore.gui") --- @dep expcore.gui
local PlayerData = require("modules.exp_legacy.expcore.player_data") --- @dep expcore.player_data local PlayerData = require("modules.exp_legacy.expcore.player_data") --- @dep expcore.player_data
-- Storage queue used to store trees that need to be removed, also cache for player roles -- Storage queue used to store trees that need to be removed, also cache for player roles
@@ -34,13 +35,20 @@ end
local HasEnabledDecon = PlayerData.Settings:combine("HasEnabledDecon") local HasEnabledDecon = PlayerData.Settings:combine("HasEnabledDecon")
HasEnabledDecon:set_default(false) HasEnabledDecon:set_default(false)
Gui.toolbar_toggle_button("entity/tree-01", { "tree-decon.main-tooltip" }, function(player) Gui.create_toolbar_button{
name = "toggle-tree-decon",
sprite = "entity/tree-01",
tooltip = { "tree-decon.main-tooltip" },
auto_toggle = true,
visible = function(player, _)
return Roles.player_allowed(player, "fast-tree-decon") return Roles.player_allowed(player, "fast-tree-decon")
end) end
:on_event(Gui.events.on_toolbar_button_toggled, function(player, _, event) }:on_event(Gui.on_toolbar_button_toggled, function(def, event)
--- @cast event EventData.on_toolbar_button_toggled
local player = Gui.get_player(event)
HasEnabledDecon:set(player, event.state) HasEnabledDecon:set(player, event.state)
player.print{ "tree-decon.toggle-msg", event.state and { "tree-decon.enabled" } or { "tree-decon.disabled" } } player.print{ "tree-decon.toggle-msg", event.state and { "tree-decon.enabled" } or { "tree-decon.disabled" } }
end) end)
-- Add trees to queue when marked, only allows simple entities and for players with role permission -- Add trees to queue when marked, only allows simple entities and for players with role permission
Event.add(defines.events.on_marked_for_deconstruction, function(event) Event.add(defines.events.on_marked_for_deconstruction, function(event)

View File

@@ -1,6 +1,7 @@
local Event = require("modules/exp_legacy/utils/event") --- @dep utils.event
local Storage = require("modules/exp_util/storage") local Storage = require("modules/exp_util/storage")
local Gui = require("modules.exp_legacy.expcore.gui") --- @dep expcore.gui local Gui = require("modules/exp_gui")
local Event = require("modules/exp_legacy/utils/event") --- @dep utils.event
----- Locals ----- ----- Locals -----
local follow_label -- Gui constructor local follow_label -- Gui constructor
@@ -88,7 +89,7 @@ function Public.stop_follow(player)
Public.stop_spectate(player) Public.stop_spectate(player)
end end
Gui.destroy_if_valid(player.gui.screen[follow_label.name]) Gui.destroy_if_valid(player.gui.screen.follow_label)
following[player.index] = nil following[player.index] = nil
end end
@@ -102,19 +103,18 @@ end
----- Gui ----- ----- Gui -----
--- Label used to show that the player is following, also used to allow esc to stop following --- Label used to show that the player is following, also used to allow esc to stop following
-- @element follow_label follow_label = Gui.element("follow-label")
follow_label = :draw(function(def, parent, target)
Gui.element(function(definition, parent, target) Gui.destroy_if_valid(parent.follow_label)
Gui.destroy_if_valid(parent[definition.name])
local label = parent.add{ local label = parent.add{
type = "label", type = "label",
name = "follow_label",
style = "frame_title", style = "frame_title",
caption = "Following " .. target.name .. ".\nClick here or press esc to stop following.", caption = "Following " .. target.name .. ".\nClick here or press esc to stop following.",
name = definition.name,
} }
local player = Gui.get_player_from_element(parent) local player = Gui.get_player(parent)
local res = player.display_resolution local res = player.display_resolution
label.location = { 0, res.height - 150 } label.location = { 0, res.height - 150 }
label.style.width = res.width label.style.width = res.width
@@ -123,12 +123,11 @@ follow_label =
return label return label
end) end)
:static_name(Gui.unique_static_name)
:on_click(Public.stop_follow) :on_click(Public.stop_follow)
:on_close(function(player) :on_closed(function(def, event)
-- Don't call set_controller during on_close as it invalidates the controller -- Don't call set_controller during on_close as it invalidates the controller
-- Setting an invalid position (as to not equal their current) will call stop_follow on the next tick -- Setting an invalid position (as to not equal their current) will call stop_follow on the next tick
following[player.index][3] = {} following[event.player_index][3] = {}
end) end)
----- Events ----- ----- Events -----

View File

@@ -1,4 +1,5 @@
local Event = require("modules/exp_legacy/utils/event") local Event = require("modules/exp_legacy/utils/event")
local Storage = require("modules/exp_util/storage")
local Gui = require("modules.exp_legacy.utils.gui") local Gui = require("modules.exp_legacy.utils.gui")
local Model = require("modules.exp_legacy.modules.gui.debug.model") local Model = require("modules.exp_legacy.modules.gui.debug.model")
@@ -21,6 +22,11 @@ local checkbox_name = Gui.uid_name()
local filter_name = Gui.uid_name() local filter_name = Gui.uid_name()
local clear_filter_name = Gui.uid_name() local clear_filter_name = Gui.uid_name()
local storage = {}
Storage.register(storage, function(tbl)
storage = tbl
end)
-- storage tables -- storage tables
local enabled = {} local enabled = {}
local last_events = {} local last_events = {}

View File

@@ -1,6 +1,7 @@
local Gui = require("modules.exp_legacy.utils.gui") --- @dep utils.gui local ExpElement = require("modules/exp_gui/prototype")
local ExpGui = require("modules.exp_legacy.expcore.gui")
local Color = require("modules/exp_util/include/color") local Color = require("modules/exp_util/include/color")
local Gui = require("modules.exp_legacy.utils.gui") --- @dep utils.gui
local Model = require("modules.exp_legacy.modules.gui.debug.model") --- @dep modules.gui.debug.model local Model = require("modules.exp_legacy.modules.gui.debug.model") --- @dep modules.gui.debug.model
local dump = Model.dump local dump = Model.dump
@@ -24,9 +25,10 @@ function Public.show(container)
local left_panel_style = left_panel.style local left_panel_style = left_panel.style
left_panel_style.width = 300 left_panel_style.width = 300
for element_id, file_path in pairs(ExpGui.file_paths) do --- @diagnostic disable-next-line invisible
local header = left_panel.add{ type = "flow" }.add{ type = "label", name = header_name, caption = element_id .. " - " .. file_path } for element_name in pairs(ExpElement._elements) do
Gui.set_data(header, element_id) local header = left_panel.add{ type = "flow" }.add{ type = "label", name = header_name, caption = element_name }
Gui.set_data(header, element_name)
end end
local right_flow = main_flow.add{ type = "flow", direction = "vertical" } local right_flow = main_flow.add{ type = "flow", direction = "vertical" }
@@ -70,7 +72,7 @@ Gui.on_click(
header_name, header_name,
function(event) function(event)
local element = event.element local element = event.element
local element_id = Gui.get_data(element) local element_name = Gui.get_data(element)
local left_panel = element.parent.parent local left_panel = element.parent.parent
local data = Gui.get_data(left_panel) local data = Gui.get_data(left_panel)
@@ -85,10 +87,11 @@ Gui.on_click(
element.style.font_color = Color.orange element.style.font_color = Color.orange
data.selected_header = element data.selected_header = element
input_text_box.text = concat{ "Gui.defines[", element_id, "]" } input_text_box.text = concat{ "ExpElement._elements[", element_name, "]" }
input_text_box.style.font_color = Color.black input_text_box.style.font_color = Color.black
local content = dump(ExpGui.debug_info[element_id]) or "nil" --- @diagnostic disable-next-line invisible
local content = dump(ExpElement._elements[element_name]) or "nil"
right_panel.text = content right_panel.text = content
end end
) )

View File

@@ -1,7 +1,6 @@
local Gui = require("modules.exp_legacy.utils.gui") --- @dep utils.gui local Gui = require("modules.exp_legacy.utils.gui") --- @dep utils.gui
local ExpUtil = require("modules/exp_util")
local gui_names = Gui.names
local type = type
local concat = table.concat local concat = table.concat
local inspect = table.inspect local inspect = table.inspect
local pcall = pcall local pcall = pcall
@@ -10,76 +9,7 @@ local rawset = rawset
local Public = {} local Public = {}
local LuaObject = { "{", nil, ", name = '", nil, "'}" } local inspect_process = ExpUtil.safe_value
local LuaPlayer = { "{LuaPlayer, name = '", nil, "', index = ", nil, "}" }
local LuaEntity = { "{LuaEntity, name = '", nil, "', unit_number = ", nil, "}" }
local LuaGuiElement = { "{LuaGuiElement, name = '", nil, "'}" }
local function get(obj, prop)
return obj[prop]
end
local function get_name_safe(obj)
local s, r = pcall(get, obj, "name")
if not s then
return "nil"
else
return r or "nil"
end
end
local function get_lua_object_type_safe(obj)
local s, r = pcall(get, obj, "help")
if not s then
return
end
return r():match("Lua%a+")
end
local function inspect_process(item)
if type(item) ~= "table" or type(item.__self) ~= "userdata" then
return item
end
local suc, valid = pcall(get, item, "valid")
if not suc then
-- no 'valid' property
return get_lua_object_type_safe(item) or "{NoHelp LuaObject}"
end
if not valid then
return "{Invalid LuaObject}"
end
local obj_type = get_lua_object_type_safe(item)
if not obj_type then
return "{NoHelp LuaObject}"
end
if obj_type == "LuaPlayer" then
LuaPlayer[2] = item.name or "nil"
LuaPlayer[4] = item.index or "nil"
return concat(LuaPlayer)
elseif obj_type == "LuaEntity" then
LuaEntity[2] = item.name or "nil"
LuaEntity[4] = item.unit_number or "nil"
return concat(LuaEntity)
elseif obj_type == "LuaGuiElement" then
local name = item.name
LuaGuiElement[2] = gui_names and gui_names[name] or name or "nil"
return concat(LuaGuiElement)
else
LuaObject[2] = obj_type
LuaObject[4] = get_name_safe(item)
return concat(LuaObject)
end
end
local inspect_options = { process = inspect_process } local inspect_options = { process = inspect_process }
function Public.dump(data) function Public.dump(data)

View File

@@ -1,6 +1,6 @@
local Storage = require("modules/exp_util/storage") local Storage = require("modules/exp_util/storage")
local Event = require("modules/exp_legacy/utils/event") --- @dep expcore.gui local Event = require("modules/exp_legacy/utils/event")
local mod_gui = require "mod-gui" --- @dep mod-gui local mod_gui = require "mod-gui"
local Gui = {} local Gui = {}
local data = {} local data = {}

View File

@@ -5,8 +5,9 @@ Adds rcon interfaces for the legacy exp core
local Commands = require("modules/exp_commands") local Commands = require("modules/exp_commands")
local add_static, add_dynamic = Commands.add_rcon_static, Commands.add_rcon_dynamic local add_static, add_dynamic = Commands.add_rcon_static, Commands.add_rcon_dynamic
add_static("Gui", require("modules/exp_gui"))
add_static("Group", require("modules.exp_legacy.expcore.permission_groups")) add_static("Group", require("modules.exp_legacy.expcore.permission_groups"))
add_static("Roles", require("modules.exp_legacy.expcore.roles")) add_static("Roles", require("modules.exp_legacy.expcore.roles"))
add_static("Gui", require("modules.exp_legacy.expcore.gui"))
add_static("Datastore", require("modules.exp_legacy.expcore.datastore")) add_static("Datastore", require("modules.exp_legacy.expcore.datastore"))
add_static("External", require("modules.exp_legacy.expcore.external")) add_static("External", require("modules.exp_legacy.expcore.external"))