Advertisement
Marlingaming

Display Application and Desktop Management system

Mar 28th, 2022 (edited)
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.52 KB | None | 0 0
  1. local w, h = term.getSize()
  2. local Entities = {}
  3. local Application
  4. local GameMode = false
  5. local AllowScroll = false
  6. local Screen_Background = colors.lightBlue
  7. local Screen_TextColor = colors.white
  8. local InputActive = -1
  9. local Desktop_Background
  10. local User_Name
  11. local Notifications = {}
  12. local NotTimer = -1
  13. local Popup_Data = -1
  14. local Desk_Apps = {}
  15. local Dropdown = false
  16. local Desk_Page = 1
  17. local Desk_EndPage = 3
  18. local Desktop_LineColors = {}
  19.  
  20. settings.load("os/settings.txt")
  21.  
  22. local Desktop_Back = settings.get("screen_desktop_background")
  23. local Dropdown_Back = settings.get("screen_dropdown_background")
  24.  
  25. local function Clear()
  26. term.clear()
  27. term.setCursorPos(1,1)
  28. end
  29.  
  30. function Startup()
  31. if h > settings.get("screen_maxHeight") then h = settings.get("screen_maxHeight") end
  32. term.setBackgroundColor(colours.black)
  33. Clear()
  34. os.sleep(1)
  35. local Image = paintutils.loadImage(Desktop_Back)
  36. paintutils.drawImage(Image,1,2)
  37.  
  38. os.sleep(2)
  39. Start()
  40. end
  41.  
  42. function Start()
  43. DesktopDraw()
  44. GetHomeApps()
  45. DrawApps()
  46. if settings.get("active_user") == "Guest" then os.queueEvent("system_notification","system","your account is not setup. click here to fix that!","os/System/Programs/SettingsApp.lua") end
  47. Loop()
  48. end
  49.  
  50. function Loop()
  51. while true do
  52. local event, a, b, c, d, e, f, g = os.pullEvent()
  53. if NotTimer ~= -1 and os.time() >= NotTimer then
  54.     NotTimer = -1
  55.     Popup_Data = -1
  56.     if Dropdown == false then
  57.         if Application == nil then
  58.             DesktopDraw()
  59.             DrawApps()
  60.         else
  61.             DrawScreen()
  62.             TopBarDraw()
  63.         end
  64.     end
  65. end
  66.        
  67.    
  68. if string.find(event,"system") then
  69.     if event == "system_notification" then
  70.         Notifications[#Notifications + 1] = {a,b,os.time(),c,d}
  71.         Popup_Data = #Notifications
  72.         NotTimer = os.time() + 0.40
  73.         Popup(a,b)
  74.     elseif event == "system_update" then
  75.  
  76.     elseif event == "system_alert" then
  77.  
  78.     elseif event == "system_error" then
  79.  
  80.     end
  81.  
  82. elseif Dropdown == true then
  83.     ManageDropdown(event,a,b,c,d)
  84. elseif Application == nil then
  85.     if event == "mouse_click" then
  86.         if c == 1 then
  87.             DropToggle()
  88.         elseif NotTimer ~= -1 and c == 3 and b < w/8 + 0.5 and b > w/8 - 0.75 then
  89.             ClosePopup()
  90.         elseif Popup_Data ~= -1 and c >= 3 and c <= 7 and b > w/8 + 0.5 and b <= w - w/8 then
  91.             NotTimer = -1
  92.             OpenNotification(Popup_Data)
  93.         elseif ( b == 1 and Desk_Page > 1) == true or (b == w and Desk_Page < Desk_EndPage) == true then
  94.             if b == 1 and Desk_Page > 1 then Desk_Page = Desk_Page - 1 else Desk_Page = Desk_Page + 1 end
  95.             DesktopDraw()
  96.             DrawApps()
  97.             if Popup_Data ~= -1 then Popup() end
  98.         else
  99.             local App = CheckApps(b,c)
  100.             if App ~= false then RunApp(App) end
  101.         end
  102.     elseif event == "key" then
  103.         if a == keys.m then DropToggle() end
  104.     elseif event == "desktop_update" then
  105.  
  106.     end
  107. elseif Application ~= nil then
  108.     if GameMode == true then
  109.         GameMode = false
  110.         CloseApp()
  111.     elseif event == "mouse_click" then
  112.         local Entity = CheckEntities(b,c)
  113.         if b > w/6 and b < w - (w/6) and c == h and Entity == false then
  114.             CloseApp()
  115.         elseif c == h - 1 and AllowScroll == true then
  116.             term.scroll(1)
  117.             DrawScreen()
  118.         elseif InputActive ~= -1 and ((b < Entities[InputActive][5]) == true or (b > Entities[InputActive][4]) == true or (c > Entities[InputActive][6]) == true or (c < Entities[InputActive][6]) == true) == true then
  119.             InputActive = -1
  120.         elseif c == 1 then
  121.             DropToggle()
  122.         elseif NotTimer ~= -1 and c == 3 and b < w/8 + 0.5 and b > w/8 - 0.5 then
  123.             ClosePopup()
  124.         elseif NotTimer ~= -1 and c >= 3 and c <= 7 and b >= w/8 and b <= w - w/8 then
  125.             NotTimer = -1
  126.             OpenNotification(Popup_Data)
  127.             Popup_Data = -1
  128.         elseif Entity ~= false then
  129.             RunEntity(Entity,"click")
  130.         end
  131.     elseif InputActive ~= -1 and event == "char" then
  132.         EditInput(a)
  133.     elseif event == "key" then
  134.         if a == keys.m then
  135.             DropToggle()
  136.         elseif a == keys.x then
  137.             CloseApp()
  138.         elseif a == keys.enter and InputActive ~= -1 then
  139.             shell.run(Application,Entities[InputActive][1],Entities[InputActive][3])
  140.             InputActive = -1
  141.         end
  142.     elseif event == "application_task" then
  143.         Task(a,b,c,d,e,f,g)
  144.     end
  145. end
  146. UI()
  147. os.queueEvent("pass")
  148. end
  149. end
  150. --Toggle
  151. function DropToggle()
  152. if Dropdown == true then
  153.     Dropdown = false
  154.     if Application == nil then
  155.         DesktopDraw()
  156.         DrawApps()
  157.     else
  158.         term.setBackgroundColor(Screen_Background)
  159.         Clear()
  160.         TopBarDraw()
  161.         DrawScreen()
  162.     end
  163.     if Popup_Data ~= -1 then Popup() end
  164. else
  165.     Dropdown = true
  166.     DrawDropdown()
  167. end
  168. end
  169.  
  170. --Files
  171. function GetHomeApps()
  172. local Path = "os/Desktop_Shortcuts"
  173. local List = fs.list(Path)
  174. local X = 3
  175. local Y = 3
  176. local Page = 1
  177.  
  178. for i = 1, #List do
  179.     if string.find(List[i],".lnk") then
  180.         local file = fs.open(fs.combine(Path,List[i]),"r")
  181.         local Ap = file.readLine()
  182.         if string.find(Ap,".cpf") or string.find(Ap,".lua") then
  183.             Desk_Apps[#Desk_Apps + 1] = {Ap,file.readLine(),file.readLine(),X,Y,Page}
  184.             if X + 7 < 22 then
  185.                 X = X + 7
  186.             else
  187.                 X = 3
  188.                 Y = Y + 6
  189.             end
  190.         end
  191.     end
  192. end
  193. end
  194.  
  195. --Run
  196. function RunApp(App)
  197. term.setBackgroundColor(Screen_Background)
  198. Clear()
  199. TopBarDraw()
  200. Application = Desk_Apps[App][1]
  201. shell.run(Application,"start")
  202. end
  203.  
  204. function MSG_RunApp(App,ar)
  205. term.setBackgroundColor(Screen_Background)
  206. Clear()
  207. TopBarDraw()
  208. Application = App
  209. shell.run(Application,"start",ar)
  210. end
  211.  
  212. function RunEntity(i,action)
  213. if Entities[i][2] == "button" then
  214.     if string.find(Entities[i][4],"lnk.") then
  215.         local Ap = string.sub(Entities[i][4],5,string.len(Entities[i][4]))
  216.         CloseApp()
  217.         MSG_RunApp(Ap)
  218.     elseif string.find(Entities[i][4],"tsk.") then
  219.         shell.run(Application,string.sub(Entities[i][4],5,string.len(Entities[i][4])))
  220.     end
  221. elseif Entities[i][2] == "input" then
  222.     InputActive = i
  223. end
  224. end
  225.  
  226. function OpenNotification(I)
  227. if Notifications[I][4] ~= nil then
  228.     MSG_RunApp(Notifications[I][4],Notifications[I][5])
  229.     if I == Popup_Data then
  230.         Popup_Data = -1
  231.         NotTimer = -1
  232.     end
  233.     Notifications[I] = nil
  234. else
  235.     if I == Popup_Data then
  236.         Popup_Data = -1
  237.         NotTimer = -1
  238.     end
  239.     Notifications[I] = nil
  240.     if Dropdown == false then
  241.         if Application == nil then
  242.             DesktopDraw()
  243.             DrawApps()
  244.         else
  245.             DrawScreen()
  246.         end
  247.     else
  248.         DrawDropdown()
  249.     end
  250. end
  251. end
  252.  
  253. --states
  254. function CloseApp()
  255. shell.run(Application,"stop")
  256. Application = nil
  257. Entities = {}
  258. DesktopDraw()
  259. DrawApps()
  260. InputActive = -1
  261. AllowScroll = false
  262. end
  263.  
  264. function ClosePopup()
  265. NotTimer = -1
  266. Popup_Data = -1
  267. if Application == nil then
  268.     DesktopDraw()
  269.     DrawApps()
  270. else
  271.     DrawScreen()
  272. end
  273. end
  274.  
  275. --Check
  276. function CheckApps(X,Y)
  277. local Res = false
  278. for i = 1, #Desk_Apps do
  279.     if Desk_Apps[i][4] <= X and Desk_Apps[i][4] + 5 >= X and Desk_Apps[i][5] <= Y and Desk_Apps[i][5] + 4 >= Y and Desk_Apps[i][6] == Desk_Page then Res = i end
  280. end
  281. return Res
  282. end
  283.  
  284. function CheckEntities(X,Y)
  285. local X1 = 0
  286. local X2 = 0
  287. local Y1 = 0
  288. local Y2 = 0
  289. local R = false
  290. for i = 1, #Entities do
  291.     if Entities[i][2] == "button" then
  292.         X1 = Entities[i][5]
  293.         Y1 = Entities[i][6]
  294.         X2 = X1 + string.len(Entities[i][3])
  295.         Y2 = Y1
  296.         if X >= X1 and X <= X2 and Y >= Y1 and Y <= Y2 then R = i end
  297.     elseif Entities[i][2] == "input" then
  298.         X1 = Entities[i][5]
  299.         Y1 = Entities[i][6]
  300.         X2 = Entities[i][4]
  301.         Y2 = Y1
  302.         if X >= X1 and X <= X2 and Y >= Y1 and Y <= Y2 then R = i end
  303.     end
  304. end
  305. return R
  306. end
  307.  
  308. function EditInput(char)
  309. Entities[InputActive][3] = Entities[InputActive][3]..char
  310. DrawScreen()
  311. end
  312.  
  313. function Task(a,b,c,d,e,f,g)
  314. if string.find(a,"create") then
  315.     if a == "create_text" then--id,text,x,y
  316.         Entities[#Entities + 1] = {b,"text",c,d,e+1}
  317.     elseif a == "create_button" then--id,text,lnk/action,x,y
  318.         Entities[#Entities + 1] = {b,"button",c,d,e,f+1}
  319.     elseif a == "create_image" then--id,file,x,y
  320.         Entities[#Entities + 1] = {b,"image",c,d,e+1}
  321.     elseif a == "create_input" then--id,X2,X1,Y
  322.         Entities[#Entities + 1] = {b,"input","",c,d,e+1}
  323.     elseif a == "create_dropdown" then
  324.         Entities[#Entities + 1] = {b,"drop",{},c,d+1}
  325.     end
  326. elseif string.find(a,"set") then
  327.     if a == "set_background" then
  328.         Screen_Background = b
  329.     elseif a == "set_textcolor" then
  330.         Screen_TextColor = b
  331.     elseif a == "set_scroll" then
  332.         AllowScroll = b
  333.     elseif a == "set_application" then
  334.         if b == nil then
  335.             CloseApp()
  336.         else
  337.             MSG_RunApp(b)
  338.         end
  339.     end
  340. elseif a == "edit" then
  341.     local I = FindEntId(b)
  342.     if I == false then
  343.  
  344.     elseif c == "text" then
  345.         Entities[I][3] = d
  346.     elseif c == "cords" then
  347.         if Entities[I][2] == "text" then
  348.             Entities[I][4] = d
  349.             Entities[I][5] = e
  350.         elseif Entities[I][2] == "button" then
  351.             Entities[I][5] = d
  352.             Entities[I][6] = e
  353.         elseif Entities[I][2] == "image" then
  354.             Entities[I][4] = d
  355.             Entities[I][5] = e
  356.         end
  357.     end
  358. elseif a == "runGame" then
  359.     Application = b
  360.     GameMode = true
  361. elseif a == "get" then
  362.     shell.run(Application,"item",Entities[b][2])
  363. else
  364.     if a == "clearEntities" then
  365.         InputActive = -1
  366.         Entities = {}
  367.     end
  368. end
  369. DrawScreen()
  370. end
  371.  
  372. function FindEntId(ID)
  373. local Result
  374. for i = 1, #Entities do
  375.     if Entities[i][1] == ID then Result = i end
  376. end
  377. if Result == nil then
  378.     return false
  379. else
  380.     return Result
  381. end
  382. end
  383.  
  384. --Display
  385. function DesktopDraw()
  386. Clear()
  387. local Image = paintutils.loadImage(Desktop_Back)
  388. paintutils.drawImage(Image,1,2)
  389. TopBarDraw()
  390. end
  391.  
  392. function DrawApps()
  393. for i = 1, #Desk_Apps do
  394.     if Desk_Apps[i][6] == Desk_Page then
  395.         if Desk_Apps[i][3] == "n" then
  396.             paintutils.drawFilledBox(Desk_Apps[i][4],Desk_Apps[i][5],Desk_Apps[i][4] + 5, Desk_Apps[i][5] + 4, colors.gray)
  397.         else
  398.             local Image = paintutils.loadImage(Desk_Apps[i][3])
  399.             paintutils.drawImage(Image,Desk_Apps[i][4],Desk_Apps[i][5])
  400.         end
  401.         local Text = string.sub(Desk_Apps[i][2],1,8)
  402.         term.setCursorPos((Desk_Apps[i][4]+ 3) - string.len(Text)/2 , Desk_Apps[i][5] + 4)
  403.         local Ba = fs.open(Desktop_Back,"r")
  404.         local LineColor
  405.         for I = 1, Desk_Apps[i][5] + 4 do
  406.             LineColor = Ba.readLine()
  407.         end
  408.         Ba.close()
  409.         local TextBack = "0"
  410.         for T = 2, string.len(Text) do
  411.             TextBack = TextBack.."0"
  412.         end
  413.         local F = (Desk_Apps[i][4]+ 3) - string.len(Text)/2
  414.         term.blit(Text,TextBack,string.sub(LineColor,F, F + string.len(TextBack) - 1))
  415.     end
  416. end
  417. local AX = w/2 - Desk_EndPage/2
  418. paintutils.drawFilledBox(AX - 1, h - 2, w/2 + Desk_EndPage/2 + 2, h, colors.gray)
  419. for i = 1, Desk_EndPage do
  420.     if i == Desk_Page then
  421.         paintutils.drawPixel(AX, h - 1, colors.white)
  422.     else
  423.         paintutils.drawPixel(AX, h - 1, colors.lightGray)
  424.     end
  425.     AX = AX + 2
  426. end
  427. end
  428.  
  429. function TopBarDraw()
  430. paintutils.drawBox(1,1,9,1,colors.lightGray)
  431. paintutils.drawBox(w - 9,1,w,1,colors.lightGray)
  432. paintutils.drawBox(10,1, w - 10, 1, colors.gray)
  433. end
  434.  
  435. function DrawDropdown()
  436.  
  437. Clear()
  438. local Image = paintutils.loadImage(Dropdown_Back)
  439. paintutils.drawImage(Image,1,1)
  440. local Time = textutils.formatTime(os.time())
  441. term.setCursorPos(w/2 - string.len(Time)/2, 3)
  442. term.write(Time)
  443. term.setCursorPos(w/8,6)
  444. for I = 1, #Notifications do
  445.     paintutils.drawFilledBox(w/8, (I * 3) + 4, w - w/8, (I * 3) + 7, colors.lightGray)
  446.     term.setCursorPos(w/8, (I * 3) + 4)
  447.     term.write(Notifications[I][1])
  448.     term.setCursorPos(w - w/8 - 9, (I * 3) + 4)
  449.     term.write(textutils.formatTime(Notifications[I][3]))
  450.     local lines = require "cc.strings".wrap(Notifications[I][2], w - ((w/8) * 2))
  451.     for i = 1, #lines do
  452.       term.setCursorPos(w/8, (i + 7) * I)
  453.       term.write(lines[i])
  454.     end
  455. end
  456. paintutils.drawBox(w/10,h,w - w/10, h, colors.gray)
  457. end
  458.  
  459. function DrawScreen()
  460.  
  461. term.setBackgroundColor(Screen_Background)
  462. Clear()
  463. TopBarDraw()
  464.  
  465. for i = 1, #Entities do
  466.     if Entities[i][2] == "text" then
  467.         term.setBackgroundColor(Screen_Background)
  468.         term.setTextColor(Screen_TextColor)
  469.         term.setCursorPos(Entities[i][4],Entities[i][5])
  470.         term.write(Entities[i][3])
  471.     elseif Entities[i][2] == "button" then
  472.         term.setBackgroundColor(colors.yellow)
  473.         term.setTextColor(colors.blue)
  474.         term.setCursorPos(Entities[i][5],Entities[i][6])
  475.         term.write(Entities[i][3])
  476.     elseif Entities[i][2] == "image" then
  477.         local Image = paintutils.loadImage(Entities[i][3])
  478.         paintutils.drawImage(Image,Entities[i][4],Entities[i][5])
  479.     elseif Entities[i][2] == "input" then
  480.         paintutils.drawBox(Entities[i][5],Entities[i][6],Entities[i][4],Entities[i][6],colors.gray)
  481.         term.setBackgroundColor(colors.gray)
  482.         term.setTextColor(colors.white)
  483.         term.setCursorPos(Entities[i][5],Entities[i][6])
  484.         term.write(Entities[i][3])
  485.     end
  486. end
  487. local BarColor
  488. if Screen_Background == colors.white then
  489.     BarColor = colors.gray
  490. else
  491.     BarColor = colors.white
  492. end
  493. if AllowScroll == true then paintutils.drawBox(w/6, h - 1, w - (w/6), h - 1, colors.combine(Screen_Background,colors.blue)) end
  494. paintutils.drawBox(w/6, h, w - (w/6), h, BarColor)
  495. term.setBackgroundColor(Screen_Background)
  496. term.setTextColor(colors.white)
  497. if NotTimer ~= -1 then
  498.     Popup()
  499. end
  500. end
  501.  
  502. function Popup()
  503. local a = Notifications[Popup_Data][1]
  504. local b = Notifications[Popup_Data][2]
  505. local TI = Notifications[Popup_Data][3]
  506.  
  507. term.setBackgroundColor(colors.white)
  508. term.setTextColor(colors.black)
  509. paintutils.drawFilledBox(w/8,3,w - w/8, 5,colors.white)
  510. paintutils.drawBox(w/8,3,w/8,3,colors.red)
  511. term.setBackgroundColor(colors.white)
  512. term.setCursorPos((w/8) + 1, 3)
  513. term.write(a)
  514. term.setCursorPos(w - w/8 - 7, 3)
  515. term.write(textutils.formatTime(TI))
  516. term.setCursorPos(w/8,4)
  517.  
  518. local lines = require "cc.strings".wrap(b, w - ((w/8) * 2))
  519. for i = 1, #lines do
  520.   term.setCursorPos(w/8, i + 3)
  521.   term.write(lines[i])
  522. end
  523. term.setTextColor(colors.black)
  524. end
  525.  
  526. function UI()
  527.  
  528. if Application == nil then
  529.    
  530. elseif Dropdown == false then
  531. end
  532.  
  533. local Time = textutils.formatTime(os.time())
  534. if Dropdown == false then
  535.     term.setCursorPos(w - 8, 1)
  536.     term.setBackgroundColor(colors.lightGray)
  537.     term.setTextColor(colors.white)
  538.     term.write(Time)
  539.     term.setCursorPos(1,1)
  540.     term.write(#Notifications)
  541. else
  542.     term.setCursorPos(w/2 - string.len(Time)/2, 3)
  543.     term.write(Time)
  544. end
  545.  
  546. end
  547.  
  548. function ManageDropdown(event,a,b,c,d)
  549. if event == "mouse_click" then
  550.     if c == h then
  551.         DropToggle()
  552.     else
  553.         for i = 1, #Notifications do
  554.             if c >= (i*3) + 4 and c <= (i*3) + 7 and b > w/8 and b < w - w/8 then DropToggle() OpenNotification(i) end
  555.         end
  556.     end
  557. elseif event == "key" then
  558.     if a == keys.m then
  559.         DropToggle()
  560.     end
  561. end
  562. end
  563.    
  564. Startup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement