66 lines
1.6 KiB
Lua
66 lines
1.6 KiB
Lua
local Object = require "classic"
|
|
local Slider = Object:extend()
|
|
|
|
active = false
|
|
|
|
function Slider:new(x, y, w, h, limit)
|
|
self.x = x or 0
|
|
self.y = y or 0
|
|
self.w = w or 0
|
|
self.h = h or 0
|
|
self.knobw = 8
|
|
self.knobh = 16
|
|
self.knobx = self.x
|
|
self.knoby = self.y
|
|
self.active = false
|
|
self.level = 0
|
|
self.limit = 255
|
|
end
|
|
|
|
function round(n)
|
|
return math.floor(n + 0.5)
|
|
end
|
|
|
|
function Slider:update(dt)
|
|
mousex, mousey = love.mouse.getPosition()
|
|
self.level = round(((self.knobx-self.x)/self.w)*255)
|
|
|
|
if self.level < 0 then
|
|
self.knobx = self.x
|
|
self.level = 0
|
|
elseif self.level > 255 then
|
|
self.knobx = self.x+self.w
|
|
self.level = 255
|
|
end
|
|
|
|
if love.mouse.isDown(1)
|
|
and mousex >= self.x
|
|
and mousex <= self.x+self.w
|
|
and mousey >= self.y
|
|
and mousey <= self.y+self.h
|
|
and not active
|
|
and self.active == false then
|
|
self.active = true
|
|
active = true
|
|
elseif love.mouse.isDown(1) == false then
|
|
self.active = false
|
|
active = false
|
|
end
|
|
if self.active then
|
|
self.knobx = round(
|
|
(math.min (math.max(self.x, mousex), self.x+self.w)
|
|
)/(self.w/255)
|
|
)*(self.w/255)
|
|
end
|
|
end
|
|
|
|
function Slider:draw()
|
|
love.graphics.setColor(200/255, 200/255, 200/255)
|
|
love.graphics.rectangle("line", self.x, self.y, self.w, self.h)
|
|
love.graphics.setColor(237/255, 86/255, 83/255)
|
|
love.graphics.rectangle("fill", self.knobx-(self.knobw/2), self.knoby-(self.knobh/3), self.knobw, self.knobh)
|
|
love.graphics.setColor(1, 1, 1)
|
|
love.graphics.print(self.level, self.x-32, self.y-self.h/2)
|
|
end
|
|
|
|
return Slider
|