Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/data/gui/scripts/NotificationLayer.lua @ 8703

Last change on this file since 8703 was 8703, checked in by landauf, 13 years ago

two fixes for cegui 0.7

sometimes there's an exception while closing orxonox, but that's a bug in cegui: http://www.cegui.org.uk/mantis/view.php?id=423

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