I'm trying to use a gridview
as a message box. I thought it would be a simple way to scroll text in a window, however, the scrolling only update after the up or down button is released and then far less than I would have expected.
NB On the device it scrolls much better. Which is odder, as the simulator is over powered.
local pd <const> = playdate
local gfx <const> = pd.graphics
local ui <const> = pd.ui
local geo <const> = pd.geometry
class('MessageBox').extends(gfx.sprite)
---@param textItems table Array of strings
function MessageBox:init(textItems)
printTable(#textItems .. ' textItems', textItems)
self:setCenter(0,0)
self:setSize(300,100)
self:moveTo(100, 50)
self:add()
local listview <const> = ui.gridview.new(0, 20)
listview.backgroundImage = gfx.nineSlice.new('simple-nine-slice', 8, 8, 1, 1)
listview:setNumberOfRows(#textItems)
listview:setCellPadding(4, 4, 2, 2)
listview:setContentInset(8, 8, 8, 8)
listview:setNumberOfRows(#textItems)
listview:setNumberOfColumns(1)
function listview:drawCell(section, row, column, selected, x, y, width, height)
gfx.drawTextInRect(textItems[row], x, y, width, height, nil, '...', kTextAlignment.center)
end
self.listview = listview
end
function MessageBox:draw()
local spriteWidth, spriteHeight = self:getSize()
self.listview:drawInRect(0, 0, spriteWidth, spriteHeight)
end
function MessageBox:update()
local scrollSpeed = 10
local listview <const> = self.listview
local x, y = listview:getScrollPosition()
print('Pos', y)
print(listview:getScrollPosition())
if (pd.buttonIsPressed(pd.kButtonDown)) then
listview:setScrollPosition(x, y + scrollSpeed)
elseif (pd.buttonIsPressed(pd.kButtonUp)) then
listview:setScrollPosition(x, y - scrollSpeed)
end
end
Any advise on what's happening or how to fix it?