FrameworkZ 10.8.3
Provides a framework for Project Zomboid with various systems.
Loading...
Searching...
No Matches
Notifications.lua
Go to the documentation of this file.
1if not isClient() then return end
2
4
5--! \brief Notifications module for FrameworkZ. Queues and sends notifications.
6--! \class FrameworkZ.Notifications
7FrameworkZ.Notifications = {}
8FrameworkZ.Notifications.__index = FrameworkZ.Notifications
9FrameworkZ.Notifications.Queue = {}
10FrameworkZ.Notifications.List = {}
11FrameworkZ.Notifications.Types = {
12 Default = "Default",
13 Info = "Info",
14 Success = "Success",
15 Warning = "Warning",
16 Danger = "Danger"
18FrameworkZ.Notifications.Colors = {
19 Default = {r = 1, g = 1, b = 1, a = 1},
20 Info = {r = 0.051, g = 0.792, b = 0.941, a = 1},
21 Success = {r = 0.098, g = 0.529, b = 0.329, a = 1},
22 Warning = {r = 1, g = 0.757, b = 0.027, a = 1},
23 Danger = {r = 0.863, g = 0.208, b = 0.271, a = 1}
24}
25FrameworkZ.Notifications = FrameworkZ.Foundation:NewModule(FrameworkZ.Notifications, "Notifications")
26
27function FrameworkZ.Notifications:ProcessQueue(isProcessingContinued)
28 if not (isProcessingContinued or not self.isProcessing) and not (#self.Queue > 0 or #self.List > 0) then return false end
29
30 if isProcessingContinued or not self.isProcessing then
31 if #self.Queue > 0 then
32 self.isProcessing = true
33 local queuedNotification = self.Queue[1]
34
35 queuedNotification:initialise()
36
37 if queuedNotification.parentUI then
38 queuedNotification.parentUI:addChild(queuedNotification)
39 else
40 queuedNotification:addToUIManager()
41 end
42
43 local player = FrameworkZ.Players:GetPlayerByID(getPlayer():getUsername())
44 if player then player:PlayLocalSound("pfw_lightswitch2") end
45
46 table.remove(self.Queue, 1)
47 table.insert(self.List, 1, queuedNotification)
48
49 if #self.List > 1 then
50 for i = 2, #self.List, 1 do
51 local position = i - 1
52 local topNotification = self.List[1]
53 local notification = self.List[i]
54 notification:setY(topNotification:getY() + notification:getHeight() * position + 10 * position)
55 end
56 end
57
58 FrameworkZ.Timers:Simple(1, function()
59 self.isProcessing = self:ProcessQueue(true)
60 end)
61
62 return true
63 elseif #self.List > 0 then
64 self.isProcessing = true
65 local processingID = nil
67 for i = 1, #self.List, 1 do
68 local notification = self.List[i]
69 local expired = notification.hasExpired
71 if expired then
72 notification.isExpiring = true
73 processingID = i
74 break
75 end
76 end
77
78 if not processingID then return false end
79
80 local processingNotification = self.List[processingID]
81
82 FrameworkZ.Timers:Create("NotificationFadeOut", 0, 0, function()
83 if processingNotification.isExpiring and processingNotification.textLabel.a > 0 then
84 processingNotification.backgroundColor.a = processingNotification.backgroundColor.a - processingNotification.originalAlpha * 0.01
85 processingNotification.borderColor.a = processingNotification.borderColor.a - processingNotification.originalAlpha * 0.01
86 processingNotification.textLabel.a = processingNotification.textLabel.a - 1 * 0.01
87 elseif processingNotification.isExpiring and FrameworkZ.Timers:Exists("NotificationFadeOut") then
88 FrameworkZ.Timers:Remove("NotificationFadeOut")
89 processingNotification.hasFullyExpired = true
90 processingNotification.backgroundColor.a = 0
91 processingNotification.borderColor.a = 0
92 processingNotification.textLabel.a = 0
93
94 FrameworkZ.Timers:Simple(0.5, function()
95 for i = processingID, #self.List, 1 do
96 local notification = self.List[i]
97
98 if not notification.isExpiring then
99 notification:setY(notification:getY() - (processingNotification:getHeight() + 10))
100 end
101 end
102
103 processingNotification:removeFromUIManager()
104 table.remove(self.List, processingID)
105
106 FrameworkZ.Timers:Simple(1, function()
107 self.isProcessing = self:ProcessQueue(true)
108 end)
109 end)
110 elseif not processingNotification.hasFullyExpired and not processingNotification.isExpiring then
111 FrameworkZ.Timers:Remove("NotificationFadeOut")
112 processingNotification.backgroundColor.a = math.min(processingNotification.originalAlpha + 0.25, 1)
113 processingNotification.borderColor.a = processingNotification.originalAlpha
114 processingNotification.textLabel.a = 1
115
116 FrameworkZ.Timers:Simple(1, function()
117 self.isProcessing = self:ProcessQueue(true)
118 end)
119 end
120 end)
121
122 return true
123 end
124 end
125end
126
127--! \brief Adds a notification to the queue.
128--! \param message \string The message to display.
129--! \param notificationType \string The type of notification to display. See notification types.
130--! \param duration \integer The duration the notification should be displayed for.
131--! \param ui \object The UI object that would cover where notifications display. Useful if you want to display notifications over top of a specific UI object.
132--! \return \object The notification UI object.
133function FrameworkZ.Notifications:AddToQueue(message, notificationType, duration, ui)
134 if not message then return end
135
136 local notification = FrameworkZ.UI.Notification:new(notificationType or FrameworkZ.Notifications.Types.Default, message, duration or 10, getPlayer())
137
138 if ui then
139 notification.parentUI = ui
140 end
141
142 table.insert(self.Queue, notification)
143
144 if not self.isProcessing then
145 self.isProcessing = self:ProcessQueue(false)
146 end
147
148 return notification
149end
150
151--[[
152function FrameworkZ.Notifications:OnGameStart()
153 FrameworkZ.Timers:Create("NotificationTick", 0.5, 0, function()
154 if not self.isProcessing then
155 self.isProcessing = self:ProcessQueue(false)
156 end
157 end)
158end
159--]]
160
161function FrameworkZ.Notifications:PlayerTick(isoPlayer)
162 if not self.isProcessing then
163 self.isProcessing = self:ProcessQueue(false)
164 end
165end
166
167FrameworkZ.Foundation:RegisterModule(FrameworkZ.Notifications)
void local message
void self self
Definition MainMenu.lua:89
void local player()
void local topNotification()
void local processingNotification()
void local processingID()
void local expired()
void local notification()
void local position()
void for i()
void FrameworkZ()
void local isClient()
void isoPlayer()
Notifications module for FrameworkZ. Queues and sends notifications.
void PlayerTick(isoPlayer)
FrameworkZ Notifications Colors
object AddToQueue(message, notificationType, duration, ui)
Adds a notification to the queue.
FrameworkZ Notifications __index
void ProcessQueue(isProcessingContinued)
FrameworkZ Notifications List
FrameworkZ Notifications Types
FrameworkZ Notifications Queue