-
Notifications
You must be signed in to change notification settings - Fork 0
/
slider.lua
93 lines (69 loc) · 1.71 KB
/
slider.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
Slider = {}
function Slider:new(label,y,t,def,min,max)
local new = {}
setmetatable(new, self)
self.__index = self
if t == "full" then
new.x = width*0.15
new.y = y
new.w = width*0.7
elseif t == "left" then
new.x = width*0.15
new.y = y
new.w = width*0.3
elseif t == "right" then
new.x = width*0.55
new.y = y
new.w = width*0.3
end
new.val = def or 0
new.min = min or 0
new.max = max or 1
new.label = label or ""
new.selected = false
new.hover = false
return new
end
function Slider:checkDist(x,y)
local fr = (self.val - self.min)/(self.max-self.min)
local sx = self.x + self.w*fr
local sy = self.y
local dist = math.sqrt((sx-x)^2 + (sy-y)^2)
if dist < 25 then
return true
end
return false
end
function Slider:mousepressed(x,y)
self.selected = self:checkDist(x,y)
end
function Slider:mousereleased()
self.selected = false
end
function Slider:update()
if self.selected then
local f = (mouseX-self.x)/self.w
--print(f)
self.val = f*(self.max-self.min) + self.min
print(self.val)
end
self.hover = self:checkDist(mouseX,mouseY)
self.val = math.min(math.max(self.val,self.min),self.max)
end
function Slider:draw()
love.graphics.setColor(1,1,1)
love.graphics.line(self.x,self.y,self.x+self.w,self.y)
local c = 0
if self.selected then
c = 1.0
elseif self.hover then
c = 0.4
end
local fr = (self.val - self.min)/(self.max-self.min)
love.graphics.setColor(c,c,c)
love.graphics.circle("fill", self.x + self.w*fr, self.y, 15,30)
love.graphics.setColor(1,1,1)
love.graphics.circle("line", self.x + self.w*fr, self.y, 15, 30)
--love.graphics.print(math.floor(self.val*100)/100, self.x + self.w + 20 , self.y-8)
love.graphics.print(self.label, self.x + 0 , self.y-32)
end