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

Creating my first GUI/OS

Started by Klausar, 01 July 2012 - 12:31 PM
Klausar #1
Posted 01 July 2012 - 02:31 PM
Ok, so I want to create my first GUI that shows all my programs. I'm very new to coding so if you try to explain something please do it noobish :P/>/>

I use this code to go up and Down


local currentX = 1
local currentY = 1

function drawCursor()
  term.clear() --Clears the screen
  term.setCursorPos(currentX, currentY)
  write(">")
end

while true do
  drawCursor()
  local e,key = os.pullEvent( "key" )
  if key == 17 or key == 200 then
	    currentY = currentY -1
  elseif key == 31 or key == 208 then
	    currentY = currentY +1
  end
end

First of all I want the Cursor to stop when it reaches the top or the bottom, how do I do this? Then of course I want to list all programs and make them chooseable with the cursor by pressing enter, how do I do this?
tesla1889 #2
Posted 01 July 2012 - 10:12 PM
selection:
if you aren't going to use a list-type gui, you have to assign the locations of each letter in each option to a table.
when the user presses the key you want to select with, have it call a function that checks each option's table for a match on currentX, currentY
the function will return either nil for no match, or the path to the option.
then, simply run that path.

keeping the cursor in bounds:
add this line to the top of your code

local sx, sy = term.getSize()
if currentX is less than 0, dont subtract
if currentX is greater than sx, don't add
if currentY is less than 0, dont subtract
if currentY is greater than sy, don't add
tesla1889 #3
Posted 01 July 2012 - 10:33 PM

local tList = fs.list(shell.dir()) -- get all items in current dir
local tFiles = {}
local tPaths = {}
for n,sItem in ipairs(tList) do -- only add files to tFiles
local sPath = fs.combine(shell.dir(),sItem)
if fs.isDir(sPath) == false then
  table.insert(tFiles,sItem) -- insert filename into tFiles
  table.insert(tPaths,sPath) -- insert path into tPaths
end
end
local currentY = 3
local function draw_gui()
term.clear()
print("")
print("  Files:")
for n = 1,#tFiles do -- print out all files in tFiles
  print("  "..tFiles[n])
end
term.setCursorPos(1, currentY)
write(">")
end
local function select_item()
shell.run(tPaths[currentY - 2]) -- run the path
end
while true do
draw_gui()
local e,key = os.pullEvent( "key" )
if key == 17 or key == 200 then
  if currentY > 2 then
   currentY = currentY -1
  end
elseif key == 31 or key == 208 then
  if currentY < (#tFiles + 2) then
   currentY = currentY +1
  end
elseif key == <the key you want> then
  select_item()
end
end
Klausar #4
Posted 02 July 2012 - 01:08 PM
Thank you very much for your help. How do I list only some programs?
Wolvan #5
Posted 02 July 2012 - 01:51 PM
Thank you very much for your help. How do I list only some programs?
You have to either write all listed programs in the program yourself or you use an if-clause

if tFiles == "[The file not to show" or tFiles == "..." then
else
print(tFiles[n])
end