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
Line 
1-- NotificationLayer.lua
2
3local P = createMenuSheet("NotificationLayer", true, TriBool.True, TriBool.True)
4
5P.queueList = {}
6
7P.sampleWindow = nil
8
9-- Loads the queues from the NotificationManager and creates the sample window, that is used to measure the width various strings need.
10function P.onLoad()
11    orxonox.NotificationManager:getInstance():loadQueues()
12    P.sampleWindow = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/NotificationLayer/Root/SampleWindow")
13end
14
15-- Creates a queue in the GUI.
16function P.createQueue(name, size)
17    local root = winMgr:getWindow("orxonox/NotificationLayer/Root")
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")
23    root:addChildWindow(queue)
24
25    local queueTuple =
26    {
27        ["window"]    = queue,
28        ["name"]      = name,
29        ["edit"]      = nil,
30        ["visible"]   = false,
31        ["fontSize"]  = 12,
32        ["fontColor"] = "FFFFFFFF",
33        ["alignment"] = "LeftAligned",
34        ["items"]     = {},
35        ["first"]     = 1,
36        ["last"]      = 1
37    }
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))))
41
42    P.queueList[name] = queueTuple -- name access
43    P.setVisible(queueTuple, false) -- Set the queue to invisible as long as there are no notifications in it.
44end
45
46-- Removes a queue from the GUI.
47function P.removeQueue(queueName)
48    local queue = P.queueList[queueName]
49
50    if queue ~= nil then
51        winMgr:destroyWindow(queue.window)
52    end
53    P.queueList[queueName] = nil
54end
55
56-- Pushes an input notification to the input queue.
57function P.pushNotification(queueName, notification)
58    local queue = P.queueList[queueName]
59    if queue == nil then
60        return
61    end
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
73    end
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")
81    item:setProperty("HorzFormatting", queue.alignment)
82    queue.items[queue.last] = item
83    queue.last = queue.last+1
84
85    -- If the queue has been invisible, set it to visible.
86    if queue.visible == false then
87        P.setVisible(queue, true)
88    end
89end
90
91-- Pops the least recently added notification from the queue.
92function P.popNotification(queueName)
93    local queue = P.queueList[queueName]
94    if queue == nil then
95        return
96    end
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
102
103    -- Sets the queue to invisible if there are no more notifications in it.
104    if queue.last-queue.first == 0 then
105        P.setVisible(queue, false)
106    end
107end
108
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.
110function P.removeNotification(queueName, index)
111    local queue = P.queueList[queueName]
112    if queue == nil then
113        return
114    end
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
122    -- Removes the item.
123    local item = queue.items[index]
124    queue.window:removeChildWindow(item)
125    winMgr:destroyWindow(item)
126    queue.items[index] = nil
127
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
142    -- Sets the queue to invisible if there are no more notifications in it.
143    if queue.last-queue.first == 0 then
144        P.setVisible(queue, false)
145    end
146end
147
148-- Clears the queue. Removes all notifications from it.
149function P.clearQueue(queueName)
150    local queue = P.queueList[queueName]
151    if queue == nil then
152        return
153    end
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
162
163    -- Sets the queue to invisible.
164    P.setVisible(queue, false)
165end
166
167-- Sets the visibility of the queue.
168function P.setVisible(queue, visible)
169    if queue == nil then
170        return
171    end
172    queue.window:setVisible(visible)
173    queue.visible = visible
174end
175
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
199-- The parameters are (in order) 'name of the queue', 'font size', 'ARGB of the font color in hex notation'.
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
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
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
245        item:setProperty("TextColours", "tl:" .. queue.fontColor .. " tr:" .. queue.fontColor .. " bl:" .. queue.fontColor .. " br:" .. queue.fontColor .. "")
246    end
247end
248
249-- Helper function. Returns height a queue needs to have to display 'size' items.
250function P.queueHeightHelper(queue, size)
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
263end
264
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
278return P
Note: See TracBrowser for help on using the repository browser.