1 | -- NotificationLayer.lua |
---|
2 | |
---|
3 | local P = createMenuSheet("NotificationLayer", true, tribool(true), tribool(true)) |
---|
4 | |
---|
5 | P.queueList = {} |
---|
6 | |
---|
7 | P.sampleWindow = nil |
---|
8 | |
---|
9 | -- Loads the queues from the NotificationManager and creates the sample window, that is used to measure the width various strings need. |
---|
10 | function P.onLoad() |
---|
11 | orxonox.NotificationManager:getInstance():loadQueues() |
---|
12 | P.sampleWindow = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/NotificationLayer/Root/SampleWindow") |
---|
13 | end |
---|
14 | |
---|
15 | -- Creates a queue in the GUI. |
---|
16 | function P.createQueue(name, size) |
---|
17 | local root = winMgr:getWindow("orxonox/NotificationLayer/Root") |
---|
18 | --local queue = winMgr:createWindow("MenuWidgets/Listbox", "orxonox/NotificationLayer/Root/Queue/" .. name) |
---|
19 | --queue:setProperty("BackgroundColor", "00FFFFFF") -- Set background to be fully transparent. |
---|
20 | local queue = winMgr:createWindow("MenuWidgets/ScrollablePane", "orxonox/NotificationLayer/Root/Queue/" .. name) |
---|
21 | queue:setProperty("Alpha", 0.0) |
---|
22 | --queue:setProperty("FrameEnabled", "false") |
---|
23 | root:addChildWindow(queue) |
---|
24 | |
---|
25 | local queueTuple = |
---|
26 | { |
---|
27 | ["window"] = queue, |
---|
28 | ["name"] = name, |
---|
29 | ["maxSize"] = size, |
---|
30 | ["visible"] = false, |
---|
31 | ["fontSize"] = 12, |
---|
32 | ["fontColor"] = "FFFFFFFF", |
---|
33 | ["alignment"] = "LeftAligned", |
---|
34 | ["items"] = {}, |
---|
35 | ["first"] = 1, |
---|
36 | ["last"] = 1 |
---|
37 | } |
---|
38 | |
---|
39 | queue:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 0), CEGUI.UDim(0, 0))) |
---|
40 | queue:setSize(CEGUI.UVector2(CEGUI.UDim(1.0, 0), CEGUI.UDim(0, P.queueHeightHelper(queueTuple, size)))) |
---|
41 | |
---|
42 | P.queueList[name] = queueTuple -- name access |
---|
43 | P.setVisible(queueTuple, false) -- Set the queue to invisible as long as there are no notifications in it. |
---|
44 | end |
---|
45 | |
---|
46 | -- Removes a queue from the GUI. |
---|
47 | function P.removeQueue(queueName) |
---|
48 | local queue = P.queueList[queueName] |
---|
49 | |
---|
50 | if queue ~= nil then |
---|
51 | queue.window:getParent():removeChildWindow(queue.window) |
---|
52 | winMgr:destroyWindow(queue.window) |
---|
53 | end |
---|
54 | P.queueList[queueName] = nil |
---|
55 | end |
---|
56 | |
---|
57 | -- Pushes an input notification to the input queue. |
---|
58 | function P.pushNotification(queueName, notification) |
---|
59 | local queue = P.queueList[queueName] |
---|
60 | if queue == nil then |
---|
61 | return |
---|
62 | end |
---|
63 | |
---|
64 | if not guiMgr:usingOldCEGUI() then |
---|
65 | notification = string.gsub(notification, "%[", "\\%[") -- escape '[' which is used to format text since cegui 0.7 |
---|
66 | end |
---|
67 | |
---|
68 | local item = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/NotificationLayer/Root/Queue/" .. queueName .. "/" .. queue.last) |
---|
69 | item:setText(notification) |
---|
70 | P.setItemFontHelper(item, queue, true) |
---|
71 | -- Add the item to the top of the queue. |
---|
72 | local itemHeight = P.itemHeightHelper(queue) |
---|
73 | if queue.last-queue.first > 0 then -- If the queue is not empty move all items down |
---|
74 | for i=queue.first,queue.last-1 do |
---|
75 | local item = queue.items[i] |
---|
76 | item:setYPosition(CEGUI.UDim(0, itemHeight*(queue.last-i))) |
---|
77 | end |
---|
78 | end |
---|
79 | queue.window:addChildWindow(item) |
---|
80 | item:setSize(CEGUI.UVector2(CEGUI.UDim(1, 0), CEGUI.UDim(0, itemHeight))) |
---|
81 | item:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 0), CEGUI.UDim(0, 0))) |
---|
82 | item:setProperty("Alpha", 1.0) |
---|
83 | item:setProperty("InheritsAlpha", "false") |
---|
84 | item:setProperty("BackgroundEnabled", "false") |
---|
85 | item:setProperty("FrameEnabled", "false") |
---|
86 | item:setProperty("HorzFormatting", queue.alignment) |
---|
87 | queue.items[queue.last] = item |
---|
88 | queue.last = queue.last+1 |
---|
89 | |
---|
90 | -- If the queue has been invisible, set it to visible. |
---|
91 | if queue.visible == false then |
---|
92 | P.setVisible(queue, true) |
---|
93 | end |
---|
94 | end |
---|
95 | |
---|
96 | -- Pops the least recently added notification from the queue. |
---|
97 | function P.popNotification(queueName) |
---|
98 | local queue = P.queueList[queueName] |
---|
99 | if queue == nil then |
---|
100 | return |
---|
101 | end |
---|
102 | local item = queue.items[queue.first] |
---|
103 | -- Removes the item from the bottom of the queue. |
---|
104 | queue.window:removeChildWindow(item) |
---|
105 | winMgr:destroyWindow(item) |
---|
106 | queue.first = queue.first+1 |
---|
107 | |
---|
108 | -- Sets the queue to invisible if there are no more notifications in it. |
---|
109 | if queue.last-queue.first == 0 then |
---|
110 | P.setVisible(queue, false) |
---|
111 | end |
---|
112 | end |
---|
113 | |
---|
114 | -- Removes a notification at a given index from the queue. Where the 0th item is the newest and the nth the (n+1)th newest. |
---|
115 | function P.removeNotification(queueName, index) |
---|
116 | local queue = P.queueList[queueName] |
---|
117 | if queue == nil then |
---|
118 | return |
---|
119 | end |
---|
120 | |
---|
121 | index = queue.last-tonumber(index)-1 |
---|
122 | --if index == queue.first then -- If we want to remove the oldest notification, we can just use pop. |
---|
123 | -- P.popNotification(queueName) |
---|
124 | -- return |
---|
125 | --end |
---|
126 | |
---|
127 | -- Removes the item. |
---|
128 | local item = queue.items[index] |
---|
129 | queue.window:removeChildWindow(item) |
---|
130 | winMgr:destroyWindow(item) |
---|
131 | queue.items[index] = nil |
---|
132 | |
---|
133 | -- Move the items below, up. |
---|
134 | local itemHeight = P.itemHeightHelper(queue) |
---|
135 | local moved = false |
---|
136 | if index > queue.first then -- Move all older notifications up in the list. |
---|
137 | for i=index-1,-1,queue.first do |
---|
138 | cout(0, i) |
---|
139 | item = queue.items[i] |
---|
140 | item:setYposition(CEGUI.UDim(0, itemHeight*(queue.last-i-1))) |
---|
141 | queue.items[i+1] = item |
---|
142 | end |
---|
143 | end |
---|
144 | queue.items[queue.first] = nil |
---|
145 | queue.first = queue.first+1 |
---|
146 | |
---|
147 | -- Sets the queue to invisible if there are no more notifications in it. |
---|
148 | if queue.last-queue.first == 0 then |
---|
149 | P.setVisible(queue, false) |
---|
150 | end |
---|
151 | end |
---|
152 | |
---|
153 | -- Clears the queue. Removes all notifications from it. |
---|
154 | function P.clearQueue(queueName) |
---|
155 | local queue = P.queueList[queueName] |
---|
156 | if queue == nil then |
---|
157 | return |
---|
158 | end |
---|
159 | for i=queue.first,queue.last-1 do |
---|
160 | local item = queue.items[i] |
---|
161 | queue.window:removeChildWindow(item) |
---|
162 | winMgr:destroyWindow(item) |
---|
163 | end |
---|
164 | queue.items = {} |
---|
165 | queue.first = 1 |
---|
166 | queue.last = 1 |
---|
167 | |
---|
168 | -- Sets the queue to invisible. |
---|
169 | P.setVisible(queue, false) |
---|
170 | end |
---|
171 | |
---|
172 | -- Sets the visibility of the queue. |
---|
173 | function P.setVisible(queue, visible) |
---|
174 | if queue == nil then |
---|
175 | return |
---|
176 | end |
---|
177 | queue.window:setVisible(visible) |
---|
178 | queue.visible = visible |
---|
179 | end |
---|
180 | |
---|
181 | -- Change the position of the queue. |
---|
182 | -- The parameters are (in order) 'name of the queue', 'relative x-position', 'absolute x-position in pixel', 'relative y-position', 'absolute y-position in pixel'. |
---|
183 | function P.moveQueue(queueName, relativeXPos, absoluteXPos, relativeYpos, absoluteYPos) |
---|
184 | local queueWindow = P.queueList[queueName].window |
---|
185 | queueWindow:setPosition(CEGUI.UVector2(CEGUI.UDim(relativeXPos, absoluteXPos), CEGUI.UDim(relativeYpos, absoluteYPos))) |
---|
186 | end |
---|
187 | |
---|
188 | -- Change the size of the queue. |
---|
189 | -- The parameters are (in order) 'name of the queue', 'relative width', 'absolute width in pixel', 'relative height', 'absolute heigth in pixel'. |
---|
190 | -- Additionally the last two parameters can be ommitted, which leads to the height being set such that all notifications can be displayed. using the size of the queue. |
---|
191 | function P.resizeQueue(queueName, relativeWidth, absoluteWidth, relativeHeight, absoluteHeigth) |
---|
192 | local queue = P.queueList[queueName] |
---|
193 | local queueWindow = queue.window |
---|
194 | if queueWindow == nil then |
---|
195 | return |
---|
196 | end |
---|
197 | if absoluteHeigth == nil then |
---|
198 | absoluteHeigth = P.queueHeightHelper(queue, queue.maxSize) |
---|
199 | relativeHeight = 0 |
---|
200 | end |
---|
201 | queueWindow:setSize(CEGUI.UVector2(CEGUI.UDim(relativeWidth, absoluteWidth), CEGUI.UDim(relativeHeight, absoluteHeigth))) |
---|
202 | end |
---|
203 | |
---|
204 | -- Change the horizontal alignment of the displayed notifications. |
---|
205 | -- The parameters are the name of the queue and the alignment parameter, |
---|
206 | function P.changeQueueAlignment(queueName, alignment) |
---|
207 | local queue = P.queueList[queueName] |
---|
208 | local queueWindow = queue.window |
---|
209 | if queueWindow == nil then |
---|
210 | return |
---|
211 | end |
---|
212 | |
---|
213 | queue.alignment = alignment |
---|
214 | local item = nil |
---|
215 | for i=queue.first,queue.last-1 do |
---|
216 | item = queue.items[i] |
---|
217 | item:setProperty("HorzFormatting", queue.alignment) |
---|
218 | end |
---|
219 | end |
---|
220 | |
---|
221 | -- Change the font size of all notifications in a queue. |
---|
222 | -- The parameters are (in order) 'name of the queue', 'font size'. |
---|
223 | function P.changeQueueFontSize(queueName, size) |
---|
224 | local queue = P.queueList[queueName] |
---|
225 | local queueWindow = queue.window |
---|
226 | if queueWindow == nil then |
---|
227 | return |
---|
228 | end |
---|
229 | |
---|
230 | queue.fontSize = size |
---|
231 | for i=queue.first,queue.last-1 do |
---|
232 | P.setItemFontHelper(queue.items[i], queue, false) |
---|
233 | end |
---|
234 | end |
---|
235 | |
---|
236 | -- Change the font color of all notifications in a queue. |
---|
237 | -- The parameters are (in order) 'name of the queue', 'ARGB of the font color in hex notation'. |
---|
238 | function P.changeQueueFontColor(queueName, color) |
---|
239 | local queue = P.queueList[queueName] |
---|
240 | local queueWindow = queue.window |
---|
241 | if queueWindow == nil then |
---|
242 | return |
---|
243 | end |
---|
244 | |
---|
245 | queue.fontColor = color |
---|
246 | for i=queue.first,queue.last-1 do |
---|
247 | P.setItemFontHelper(queue.items[i], queue, true) |
---|
248 | end |
---|
249 | end |
---|
250 | |
---|
251 | -- Helper function to set the font size and color of a item of a queue. |
---|
252 | -- The parameters are (in order) 'the ListboxItem', 'the queue table', 'whether color should be changed as well' |
---|
253 | function P.setItemFontHelper(item, queue, changeColor) |
---|
254 | --local item = tolua.cast(item, "CEGUI::ListboxTextItem") |
---|
255 | local fontMgr = CEGUI.FontManager:getSingleton() |
---|
256 | if (fontMgr["isFontPresent"] and fontMgr:isFontPresent("BlueHighway-" .. queue.fontSize)) or -- cegui 0.6 |
---|
257 | (fontMgr["isDefined"] and fontMgr:isDefined("BlueHighway-" .. queue.fontSize)) then -- cegui 0.7 |
---|
258 | item:setFont("BlueHighway-" .. queue.fontSize) |
---|
259 | else |
---|
260 | orxonox.GUIManager:addFontHelper("BlueHighway-" .. queue.fontSize, queue.fontSize, "bluehigh.ttf") |
---|
261 | item:setFont("BlueHighway-" .. queue.fontSize) |
---|
262 | end |
---|
263 | if changeColor then |
---|
264 | item:setProperty("TextColours", "tl:" .. queue.fontColor .. " tr:" .. queue.fontColor .. " bl:" .. queue.fontColor .. " br:" .. queue.fontColor .. "") |
---|
265 | end |
---|
266 | end |
---|
267 | |
---|
268 | -- Helper function. Returns height a queue needs to have to display 'size' items. |
---|
269 | function P.queueHeightHelper(queue, size) |
---|
270 | --local listbox = CEGUI.toListbox(queue.window) |
---|
271 | --local item = CEGUI.createListboxTextItem("Text") |
---|
272 | --P.setItemFontHelper(item, queue, false) |
---|
273 | --listbox:addItem(item) |
---|
274 | --local singleItemHeight = listbox:getTotalItemsHeight() |
---|
275 | local singleItemHeight = P.itemHeightHelper(queue) |
---|
276 | --local lookAndFeel = CEGUI.WidgetLookManager:getSingleton():getWidgetLook(queue.window:getLookNFeel()) |
---|
277 | --local formattedArea = lookAndFeel:getNamedArea("ItemRenderingArea"):getArea():getPixelRect(queue.window) |
---|
278 | --local frameHeight = queue.window:getUnclippedOuterRect():getHeight() - formattedArea:getHeight() |
---|
279 | --listbox:removeItem(item) |
---|
280 | --return frameHeight + singleItemHeight*size |
---|
281 | return singleItemHeight*size + 1 |
---|
282 | end |
---|
283 | |
---|
284 | function P.itemHeightHelper(queue) |
---|
285 | local item = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/NotificationLayer/Root/Test/") |
---|
286 | item:setText("text") |
---|
287 | P.setItemFontHelper(item, queue, true) |
---|
288 | queue.window:addChildWindow(item) |
---|
289 | item:setSize(CEGUI.UVector2(CEGUI.UDim(1, 0), CEGUI.UDim(1, 0))) |
---|
290 | item:setProperty("FrameEnabled", "false") |
---|
291 | local height = getStaticTextWindowHeight(item) |
---|
292 | queue.window:removeChildWindow(item) |
---|
293 | winMgr:destroyWindow(item) |
---|
294 | return height |
---|
295 | end |
---|
296 | |
---|
297 | return P |
---|