From e439216eee5a384f527b731489b9a3ba8354581a Mon Sep 17 00:00:00 2001 From: Bauumm Date: Sun, 5 Nov 2023 12:08:29 +0100 Subject: [PATCH] make settings take effect immediately --- config.lua | 38 ++++++++++++++++++++++++++++++++++---- game_handler/init.lua | 14 +++++++++----- ui/elements/entry.lua | 4 +++- ui/elements/slider.lua | 5 +++++ ui/elements/toggle.lua | 4 +++- ui/init.lua | 19 +++++++++++++++++++ ui/layout/scroll.lua | 5 +++++ ui/overlay/settings.lua | 10 ++++++++++ 8 files changed, 88 insertions(+), 11 deletions(-) diff --git a/config.lua b/config.lua index 9f8a7c0..de53228 100644 --- a/config.lua +++ b/config.lua @@ -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 } }) diff --git a/game_handler/init.lua b/game_handler/init.lua index 4e2aa45..abad501 100644 --- a/game_handler/init.lua +++ b/game_handler/init.lua @@ -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 diff --git a/ui/elements/entry.lua b/ui/elements/entry.lua index bd259f9..7652702 100644 --- a/ui/elements/entry.lua +++ b/ui/elements/entry.lua @@ -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 diff --git a/ui/elements/slider.lua b/ui/elements/slider.lua index 63f0d5d..4ba899a 100644 --- a/ui/elements/slider.lua +++ b/ui/elements/slider.lua @@ -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 @@ -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 diff --git a/ui/elements/toggle.lua b/ui/elements/toggle.lua index 63a9b6b..2df32a0 100644 --- a/ui/elements/toggle.lua +++ b/ui/elements/toggle.lua @@ -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 diff --git a/ui/init.lua b/ui/init.lua index f64fb35..81cb23a 100644 --- a/ui/init.lua +++ b/ui/init.lua @@ -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) @@ -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() diff --git a/ui/layout/scroll.lua b/ui/layout/scroll.lua index 1ff9bd8..13aabac 100644 --- a/ui/layout/scroll.lua +++ b/ui/layout/scroll.lua @@ -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 @@ -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 @@ -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 diff --git a/ui/overlay/settings.lua b/ui/overlay/settings.lua index 53d8c1c..ffbc279 100644 --- a/ui/overlay/settings.lua +++ b/ui/overlay/settings.lua @@ -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 @@ -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