2019-02-10 11:56:45 +01:00
|
|
|
local cwd = (...):gsub('%.textmenu$', '') .. "."
|
2019-02-10 12:00:02 +01:00
|
|
|
local Menu = require(cwd .. "parent")
|
2019-02-10 11:56:45 +01:00
|
|
|
|
|
|
|
local TextMenu = Menu:extend()
|
|
|
|
|
2019-02-10 21:14:31 +01:00
|
|
|
function TextMenu:new(controller, x, y, font, slots)
|
|
|
|
TextMenu.super.new(self, controller, x, y, 0, 0)
|
2019-02-10 11:56:45 +01:00
|
|
|
self.ox = x
|
|
|
|
self.oy = y
|
|
|
|
self.font = font
|
|
|
|
self.align = "left"
|
|
|
|
|
|
|
|
self.slots = slots
|
|
|
|
self.begin = 1
|
|
|
|
|
|
|
|
self:getBoundingBox()
|
|
|
|
end
|
|
|
|
|
|
|
|
function TextMenu:getBoundingBox()
|
|
|
|
self:setWidthAuto()
|
2019-02-10 14:34:04 +01:00
|
|
|
self.widget.h = self.font:getHeight()
|
|
|
|
self.h = self.widget.h * self.slots
|
2019-02-10 11:56:45 +01:00
|
|
|
|
|
|
|
if self.align == "right" then
|
|
|
|
self.x = self.ox - self.w
|
|
|
|
elseif self.align == "center" then
|
|
|
|
self.x = self.ox - self.w / 2
|
|
|
|
else
|
|
|
|
self.x = self.ox
|
|
|
|
end
|
|
|
|
|
|
|
|
self.y = self.oy
|
|
|
|
end
|
|
|
|
|
|
|
|
function TextMenu:centerText()
|
|
|
|
self:setAlign("center")
|
|
|
|
end
|
|
|
|
|
|
|
|
function TextMenu:setAlign(align)
|
|
|
|
self.align = align
|
|
|
|
self.font:setAlign("center")
|
|
|
|
end
|
|
|
|
|
|
|
|
function TextMenu:update(dt)
|
|
|
|
self:getBoundingBox()
|
2019-02-10 14:28:08 +01:00
|
|
|
if self.widget.selected ~= 0 then
|
|
|
|
if self.widget.selected < self.begin then
|
|
|
|
self.begin = self.widget.selected
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
2019-02-10 14:28:08 +01:00
|
|
|
if self.widget.selected > self.begin + self.slots - 1 then
|
|
|
|
self.begin = self.widget.selected - self.slots + 1
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.begin < 1 then
|
|
|
|
self.begin = 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function TextMenu:setWidthAuto()
|
|
|
|
local width = self.w
|
|
|
|
|
2019-02-10 14:28:08 +01:00
|
|
|
for i,v in ipairs(self.widget.list) do
|
2019-02-10 11:56:45 +01:00
|
|
|
local stringWidth = self.font:getWidth(v.beginlabel .. v.label .. v.endlabel)
|
|
|
|
width = math.max(stringWidth, width)
|
|
|
|
end
|
|
|
|
if width ~= self.w then
|
|
|
|
self.canvas.needRedraw = true
|
|
|
|
end
|
|
|
|
self.w = width
|
|
|
|
end
|
|
|
|
|
|
|
|
function TextMenu:getWidth()
|
|
|
|
self:setWidthAuto()
|
|
|
|
return self.w
|
|
|
|
end
|
|
|
|
|
|
|
|
function TextMenu:getHeight()
|
|
|
|
return self.h
|
|
|
|
end
|
|
|
|
|
|
|
|
function TextMenu:keyreleased(key, code)
|
|
|
|
|
|
|
|
if key == 'up' then
|
2019-02-10 14:28:08 +01:00
|
|
|
self:moveCursor(self.widget.selected - 1)
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if key == 'down' then
|
2019-02-10 14:28:08 +01:00
|
|
|
self:moveCursor(self.widget.selected + 1)
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if key == "A" then
|
2019-02-10 14:28:08 +01:00
|
|
|
if (self.widget.selected > 0) and (self.widget.selected <= #self.widget.list) then
|
|
|
|
self.widget.list[self.widget.selected]:action()
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if key == "B" then
|
|
|
|
self:cancelAction()
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
function TextMenu:mousemoved(x, y)
|
2019-02-10 14:28:08 +01:00
|
|
|
local selectedPrevous = self.widget.selected
|
2019-02-10 14:34:04 +01:00
|
|
|
self.widget.selected = self.begin + math.floor(y / self.widget.h)
|
2019-02-10 14:28:08 +01:00
|
|
|
if self.widget.selected < 1 then
|
|
|
|
self.widget.selected = 1
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
2019-02-10 14:28:08 +01:00
|
|
|
if self.widget.selected > #self.widget.list then
|
|
|
|
self.widget.selected = #self.widget.list
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
2019-02-10 14:28:08 +01:00
|
|
|
if self.widget.selected ~= selectedPrevious then
|
2019-02-10 11:56:45 +01:00
|
|
|
self.canvas.needRedraw = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function TextMenu:mousepressed(x, y, button, isTouch)
|
2019-02-10 14:34:04 +01:00
|
|
|
self.widget.selected = self.begin + math.floor(y / self.widget.h)
|
2019-02-10 14:28:08 +01:00
|
|
|
if self.widget.selected < 1 then
|
|
|
|
self.widget.selected = 1
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
2019-02-10 14:28:08 +01:00
|
|
|
if self.widget.selected > #self.widget.list then
|
|
|
|
self.widget.selected = #self.widget.list
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
2019-02-10 14:28:08 +01:00
|
|
|
if #self.widget.list > 0 then
|
|
|
|
self.widget.list[self.widget.selected]:action()
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
2019-02-10 14:28:08 +01:00
|
|
|
if self.widget.selected ~= selectedPrevious then
|
2019-02-10 11:56:45 +01:00
|
|
|
self.canvas.needRedraw = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function TextMenu:drawCanvas()
|
|
|
|
print("redraw menu")
|
|
|
|
|
|
|
|
self.canvas.texture = love.graphics.newCanvas(self.w, self.h)
|
|
|
|
love.graphics.setCanvas( self.canvas.texture )
|
|
|
|
|
|
|
|
local ox, x
|
|
|
|
|
|
|
|
local widgety = 0
|
|
|
|
local ox = self.w / 2
|
|
|
|
local x = 0
|
|
|
|
|
|
|
|
self.font:set()
|
2019-02-10 14:28:08 +01:00
|
|
|
for i, v in ipairs(self.widget.list) do
|
2019-02-10 11:56:45 +01:00
|
|
|
if (i >= self.begin) and (i < self.begin + self.slots) then
|
|
|
|
self:drawWidget(i, widgety)
|
|
|
|
|
2019-02-10 14:34:04 +01:00
|
|
|
widgety = widgety + self.widget.h
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
utils.draw.resetColor()
|
|
|
|
|
|
|
|
love.graphics.setCanvas( )
|
|
|
|
end
|
|
|
|
|
|
|
|
function TextMenu:drawWidget(widgetID, y)
|
2019-02-10 14:28:08 +01:00
|
|
|
local widget = self.widget.list[widgetID]
|
2019-02-10 11:56:45 +01:00
|
|
|
print(widget)
|
|
|
|
if widget.canvas.needRedraw == true then
|
|
|
|
self:drawWidgetCanvas(widget)
|
|
|
|
widget.canvas.needRedraw = false
|
|
|
|
end
|
|
|
|
|
2019-02-10 14:28:08 +01:00
|
|
|
if self.widget.selected == widgetID and self.focus == true then
|
2019-02-10 11:56:45 +01:00
|
|
|
love.graphics.setColor(1, 1, 0, 1)
|
|
|
|
else
|
|
|
|
love.graphics.setColor(1, 1, 1, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
love.graphics.draw(widget.canvas.texture, 0, y)
|
|
|
|
end
|
|
|
|
|
|
|
|
function TextMenu:drawWidgetCanvas(widget)
|
2019-02-10 14:34:04 +01:00
|
|
|
widget.canvas.texture = love.graphics.newCanvas(self.w, self.widget.h)
|
2019-02-10 11:56:45 +01:00
|
|
|
|
|
|
|
love.graphics.setCanvas( widget.canvas.texture )
|
|
|
|
|
|
|
|
self.font:draw(widget.label, math.floor(self.w / 2), 0, -1, self.align)
|
|
|
|
self.font:draw(widget.beginlabel, math.floor(0), 0, -1, "left")
|
|
|
|
self.font:draw(widget.endlabel, math.floor(self.w), 0, -1, "right")
|
|
|
|
|
|
|
|
love.graphics.setCanvas( self.canvas.texture )
|
|
|
|
end
|
|
|
|
|
|
|
|
function TextMenu:resetView()
|
|
|
|
self.begin = 1
|
|
|
|
self.canvas.needRedraw = true
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:moveView(begin, absolute)
|
|
|
|
--local absolute = absolute or true
|
2019-02-10 14:28:08 +01:00
|
|
|
self.widget.selected = 0
|
2019-02-10 11:56:45 +01:00
|
|
|
|
|
|
|
if (absolute) then
|
|
|
|
self.begin = begin
|
|
|
|
else
|
|
|
|
self.begin = self.begin + begin
|
|
|
|
end
|
|
|
|
-- ne sert à rien ici, c'est juste pour éviter des crash
|
|
|
|
|
|
|
|
self.canvas.needRedraw = true
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:getView()
|
|
|
|
return self.begin
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:isViewAtBeggining()
|
|
|
|
return (self.begin <= 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:isViewAtEnd()
|
2019-02-10 14:28:08 +01:00
|
|
|
return ((self.begin + self.slots) > (#self.widget.list))
|
2019-02-10 11:56:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return TextMenu
|