Advertisement
ndfjay

rUI

Apr 11th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.63 KB | None | 0 0
  1. w,h = term.getSize()
  2.  
  3. os.pullEvent = os.pullEventRaw
  4.  
  5. local name = "Rie-UI"
  6. local version = "0.1"
  7. local prefix = "Dev"
  8. local seperator = "_"
  9. local update = false
  10. local menu = "0"
  11. local notifications = {}
  12. local seenUpdate = false
  13. local cntx = false
  14. local bgIndex = 1
  15. backgrounds = api.list(root.."/wallpaper")
  16.  
  17. --Config
  18. backgroundColour = 1
  19. tbc = api.rColor("gray")
  20. bgIMG = backgrounds[2]
  21. tbTextColor = api.rColor("white")
  22. notificationBackground = api.rColor("gray")
  23. notificationText = api.rColor("white")
  24.  
  25. drawBackground = function(img)
  26.     api.rClear("-scr", {backgroundColour})
  27.     paintutils.drawImage(paintutils.loadImage(img), 1, 2)
  28. end
  29.  
  30. nextNotification = function()
  31.     local tmpT = {}
  32.     if #notifications >= 1 then
  33.         for i = 2, #notifications do
  34.             tmpT[i-1] = notifications[i]
  35.         end
  36.     end
  37.     notifications = tmpT
  38. end
  39.  
  40. local notif = {
  41.     [1] = {x=math.floor(w)-18, xx=math.floor(w)-14, y=math.floor(h)-1, txt="[Yes]", cmd=function() shell.run("/RUI/.ds_data/update") os.reboot() end},
  42.     [2] = {x=math.floor(w)-4, xx=math.floor(w), y=math.floor(h)-1, txt="[No]", cmd=function() menu = "0" update = true nextNotification() end}
  43. }
  44.  
  45. drawNotification = function(info)
  46.     for i = math.ceil(h)-5, math.ceil(h) do
  47.         api.rPrint("-r", {22, i}, {string.rep(" ", 20)}, {1, "gray"}, -1)
  48.     end
  49.     if notifications[1] == "Update" then
  50.         api.rPrint("-r", {1, math.floor(h)-4}, {"Update found!"}, {1, "gray"}, 1)
  51.         api.rPrint("-r", {1, math.floor(h)-3}, {"Update to "..nVersion.." now?"}, {1, "gray"}, 0)
  52.         term.setCursorPos(notif[1].x, notif[1].y)
  53.         write(notif[1].txt)
  54.         term.setCursorPos(notif[2].x, notif[2].y)
  55.         write(notif[2].txt)
  56.     end
  57. end
  58.  
  59.  
  60. --tables
  61. local context = {
  62.     [0] = {txt="-------------", tcol="lightGray", bcol=tbc, offX=0, cmd=function() end},
  63.     [1] = {txt=" New         ", tcol=tbTextColor, bcol=tbc, offX=0, cmd=function() end},
  64.     [2] = {txt=" S/H Icons   ", tcol=tbTextColor, bcol=tbc, offX=0, cmd=function() end},
  65.     [3] = {txt=" Edit Config ", tcol=tbTextColor, bcol=tbc, offX=0, cmd=function() shell.run("fg", "edit", "testConf") end},
  66.     [4] = {txt="-------------", tcol="lightGray", bcol=tbc, offX=0, cmd=function() end},
  67. }
  68.  
  69. local tBar = {
  70.     [1] = {x=1, xx=3, y=1, txt="(R)", col=tbTextColor, cmd=function() menu = "start" end, offX=0},
  71.     [2] = {x=math.floor(w-1), xx=math.floor(w), y=1, txt="v", col=tbTextColor, cmd=function() drawDropDown() end, offX=2},
  72.     [3] = {x=math.floor(w)+2, xx=math.floor(w)+2, y=1, txt="", col=api.rColor("red"), cmd=function()  menu = "update" end, offX=2}
  73. }
  74.  
  75. local start = {
  76.     [1] = {x=1, xx=13, y=2, txt="             ", tcol=tbTextColor, bcol=tbc, offX=0, cmd=function() end},
  77.     [2] = {x=1, xx=13, y=3, txt=" Marketplace ", tcol=tbTextColor, bcol=tbc, offX=0, cmd=function() menu="0" end},
  78.     [3] = {x=1, xx=13, y=4, txt=" Programs    ", tcol=tbTextColor, bcol=tbc, offX=0, cmd=function() menu="0" end},
  79.     [4] = {x=1, xx=13, y=5, txt=" Settings    ", tcol=tbTextColor, bcol=tbc, offX=0, cmd=function() menu="0" end},
  80.     [5] = {x=1, xx=13, y=6, txt="-------------", tcol=api.rColor("lightGray"), bcol=tbc, offX=0, cmd=function() end},
  81.     [6] = {x=1, xx=13, y=7, txt=" Terminal    ", tcol=tbTextColor, bcol=tbc, offX=0, cmd=function() api.rClear("-scr", {"black" ,1}) term.setCursorPos(1,1) term.setTextColor(api.rColor("yellow")) print("Welcome to the terminal") loggedin=false end},
  82.     [7] = {x=1, xx=13, y=8, txt="-------------", tcol=api.rColor("lightGray"), bcol=tbc, offX=0, cmd=function() end},
  83.     [8] = {x=1, xx=13, y=9, txt=" (S) (R) (L) ", tcol=tbTextColor, bcol=tbc, offX=0, cmd=function() end},
  84.     [9] = {x=1, xx=13, y=10, txt="             ", tcol=tbTextColor, bcol=tbc, offX=0, cmd=function() end},
  85.  
  86. }
  87.  
  88. --functions
  89.  
  90. drawContext = function(...)
  91.     cOpts = {...}
  92.  
  93.     for i,v in pairs(context) do
  94.         api.rPrint("-l", {cOpts[1], tonumber(cOpts[2]+i)-1}, {v.txt}, {v.tcol, v.bcol}, 0)
  95.     end
  96. end
  97.  
  98. drawTaskBar = function(...)
  99.     tBarArgs = {...}
  100.     api.rClear("-ln", {tbc, 1})
  101.     api.rPrint("-l", {tBar[1].x, tBar[1].y}, {tBar[1].txt}, {tBar[1].col}, tBar[1].offX)
  102.     api.rPrint("-l", {tBar[2].x, tBar[2].y}, {tBar[2].txt}, {tBar[2].col}, tBar[2].offX)
  103.     api.rPrint("-l", {tBar[3].x, tBar[3].y}, {tBar[3].txt}, {tBar[3].col}, tBar[3].offX)
  104.     for k,v in pairs(tBarArgs) do
  105.         v()
  106.     end
  107. end
  108.  
  109. local prevClick = {}
  110. mouseClick = function(...)
  111.     btns = {...}
  112.     if btns[1] == 2 then
  113.         cntx = true
  114.         tmpCx = btns[2]
  115.         tmpCy = btns[3]
  116.         if btns[2] >= math.floor(w)-13 then tmpCx = math.floor(w)-13 end
  117.         if btns[3] >= math.floor(h-1)-#context then tmpCy = math.floor(h-1)-#context end
  118.         if btns[3] == 1 then tmpCy = 2 end
  119.         drawContext(tmpCx, tmpCy+1)
  120.         prevClick = {tmpCx, tmpCy}
  121.         return true
  122.     end
  123.     if cntx then
  124.         for k,v in pairs(context) do
  125.             if btns[2] >= prevClick[1] and btns[2] <= prevClick[1]+#v.txt and btns[3] == tonumber(prevClick[2]+k) and btns[1] == 1 then
  126.                 v.cmd()
  127.                 cntx = false
  128.                 return true
  129.             end
  130.         end
  131.     else
  132.         for i,v in pairs(btns[4]) do
  133.             for k,v in pairs(btns[4][i]) do
  134.                 if btns[2] >= v.x and btns[2] <= v.xx and btns[3] == v.y and btns[1] == 1 then
  135.                     v.cmd()
  136.                     return true
  137.                 end
  138.             end
  139.         end
  140.     end
  141.     if menu ~= "update" then
  142.         menu = "0"
  143.     end
  144.     cntx = false
  145. end
  146.  
  147. drawMenus = function(...)
  148.     mOpts = {...}
  149.     for k,v in pairs(mOpts[1]) do
  150.         api.rPrint("-l", {v.x, v.y}, {v.txt}, {v.tcol, v.bcol}, 0)
  151.     end
  152. end
  153.  
  154. power = function(MtmpX, MtmpY, btnPressed)
  155.     btnY = #start
  156.     if MtmpX >= 2 and MtmpX <=4 and MtmpY == btnY then
  157.         os.shutdown()
  158.     elseif MtmpX >= 6 and MtmpX <=8 and MtmpY == btnY then
  159.         os.reboot()
  160.     elseif MtmpX >= 10 and MtmpX <=12 and MtmpY == btnY then
  161.         api.rClear("-scr", {"black" ,1}) term.setCursorPos(1,1)
  162.         shell.run("/RUI/.ds_data/.auth", "false")
  163.         loggedin=false
  164.     else
  165.         mouseClick(btnPressed, MtmpX, MtmpY, {start})
  166.     end
  167. end
  168.  
  169. drawDesktop = function()
  170.     drawBackground(bgIMG)
  171.     api.rPrint("-r", {1, math.ceil(h)}, {prefix..seperator..version}, {"black"}, 0)
  172.     drawTaskBar()
  173. end
  174.  
  175. nextBackground = function()
  176. backgrounds = api.list(root.."/wallpaper")
  177.     if bgIndex == #backgrounds then
  178.         bgIMG = backgrounds[1]
  179.         bgIndex = 1
  180.     else
  181.         bgIndex = bgIndex + 1
  182.         bgIMG = backgrounds[bgIndex]
  183.     end
  184.     drawDesktop()
  185. end
  186.  
  187. local dropDown = {
  188.     [1] = {x=math.floor(w)-11, xx=math.floor(w), y=2, txt=" ---------- ", tcol=api.rColor("lightGray"), bcol=tbc, offX=0, cmd=function() end},
  189.     [2] = {x=math.floor(w)-11, xx=math.floor(w), y=3, txt=" Background ", tcol=tbTextColor, bcol=tbc, offX=0, cmd=function() nextBackground() end},
  190.     [3] = {x=math.floor(w)-11, xx=math.floor(w), y=4, txt=" ---------- ", tcol=api.rColor("lightGray"), bcol=tbc, offX=0, cmd=function() end}
  191. }
  192.  
  193. drawDropDown = function()
  194.     for k,v in pairs(dropDown) do
  195.         api.rPrint("-l", {v.x, k+1}, {v.txt}, {v.tcol, v.bcol}, v.offX)
  196.     end
  197.     menu="drop"
  198. end
  199.  
  200. -- init
  201. local init = function(...)
  202.     initArgs = {...}
  203.     api.rClear("-a", {1,1})
  204.     drawBackground(bgIMG)
  205.     api.rPrint("-r", {1, math.ceil(h)}, {prefix..seperator..version}, {"black"}, 0)
  206.     drawTaskBar()
  207. end
  208.  
  209. -- Main Code
  210. nVersion = http.get("https://pastebin.com/raw/Rrcssfup").readAll()
  211. init()
  212.  
  213. main = function()
  214.     while loggedin do
  215.     if menu ~= "0" then
  216.         if menu == "start" then
  217.             drawMenus(start)
  218.         end
  219.     end
  220.     local e = {os.pullEvent()}
  221.     if e[1] == "mouse_click" then
  222.         drawDesktop()
  223.         if menu == "update" then
  224.             if #notifications > 0 then
  225.                 mouseClick(e[2], e[3], e[4], {notif, tBar})
  226.             end
  227.         end
  228.         if menu == "drop" then
  229.             if #notifications > 0 then
  230.                 mouseClick(e[2], e[3], e[4], {notif, tBar, dropDown})
  231.             else
  232.                 mouseClick(e[2], e[3], e[4], {tBar, dropDown})
  233.             end
  234.         end
  235.         if e[3] > 1 and e[4] == 1  and menu ~= "update" then
  236.             mouseClick(e[2], e[3], e[4], {tBar})
  237.         end
  238.         if e[4] > 1 then
  239.             if menu ~= "0" then
  240.                 if menu == "start" then
  241.                     power(e[3], e[4], e[2])
  242.                 end
  243.             end
  244.         end
  245.         if e[3] > 1 and e[4] > 1 then
  246.             mouseClick(e[2], e[3], e[4], {tBar})
  247.         end
  248.     end
  249.     end
  250. end
  251.  
  252. notificationHandler = function()
  253.     while loggedin do
  254.         sleep(1)
  255.         nVersion = http.get("https://pastebin.com/raw/Rrcssfup").readAll()
  256.         if nVersion ~= version and not update and not seenUpdate then
  257.             notifications[#notifications+1] = "Update"
  258.             seenUpdate = true
  259.         end
  260.         if #notifications < 1 then
  261.             tBar[2].x = math.floor(w)-1
  262.             tBar[2].xx = math.floor(w)-1
  263.             tBar[3].x = math.floor(w)+2
  264.             tBar[3].xx = math.floor(w)+2
  265.             tBar[3].txt=""
  266.         else
  267.             tBar[2].x = math.floor(w)-4
  268.             tBar[2].xx = math.floor(w)-4
  269.             tBar[3].x = math.floor(w)-2
  270.             tBar[3].xx = math.floor(w)
  271.             tBar[3].txt="(i)"
  272.             for i,v in pairs(notifications) do
  273.                 if notifications[i] == "Update" then
  274.                     menu = "update"
  275.                     drawNotification("Update")
  276.                 end
  277.             end
  278.         end
  279.         drawTaskBar()
  280.     end
  281. end
  282.  
  283. parallel.waitForAll(main, notificationHandler)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement