Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Removing editMode stuff, since that doesn't work anymore and that won't change for quite some time.
Seperating CEGUI from NotificationQueue stuff by introducing a new NotificationQueue called the NotificationQueueCEGUI.

  • Property svn:eol-style set to native
File size: 10.3 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,
29        ["edit"]      = nil,
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'.
185-- Additionally the last parameter can be ommitted and relativeHeight can be set to the size (i.e. the maximal number of notifications displayed) of the queue, which leads to the height being set such that all notifications can be displayed.
186function P.resizeQueue(queueName, relativeWidth, absoluteWidth, relativeHeight, absoluteHeigth)
187    local queueWindow = P.queueList[queueName].window
188    if queueWindow == nil then
189        return
190    end
191    if absoluteHeigth == nil then
192        absoluteHeigth = P.queueHeightHelper(P.queueList[queueName], relativeHeight)
193        relativeHeight = 0
194    end
195    queueWindow:setSize(CEGUI.UVector2(CEGUI.UDim(relativeWidth, absoluteWidth), CEGUI.UDim(relativeHeight, absoluteHeigth)))
196end
197
198-- Change the font size and font color of all notifications in a queueHeightHelper
[8377]199-- The parameters are (in order) 'name of the queue', 'font size', 'ARGB of the font color in hex notation'.
[8371]200function P.changeQueueFont(queueName, size, color)
201    local queue = P.queueList[queueName]
202    local queueWindow = queue.window
203    if queueWindow == nil then
204        return
205    end
206
207    queue.fontSize = size
208    local changeColor = false
209    if color ~= nil then
210        queue.fontColor = color
211        changeColor = true
212    end
213    for i=queue.first,queue.last-1 do
214        P.setItemFontHelper(queue.items[i], queue, changeColor)
215    end
216end
217
[8378]218function P.changeQueueAlignment(queueName, alignment)
219    local queue = P.queueList[queueName]
220    local queueWindow = queue.window
221    if queueWindow == nil then
222        return
223    end
224   
225    queue.alignment = alignment
226    local item = nil
227    for i=queue.first,queue.last-1 do
228        item = queue.items[i]
229        item:setProperty("HorzFormatting", queue.alignment)
230    end
231end
232
[8371]233-- Helper function to set the font size and color of a item of a queue.
234-- The parameters are (in order) 'the ListboxItem', 'the queue table', 'whether color should be changed as well'
235function P.setItemFontHelper(item, queue, changeColor)
236    --local item = tolua.cast(item, "CEGUI::ListboxTextItem")
237    local fontMgr = CEGUI.FontManager:getSingleton()
238    if fontMgr:isFontPresent("BlueHighway-" .. queue.fontSize) then
239        item:setFont("BlueHighway-" .. queue.fontSize)
240    else
241        orxonox.GUIManager:addFontHelper("BlueHighway-" .. queue.fontSize, queue.fontSize, "bluehigh.ttf")
242        item:setFont("BlueHighway-" .. queue.fontSize)
243    end
244    if changeColor then
[8377]245        item:setProperty("TextColours", "tl:" .. queue.fontColor .. " tr:" .. queue.fontColor .. " bl:" .. queue.fontColor .. " br:" .. queue.fontColor .. "")
[8371]246    end
247end
248
[7399]249-- Helper function. Returns height a queue needs to have to display 'size' items.
[7342]250function P.queueHeightHelper(queue, size)
[8371]251    --local listbox = CEGUI.toListbox(queue.window)
252    --local item = CEGUI.createListboxTextItem("Text")
253    --P.setItemFontHelper(item, queue, false)
254    --listbox:addItem(item)
255    --local singleItemHeight = listbox:getTotalItemsHeight()
256    local singleItemHeight = P.itemHeightHelper(queue)
257    --local lookAndFeel = CEGUI.WidgetLookManager:getSingleton():getWidgetLook(queue.window:getLookNFeel())
258    --local formattedArea = lookAndFeel:getNamedArea("ItemRenderingArea"):getArea():getPixelRect(queue.window)
259    --local frameHeight = queue.window:getUnclippedOuterRect():getHeight() - formattedArea:getHeight()
260    --listbox:removeItem(item)
261    --return frameHeight + singleItemHeight*size
262    return singleItemHeight*size + 1
[7342]263end
264
[8371]265function P.itemHeightHelper(queue)
266    local item = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/NotificationLayer/Root/Test/")
267    item:setText("text")
268    P.setItemFontHelper(item, queue, true)
269    queue.window:addChildWindow(item)
270    item:setSize(CEGUI.UVector2(CEGUI.UDim(1, 0), CEGUI.UDim(1, 0)))
271    item:setProperty("FrameEnabled", "false")
272    local height = getStaticTextWindowHeight(item)
273    queue.window:removeChildWindow(item)
274    winMgr:destroyWindow(item)
275    return height
276end
277
[7338]278return P
Note: See TracBrowser for help on using the repository browser.