Advertisement
theinsekt

clickable list

Sep 16th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.03 KB | None | 0 0
  1. --experiment, not tested, not working
  2. --pastebin get 1QUnMKbz startup
  3.  
  4. function getClick(window0,list)
  5.  window0.clear()
  6.  window0.setCursorPos(1,1)
  7.  local w,h=window0.getSize()
  8.  
  9.  for k,v in ipairs(list) do
  10.   local window1=window.create(window0, 1, k, w, 1, true)
  11.   window1.setBackgroundColor(colors.orange)
  12.   window1.write(tostring(k)..": "..v)
  13.   --window0.write(tostring(k)..": "..v)
  14.   --nextLine(window0)
  15.  end
  16.  
  17.  while(true) do
  18.   local event={pullEvents({["key"]=true,["mouse_click"]=true,["mouse_scroll"]=true})}
  19.   --window0.write(event[1])
  20.   if event[1]=="mouse_click" then
  21.    local b=event[2]
  22.    local x=event[3]
  23.    local y=event[4]
  24.    if isInsideWindow(window0,x,y) then
  25.     return x,y
  26.    end
  27.   elseif event[1]=="mouse_scroll" then
  28.    local scrollDirection=event[2]
  29.    local x=event[3]
  30.    local y=event[4]
  31.    if isInsideWindow(window0,x,y) then
  32.     --window0.write(" "..tostring(scrollDirection).." ")
  33.     window0.scroll(scrollDirection)
  34.     window0.redraw()
  35.    end
  36.   elseif event[1]=="key" then
  37.    local keycode=event[2]
  38.   end
  39.   --nextLine(window0)
  40.   sleep(0.15)
  41.  end
  42. end
  43.  
  44. function nextLine(window0)
  45.  local x,y=window0.getCursorPos()
  46.  window0.setCursorPos(1,y+1)
  47. end
  48.  
  49. function isInsideWindow(window0,x,y)
  50.  local wx, wy=window0.getPosition()
  51.  local w,h=window0.getSize()
  52.  return x>=wx and y>=wy and x<=wx+w and y<=wy+h
  53. end
  54.  
  55. function toWindowCoordinate(window0,x,y)
  56.  local wx, wy=window0.getPosition()
  57.  return x-wx+1, y-wy+1
  58. end
  59.  
  60. --example usage: pullEvents({["key"]=true,["mouse_click"]=true})
  61. function pullEvents(eventDict)
  62.  while(true) do
  63.   local event = { os.pullEvent() }
  64.   if eventDict[event[1]]~=nil then
  65.    return unpack(event)
  66.   end
  67.  end
  68. end
  69.  
  70. --window.create(table parentTerm, number x, number y, number width, number height [, boolean visible])
  71. function screenCoveringWindow()
  72.  local w, h = term.getSize()
  73.  return window.create(term.current(),1,1,w,h,true)
  74. end
  75.  
  76. function test()
  77.  local list={"one","two","three"}
  78.  local window0=screenCoveringWindow()
  79.  getClick(window0,list)
  80. end
  81.  
  82. test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement