Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- function writeLoc(x,y,sText)
- term.setCursorPos(x,y)
- term.clearLine()
- term.write(sText)
- end
- function rawWriteLoc(x,y,sText)
- term.setCursorPos(x,y)
- term.write(sText)
- 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 textBox(x,y,len,size,focus,hideChar,default)
- 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
- 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
- while not bExit do
- local sEvent, param = os.pullEvent()
- 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
- end
- end
- --if (globalsEnabled) then
- windows = {
- ["showWindow"] = function( ... )
- return showWindow( ... )
- end,
- ["selectWindow"] = function( ... )
- return selectWindow( ... )
- end,
- ["textBox"] = function( ... )
- return textBox( ... )
- end,
- ["clearArea"] = function( ... )
- return clearArea( ... )
- end,
- }
- --end
- --[[Uncomment to see a demo
- term.clear()
- showWindow(3,3,40,10,"Testing!","\nUsername:\n\nPassword:")
- textBox(14,7,20,1,false,false,"xlilcasper")
- textBox(14,8,20,2,false,true,"")
- local enterPressed = false
- local username,password="",""
- while not enterPressed do
- username, enterPressed=textBox(14,7,20,1,true,false,"xlilcasper")
- password, enterPressed=textBox(14,8,20,2,true,true,"")
- end
- term.clear()
- term.setCursorPos(1,1)
- print(username..":"..password)
- local width, height = term.getSize()
- term.setCursorPos(1,height)
- --]]
- --[[
- term.clear()
- local iSelected, sText = windows.selectWindow(2,4,"Options",{"Pulse Time", "Side", "Activiate", "Exit"})
- print(sText)
- local width, height = term.getSize()
- term.setCursorPos(1,height)
- --]]
- --[[
- local sText, focus=textBox(5,5,20,true,true,"Testing")
- term.clear()
- term.setCursorPos(1,1)
- print(sText)
- print("Enter "..tostring(focus))
- local width, height = term.getSize()
- term.setCursorPos(1,height)
- --]]
Add Comment
Please, Sign In to add comment