2019-02-10 11:56:45 +01:00
|
|
|
local Widget = {}
|
|
|
|
|
|
|
|
BaseWidget = Object:extend()
|
|
|
|
|
2019-02-11 22:37:48 +01:00
|
|
|
function BaseWidget:new(menu)
|
|
|
|
self.menu = menu
|
2019-02-10 21:12:33 +01:00
|
|
|
|
2019-02-10 11:56:45 +01:00
|
|
|
self.destroyed = false
|
|
|
|
self.selectable = false
|
|
|
|
self.selection_margin = 0
|
|
|
|
self.margin = 2
|
|
|
|
|
2019-02-11 22:37:48 +01:00
|
|
|
self:register()
|
|
|
|
|
|
|
|
self.height, self.width = self.menu:getWidgetSize()
|
2019-02-10 11:56:45 +01:00
|
|
|
|
|
|
|
self.canvas = {}
|
|
|
|
self.canvas.texture = nil
|
|
|
|
self.canvas.needRedraw = true
|
2019-02-10 21:12:33 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function BaseWidget:register()
|
2019-02-11 22:37:48 +01:00
|
|
|
self.menu:addWidget(self)
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
2019-02-11 22:37:48 +01:00
|
|
|
function BaseWidget:redrawCanvas()
|
|
|
|
self.canvas.texture = love.graphics.newCanvas(self.width, self.height)
|
|
|
|
love.graphics.setCanvas( self.canvas.texture )
|
2019-02-10 11:56:45 +01:00
|
|
|
|
2019-02-11 22:37:48 +01:00
|
|
|
self:drawCanvas()
|
|
|
|
self.canvas.needRedraw = false
|
2019-02-10 11:56:45 +01:00
|
|
|
|
2019-02-11 22:37:48 +01:00
|
|
|
love.graphics.setCanvas( )
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
2019-02-11 22:37:48 +01:00
|
|
|
function BaseWidget:drawCanvas()
|
|
|
|
self.r = love.math.random(128)/256
|
|
|
|
self.g = love.math.random(128)/256
|
|
|
|
self.b = love.math.random(128)/256
|
2019-02-10 11:56:45 +01:00
|
|
|
|
2019-02-11 22:37:48 +01:00
|
|
|
love.graphics.setColor(self.r, self.g, self.b, 70)
|
|
|
|
love.graphics.rectangle("fill", 0, 0, self.width, self.height)
|
|
|
|
love.graphics.setColor(self.r, self.g, self.b)
|
|
|
|
love.graphics.rectangle("line", 0, 0, self.width, self.height)
|
|
|
|
utils.graphics.resetColor()
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
2019-02-11 22:37:48 +01:00
|
|
|
function BaseWidget:selectAction()
|
|
|
|
-- Do nothing
|
|
|
|
end
|
2019-02-10 11:56:45 +01:00
|
|
|
|
2019-02-11 22:37:48 +01:00
|
|
|
function BaseWidget:draw(x, y)
|
|
|
|
if self.canvas.texture ~= nil then
|
|
|
|
utils.graphics.resetColor()
|
|
|
|
love.graphics.draw(self.canvas.texture, x, y)
|
|
|
|
end
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function BaseWidget:drawSelected(x,y,w,h)
|
|
|
|
self:draw(x, y, w, h)
|
|
|
|
x = x + self.selection_margin
|
|
|
|
y = y + self.selection_margin
|
|
|
|
w = w - self.selection_margin * 2
|
|
|
|
h = h - self.selection_margin * 2
|
|
|
|
|
|
|
|
love.graphics.setColor(0,0,0)
|
|
|
|
love.graphics.rectangle("fill", x, y, 4, 8)
|
|
|
|
love.graphics.rectangle("fill", x, y, 8, 4)
|
|
|
|
|
|
|
|
love.graphics.rectangle("fill", x + w, y, -4, 8)
|
|
|
|
love.graphics.rectangle("fill", x + w, y, -8, 4)
|
|
|
|
|
|
|
|
love.graphics.rectangle("fill", x, y + h, 4, -8)
|
|
|
|
love.graphics.rectangle("fill", x, y + h, 8, -4)
|
|
|
|
|
|
|
|
love.graphics.rectangle("fill", x + w, y + h, -4, -8)
|
|
|
|
love.graphics.rectangle("fill", x + w, y + h, -8, -4)
|
|
|
|
|
|
|
|
love.graphics.setColor(255,255,255)
|
|
|
|
love.graphics.rectangle("fill", x + 1, y + 1, 2, 6)
|
|
|
|
love.graphics.rectangle("fill", x + 1, y + 1, 6, 2)
|
|
|
|
|
|
|
|
love.graphics.rectangle("fill", x + w - 1, y + 1, -2, 6)
|
|
|
|
love.graphics.rectangle("fill", x + w - 1, y + 1, -6, 2)
|
|
|
|
|
|
|
|
love.graphics.rectangle("fill", x + 1, y + h - 1, 2, -6)
|
|
|
|
love.graphics.rectangle("fill", x + 1, y + h - 1, 6, -2)
|
|
|
|
|
|
|
|
love.graphics.rectangle("fill", x + w - 1, y + h - 1, -2, -6)
|
|
|
|
love.graphics.rectangle("fill", x + w - 1, y + h - 1, -6, -2)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
function BaseWidget:update(dt)
|
2019-02-11 22:37:48 +01:00
|
|
|
if (self.canvas.needRedraw) then
|
|
|
|
self:redrawCanvas()
|
|
|
|
end
|
2019-02-10 11:56:45 +01:00
|
|
|
-- N/A
|
|
|
|
end
|
|
|
|
|
|
|
|
function BaseWidget:action()
|
|
|
|
self:destroy()
|
|
|
|
end
|
|
|
|
|
|
|
|
function BaseWidget:destroy()
|
|
|
|
self.destroyed = true
|
|
|
|
end
|
|
|
|
|
|
|
|
function drawWidget_selected(x, y, w, h)
|
|
|
|
love.graphics.setColor(0,0,0)
|
|
|
|
love.graphics.rectangle("fill", x, y, 4, 8)
|
|
|
|
love.graphics.rectangle("fill", x, y, 8, 4)
|
|
|
|
|
|
|
|
love.graphics.rectangle("fill", x + w, y, -4, 8)
|
|
|
|
love.graphics.rectangle("fill", x + w, y, -8, 4)
|
|
|
|
|
|
|
|
love.graphics.rectangle("fill", x, y + h, 4, -8)
|
|
|
|
love.graphics.rectangle("fill", x, y + h, 8, -4)
|
|
|
|
|
|
|
|
love.graphics.rectangle("fill", x + w, y + h, -4, -8)
|
|
|
|
love.graphics.rectangle("fill", x + w, y + h, -8, -4)
|
|
|
|
|
|
|
|
love.graphics.setColor(255,255,255)
|
|
|
|
love.graphics.rectangle("fill", x + 1, y + 1, 2, 6)
|
|
|
|
love.graphics.rectangle("fill", x + 1, y + 1, 6, 2)
|
|
|
|
|
|
|
|
love.graphics.rectangle("fill", x + w - 1, y + 1, -2, 6)
|
|
|
|
love.graphics.rectangle("fill", x + w - 1, y + 1, -6, 2)
|
|
|
|
|
|
|
|
love.graphics.rectangle("fill", x + 1, y + h - 1, 2, -6)
|
|
|
|
love.graphics.rectangle("fill", x + 1, y + h - 1, 6, -2)
|
|
|
|
|
|
|
|
love.graphics.rectangle("fill", x + w - 1, y + h - 1, -2, -6)
|
|
|
|
love.graphics.rectangle("fill", x + w - 1, y + h - 1, -6, -2)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
Widget.Base = BaseWidget
|
|
|
|
|
|
|
|
return Widget
|