Guest User

desktop

a guest
Aug 15th, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. local bg = colors.orange
  2. local doubleclicktimeout = 0.4
  3.  
  4.  
  5. local apps = files.loadTableFromFile("/.bacon/appdata")
  6. local icon = paintutils.loadImage("/.bacon/theme")
  7.  
  8. local lstc = os.clock()
  9.  
  10. local selected = ""
  11.  
  12. local tx,ty = term.getSize()
  13.  
  14. function drawIcon(x,y,name)
  15.   for i=1,3 do
  16.     for j=1,4 do
  17.       term.setCursorPos(j+x-1,i+y-1)
  18.       term.setBackgroundColor(icon[i][j])
  19.       buffer.write(" ")
  20.     end
  21.   end
  22.   if name==selected then
  23.     term.setBackgroundColor(colors.cyan)
  24.   else
  25.     term.setBackgroundColor(bg)
  26.   end
  27.   term.setCursorPos(x,y+3)
  28.   buffer.write(name:sub(1,4))
  29.   term.setBackgroundColor(bg)
  30. end
  31.  
  32. function draw()
  33.   term.setBackgroundColor(bg)
  34.   term.setTextColor(colors.black)
  35.   buffer.clear()
  36.   for i=1,#apps do
  37.     drawIcon((apps[i].x-1)*5+2,(apps[i].y-1)*5+2,apps[i].name)
  38.   end
  39.   term.setCursorPos(1,ty)
  40.   term.setBackgroundColor(colors.pink)
  41.   buffer.write(string.rep(" ",tx))
  42.   term.setCursorPos(1,ty)
  43.   buffer.write("Menu|")
  44.   buffer.flush()
  45. end
  46.  
  47. function inapp(x,y,app)
  48.   if x>=((app.x-1)*5+2) then
  49.     if x<=((app.x-1)*5+5) then
  50.       if y>=((app.y-1)*5+2) then
  51.         if y<=((app.y-1)*5+6) then
  52.           return true
  53.         end
  54.       end
  55.     end
  56.   end
  57.   return false
  58. end
  59.  
  60. function sleep()
  61.   term.setBackgroundColor(colors.black)
  62. --  term.setTextColor(colors.gray)
  63.   for i=1,ty do
  64.     term.setCursorPos(1,i)
  65.     --write(string.rep(" ",tx))
  66.     sleep(0.01)
  67.   end
  68.   term.setCursorPos(2,ty-1)
  69.   write("Touch To Wake")
  70.   os.pullEvent("mouse_click")
  71. end
  72.  
  73. clkt = doubleclicktimeout --To shorten the name for code usage
  74. while true do
  75.   draw()
  76.   local e, p1, p2, p3 = os.pullEvent()
  77.   if e=="mouse_click" then
  78.     local b,x,y = p1,p2,p3
  79.     if b==1 then
  80.       if x<6 and y==ty then
  81.         local cm = menu.new(1,ty-1)
  82.         cm:addMenuButton("Shutdown",function() os.shutdown() end)
  83.         cm:addMenuButton("Reboot",function() os.reboot() end)
  84.         cm:addMenuButton("Sleep",sleep)
  85.         cm:captureButton()
  86.       else
  87.         local found = false
  88.         for i=1,#apps do
  89.           if inapp(x,y,apps[i]) then
  90.             found = true
  91.             if os.clock()<=lstc+clkt and selected == apps[i].name then
  92.               --Run app
  93.               term.setBackgroundColor(colors.black)
  94.               term.setTextColor(colors.white)
  95.               term.clear()
  96.               term.setCursorPos(1,1)
  97.               shell.run(apps[i].exec)
  98.             else
  99.               selected = apps[i].name
  100.             end
  101.             lstc = os.clock()
  102.           end
  103.         end
  104.         if not found then
  105.           selected = ""
  106.         end
  107.       end
  108.     end
  109.   end
  110. end
Advertisement
Add Comment
Please, Sign In to add comment