Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutoriallevel3/data/gui/scripts/NotificationLayer.lua @ 8636

Last change on this file since 8636 was 8636, checked in by dafrick, 13 years ago

Making NotificationQueue XML-loadable. Adding notifications to all levels.

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