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

The step variable on Slider not work with mouse. #77

Open
Howld opened this issue Jul 6, 2019 · 1 comment
Open

The step variable on Slider not work with mouse. #77

Howld opened this issue Jul 6, 2019 · 1 comment

Comments

@Howld
Copy link

Howld commented Jul 6, 2019

                -- 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.

@ActivexDiamond
Copy link

ActivexDiamond commented Jul 7, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants