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

Menu Bar

Started by MegaShrimp, 07 November 2012 - 02:28 PM
MegaShrimp #1
Posted 07 November 2012 - 03:28 PM
Ok guys. So I'm kind of new to this whole coding thing and I'm just not sure on something I would like to accomplish. I've worked with lua before and can use it at a semi efficient rate. But basically I'm not the best at this kind of thing at the moment.

I've been messing around with the os.pullEvent() and have been trying to make a menu bar for selections

similar to the effect of

Direction1

[Direction2]

Direction3

and when I press the up and down Keys (key208) and (key200) It will scroll through the options till I've chosen what I would like to select in which I would use the enter key (key28) <- I think.. to go any further. But I have no clue whatsoever how to even begin.

I'm not looking for anything special just that simple function. I'm already familier with this part two :P/>/>
——————
while true do

event, pressed = os.pullEvent()

if event == "key" and pressed == "200" then

–blah

end
end

——————-

But as for adding the brackets and whatever its just.. Uhg.. Can someone help me with this?

I may have screwed up in my scripts somewhere :3…….
Kryptanyte #2
Posted 07 November 2012 - 05:13 PM
Well its actually quite simple really. Heres an example of the code you would need to do this sort of thing without having an auto detect of how big each string is and automatically center it on the page.


function menu(table)  -- Creating a function to run the code. (table) is something that is added when calling the function, see below.
local sel = 1  -- Setting a variable for the selection.
clear() -- This is just an API function I use on my computers, saves a bit of time when trying to rip out a code fast.


while true do
	  
  term.setCursorPos(1, 1) -- This can be changed to anything so you can move it anywhere.
  for i = 1,#table do  -- Telling the program to run this part of the code to write [ ] around table[i] so if i = 2 then it would print the second
			    	-- string in the table with [ ] around it but nothing else
			  
			if i == sel then write"[" end
			write(table[i])
			if i == sel then write"]" end
			print("")
			  
		end -- Ending the 'for' loop


  local r,s = os.pullEvent()  -- Detecting any events that may happen.
  
   if r == "key" then  -- Refining the events detected to only key strokes.
	
	if s == 200 then sel = sel - 1 end -- If the up key is pushed take 1 off of 'sel' variable.
	
	if sel == 0 then sel = #table end -- still checking through whats happening to the table, so if 'sel' is 0 as in you have used the up key
					  -- when on the number 1 table item it will go to the last item in the table
  
	if s == 208 then sel = sel + 1 end -- When the down key is pushed add 1 to 'sel'.
	
	if sel == #table +1 then sel = 1 end -- Pretty much the same as 'if sel == 0 then' but going in reverse.
	
	if s == 28 then  -- If the 'enter' key is pushed.
	
	 clear()  -- Just clearing the screen.
	 return table[sel]  -- Telling the function to return whatever item in the table is selected to the rest of the program.
	
	end  -- Ending the 'enter' key if statement.
   end  -- Ending the os.pullEvent() 'key' if statement.
  
end  -- Ending the while loop.

end  -- Closing the function.

As for using this function to utilise the menu.

Basically we need to call upon the function, to do this first of all we need a table.

A table can be created by using { }'s with strings separated by comma's.

Next we need to call the table a variable so we can call on the function nice and cleanly.

Finally you call the function using the variable.

This is what it would look like.



items = {"String 1", "String 2", "String 3"}  --[[ You can call 'items' whatever you like as it is a variable, it just has to match up to the corresponding
						  variable in the function above, so if the function was; function menu(table, Ylocation, Xlocation) then
						  you wouldn't go and put 'items' in the 2nd or 3rd place inside the ( )'s when calling the function.]]

menu(items)  -- Calling the function and telling it to use the table 'items'

Now how to make it so that when you press enter it runs another program or script.

This is really easy. Heres an example of how you would do it;




items = {"String 1", "String 2", "String 3"}

result = menu(items) -- The reason this is now a variable is so that when the function returns the selected menu items you have something to use in an if statement

if result == "String 1" then
	--[Put your code here for what you want this option to run]
end

if result == "String 2" then
	--[Put your code here for what you want this option to run]
end

if result == "String 3" then
	--[Put your code here for what you want this option to run]
end


I'm not 100% sure if the above example would work, haven't tested it as usually I just use elseif to branch off the different results which results in messy code.

You can also call your menu (If in a function) when another menu has returned a certain item. So say you selected "String 1" and pushed enter, you could
then make a if statement to say; if result == "String 1" then menu(items2). Or something like that so it runs the menu again with other items. Its all up to
you.

Anyway, I hope this is of some use to you and helps you figure out a menu of your own. You are free to use the menu code above as long as you give me
credit for it, you may also edit it as long as you give me credit for the original bits of the code.

Please tell me if anything in this post is confusing you or is incorrect as well. I havent tested some of it.
Welcome to Lua my friend =D
MegaShrimp #3
Posted 08 November 2012 - 03:25 PM
Thank you so much! I promise to take all that I can from this.
MegaShrimp #4
Posted 08 November 2012 - 03:55 PM
Success it works!

But how would I manipulate the table so that this is all vertical?
remiX #5
Posted 08 November 2012 - 09:59 PM
This can work

local function menuW(...)
    local sel = 1
    local list = {...}
    local offX,offY = term.getCursorPos()
    local curX,curY = term.getCursorPos()
    while true do
        if sel > #list then sel = 1 end
        if sel < 1 then sel = #list end
        for i = 1,#list do
            if sel == i then
                write("[ "..list[i].." ]")
            else
                write("  "..list[i].."  ")
            end
        end
        while true do
            local e,e1 = os.pullEvent()
            if e == "key" then
                if e1 == 203 then -- left key
                    sel = sel-1
                    term.setCursorPos(curX,curY)
                    break
                end
                if e1 == 205 then -- right key
                    sel = sel+1
                    term.setCursorPos(curX,curY)
                    break
                end
                if e1 == 28 then -- enter key
                    term.setCursorPos(curX,curY)
                    return list[sel],sel
                end
            end
        end
    end
end

And then to put what is in the menu:
local selection = menuW("OK", "Clear left", "Clear right")

And then to do what each does on enter:

if selection == "OK" then
ok()
elseif selection == "Clear left" then
left()
elseif selection == "Clear right" then
right()
end
MegaShrimp #6
Posted 09 November 2012 - 12:15 PM
Oh crap T-T. I misused Horizontal, What I ment was vertical D:. I understand if you want to curse me out now seeing as that you were willing to give me that, T-T I'm sorry D:
Kryptanyte #7
Posted 09 November 2012 - 02:33 PM
The table has nothing to do with the way the menu is presented. The way the menu code prints the table it reads is what matters.

If you want a vertical menu then go and look at this: http://www.computercraft.info/forums2/index.php?/topic/5777-wraithbones-gui-tui-really-functions/

Wraithbone has a vertical menu that centers itself on the screen and also centers text in the menu. Otherwise the one I posted is vertical but not centered, mainly you would use something like that for a full screen GUI menu system sort of thing.