98 lines
2 KiB
Lua
98 lines
2 KiB
Lua
local Widget = {}
|
|
|
|
BaseWidget = Object:extend()
|
|
TextWidget = BaseWidget:extend()
|
|
|
|
function BaseWidget:new(menu)
|
|
self.menu = menu
|
|
|
|
self.destroyed = false
|
|
self.selectable = false
|
|
self.selection_margin = 0
|
|
self.margin = 2
|
|
|
|
self:register()
|
|
|
|
self.canvas = {}
|
|
self.canvas.texture = nil
|
|
self.canvas.needRedraw = true
|
|
end
|
|
|
|
function BaseWidget:register()
|
|
self.menu:addWidget(self)
|
|
end
|
|
|
|
function BaseWidget:redrawCanvas()
|
|
self.width, self.height = self.menu:getWidgetSize(self.id)
|
|
|
|
self.canvas.texture = love.graphics.newCanvas(self.width, self.height)
|
|
love.graphics.setCanvas( self.canvas.texture )
|
|
|
|
self:drawCanvas()
|
|
self.canvas.needRedraw = false
|
|
|
|
love.graphics.setCanvas( )
|
|
end
|
|
|
|
function BaseWidget:drawCanvas()
|
|
self.r = love.math.random(128)/256
|
|
self.g = love.math.random(128)/256
|
|
self.b = love.math.random(128)/256
|
|
|
|
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()
|
|
end
|
|
|
|
function BaseWidget:selectAction()
|
|
-- Do nothing
|
|
end
|
|
|
|
function BaseWidget:draw(x, y)
|
|
if self.canvas.texture ~= nil then
|
|
utils.graphics.resetColor()
|
|
love.graphics.draw(self.canvas.texture, x, y)
|
|
end
|
|
end
|
|
|
|
function BaseWidget:drawSelected(x,y,w,h)
|
|
self:draw(x, y, w, h)
|
|
end
|
|
|
|
function BaseWidget:update(dt)
|
|
if (self.canvas.needRedraw) then
|
|
self:redrawCanvas()
|
|
end
|
|
-- N/A
|
|
end
|
|
|
|
function BaseWidget:action(source)
|
|
--self:destroy()
|
|
end
|
|
|
|
function BaseWidget:destroy()
|
|
self.destroyed = true
|
|
end
|
|
|
|
-- Simple text widget
|
|
|
|
function TextWidget:new(menu, font, label)
|
|
TextWidget.super.new(self, menu)
|
|
self.font = font
|
|
self.label = label
|
|
end
|
|
|
|
function TextWidget:drawCanvas()
|
|
local w, h
|
|
w = math.floor(self.width / 2)
|
|
h = math.floor(self.height / 2) - (self.font:getHeight() / 2)
|
|
self.font:draw(self.label, w, h, -1, "center")
|
|
end
|
|
|
|
|
|
Widget.Base = BaseWidget
|
|
Widget.Text = TextWidget
|
|
|
|
return Widget
|