This repository has been archived by the owner on Aug 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnotif.lua
138 lines (117 loc) · 4.25 KB
/
notif.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
-- --------------------------------------------
-- Settings
-- --------------------------------------------
local body = {
-- Text
scale = 0.3,
offsetLine = 0.02,
-- Warp
offsetX = 0.005,
offsetY = 0.004,
-- Sprite
dict = 'commonmenu',
sprite = 'gradient_bgd',
width = 0.14,
height = 0.012,
heading = -90.0,
-- Betwenn != notifications
gap = 0.002,
}
local defaultText = '~r~~h~ERROR : ~h~~s~The text of the notification is nil.'
local defaultType = 'topRight'
local defaultTimeout = 6000
RequestStreamedTextureDict(body.dict) -- Load the sprite dict. properly
-- --------------------------------------------
-- Calculus functions
-- --------------------------------------------
local function goDown(v, id) -- Notifications will go under the previous notifications
for i = 1, #v do
if v[i].draw and i ~= id then
v[i].y = v[i].y + (body.height + (v[id].lines*2 + 1)*body.offsetLine)/2 + body.gap
end
end
end
local function goUp(v, id) -- Notifications will go above the previous notifications
for i = 1, #v do
if v[i].draw and i ~= id then
v[i].y = v[i].y - (body.height + (v[id].lines*2 + 1)*body.offsetLine)/2 - body.gap
end
end
end
local function centeredDown(v, id) -- Notification will stay centered from the default position and new notification will go at the bottom
for i = 1, #v do
if v[i].draw and i ~= id then
v[i].y = v[i].y - (body.height + (v[id].lines*2 + 1)*body.offsetLine)/4 - body.gap/2
v[id].y = v[i].y + (body.height + (v[id].lines*2 + 1)*body.offsetLine)/2 + body.gap
end
end
end
local function centeredUp(v, id) -- Notification will stay centered from the default position and new notification will go at the top
for i = 1, #v do
if v[i].draw and i ~= id then
v[i].y = v[i].y + (body.height + (v[id].lines*2 + 1)*body.offsetLine)/4 + body.gap/2
v[id].y = v[i].y - (body.height + (v[id].lines*2 + 1)*body.offsetLine)/2 - body.gap
end
end
end
local function CountLines(v, text)
BeginTextCommandLineCount("STRING")
SetTextScale(body.scale, body.scale)
SetTextWrap(v.x, v.x + body.width - body.offsetX)
AddTextComponentSubstringPlayerName(text)
local nbrLines = GetTextScreenLineCount(v.x + body.offsetX, v.y + body.offsetY)
return nbrLines
end
local function DrawText(v, text)
SetTextScale(body.scale, body.scale)
SetTextWrap(v.x, v.x + body.width - body.offsetX)
BeginTextCommandDisplayText("STRING")
AddTextComponentSubstringPlayerName(text)
EndTextCommandDisplayText(v.x + body.offsetX, v.y + body.offsetY)
end
local function DrawBackground(v)
DrawSprite(body.dict, body.sprite, v.x + body.width/2, v.y + (body.height + v.lines*body.offsetLine)/2, body.width, body.height + v.lines*body.offsetLine, body.heading, 255, 255, 255, 255)
end
-- --------------------------------------------
-- Different options
-- --------------------------------------------
local positions = {
['centerRight'] = { x = 0.85, y = 0.5, notif = {}, offset = centeredUp },
['centerLeft'] = { x = 0.01, y = 0.5, notif = {}, offset = centeredUp },
['topRight'] = { x = 0.85, y = 0.015, notif = {}, offset = goDown },
['topLeft'] = { x = 0.01, y = 0.015, notif = {}, offset = goDown },
['bottomRight'] = { x = 0.85, y = 0.955, notif = {}, offset = goUp },
['bottomLeft'] = { x = 0.015, y = 0.75, notif = {}, offset = goUp },
-- ['position name'] = { starting x, starting y, notif = {} (nothing toput here it's juste the handle), offset = the way multiple notifications will stack up}
}
-- --------------------------------------------
-- Main
-- --------------------------------------------
function SendNotification(options)
local text = options.text or defaultText
local type = options.type or defaultType
local timeout = options.timeout or defaultTimeout
local p = positions[type]
local id = #p.notif + 1
local nbrLines = CountLines(p, text)
p.notif[id] = {
x = p.x,
y = p.y,
lines = nbrLines,
draw = true,
}
if id > 1 then
p.offset(p.notif, id)
end
Citizen.CreateThread(function()
Wait(timeout)
p.notif[id].draw = false
end)
Citizen.CreateThread(function()
while p.notif[id].draw do
Wait(0)
DrawBackground(p.notif[id])
DrawText(p.notif[id], text)
end
end)
end