ZLP1/scripts/ui/button.lua

107 lines
2.8 KiB
Lua

interfaceButton = {type = "Button"}
-- centered buttons
function interfaceButton:New(x,y,w,h,table_values,value,style)
o = {}
o.pos = {
x = x,
y = y
}
o.size = {
w = w,
h = h
}
o.values = table_values or {false,true}
o.value = value or 1
o.target_variable = o.values[o.value]
o.clicked = false
o.style = {
text = style.text or nil,
color = style.color or {1,1,1},
color2 = style.color2 or {0,0,0},
--color3 = style.color3 or style.color2 or {0,0,0},
alpha = style.alpha or 1,
scale = style.scale or 1,
scale_x = style.scale_x or 1,
scale_y = style.scale_y or 1,
scale_proportion = 1
}
o.style.unselected = {
scale_proportion = o.style.scale_proportion
}
o.style.selected = {
scale_proportion = 1.5
}
AddElement(o)
setmetatable(o, self)
self.__index = self
return o
end
function interfaceButton:getVariable()
return self.target_variable
end
function AddElement(self)
table.insert(List.UIElement,self)
self.id = #List.UIElement
end
function interfaceButton:checkMouse(mouse_x, mouse_y)
if self.clicked == false
and mouse_x < self.pos.x + self.size.w/2
and mouse_x > self.pos.x - self.size.w/2
and mouse_y < self.pos.y + self.size.h/2
and mouse_y > self.pos.y - self.size.h/2 then
o.style.scale_proportion = o.style.selected.scale_proportion
if love.mouse.isDown(1) then
self.clicked = true
self.value = self.value + 1
if self.value > #self.values then
self.value = 1
end
self.target_variable = self.values[self.value]
end
else
o.style.scale_proportion = o.style.unselected.scale_proportion
self.clicked = false
end
end
function interfaceButton:Draw()
local c1, c2, c3, a = love.graphics.getColor()
love.graphics.setColor(self.style.color[1],self.style.color[2],self.style.color[3],self.style.alpha)
love.graphics.rectangle(
"fill",
self.pos.x-(self.size.w/2)*self.style.scale_x*self.style.scale_proportion,
self.pos.y-(self.size.h/2)*self.style.scale_y*self.style.scale_proportion,
self.size.w *self.style.scale_x*self.style.scale_proportion,
self.size.h *self.style.scale_y*self.style.scale_proportion)
love.graphics.setColor(self.style.color2[1],self.style.color2[2],self.style.color2[3],self.style.alpha)
love.graphics.rectangle(
"line",
self.pos.x-(self.size.w/2)*self.style.scale_x*self.style.scale_proportion,
self.pos.y-(self.size.h/2)*self.style.scale_y*self.style.scale_proportion,
self.size.w *self.style.scale_x*self.style.scale_proportion,
self.size.h *self.style.scale_y*self.style.scale_proportion)
if self.style.text ~= nil then
love.graphics.print(self.style.text,self.pos.x,self.pos.y)
else
love.graphics.print(tostring(self.target_variable),self.pos.x,self.pos.y)
end
love.graphics.setColor(c1,c2,c3,a)
end