core/menusystem: add initial version based on my old menusystem
This commit is contained in:
parent
8dda622021
commit
6e7d75f09a
7 changed files with 1086 additions and 0 deletions
165
sonic-boost.love/core/modules/menusystem/flowbox.lua
Normal file
165
sonic-boost.love/core/modules/menusystem/flowbox.lua
Normal file
|
@ -0,0 +1,165 @@
|
|||
local cwd = (...):gsub('%.flowbox$', '') .. "."
|
||||
local Menu = require(cwd .. "menu")
|
||||
|
||||
FlowBox = Menu:extend()
|
||||
|
||||
function FlowBox:new(x,y,w,h,slots_hor,slots_vert)
|
||||
ListBox.super.new(self, x, y, w, h)
|
||||
self.slots = slots_hor * slots_vert
|
||||
self.slots_hor = slots_hor
|
||||
self.slots_vert = slots_vert
|
||||
self.begin = 1
|
||||
self.widgetsH = math.floor( self.h / slots_vert )
|
||||
self.widgetsW = math.floor( self.w / slots_hor )
|
||||
self.h = slots_vert * self.widgetsH -- On fait en sorte que la hauteur
|
||||
self.w = slots_hor * self.widgetsW -- et la largeur
|
||||
-- soit un multiple du nombre de slot et de leur dimensions
|
||||
end
|
||||
|
||||
function FlowBox:update(dt)
|
||||
local col, line = self:getCoord(self.selected)
|
||||
local begincol, beginline = self:getCoord(self.begin)
|
||||
|
||||
if line < beginline then
|
||||
beginline = line
|
||||
end
|
||||
|
||||
if line > beginline + self.slots_vert - 1 then
|
||||
beginline = line - self.slots_vert + 1
|
||||
end
|
||||
|
||||
if beginline < 0 then
|
||||
beginline = 0
|
||||
end
|
||||
|
||||
self.begin = beginline * self.slots_hor + 1
|
||||
end
|
||||
|
||||
function FlowBox:getCoord(id_selected)
|
||||
id_selected = id_selected - 1 -- On simplifie les calcul en prenant 0 comme départ
|
||||
local line, col
|
||||
line = math.floor(id_selected / self.slots_hor)
|
||||
col = id_selected - (line * self.slots_hor)
|
||||
return col, line
|
||||
end
|
||||
|
||||
function FlowBox:moveCursor(new_col, new_line)
|
||||
local col, line = self:getCoord(self.selected)
|
||||
local lastcol, lastline = self:getCoord(#self.listWidget)
|
||||
|
||||
|
||||
if new_line < 0 then
|
||||
new_line = lastline
|
||||
end
|
||||
|
||||
if new_line > lastline then
|
||||
new_line = 0
|
||||
end
|
||||
|
||||
if (new_line == lastline) then
|
||||
if new_col < 0 then
|
||||
new_col = lastcol
|
||||
end
|
||||
|
||||
if new_col > lastcol then
|
||||
if (line == lastline) then
|
||||
new_col = 0
|
||||
else
|
||||
new_col = lastcol
|
||||
end
|
||||
end
|
||||
else
|
||||
if new_col < 0 then
|
||||
new_col = self.slots_hor - 1
|
||||
end
|
||||
|
||||
if new_col == self.slots_hor then
|
||||
new_col = 0
|
||||
end
|
||||
end
|
||||
|
||||
self.selected = (new_line * self.slots_hor) + new_col + 1
|
||||
end
|
||||
|
||||
function FlowBox:keyreleased(key, code)
|
||||
local col, line = self:getCoord(self.selected)
|
||||
if key == 'left' then
|
||||
self:moveCursor(col - 1, line)
|
||||
end
|
||||
|
||||
if key == 'right' then
|
||||
self:moveCursor(col + 1, line)
|
||||
end
|
||||
|
||||
if key == 'up' then
|
||||
self:moveCursor(col, line - 1)
|
||||
end
|
||||
|
||||
if key == 'down' then
|
||||
self:moveCursor(col, line + 1)
|
||||
end
|
||||
|
||||
if key == "A" then
|
||||
self.listWidget[self.selected]:action()
|
||||
end
|
||||
end
|
||||
|
||||
function FlowBox:mousemoved(x, y)
|
||||
local col, line = self:getCoord(self.selected)
|
||||
local begincol, beginline = self:getCoord(self.begin)
|
||||
local newcol, newline
|
||||
|
||||
newline = beginline + math.floor(y / self.widgetsH)
|
||||
newcol = math.floor(x / self.widgetsW)
|
||||
self.selected = (newline * self.slots_hor) + newcol + 1
|
||||
|
||||
if self.selected < 1 then
|
||||
self.selected = 1
|
||||
end
|
||||
if self.selected > #self.listWidget then
|
||||
self.selected = #self.listWidget
|
||||
end
|
||||
end
|
||||
|
||||
function FlowBox:mousepressed(x, y, button, isTouch)
|
||||
local col, line = self:getCoord(self.selected)
|
||||
local begincol, beginline = self:getCoord(self.begin)
|
||||
local newline, newcol
|
||||
|
||||
newline = beginline + math.floor(y / self.widgetsH)
|
||||
newcol = math.floor(x / self.widgetsW)
|
||||
self.selected = (newline * self.slots_hor) + newcol + 1
|
||||
|
||||
if self.selected < 1 then
|
||||
self.selected = 1
|
||||
end
|
||||
if self.selected > #self.listWidget then
|
||||
self.selected = #self.listWidget
|
||||
end
|
||||
|
||||
if #self.listWidget > 0 then
|
||||
self.listWidget[self.selected]:action()
|
||||
end
|
||||
end
|
||||
|
||||
function FlowBox:draw()
|
||||
local widgety = self.y
|
||||
local widgetx = self.x
|
||||
for i,v in ipairs(self.listWidget) do
|
||||
if (i >= self.begin) and (i < self.begin + self.slots) then
|
||||
v:draw(widgetx, widgety, self.widgetsW, self.widgetsH)
|
||||
if self.selected == i and self.focus == true then
|
||||
v:drawSelected(widgetx, widgety, self.widgetsW, self.widgetsH)
|
||||
else
|
||||
v:draw(widgetx, widgety, self.widgetsW, self.widgetsH)
|
||||
end
|
||||
widgetx = widgetx + self.widgetsW
|
||||
if widgetx == (self.x + self.w) then
|
||||
widgetx = self.x
|
||||
widgety = widgety + self.widgetsH
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return FlowBox
|
233
sonic-boost.love/core/modules/menusystem/grid.lua
Normal file
233
sonic-boost.love/core/modules/menusystem/grid.lua
Normal file
|
@ -0,0 +1,233 @@
|
|||
local cwd = (...):gsub('%.grid$', '') .. "."
|
||||
local Menu = require(cwd .. "menu")
|
||||
|
||||
|
||||
GridBox = Menu:extend()
|
||||
|
||||
function GridBox:new(x,y,w,h,slots_hor,slots_vert)
|
||||
ListBox.super.new(self, x, y, w, h)
|
||||
self.slots = slots_hor * slots_vert
|
||||
self.slots_hor = slots_hor
|
||||
self.slots_vert = slots_vert
|
||||
self.begin = 1
|
||||
self.widgetsH = math.floor( self.h / slots_vert )
|
||||
self.widgetsW = math.floor( self.w / slots_hor )
|
||||
self.h = slots_vert * self.widgetsH -- On fait en sorte que la hauteur
|
||||
self.w = slots_hor * self.widgetsW -- et la largeur
|
||||
-- soit un multiple du nombre de slot et de leur dimensions
|
||||
self.cursor = {}
|
||||
self.cursor.x = 0
|
||||
self.cursor.y = 0
|
||||
|
||||
-- La gridbox possède la particularité de pouvoir fusioner des slots, on fait
|
||||
-- donc une liste de slots disponibles, qui serviront par la suite.
|
||||
self.listSlot = {}
|
||||
for i= 1, self.slots do
|
||||
self.listSlot[i] = {}
|
||||
self.listSlot[i].sizeH = 1
|
||||
self.listSlot[i].sizeW = 1
|
||||
self.listSlot[i].isSlave = 0
|
||||
self.listSlot[i].widgetID = i
|
||||
end
|
||||
end
|
||||
|
||||
function GridBox:update(dt)
|
||||
self.begin = 1
|
||||
local slotID = self:getSlotbyCoord(self.cursor.x, self.cursor.y)
|
||||
if self.listSlot[slotID].isSlave > 0 then
|
||||
slotID = self.listSlot[slotID].isSlave
|
||||
end
|
||||
self.selected = self.listSlot[slotID].widgetID
|
||||
self.cursor.x, self.cursor.y = self:getCoord(slotID)
|
||||
end
|
||||
|
||||
function GridBox:regenSlots()
|
||||
local widgetID = 1
|
||||
for i,v in ipairs(self.listSlot) do
|
||||
if v.isSlave == 0 and (widgetID <= #self.listWidget) then
|
||||
self.listSlot[i].widgetID = widgetID
|
||||
widgetID = widgetID + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function GridBox:addCol(slotID)
|
||||
local col, line = self:getCoord(slotID)
|
||||
if (col + self.listSlot[slotID].sizeW + 1) <= self.slots_hor then
|
||||
slotSlave = slotID + self.listSlot[slotID].sizeW
|
||||
for i = 1, self.listSlot[slotID].sizeH do
|
||||
self.listSlot[slotSlave + ((i-1)* self.slots_hor)].isSlave = slotID
|
||||
end
|
||||
self.listSlot[slotID].sizeW = self.listSlot[slotID].sizeW + 1
|
||||
end
|
||||
end
|
||||
|
||||
function GridBox:addLine(slotID)
|
||||
local col, line = self:getCoord(slotID)
|
||||
if (line + self.listSlot[slotID].sizeH + 1) <= self.slots_vert then
|
||||
slotSlave = slotID + (self.listSlot[slotID].sizeH * self.slots_hor)
|
||||
for i = 1, self.listSlot[slotID].sizeW do
|
||||
self.listSlot[slotSlave + (i-1)].isSlave = slotID
|
||||
end
|
||||
self.listSlot[slotID].sizeH = self.listSlot[slotID].sizeH + 1
|
||||
end
|
||||
end
|
||||
|
||||
function GridBox:getCoord(id_selected)
|
||||
id_selected = id_selected - 1 -- On simplifie les calcul en prenant 0 comme départ
|
||||
local line, col
|
||||
line = math.floor(id_selected / self.slots_hor)
|
||||
col = id_selected - (line * self.slots_hor)
|
||||
return col, line
|
||||
end
|
||||
|
||||
function GridBox:getSlotbyCoord(col, line)
|
||||
return (line * self.slots_hor) + col + 1
|
||||
end
|
||||
|
||||
function GridBox:getSlot(widgetID)
|
||||
local slotID
|
||||
for i,v in ipairs(self.listSlot) do
|
||||
if v.widgetID == widgetID then
|
||||
return i
|
||||
end
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
function GridBox:moveCursor(newcol, newline)
|
||||
local col, line = self.cursor.x, self.cursor.y
|
||||
local relcol, relline = newcol - col, newline - line
|
||||
self.cursor.x, self.cursor.y = newcol, newline
|
||||
|
||||
while self.cursor.y < 0 do
|
||||
self.cursor.y = self.cursor.y + self.slots_vert
|
||||
end
|
||||
|
||||
while self.cursor.x < 0 do
|
||||
self.cursor.x = self.cursor.x + self.slots_hor
|
||||
end
|
||||
|
||||
while self.cursor.y >= self.slots_vert do
|
||||
self.cursor.y = self.cursor.y - self.slots_vert
|
||||
end
|
||||
|
||||
while self.cursor.x >= self.slots_hor do
|
||||
self.cursor.x = self.cursor.x - self.slots_hor
|
||||
end
|
||||
previousSlot = self:getSlotbyCoord(col, line)
|
||||
newSlot = self:getSlotbyCoord(self.cursor.x, self.cursor.y)
|
||||
|
||||
if (self.listSlot[newSlot].isSlave > 0) or (self.listSlot[newSlot].widgetID > #self.listWidget) then
|
||||
if (self.listSlot[newSlot].isSlave == previousSlot) or (self.listSlot[newSlot].widgetID > #self.listWidget) then
|
||||
self:moveCursor(self.cursor.x + relcol, self.cursor.y + relline)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function GridBox:keyreleased(key, code)
|
||||
slotID = self:getSlot(self.selected)
|
||||
local col, line = self.cursor.x, self.cursor.y
|
||||
if key == 'left' then
|
||||
--self:moveCol(-1)
|
||||
self:moveCursor(col - 1, line)
|
||||
end
|
||||
|
||||
if key == 'right' then
|
||||
--self:moveCol(1)
|
||||
self:moveCursor(col + 1, line)
|
||||
end
|
||||
|
||||
if key == 'up' then
|
||||
self:moveCursor(col, line - 1)
|
||||
end
|
||||
|
||||
if key == 'down' then
|
||||
self:moveCursor(col, line + 1)
|
||||
end
|
||||
|
||||
if key == "A" and self.selected <= #self.listWidget then
|
||||
self.listWidget[self.selected]:action()
|
||||
end
|
||||
end
|
||||
|
||||
function GridBox:mousemoved(x, y)
|
||||
local col, line = self:getCoord(self.selected)
|
||||
local begincol, beginline = self:getCoord(self.begin)
|
||||
local newcol, newline
|
||||
local newselect, slotID
|
||||
|
||||
newline = beginline + math.floor(y / self.widgetsH)
|
||||
newcol = math.floor(x / self.widgetsW)
|
||||
self.cursor.x = newcol
|
||||
self.cursor.y = newline
|
||||
|
||||
if self.selected < 1 then
|
||||
self.selected = 1
|
||||
end
|
||||
if self.selected > #self.listWidget then
|
||||
self.selected = #self.listWidget
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function GridBox:mousepressed(x, y, button, isTouch)
|
||||
local col, line = self:getCoord(self.selected)
|
||||
local begincol, beginline = self:getCoord(self.begin)
|
||||
local newcol, newline
|
||||
local newselect, slotID
|
||||
|
||||
newline = beginline + math.floor(y / self.widgetsH)
|
||||
newcol = math.floor(x / self.widgetsW)
|
||||
newselect = (newline * self.slots_hor) + newcol + 1
|
||||
|
||||
if self.listSlot[newselect].isSlave > 0 then
|
||||
slotID = self.listSlot[newselect].isSlave
|
||||
else
|
||||
slotID = newselect
|
||||
end
|
||||
|
||||
self.selected = self.listSlot[slotID].widgetID
|
||||
|
||||
if #self.listWidget > 0 and self.selected > 1 and self.selected <= #self.listWidget then
|
||||
self.listWidget[self.selected]:action()
|
||||
end
|
||||
end
|
||||
|
||||
function GridBox:draw()
|
||||
local widgety = self.y
|
||||
local widgetx = self.x
|
||||
|
||||
self:regenSlots() -- On reget les slots au cas où :p
|
||||
for i,v in ipairs(self.listSlot) do
|
||||
if (v.isSlave == 0) and (v.widgetID <= #self.listWidget) then
|
||||
--self.listWidget[v.widgetID]:draw(widgetx, widgety, self.widgetsW * v.sizeW, self.widgetsH * v.sizeH)
|
||||
if self.selected == v.widgetID and self.focus == true then
|
||||
self.listWidget[v.widgetID]:drawSelected(widgetx, widgety, self.widgetsW * v.sizeW, self.widgetsH * v.sizeH)
|
||||
else
|
||||
self.listWidget[v.widgetID]:draw(widgetx, widgety, self.widgetsW * v.sizeW, self.widgetsH * v.sizeH)
|
||||
end
|
||||
end
|
||||
if (v.isSlave > 0) and false then
|
||||
love.graphics.setColor(255,255,255,128)
|
||||
love.graphics.rectangle("fill", widgetx, widgety, self.widgetsW, self.widgetsH)
|
||||
end
|
||||
local col, line = self:getCoord(i)
|
||||
|
||||
if (col == self.cursor.x) and (line == self.cursor.y) and false then
|
||||
love.graphics.setColor(255,255,0,128)
|
||||
love.graphics.rectangle("fill", widgetx, widgety, self.widgetsW, self.widgetsH)
|
||||
end
|
||||
|
||||
--love.graphics.setColor(0,0,0,10)
|
||||
--love.graphics.rectangle("line", widgetx, widgety, self.widgetsW, self.widgetsH)
|
||||
widgetx = widgetx + self.widgetsW
|
||||
if widgetx == (self.x + self.w) then
|
||||
widgetx = self.x
|
||||
widgety = widgety + self.widgetsH
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return GridBox
|
99
sonic-boost.love/core/modules/menusystem/init.lua
Normal file
99
sonic-boost.love/core/modules/menusystem/init.lua
Normal file
|
@ -0,0 +1,99 @@
|
|||
local MenuSystem = {}
|
||||
local MenuController = Object:extend()
|
||||
|
||||
local cwd = (...):gsub('%.init$', '') .. "."
|
||||
|
||||
MenuSystem.Parent = require(cwd .. "menu")
|
||||
MenuSystem.ListBox = require(cwd .. "listbox")
|
||||
MenuSystem.FlowBox = require(cwd .. "flowbox")
|
||||
MenuSystem.Grid = require(cwd .. "grid")
|
||||
MenuSystem.TextMenu = require(cwd .. "textmenu")
|
||||
|
||||
MenuSystem.Widget = require(cwd .. "widgets")
|
||||
|
||||
|
||||
--local VirtualPad = require "modules.virtualpad"
|
||||
|
||||
function MenuController:new()
|
||||
self.menus = {}
|
||||
self.virtualpad = game.input --VirtualPad(1)
|
||||
end
|
||||
|
||||
function MenuController:reset()
|
||||
self.menus = {}
|
||||
self.virtualpad = game.input --VirtualPad(1)
|
||||
end
|
||||
|
||||
function MenuController:addMenu(menu)
|
||||
table.insert(self.menus, menu)
|
||||
end
|
||||
|
||||
function MenuController:update(dt)
|
||||
self:clear()
|
||||
for i,v in ipairs(self.menus) do
|
||||
v.id = i
|
||||
v:update(dt)
|
||||
v:updateWidgets(dt)
|
||||
if v.focus == true then
|
||||
for k,w in pairs(self.virtualpad.keys) do
|
||||
if self.virtualpad.keys[k].isPressed then
|
||||
v:keyreleased(k)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function MenuController:clear()
|
||||
-- On retire les entitées marquées comme supprimées
|
||||
for i,v in ipairs(self.menus) do
|
||||
if (v.destroyed == true) then
|
||||
table.remove(self.menus, i)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function MenuController:updateList()
|
||||
for i,v in ipairs(self.menus) do
|
||||
v.id = i
|
||||
end
|
||||
end
|
||||
|
||||
function MenuController:keyreleased(key, code)
|
||||
-- TODO:depreciated function
|
||||
end
|
||||
|
||||
function MenuController:mousemoved(x, y, dx, dy)
|
||||
for i,v in ipairs(self.menus) do
|
||||
if (x > v.x) and (x < v.x + v.w) and (y > v.y) and (y < v.y + v.h) then
|
||||
v:mousemoved(x - v.x, y - v.y)
|
||||
for j,u in ipairs(self.menus) do
|
||||
u.focus = false
|
||||
end
|
||||
v.focus = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function MenuController:mousepressed( x, y, button, istouch )
|
||||
for i,v in ipairs(self.menus) do
|
||||
if (x > v.x) and (x < v.x + v.w) and (y > v.y) and (y < v.y + v.h) then
|
||||
v:mousepressed(x - v.x, y - v.y, button, istouch )
|
||||
for j,u in ipairs(self.menus) do
|
||||
u.focus = false
|
||||
end
|
||||
v.focus = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function MenuController:draw(dt) -- On dessine les entitées
|
||||
for i,v in ipairs(self.menus) do
|
||||
v.id = i
|
||||
v:draw(dt)
|
||||
end
|
||||
end
|
||||
|
||||
MenuSystem.Controller = MenuController
|
||||
|
||||
return MenuSystem
|
82
sonic-boost.love/core/modules/menusystem/listbox.lua
Normal file
82
sonic-boost.love/core/modules/menusystem/listbox.lua
Normal file
|
@ -0,0 +1,82 @@
|
|||
local cwd = (...):gsub('%.listbox$', '') .. "."
|
||||
local Menu = require(cwd .. "menu")
|
||||
|
||||
ListBox = Menu:extend()
|
||||
|
||||
function ListBox:new(x,y,w,h,slots)
|
||||
ListBox.super.new(self, x, y, w, h)
|
||||
self.slots = slots
|
||||
self.begin = 1
|
||||
self.widgetsH = math.floor( self.h / slots )
|
||||
self.h = slots * self.widgetsH -- On fait en sorte que la hauteur
|
||||
-- soit un multiple du nombre de slot et de leur hauteur
|
||||
end
|
||||
|
||||
function ListBox:update(dt)
|
||||
if self.selected < self.begin then
|
||||
self.begin = self.selected
|
||||
end
|
||||
if self.selected > self.begin + self.slots - 1 then
|
||||
self.begin = self.selected - self.slots + 1
|
||||
end
|
||||
|
||||
if self.begin < 1 then
|
||||
self.begin = 1
|
||||
end
|
||||
end
|
||||
|
||||
function ListBox:keyreleased(key, code)
|
||||
|
||||
if key == 'up' then
|
||||
self:moveCursor(self.selected - 1)
|
||||
end
|
||||
|
||||
if key == 'down' then
|
||||
self:moveCursor(self.selected + 1)
|
||||
end
|
||||
|
||||
if key == "A" then
|
||||
self.listWidget[self.selected]:action()
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function ListBox:mousemoved(x, y)
|
||||
self.selected = self.begin + math.floor(y / self.widgetsH)
|
||||
if self.selected < 1 then
|
||||
self.selected = 1
|
||||
end
|
||||
if self.selected > #self.listWidget then
|
||||
self.selected = #self.listWidget
|
||||
end
|
||||
end
|
||||
|
||||
function ListBox:mousepressed(x, y, button, isTouch)
|
||||
self.selected = self.begin + math.floor(y / self.widgetsH)
|
||||
if self.selected < 1 then
|
||||
self.selected = 1
|
||||
end
|
||||
if self.selected > #self.listWidget then
|
||||
self.selected = #self.listWidget
|
||||
end
|
||||
if #self.listWidget > 0 then
|
||||
self.listWidget[self.selected]:action()
|
||||
end
|
||||
end
|
||||
|
||||
function ListBox:draw()
|
||||
local widgety = self.y
|
||||
for i,v in ipairs(self.listWidget) do
|
||||
if (i >= self.begin) and (i < self.begin + self.slots) then
|
||||
v:draw(self.x, widgety, self.w, self.widgetsH)
|
||||
if self.selected == i and self.focus == true then
|
||||
v:drawSelected(self.x, widgety, self.w, self.widgetsH)
|
||||
else
|
||||
v:draw(self.x, widgety, self.w, self.widgetsH)
|
||||
end
|
||||
widgety = widgety + self.widgetsH
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return ListBox
|
143
sonic-boost.love/core/modules/menusystem/menu.lua
Normal file
143
sonic-boost.love/core/modules/menusystem/menu.lua
Normal file
|
@ -0,0 +1,143 @@
|
|||
local Menu = Object:extend()
|
||||
|
||||
function Menu:new(x,y,w,h)
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.w = w
|
||||
self.h = h
|
||||
self.listWidget = {}
|
||||
self.selected = 0
|
||||
self.selectedPrevious = 0
|
||||
|
||||
self.destroyed = false
|
||||
self.focus = false
|
||||
self.cancel = 0
|
||||
|
||||
self.sound = {}
|
||||
self.sound.asset = nil
|
||||
self.sound.active = false
|
||||
|
||||
self.canvas = {}
|
||||
self.canvas.texture = nil
|
||||
self.canvas.needRedraw = true
|
||||
end
|
||||
|
||||
function Menu:setLastWidgetCancel()
|
||||
self.cancel = #self.listWidget
|
||||
end
|
||||
|
||||
function Menu:cancelAction()
|
||||
if (self.cancel ~= 0) then
|
||||
self.listWidget[self.cancel]:action()
|
||||
end
|
||||
end
|
||||
|
||||
function Menu:update(dt)
|
||||
-- Cette fonction ne contient rien par défaut
|
||||
end
|
||||
|
||||
function Menu:empty()
|
||||
self.listWidget = {}
|
||||
self.cancel = 0
|
||||
end
|
||||
|
||||
function Menu:resize(x,y,w,h)
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.w = w
|
||||
self.h = h
|
||||
end
|
||||
|
||||
function Menu:destroy()
|
||||
self.destroyed = true
|
||||
end
|
||||
|
||||
function Menu:draw()
|
||||
if self.canvas.needRedraw == true then
|
||||
CScreen:cease()
|
||||
self:drawCanvas()
|
||||
self.canvas.needRedraw = false
|
||||
CScreen:apply()
|
||||
end
|
||||
|
||||
love.graphics.draw(self.canvas.texture, self.x, self.y)
|
||||
end
|
||||
|
||||
function Menu:drawCanvas()
|
||||
|
||||
end
|
||||
|
||||
function Menu:keyreleased(key)
|
||||
-- Cette fonction ne contient rien par défaut
|
||||
end
|
||||
|
||||
function Menu:mousemoved(x, y)
|
||||
-- Cette fonction ne contient rien par défaut
|
||||
end
|
||||
|
||||
function Menu:mousepressed( x, y, button, istouch )
|
||||
-- Cette fonction ne contient rien par défaut
|
||||
end
|
||||
|
||||
function Menu:addWidget(newwidget)
|
||||
if #self.listWidget == 0 then
|
||||
self.selected = 1
|
||||
end
|
||||
table.insert(self.listWidget, newwidget)
|
||||
end
|
||||
|
||||
function Menu:updateWidgets(dt)
|
||||
self:clearWidgets()
|
||||
for i,v in ipairs(self.listWidget) do
|
||||
v.id = i
|
||||
v:update(dt)
|
||||
end
|
||||
end
|
||||
|
||||
function Menu:clearWidgets() -- On retire les widgets marquées comme supprimées
|
||||
for i,v in ipairs(self.listWidget) do
|
||||
if (v.destroyed == true) then
|
||||
table.remove(self.listWidget, i)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Menu:setCursor(cursorid)
|
||||
self.selected = cursorid --math.max(1, math.min(cursorid, #self.listWidget))
|
||||
end
|
||||
|
||||
function Menu:moveCursor(new_selected)
|
||||
self:playSelectSound()
|
||||
if new_selected < 1 then
|
||||
self.selected = #self.listWidget + new_selected
|
||||
else
|
||||
if new_selected > #self.listWidget then
|
||||
self.selected = new_selected - #self.listWidget
|
||||
else
|
||||
self.selected = new_selected
|
||||
end
|
||||
end
|
||||
self.canvas.needRedraw = true
|
||||
end
|
||||
|
||||
function Menu:setSound(soundasset)
|
||||
self.sound.active = true
|
||||
self.sound.asset = soundasset
|
||||
end
|
||||
|
||||
function Menu:playSelectSound()
|
||||
if self.sound.active == true then
|
||||
self.sound.asset:setVolume(game.options.data.audio.sfx / 100)
|
||||
love.audio.play( self.sound.asset )
|
||||
end
|
||||
end
|
||||
|
||||
function Menu:resetView()
|
||||
-- ne sert à rien ici, c'est juste pour éviter des crash
|
||||
end
|
||||
|
||||
function Menu:moveView()
|
||||
-- ne sert à rien ici, c'est juste pour éviter des crash
|
||||
end
|
||||
|
||||
return Menu
|
221
sonic-boost.love/core/modules/menusystem/textmenu.lua
Normal file
221
sonic-boost.love/core/modules/menusystem/textmenu.lua
Normal file
|
@ -0,0 +1,221 @@
|
|||
local cwd = (...):gsub('%.textmenu$', '') .. "."
|
||||
local Menu = require(cwd .. "menu")
|
||||
|
||||
local TextMenu = Menu:extend()
|
||||
|
||||
function TextMenu:new(x, y, font, slots)
|
||||
TextMenu.super.new(self, x, y, 0, 0)
|
||||
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()
|
||||
self.widgetsH = self.font:getHeight()
|
||||
self.h = self.widgetsH * self.slots
|
||||
|
||||
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()
|
||||
if self.selected ~= 0 then
|
||||
if self.selected < self.begin then
|
||||
self.begin = self.selected
|
||||
end
|
||||
if self.selected > self.begin + self.slots - 1 then
|
||||
self.begin = self.selected - self.slots + 1
|
||||
end
|
||||
end
|
||||
|
||||
if self.begin < 1 then
|
||||
self.begin = 1
|
||||
end
|
||||
end
|
||||
|
||||
function TextMenu:setWidthAuto()
|
||||
local width = self.w
|
||||
|
||||
for i,v in ipairs(self.listWidget) do
|
||||
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
|
||||
self:moveCursor(self.selected - 1)
|
||||
end
|
||||
|
||||
if key == 'down' then
|
||||
self:moveCursor(self.selected + 1)
|
||||
end
|
||||
|
||||
if key == "A" then
|
||||
if (self.selected > 0) and (self.selected <= #self.listWidget) then
|
||||
self.listWidget[self.selected]:action()
|
||||
end
|
||||
end
|
||||
|
||||
if key == "B" then
|
||||
self:cancelAction()
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function TextMenu:mousemoved(x, y)
|
||||
local selectedPrevous = self.selected
|
||||
self.selected = self.begin + math.floor(y / self.widgetsH)
|
||||
if self.selected < 1 then
|
||||
self.selected = 1
|
||||
end
|
||||
if self.selected > #self.listWidget then
|
||||
self.selected = #self.listWidget
|
||||
end
|
||||
|
||||
if self.selected ~= selectedPrevious then
|
||||
self.canvas.needRedraw = true
|
||||
end
|
||||
end
|
||||
|
||||
function TextMenu:mousepressed(x, y, button, isTouch)
|
||||
self.selected = self.begin + math.floor(y / self.widgetsH)
|
||||
if self.selected < 1 then
|
||||
self.selected = 1
|
||||
end
|
||||
if self.selected > #self.listWidget then
|
||||
self.selected = #self.listWidget
|
||||
end
|
||||
if #self.listWidget > 0 then
|
||||
self.listWidget[self.selected]:action()
|
||||
end
|
||||
|
||||
if self.selected ~= selectedPrevious then
|
||||
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()
|
||||
for i, v in ipairs(self.listWidget) do
|
||||
if (i >= self.begin) and (i < self.begin + self.slots) then
|
||||
self:drawWidget(i, widgety)
|
||||
|
||||
widgety = widgety + self.widgetsH
|
||||
end
|
||||
end
|
||||
utils.draw.resetColor()
|
||||
|
||||
love.graphics.setCanvas( )
|
||||
end
|
||||
|
||||
function TextMenu:drawWidget(widgetID, y)
|
||||
local widget = self.listWidget[widgetID]
|
||||
print(widget)
|
||||
if widget.canvas.needRedraw == true then
|
||||
self:drawWidgetCanvas(widget)
|
||||
widget.canvas.needRedraw = false
|
||||
end
|
||||
|
||||
if self.selected == widgetID and self.focus == true then
|
||||
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)
|
||||
widget.canvas.texture = love.graphics.newCanvas(self.w, self.widgetsH)
|
||||
|
||||
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
|
||||
self.selected = 0
|
||||
|
||||
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()
|
||||
return ((self.begin + self.slots) > (#self.listWidget))
|
||||
end
|
||||
|
||||
return TextMenu
|
143
sonic-boost.love/core/modules/menusystem/widgets/init.lua
Normal file
143
sonic-boost.love/core/modules/menusystem/widgets/init.lua
Normal file
|
@ -0,0 +1,143 @@
|
|||
local Widget = {}
|
||||
|
||||
BaseWidget = Object:extend()
|
||||
DummyWidget = BaseWidget:extend()
|
||||
|
||||
function BaseWidget:new()
|
||||
self.destroyed = false
|
||||
self.selectable = false
|
||||
self.selection_margin = 0
|
||||
self.margin = 2
|
||||
self.label = ""
|
||||
|
||||
self.beginlabel = ""
|
||||
self.endlabel = ""
|
||||
|
||||
self.canvas = {}
|
||||
self.canvas.texture = nil
|
||||
self.canvas.needRedraw = true
|
||||
end
|
||||
|
||||
function BaseWidget:setCanvas(w, h)
|
||||
self.canvas = {}
|
||||
self.canvas.width = w
|
||||
self.canvas.height = h
|
||||
|
||||
self.canvas.selected = {}
|
||||
self.canvas.selected.texture = nil
|
||||
self.canvas.selected.active = false
|
||||
|
||||
self.canvas.base = {}
|
||||
self.canvas.base.texture = nil
|
||||
self.canvas.base.active = false
|
||||
end
|
||||
|
||||
function BaseWidget:selectAction()
|
||||
-- Do nothing
|
||||
end
|
||||
|
||||
function DummyWidget:new()
|
||||
DummyWidget.super.new(self)
|
||||
self.r = love.math.random(128)
|
||||
self.g = love.math.random(128)
|
||||
self.b = love.math.random(128)
|
||||
self.selectable = true
|
||||
self.label = "DUMMY WIDGET (see modules.menus.widget)"
|
||||
end
|
||||
|
||||
function DummyWidget:draw(x, y, w, h)
|
||||
x = x + self.margin
|
||||
y = y + self.margin
|
||||
w = w - self.margin * 2
|
||||
h = h - self.margin * 2
|
||||
|
||||
love.graphics.setColor(self.r,self.g,self.b,70)
|
||||
love.graphics.rectangle("fill", x, y, w, h)
|
||||
love.graphics.setColor(self.r,self.g,self.b)
|
||||
love.graphics.rectangle("line", x, y, w, h)
|
||||
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:draw(x, y, w, h)
|
||||
end
|
||||
|
||||
function BaseWidget:update(dt)
|
||||
-- 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
|
||||
Widget.Dummy = DummyWidget
|
||||
|
||||
return Widget
|
Loading…
Reference in a new issue