2022-08-24 11:18:48 +02:00
|
|
|
local BASE = (...):gsub('slider$', '')
|
2022-08-15 23:41:17 +02:00
|
|
|
|
2022-08-16 00:23:52 +02:00
|
|
|
local Widget = require(BASE..'widget')
|
|
|
|
local core = require(BASE..'core')
|
2022-08-15 23:41:17 +02:00
|
|
|
|
|
|
|
local Slider = setmetatable({}, Widget)
|
|
|
|
Slider.__index = Slider
|
|
|
|
|
|
|
|
|
|
|
|
function Slider.new(args)
|
|
|
|
local self = setmetatable(args, Slider)
|
|
|
|
|
|
|
|
self.color = self.color or core.theme.color
|
|
|
|
self.cornerRadius = self.cornerRadius or core.theme.cornerRadius
|
|
|
|
self.vertical = self.vertical or false
|
|
|
|
self.min = self.min or 0
|
|
|
|
self.max = self.max or 1
|
|
|
|
self.value = self.value or self.min
|
|
|
|
self.step = self.step or (self.max - self.min) / 10
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
function Slider:onPointerInput(px,py, clicked, down)
|
|
|
|
self:grabFocus()
|
|
|
|
if not down then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local x,y,w,h = self.x,self.y,self.w,self.h
|
|
|
|
|
|
|
|
local fraction
|
|
|
|
if self.vertical then
|
|
|
|
fraction = math.min(1, math.max(0, (y+h - py) / h))
|
|
|
|
else
|
|
|
|
fraction = math.min(1, math.max(0, (px - x) / w))
|
|
|
|
end
|
|
|
|
|
|
|
|
local v = fraction*(self.max - self.min) + self.min
|
|
|
|
if v ~= self.value then
|
|
|
|
self.value = v
|
|
|
|
self:onChange(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Slider:onActionInput(action)
|
|
|
|
local up = self.vertical and 'up' or 'right'
|
|
|
|
local down = self.vertical and 'down' or 'left'
|
|
|
|
|
|
|
|
local handled = false
|
|
|
|
if action[up] then
|
|
|
|
self.value = math.min(self.max, self.value + self.step)
|
|
|
|
handled = true
|
|
|
|
elseif action[down] then
|
|
|
|
self.value = math.max(self.min, self.value - self.step)
|
|
|
|
handled = true
|
|
|
|
end
|
|
|
|
if handled then
|
|
|
|
self:onChange(self.value)
|
|
|
|
end
|
|
|
|
|
|
|
|
return handled
|
|
|
|
end
|
|
|
|
|
|
|
|
function Slider:draw()
|
|
|
|
local x,y,w,h = self.x,self.y,self.w,self.h
|
|
|
|
local r = math.min(w,h) / 2.1
|
|
|
|
local c = self:colorForState()
|
|
|
|
local fraction = (self.value - self.min) / (self.max - self.min)
|
|
|
|
|
|
|
|
local xb, yb, wb, hb -- size of the progress bar
|
|
|
|
if self.vertical then
|
|
|
|
x, w = x + w*.25, w*.5
|
|
|
|
xb, yb, wb, hb = x, y+h*(1-fraction), w, h*fraction
|
|
|
|
else
|
|
|
|
y, h = y + h*.25, h*.5
|
|
|
|
xb, yb, wb, hb = x,y, w*fraction, h
|
|
|
|
end
|
|
|
|
|
|
|
|
core.drawBox(x,y,w,h, c, self.cornerRadius)
|
|
|
|
core.drawBox(xb,yb,wb,hb, {bg=c.fg}, self.cornerRadius)
|
|
|
|
|
|
|
|
if self:isFocused() then
|
|
|
|
love.graphics.setColor(c.fg)
|
|
|
|
if self.vertical then
|
|
|
|
love.graphics.circle('fill', x+wb/2, yb, r)
|
|
|
|
else
|
|
|
|
love.graphics.circle('fill', x+wb, yb+hb/2, r)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return Slider
|