Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/HUD_HS16/data/gui/scripts/NotificationLayer.lua @ 11293

Last change on this file since 11293 was 8858, checked in by landauf, 13 years ago

merged output branch back to trunk.

Changes:

  • you have to include util/Output.h instead of util/Debug.h
  • COUT(x) is now called orxout(level)
  • output levels are now defined by an enum instead of numbers. see util/Output.h for the definition
  • it's possible to use output contexts with orxout(level, context). see util/Output.h for some common contexts. you can define more contexts
  • you must use 'endl' at the end of an output message, '\n' does not flush the message

Output levels:

  • instead of COUT(0) use orxout()
  • instead of COUT(1) use orxout(user_error) or orxout(internal_error)
  • instead of COUT(2) use orxout(user_warning) or orxout(internal_warning)
  • instead of COUT(3) use orxout(user_status/user_info) or orxout(internal_status/internal_info)
  • instead of COUT(4) use orxout(verbose)
  • instead of COUT(5) use orxout(verbose_more)
  • instead of COUT(6) use orxout(verbose_ultra)

Guidelines:

  • user_* levels are for the user, visible in the console and the log-file
  • internal_* levels are for developers, visible in the log-file
  • verbose_* levels are for debugging, only visible if the context of the output is activated

Usage in C++:

  • orxout() << "message" << endl;
  • orxout(level) << "message" << endl;
  • orxout(level, context) << "message" << endl;

Usage in Lua:

  • orxout("message")
  • orxout(orxonox.level.levelname, "message")
  • orxout(orxonox.level.levelname, "context", "message")

Usage in Tcl (and in the in-game-console):

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