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

[Help] GUI

Started by cheekycharlie101, 21 September 2012 - 03:20 PM
cheekycharlie101 #1
Posted 21 September 2012 - 05:20 PM
ok, i made a program that controls turtles via rednet. so you would go to computer and you would get

direction: forward

distance: 10

that would move the turtle forward 10 blocks. anyway i wanna make a gui for this. like add buttons but i have no idea about how to do it. i would either like it so everytime you pressed the forward button it would move forward 1. or you press the button then put the distance. can someone pleaseeeeeeeeeeeeeeeee help me :)/>/> thanks
-Cheeky
austin2162 #2
Posted 21 September 2012 - 05:28 PM
Try


rednet.open("left")
line1="[Forward]"
line2=" Back "
line3=" Turn Left "
line4=" Turn RIght "
while i==0 do
term.clear()
term.setCursorPos(16,6)
print "Select your Direction."
term.setCursorPos(21,7)
print(line1)
term.setCursorPos(21,8)
print(line2)
term.setCursorPos(21,9)
print(line3)
term.setCursorPos(21,10)
print(line4)
event,param1,param2,param3 = os.pullEvent()
if event == "key" and param1 == 17 and selected>1 then
if selected==2 then
  line1="[Forward]"
  line2=" Back "
  selected=1
end
if selected==3 then
  line2="[Back]"
  line3=" Turn Left "
  selected=2
end
if selected==4 then
  line3="[Turn Left]"
  line4=" Turn RIght"
  selected=3
end
end
if event == "key"  and param1 == 31 and selected<4 then
if selected==3 then
  line4="[Turn RIght]"
  line3=" Turn Left "
  selected=4
end
if selected==2 then
  line3="[Turn Left]"
  line2=" Back "
  selected=3
end
if selected==1 then
  line2="[Back]"
  line1=" Forward "
  selected=2
end
end
if event == "key" and param1 == 33 then
if selected==1 then
  -your rednet message for forward
end
if selected==2 then
   -your rednet message for back
end
if selected==3 then
   -your rednet message for turn left
end
if selected==4 then
   -your rednet message for turn right
end
end
sleep(.2)
end
and just modify it to work for yours =p
the keys are
w for up
s for down
f to select
edit i fixed for you
cheekycharlie101 #3
Posted 21 September 2012 - 05:42 PM
Try


rednet.open("left")
line1="[Forward]"
line2=" Back "
line3=" Turn Left "
line4=" Turn RIght "
while i==0 do
term.clear()
term.setCursorPos(16,6)
print "Select your Direction."
term.setCursorPos(21,7)
print(line1)
term.setCursorPos(21,8)
print(line2)
term.setCursorPos(21,9)
print(line3)
term.setCursorPos(21,10)
print(line4)
event,param1,param2,param3 = os.pullEvent()
if event == "key" and param1 == 17 and selected>1 then
if selected==2 then
  line1="[Forward]"
  line2=" Back "
  selected=1
end
if selected==3 then
  line2="[Back]"
  line3=" Turn Left "
  selected=2
end
if selected==4 then
  line3="[Turn Left]"
  line4=" Turn RIght"
  selected=3
end
end
if event == "key"  and param1 == 31 and selected<4 then
if selected==3 then
  line4="[Turn RIght]"
  line3=" Turn Left "
  selected=4
end
if selected==2 then
  line3="[Turn Left]"
  line2=" Back "
  selected=3
end
if selected==1 then
  line2="[Back]"
  line1=" Forward "
  selected=2
end
end
if event == "key" and param1 == 33 then
if selected==1 then
  -your rednet message for forward
end
if selected==2 then
   -your rednet message for back
end
if selected==3 then
   -your rednet message for turn left
end
if selected==4 then
   -your rednet message for turn right
end
end
sleep(.2)
end
and just modify it to work for yours =p
the keys are
w for up
s for down
f to select
edit i fixed for you
thanks for adding keys. but can you add buttons. so you hover over it and hit enter ?:)/>/>
austin2162 #4
Posted 21 September 2012 - 05:49 PM
I dunno how to do that if its possible
cheekycharlie101 #5
Posted 21 September 2012 - 05:53 PM
I dunno how to do that if its possible
it is, ive seen buttons in loads of programs. a prime example would be the email client by nitrogenfingers. look it up o youtube
Cranium #6
Posted 21 September 2012 - 06:16 PM
Check out this tutorial. It helped me make menus and buttons.
cheekycharlie101 #7
Posted 21 September 2012 - 07:17 PM
Check out this tutorial. It helped me make menus and buttons.
cranium, this is a further extension to my project that i was working on. anyway i installed your tekkit master control and it was brilliant. infact its how i got the idea of using menues. anyway i need to know the code that makes a option and when its pressed it executes another line of code. i looked at the link you gave me but it dident really show anything. please help me :)/>/> thanks -Cheeky
sjele #8
Posted 21 September 2012 - 07:22 PM
I don't think it is possible to detect a mouse press. Use arrow keys etc for now
cheekycharlie101 #9
Posted 21 September 2012 - 07:40 PM
I don't think it is possible to detect a mouse press. Use arrow keys etc for now
i dont mean a mouse press. i mean you use the arrow keys to scroll a menua then hit the enter key and it executes some code
Cranium #10
Posted 21 September 2012 - 08:14 PM

options = {
"Option 1",
"Option 2",
"Option 3"
}

local function opt(m,mY)
n=1
l=#m
while true do
for i=1, l, 1 do
if i==n then
local x, y = term.getSize()
local b = string.len(">"..m[i].."<")/2
local x = (x/2)-b
term.setCursorPos(x,i+mY)
term.clearLine()
print(">"..m[i].."<")
else
local x, y = term.getSize()
b = string.len(m[i])/2
x = (x/2)-b
term.setCursorPos(x,i+mY)
term.clearLine()
print(m[i]) end
end
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<l then n=n+1 end
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
This is the main option function I use for many of my programs. It lists the options stored as strings in the table named options (but you can name the table whatever you want). You call back to it using opt(options,0) (the 0 is 1 on the y position on the screen, since it centers the text.)

Edit: Whoops, meant to say use this:

local input = opt(options,0)
if input == 1 then
--do option 1
elseif input == 2 then
--do option 2
--etc...
end
Edited on 21 September 2012 - 06:16 PM
cheekycharlie101 #11
Posted 21 September 2012 - 08:52 PM

options = {
"Option 1",
"Option 2",
"Option 3"
}

local function opt(m,mY)
n=1
l=#m
while true do
for i=1, l, 1 do
if i==n then
local x, y = term.getSize()
local b = string.len(">"..m[i].."<")/2
local x = (x/2)-b
term.setCursorPos(x,i+mY)
term.clearLine()
print(">"..m[i].."<")
else
local x, y = term.getSize()
b = string.len(m[i])/2
x = (x/2)-b
term.setCursorPos(x,i+mY)
term.clearLine()
print(m[i]) end
end
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<l then n=n+1 end
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
This is the main option function I use for many of my programs. It lists the options stored as strings in the table named options (but you can name the table whatever you want). You call back to it using opt(options,0) (the 0 is 1 on the y position on the screen, since it centers the text.)

Edit: Whoops, meant to say use this:

local input = opt(options,0)
if input == 1 then
--do option 1
elseif input == 2 then
--do option 2
--etc...
end
Thanks sooo much. it works great. one last thing. is there a way i can make it go back to the same option. right now it just ends the program. so i fixed that by doing

shell.run("menu")
but. that makes it go to the top of the menu. is there a way where i can hit enter over option 2 and then it will still have option 2 selected? if so please tell me
Cranium #12
Posted 21 September 2012 - 09:01 PM
You would want to put it within a while true loop. For example:

while true do
term.clear()
term.setCursorPos(1,1)
print("What option do you want today?")
local input = opt(option,0) --again, the 0 is the y position you want. You can put this wherever you want.
if input == 1 then
--do option 1
elseif input == 2 then
--do option 2
end
end
It's a fairly simple code, and pretty flexible.If you have any questions, or want to modify it to do specific things, I can help you with that.
cheekycharlie101 #13
Posted 21 September 2012 - 09:04 PM
You would want to put it within a while true loop. For example:

while true do
term.clear()
term.setCursorPos(1,1)
print("What option do you want today?")
local input = opt(option,0) --again, the 0 is the y position you want. You can put this wherever you want.
if input == 1 then
--do option 1
elseif input == 2 then
--do option 2
end
end
It's a fairly simple code, and pretty flexible.If you have any questions, or want to modify it to do specific things, I can help you with that.
Dude thank you soo much. the ammount of support you give, you should be a moderator here :)/>/> !
Cranium #14
Posted 21 September 2012 - 09:06 PM
Nah, I'm just super bored at work :)/>/>
Hooray for unrestricted internet access, and a slow workload!
cheekycharlie101 #15
Posted 21 September 2012 - 09:09 PM
You would want to put it within a while true loop. For example:

while true do
term.clear()
term.setCursorPos(1,1)
print("What option do you want today?")
local input = opt(option,0) --again, the 0 is the y position you want. You can put this wherever you want.
if input == 1 then
--do option 1
elseif input == 2 then
--do option 2
end
end
It's a fairly simple code, and pretty flexible.If you have any questions, or want to modify it to do specific things, I can help you with that.
Dude thank you soo much. the ammount of support you give, you should be a moderator here :)/>/> !
one min. it still goes to the top of the menu after i press something. is there a way to stop that? il try and work it out in the mean time but any help would be appreciated :D/>/> thanks -Cheeky
Cranium #16
Posted 21 September 2012 - 09:23 PM
Well, it is supposed to go to the top of the menu. If you wanted it to stay were it was after pressing the first option, you would have to add some additional code within the menu function.
cheekycharlie101 #17
Posted 21 September 2012 - 09:28 PM
Well, it is supposed to go to the top of the menu. If you wanted it to stay were it was after pressing the first option, you would have to add some additional code within the menu function.
what would the code be? i wanna use it because its moving a turtle. so it gets a bit annoying when you have to scroll down 3 times after you press the button :)/>/>
anyway, if you could help me with the extra code that would be awesome, thanks again -Cheeky
Cranium #18
Posted 21 September 2012 - 09:33 PM
Nah…I'm starting to feel like I'm coding it for you. Come on, buddy, you can figure it out. :)/>/>
cheekycharlie101 #19
Posted 21 September 2012 - 10:56 PM
Nah…I'm starting to feel like I'm coding it for you. Come on, buddy, you can figure it out. :)/>/>
i understand where your coming from. and i agree, i dont want people to code for me, but could you give me any api's to refer to on the wiki or anything. sometimes i just need pointing in the right direction
Cranium #20
Posted 21 September 2012 - 11:05 PM
I'm not sure where I can point you other than the tutorial on menus I already posted. Perhaps someone around here knows.