Files
PathOfBuilding/Classes/ButtonControl.lua
Openarl bae83782f4 Major update; nearing release
Changes:
 - Literally everything
 - Everything else too
2016-08-29 21:52:56 +10:00

77 lines
1.8 KiB
Lua

-- Path of Building
--
-- Class: Button Control
-- Basic button control.
--
local launch, main = ...
local ButtonClass = common.NewClass("ButtonControl", "Control", function(self, anchor, x, y, width, height, label, onClick)
self.Control(anchor, x, y, width, height)
self.label = label
self.onClick = onClick
end)
function ButtonClass:IsMouseOver()
if not self:IsShown() then
return false
end
local x, y = self:GetPos()
local width, height = self:GetSize()
local cursorX, cursorY = GetCursorPos()
return cursorX >= x and cursorY >= y and cursorX < x + width and cursorY < y + height
end
function ButtonClass:Draw()
local x, y = self:GetPos()
local width, height = self:GetSize()
local enabled = self:IsEnabled()
local mOver = self:IsMouseOver()
local locked = self:GetProperty("locked")
if not enabled then
SetDrawColor(0.33, 0.33, 0.33)
elseif mOver or locked then
SetDrawColor(1, 1, 1)
else
SetDrawColor(0.5, 0.5, 0.5)
end
DrawImage(nil, x, y, width, height)
if not enabled then
SetDrawColor(0, 0, 0)
elseif self.clicked and mOver then
SetDrawColor(0.5, 0.5, 0.5)
elseif mOver or locked then
SetDrawColor(0.33, 0.33, 0.33)
else
SetDrawColor(0, 0, 0)
end
DrawImage(nil, x + 1, y + 1, width - 2, height - 2)
if enabled then
SetDrawColor(1, 1, 1)
else
SetDrawColor(0.33, 0.33, 0.33)
end
DrawString(x + width / 2, y + 2, "CENTER_X", height - 4, "VAR", self:GetProperty("label"))
end
function ButtonClass:OnKeyDown(key)
if not self:IsShown() or not self:IsEnabled() then
return
end
if key == "LEFTBUTTON" then
self.clicked = true
end
return self
end
function ButtonClass:OnKeyUp(key)
if not self:IsShown() or not self:IsEnabled() then
return
end
if key == "LEFTBUTTON" then
if self:IsMouseOver() then
self.onClick()
end
end
self.clicked = false
end