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

Question about the Scrolling event.

Started by jasperdekiller, 29 November 2014 - 11:07 AM
jasperdekiller #1
Posted 29 November 2014 - 12:07 PM
Hello,

I'm trying to make a simple database to save passwords so I can do something with it later.
Everything is working good so far. But If I got more then 10 users, then I cant put it on one page.
So I thought maybe if I use a Scrolling system so if I scroll down I can see the rest of the users
and If I receive the top again the its stops so you cant go further from the top. But I already tried some few
things, but I dont know how to do it.. I got a Table where I stored the {Username, password} and I dont know if you scroll
down how to print the other users in the table. So I need to know how to use the function and if it reached the top again it stop.

Acc_pass = {}

function userScrolls()

while true do
for n, account in pairs(Acc_pass) do
event,arg,x,y = os.pullEvent("mouse_scroll")
term.setCursorPos(x,y)
term.setTextColor(colors.black)
term.scroll(arg)

end
end
end
Bomb Bloke #2
Posted 30 November 2014 - 01:15 AM
Tiny bit of math involved here. You need to keep track of how far you've scrolled, and you also need to keep track of how far you can scroll - which depends on the length of the list, versus the length of the screen.

Something along these lines:

local function listScroll(list)
	term.setTextColour(colours.white)
	term.setBackgroundColour(colours.black)
	
	local scrollPos = 1
	
	local xSize, ySize = term.getSize()
	
	while true do
		term.clear()
		for i = 1, math.min(#list, ySize) do
			term.setCursorPos(1, i)
			term.write(list[scrollPos + i - 1])
		end
	
		local myEvent = {os.pullEvent()}
		
		if myEvent[1] == "mouse_scroll" then
			scrollPos = scrollPos + myEvent[2]
			if scrollPos > #list - ySize + 1 then scrollPos = #list - ySize + 1 end
			if scrollPos < 1 then scrollPos = 1 end
			
		elseif myEvent[1] == "char" or myEvent[1] == "mouse_click" then
			return
		
		end
	end
end

listScroll({"this","is","an","example","of","a","list","this","is","an","example","of",
	"a","list","this","is","an","example","of","a","list","this","is","an","example",
	"of","a","list","this","is","an","example","of","a","list","this","is","an","example",
	"of","a","list"})