Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sliders to settings #14469

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions builtin/mainmenu/settings/components.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,40 @@ end, is_valid_number)
make.string = make_field(tostring, nil)


function make.slider(setting)
local steps = tonumber(setting.steps) or 100

return {
info_text = setting.comment,
setting = setting,

get_formspec = function(self, avail_w)
local value = core.settings:get(setting.name) or setting.default
local scrollbar_value = (value - setting.min)/(setting.max - setting.min)*steps

self.resettable = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.resettable = true
self.resettable = core.settings:has(setting.name)


local setting_label = ("label[0,0.25;%s]"):format(
setting.readable_name)
local scrollbar_options = ("scrollbaroptions[min=0;max=%f;smallstep=1;largestep=10]"):format(
steps)
local scrollbar_formspec = ("scrollbar[0,0.5;%f,0.4;horizontal;%s;%f]"):format(
avail_w - 1, setting.name, scrollbar_value)
local formspec = setting_label..scrollbar_options..scrollbar_formspec
Copy link
Contributor

@y5nw y5nw Mar 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking: Why not use table.concat? That would make sense if you want to add more (e.g. value display/input box) to this field.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest of code in this file also does this so I guess it's better for consistency.


return formspec, 1.1
end,

on_submit = function(self, fields)
local raw_value = core.explode_scrollbar_event(fields[setting.name]).value
local value = (raw_value/steps) * (setting.max - setting.min) + setting.min
core.settings:set(setting.name, tostring(value))
return false
Comment on lines +154 to +157
Copy link
Contributor

@y5nw y5nw Mar 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value should only be changed if the scrollbar is modified.

Suggested change
local raw_value = core.explode_scrollbar_event(fields[setting.name]).value
local value = (raw_value/steps) * (setting.max - setting.min) + setting.min
core.settings:set(setting.name, tostring(value))
return false
local event = core.explode_scrollbar_event(fields[setting.name])
if event.type ~= "CHG" then
return false
end
local raw_value = event.value
local value = (raw_value/steps) * (setting.max - setting.min) + setting.min
core.settings:set(setting.name, tostring(value))
return true

Edit: kotek pointed out in Discord that this somehow breaks mouse dragging.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reason why mouse dragging stops working is that after returning true GUI gets regenerated and this probably causes scrollbar to lose focus.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normally regenerating the GUI doesn't make scrollbars loose focus, but for some minetest reason it does in the main menu

end
}
end


function make.bool(setting)
return {
info_text = setting.comment,
Expand Down
29 changes: 29 additions & 0 deletions builtin/mainmenu/settings/settingtypes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,35 @@ local function parse_setting_line(settings, line, read_all, base_level, allow_se
return
end

if setting_type == "slider" then
local default, min, max, steps = remaining_line:match("^"
-- first 3 floats are required, the last is optional
.. "(" .. CHAR_CLASSES.FLOAT .. "+)" .. CHAR_CLASSES.SPACE .. "*"
.. "(" .. CHAR_CLASSES.FLOAT .. "+)" .. CHAR_CLASSES.SPACE .. "*"
.. "(" .. CHAR_CLASSES.FLOAT .. "+)" .. CHAR_CLASSES.SPACE .. "*"
.. "(" .. CHAR_CLASSES.FLOAT .. "*)"
.."$")

if not default or not tonumber(default) then
return "Invalid float setting"
end

min = tonumber(min)
max = tonumber(max)
table.insert(settings, {
name = name,
readable_name = readable_name,
type = "slider",
default = default,
min = min,
max = max,
steps = steps,
requires = requires,
comment = comment,
})
return
end

if setting_type == "enum" then
local default, values = remaining_line:match("^"
-- first value (default) may be empty (i.e. is optional)
Expand Down
8 changes: 4 additions & 4 deletions builtin/settingtypes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ invert_mouse (Invert mouse) bool false
# Mouse sensitivity multiplier.
#
# Requires: keyboard_mouse
mouse_sensitivity (Mouse sensitivity) float 0.2 0.001 10.0
mouse_sensitivity (Mouse sensitivity) slider 0.2 0.001 1.0 500

# Enable mouse wheel (scroll) for item selection in hotbar.
#
Expand Down Expand Up @@ -646,10 +646,10 @@ enable_volumetric_lighting (Volumetric lighting) bool false

# Volume of all sounds.
# Requires the sound system to be enabled.
sound_volume (Volume) float 0.8 0.0 1.0
sound_volume (Volume) slider 0.8 0.0 1.0

# Volume multiplier when the window is unfocused.
sound_volume_unfocused (Volume when unfocused) float 0.3 0.0 1.0
sound_volume_unfocused (Volume when unfocused) slider 0.3 0.0 1.0

# Whether to mute sounds. You can unmute sounds at any time, unless the
# sound system is disabled (enable_sound=false).
Expand Down Expand Up @@ -704,7 +704,7 @@ menu_clouds (Clouds in menu) bool true
[**HUD]

# Modifies the size of the HUD elements.
hud_scaling (HUD scaling) float 1.0 0.5 20
hud_scaling (HUD scaling) slider 1.0 0.5 20 1000

# Whether name tag backgrounds should be shown by default.
# Mods may still set a background.
Expand Down