Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

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

  • Property svn:eol-style set to native
File size: 10.7 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")
[8453]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    {
[8453]27        ["window"]    = queue,
28        ["name"]      = name,
[8636]29        ["maxSize"]   = size,
[8453]30        ["visible"]   = false,
31        ["fontSize"]  = 12,
32        ["fontColor"] = "FFFFFFFF",
33        ["alignment"] = "LeftAligned",
34        ["items"]     = {},
35        ["first"]     = 1,
36        ["last"]      = 1
[7395]37    }
[8453]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
[8636]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
[8453]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
[7338]74    end
[8453]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
[7351]85
[7399]86    -- If the queue has been invisible, set it to visible.
[7395]87    if queue.visible == false then
[7351]88        P.setVisible(queue, true)
89    end
[7338]90end
91
[7399]92-- Pops the least recently added notification from the queue.
[7338]93function P.popNotification(queueName)
[7395]94    local queue = P.queueList[queueName]
[7338]95    if queue == nil then
96        return
97    end
[8453]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
[7351]103
[7399]104    -- Sets the queue to invisible if there are no more notifications in it.
[8453]105    if queue.last-queue.first == 0 then
[7351]106        P.setVisible(queue, false)
107    end
[7338]108end
109
[8453]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.
[7338]111function P.removeNotification(queueName, index)
[7395]112    local queue = P.queueList[queueName]
[7338]113    if queue == nil then
114        return
115    end
[8453]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
[7399]123    -- Removes the item.
[8453]124    local item = queue.items[index]
125    queue.window:removeChildWindow(item)
126    winMgr:destroyWindow(item)
127    queue.items[index] = nil
[7351]128
[8453]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
[7399]143    -- Sets the queue to invisible if there are no more notifications in it.
[8453]144    if queue.last-queue.first == 0 then
[7351]145        P.setVisible(queue, false)
146    end
[7338]147end
148
[7399]149-- Clears the queue. Removes all notifications from it.
150function P.clearQueue(queueName)
151    local queue = P.queueList[queueName]
[7338]152    if queue == nil then
153        return
154    end
[8453]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
[7351]163
[7399]164    -- Sets the queue to invisible.
[7351]165    P.setVisible(queue, false)
[7338]166end
167
[7399]168-- Sets the visibility of the queue.
[7395]169function P.setVisible(queue, visible)
[7343]170    if queue == nil then
171        return
172    end
[7395]173    queue.window:setVisible(visible)
174    queue.visible = visible
[7343]175end
176
[8453]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
[7354]183
[8453]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
[7354]192    end
[8453]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)))
[7354]198end
199
[8453]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
[7399]208
[8453]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
[7395]215end
216
[8453]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
[7354]224    end
[7362]225
[8453]226    queue.fontSize = size
227    for i=queue.first,queue.last-1 do
228        P.setItemFontHelper(queue.items[i], queue, false)
[7354]229    end
230end
231
[8453]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
[7413]238        return
239    end
240
[8453]241    queue.fontColor = color
242    for i=queue.first,queue.last-1 do
243        P.setItemFontHelper(queue.items[i], queue, true)
[7338]244    end
245end
246
[8453]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
[7395]261end
262
[7399]263-- Helper function. Returns height a queue needs to have to display 'size' items.
[7342]264function P.queueHeightHelper(queue, size)
[8453]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
[7342]277end
278
[8453]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
[7338]292return P
Note: See TracBrowser for help on using the repository browser.