Hello!
I've come back to an old project and I'm running into issues with a gridview object updating properly. I've copied the object into its own lua file to debug it, and the problem behavior exhibits itself there.
The issue I'm having is that my object's "needsDisplay" is not updating the cells drawn to my list-style gridview. In working examples of the gridview object, "needsDisplay" is triggered multiple times per update, whereas mine only updates once. This seems to prevent scrolling, which is undesired. My intention is to have
Either I am missing a parameter or my update scheme is preventing the gridview from scrolling.
My code is here:
import 'assets/gridview.lua'
import 'assets/nineslice'
--Font setting
local dFont = playdate.graphics.font.new('assets/DBLSW2')
playdate.graphics.setFont(dFont)
--Menu Object Table
menuIndex = {}
--Handles Input Depending on Context. Simplified to One Context In This Example
function menuInputContext(string)
if string == "b" then
--intentionally left blank
elseif string == "a" then --access Menu item at current Row
local fs = menuIndex[#menuIndex]
fs:goMenu()
elseif string == "left" then
--intentionally left blank
elseif string == "right" then
--intentionally left blank
elseif string == "up" then
local fs = menuIndex[#menuIndex]
fs:selectPreviousRow(true,true,true)
elseif string == "down" then
local fs = menuIndex[#menuIndex]
fs:selectNextRow(true,true,true)
end
end
--Sends Input to Context Handler, May Eventually Be Removed
function getInput()
if playdate.buttonJustPressed("b") then
menuInputContext("b")
elseif playdate.buttonJustPressed("a") then
menuInputContext("a")
elseif playdate.buttonJustPressed("right") then
menuInputContext("right")
elseif playdate.buttonJustPressed("up") then
menuInputContext("up")
elseif playdate.buttonJustPressed("down") then
menuInputContext("down")
elseif playdate.buttonJustPressed("left") then
menuInputContext("left")
end
end
--Function Check for Menu Item
function goMenu(item)
-- Check if the selected item has a corresponding function and call it
if menuFunc[item] then
menuFunc[item]()
elseif not menuFunc[item] then
local oFat = loadSavedPlayers("all")
for i,v in pairs(oFat) do
for k,c in pairs(v) do
if c.chrName == item then
chrStat(c)
end
end
end
--do it again for cards
else
print("No action defined for menu item:", item)
end
end
--Dummy Function to Return a List of Items to be Drawn in Gridview Cells
function loadSavedPlayers(string)
local plr = {}
if string == "all" then
plr = {"One","Two","Three","Four","Five","Six","Seven"}
else
plr = toString(string)
end
return plr
end
--Create statusList Object Class from Gridview
statusList = playdate.ui.gridview.new(0,25)
statusList.backgroundImage = playdate.graphics.nineSlice.new("assets/textBorder",10,10,16,16)
--Create statusList Object
function statusList:new()
print("Gridview Object Created")
local o = o or {}
setmetatable(o,self)
self.__index=self
o:setCellPadding(0,0,0,0)
o:setContentInset(5,5,7,7)
local menuX = 0 --size of background box and position to be set later
local menuY = 0
local yPos = 0
local xPos = 0
--Simplified Table Retrieval
local oFat = loadSavedPlayers("all")
o.listRows = oFat
o.menuNumberBox = {}
o:setNumberOfColumns(1)
o:setNumberOfRows(#oFat)
xPos, yPos = 40,40
menuY, menuX = 140,100
function o:getOption() -- item selection in menu
local s = o:getSelectedRow()
for i,v in pairs(o.listRows) do
if s==i then
return v
end
end
end
local statusListSprite = playdate.graphics.sprite.new()
statusListSprite:setCenter(0,0)
function o:spriteKill()
statusListSprite:remove()
end
statusListSprite:add()
function o:menuUpdate()
if o.needsDisplay then
local statusListImage = playdate.graphics.image.new(menuX,menuY,playdate.graphics.kColorWhite)
statusListSprite:moveTo(xPos,yPos)
local zInNew = 130
zInNew = zInNew + #menuIndex -- newest menu will always be drawn on top
statusListSprite:setZIndex(zInNew)
playdate.graphics.pushContext(statusListImage)
o:drawInRect(0,0,menuX,menuY)
playdate.graphics.popContext()
statusListSprite:setImage(statusListImage)
--Debug Statement
print("menuUpdate Running")
end
end
function o:drawCell(section,row,column,selected,x,y,width,height)
print("drawCell Running. Drawing Row "..row)
if selected then
playdate.graphics.drawRect(x+1,y+2,width-2,height-2)
playdate.graphics.drawRect(x,y,width,height)
else
playdate.graphics.drawRect(x,y,width,height)
end
local fontHeight = playdate.graphics.getSystemFont():getHeight()
playdate.graphics.drawTextInRect(o.listRows[row], x+2, y + (height/2 - fontHeight/2) + 2, width, height, nil, truncationString, kTextAlignment.left)
end
local countI = 0
for _ in pairs(menuIndex) do
countI = countI + 1
end
o.index = countI + 1
menuIndex[o.index] = o
return o
end
function menuCreate()
if #menuIndex == 0 then
statusList:new()
end
end
function playdate.update()
menuCreate()
if #menuIndex > 0 then
for i,v in pairs(menuIndex) do
if i == #menuIndex then -- This ensures only the top-level menu has control context.
v:menuUpdate()
end
end
end
getInput()
playdate.graphics.sprite.update()
playdate.timer.updateTimers()
end