Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Merging tutoriallevel2 branch into tutoriallevel3 branch.

  • Property svn:eol-style set to native
File size: 10.6 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        ["maxSize"]      = size,
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 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.
186function P.resizeQueue(queueName, relativeWidth, absoluteWidth, relativeHeight, absoluteHeigth)
187    local queue = P.queueList[queueName]
188    local queueWindow = queue.window
189    if queueWindow == nil then
190        return
191    end
192    if absoluteHeigth == nil then
193        absoluteHeigth = P.queueHeightHelper(queue, queue.maxSize)
194        relativeHeight = 0
195    end
196    queueWindow:setSize(CEGUI.UVector2(CEGUI.UDim(relativeWidth, absoluteWidth), CEGUI.UDim(relativeHeight, absoluteHeigth)))
197end
198
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)
202    local queue = P.queueList[queueName]
203    local queueWindow = queue.window
204    if queueWindow == nil then
205        return
206    end
207
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
225    queue.fontSize = size
226    for i=queue.first,queue.last-1 do
227        P.setItemFontHelper(queue.items[i], queue, false)
228    end
229end
230
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)
234    local queue = P.queueList[queueName]
235    local queueWindow = queue.window
236    if queueWindow == nil then
237        return
238    end
239
240    queue.fontColor = color
241    for i=queue.first,queue.last-1 do
242        P.setItemFontHelper(queue.items[i], queue, true)
243    end
244end
245
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
258        item:setProperty("TextColours", "tl:" .. queue.fontColor .. " tr:" .. queue.fontColor .. " bl:" .. queue.fontColor .. " br:" .. queue.fontColor .. "")
259    end
260end
261
262-- Helper function. Returns height a queue needs to have to display 'size' items.
263function P.queueHeightHelper(queue, size)
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
276end
277
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
291return P
Note: See TracBrowser for help on using the repository browser.