Skip to content

Commit

Permalink
Fix formspec lag when settings scrollbar is moved
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneWolfHT committed Nov 20, 2023
1 parent 75ec171 commit d534180
Showing 1 changed file with 53 additions and 12 deletions.
65 changes: 53 additions & 12 deletions mods/apis/ctf_settings/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,72 @@ function ctf_settings.get(player, setting)
return value == "" and info.default or value
end

-- This Function MIT by Rubenwardy
--- Creates a scrollbaroptions for a scroll_container
--
-- @param visible_l the length of the scroll_container and scrollbar
-- @param total_l length of the scrollable area
-- @param scroll_factor as passed to scroll_container
local function make_scrollbaroptions_for_scroll_container(visible_l, total_l, scroll_factor)

assert(total_l >= visible_l)

local thumb_size = (visible_l / total_l) * (total_l - visible_l)

local max = total_l - visible_l

return ("scrollbaroptions[min=0;max=%f;thumbsize=%f]"):format(max / scroll_factor, thumb_size / scroll_factor)
end

minetest.register_on_mods_loaded(function()
sfinv.register_page("ctf_settings:settings", {
title = "Settings",
get = function(self, player, context)
local setting_list = {}
local lastypos = -0.5

if not context then
context = {}
end

if not context.setting then
context.setting = {}
end

for k, setting in ipairs(ctf_settings.settings_list) do
local settingdef = ctf_settings.settings[setting]

if not context.setting[setting] then
context.setting[setting] = ctf_settings.get(player, setting)
end

if settingdef.type == "bool" then
setting_list[k] = {
"checkbox[0,%f;%s;%s;%s]tooltip[%s;%s]",
lastypos,
setting,
settingdef.label or setting,
ctf_settings.get(player, setting),
context.setting[setting],
setting,
settingdef.description or HumanReadable(setting)
}

lastypos = lastypos + 0.5
elseif settingdef.type == "list" then
local max_len = 0

for _, val in pairs(settingdef.list) do
max_len = math.max(val:len(), max_len)
end

lastypos = lastypos + 0.3
setting_list[k] = {
"dropdown[0,%f;%f;%s;%s;%d]tooltip[0,%f;%f,0.6;%s]",
lastypos,
FORMSIZE.x/1.7,
math.max(FORMSIZE.x / 2, math.min(FORMSIZE.x - SCROLLBAR_W + 2, 0.5 + (max_len * 0.23))),
setting,
settingdef.list,
ctf_settings.get(player, setting),
context.setting[setting],
--label
lastypos,
(FORMSIZE.x/1.7) - 0.3,
Expand All @@ -91,12 +126,12 @@ minetest.register_on_mods_loaded(function()
},
ctf_gui.list_to_formspec_str(setting_list),
"scroll_container_end[]",
{"scrollbaroptions[max=%d]", math.ceil((lastypos - 3.833) * 11.538)},
make_scrollbaroptions_for_scroll_container(FORMSIZE.y + 0.7, math.max(lastypos+1, FORMSIZE.y + 0.7), 0.1),
{"scrollbar[%f,-0.1;%f,%f;vertical;settings_scrollbar;%f]",
FORMSIZE.x - SCROLLBAR_W,
SCROLLBAR_W,
FORMSIZE.y,
context and context.settings_scrollbar or 0
context.settings_scrollbar or 0
},
}

Expand All @@ -112,24 +147,30 @@ minetest.register_on_mods_loaded(function()
if setting.type == "bool" then
local newvalue = value == "true" and "true" or "false"

ctf_settings.set(player, field, newvalue)
if context.setting[field] ~= newvalue then
context.setting[field] = newvalue
ctf_settings.set(player, field, newvalue)

if setting.on_change then
setting.on_change(player, newvalue)
if setting.on_change then
setting.on_change(player, newvalue)
end

refresh = true
end
elseif setting.type == "list" then
local idx = table.indexof(setting.list, value)

if idx ~= -1 then
if idx ~= -1 and context.setting[field] ~= tostring(idx) then
context.setting[field] = tostring(idx)
ctf_settings.set(player, field, tostring(idx))

if setting.on_change then
setting.on_change(player, idx)
setting.on_change(player, tostring(idx))
end

refresh = true
end
end

refresh = true
end
end

Expand Down

0 comments on commit d534180

Please sign in to comment.