mast3rillusion

notifications.lua

Jul 27th, 2021 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.41 KB | None | 0 0
  1. os.loadAPI("graphics.lua")
  2.  
  3. notifications = {}
  4. drawingNotifications = false
  5.  
  6. function createNotification(text, bkgColor, txtColor)
  7.     notification = graphics.label:new()
  8.     notification.text = text
  9.     length = string.len(text)
  10.     local w, h = term.getSize()
  11.     notification.x = (w / 2) - (length / 2) + 1
  12.     notification.y = 1
  13.     notification.bkgColor = bkgColor
  14.     notification.tColor = txtColor
  15.     return notification
  16. end
  17.  
  18. function addNotification(name, timer, text, bkgColor, txtColor)
  19.     found = false
  20.     for k,v in pairs(notifications) do
  21.         if v["name"] == name then
  22.             found = true
  23.             notifications[k]["notification"].text = text
  24.             notifications[k]["notification"].bkgColor = bkgColor
  25.             notifications[k]["notification"].tColor = txtColor
  26.             notifications[k]["time"] = timer
  27.             notifiTable["text"] = text
  28.         end
  29.     end
  30.     if found == false then
  31.         notification = createNotification(text, bkgColor, txtColor)
  32.         notifiTable = {}
  33.         notifiTable["name"] = name
  34.         notifiTable["notification"] = notification
  35.         notifiTable["time"] = timer
  36.         notifiTable["text"] = text
  37.         table.insert(notifications, notifiTable)
  38.     end
  39. end
  40.  
  41. function drawNotifications()
  42.     for k,v in pairs(notifications) do
  43.         if v["time"] >= 0 then
  44.             drawingNotifications = true
  45.             v["notification"].draw(v["notification"])
  46.         end
  47.     end
  48. end
  49.  
  50. function timeNotifications()
  51.     for k,v in pairs(notifications) do
  52.         if notifications[k]["time"] >= 0 then
  53.             t = v["time"] - 1
  54.             notifications[k]["time"] = t
  55.             term.setCursorPos(notifications[k]["notification"].x,notifications[k]["notification"].y)
  56.             term.setBackgroundColor(v["notification"].bkgColor)
  57.             term.clearLine()
  58.             drawingNotifications = true
  59.             v["notification"].draw(v["notification"])
  60.         else
  61.             term.setCursorPos(notifications[k]["notification"].x,notifications[k]["notification"].y)
  62.             term.setBackgroundColor(settings.headerColor)
  63.             term.clearLine()
  64.             notifications[k] = nil
  65.             table.remove(notifications, k)
  66.             drawingNotifications = false
  67.         end
  68.     end
  69. end
  70.  
  71. function update()
  72.     timeNotifications()
  73.     drawNotifications()
  74.     if drawingNotifications == false then
  75.         notification = graphics.label:new()
  76.         notification.text = settings.headerText
  77.         length = string.len(notification.text)
  78.         local w, h = term.getSize()
  79.         notification.x = (w / 2) - (length / 2) + 1
  80.         notification.y = 1
  81.         notification.bkgColor = colors.blue
  82.         notification.tColor = colors.white
  83.         notification.draw(notification)
  84.     end
  85. end
Add Comment
Please, Sign In to add comment