Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutoriallevel2/data/gui/scripts/NotificationLayer.lua @ 8526

Last change on this file since 8526 was 8448, checked in by dafrick, 14 years ago

Extending NotificationQueueCEGUI.

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