Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 10, 2010, 11:17:02 PM (14 years ago)
Author:
dafrick
Message:

Some cleanup and documenting.
After some more extensive testing it seems to work, the code looks ok as well…

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/notifications/data/gui/scripts/NotificationLayer.lua

    r7398 r7399  
    88P.sampleWindow = nil
    99
     10-- Loads the queues from the NotificationManager and creates the sample window, that is used to measure the width various strings need.
    1011function P.onLoad()
    1112    orxonox.NotificationManager:getInstance():loadQueues()
     
    1314end
    1415
     16-- Creates a queue in the GUI.
    1517function P.createQueue(name, size)
    1618    local root = winMgr:getWindow("orxonox/NotificationLayer/Root")
    1719    local queue = winMgr:createWindow("MenuWidgets/Listbox", "orxonox/NotificationLayer/Root/Queue/" .. name)
    18     queue:setProperty("BackgroundColor", "00FFFFFF")
     20    queue:setProperty("BackgroundColor", "00FFFFFF") -- Set background to be fully transparent.
    1921    root:addChildWindow(queue)
    2022
     
    3133
    3234    P.queueList[name] = queueTuple -- name access
    33     P.setVisible(queueTuple, false)
    34 end
    35 
    36 function P.removeQueue(name)
    37     local queue = P.queueList[name]
     35    P.setVisible(queueTuple, false) -- Set the queue to invisible as long as there are no notifications in it.
     36end
     37
     38-- Removes a queue from the GUI.
     39function P.removeQueue(queueName)
     40    local queue = P.queueList[queueName]
    3841
    3942    if queue ~= nil then
    4043        winMgr:destroyWindow(queue.window)
    4144    end
    42     P.queueList[name] = nil
    43 end
    44 
     45    P.queueList[queueName] = nil
     46end
     47
     48-- Pushes an input notification to the input queue.
    4549function P.pushNotification(queueName, notification)
    4650    local queue = P.queueList[queueName]
    4751    if queue == nil then
    48         cout(0, "0Queue is nil! " .. queueName)
    4952        return
    5053    end
    5154    item = CEGUI.createListboxTextItem(notification)
    5255    local listbox = CEGUI.toListbox(queue.window)
     56    -- Add the item to the top of the listbox.
    5357    if listbox:getItemCount() == 0 then
    5458        listbox:addItem(item)
     
    5761    end
    5862
     63    -- If the queue has been invisible, set it to visible.
    5964    if queue.visible == false then
    6065        P.setVisible(queue, true)
     
    6267end
    6368
     69-- Pops the least recently added notification from the queue.
    6470function P.popNotification(queueName)
    6571    local queue = P.queueList[queueName]
    6672    if queue == nil then
    67         cout(0, "1Queue is nil! " .. queueName)
    6873        return
    6974    end
    7075    local listbox = CEGUI.toListbox(queue.window)
     76    -- Removes the item from the bottom of the listbox.
    7177    listbox:removeItem(listbox:getListboxItemFromIndex(listbox:getItemCount()-1))
    7278
     79    -- Sets the queue to invisible if there are no more notifications in it.
    7380    if listbox:getItemCount() == 0 then
    7481        P.setVisible(queue, false)
     
    7683end
    7784
     85-- Removes a notification at a given index from the queue.
    7886function P.removeNotification(queueName, index)
    7987    local queue = P.queueList[queueName]
    8088    if queue == nil then
    81         cout(0, "2Queue is nil! " .. queueName)
    8289        return
    8390    end
    8491    local listbox = CEGUI.toListbox(queue.window)
     92    -- Removes the item.
    8593    listbox:removeItem(listbox:getListboxItemFromIndex(tonumber(index)))
    8694
     95    -- Sets the queue to invisible if there are no more notifications in it.
    8796    if listbox:getItemCount() == 0 then
    8897        P.setVisible(queue, false)
     
    9099end
    91100
    92 function P.clearQueue(name)
    93     local queue = P.queueList[name]
    94     if queue == nil then
    95         cout(0, "3Queue is nil! " .. name)
     101-- Clears the queue. Removes all notifications from it.
     102function P.clearQueue(queueName)
     103    local queue = P.queueList[queueName]
     104    if queue == nil then
    96105        return
    97106    end
     
    99108    CEGUI.toListbox(queue.window):resetList()
    100109
     110    -- Sets the queue to invisible.
    101111    P.setVisible(queue, false)
    102112end
    103113
    104 function P.changeSize(name, size)
    105     local queue = P.queueList[name]
    106     if queue == nil then
    107         cout(0, "5Queue is nil! " .. name)
    108         return
    109     end
    110     queue.window:setHeight(CEGUI.UDim(0, P.queueHeightHelper(queue.window, size)))
    111 end
    112 
     114-- Sets the visibility of the queue.
    113115function P.setVisible(queue, visible)
    114116    if queue == nil then
    115         cout(0, "6Queue is nil! " .. queue.name)
    116117        return
    117118    end
     
    120121end
    121122
     123-- Enter the edit mode of the notification layer.
    122124function P.enterEditMode()
    123125    P.editMode = true
     
    139141    vertOffset = 0
    140142    horzOffset = 0
     143    -- Line to be able to create a new queue.
    141144    local newQueueTitle = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/NotificationLayer/Root/EditMode/ControlWindow/NewQueueTitle")
    142145    newQueueTitle:setText("Create a new NotificationQueue:")
     
    165168
    166169    horzOffset = 0
     170    -- Button to leave the edit mode.
    167171    local leave = winMgr:createWindow("MenuWidgets/Button", "orxonox/NotificationLayer/Root/EditMode/ControlWindow/LeaveEditModeButton")
    168172    leave:setText("leave Edit Mode")
     
    180184        if v ~= nil then
    181185            local queue = P.queueList[k]
    182             root:removeChildWindow(queue.window)
    183            
    184             local window = P.createQueueEditFrame(queue.name)
    185             window:setArea(queue.window:getArea())
    186            
    187             queue.edit = window
     186            -- Remove the window that displays the queue from the root window such that it is no longer displayed.
     187            root:removeChildWindow(v.window)
     188
     189            -- Create the frame window, with options to edit the queue, that is displayed instead of the queue.
     190            local window = P.createQueueEditFrame(v.name)
     191            window:setArea(v.window:getArea()) -- Set the frame window size and position to the same as the queue.
     192
     193            v.edit = window
    188194        end
    189195    end
    190196end
    191197
    192 function P.createQueueEditFrame(name)
     198-- Helper function. Creates a frame for the input queue.
     199function P.createQueueEditFrame(queueName)
    193200    local root = winMgr:getWindow("orxonox/NotificationLayer/Root")
    194     window = winMgr:createWindow("MenuWidgets/FrameWindow", "orxonox/NotificationLayer/Root/EditMode/" .. name)
     201
     202    window = winMgr:createWindow("MenuWidgets/FrameWindow", "orxonox/NotificationLayer/Root/EditMode/" .. queueName)
    195203    local frame = tolua.cast(window, "CEGUI::FrameWindow")
    196204    frame:setCloseButtonEnabled(true)
    197205    orxonox.GUIManager:subscribeEventHelper(frame, "CloseClicked", P.name .. ".closeQueue_clicked")
    198     frame:setText("NotificationQueue \"" .. name .. "\"")
     206    frame:setText("NotificationQueue \"" .. queueName .. "\"")
    199207    root:addChildWindow(window)
    200     local pane = winMgr:createWindow("MenuWidgets/ScrollablePane", "orxonox/NotificationLayer/Root/EditMode/" .. name .. "/ScrollingPane")
     208    local pane = winMgr:createWindow("MenuWidgets/ScrollablePane", "orxonox/NotificationLayer/Root/EditMode/" .. queueName .. "/ScrollingPane")
    201209    pane:setSize(CEGUI.UVector2(CEGUI.UDim(1,-20), CEGUI.UDim(1,-30)))
    202210    pane:setPosition(CEGUI.UVector2(CEGUI.UDim(0, 10), CEGUI.UDim(0, 26)))
     
    206214    local vertOffset = 0
    207215
    208     local targetsTitle = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/NotificationLayer/Root/EditMode/" .. name .. "/TargetsTitle")
     216    -- The line that lets you edit the targets of the queue.
     217    local targetsTitle = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/NotificationLayer/Root/EditMode/" .. queueName .. "/TargetsTitle")
    209218    targetsTitle:setText("Targets:")
    210219    local size = getMinTextSize(targetsTitle)
     
    214223    pane:addChildWindow(targetsTitle)
    215224    horzOffset = horzOffset + size[2] + 5
    216     local targets = winMgr:createWindow("MenuWidgets/Editbox", "orxonox/NotificationLayer/Root/EditMode/" .. name .. "/Targets")
     225    local targets = winMgr:createWindow("MenuWidgets/Editbox", "orxonox/NotificationLayer/Root/EditMode/" .. queueName .. "/Targets")
    217226    targets:setProperty("ReadOnly", "set:False")
    218     local targetsText = orxonox.NotificationManager:getInstance():getQueue(name):getTargets()
     227    local targetsText = orxonox.NotificationManager:getInstance():getQueue(queueName):getTargets()
    219228    targets:setText(targetsText)
    220229    P.sampleWindow:setText(targetsText)
     
    224233    horzOffset = horzOffset + size[2]*2+20 + 5
    225234    pane:addChildWindow(targets)
    226     local save = winMgr:createWindow("MenuWidgets/Button", "orxonox/NotificationLayer/Root/EditMode/" .. name .. "/Targets/Save")
     235    local save = winMgr:createWindow("MenuWidgets/Button", "orxonox/NotificationLayer/Root/EditMode/" .. queueName .. "/Targets/Save")
    227236    save:setText("save")
    228237    P.sampleWindow:setText("save")
     
    237246
    238247    horzOffset = 0
    239     local sizeTitle = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/NotificationLayer/Root/EditMode/" .. name .. "/SizeTitle")
     248    -- The line that lets you edit the size of the queue.
     249    local sizeTitle = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/NotificationLayer/Root/EditMode/" .. queueName .. "/SizeTitle")
    240250    sizeTitle:setText("Size:")
    241251    size = getMinTextSize(sizeTitle)
     
    244254    pane:addChildWindow(sizeTitle)
    245255    horzOffset = horzOffset + size[2] + 5
    246     local queueSize = winMgr:createWindow("MenuWidgets/Editbox", "orxonox/NotificationLayer/Root/EditMode/" .. name .. "/Size")
     256    local queueSize = winMgr:createWindow("MenuWidgets/Editbox", "orxonox/NotificationLayer/Root/EditMode/" .. queueName .. "/Size")
    247257    queueSize:setProperty("ReadOnly", "set:False")
    248     local maxSize = orxonox.NotificationManager:getInstance():getQueue(name):getMaxSize()
     258    local maxSize = orxonox.NotificationManager:getInstance():getQueue(queueName):getMaxSize()
    249259    queueSize:setText(maxSize)
    250260    P.sampleWindow:setText(maxSize)
     
    254264    horzOffset = horzOffset + size[2]*2+20 + 5
    255265    pane:addChildWindow(queueSize)
    256     save = winMgr:createWindow("MenuWidgets/Button", "orxonox/NotificationLayer/Root/EditMode/" .. name .. "/Size/Save")
     266    save = winMgr:createWindow("MenuWidgets/Button", "orxonox/NotificationLayer/Root/EditMode/" .. queueName .. "/Size/Save")
    257267    save:setText("save")
    258268    P.sampleWindow:setText("save")
     
    267277
    268278    horzOffset = 0
    269     local displayTimeTitle = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/NotificationLayer/Root/EditMode/" .. name .. "/DisplayTimeTitle")
     279    -- The line that lets you edit the display time of the queue.
     280    local displayTimeTitle = winMgr:createWindow("MenuWidgets/StaticText", "orxonox/NotificationLayer/Root/EditMode/" .. queueName .. "/DisplayTimeTitle")
    270281    displayTimeTitle:setText("Display time:")
    271282    size = getMinTextSize(displayTimeTitle)
     
    274285    pane:addChildWindow(displayTimeTitle)
    275286    horzOffset = horzOffset + size[2] + 5
    276     local displayTime = winMgr:createWindow("MenuWidgets/Editbox", "orxonox/NotificationLayer/Root/EditMode/" .. name .. "/DisplayTime")
     287    local displayTime = winMgr:createWindow("MenuWidgets/Editbox", "orxonox/NotificationLayer/Root/EditMode/" .. queueName .. "/DisplayTime")
    277288    displayTime:setProperty("ReadOnly", "set:False")
    278     local time = orxonox.NotificationManager:getInstance():getQueue(name):getDisplayTime()
     289    local time = orxonox.NotificationManager:getInstance():getQueue(queueName):getDisplayTime()
    279290    displayTime:setText(time)
    280291    P.sampleWindow:setText(time)
     
    284295    horzOffset = horzOffset + size[2]*2+20 + 5
    285296    pane:addChildWindow(displayTime)
    286     save = winMgr:createWindow("MenuWidgets/Button", "orxonox/NotificationLayer/Root/EditMode/" .. name .. "/DisplayTime/Save")
     297    save = winMgr:createWindow("MenuWidgets/Button", "orxonox/NotificationLayer/Root/EditMode/" .. queueName .. "/DisplayTime/Save")
    287298    save:setText("save")
    288299    P.sampleWindow:setText("save")
     
    299310end
    300311
     312-- Leave the edit mode.
    301313function P.leaveEditMode()
    302314    P.editMode = false
     
    306318    for k,v in pairs(P.queueList) do
    307319        if v ~= nil then
     320            -- Add the queue window to the root window to have it displayed again.
    308321            root:addChildWindow(v.window)
     322            -- Set the size and position of the queue window to the size and position of the queue edit frame.
    309323            v.window:setArea(v.edit:getArea())
     324            -- Destroy the edit frame.
    310325            winMgr:destroyWindow(v.edit)
    311326            v.edit = nil
     
    317332end
    318333
     334-- Is called after the sheet has been hidden.
    319335function P.afterHide()
     336    -- If we leave the edit mode we show the sheet again.
    320337    if P.editMode then
    321338        P.leaveEditMode()
     
    324341end
    325342
     343-- If the button to save the targets of a queue has been clicked.
    326344function P. saveTargets_clicked(e)
    327345    local we = CEGUI.toWindowEventArgs(e)
     
    337355
    338356    local queue = orxonox.NotificationManager:getInstance():getQueue(queueName)
     357    -- Set the new targets.
    339358    queue:setTargets(window:getText())
    340359    local targets = queue:getTargets()
     
    343362    P.sampleWindow:setText(targets)
    344363    local size = getMinTextSize(P.sampleWindow)
     364    -- Adjust the width of the targets field.
    345365    window:setWidth(CEGUI.UDim(0, size[2]*2+20))
     366    -- Adjust the position of the save button after the targets field.
    346367    save:setXPosition(CEGUI.UDim(0, save:getXPosition():asAbsolute(1)-width+window:getWidth():asAbsolute(1)))
    347368end
    348369
     370-- If the button to save the size if a queue has been clicked.
    349371function P. saveSize_clicked(e)
    350372    local we = CEGUI.toWindowEventArgs(e)
     
    360382
    361383    local queue = orxonox.NotificationManager:getInstance():getQueue(queueName)
     384    -- Set the new size.
    362385    queue:setMaxSize(tonumber(window:getText()))
    363386    local maxSize = queue:getMaxSize()
     
    366389    P.sampleWindow:setText(maxSize)
    367390    local size = getMinTextSize(P.sampleWindow)
     391    -- Adjust the width of the size field.
    368392    window:setWidth(CEGUI.UDim(0, size[2]*2+20))
     393    -- Adjust the position of the save button after the size field.
    369394    save:setXPosition(CEGUI.UDim(0, save:getXPosition():asAbsolute(1)-width+window:getWidth():asAbsolute(1)))
    370395end
    371396
     397-- If the button to save the display time if a queue has been clicked.
    372398function P. saveDisplayTime_clicked(e)
    373399    local we = CEGUI.toWindowEventArgs(e)
     
    383409
    384410    local queue = orxonox.NotificationManager:getInstance():getQueue(queueName)
     411    -- Set the new display time.
    385412    queue:setDisplayTime(tonumber(window:getText()))
    386413    local time = queue:getDisplayTime()
     
    389416    P.sampleWindow:setText(time)
    390417    local size = getMinTextSize(P.sampleWindow)
     418    -- Adjust the width of the display time field.
    391419    window:setWidth(CEGUI.UDim(0, size[2]*2+20))
     420    -- Adjust the position of the save button after the display time field.
    392421    save:setXPosition(CEGUI.UDim(0, save:getXPosition():asAbsolute(1)-width+window:getWidth():asAbsolute(1)))
    393422end
    394423
     424-- if the button to create a new queue has been clicked.
    395425function P.createNewQueue_clicked(e)
    396426    local window = winMgr:getWindow("orxonox/NotificationLayer/Root/EditMode/ControlWindow/NewQueueName")
    397427    local name = window:getText()
     428    -- Creates the new queue.
    398429    orxonox.NotificationManager:getInstance():createQueue(name)
    399430
    400431    local queue = P.queueList[name]
    401432    if queue == nil then
    402         cout(0, "7Queue is nil! " .. name)
    403         return
    404     end
    405    
     433        return
     434    end
     435
     436    -- Create the frame that represents the queue in edit mode, since that's what we're in.
    406437    local frame = P.createQueueEditFrame(name)
    407438    local root = winMgr:getWindow("orxonox/NotificationLayer/Root")
     439    -- Remove the queue window from the root window, since we're in edit mode.
    408440    root:removeChildWindow(queue.window)
     441    -- Set the frame window size and position to that of the queue window.
    409442    frame:setArea(queue.window:getArea())
    410443    queue.edit = frame
    411    
     444
     445    -- Reset the text to create a new queue.
    412446    window:setText("")
    413447end
    414448
     449-- If the button to leave the edit mode has been clicked.
    415450function P.leaveEditMode_clicked(e)
    416451    hideMenuSheet(P.name)
    417452end
    418453
     454-- If the button to close the queue has been clicked.
    419455function P.closeQueue_clicked(e)
    420456    local we = CEGUI.toWindowEventArgs(e)
     
    425461    local queueName = string.sub(nameStr, 10, string.len(nameStr))
    426462
     463    -- Destroy the frame window,
    427464    winMgr:destroyWindow(P.queueList[queueName].edit)
    428465    P.queueList[queueName].edit = nil
     466    -- Destroy the queue.
    429467    orxonox.NotificationManager:getInstance():getQueue(queueName):destroy()
    430468end
    431469
     470-- Helper function. Returns height a queue needs to have to display 'size' items.
    432471function P.queueHeightHelper(queue, size)
    433472    local listbox = CEGUI.toListbox(queue)
Note: See TracChangeset for help on using the changeset viewer.