Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- NetApi A stripped down version of CrazyApi By Crazydrift (I USE IT AS A RUNTIME)
- -- THIS API IS A FUCKING MESS BUT IT WORKS FOR WHAT I NEED IT FOR.
- function writeAt(x, y, s)
- term.setCursorPos(x,y)
- print (s)
- end
- function clearTerm()
- term.clear()
- term.setCursorPos(1,1)
- end
- function printCentred( y, s )
- local x = math.floor((w - string.len(s)) / 2)
- term.setCursorPos(x,y)
- term.clearLine()
- term.write( s )
- end
- function disableTerminate()
- function os.pullEvent()
- local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
- if event == "terminate" then
- end
- return event, p1, p2, p3, p4, p5
- end
- end
- function enableTerminate()
- function os.pullEvent()
- local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
- return event, p1, p2, p3, p4, p5
- end
- end
- --End of General functions
- --Simple Networking (You save some lines...)
- function networkOpen(side)
- rednet.open(side)
- end
- function networkClose(side)
- rednet.close(side)
- end
- function networkReceive(timeout)
- var1, var2 = rednet.receive(timeout)
- return var2
- end
- function networkIDReceive(timeout)
- var1, var2 = rednet.receive(timeout)
- return var1, var2
- end
- function networkBroadcast(s)
- repeat
- local boolean = rednet.broadcast(s)
- until boolean == true
- end
- function networkSendToID(id, s)
- repeat
- local boolean = rednet.send(id, s)
- if boolean == false then
- sleep(0.1)
- end
- until boolean == true
- end
- --end of Simple Networking
- --Windows library from RedWorks (Probly the best thing they have did so far)
- function rawWriteLoc(x,y,sText)
- term.setCursorPos(x,y)
- term.write(sText)
- end
- function textBox(x,y,len,size,focus,hideChar,default,disableTerm)
- local h=2
- local w=len+3
- local char=""
- local line=""
- if size==1 then
- y=y
- w=len
- for tx=0,w do
- rawWriteLoc(x+tx,y,"_")
- end
- elseif size==2 then
- for tx=0,w do
- rawWriteLoc(x+tx,y+2,"-")
- end
- else
- for ty=0,h do
- for tx=0,w do
- char=" "
- if (ty==0) or (ty==h) then --horizonal line
- if (tx==0 or tx==w) then
- char="+"
- else
- char="-"
- end
- elseif (ty==1) then
- if (tx==0 or tx==w) then
- char="|"
- end
- else--normal line
- if (tx==0 or tx==w) then
- char="|"
- end
- end
- line=line..char
- end
- rawWriteLoc(x,y+ty,line)
- line=""
- end
- end
- if (focus) then
- if size==2 then
- y=y+1
- len=len+3
- elseif size==3 then
- x=x+2
- y=y+1
- end
- term.setCursorPos(x,y)
- term.setCursorBlink(true)
- sText=default or ""
- rawWriteLoc(x,y,sText)
- bExit = false
- local dispText=""
- local clearChar=" "
- if size==1 then clearChar="_" end
- local falt, sEvent, param = nil,nil,nil
- while not bExit do
- if disableTerm then
- falt, sEvent, param = pcall(os.pullEvent)
- else
- sEvent, param = os.pullEvent()
- falt=false
- end
- if sEvent == "key" then
- if param == 28 then
- return sText,true
- elseif param==15 then
- return sText,false
- elseif param == 14 then
- sText = string.sub(sText,1,string.len(sText)-1)
- end
- end
- if sEvent == "char" then
- sText = sText .. param
- end
- local dispChars=string.len(sText)
- if dispChars > len then dispChars=len end
- if not hideChar then
- dispText = string.sub(sText,-len)
- else
- dispText=""
- for n=1,dispChars do
- dispText=dispText.."*"
- end
- dispText = string.sub(dispText,-len)
- end
- dispChars=string.len(dispText)
- for n=dispChars,len do
- dispText=dispText..clearChar
- end
- rawWriteLoc(x,y,dispText)
- term.setCursorPos(x+dispChars,y)
- end
- else
- return sText,true
- end
- end
- function tCopy(t)
- local u = { }
- for k, v in pairs(t) do u[k] = v end
- return setmetatable(u, getmetatable(t))
- end
- function showWindow(x,y,w,h,title,text)
- local sTitle = title or ""
- local sText = text or ""
- width, height = term.getSize()
- sTitle = string.sub(sTitle,1,w-2)
- iTitleStart = ((w/2)-1)-((#sTitle-1)/2)
- if (not x) then
- x=(width-w)/2
- end
- if (not y) then
- y=(height-h)/2
- end
- if (w > width) then w = width end
- if (h > height) then h = height end
- local line=""
- local char=""
- local curChar=1
- local waitForLine=1
- if (sTitle ~= "") then waitForLine = 2 end
- for ty=0,h do
- for tx=0,w do
- char=" "
- if (ty==0) or ((ty==2) and (sTitle ~= "")) or (ty==h) then --horizonal line
- if (tx==0 or tx==w) then
- char="+"
- else
- char="-"
- end
- elseif (ty==1) then
- if (tx==0 or tx==w) then
- char="|"
- else
- --if (tx-iTitleStart < #sTitle+1) and (tx > iTitleStart) then
- -- char = string.sub(sTitle,tx-iTitleStart,tx-iTitleStart)
- --end
- end
- else--normal line
- if (tx==0 or tx==w) then
- char="|"
- end
- end
- line=line..char
- end
- rawWriteLoc(x,y+ty,line)
- line=""
- end
- if (sTitle ~= "") then
- rawWriteLoc(x+iTitleStart+1,y+1,sTitle)
- end
- if (sText ~= "") then
- term.setCursorPos(x+1,y+3)
- local wrapedText = getWrapedText(x+1,y+2,x+w-2,y+h-1,sText)
- local offset = 0
- if (sTitle ~= "") then offset=2 end
- for ty=1,#wrapedText do
- rawWriteLoc(x+1,ty+y+offset,wrapedText[ty])
- if (ty>h-offset-2) then break end
- end
- end
- end
- function getWrapedText(sx,sy,w,h, sText )
- local x,y = sx,sy
- local line = ""
- local wraped = { }
- local function newLine()
- table.insert(wraped,line)
- line=""
- x=sx
- end
- local function storeText(text)
- line=line..text
- return #text
- end
- -- Print the line with proper word wrapping
- while string.len(sText) > 0 do
- local whitespace = string.match( sText, "^[ \t]+" )
- if whitespace then
- -- Print whitespace
- x = x + storeText(whitespace)
- sText = string.sub( sText, string.len(whitespace) + 1 )
- end
- local newline = string.match( sText, "^\n" )
- if newline then
- -- Print newlines
- newLine()
- sText = string.sub( sText, 2 )
- end
- local text = string.match( sText, "^[^ \t\n]+" )
- if text then
- sText = string.sub( sText, string.len(text) + 1 )
- if string.len(text) > w then
- -- Print a multiline word
- while string.len( text ) > 0 do
- if x > w then
- newLine()
- end
- x = x + storeText( text )
- text = string.sub( text, (w-x) + 2 )
- end
- else
- -- Print a word normally
- if x + string.len(text) > w then
- newLine()
- end
- x = x + storeText( text )
- end
- end
- end
- newLine()
- return wraped
- end
- function selectWindow(x,y,title,tOptions,iSelected)
- local w=#title
- local h=1
- local selected=iSelected or 1
- local tOptionsOrg=tCopy(tOptions)
- local width, height = term.getSize()
- for n=1,#tOptions do
- tOptions[n]=" "..tOptionsOrg[n].." "
- if (string.len(tOptions[n])>w) then w=string.len(tOptions[n]) end
- end
- w=w+2
- local sTitle = title or ""
- if (sTitle ~= "") then h = h + 2 end
- h = h + #tOptions
- tOptions[selected]="*"..tOptionsOrg[selected].."*"
- if (not x) then
- x=(width-w)/2
- end
- if (not y) then
- y=(height-h)/2
- end
- showWindow(x,y,w,h,sTitle,table.concat(tOptions,"\n"))
- --loop and capture arrow keys for selecting output.
- bExit = false
- while not bExit do
- event, param = os.pullEvent()
- if event == "key" then
- if param == 197 then --Exit on pause/break
- bExit = true
- elseif param == 208 then
- tOptions[selected]=" "..tOptionsOrg[selected].." " --Clear selection
- selected=selected+1
- if (selected>#tOptions) then selected=1 end
- tOptions[selected]="*"..tOptionsOrg[selected].."*"
- showWindow(x,y,w,h,sTitle,table.concat(tOptions,"\n"))
- elseif param == 200 then
- tOptions[selected]=" "..tOptionsOrg[selected].." " --Clear selection
- selected=selected-1
- if (selected<1) then selected=#tOptions end
- tOptions[selected]="*"..tOptionsOrg[selected].."*"
- showWindow(x,y,w,h,sTitle,table.concat(tOptions,"\n"))
- elseif param == 28 then
- return selected,tOptionsOrg[selected]
- end
- end
- end
- end
- function clearArea(x,y,w,h,char)
- local sChar = char or " "
- for yy=0,h do
- for xx=0,w do
- rawWriteLoc(x+xx,y+yy,sChar)
- end
- end
- end
- --End of windows libary
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement