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

GUI help

Started by CaptiveCreeper, 23 April 2014 - 03:20 AM
CaptiveCreeper #1
Posted 23 April 2014 - 05:20 AM
A while back i saw a forum post on how to make a text user interface in CC and i thought that was really cool so i made it a api. Now when i tried to use it with more items it is too long how would i make it so that it only show so it fits on the screen and then when you press down below the bottom option it will scroll down?


function custom(options)
  position = 1
  while true do
	term.clear()
	term.setCursorPos(1,2)
	for i = 1, #options, 1 do
	  if i == position then print(" >"..options[i].."<") else print( " ".. options[i]) end
	end
	a, b = os.pullEvent("key")
	if b == 200 and position > 1 then position = position - 1 end
	if b == 208 and position < #options then position = position + 1 end
	if b == 57 then break end
  end
  term.clear()
  term.setCursorPos(1,2)
  return position
end


Any help will be appreciated in advanced thanks.
Edited on 23 April 2014 - 04:29 AM
bigbaddevil6 #2
Posted 23 April 2014 - 07:17 AM
I'm assuming the forum post you got the from was the YES/NO located here. If I'm correct why not just go with the customizable one?
Edited on 23 April 2014 - 05:24 AM
CaptiveCreeper #3
Posted 25 April 2014 - 04:07 AM
yes that is the one and the code i posted is the custom one from that post but how would you make it so if you have a long list that goes further that the screen can show at one time how would you make it so it scrolls through the list?
HometownPotato #4
Posted 26 April 2014 - 05:23 AM
I think this is what you are looking for:
http://computercraft.info/wiki/Textutils.pagedPrint
CaptiveCreeper #5
Posted 29 April 2014 - 03:08 AM
That looks promising i have to do homework right now so i can't check to see if that works (it looks like it will). I will have try that tomorrow.
CaptiveCreeper #6
Posted 30 April 2014 - 03:48 AM
Now that i look at it that would only let it scroll down and also i don't know how it would make the program know what index it ends on.
CaptiveCreeper #7
Posted 30 April 2014 - 04:56 AM
Actually you gave me a idea of how it may work i have most of it working but i have a problem and i don't know how to fix

local main = {}
for i = 1,50 do
  main[i] = i
  print(i)
end
local index = 1
while true do
selected = index
for index = index, 17 + index do
    if index == selected then print(" > " .. index .. ". " .. main[index] .. " < ")
    else print(index .. ". " .. main[index])
    end
end
local a
local b
a, b = os.pullEvent("key")
if b == 200 and index > 1 then index = index - 1 end
if b == 208 and index < #main then index = index + 1 end
if b == 28 then break end
end
return index

At line 11 when it gets to 17 less than the last value it crashes with error "Attempt to concentrate nil and string"
Bomb Bloke #8
Posted 30 April 2014 - 05:50 AM
You could use:

for index = index, math.min(#main, 17 + index) do

Also this:

local a
local b
a, b = os.pullEvent("key")

Can be condensed to this:

local a, b = os.pullEvent("key")
CaptiveCreeper #9
Posted 30 April 2014 - 09:40 PM
I did that and now when it gets to 33 it starts printing 50 at the top and "selecting" meltable lines at once.
Bomb Bloke #10
Posted 01 May 2014 - 02:36 AM
Maybe throw a "term.clear()" in at the bottom of the loop, see if it makes more sense then.
CaptiveCreeper #11
Posted 01 May 2014 - 03:19 AM
When you do that it just shows a blank screen.
Bomb Bloke #12
Posted 01 May 2014 - 04:23 AM
Sorry, I wasn't specific - I'm guessing you've put it at the end of the "for" loop. I was referring to the "while" loop - just before it repeats. Or you could stick it at the top. Whatever suits you.
CaptiveCreeper #13
Posted 01 May 2014 - 04:57 AM
That works perfectly thank you and ya i put it in the for loop.
CaptiveCreeper #14
Posted 01 May 2014 - 05:08 AM
How would i make it so that at the end if it is less than 19 lines that it would show them all instead of when you press down it gets rid of the ones above it. If you understand.
Bomb Bloke #15
Posted 01 May 2014 - 08:00 AM
for index = math.min(#main - 17, index), math.min(#main, 17 + index) do

Or something like that, anyway.

In case it's not clear to you, math.min() returns the lowest value out of those you pass to it, whereas math.max() returns the highest.
CaptiveCreeper #16
Posted 06 May 2014 - 11:57 PM
Sorry for taking so long to respond. But when i use that line it doesn't select anything.
Bomb Bloke #17
Posted 07 May 2014 - 12:46 AM
You might need to show the current state of your script. As far as I can tell, it should work fine if you dump the line from here directly into the version here.
CaptiveCreeper #18
Posted 07 May 2014 - 02:44 AM

local main = {}
for i = 1,50 do
  main[i] = i
  print(i)
end
local index = 1
while true do
term.clear()
selected = index
for index = index, math.min(#main,17 + index) do
    if index == selected then print(" > " .. index .. ". " .. main[index] .. " < ")
    else print(index .. ". " .. main[index])
    end
end
local a
local b
a, b = os.pullEvent("key")
if b == 200 and index > 1 then index = index - 1 end
if b == 208 and index < #main then index = index + 1 end
if b == 28 then break end
end
return index
there you go that is what i have currently
Bomb Bloke #19
Posted 07 May 2014 - 03:54 AM
Ok. Now play spot the difference between what's in that, and the line I gave you here.
CaptiveCreeper #20
Posted 08 May 2014 - 11:54 PM
i did that and it did not put the > < around the "selected" item