suit icon indicating copy to clipboard operation
suit copied to clipboard

The step variable on Slider not work with mouse.

Open Howld opened this issue 6 years ago • 1 comments

                -- keyboard update
		local key_up = opt.vertical and 'up' or 'right'
		local key_down = opt.vertical and 'down' or 'left'
		if core:getPressedKey() == key_up then
			info.value = math.min(info.max, info.value + info.step)
			value_changed = true
		elseif core:getPressedKey() == key_down then
			info.value = math.max(info.min, info.value - info.step)
			value_changed = true
		end

It seems can't use mouse with step variable. And it also not work with keyboard beacuse there is no way to active slider when you want to use keyboard.

Howld avatar Jul 06 '19 14:07 Howld

It's a pretty simple fix, here's what I personally do.

In mouse update, between the declaration of v and the if after it, add the following two lines:

v = math.floor((v/info.step) + 0.5) * info.step
fraction = v / (info.max - info.min) + info.min

Note, the second line is because fraction is passed into the theme to decide where to draw the slider. If you remove it, the slider will always follow the mouse when held, but the value will only update on step intervals, and the slider will snap into place once released. Note: math.floor(x + 0.5) is used to round the value to the nearest position, if you remove the +0.5 it will always snap down.

Here's the full mouse update after the change:

-- mouse update
local mx,my = core:getMousePosition()
if opt.vertical then
	fraction = math.min(1, math.max(0, (y+h - my) / h))
else
	fraction = math.min(1, math.max(0, (mx - x) / w))
end
local v = fraction * (info.max - info.min) + info.min
v = math.floor((v/info.step) + 0.5) * info.step			--Changed line.
fraction = v / (info.max - info.min) + info.min			--Changed line.
if v ~= info.value then
	info.value = v
	value_changed = true
end

ActivexDiamond avatar Jul 07 '20 12:07 ActivexDiamond