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

Menu Help

Started by grand_mind1, 23 January 2013 - 01:56 PM
grand_mind1 #1
Posted 23 January 2013 - 02:56 PM
I have a program that will craft things when you type the specific item in. Each item that my system is able to craft is listen on the screen. I am happy with this program however I have seen in other programs that you can use the up, down, and enter keys to control what you do and highlight the selected item instead of typing things in. I really like how this looks and would like to try to apply it to my crafting program. The only problem is that I don't even know where to begin. I tried to do something with taking in what key you press and changing the color accordingly, but that didn't seem to work. A push in the right direction would be greatly appreciated.
Thanks! :D/>
ChunLing #2
Posted 23 January 2013 - 05:14 PM
assign an initial selection value
Make a loop.
1. you print your menu
2. you set the cursor position to the selected option
3. you write the selection marker, ">" or whatever.
4. you pull a key event.
5. change the selection value, or execute the selected option depending on the key.
end
KaoS #3
Posted 24 January 2013 - 05:14 AM
here is a link to my many menu functions. I trust one will suit your needs. if you do not find what you are looking for please let me know and I would love to code it. I thoroughly enjoy making GUIs
grand_mind1 #4
Posted 28 January 2013 - 11:41 AM
assign an initial selection value
Make a loop.
1. you print your menu
2. you set the cursor position to the selected option
3. you write the selection marker, ">" or whatever.
4. you pull a key event.
5. change the selection value, or execute the selected option depending on the key.
end
Could you please explain this a bit more? I've tried different things but it never works.
ChunLing #5
Posted 28 January 2013 - 04:39 PM
If you look in Kaos' linked post you'll see several menu options, some are this basic and others are more complex.
KaoS #6
Posted 28 January 2013 - 05:12 PM
Could you please explain this a bit more? I've tried different things but it never works.

I have re-written my basic menu function, stripped it down further and removed that confusing write((selected==num and "[" or " ")..item..(selected==num and "]" or " ")) as well as added comments explaining what we are doing each time. if that does not help then try it once more, post the code you used and tell us what it is doing wrong, we will then be able to help you a little better


local function newmenu(tList) --main function, tList is a table of options like {"one","two","three"}
  local selected=1 --the default selected item, if I set this to 3 it would start with the third option selected
  local function render() --you re-print your menu each time the selection changes
   for num,item in ipairs(tList) do --go through the table and print a choice on each line
    term.setCursorPos(1,num)
    if num==selected then --if the choice is the selected one then print a "[" before it and "]" after it otherwise print a space so things line up
	 write("[")
    else
	 write(" ")
    end
    write(item)
    if num==selected then
	 write("]")
    else
	 write(" ")
    end
   end
  end
  while true do --the actual menu functioning, draw the menu and then pull a key event
   render()
   local evts={os.pullEvent('key')}
   if evts[1]=="key" and evts[2]==200 and selected>1 then --if the key is 200 (up arrow) and you are not at the top already then move the selection one up
    selected=selected-1
   elseif evts[1]=="key" and evts[2]==208 and selected<#tList then --if the key is 208 (down arrow) and you are not at the bottom already then move the selection one down
    selected=selected+1
   elseif evts[1]=="key" and evts[2]==28 or evts[2]==156 then --if the key is 28 (return/enter) then return the current menu item
    return tList[selected]
   end
  end
end
grand_mind1 #7
Posted 29 January 2013 - 11:05 AM
Could you please explain this a bit more? I've tried different things but it never works.

I have re-written my basic menu function, stripped it down further and removed that confusing write((selected==num and "[" or " ")..item..(selected==num and "]" or " ")) as well as added comments explaining what we are doing each time. if that does not help then try it once more, post the code you used and tell us what it is doing wrong, we will then be able to help you a little better


local function newmenu(tList) --main function, tList is a table of options like {"one","two","three"}
  local selected=1 --the default selected item, if I set this to 3 it would start with the third option selected
  local function render() --you re-print your menu each time the selection changes
   for num,item in ipairs(tList) do --go through the table and print a choice on each line
	term.setCursorPos(1,num)
	if num==selected then --if the choice is the selected one then print a "[" before it and "]" after it otherwise print a space so things line up
	 write("[")
	else
	 write(" ")
	end
	write(item)
	if num==selected then
	 write("]")
	else
	 write(" ")
	end
   end
  end
  while true do --the actual menu functioning, draw the menu and then pull a key event
   render()
   local evts={os.pullEvent('key')}
   if evts[1]=="key" and evts[2]==200 and selected>1 then --if the key is 200 (up arrow) and you are not at the top already then move the selection one up
	selected=selected-1
   elseif evts[1]=="key" and evts[2]==208 and selected<#tList then --if the key is 208 (down arrow) and you are not at the bottom already then move the selection one down
	selected=selected+1
   elseif evts[1]=="key" and evts[2]==28 or evts[2]==156 then --if the key is 28 (return/enter) then return the current menu item
	return tList[selected]
   end
  end
end
Thank you
KaoS #8
Posted 29 January 2013 - 05:50 PM
no problem :)/> let us know when you have your first, working menu