Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/notifications/data/gui/scripts/NotificationLayer.lua @ 7351

Last change on this file since 7351 was 7351, checked in by dafrick, 15 years ago

NotificationQueue now disappears if there are no notifications to be displayed. Also: "Fixed" Seg-fault.

File size: 4.0 KB
Line 
1-- NotificationLayer.lua
2
3local P = createMenuSheet("NotificationLayer")
4
5P.queueList = {}
6P.nameList = {}
7P.visible = nil
8
9function P.createQueue(name, size)
10    local root = winMgr:getWindow("orxonox/NotificationLayer/Root")
11    local queue = winMgr:createWindow("MenuWidgets/Listbox", "orxonox/NotificationLayer/Root/Queue/" .. name)
12    queue:setProperty("BackgroundColor", "66FFFFFF")
13    root:addChildWindow(queue)
14
15    queue:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 0), CEGUI.UDim(0, 0)))
16    queue:setSize(CEGUI.UVector2(CEGUI.UDim(1.0, 0), CEGUI.UDim(0, P.queueHeightHelper(queue, size))))
17
18    P.setVisible(queue, false)
19
20    table.insert(P.queueList, queue)
21    table.insert(P.nameList, name)
22    --TODO: Check name for uniqueness.
23end
24
25function P.removeQueue(name)
26    local queue = nil
27
28    for k,v in pairs(P.nameList) do
29        if v == name then
30            P.nameList[k] = nil
31            queue = P.queueList[k]
32            P.queueList[k] = nil
33            break
34        end
35    end
36
37    if queue ~= nil then
38        winMgr:destroyWindow(queue)
39    end
40end
41
42function P.pushNotification(queueName, notification)
43    local queue = P.nameToQueueHelper(queueName)
44    if queue == nil then
45        cout(0, "Queue is nil!")
46        return
47    end
48    item = CEGUI.createListboxTextItem(notification)
49    local listbox = CEGUI.toListbox(queue)
50    if listbox:getItemCount() == 0 then
51        listbox:addItem(item)
52    else
53        listbox:insertItem(item, listbox:getListboxItemFromIndex(0))
54    end
55
56    if P.visible == false then
57        P.setVisible(queue, true)
58    end
59end
60
61function P.popNotification(queueName)
62    local queue = P.nameToQueueHelper(queueName)
63    if queue == nil then
64        cout(0, "Queue is nil!")
65        return
66    end
67    local listbox = CEGUI.toListbox(queue)
68    listbox:removeItem(listbox:getListboxItemFromIndex(listbox:getItemCount()-1))
69
70    if listbox:getItemCount() == 0 then
71        P.setVisible(queue, false)
72    end
73end
74
75function P.removeNotification(queueName, index)
76    local queue = P.nameToQueueHelper(queueName)
77    if queue == nil then
78        cout(0, "Queue is nil!")
79        return
80    end
81    local listbox = CEGUI.toListbox(queue)
82    listbox:removeItem(listbox:getListboxItemFromIndex(tonumber(index)))
83
84    if listbox:getItemCount() == 0 then
85        P.setVisible(queue, false)
86    end
87end
88
89function P.clearQueue(name)
90    local queue = P.nameToQueueHelper(name)
91    if queue == nil then
92        cout(0, "Queue is nil!")
93        return
94    end
95    local listbox = CEGUI.toListbox(queue)
96    CEGUI.toListbox(queue):resetList()
97
98    P.setVisible(queue, false)
99end
100
101function P.changePosition(name, xPos, yPos)
102    local queue = P.nameToQueueHelper(name)
103    if queue == nil then
104        cout(0, "Queue is nil!")
105        return
106    end
107    queue:setPosition(CEGUI.UVector2(CEGUI.UDim(tonumber(xPos), 0), CEGUI.UDim(tonumber(yPos), 0)))
108    queue:setWidth(CEGUI.UDim(1.0, -xPos))
109end
110
111function P.changeSize(name, size)
112    local queue = P.nameToQueueHelper(name)
113    if queue == nil then
114        cout(0, "Queue is nil!")
115        return
116    end
117    queue:setHeight(CEGUI.UDim(0, P.queueHeightHelper(queue, size)))
118end
119
120function P.setVisible(queue, visible)
121    queue:setVisible(visible)
122    P.visible = visible
123end
124
125function P.nameToQueueHelper(name)
126    local queue = nil
127    for k,v in pairs(P.nameList) do
128        if v == name then
129            queue = P.queueList[k]
130            break
131        end
132    end
133    return queue
134end
135
136function P.queueHeightHelper(queue, size)
137    local listbox = CEGUI.toListbox(queue)
138    local item = CEGUI.createListboxTextItem("Text")
139    listbox:addItem(item)
140    local singleItemHeight = listbox:getTotalItemsHeight()
141    local lookAndFeel = CEGUI.WidgetLookManager:getSingleton():getWidgetLook(queue:getLookNFeel())
142    local formattedArea = lookAndFeel:getNamedArea("ItemRenderingArea"):getArea():getPixelRect(queue)
143    local frameHeight = queue:getUnclippedPixelRect():getHeight() - formattedArea:getHeight()
144    listbox:removeItem(item)
145    return frameHeight + singleItemHeight*size
146end
147
148return P
149
Note: See TracBrowser for help on using the repository browser.