Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.clear()
- local w, h = term.getSize()
- selOff = 0
- clearColor = colors.black
- _SelectedWindow = 0
- _Windows = {}
- _Windows.structOrder = {}
- running = true
- _readIndex = 0
- _windowsBoundsSX = 1
- _windowsBoundsSY = 1
- _windowsBoundsMX = w
- _windowsBoundsMY = h-1
- runningWindow = false
- function addWindow(wname,x,y,w,h,ev,parent,shadow)
- parent = parent or term.current()
- shadow = shadow or false
- ev = ev or (function(self,e) end)
- _Windows[#_Windows+1] = {name=wname,win=window.create(parent,x,y,w,h), winX = x, winY = y, winW = w, winH = h, winShadow = shadow,winNumber=#_Windows+1, update=ev}
- _Windows.structOrder[#_Windows.structOrder+1] = #_Windows
- os.queueEvent("window_created",#_Windows)
- end
- function getWindow(ind)
- return _Windows[ind].win
- end
- -- By name functions, DO NOT use in realtime execution, super slow.
- function getWindowByName(winname)
- -- Search for window
- for i = 1, #_Windows do
- if(_Windows[i].name == winname)then
- return _Windows[i].win
- end
- end
- end
- function getWindowNameID(winname)
- -- Search for window
- for i = 1, #_Windows do
- if(_Windows[i].name == winname)then
- return i
- end
- end
- end
- function getFrontWindow()
- return _Windows[_Windows.structOrder[#_Windows.structOrder]].win
- end
- function getFrontIndex()
- return tonumber(_Windows.structOrder[#_Windows.structOrder])
- end
- function getWindowByOrder(ind)
- return _Windows[_Windows.structOrder[ind]].win
- end
- function renderWindows(shd)
- for i = 1, #_Windows.structOrder do
- if(shd)then
- --if(_Windows[ind].winShadow)then
- -- _Windows[ind]:winShadow()
- --else
- local wx, wy, ww, wh = getWindowSize(_Windows.structOrder[i])
- paintutils.drawFilledBox(wx+1,wy+1,ww+wx,wh+wy,colors.black)
- --end
- end
- _Windows[_Windows.structOrder[i]].win.redraw()
- end
- end
- function wrapWindow(win,draws)
- local otrm = term.current()
- term.redirect(_Windows[win].win)
- draws()
- term.redirect(otrm)
- end
- function moveToFront(ind)
- local parse = 0
- for i = 1, #_Windows.structOrder do
- if(_Windows.structOrder[i] == ind)then
- parse = i
- end
- end
- if(parse == 0)then return false end
- table.remove(_Windows.structOrder, parse)
- _Windows.structOrder[#_Windows.structOrder+1] = ind
- end
- function undrawWindow(ind, col)
- local wx, wy, ww, wh = getWindowSize(ind)
- paintutils.drawFilledBox(wx,wy,ww+wx,wh+wy,col)
- end
- function moveWindow(ind,x,y,rd)
- rd = rd or true
- if( x >= _windowsBoundsSX and y >= _windowsBoundsSY
- and x+_Windows[ind].winW <= _windowsBoundsMX and y+_Windows[ind].winH <= _windowsBoundsMY)then
- if(rd)then
- undrawWindow(ind,clearColor)
- end
- _Windows[ind].win.reposition(x,y,_Windows[ind].winW,_Windows[ind].winH)
- _Windows[ind].winX = x
- _Windows[ind].winY = y
- --else
- -- winSel = false
- end
- end
- function setBackColor(col)
- clearColor = col
- term.setBackgroundColor(col)
- term.clear()
- end
- function isWindowOpen(ind)
- for i = 1, #_Windows.structOrder do
- if(_Windows.structOrder[i] == ind)then
- return true
- end
- end
- return false
- end
- function setWindowsBounds(sx,sy,mx,my)
- _windowsBoundsSX = sx
- _windowsBoundsSY = sx
- _windowsBoundsMX = mx
- _windowsBoundsMY = mx
- end
- function checkPointRect(x,y,bx,by,bw,bh)
- return (x >= bx and y >= by and x <= bx+bw and y <= by+bh)
- end
- function getWindowedInput(x,y,e,buffer,mask,size)
- term.setCursorBlink(false)
- local w, h = term.getSize()
- local ev, key = e[1], e[2]
- size = size or w-x
- mask = mask or ""
- buffer = buffer or ""
- if(ev == "char")then
- if(_readIndex == #buffer+1)then
- buffer = buffer .. key
- else
- buffer = buffer:sub(1,_readIndex) .. key .. buffer:sub(_readIndex+1)
- end
- _readIndex = _readIndex + 1
- elseif(ev == "key")then
- if(key == keys.left)then
- if(_readIndex >= 1)then _readIndex = _readIndex - 1 end
- elseif(key == keys.right)then
- if(_readIndex < #buffer)then _readIndex = _readIndex + 1 end
- elseif(key == keys.enter)then
- _readIndex = 0
- term.setCursorBlink(false)
- return buffer, true
- elseif(key == keys.home)then
- _readIndex = 0
- elseif(key == keys['end'])then
- _readIndex = #buffer
- elseif(key == keys.delete)then
- buffer = buffer:sub(1,_readIndex) .. buffer:sub(_readIndex+2,-1)
- elseif(key == keys.backspace and #buffer > 0)then
- if(_readIndex >= 1)then
- if(_readIndex == #buffer+1)then
- buffer = buffer:sub(1,-2)
- else
- buffer = buffer:sub(1,_readIndex-1) .. buffer:sub(_readIndex+1,-1)
- end
- _readIndex = _readIndex - 1
- end
- end
- end
- -- Render the stuff.
- local drawStr = buffer:sub(1,size+1)
- local cursorInd = _readIndex
- if(_readIndex > size)then
- cursorInd = _readIndex - (_readIndex-size)+1
- drawStr = buffer:sub((_readIndex-size),_readIndex)
- end
- if(mask ~= "")then
- local rnch = math.random(1,#mask)
- drawStr = string.rep(mask:sub(rnch,rnch),#drawStr)
- end
- term.setCursorPos(x, y)
- write(string.rep(" ",size+1))
- term.setCursorPos(x, y)
- write(drawStr)
- term.setCursorPos(x+cursorInd, y)
- term.setCursorBlink(true)
- return buffer, false
- end
- function closeWindow(ind)
- -- Take window out of structure.
- local parse = 0
- for i = 1, #_Windows.structOrder do
- if(_Windows.structOrder[i] == ind)then
- parse = i
- end
- end
- if(parse == 0)then return false end
- table.remove(_Windows.structOrder, parse)
- undrawWindow(ind,clearColor)
- winSel = false
- os.queueEvent("window_minimized",ind)
- end
- function checkBehind(ind)
- local winInd = 0
- local wx, wy, ww, wh = getWindowSize(ind)
- for i = 1, #_Windows.structOrder do
- if(_Windows.structOrder[i] == ind)then
- winInd = i
- elseif(winInd ~= 0)then
- local sx, sy, sw, sh = getWindowSize(i)
- if( wx+ww < sx+sw and sx > wx and
- sy > wy and wy+wh < sy+sh )then
- return _Windows.structOrder[i]
- end
- end
- end
- return ind
- end
- function getWindowObject(ind)
- return _Windows[ind]
- end
- function openWindow(ind)
- -- Place window of ID, into structure.
- for i = 1, #_Windows.structOrder do
- if(_Windows.structOrder[i] == ind)then
- return false
- end
- end
- _Windows.structOrder[#_Windows.structOrder+1] = ind
- renderWindows(1)
- os.queueEvent("window_maximized",ind)
- end
- function deleteWindow(ind)
- -- Take window out of structure.
- local parse = 0
- for i = 1, #_Windows.structOrder do
- if(_Windows.structOrder[i] == ind)then
- parse = i
- end
- end
- if(parse == 0)then return false end
- table.remove(_Windows.structOrder, parse)
- undrawWindow(ind,clearColor)
- winSel = false
- table.remove(_Windows,ind)
- os.queueEvent("window_closed",ind)
- end
- function _CallWindowEvent(ind,e)
- local parse = 0
- for i = 1, #_Windows.structOrder do
- if(_Windows.structOrder[i] == ind)then
- parse = i
- end
- end
- if(parse ~= 0)then
- _Windows[ind]:update(e)
- end
- end
- function updateWindows(e, rd)
- rd = rd or true
- if(#_Windows.structOrder > 0)then
- -- Window draging/selecting.
- if(e[1] == "mouse_drag" and winSel)then
- term.current().setVisible(false)
- moveWindow(getFrontIndex(),e[3]-selOff,e[4],rd)
- renderWindows(1)
- term.current().setVisible(true)
- end
- if(e[1] == "mouse_click" or e[1] == "mouse_drag")then
- winSel = false
- for i = #_Windows.structOrder, 1, -1 do
- local win = _Windows.structOrder[i]
- if(e[3] >= _Windows[win].winX and e[3] <= _Windows[win].winX+_Windows[win].winW and
- e[4] >= _Windows[win].winY and e[4] <= _Windows[win].winY+_Windows[win].winH and
- not (e[3] >= _Windows[getFrontIndex()].winX and e[3] <= _Windows[getFrontIndex()].winX+_Windows[getFrontIndex()].winW and
- e[4] >= _Windows[getFrontIndex()].winY and e[4] <= _Windows[getFrontIndex()].winY+_Windows[getFrontIndex()].winH and isWindowOpen(getFrontIndex())) )then
- moveToFront(win)
- term.current().setVisible(false)
- renderWindows(1)
- term.current().setVisible(true)
- winSel = false
- elseif(e[3] >= _Windows[win].winX and e[3] <= _Windows[win].winX+_Windows[win].winW and e[4] == _Windows[win].winY)then
- if(not (e[3] >= _Windows[getFrontIndex()].winX and e[3] <= _Windows[getFrontIndex()].winX+_Windows[getFrontIndex()].winW and
- e[4] >= _Windows[getFrontIndex()].winY and e[4] <= _Windows[getFrontIndex()].winY+_Windows[getFrontIndex()].winH and isWindowOpen(getFrontIndex()))
- or win == getFrontIndex())then
- winSel = true
- selOff = (e[3] - _Windows[win].winX)
- moveToFront(win)
- term.current().setVisible(false)
- renderWindows(1)
- term.current().setVisible(true)
- end
- end
- end
- end
- if(e[1] == "mouse_up" and winSel)then
- winSel = false
- end
- _CallWindowEvent(getFrontIndex(),e)
- end
- end
- function getWindowSize(ind)
- return _Windows[ind].winX, _Windows[ind].winY, _Windows[ind].winW, _Windows[ind].winH
- end
- function getWindowProperty(ind,prop)
- return _Windows[ind][prop]
- end
- function runWindowsDemo(ind)
- if(ind == 1)then
- setBackColor(colors.lightBlue)
- addWindow("One",5,5,10,10,function(self, e)
- if(e[1] == "mouse_click")then
- if(e[3] == self.winX+self.winW-1 and e[4] == self.winY)then
- closeWindow(self.winNumber)
- renderWindows(1)
- end
- end
- end)
- addWindow("Two",20,5,20,10,function(self, e)
- local old = term.current()
- term.redirect(self.win)
- term.setBackgroundColor(colors.blue)
- term.setCursorPos(3,3)
- write("Test: ")
- term.setCursorPos(8,3)
- self.winPut, self.isDone = getWindowedInput(8,3,e,self.winPut,"",10)
- if(self.isDone)then
- self.winPut = ""
- self.isDone = false
- end
- term.redirect(old)
- if(e[1] == "mouse_click")then
- if(e[3] == self.winX+self.winW-1 and e[4] == self.winY)then
- closeWindow(self.winNumber)
- renderWindows(1)
- end
- elseif(e[1] == "key" and e[2] == keys.y)then
- if(not isWindowOpen(1))then openWindow(1) end
- end
- end)
- getWindowObject(2).winPut = ""
- getWindowObject(2).isDone = false
- addWindow("Static",10,4,20,12)
- getWindowByName("Static").setBackgroundColor(colors.lightGray)
- getWindowByName("Static").clear()
- wrapWindow(getWindowNameID("Static"), function()
- paintutils.drawLine(1,1,20,1,colors.blue)
- term.setCursorPos(1,1)
- write("Can't close me!")
- --term.setCursorPos(20,1)
- --term.blit("X","0","7")
- end)
- getWindowByName("One").setBackgroundColor(colors.lightGray)
- getWindowByName("One").clear()
- wrapWindow(getWindowNameID("One"), function()
- paintutils.drawLine(1,1,10,1,colors.blue)
- term.setCursorPos(1,1)
- write("Window 1!")
- term.setCursorPos(10,1)
- term.blit(":","0","7")
- end)
- getWindowByName("Two").setBackgroundColor(colors.lightGray)
- getWindowByName("Two").clear()
- wrapWindow(getWindowNameID("Two"),function()
- paintutils.drawLine(1,1,20,1,colors.blue)
- term.setCursorPos(1,1)
- write("Window Two!")
- term.setCursorPos(20,1)
- term.blit(":","0","7")
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(2,5)
- write("Press Y to open \n window 1.")
- end)
- renderWindows(1)
- _winTray = {}
- function update()
- while running do
- local e = {os.pullEvent()}
- -- Simple optimization "hack" to speed up draging windows around.
- if(e[1] ~= "mouse_drag")then
- term.setCursorPos(1, h)
- term.setBackgroundColor(colors.blue)
- term.clearLine()
- term.setCursorPos(1, h)
- if(e[1] == "window_minimized")then
- _winTray[#_winTray+1] = {winNumber = e[2],name=getWindowProperty(e[2],"name")}
- elseif(e[1] == "window_maximized")then
- for i = 1, #_winTray do
- if(e[2] == _winTray[i].winNumber)then
- table.remove(_winTray, i)
- end
- end
- end
- for i = 1, #_winTray do
- term.setBackgroundColor(colors.gray)
- write(_winTray[i].name:sub(1,4))
- term.setBackgroundColor(colors.blue)
- write(" ")
- if(e[1] == "mouse_click" and e[3] >= (i-1)*4 and e[3] <= ((i-1)*4)+3 and e[4] == h)then
- openWindow(_winTray[i].winNumber)
- table.remove(_winTray, i)
- break
- end
- end
- end
- updateWindows(e)
- end
- end
- update()
- elseif(ind == 2)then
- local usr, pss = "Admin", "password"
- local running = true
- term.setBackgroundColor(colors.cyan)
- term.clear()
- setBackColor(colors.cyan)
- addWindow("Password",10,8,20,8,
- function(self, e)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.blue)
- local isDone = false
- if(self.cinput == 1)then
- self.winusr, isDone = getWindowedInput(
- self.winX+7,self.winY+2,e,self.winusr,"",8)
- if(isDone)then
- self.cinput = 2
- end
- else
- self.winpss, isDone = getWindowedInput(
- self.winX+7, self.winY+4,e,self.winpss,"*",8)
- if(isDone)then
- if(self.winusr == usr and self.winpss == pss)then
- running = false
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- else
- self.winusr = ""
- self.winpss = ""
- self.cinput = 1
- self.win.redraw()
- end
- end
- end
- if(e[1] == "mouse_click" and e[3] == self.winX+self.winW-1 and e[4] == self.winY)then
- term.setCursorBlink(false)
- closeWindow(1)
- end
- end)
- getWindowObject(1).winusr = ""
- getWindowObject(1).winpss = ""
- getWindowObject(1).cinput = 1
- wrapWindow(1,function()
- local w, h = term.getSize()
- term.setBackgroundColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.gray)
- term.clearLine()
- term.setTextColor(colors.white)
- write("Enter Password")
- term.setCursorPos(w,1)
- term.setBackgroundColor(colors.red)
- write("X")
- term.setBackgroundColor(colors.white)
- term.setCursorPos(2,3)
- term.setTextColor(colors.black)
- write("User: ")
- term.setBackgroundColor(colors.blue)
- write(string.rep(" ",9))
- term.setBackgroundColor(colors.white)
- write("\n\n Pass: ")
- term.setBackgroundColor(colors.blue)
- write(string.rep(" ",9))
- end)
- closeWindow(1)
- while running do
- local e = {os.pullEvent()}
- updateWindows(e)
- if(e[1] == "key" and e[2] == keys.g and not isWindowOpen(1) )then
- openWindow(1)
- sleep(0.1)
- end
- end
- end
- end
- local targs = { ... }
- if(#targs < 1)then error("Usage: " .. shell.resolveProgram(shell.getRunningProgram()) .. " <demo number (1/2)>") end
- runWindowsDemo(tonumber(targs[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement