Skip to content

Commit

Permalink
make settings take effect immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
Bauumm committed Nov 5, 2023
1 parent 76ce33e commit e439216
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 11 deletions.
38 changes: 34 additions & 4 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,42 @@ local function add_setting(category, name, default, options)
end

add_setting("Gameplay", "game_resolution_scale", 1, { min = 1, max = 10, step = 1 })
add_setting("UI", "gui_scale", 1, { min = 0.5, max = 2, step = 0.1 })
add_setting("UI", "area_based_gui_scale", false)
add_setting("UI", "gui_scale", 1, {
min = 0.5,
max = 2,
step = 0.1,
onchange = function()
local ui = require("ui")
if not ui.get_grabbed() then
ui.process_event("resize")
return true
end
end,
})
add_setting("UI", "area_based_gui_scale", false, {
onchange = function()
require("ui").process_event("resize")
return true
end
})
add_setting("UI", "background_preview", true)
add_setting("UI", "background_preview_has_text", false)
add_setting("Audio", "background_preview_music_volume", 0, { min = 0, max = 1, step = 0.05 })
add_setting("Audio", "background_preview_sound_volume", 0, { min = 0, max = 1, step = 0.05 })
add_setting("Audio", "background_preview_music_volume", 0, {
min = 0,
max = 1,
step = 0.05,
onchange = function(value)
require("game_handler").set_volume(value)
end,
})
add_setting("Audio", "background_preview_sound_volume", 0, {
min = 0,
max = 1,
step = 0.05,
onchange = function(value)
require("game_handler").set_volume(nil, value)
end,
})
add_setting("General", "preload_all_packs", false)
add_setting("Display", "fps_limit", 200)
add_setting("Gameplay", "official_mode", true, { can_change_in_offical = false, game_version = { 192, 20, 21, 3 } })
Expand Down
14 changes: 9 additions & 5 deletions game_handler/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,16 @@ game_handler.init = async(function(config, audio)
end)

---set music and sound volume (0..1)
---@param music_volume number
---@param sound_volume number
---@param music_volume number?
---@param sound_volume number?
function game_handler.set_volume(music_volume, sound_volume)
music.update_volume(music_volume)
for _, game in pairs(games) do
game.set_volume(sound_volume)
if music_volume then
music.update_volume(music_volume)
end
if sound_volume then
for _, game in pairs(games) do
game.set_volume(sound_volume)
end
end
end

Expand Down
4 changes: 3 additions & 1 deletion ui/elements/entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ function entry:process_event(name, ...)
if text ~= self.text then
self:set_text(text)
if self.change_handler then
self.change_handler(text)
if self.change_handler(text) then
stop_propagation = true
end
end
end
if self.cursor_pos ~= last_cursor_pos then
Expand Down
5 changes: 5 additions & 0 deletions ui/elements/slider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function slider:process_event(name, ...)
end
if name == "mousepressed" and self.is_mouse_over then
self.grabbed = true
require("ui").set_grabbed(self)
select_this_elem()
move_state_to_mouse()
end
Expand All @@ -61,6 +62,10 @@ function slider:process_event(name, ...)
if name == "mousereleased" then
if self.grabbed then
self.grabbed = false
require("ui").set_grabbed(nil)
if self.change_handler then
self.change_handler(self.state)
end
return true
end
end
Expand Down
4 changes: 3 additions & 1 deletion ui/elements/toggle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ function toggle:new(options)
elem.state_indicator_offset:keyframe(0.1, 0)
end
if elem.change_handler then
elem.change_handler(elem.state)
if elem.change_handler(elem.state) then
return true
end
end
end
if obj.state then
Expand Down
19 changes: 19 additions & 0 deletions ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,21 @@ local key_repeat = require("ui.key_repeat")
local ui = {}
local keyboard_navigation = require("ui.keyboard_navigation")
local current_screen
local grabbed_element
local transform = love.math.newTransform()

---set an element to be the only one to receive the next mousereleased event
---@param elem any
function ui.set_grabbed(elem)
grabbed_element = elem
end

---get the currently grabbed element
---@return unknown
function ui.get_grabbed()
return grabbed_element
end

---set gui scale
---@param scale number
function ui.set_scale(scale)
Expand Down Expand Up @@ -88,6 +101,12 @@ function ui.process_event(name, ...)
-- reset scrolled_already value (determines if a container can still scroll, ensures child priority over parent with scrolling (children are processed before parents))
scroll.scrolled_already = false
love.graphics.origin()
if name == "mousereleased" and grabbed_element then
love.graphics.translate(grabbed_element.x, grabbed_element.y)
grabbed_element:process_event(name, ...)
grabbed_element = nil
return
end
if current_screen then
if name == "resize" then
calculate_layout()
Expand Down
5 changes: 5 additions & 0 deletions ui/layout/scroll.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ function scroll:new(element, options)
change_map = {
scale = true,
},
-- position on screen
x = 0,
y = 0,
}, scroll)
obj.element.parent = obj
if options.style then
Expand Down Expand Up @@ -225,6 +228,7 @@ function scroll:process_event(name, ...)
if point_in_scrollbar(self, x, y) then
propagate = false
self.scrollbar_grabbed = true
require("ui").set_grabbed(self)
end
end

Expand Down Expand Up @@ -397,6 +401,7 @@ end

---draw the scroll container with its child
function scroll:draw()
self.x, self.y = love.graphics.transformPoint(0, 0)
if math.floor(self.width) == 0 or math.floor(self.height) == 0 then
-- don't draw anything without having any size
return
Expand Down
10 changes: 10 additions & 0 deletions ui/overlay/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ local function create_setting(name, property, value)
end
setter.change_handler = function(state)
config.set(name, state)
if property.onchange then
if property.onchange(state) then
return true
end
end
end
elseif type(property.default) == "number" then
if property.min and property.max and property.step then
Expand All @@ -62,6 +67,11 @@ local function create_setting(name, property, value)
text.raw_text = property.display_name .. ": " .. state
text.changed = true
layout:mutated()
if property.onchange then
if property.onchange(state) then
return true
end
end
end
else
-- TODO: implement all settings to remove need for placeholder
Expand Down

0 comments on commit e439216

Please sign in to comment.