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

Need some help with my scroll program

Started by jasperdekiller, 25 February 2015 - 01:51 PM
jasperdekiller #1
Posted 25 February 2015 - 02:51 PM
Hello,

I was thinking let's start with a Scroll Program.
But what I got so far is like the draw part for the table it should print
and thats it. What I want is a line on the right side of the screen where you can
scroll with. Like I know how to make it scroll. But I dont know how to count the (blue) line.
Like what you can pull down with mouse_drag to scroll too.

This is the code I got so far:



local startY = nil
local endY = nil
local eTable = {}

term.setBackgroundColor(colors.white)
term.clear()
term.setTextColor(colors.black)

function countLines()
if startY and endY then
  som = endY / 19
  return som
end
end

function drawScrollBar(barColor)
if endY ~= nil then
  if barColor then
   term.setBackgroundColor(barColor)
  else
   term.setBackgroundColor(colors.blue)
  end
   Lines = countLines()
   for i = 1,Lines do
	term.setCursorPos(28,i)
	term.write(" ")
   end
end
end


function newScroll(X,Y,table,barColor)
startY = Y
eTable = table
if type(eTable) == "table" and #eTable ~= nil then
for i = 1, #eTable do
  term.setCursorPos(X,Y)
  term.write(eTable[i])
  Y = Y + 1
end
  if Y > 19 then
   endY = Y
  end
if barColor then
  drawScrollBar(barColor)
else
  drawScrollBar()
end
end
end


sTable = {"Jasper","example","Jasper","example","Jasper","example","Jasper","example","Jasper","example","Jasper","example",
		 "Jasper","example","Jasper","example","Jasper","example","Jasper","example"}

newScroll(1,1,sTable)



I hope someone can help me with this. If you need some more information be free to ask.

Thankyou,
Jasper
KingofGamesYami #2
Posted 25 February 2015 - 04:58 PM
I think you want to make a bar on the side that moves, yes? This might help you. It's not specifically about lua, but it is about the math required which is relatively easy to translate into lua.
jasperdekiller #3
Posted 26 February 2015 - 03:01 PM
Thankyou.
But still didn't helped this..
Actually I would like to have it directly in Lua.
wieselkatze #4
Posted 26 February 2015 - 04:43 PM
This way my approach on a scroll bar ( line 216+ ).
As CC display have relatively few pixels you have to be precise about your positioning of the scrollbar. E.g. you don't want the maths resulting in a scrollbar already at the bottom, when there's still content below your current positon.
The solution i coded was checking if the current position is either at 1 or at the very end of the content - only then the scrollbar will be displayed at the top or bottom position.
So, as these positions are already taken, I'll subtract 2 from the actual length of the scrollbar area.
The scrollbar itself will always be >= 1 pixel, but bigger when there's only 1 or 2 elements to scroll. That means, that if my list is the screen area +1, there'll be two positions for the scroll bar to be - either at the start of my list or at the end.
Because of that my scrollbar is now 50% of the height. If there are 2 elements, there'll be 3 positions, so 33,3% and so on.

With the code I currenty have it should be pretty easy to implement mouse_drag on the scrollbar - you'd just have to check the position of the scrollbar the mouse is currently on and if it's either at the top or the bottom of the content.
Basically it's just reversing the maths. It's all about the maths :P/>

As the code was not meant for public use, it has no documentation whatsoever. Nevertheless I hope you'll understand the most of it.

If you still have questions - feel free to ask
Edited on 26 February 2015 - 03:45 PM