PwnagePineapple

OpenGlasses Push Notification Library

Oct 13th, 2016
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.05 KB | None | 0 0
  1. local os = require("os")
  2. local component = require("component")
  3. local gc = require("ogcolor")
  4. local gls = component.glasses
  5.  
  6. local function wrap_string(str, wrapLength)
  7.   local lines = {}
  8.  
  9.   while #str > 0 do
  10.     local lastSpaceIndex
  11.  
  12.     -- Only check substrings longer than the wrap length
  13.     if #str > wrapLength then
  14.       -- Check for first space so words longer than `wrapLength` don't break.
  15.       lastSpaceIndex = str:sub(1, wrapLength):find'%s%S*$' or str:find'%s'
  16.     end
  17.  
  18.     table.insert(lines, str:sub(1, lastSpaceIndex))
  19.  
  20.     if not lastSpaceIndex then break end
  21.  
  22.     str = str:sub(lastSpaceIndex + 1)
  23.   end
  24.   return lines
  25. end
  26. local ognotify = {}
  27.  
  28. --Send Notifications
  29. function  ognotify.send(title,desc,timeout,bcolor,balpha,tcolor,talpha,dcolor,dalpha)
  30.    
  31.     local description = wrap_string(desc,35) -- 35 is approx. max num of characters before text protrudes from box
  32.     --Background Rectangle
  33.     local rect = gls.addRect()
  34.     rect.setColor(table.unpack(bcolor))
  35.     rect.setPosition(750,10)
  36.     rect.setSize(25+(10 * #description),200)
  37.     rect.setAlpha(balpha)
  38.    
  39.     --Title text
  40.     local titleWidget = gls.addTextLabel()
  41.     titleWidget.setText(title)
  42.     titleWidget.setColor(table.unpack(tcolor))
  43.     titleWidget.setPosition(690,15)
  44.     titleWidget.setScale(1.1)
  45.     titleWidget.setAlpha(talpha)
  46.    
  47.     --Description text
  48.     local descWidget = {}
  49.     local yPos = 30
  50.     for i in ipairs(description) do
  51.         --table.insert(descWidget,)
  52.         descWidget[i] = gls.addTextLabel()
  53.         descWidget[i].setScale(1.0)
  54.         descWidget[i].setColor(table.unpack(dcolor))
  55.         descWidget[i].setAlpha(dalpha)
  56.         descWidget[i].setPosition(760,yPos)
  57.         descWidget[i].setText(description[i])
  58.         yPos = yPos + 10
  59.     end
  60.    
  61.     os.sleep(timeout)
  62.    
  63.     --Clear notification
  64.     gls.removeObject(rect.getID())
  65.     gls.removeObject(titleWidget.getID())
  66.     for i in ipairs(descWidget) do
  67.         gls.removeObject(descWidget[i].getID())
  68.     end
  69.     return True
  70. end
  71.  
  72. return ognotify
Advertisement
Add Comment
Please, Sign In to add comment