Jump to content

Help on Notification Bar


xXMADEXx

Recommended Posts

Hey guys, i got the notification bar from GUI-editor (thank you, Remp.) But, when i tried to use it, no luck. Any probs seen below, please comment.

gColours = { 
    primary = {255, 69, 59, 255}, 
    secondary = {255, 118, 46, 255}, 
    tertiary = {232, 42, 104, 255}, 
    defaultLabel = {255, 255, 255}, 
    grey = {120, 120, 120}, 
} 
ContextBar = { 
    height = 28, 
    speed = 20, 
    step = 2, 
    life = 8000, 
    update = 0, 
    textColour = {unpack(gColours.primary)}, 
    lineColour = {0, 0, 0}, 
    entries = {} 
} 
function ContextBar.add(text) 
    if not gEnabled then 
        return 
    end 
     
    local y = gScreen.y - ContextBar.height 
     
    if #ContextBar.entries > 0 then 
        if ContextBar.entries[#ContextBar.entries].text == text then 
            return 
        end 
         
        y = ContextBar.entries[#ContextBar.entries].y - ContextBar.height 
    end 
     
    local pixelsPerSecond = (1000 / ContextBar.speed) * ContextBar.step 
    local alphaStep = (gScreen.y - y - ContextBar.height) / pixelsPerSecond 
     
    ContextBar.entries[#ContextBar.entries + 1] = { 
        text = text,  
        creation = getTickCount(), 
        y = y,  
        landed = false,  
        alphaStep = alphaStep == 0 and 0.05 or (1 / ((alphaStep * 1000) / ContextBar.speed)), 
        alpha = alphaStep == 0 and 1 or 0, 
    } 
end 
addEventHandler("onClientRender", root, 
    function() 
        for _,bar in ipairs(ContextBar.entries) do 
            dxDrawRectangle(0, bar.y, gScreen.x, ContextBar.height, tocolor(0, 0, 0, math.lerp(0, 170, bar.alpha)), true) 
            dxDrawText(bar.text, 0, bar.y, gScreen.x, bar.y + ContextBar.height, tocolor(ContextBar.textColour[1], ContextBar.textColour[2], ContextBar.textColour[3], math.lerp(0, 255, bar.alpha)), 1, "default-bold", "center", "center", true, true, true) 
             
            dxDrawLine(0, bar.y, gScreen.x, bar.y, tocolor(ContextBar.lineColour[1], ContextBar.lineColour[2], ContextBar.lineColour[3], math.lerp(0, 255, bar.alpha)), 1, true) 
        end 
         
        local tick = getTickCount() 
         
        if tick > (ContextBar.update + ContextBar.speed) then 
            ContextBar.update = tick 
             
            if #ContextBar.entries > 1 then 
                for i = #ContextBar.entries, 1, -1 do 
                    if ContextBar.entries[i].y > gScreen.y then 
                        table.remove(ContextBar.entries, i) 
                    else 
                        ContextBar.entries[i].alpha = math.min(1, ContextBar.entries[i].alpha + ContextBar.entries[i].alphaStep) 
                        ContextBar.entries[i].y = ContextBar.entries[i].y + ContextBar.step 
                        if i ~= 1 and #ContextBar.entries == 2 and ContextBar.entries[i].y > (gScreen.y - ContextBar.height) then 
                            ContextBar.entries[i].y = gScreen.y - ContextBar.height 
                        end 
                    end 
                end 
            elseif #ContextBar.entries == 1 then 
                if not ContextBar.entries[1].landed then 
                    ContextBar.entries[1].landed = true 
                    ContextBar.entries[1].creation = tick 
                end 
  
                if tick > (ContextBar.entries[1].creation + ContextBar.life) then                    
                    ContextBar.entries[1].alpha = ContextBar.entries[1].alpha - ContextBar.entries[1].alphaStep 
                     
                    if ContextBar.entries[1].alpha <= 0 then 
                        ContextBar.entries[1] = nil 
                    end 
                end 
            end 
        end 
    end 
) 
  
function add() 
    ContextBar.add("Basic Testing.") 
end 
addCommandHandler("test",add) 

Link to comment

Ok, I asked to Remp and he says that he gave permission, so I've fixed the code ( you had missing stuff, plus "test" command is built-in MTA, so I renamed it to "test2" ).

gColours = { 
    primary = {255, 69, 59, 255}, 
    secondary = {255, 118, 46, 255}, 
    tertiary = {232, 42, 104, 255}, 
    defaultLabel = {255, 255, 255}, 
    grey = {120, 120, 120}, 
} 
ContextBar = { 
    height = 28, 
    speed = 20, 
    step = 2, 
    life = 8000, 
    update = 0, 
    textColour = {unpack(gColours.primary)}, 
    lineColour = {0, 0, 0}, 
    entries = {} 
} 
  
local sx, sy = guiGetScreenSize ( ) 
  
gEnabled = true 
  
function ContextBar.add(text) 
    if not gEnabled then 
        return 
    end 
    
    local y = sy - ContextBar.height 
    
    if #ContextBar.entries > 0 then 
        if ContextBar.entries[#ContextBar.entries].text == text then 
            return 
        end 
        
        y = ContextBar.entries[#ContextBar.entries].y - ContextBar.height 
    end 
    
    local pixelsPerSecond = (1000 / ContextBar.speed) * ContextBar.step 
    local alphaStep = (sy - y - ContextBar.height) / pixelsPerSecond 
    
    ContextBar.entries[#ContextBar.entries + 1] = { 
        text = text, 
        creation = getTickCount(), 
        y = y, 
        landed = false, 
        alphaStep = alphaStep == 0 and 0.05 or (1 / ((alphaStep * 1000) / ContextBar.speed)), 
        alpha = alphaStep == 0 and 1 or 0, 
    } 
end 
  
addEventHandler("onClientRender", root, 
    function() 
        for _,bar in ipairs(ContextBar.entries) do 
            dxDrawRectangle(0, bar.y, sx, ContextBar.height, tocolor(0, 0, 0, math.lerp(0, 170, bar.alpha)), true) 
            dxDrawText(bar.text, 0, bar.y, sx, bar.y + ContextBar.height, tocolor(ContextBar.textColour[1], ContextBar.textColour[2], ContextBar.textColour[3], math.lerp(0, 255, bar.alpha)), 1, "default-bold", "center", "center", true, true, true) 
            
            dxDrawLine(0, bar.y, sx, bar.y, tocolor(ContextBar.lineColour[1], ContextBar.lineColour[2], ContextBar.lineColour[3], math.lerp(0, 255, bar.alpha)), 1, true) 
        end 
        
        local tick = getTickCount() 
        
        if tick > (ContextBar.update + ContextBar.speed) then 
            ContextBar.update = tick 
            
            if #ContextBar.entries > 1 then 
                for i = #ContextBar.entries, 1, -1 do 
                    if ContextBar.entries[i].y > sy then 
                        table.remove(ContextBar.entries, i) 
                    else 
                        ContextBar.entries[i].alpha = math.min(1, ContextBar.entries[i].alpha + ContextBar.entries[i].alphaStep) 
                        ContextBar.entries[i].y = ContextBar.entries[i].y + ContextBar.step 
                        if i ~= 1 and #ContextBar.entries == 2 and ContextBar.entries[i].y > (sy - ContextBar.height) then 
                            ContextBar.entries[i].y = sy - ContextBar.height 
                        end 
                    end 
                end 
            elseif #ContextBar.entries == 1 then 
                if not ContextBar.entries[1].landed then 
                    ContextBar.entries[1].landed = true 
                    ContextBar.entries[1].creation = tick 
                end 
  
                if tick > (ContextBar.entries[1].creation + ContextBar.life) then                   
                    ContextBar.entries[1].alpha = ContextBar.entries[1].alpha - ContextBar.entries[1].alphaStep 
                    
                    if ContextBar.entries[1].alpha <= 0 then 
                        ContextBar.entries[1] = nil 
                    end 
                end 
            end 
        end 
    end 
) 
  
function math.lerp(from, to, t) 
    return from + (to - from) * t 
end 
  
function add() 
    ContextBar.add("Basic Testing.") 
end 
addCommandHandler("test2",add) 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...