Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.04 KB | None | 0 0
  1. -- NetApi A stripped down version of CrazyApi By Crazydrift (I USE IT AS A RUNTIME)
  2. -- THIS API IS A FUCKING MESS BUT IT WORKS FOR WHAT I NEED IT FOR.
  3.  
  4. function writeAt(x, y, s)
  5.     term.setCursorPos(x,y)
  6.     print (s)
  7. end
  8.  
  9. function clearTerm()
  10. term.clear()
  11. term.setCursorPos(1,1)
  12. end
  13.  
  14. function printCentred( y, s )
  15.     local x = math.floor((w - string.len(s)) / 2)
  16.     term.setCursorPos(x,y)
  17.     term.clearLine()
  18.     term.write( s )
  19. end
  20.  
  21. function disableTerminate()
  22. function os.pullEvent()
  23. local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
  24. if event == "terminate" then
  25. end
  26. return event, p1, p2, p3, p4, p5
  27. end
  28. end
  29.  
  30. function enableTerminate()
  31. function os.pullEvent()
  32. local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
  33. return event, p1, p2, p3, p4, p5
  34. end
  35. end
  36.  
  37. --End of General functions
  38.  
  39. --Simple Networking (You save some lines...)
  40.  
  41. function networkOpen(side)
  42.     rednet.open(side)
  43. end
  44.  
  45. function networkClose(side)
  46.     rednet.close(side)
  47. end
  48.  
  49. function networkReceive(timeout)
  50.     var1, var2 = rednet.receive(timeout)
  51.     return var2
  52. end
  53.  
  54. function networkIDReceive(timeout)
  55.     var1, var2 = rednet.receive(timeout)
  56.     return var1, var2
  57. end
  58.  
  59. function networkBroadcast(s)
  60.     repeat
  61.         local boolean = rednet.broadcast(s)
  62.     until boolean == true
  63. end
  64.  
  65. function networkSendToID(id, s)
  66.     repeat
  67.         local boolean = rednet.send(id, s)
  68.         if boolean == false then
  69.             sleep(0.1)
  70.         end
  71.     until boolean == true
  72. end
  73.  
  74. --end of Simple Networking
  75.  
  76. --Windows library from RedWorks (Probly the best thing they have did so far)
  77.  
  78. function rawWriteLoc(x,y,sText)
  79.     term.setCursorPos(x,y)
  80.     term.write(sText)
  81. end
  82.  
  83. function textBox(x,y,len,size,focus,hideChar,default,disableTerm)
  84.     local h=2
  85.     local w=len+3
  86.     local char=""
  87.     local line=""
  88.     if size==1 then
  89.         y=y
  90.         w=len
  91.         for tx=0,w do
  92.             rawWriteLoc(x+tx,y,"_")
  93.         end
  94.     elseif size==2 then
  95.         for tx=0,w do
  96.             rawWriteLoc(x+tx,y+2,"-")
  97.         end
  98.     else
  99.         for ty=0,h do
  100.             for tx=0,w do
  101.                 char=" "
  102.                 if (ty==0) or (ty==h) then --horizonal line
  103.                     if (tx==0 or tx==w) then
  104.                         char="+"
  105.                     else
  106.                         char="-"
  107.                     end
  108.                 elseif (ty==1) then
  109.                     if (tx==0 or tx==w) then
  110.                         char="|"
  111.                     end
  112.                 else--normal line
  113.                     if (tx==0 or tx==w) then
  114.                         char="|"
  115.                     end
  116.                 end
  117.             line=line..char
  118.             end
  119.             rawWriteLoc(x,y+ty,line)
  120.             line=""
  121.         end
  122.     end
  123.    
  124.     if (focus) then
  125.         if size==2 then
  126.             y=y+1
  127.             len=len+3
  128.         elseif size==3 then
  129.             x=x+2
  130.             y=y+1
  131.         end
  132.         term.setCursorPos(x,y)
  133.         term.setCursorBlink(true)
  134.         sText=default or ""
  135.         rawWriteLoc(x,y,sText)
  136.         bExit = false
  137.         local dispText=""
  138.         local clearChar=" "
  139.         if size==1 then clearChar="_" end
  140.         local falt, sEvent, param = nil,nil,nil
  141.         while not bExit do
  142.             if disableTerm then
  143.                 falt, sEvent, param = pcall(os.pullEvent)
  144.             else
  145.                 sEvent, param = os.pullEvent()
  146.                 falt=false
  147.             end
  148.            
  149.             if sEvent == "key" then
  150.                 if param == 28 then
  151.                     return sText,true
  152.                 elseif param==15 then
  153.                     return sText,false
  154.                 elseif param == 14 then
  155.                     sText = string.sub(sText,1,string.len(sText)-1)
  156.                 end
  157.             end
  158.            
  159.             if sEvent == "char" then
  160.                 sText = sText .. param
  161.             end
  162.            
  163.             local dispChars=string.len(sText)
  164.             if dispChars > len then dispChars=len end
  165.             if not hideChar then
  166.                 dispText = string.sub(sText,-len)
  167.             else
  168.                 dispText=""
  169.                 for n=1,dispChars do
  170.                     dispText=dispText.."*"
  171.                 end
  172.                 dispText = string.sub(dispText,-len)
  173.             end
  174.             dispChars=string.len(dispText)
  175.             for n=dispChars,len do
  176.                 dispText=dispText..clearChar
  177.             end
  178.             rawWriteLoc(x,y,dispText)
  179.             term.setCursorPos(x+dispChars,y)
  180.         end
  181.     else
  182.         return sText,true
  183.     end
  184. end
  185.  
  186. function tCopy(t)
  187.   local u = { }
  188.   for k, v in pairs(t) do u[k] = v end
  189.   return setmetatable(u, getmetatable(t))
  190. end
  191.  
  192. function showWindow(x,y,w,h,title,text)
  193.     local sTitle = title or ""
  194.     local sText = text or ""
  195.     width, height = term.getSize()
  196.     sTitle = string.sub(sTitle,1,w-2)
  197.     iTitleStart = ((w/2)-1)-((#sTitle-1)/2)
  198.     if (not x) then
  199.         x=(width-w)/2
  200.     end
  201.     if (not y) then
  202.         y=(height-h)/2
  203.     end
  204.     if (w > width) then w = width end
  205.     if (h > height) then h = height end
  206.     local line=""
  207.     local char=""
  208.     local curChar=1
  209.     local waitForLine=1
  210.     if (sTitle ~= "") then waitForLine = 2 end
  211.     for ty=0,h do
  212.         for tx=0,w do
  213.             char=" "
  214.             if (ty==0) or ((ty==2) and (sTitle ~= "")) or (ty==h) then --horizonal line
  215.                 if (tx==0 or tx==w) then
  216.                     char="+"
  217.                 else
  218.                     char="-"
  219.                 end
  220.             elseif (ty==1) then
  221.                 if (tx==0 or tx==w) then
  222.                     char="|"
  223.                 else
  224.                     --if (tx-iTitleStart < #sTitle+1) and (tx > iTitleStart) then
  225.                     --  char = string.sub(sTitle,tx-iTitleStart,tx-iTitleStart)
  226.                     --end
  227.                 end
  228.             else--normal line
  229.                 if (tx==0 or tx==w) then
  230.                     char="|"
  231.                 end
  232.             end
  233.         line=line..char
  234.         end
  235.         rawWriteLoc(x,y+ty,line)
  236.         line=""
  237.     end
  238.     if (sTitle ~= "") then
  239.         rawWriteLoc(x+iTitleStart+1,y+1,sTitle)
  240.     end
  241.     if (sText ~= "") then
  242.         term.setCursorPos(x+1,y+3)
  243.         local wrapedText = getWrapedText(x+1,y+2,x+w-2,y+h-1,sText)
  244.         local offset = 0
  245.         if (sTitle ~= "") then offset=2 end
  246.         for ty=1,#wrapedText do
  247.             rawWriteLoc(x+1,ty+y+offset,wrapedText[ty])
  248.             if (ty>h-offset-2) then break end
  249.         end
  250.     end
  251. end
  252.  
  253. function getWrapedText(sx,sy,w,h, sText )
  254.     local x,y = sx,sy
  255.     local line = ""
  256.     local wraped = { }
  257.     local function newLine()
  258.         table.insert(wraped,line)
  259.         line=""
  260.         x=sx
  261.     end
  262.    
  263.     local function storeText(text)
  264.         line=line..text
  265.         return #text
  266.     end
  267.    
  268.     -- Print the line with proper word wrapping
  269.     while string.len(sText) > 0 do
  270.         local whitespace = string.match( sText, "^[ \t]+" )
  271.         if whitespace then
  272.             -- Print whitespace
  273.             x = x + storeText(whitespace)
  274.             sText = string.sub( sText, string.len(whitespace) + 1 )
  275.         end
  276.        
  277.         local newline = string.match( sText, "^\n" )
  278.         if newline then
  279.             -- Print newlines
  280.             newLine()
  281.             sText = string.sub( sText, 2 )
  282.         end
  283.        
  284.         local text = string.match( sText, "^[^ \t\n]+" )
  285.         if text then
  286.             sText = string.sub( sText, string.len(text) + 1 )
  287.             if string.len(text) > w then
  288.                 -- Print a multiline word              
  289.                 while string.len( text ) > 0 do
  290.                 if x > w then
  291.                     newLine()
  292.                 end
  293.                     x = x + storeText( text )
  294.                     text = string.sub( text, (w-x) + 2 )
  295.                 end
  296.             else
  297.                 -- Print a word normally
  298.                 if x + string.len(text) > w then
  299.                     newLine()
  300.                 end
  301.                 x = x + storeText( text )
  302.             end
  303.         end
  304.     end
  305.     newLine()
  306.     return wraped
  307. end
  308.  
  309. function selectWindow(x,y,title,tOptions,iSelected)
  310.     local w=#title
  311.     local h=1
  312.     local selected=iSelected or 1
  313.     local tOptionsOrg=tCopy(tOptions)
  314.     local width, height = term.getSize()
  315.     for n=1,#tOptions do
  316.         tOptions[n]=" "..tOptionsOrg[n].."  "
  317.         if (string.len(tOptions[n])>w) then w=string.len(tOptions[n]) end      
  318.     end
  319.     w=w+2
  320.     local sTitle = title or ""
  321.     if (sTitle ~= "") then h = h + 2 end
  322.     h = h + #tOptions
  323.     tOptions[selected]="*"..tOptionsOrg[selected].."*"
  324.     if (not x) then
  325.         x=(width-w)/2
  326.     end
  327.     if (not y) then
  328.         y=(height-h)/2
  329.     end
  330.     showWindow(x,y,w,h,sTitle,table.concat(tOptions,"\n"))
  331.    
  332.     --loop and capture arrow keys for selecting output.
  333.     bExit = false
  334.     while not bExit do
  335.         event, param = os.pullEvent()
  336.         if event == "key" then
  337.             if param == 197 then --Exit on pause/break
  338.                 bExit = true
  339.             elseif param == 208 then
  340.                 tOptions[selected]=" "..tOptionsOrg[selected].."  " --Clear selection
  341.                 selected=selected+1
  342.                 if (selected>#tOptions) then selected=1 end
  343.                 tOptions[selected]="*"..tOptionsOrg[selected].."*"
  344.                 showWindow(x,y,w,h,sTitle,table.concat(tOptions,"\n"))
  345.             elseif param == 200 then
  346.                 tOptions[selected]=" "..tOptionsOrg[selected].."  " --Clear selection
  347.                 selected=selected-1
  348.                 if (selected<1) then selected=#tOptions end
  349.                 tOptions[selected]="*"..tOptionsOrg[selected].."*"
  350.                 showWindow(x,y,w,h,sTitle,table.concat(tOptions,"\n"))
  351.             elseif param == 28 then
  352.                 return selected,tOptionsOrg[selected]
  353.             end
  354.         end
  355.        
  356.     end
  357. end
  358.  
  359. function clearArea(x,y,w,h,char)
  360.     local sChar = char or " "
  361.     for yy=0,h do
  362.         for xx=0,w do
  363.             rawWriteLoc(x+xx,y+yy,sChar)
  364.         end
  365.     end
  366. end
  367.  
  368. --End of windows libary
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement