Advertisement
HyperQ

Menu API - ComputerCraft

Oct 11th, 2012
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.70 KB | None | 0 0
  1. local function center(str, width)
  2.     local blank = string.rep(' ',math.floor((width-#str)/2))
  3.     return blank .. str
  4. end
  5.  
  6. local function printc(stri,coordx,coordy)
  7.     term.setCursorPos(coordx,coordy)
  8.     write(stri)
  9. end
  10.  
  11. local function split(str, pat)
  12.     local t = { }
  13.     local fpat = "(.-)"..pat
  14.     local last_end = 1
  15.     local s, e, cap = str:find(fpat, 1)
  16.     while s do
  17.         if s ~= 1 or cap ~= "" then
  18.             table.insert(t,cap)
  19.         end
  20.         last_end = e+1
  21.         s, e, cap = str:find(fpat, last_end)
  22.     end
  23.     if last_end <= #str then
  24.         cap = str:sub(last_end)
  25.         table.insert(t, cap)
  26.     end
  27.     return t
  28. end
  29.  
  30.  
  31. function create(title,items,blurb,func)
  32.     if selected == nil then selected = 1 end
  33.     term.clear()
  34.     term.setCursorPos(1,1)
  35.     if #title > 28 then title = string.sub(title, 1, 27-#title) end
  36.     printc("+----------------------------+",11,1)
  37.     printc("|"..center(title,28),11,2)
  38.     printc("|",40,2)
  39.     printc("+----------------------------+",11,3)
  40.     printc("|                            |",11,4)
  41.     printc("|                            |",11,5)
  42.     printc("|                            |",11,6)
  43.     printc("|                            |",11,7)
  44.     printc("|                            |",11,8)
  45.     printc("|                            |",11,9)
  46.     printc("|                            |",11,10)
  47.     printc("|                            |",11,11)
  48.     printc("|                            |",11,12)
  49.     printc("|                            |",11,13)
  50.     printc("|                            |",11,14)
  51.     printc("+----------------------------+",11,15)
  52.     printc("|                            |",11,16)
  53.     printc("|                            |",11,17)
  54.     printc("+----------------------------+",11,18)
  55.     for linen, item in ipairs(items) do
  56.         if linen <= 10 then
  57.             term.setCursorPos(14,3 + linen)
  58.             print(item)
  59.         end
  60.     end
  61.     term.setCursorPos(13,3 + selected)
  62.     print("["..items[selected].."]")
  63.     blurbword = split(blurb[selected]," ")
  64.     local l1 = ""
  65.     local l2 = ""
  66.     for i,w in ipairs(blurbword) do
  67.       if string.len(l1..w) <= 25 then
  68.        if i == 1 then
  69.         l1 = w
  70.        else
  71.         l1 = l1.." "..w
  72.        end
  73.       elseif string.len(l1..w) > 25 then
  74.        if string.len(l2..w) <= 25 then
  75.         if string.len(l2) == 0 then
  76.              l2 = w
  77.         else
  78.              l2 = l2.." "..w
  79.         end
  80.    end
  81.   end
  82. end
  83. printc(l1,13,16)
  84. printc(l2,13,17)
  85. while true do
  86.     local sEvent, param = os.pullEvent()
  87.         if sEvent == "key" then
  88.             if param == 28 then
  89.                 functorun = loadstring(func[selected])
  90.                 pcall(functorun)
  91.             elseif param == 208 then
  92.                 if selected < #items then selected = selected + 1 end
  93.                 create(title,items,blurb,func)
  94.         elseif param == 200 then
  95.             if selected > 1 then selected = selected - 1 end
  96.             create(title,items,blurb,func)
  97.         end
  98.     end
  99. end
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement