Lion4ever

Pause for Advanced Computers

May 31st, 2015
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.18 KB | None | 0 0
  1. local pauseKey = 68 --f10
  2. local ignoreEvent = {mouse_click=true,term_resize=true}
  3. local dropEvent = {key=true,char=true,mouse_click=true,mouse_scroll=true,paste=true,terminate=true}
  4. local password = ""
  5.  
  6. if #password > 0 then
  7.   ignoreEvent["char"] = false
  8. end
  9.  
  10. local paused = false
  11. local qEvents = {}
  12. local pchars = 0
  13. local ptyped
  14. local needRedraw = false
  15. local dragX,dragY
  16. local oldPull = os.pullEventRaw
  17.  
  18. local tx,ty = term.native().getSize()
  19. local nTerm = term.native()
  20. local pTerm = term.current()
  21. local mx,my = (#password > 5 and #password+5 or 10),(#password>0 and 7 or 5)
  22. ptyped = #password > 0 and mx-5 or 0
  23. local pauseImg = window.create(nTerm, math.floor((tx-mx)/2), math.floor((ty-my)/2),mx,my,false)
  24. if nTerm.isColor() then
  25.   pauseImg.setBackgroundColor(colors.gray)
  26.   pauseImg.clear()
  27.   pauseImg.setBackgroundColor(colors.lightGray)
  28.   for i=2,my-1 do
  29.     pauseImg.setCursorPos(1,i)
  30.     pauseImg.write(" ")
  31.     pauseImg.setCursorPos(mx,i)
  32.     pauseImg.write(" ")
  33.   end
  34.   for i=1,mx do
  35.     pauseImg.setCursorPos(i,1)
  36.     pauseImg.write(" ")
  37.     pauseImg.setCursorPos(i,my)
  38.     pauseImg.write(" ")
  39.   end
  40.   pauseImg.setBackgroundColor(colors.gray)
  41.   pauseImg.setTextColor(colors.green)
  42. else
  43.   for i=2,my-1 do
  44.     pauseImg.setCursorPos(1,i)
  45.     pauseImg.write("|")
  46.     pauseImg.setCursorPos(mx,i)
  47.     pauseImg.write("|")
  48.   end
  49.   for j=0,1 do
  50.     pauseImg.setCursorPos(1,j*(my-1)+1)
  51.     pauseImg.write("#")
  52.     for i=2,mx-1 do
  53.       pauseImg.write("-")
  54.     end
  55.     pauseImg.write("#")
  56.   end
  57. end
  58. pauseImg.setCursorPos(mx/2-2,3)
  59. pauseImg.write("Paused")
  60. pauseImg.setCursorPos(3,5)
  61. pauseImg.setBackgroundColor(colors.black)
  62. if #password>0 then
  63.   pauseImg.write(">")
  64.   pauseImg.setCursorBlink(true)
  65. end
  66.  
  67. local function osQueue()
  68.   for i,j in pairs(qEvents) do
  69.     os.queueEvent(unpack(j))
  70.   end
  71.   qEvents = {}
  72.   if multishell then
  73.     --term.current().redraw()
  74.     --multishell.setFocus(multishell.getFocus())
  75.     os.queueEvent("term_resize")
  76.   else
  77.     term.clear()
  78.     term.setCursorPos(1,1)
  79.   end
  80. end
  81.  
  82. local function clrPw()
  83.   pchars = 0
  84.   pauseImg.setCursorPos(4,5)
  85.   for i=1,ptyped do
  86.     pauseImg.write(" ")
  87.   end
  88.   pauseImg.setCursorPos(4,5)
  89.   ptyped=0
  90. end
  91.  
  92. if #password>0 then
  93.   clrPw()
  94. end
  95.  
  96. local function onImg(r)
  97. local px,py = pauseImg.getPosition()
  98. return (r[1] == "mouse_click" or r[1] == "mouse_drag") and r[3] >= px and r[3] <= px+mx and r[4] >= py and r[4] <= py+my
  99. end
  100.  
  101. os.pullEventRaw = function(filter)
  102.   while true do
  103.     local r = {oldPull()}
  104.     if needRedraw then
  105.       pauseImg.redraw()
  106.       needRedraw = false
  107.     end
  108.     if r[1] == "key" and r[2] and (r[2] == pauseKey or (r[2] == 28 and paused)) and (pchars == #password or not paused) then
  109.       paused = not paused
  110.       pauseImg.setVisible(paused)
  111.       if not paused then
  112.         osQueue()
  113.         clrPw()
  114.       end
  115.     elseif paused and ((not ignoreEvent[r[1]]) or onImg(r)) then
  116.       if r[1] == "terminate" and pchars == #password then
  117.         --print("Pause Programm unloaded")
  118.         os.pullEventRaw = oldPull
  119.         paused=false
  120.         pauseKey = -1 --Because of other instances
  121.         osQueue()
  122.         return oldPull(filter)
  123.       end
  124.       if r[1] == "char" and r[2] and #password > 0 then
  125.         if ptyped < mx - 5 then
  126.           ptyped = ptyped + 1
  127.           pauseImg.write("*")
  128.         end
  129.         pchars = pchars + 1
  130.         if r[2] ~= password:sub(pchars,pchars) then
  131.           pchars = -1 --r[2] == password:sub(1,1) and 1 or 0
  132.         end
  133.       elseif r[1] == "key" and r[2] and (r[2] == 14 or r[2] == pauseKey or r[2] == 28) and #password>0 then
  134.         clrPw()
  135.       elseif r[1] == "mouse_click" and onImg(r) then
  136.         local px,py = pauseImg.getPosition()
  137.         dragX,dragY = r[3]-px,r[4]-py
  138.       elseif r[1] == "mouse_drag" and dragX then
  139.         pauseImg.reposition(r[3]-dragX,r[4]-dragY)
  140.         os.queueEvent("term_resize")
  141.         --pauseImg.redraw()
  142.       elseif not dropEvent[r[1]] then
  143.         table.insert(qEvents,r)
  144.       end
  145.     elseif not filter or r[1]==filter or (r[1]=="terminate" and not paused) then
  146.       if paused then
  147.         needRedraw = true
  148.       end
  149.       return unpack(r)
  150.     end
  151.   end
  152. end
Advertisement
Add Comment
Please, Sign In to add comment