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, 14 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
RevLine 
[7338]1-- NotificationLayer.lua
2
[7395]3local P = createMenuSheet("NotificationLayer", true, TriBool.True, TriBool.True)
[7338]4
5P.queueList = {}
6
[7395]7P.sampleWindow = nil
8
[7399]9-- Loads the queues from the NotificationManager and creates the sample window, that is used to measure the width various strings need.
[7395]10function P.onLoad()
11    orxonox.NotificationManager:getInstance():loadQueues()
12    P.sampleWindow = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/NotificationLayer/Root/SampleWindow")
13end
14
[7399]15-- Creates a queue in the GUI.
[7338]16function P.createQueue(name, size)
17    local root = winMgr:getWindow("orxonox/NotificationLayer/Root")
[8637]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")
[7342]23    root:addChildWindow(queue)
24
[7395]25    local queueTuple =
26    {
[8637]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
[7395]37    }
[8637]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))))
[7351]41
[7395]42    P.queueList[name] = queueTuple -- name access
[7399]43    P.setVisible(queueTuple, false) -- Set the queue to invisible as long as there are no notifications in it.
[7338]44end
45
[7399]46-- Removes a queue from the GUI.
47function P.removeQueue(queueName)
48    local queue = P.queueList[queueName]
[7338]49
50    if queue ~= nil then
[8637]51        queue.window:getParent():removeChildWindow(queue.window)
[7395]52        winMgr:destroyWindow(queue.window)
[7338]53    end
[7399]54    P.queueList[queueName] = nil
[7338]55end
56
[8079]57-- Pushes an input notification to the input queue.
[7338]58function P.pushNotification(queueName, notification)
[7395]59    local queue = P.queueList[queueName]
[7338]60    if queue == nil then
61        return
62    end
[8637]63
[8703]64    notification = string.gsub(notification, "%[", "\\%[") -- escape '[' which is used to format text since cegui 0.7
65
[8637]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
[7338]76    end
[8637]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
[7351]87
[7399]88    -- If the queue has been invisible, set it to visible.
[7395]89    if queue.visible == false then
[7351]90        P.setVisible(queue, true)
91    end
[7338]92end
93
[7399]94-- Pops the least recently added notification from the queue.
[7338]95function P.popNotification(queueName)
[7395]96    local queue = P.queueList[queueName]
[7338]97    if queue == nil then
98        return
99    end
[8637]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
[7351]105
[7399]106    -- Sets the queue to invisible if there are no more notifications in it.
[8637]107    if queue.last-queue.first == 0 then
[7351]108        P.setVisible(queue, false)
109    end
[7338]110end
111
[8637]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.
[7338]113function P.removeNotification(queueName, index)
[7395]114    local queue = P.queueList[queueName]
[7338]115    if queue == nil then
116        return
117    end
[8637]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
[7399]125    -- Removes the item.
[8637]126    local item = queue.items[index]
127    queue.window:removeChildWindow(item)
128    winMgr:destroyWindow(item)
129    queue.items[index] = nil
[7351]130
[8637]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
[7399]145    -- Sets the queue to invisible if there are no more notifications in it.
[8637]146    if queue.last-queue.first == 0 then
[7351]147        P.setVisible(queue, false)
148    end
[7338]149end
150
[7399]151-- Clears the queue. Removes all notifications from it.
152function P.clearQueue(queueName)
153    local queue = P.queueList[queueName]
[7338]154    if queue == nil then
155        return
156    end
[8637]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
[7351]165
[7399]166    -- Sets the queue to invisible.
[7351]167    P.setVisible(queue, false)
[7338]168end
169
[7399]170-- Sets the visibility of the queue.
[7395]171function P.setVisible(queue, visible)
[7343]172    if queue == nil then
173        return
174    end
[7395]175    queue.window:setVisible(visible)
176    queue.visible = visible
[7343]177end
178
[8637]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
[7354]185
[8637]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
[7354]194    end
[8637]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)))
[7354]200end
201
[8637]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
[7399]210
[8637]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
[7395]217end
218
[8637]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
[7354]226    end
[7362]227
[8637]228    queue.fontSize = size
229    for i=queue.first,queue.last-1 do
230        P.setItemFontHelper(queue.items[i], queue, false)
[7354]231    end
232end
233
[8637]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
[7413]240        return
241    end
242
[8637]243    queue.fontColor = color
244    for i=queue.first,queue.last-1 do
245        P.setItemFontHelper(queue.items[i], queue, true)
[7338]246    end
247end
248
[8637]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()
[8703]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
[8637]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
[7395]264end
265
[7399]266-- Helper function. Returns height a queue needs to have to display 'size' items.
[7342]267function P.queueHeightHelper(queue, size)
[8637]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
[7342]280end
281
[8637]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
[7338]295return P
Note: See TracBrowser for help on using the repository browser.