1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/features/gui/redmew_settings.lua

236 lines
6.6 KiB
Lua
Raw Normal View History

2019-03-03 22:37:50 +02:00
local Gui = require 'utils.gui'
local Token = require 'utils.token'
local Event = require 'utils.event'
local Server = require 'features.server'
local Toast = require 'features.gui.toast'
local Settings = require 'utils.redmew_settings'
local Color = require 'resources.color_presets'
local pairs = pairs
local main_button_name = Gui.uid_name()
local save_changes_button_name = Gui.uid_name()
local main_frame_name = Gui.uid_name()
local Public = {}
local on_player_settings_get = Token.register(function (data)
local player = game.get_player(data.key)
2019-03-03 22:37:50 +02:00
if not player or not player.valid then
return
end
local button = player.gui.top[main_button_name]
button.enabled = true
button.tooltip = {'redmew_settings_gui.tooltip'}
if data.cancelled then
return
end
2019-03-03 22:37:50 +02:00
local settings = data.value
if settings ~= nil then
local player_index = player.index
for key, value in pairs(settings) do
Settings.set(player_index, key, value)
end
end
end)
local function player_created(event)
local player = game.get_player(event.player_index)
2019-03-03 22:37:50 +02:00
if not player or not player.valid then
return
end
player.gui.top.add({
2019-03-03 22:37:50 +02:00
type = 'sprite-button',
name = main_button_name,
sprite = 'item/iron-gear-wheel',
tooltip = {'redmew_settings_gui.tooltip_loading'},
enabled = false,
2019-03-03 22:37:50 +02:00
})
end
local function player_joined(event)
local player = game.get_player(event.player_index)
2019-03-03 22:37:50 +02:00
if not player or not player.valid then
return
end
local main_frame = player.gui.center[main_frame_name]
if main_frame then
Gui.destroy(main_frame)
end
local button = player.gui.top[main_button_name]
button.tooltip = {'redmew_settings_gui.tooltip_loading'}
button.enabled = false
Server.try_get_data_timeout('player_settings', player.name, on_player_settings_get, 30)
2019-03-03 22:37:50 +02:00
end
local function get_element_value(element)
if element.type == 'text-box' then
return element.text
end
if element.type == 'slider' then
return element.slider_value
end
if element.type == 'checkbox' then
return element.state
end
end
local function create_input_element(frame, type, value)
if type == 'fraction' then
return frame.add({type = 'slider', value = value, minimum_value = 0, maximum_value = 1})
end
if type == 'boolean' then
return frame.add({type = 'checkbox', state = value})
end
-- ensure something is always added to prevent errors
return frame.add({type = 'text-box', text = value})
end
local function draw_main_frame(center, player)
local settings = Settings.get_setting_metadata()
local settings_frame = center.add({
type = 'frame',
name = main_frame_name,
direction = 'vertical',
2019-03-04 08:51:57 +02:00
caption = {'redmew_settings_gui.frame_title'},
2019-03-03 22:37:50 +02:00
})
local settings_frame_style = settings_frame.style
settings_frame_style.width = 400
local scroll_pane = settings_frame.add({type = 'scroll-pane'})
local scroll_style = scroll_pane.style
2019-03-04 21:34:44 +02:00
scroll_style.vertically_squashable = true
2019-03-03 22:37:50 +02:00
scroll_style.maximal_height = 800
scroll_style.bottom_padding = 5
scroll_style.left_padding = 5
scroll_style.right_padding = 5
scroll_style.top_padding = 5
local setting_grid = scroll_pane.add({type = 'table', column_count = 2})
local player_index = player.index
local data = {}
for name, setting in pairs(settings) do
local label = setting_grid.add({
type = 'label',
2019-03-04 21:34:44 +02:00
caption = setting.localised_string,
2019-03-03 22:37:50 +02:00
})
label.style.horizontally_stretchable = true
2019-03-04 21:34:44 +02:00
label.style.height = 35
2019-03-03 22:37:50 +02:00
local value = Settings.get(player_index, name)
local input = create_input_element(setting_grid, setting.type, value)
data[name] = {
label = label,
input = input,
previous_value = value,
}
end
local bottom_flow = settings_frame.add({type = 'flow', direction = 'horizontal'})
local left_flow = bottom_flow.add({type = 'flow'})
left_flow.style.horizontal_align = 'left'
left_flow.style.horizontally_stretchable = true
2019-03-04 08:51:57 +02:00
local close_button = left_flow.add({type = 'button', name = main_button_name, caption = {'redmew_settings_gui.button_cancel'}})
2019-03-03 22:37:50 +02:00
close_button.style = 'back_button'
local right_flow = bottom_flow.add({type = 'flow'})
right_flow.style.horizontal_align = 'right'
2019-03-04 08:51:57 +02:00
local save_button = right_flow.add({type = 'button', name = save_changes_button_name, caption = {'redmew_settings_gui.button_save_changes'}})
2019-03-03 22:37:50 +02:00
save_button.style = 'confirm_button'
Gui.set_data(save_button, data)
player.opened = settings_frame
end
local function toggle(event)
local player = event.player
local center = player.gui.center
local main_frame = center[main_frame_name]
if main_frame then
Gui.destroy(main_frame)
else
draw_main_frame(center, player)
end
end
local function save_changes(event)
local data = Gui.get_data(event.element)
local player_index = event.player_index
local player = event.player
local errors_count = 0
local values = {}
for name, element_data in pairs(data) do
local input = element_data.input
local label = element_data.label
local value = get_element_value(input)
local validated = Settings.validate(name, value)
if nil ~= validated then
errors_count = errors_count + 1
label.style.font_color = Color.red
label.tooltip = validated
label.parent.tooltip = validated
input.tooltip = validated
input.parent.tooltip = validated
else
label.style.font_color = Color.white
label.tooltip = ''
label.parent.tooltip = ''
input.tooltip = ''
input.parent.tooltip = ''
end
values[name] = value
end
if errors_count > 0 then
return
end
for name, value in pairs (values) do
Settings.set(player_index, name, value)
end
2019-03-04 08:51:57 +02:00
Toast.toast_player(player, 5, {'redmew_settings_gui.save_success_toast_message'})
2019-03-03 22:37:50 +02:00
local main_frame = player.gui.center[main_frame_name]
if main_frame then
Gui.destroy(main_frame)
end
end
Gui.on_custom_close(main_frame_name, function(event)
Gui.destroy(event.element)
end)
Gui.allow_player_to_toggle_top_element_visibility(main_button_name)
Gui.on_click(main_button_name, toggle)
Gui.on_click(save_changes_button_name, save_changes)
Event.add(defines.events.on_player_created, player_created)
Event.add(defines.events.on_player_joined_game, player_joined)
return Public