Skip to content

Commit

Permalink
Add lerp function and simple usages
Browse files Browse the repository at this point in the history
  • Loading branch information
k-bk committed Dec 3, 2019
1 parent df77f78 commit 293eb2b
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions UI.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function draw_slider(e, x, y)
if state == "pressed" then
local new_percent = (UI.position.x - x - UI.margin) / e.width
new_percent = clamp(0, 1, new_percent)
e.set_value(new_percent * (e.max - e.min) + e.min)
e.set_value(lerp(e.min, e.max, new_percent))
end

-- draw labels with minimum and maximum
Expand All @@ -127,9 +127,9 @@ function draw_slider(e, x, y)
UI.margin, UI.corner, UI.corner)

y = y + 2 * UI.margin
for percent = 0, 1, 0.2 do
for percent = 0, 1, 1/5 do
local offset = UI.margin + e.width * percent
local val = e.min * (1 - percent) + e.max * percent
local val = lerp(e.min, e.max, percent)
color(c.border)
love.graphics.line(x + offset, y - UI.margin, x + offset, y)
color(c.text.normal)
Expand Down Expand Up @@ -289,6 +289,10 @@ function clamp ( min, max, value )
return value
end

function lerp ( min, max, percent )
return min * (1 - percent) + max * percent
end

function draw_frame(conf)
local x, y = conf.x, conf.y
local w, h = conf.width, conf.height
Expand Down

0 comments on commit 293eb2b

Please sign in to comment.