Files
alchemist-accessibility/accessibility/patches/slider.lua
T

68 lines
2.2 KiB
Lua
Raw Normal View History

-- Patch: modules/slider.lua
--
-- The slider is genuinely generic: `modules/slider.lua` is a factory
-- (`newSlider(params)`) and every quantity slider in the game comes from it.
-- Four states use it:
--
-- craft -- how many items to process
-- refine -- same
-- trade -- how many to buy/sell
-- ingame_wait -- how many turns to wait
--
-- So one wrapper on the factory narrates all four, with no per-state work. This
-- is the same leverage as the console.print and inventoryCursorPopup patches.
--
-- NOTE: the options screens (config_menu / settings_menu) do NOT use this. They
-- adjust values through config.increase / config.decrease instead, and are
-- handled by accessibility/states/config_menu.lua.
-- ORDERING: this replaces package.loaded, so it only affects requires that happen
-- AFTER it runs. That is safe here because states are loaded lazily by states.lua
-- (each `require("states/"..name)` happens on first switch), and these four are
-- the only requirers. applyPatches() also runs before the self-test, which is the
-- one thing that loads state files early.
local speech = require("accessibility.speech")
return function()
local newSlider = require("modules.slider")
if type(newSlider) ~= "function" then
error("modules.slider is not a factory function -- game structure changed")
end
package.loaded["modules.slider"] = function(params)
local slider = newSlider(params)
local realSetValue = slider.setValue
if not realSetValue then
return slider
end
-- Announce only on an actual change. setValue is called during
-- construction, on every frame of a mouse drag, and on each repeat of a
-- held key -- speaking unconditionally would stutter badly.
local lastSpoken = slider.currentValue
slider.setValue = function(newValue, noFlip)
local result = realSetValue(newValue, noFlip)
pcall(function()
local current = slider.currentValue
if current == lastSpoken then
return
end
lastSpoken = current
-- Interrupt: the value is the answer to the key just pressed, and
-- a backlog of intermediate numbers is worse than useless.
speech.say(tostring(current), true)
end)
return result
end
return slider
end
end