Jump to content

[REL] DX-Notification Script


Recommended Posts

Hey all,

I made a simple DX notification script for my gamemode (JohnMode) and I figured I might as well release it since it took 15 minutes to write. Here it is:

  
-- 
--  DX NOTIFICATIONS 
-- 
local notificationQueue = {} 
local drawNotifications 
  
function createNotification(id, message, x, y, width, height, font, scale, textColor, bgColor, showTicks) 
    if notificationQueue[id] then 
        destroyNotification(id) 
    end 
     
    if type(message) ~= "string" or type(x) ~= "number" or type(y) ~= "number" or type(width) ~= "number" or type(height) ~= "number" then 
        return false 
    end 
     
    if type(font) ~= "string" then 
        font = --[[ TODO: Define the a default font! ]] 
    end 
    if type(scale) ~= "number" then 
        scale = --[[ TODO: Define a default scale! ]] 
    end 
    if type(textColor) ~= "table" then 
        textColor = --[[ TODO: Define a default color (in format {r, g, b}) ! ]] 
    end 
    if type(bgColor) ~= "table" then 
        bgColor = --[[ TODO: Define a default background color (in format {r, g, b})! ]] 
    end 
    if not showTicks then 
        showTicks = --[[ TODO: Define a default show time (in miliseconds)! ]] 
    end 
     
    local newNotification = {} 
    newNotification.message = message 
    newNotification.x = x 
    newNotification.y = y 
    newNotification.width = width 
    newNotification.height = height 
    newNotification.font = font 
    newNotification.scale = scale 
    newNotification.textColor = textColor 
    newNotification.bgColor = bgColor 
    newNotification.showTicks = showTicks 
    newNotification.shownTicks = 0 
    newNotification.lastAlpha = 0 
     
    if #notificationQueue == 0 then 
        addEventHandler("onClientRender", root, drawNotifications) 
    end 
    notificationQueue[id] = newNotification 
end 
  
function destroyNotification(id) 
    if notificationQueue[id] then 
        notificationQueue[id] = nil 
     
        if #notificationQueue == 0 then 
            removeEventHandler("onClientRender", root, drawNotifications) 
        end 
    end 
end 
  
--[[local]] function drawNotifications() 
    --[[if not isLocalPlayerLoggedIn(localPlayer) then 
        return 
    end]] 
     
    for id, notification in pairs(notificationQueue) do 
        local shownTicks, lastAlpha = notification.shownTicks, notification.lastAlpha 
        local bgColor, textColor = notification.bgColor, notification.textColor 
         
        -- If the nofication is still new or fading in 
        if shownTicks == 0 then 
            if lastAlpha < 200 then 
                lastAlpha = lastAlpha + 6 
                if lastAlpha > 200 then 
                    lastAlpha = 200 
                end 
                 
                bgColor[4] = lastAlpha 
                textColor[4] = lastAlpha 
                notification.lastAlpha = lastAlpha 
            else 
                notification.firstTick = getTickCount() 
                notification.shownTicks = 1 
            end 
        elseif shownTicks >= notification.showTicks then -- Fade out or delete old notifications 
            if lastAlpha > 0 then 
                lastAlpha = lastAlpha - 6 
                if lastAlpha < 0 then 
                    lastAlpha = 0 
                end 
                 
                notification.lastAlpha = lastAlpha 
                bgColor[4] = lastAlpha 
                textColor[4] = lastAlpha 
            else 
                destroyNotification(id) 
            end 
        else -- Buisness as usual 
            notification.shownTicks = getTickCount() - notification.firstTick 
        end 
         
        dxDrawRectangle(notification.x, notification.y, notification.width, notification.height, tocolor(unpack(bgColor))) 
        dxDrawText(notification.message, notification.x, notification.y, notification.x + notification.width, notification.y + notification.height, tocolor(unpack(textColor)), notification.scale, notification.font, "center", "center") 
    end 
end 
  

Caveats:

- THE SCRIPT WILL NOT WORK PROPERLY IN ITS CURRENT FORM! You need to define your own default variables in the script - this is done to prevent everyone from having the same style notifications.

- This script does not support dx-fonts. If you want that functionality, implement it yourself (it's not hard)!

- This script does not magically ensure that the width of the text is less than the width of the box - that is up to you. You could easily implement an auto-size feature, however.

- It's client-side only (I really shouldn't have to mention this, but you never know).

Cheers and titties.

Edited by Guest
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...