Files
PathOfBuilding/Classes/ButtonControl.lua
2016-10-31 16:01:27 +10:00

90 lines
2.5 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
local label = self:GetProperty("label")
if label == "+" then
DrawImage(nil, x + width * 0.2, y + height * 0.45, width * 0.6, height * 0.1)
DrawImage(nil, x + width * 0.45, y + height * 0.2, width * 0.1, height * 0.6)
elseif label == "-" then
DrawImage(nil, x + width * 0.2, y + height * 0.45, width * 0.6, height * 0.1)
elseif label == "x" then
DrawImageQuad(nil, x + width * 0.1, y + height * 0.2, x + width * 0.2, y + height * 0.1, x + width * 0.9, y + height * 0.8, x + width * 0.8, y + height * 0.9)
DrawImageQuad(nil, x + width * 0.8, y + height * 0.1, x + width * 0.9, y + height * 0.2, x + width * 0.2, y + height * 0.9, x + width * 0.1, y + height * 0.8)
else
local overSize = self.overSizeText or 0
DrawString(x + width / 2, y + 2 - overSize, "CENTER_X", height - 4 + overSize * 2, "VAR",label )
end
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" and self.clicked then
self.clicked = false
if self:IsMouseOver() then
return self.onClick()
end
end
self.clicked = false
end