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

GUI Help

Started by koslas, 06 August 2012 - 04:54 AM
koslas #1
Posted 06 August 2012 - 06:54 AM
I am currently trying to make a OS with a GUI, but when I try and launch the program it doesn't seem to let the arrows work (Left and Right arrows). I have made sure I had the arrows configured. Another error I noticed is a bug where when I launch the program It doesn't work until I press a key.
I have not added the functions for the options yet.

Here is the code:



--No Terminate

os.pullEvent = os.pullEventRaw

--Rednet

rednet.open("top")
rednet.open("bottom")
rednet.open("left")
rednet.open("right")
rednet.open("front")
rednet.open("back")

--

--GUI

local option = {}
option[1] = "[Login] Register  Exit"
option[2] = " Login [Register] Exit"
option[3] = " Login  Register [Exit]"

--Login Function

function login()
end

--Register Function

function register()
end

--Exit Function

function exit()
end

--Goto Function

function gotoMenu(i)
	if i == 1 then
		login()
	elseif i == 2 then
		register()
	elseif i == 3 then
		exit()
	end
end

--Option Function

function options(i)
	if not i then
		i = 1
	end
	term.clear()
	term.setCursorPos(1,1)
	print(option[i])
		event, key = os.pullEvent("key")
while true do
		if key == 203 and not i == 1 then
			return options(i - 1)
		elseif key == 205 and not i == 3 then
			return options(i + 1)
		elseif key == 28 then
			return gotoMenu(i)
		end
	end
end

options()
brett122798 #2
Posted 06 August 2012 - 06:57 AM
I'm still reviewing your arrow key thing, but I noticed a problem with your gotoMenu function. int cannot be used as an argument, so replace it with anything else. Ex: number

EDIT: Okay, I don't know if it'll make a difference, but in my code, the key thing looks like this:


local e,key = os.pullEvent("key")

Instead of your this:


event, key = os.pullEvent("key")
Lyqyd #3
Posted 06 August 2012 - 06:58 AM
Well, you're telling it to print after it receives a key event. You should probably have the clear screen and the print before the event pull.
koslas #4
Posted 06 August 2012 - 08:11 AM
Problem Fixed
Noodle #5
Posted 06 August 2012 - 10:26 AM
I'm still reviewing your arrow key thing, but I noticed a problem with your gotoMenu function. int cannot be used as an argument, so replace it with anything else. Ex: number

EDIT: Okay, I don't know if it'll make a difference, but in my code, the key thing looks like this:


local e,key = os.pullEvent("key")

Instead of your this:


event, key = os.pullEvent("key")
It works both ways..
KaoS #6
Posted 06 August 2012 - 03:02 PM
try this out


ops={"login", "register", "exit"}
rps={login, register, exit}
function menu(options,responses,initialselect)
local selected=initialselect or 1
while true do
term.clear()
term.setCursorPos(1,1)
local numberofoptions=0
for delim,opt in pairs(options) do
if delim==selected then
  write("[")
end
write(opt)
if delim==selected then
  write("]")
end
numberofoptions=numberofoptions+1
end
local event,param1=os.pullEvent("key")
if param1==203 and selected~=1 then
selected=selected-1
elseif param1==205 and selected~=numberofoptions then
selected=selected+1
elseif param1==28 then
return responses[selected]()
end
end
end


menu(ops,rps,2)

then rename the 3 things in rps to your three functions you want to call when you select something