An example of setScrollPosition

Does anybody by chance have a working sample of the setScrollPosition function for gridview ? I didn't see any examples in the SDK files.

I can't manage to see getScrollPosition print out anything other than 0.0, 0.0 and can't seem to figure out how to set it to anything. It's looking for an x,y value according to the documentation, but is that screen space relative or relative to the gridview's current x,y (probably the latter)? Any insight would be helpful ! Thank you.

For me, it was a combination of a few different things (including being an intermediate programmer...), but some things to note for anybody who lands here in the future:

  • I was using a gridview with only one cell, and it turns out that :setNumberOfRows() needs to be set explicitly (to 1 in my case), or the drawCell function won't run
  • I didn't fully understand how scroll updating works; it mentions in the docs for drawInRect that this needs to be explicitly run in an update call, but I guess I didn't take that seriously enough and had some code doing something slightly different inside of a draw call instead
  • using printTable on the gridview was incredibly helpful in the debugging process, and poking around in the gridview.lua file also helped get me to the solution eventually

yay learning!

If anyone has other ways they have approached a scrolling text box, feel free to share.

Untitled

Code portions included below, note that it's used inside of an object subclass:

somewhere in card:init()

gfx.setFont(infoFont)
self.text = gfx.getLocalizedText(text, systemLanguage)
self.textWidth, self.textHeight = gfx.getTextSizeForMaxWidth(self.text, self.width)
		
self.textHeight = self.textHeight
self.textView = playdate.ui.gridview.new(0, self.textHeight)
self.textView:setNumberOfRows(1)
self.textView:setScrollDuration(1000)
self.scrollPos = 0
		
cellText = self.text
function self.textView:drawCell(section, row, column, selected, x, y, width, height)
	_setImageColor(kNXOR)
	gfx.drawTextInRect(cellText, x, y, width, height, nil, "...", kTextAlignment.left)
end

somewhere in card:draw()
(in draw because of how I had set up the card drawing into an image context, but could be in update instead)

self.textView:drawInRect(0, 0, self.width, self.height)

somewhere in card:update()

if self.textView.isScrolling then
	self:draw()
end
2 Likes

Will mark complete !