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

working gui arrow keys help

Started by sleawnis, 25 October 2012 - 10:42 AM
sleawnis #1
Posted 25 October 2012 - 12:42 PM
Please help me ive made a simple gui i need like it to output a different bundled wire each time any color will do this is what ive got:

http://imgur.com/w8QwJ

i need one of those cool menues using arrow keeys to like move up and down with a [ ] around it and also i need to make it output redstone bundled from the back on each 1 when u click enter
KaoS #2
Posted 25 October 2012 - 01:30 PM
here is a basic alpha try, I have not included the actual key events in the listen function, you need to do that and sort out the code, just an example as I am in a hurry


local function addcol(colour,side)
side=side or 'back'
rs.setBundledOutput(side,colors.combine(rs.getBundledOutput(side),colour))
end
local function remcol(colour,side)
side=side or 'back'
rs.setBundledOutput(side,colors.subtract(rs.getBundledOutput(side),colour))
end
local function togglecol(colour,side)
side=side or 'back'
if colors.test(rs.getBundledOutput(side),colour) then
  remcol(colour,side)
else
  addcol(colour,side)
end
end
local sSide='left'
local tMenu=setmetatable({},{__index=tOptions;__call=function(tMe,param1,param2) togglecol(tColours[param1][param2],sSide) end})
local tOptions={{'one','six'},{'two',seven'},{'three','eight'},{'four','nine'},{'five'},{'all on','all off'}}
local tColours={{colors.red,colors.white},{colors.orange,colors.magenta},{colors.blue,colors.lightBlue},{colors.yellow,colors.purple},{colors.black},{colors.combine(colors.red,colors.white,colors.orange,colors.magenta,colors.blue,colors.lightBlue,colors.yellow,colors.purple,colors.black),0}}
local function display()
for row=1,#tOptions do
  for num=1,#tOptions[row] do
   if sy==row and sx==num then
    term.setCursorPos(num*5,row)
    write('['..tMenu[row][num]..']')
   else
    term.setCursorPos(num*5+1,row)
    write(tMenu[row][num])
   end
  end
end
end
local function listen()
local evt={os.pullEvent()}
if evt[1]=='key' then
  if evt[2]==uparrow and sy>1 then sy=sy-1
  elseif evt[2]==leftarrow and sx>1 then sx=sx-1
  elseif evt[2]==rightarrow and sx<#tOptions[sy] then sx=sx+1
  elseif evt[2]==downarrow and sy<#tOptions then sy=sy+1
  elseif evt[2]==return then tMenu(sy,sx)
  end
end
end
local sx=1
local sy=1
while true do
display()
listen()
end
Kingdaro #3
Posted 25 October 2012 - 01:34 PM
Here's a menu implementation that I whipped up:

local function foo()
  term.clear()
  term.setCursorPos(4,4)
  print 'foo'
  sleep(1)
end

local function bar()
  term.clear()
  term.setCursorPos(4,4)
  print 'bar'
  sleep(1)
end

local w,h = term.getSize()
local options = {
  {'foo',4,4,foo};
  {'bar',4,5,bar};
}

local selection = 1

while true do
  term.clear()

  for i, option in pairs(options) do
    if selection == i then
      term.setCursorPos(option[2] - 2, option[3])
      write '['
      term.setCursorPos(option[2] + #option[1] + 1, option[3])
      write ']'
    end

    term.setCursorPos(option[2], option[3])
    write(option[1])
  end

  local _, k = os.pullEvent('key')
  if k == keys.down then
    selection = (selection < #options)
    and selection + 1
    or 1
  elseif k == keys.up then
    selection = (selection > 1)
    and selection - 1
    or #options
  elseif k == keys.enter then
    local o = options[selection]
    local res = o[4] and o[4]() or true
    if res == false then
      break
    end
  end
end

This is the part where you put your menu options:

local options = {
  {'foo',4,4,foo};
  {'bar',4,5,bar};
}

The first value is what you want that option to say. The second and third values are the x and y of where you want it to appear on the screen. The last one is the function you want to run when you press enter.

You can have one one of your functions return false to exit the menu. So if I wanted an exit option, I would add

  {'Exit',4,10,function() return false end}
sleawnis #4
Posted 25 October 2012 - 01:45 PM
Here's a menu implementation that I whipped up:

local function foo()
  term.clear()
  term.setCursorPos(4,4)
  print 'foo'
  sleep(1)
end

local function bar()
  term.clear()
  term.setCursorPos(4,4)
  print 'bar'
  sleep(1)
end

local w,h = term.getSize()
local options = {
  {'foo',4,4,foo};
  {'bar',4,5,bar};
}

local selection = 1

while true do
  term.clear()

  for i, option in pairs(options) do
	if selection == i then
	  term.setCursorPos(option[2] - 2, option[3])
	  write '['
	  term.setCursorPos(option[2] + #option[1] + 1, option[3])
	  write ']'
	end

	term.setCursorPos(option[2], option[3])
	write(option[1])
  end

  local _, k = os.pullEvent('key')
  if k == keys.down then
	selection = (selection < #options)
	and selection + 1
	or 1
  elseif k == keys.up then
	selection = (selection > 1)
	and selection - 1
	or #options
  elseif k == keys.enter then
	local o = options[selection]
	local res = o[4] and o[4]() or true
	if res == false then
	  break
	end
  end
end

This is the part where you put your menu options:

local options = {
  {'foo',4,4,foo};
  {'bar',4,5,bar};
}

The first value is what you want that option to say. The second and third values are the x and y of where you want it to appear on the screen. The last one is the function you want to run when you press enter.

You can have one one of your functions return false to exit the menu. So if I wanted an exit option, I would add

  {'Exit',4,10,function() return false end}

Im getting lots of errors :P/>/> can u code it like so all i do is copy and paste? sorry im only on vidio 3 of a computer craft tutorial ^_^/>/>
jag #5
Posted 25 October 2012 - 04:44 PM
here is a basic alpha try, I have not included the actual key events in the listen function, you need to do that and sort out the code, just an example as I am in a hurry


--spilled code--
I'm usually not this picky, but you missed a ' here:

                                     ↓ here
local tOptions={{'one','six'},{'two',seven'},{'three','eight'},{'four','nine'},{'five'},{'all on','all off'}}
KaoS #6
Posted 25 October 2012 - 08:07 PM
I noticed after posting, it inverts and messes up the whole syntax highlighting lol. but I was late so didn't change it. I literally churned it out in about 3 minutes