Skip to content

Instantly share code, notes, and snippets.

@LDDestroier
Last active July 15, 2018 17:05
Show Gist options
  • Save LDDestroier/82f80d4db19d6d51126781b3ff94d547 to your computer and use it in GitHub Desktop.
Save LDDestroier/82f80d4db19d6d51126781b3ff94d547 to your computer and use it in GitHub Desktop.
local explode = function(div,str,replstr,includeDiv)
if (div=='') then return false end
local pos,arr = 0,{}
for st,sp in function() return string.find(str,div,pos,false) end do
tableinsert(arr,string.sub(replstr or str,pos,st-1+(includeDiv and #div or 0)))
pos = sp + 1
end
tableinsert(arr,string.sub(replstr or str,pos))
return arr
end
local notif = {}
notif.alpha = 248
notif.height = 10
notif.width = 6
notif.time = 10
notif.wrapX = 300
local nList = {}
local colorTranslate = {
[" "] = {240, 240, 240},
["0"] = {240, 240, 240},
["1"] = {242, 178, 51 },
["2"] = {229, 127, 216},
["3"] = {153, 178, 242},
["4"] = {222, 222, 108},
["5"] = {127, 204, 25 },
["6"] = {242, 178, 204},
["7"] = {76, 76, 76 },
["8"] = {153, 153, 153},
["9"] = {76, 153, 178},
["a"] = {178, 102, 229},
["b"] = {51, 102, 204},
["c"] = {127, 102, 76 },
["d"] = {87, 166, 78 },
["e"] = {204, 76, 76 },
["f"] = {25, 25, 25 }
}
local interface, canvas = peripheral.find("neuralInterface")
if interface then
if interface.canvas then
canvas = interface.canvas()
notif.newNotification = function(char, text, back, time)
nList[#nList+1] = {char,text,back,time,1} --last one is alpha multiplier
end
notif.displayNotifications = function(doCountDown)
local adjList = {
["i"] = -4,
["l"] = -3,
["I"] = -1,
["t"] = -2,
["k"] = -1,
["!"] = -4,
["|"] = -4,
["."] = -4,
[","] = -4,
[":"] = -4,
[";"] = -4,
["f"] = -1,
["'"] = -3,
["\""] = -1,
["<"] = -1,
[">"] = -1,
}
local drawEdgeLine = function(y,alpha)
local l = canvas.addRectangle(notif.wrapX, 1+(y-1)*notif.height, 1, notif.height)
l.setColor(table.unpack(colorTranslate["0"]))
l.setAlpha(alpha)
end
local getWordWidth = function(str)
local output = 0
for a = 1, #str do
output = output + notif.width + (adjList[str:sub(a,a)] or 0)
end
return output
end
canvas.clear()
local xadj, charadj, wordadj, t, r
local x, y, words, txtwords, bgwords = 0, 0
for n = #nList, 1, -1 do
xadj, charadj = 0, 0
y = y + 1
x = 0
words = explode(" ",nList[n][1],_,true)
txtwords = explode(" ",nList[n][1],nList[n][2],true)
bgwords = explode(" ",nList[n][1],nList[n][3],true)
local char, text, back
local currentX = 0
for w = 1, #words do
char = words[w]
text = txtwords[w]
back = bgwords[w]
if currentX + getWordWidth(char) > notif.wrapX then
y = y + 1
x = 2
xadj = 0
currentX = x * notif.width
end
for cx = 1, #char do
x = x + 1
charadj = (adjList[char:sub(cx,cx)] or 0)
r = canvas.addRectangle(xadj+1+(x-1)*notif.width, 1+(y-1)*notif.height, charadj+notif.width, notif.height)
if back:sub(cx,cx) ~= " " then
r.setAlpha(notif.alpha * nList[n][5])
r.setColor(table.unpack(colorTranslate[back:sub(cx,cx)]))
else
r.setAlpha(100 * nList[n][5])
r.setColor(table.unpack(colorTranslate["7"]))
end
drawEdgeLine(y,notif.alpha * nList[n][5])
t = canvas.addText({xadj+1+(x-1)*notif.width,2+(y-1)*notif.height}, char:sub(cx,cx))
t.setAlpha(notif.alpha * nList[n][5])
t.setColor(table.unpack(colorTranslate[text:sub(cx,cx)]))
xadj = xadj + charadj
currentX = currentX + charadj+notif.width
end
end
if doCountDown then
if nList[n][4] > 1 then
nList[n][4] = nList[n][4] - 1
else
if nList[n][5] > 0 then
while true do
nList[n][5] = math.max(nList[n][5] - 0.2, 0)
notif.displayNotifications(false)
if nList[n][5] == 0 then break else sleep(0.05) end
end
end
table.remove(nList,n)
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment