This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
rickydaan's profile picture

[API] Scroll menu -How to make more "Pages"

Started by rickydaan, 08 December 2012 - 04:50 AM
rickydaan #1
Posted 08 December 2012 - 05:50 AM
Hello!

I've got a question: At this code, when there are more entries then possible (like you got 9 lines left and got 16 selections to list) how to make it scroll in different "pages"


function menu( table )
 local selected = 1
 local nx,ny = term.getCursorPos()
 local stop = false
 while not stop do
 for _,content in ipairs(table) do
  term.setCursorPos(nx,ny+_-1)
  if _ == selected then print("-> "..content)
   else
   print("   "..content)
  end
 end
  event,char = os.pullEvent()

  if event == "key" and char == 208 then if selected ~= #table then selected = selected+1 end end
  if event == "key" and char == 200 then if selected ~= 1 then selected = selected-1 end end 
  if event == "key" and char == 28 then stop = true return table[selected] end
 end
end

Thank you!
Rickydaan
Lyqyd #2
Posted 08 December 2012 - 06:46 AM
Why not keep track of an "offset" value and use that to adjust where you start drawing from the table?


local offset, selection = 1, 1
xLim, yLim = term.getSize()
--loop through until the bottom of the screen, or the number of entries, whichever is smaller.
for i = 1, math.min(yLim, #menuTable) do
  term.setCursorPos(1, i)
  --ignore these two lines if they do not make sense, just some highlighting. Remove them if pre-1.45.
  term.setTextColor((selection == offset + i - 1) and colors.black or colors.white)
  term.setBackgroundColor((selection == offset + i - 1) and colors.white or colors.black)
  --use offset + i - 1 so that when offset is one, we aren't drawing from index 2
  term.write(menuTable[offset + i - 1])
  --same as above with these two.
  term.setTextColor(colors.white)
  term.setBackgroundColor(colors.black)
end
rickydaan #3
Posted 08 December 2012 - 09:26 PM
I dont really understand that, the selection is fine, but if theres space for only 5 entries, and you got 10 entries, then how to make it scroll?
Lyqyd #4
Posted 08 December 2012 - 11:15 PM
You just change the offset value, and re-draw it, like with that loop.