Update all code styles

This commit is contained in:
Cooldude2606
2024-09-28 01:56:54 +01:00
parent 5e2a62ab27
commit 292c1a1b68
194 changed files with 9817 additions and 9703 deletions

View File

@@ -2,7 +2,7 @@ local Gui = require("modules.exp_legacy.expcore.gui") --- @dep expcore.gui
local PlayerData = require("modules.exp_legacy.expcore.player_data") --- @dep expcore.player_data
-- Used to store the state of the toolbar when a player leaves
local ToolbarState = PlayerData.Settings:combine('ToolbarState')
local ToolbarState = PlayerData.Settings:combine("ToolbarState")
ToolbarState:set_metadata{
stringify = function(value)
local buttons, favourites = 0, 0
@@ -12,15 +12,16 @@ ToolbarState:set_metadata{
favourites = favourites + 1
end
end
return string.format("Buttons: %d, Favourites: %d", buttons, favourites)
end
end,
}
-- Styles used for sprite buttons
local button_size = 20
local Styles = {
header = Gui.sprite_style(22),
item = Gui.sprite_style(button_size)
item = Gui.sprite_style(button_size),
}
--- Set the style of the fake toolbar element
@@ -46,7 +47,7 @@ local function move_toolbar_button(player, item, offset)
-- Swap the position in the top flow, offset by 1 because of settings button
local top_flow = Gui.get_top_flow(player)
top_flow.swap_children(old_index+1, new_index+1)
top_flow.swap_children(old_index + 1, new_index + 1)
-- Check if the element has a left element to move
local element_define = Gui.defines[item.tags.top_element_uid]
@@ -100,7 +101,7 @@ local function reorder_toolbar_menu(player)
-- Check if the player is allowed to see the button
local allowed = element_define.authenticator
if type(allowed) == 'function' then allowed = allowed(player) end
if type(allowed) == "function" then allowed = allowed(player) end
-- Update the checkbox state and item visibility
local toolbar_button = Gui.get_top_element(player, element_define)
@@ -121,210 +122,210 @@ end
--- Resets the toolbar to its default state when pressed
-- @element reset_toolbar
local reset_toolbar =
Gui.element {
type = "sprite-button",
sprite = "utility/reset",
style = "shortcut_bar_button_red",
tooltip = {"toolbar.reset"},
name = Gui.unique_static_name
}
:style(Gui.sprite_style(Styles.header.width, -1))
:on_click(function(player)
ToolbarState:set(player, nil)
Gui.toggle_top_flow(player, true)
reorder_toolbar_menu(player)
end)
Gui.element{
type = "sprite-button",
sprite = "utility/reset",
style = "shortcut_bar_button_red",
tooltip = { "toolbar.reset" },
name = Gui.unique_static_name,
}
:style(Gui.sprite_style(Styles.header.width, -1))
:on_click(function(player)
ToolbarState:set(player, nil)
Gui.toggle_top_flow(player, true)
reorder_toolbar_menu(player)
end)
--- Replaces the default method for opening and closing the toolbar
-- @element toggle_toolbar
toggle_toolbar =
Gui.element {
type = "sprite-button",
sprite = "utility/bookmark",
tooltip = {"toolbar.toggle"},
style = "tool_button",
auto_toggle = true,
name = Gui.unique_static_name
}
:style(Styles.header)
:on_click(function(player, element)
Gui.toggle_top_flow(player, element.toggled)
end)
Gui.element{
type = "sprite-button",
sprite = "utility/bookmark",
tooltip = { "toolbar.toggle" },
style = "tool_button",
auto_toggle = true,
name = Gui.unique_static_name,
}
:style(Styles.header)
:on_click(function(player, element)
Gui.toggle_top_flow(player, element.toggled)
end)
--- Move an element up the list
-- @element move_up
move_up =
Gui.element {
type = "sprite-button",
sprite = "utility/speed_up",
tooltip = {"toolbar.move-up"},
name = Gui.unique_static_name
}
:style(Styles.item)
:on_click(function(player, element)
local item = element.parent.parent
move_toolbar_button(player, item, -1)
end)
Gui.element{
type = "sprite-button",
sprite = "utility/speed_up",
tooltip = { "toolbar.move-up" },
name = Gui.unique_static_name,
}
:style(Styles.item)
:on_click(function(player, element)
local item = element.parent.parent
move_toolbar_button(player, item, -1)
end)
--- Move an element down the list
-- @element move_down
move_down =
Gui.element {
type = "sprite-button",
sprite = "utility/speed_down",
tooltip = {"toolbar.move-down"},
name = Gui.unique_static_name
}
:style(Styles.item)
:on_click(function(player, element)
local item = element.parent.parent
move_toolbar_button(player, item, 1)
end)
Gui.element{
type = "sprite-button",
sprite = "utility/speed_down",
tooltip = { "toolbar.move-down" },
name = Gui.unique_static_name,
}
:style(Styles.item)
:on_click(function(player, element)
local item = element.parent.parent
move_toolbar_button(player, item, 1)
end)
--- A flow which represents one item in the toolbar list
-- @element toolbar_list_item
local toolbar_list_item =
Gui.element(function(definition, parent, element_define)
local flow = parent.add {
type = "frame",
style = "shortcut_selection_row",
name = element_define.name,
tags = {
top_element_uid = element_define.uid
Gui.element(function(definition, parent, element_define)
local flow = parent.add{
type = "frame",
style = "shortcut_selection_row",
name = element_define.name,
tags = {
top_element_uid = element_define.uid,
},
}
}
flow.style.horizontally_stretchable = true
flow.style.vertical_align = "center"
flow.style.horizontally_stretchable = true
flow.style.vertical_align = "center"
-- Add the button and the icon edit button
local element = element_define(flow)
local player = Gui.get_player_from_element(parent)
local top_element = Gui.get_top_element(player, element_define)
copy_style(top_element, element)
-- Add the button and the icon edit button
local element = element_define(flow)
local player = Gui.get_player_from_element(parent)
local top_element = Gui.get_top_element(player, element_define)
copy_style(top_element, element)
-- Add the checkbox that can toggle the visibility
local checkbox = flow.add{
type = "checkbox",
name = "checkbox",
caption = element_define.tooltip or element_define.caption or "None",
state = top_element.visible or false,
tags = {
top_element_name = element_define.name
-- Add the checkbox that can toggle the visibility
local checkbox = flow.add{
type = "checkbox",
name = "checkbox",
caption = element_define.tooltip or element_define.caption or "None",
state = top_element.visible or false,
tags = {
top_element_name = element_define.name,
},
}
}
definition:triggers_events(checkbox)
checkbox.style.width = 180
definition:triggers_events(checkbox)
checkbox.style.width = 180
-- Add the buttons used to move the flow up and down
local move_flow = flow.add{ type = "flow", name = "move" }
move_flow.style.horizontal_spacing = 0
move_up(move_flow)
move_down(move_flow)
-- Add the buttons used to move the flow up and down
local move_flow = flow.add{ type = "flow", name = "move" }
move_flow.style.horizontal_spacing = 0
move_up(move_flow)
move_down(move_flow)
return definition:no_events(flow)
end)
:on_checked_changed(function(player, element)
local top_flow = Gui.get_top_flow(player)
local top_element = top_flow[element.tags.top_element_name]
local had_visible = Gui.top_flow_has_visible_elements(player)
top_element.visible = element.state
-- Check if we are on the edge case between 0 and 1 visible elements
if element.state and not had_visible then
Gui.toggle_top_flow(player, true)
local container = element.parent.parent.parent.parent
local button = container.header.alignment[toggle_toolbar.name]
button.toggled = true
button.enabled = true
elseif not element.state and not Gui.top_flow_has_visible_elements(player) then
Gui.toggle_top_flow(player, false)
local container = element.parent.parent.parent.parent
local button = container.header.alignment[toggle_toolbar.name]
button.toggled = false
button.enabled = false
end
-- Update the datastore state
ToolbarState:update(player, function(_, order)
local index = element.parent.get_index_in_parent()
order[index].favourite = element.state
return definition:no_events(flow)
end)
:on_checked_changed(function(player, element)
local top_flow = Gui.get_top_flow(player)
local top_element = top_flow[element.tags.top_element_name]
local had_visible = Gui.top_flow_has_visible_elements(player)
top_element.visible = element.state
-- Check if we are on the edge case between 0 and 1 visible elements
if element.state and not had_visible then
Gui.toggle_top_flow(player, true)
local container = element.parent.parent.parent.parent
local button = container.header.alignment[toggle_toolbar.name]
button.toggled = true
button.enabled = true
elseif not element.state and not Gui.top_flow_has_visible_elements(player) then
Gui.toggle_top_flow(player, false)
local container = element.parent.parent.parent.parent
local button = container.header.alignment[toggle_toolbar.name]
button.toggled = false
button.enabled = false
end
-- Update the datastore state
ToolbarState:update(player, function(_, order)
local index = element.parent.get_index_in_parent()
order[index].favourite = element.state
end)
end)
end)
--- Scrollable list of all toolbar buttons
-- @element toolbar_list
local toolbar_list =
Gui.element(function(_, parent)
-- This is a scroll pane for the list
local scroll_pane = parent.add {
name = "scroll",
type = "scroll-pane",
direction = "vertical",
horizontal_scroll_policy = "never",
vertical_scroll_policy = "auto",
style = "scroll_pane_under_subheader"
}
scroll_pane.style.horizontally_stretchable = true
scroll_pane.style.padding = 0
scroll_pane.style.maximal_height = 224
Gui.element(function(_, parent)
-- This is a scroll pane for the list
local scroll_pane = parent.add{
name = "scroll",
type = "scroll-pane",
direction = "vertical",
horizontal_scroll_policy = "never",
vertical_scroll_policy = "auto",
style = "scroll_pane_under_subheader",
}
scroll_pane.style.horizontally_stretchable = true
scroll_pane.style.padding = 0
scroll_pane.style.maximal_height = 224
-- This flow is the list, we need a linear list because of get_index_in_parent
local flow = scroll_pane.add {
name = "list",
type = "flow",
direction = "vertical"
}
flow.style.vertical_spacing = 0
flow.style.horizontally_stretchable = true
-- This flow is the list, we need a linear list because of get_index_in_parent
local flow = scroll_pane.add{
name = "list",
type = "flow",
direction = "vertical",
}
flow.style.vertical_spacing = 0
flow.style.horizontally_stretchable = true
return flow
end)
return flow
end)
--- Main toolbar container for the left flow
-- @element toolbar_container
toolbar_container =
Gui.element(function(definition, parent)
-- Draw the internal container
local container = Gui.container(parent, definition.name, 268)
container.style.maximal_width = 268
container.style.minimal_width = 268
Gui.element(function(definition, parent)
-- Draw the internal container
local container = Gui.container(parent, definition.name, 268)
container.style.maximal_width = 268
container.style.minimal_width = 268
-- Draw the header
local player = Gui.get_player_from_element(parent)
local header = Gui.header(container, {"toolbar.main-caption"}, {"toolbar.main-tooltip"}, true)
-- Draw the header
local player = Gui.get_player_from_element(parent)
local header = Gui.header(container, { "toolbar.main-caption" }, { "toolbar.main-tooltip" }, true)
-- Draw the toolbar control buttons
local toggle_element = toggle_toolbar(header)
toggle_element.toggled = Gui.get_top_flow(player).visible
reset_toolbar(header)
-- Draw the toolbar control buttons
local toggle_element = toggle_toolbar(header)
toggle_element.toggled = Gui.get_top_flow(player).visible
reset_toolbar(header)
-- Draw toolbar list element
local list_element = toolbar_list(container)
local flow_order = Gui.get_top_flow_order(player)
-- Draw toolbar list element
local list_element = toolbar_list(container)
local flow_order = Gui.get_top_flow_order(player)
for _, element_define in ipairs(flow_order) do
-- Ensure the element exists
local element = list_element[element_define.name]
if not element then
element = toolbar_list_item(list_element, element_define)
for _, element_define in ipairs(flow_order) do
-- Ensure the element exists
local element = list_element[element_define.name]
if not element then
element = toolbar_list_item(list_element, element_define)
end
-- Set the visible state
local allowed = element_define.authenticator
if type(allowed) == "function" then allowed = allowed(player) end
element.visible = allowed or false
end
-- Set the visible state
local allowed = element_define.authenticator
if type(allowed) == 'function' then allowed = allowed(player) end
element.visible = allowed or false
end
-- Set the state of the move buttons for the first and last element
local children = list_element.children
children[1].move[move_up.name].enabled = false
children[#children].move[move_down.name].enabled = false
-- Set the state of the move buttons for the first and last element
local children = list_element.children
children[1].move[move_up.name].enabled = false
children[#children].move[move_down.name].enabled = false
-- Return the external container
return container.parent
end)
:static_name(Gui.unique_static_name)
:add_to_left_flow(false)
-- Return the external container
return container.parent
end)
:static_name(Gui.unique_static_name)
:add_to_left_flow(false)
--- Set the default value for the datastore
local datastore_id_map = {}
@@ -353,7 +354,10 @@ for index, element_define in ipairs(Gui.top_elements) do
if prev_handler then prev_handler(player, element, event) end -- Kind of hacky but works
local frame = Gui.get_left_element(player, toolbar_container)
if not frame then return end -- Gui might not be loaded yet
if not frame.container then log(frame.name) log(frame.parent.name) end
if not frame.container then
log(frame.name)
log(frame.parent.name)
end
local button = frame.container.scroll.list[element_define.name][element_define.name]
local toolbar_button = Gui.get_top_element(player, element_define)
copy_style(toolbar_button, button)
@@ -521,4 +525,4 @@ ToolbarState:on_save(function(player_name, value)
end
return { order, favourites, left_flows, top_flow_open }
end)
end)