Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/cegui0.8_ogre1.9/data/gui/scripts/NotificationLayer.lua @ 12209

Last change on this file since 12209 was 11802, checked in by landauf, 7 years ago

migrated NotificationLayer.lua to cegui 0.8

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