Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/mergeFS18/data/gui/scripts/NotificationLayer.lua @ 12384

Last change on this file since 12384 was 11802, checked in by landauf, 7 years ago

migrated NotificationLayer.lua to cegui 0.8

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