Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/data/gui/scripts/SheetManager.lua @ 7594

Last change on this file since 7594 was 7403, checked in by dafrick, 14 years ago

Merged notifications branch back to trunk.

  • Property svn:eol-style set to native
File size: 6.5 KB
RevLine 
[6737]1-- SheetManager.lua
[6595]2
[6737]3local cursor = CEGUI.MouseCursor:getSingleton()
[6662]4local loadedSheets = {}
[6718]5local activeMenuSheets = {size = 0, topSheetTuple = nil}
[6737]6local menuSheetsRoot = guiMgr:getMenuRootWindow()
[5491]7
[6662]8-----------------------
9--- Local functions ---
10-----------------------
11
12local function hideCursor()
13    if cursor:isVisible() then
14        cursor:hide()
15    end
16end
17
18local function showCursor()
[6737]19    if not cursor:isVisible() and inputMgr:isMouseExclusive() then
[6662]20        cursor:show()
21    end
22end
23
[5491]24
[6662]25------------------------
26--- Global functions ---
27------------------------
28
[6737]29-- Loads the GUI with the specified name
30-- The name corresponds to the filename of the *.lua and *.layout files
31-- but without the extension
32function loadSheet(name)
33    -- Check if it has already been loaded
34    local sheet = loadedSheets[name]
35    if sheet == nil then
36        -- Load the sheet
37        sheet = require(name)
38        sheet:load()
39        loadedSheets[name] = sheet
40    end
41    return sheet
42end
43
[6662]44-- ?
[7403]45function showMenuSheet(name, bHidePrevious, bNoInput, ptr)
46    local sheet = showMenuSheet(name, bHidePrevious, bNoInput)
[6718]47    sheet.overlay = ptr
48    return sheet
[5491]49end
50
[6662]51-- Shows the specified menu sheet and loads it if neccessary
[7403]52function showMenuSheet(name, bHidePrevious, bNoInput)
[6737]53    if name == "" then
54        return nil
55    end
[6718]56    -- Get sheet (or load it)
57    local menuSheet = loadSheet(name)
[5491]58
[6718]59    -- Use sheet's value if nil was provided
60    if bHidePrevious == nil then
61        bHidePrevious = menuSheet.bHidePrevious
62        assert(bHidePrevious ~= nil)
63    end
64
[7403]65    -- Set bNoInput to false if it hasn't been set.
66    if bNoInput == nil then
67        bNoInput = false
68    end
69
70    -- Count the number of sheets that don't need input till the first that does.
71    local counter = noInputSheetCounter()
[6721]72    -- Pause game control if this is the first menu to be displayed
73    -- HUGE HACK?
[7403]74    if bNoInput == false and counter == 0 then
[6721]75        orxonox.HumanController:pauseControl()
76    end
77
[6662]78    -- Hide if already displayed (to make sure it is up front in the end)
79    if activeMenuSheets[name] ~= nil then
[6722]80        hideMenuSheet(name)
[6662]81    end
82
[7403]83    if bNoInput == true then
84        menuSheet.tShowCursor = TriBool.Dontcare
85    end
86
[6718]87    -- Add the sheet in a tuple of additional information
88    local sheetTuple =
89    {
90        ["sheet"]          = menuSheet,
[7403]91        ["bHidePrevious"]  = bHidePrevious,
92        ["bNoInput"]       = bNoInput
[6718]93    }
94    table.insert(activeMenuSheets, sheetTuple) -- indexed array access
95    activeMenuSheets[name] = sheetTuple -- name access
96    activeMenuSheets.size = activeMenuSheets.size + 1
97    activeMenuSheets.topSheetTuple = sheetTuple
98
[6662]99    -- Add sheet to the root window
[6737]100    menuSheetsRoot:addChildWindow(menuSheet.window)
[6662]101
102    -- Handle input distribution
[7403]103    if bNoInput == false then
104        inputMgr:enterState(menuSheet.inputState)
105    end
[6662]106
[6718]107    -- Only change cursor situation if menuSheet.tShowCursor ~= TriBool.Dontcare
108    if menuSheet.tShowCursor == TriBool.True then
[6417]109        showCursor()
[6718]110    elseif menuSheet.tShowCursor == TriBool.False then
[6417]111        hideCursor()
[5491]112    end
[6417]113
[6662]114    -- Hide all previous sheets if necessary
115    if bHidePrevious then
116        for i = 1, activeMenuSheets.size - 1 do
[6718]117            activeMenuSheets[i].sheet:hide()
[6417]118        end
119    end
[5491]120
[6662]121    menuSheet:show()
[6718]122
[6662]123    return menuSheet
[5491]124end
125
[6722]126function hideMenuSheet(name)
[6662]127    local sheetTuple = activeMenuSheets[name]
128    if sheetTuple == nil then
129        return
[6417]130    end
[5491]131
[6662]132    -- Hide the sheet
[6718]133    sheetTuple.sheet:hide()
[6662]134
135    -- Show sheets that were hidden by the sheet to be removed
136    local i = activeMenuSheets.size
137    -- Only do something if all sheets on top of sheetTuple
[6718]138    -- have bHidePrevious == true and sheetTuple.bHidePrevious == true
[6662]139    while i > 0 do
[6718]140        if activeMenuSheets[i].bHidePrevious then
[6662]141            if activeMenuSheets[i] == sheetTuple then
142                i = i - 1
143                while i > 0 do
[6718]144                    activeMenuSheets[i].sheet:show()
145                    if activeMenuSheets[i].bHidePrevious then
[6662]146                        break
147                    end
148                    i = i - 1
[6417]149                end
150            end
[6662]151            break
[6417]152        end
[6662]153        i = i - 1
[6417]154    end
[6662]155
156    -- Remove sheet with its tuple from the table
[6737]157    menuSheetsRoot:removeChildWindow(sheetTuple.sheet.window)
[6671]158    table.remove(activeMenuSheets, table.findIndex(activeMenuSheets, sheetTuple))
[6662]159    activeMenuSheets[name] = nil
160    activeMenuSheets.size = activeMenuSheets.size - 1
[6718]161    activeMenuSheets.topSheetTuple = activeMenuSheets[activeMenuSheets.size]
[6662]162
163    -- Leave the input state
[7403]164    if not sheetTuple.bNoInput then
165        inputMgr:leaveState(sheetTuple.sheet.inputState)
166    end
[6662]167   
[6718]168    -- CURSOR SHOWING
169    local i = activeMenuSheets.size
170    -- Find top most sheet that doesn't have tShowCusor == TriBool.Dontcare
171    while i > 0 and activeMenuSheets[i].sheet.tShowCursor == TriBool.Dontcare do
172        i = i - 1
173    end
174    if i > 0 and activeMenuSheets[i].sheet.tShowCursor == TriBool.True then
[6662]175        showCursor()
176    else
177        hideCursor()
178    end
179
[7403]180    -- Count the number of sheets that don't need input till the first that does.
181    local counter = noInputSheetCounter()
182    -- Resume control if the last (non-noInput) menu is hidden
183    if counter == 0 then
[6417]184        orxonox.HumanController:resumeControl()
185        hideCursor()
186    end
[7403]187
188    sheetTuple.sheet:afterHide()
[5491]189end
[6417]190
[6662]191-- Hides all menu GUI sheets
[6722]192function hideAllMenuSheets()
[6662]193    while activeMenuSheets.size ~= 0 do
[6722]194        hideMenuSheet(activeMenuSheets.topSheetTuple.sheet.name)
[6417]195    end
196end
197
198function keyESC()
[6662]199    -- HUGE, very HUGE hacks!
[7403]200
201    -- Count the number of sheets that don't need input till the first that does.
202    local counter = noInputSheetCounter()
203
204    -- If the first sheet that needs input is the MainMenu.
205    if counter == 1 and activeMenuSheets[1].sheet.name == "MainMenu" then
[6417]206        orxonox.execute("exit")
[7403]207    -- If there is at least one sheet that needs input.
208    elseif counter > 0 then
209        orxonox.execute("hideGUI "..activeMenuSheets[counter].sheet.name)
[6417]210    else
[6722]211        showMenuSheet("InGameMenu")
[6417]212    end
213end
214
[6737]215function setBackgroundImage(imageSet, imageName)
216    guiMgr:setBackgroundImage(imageSet, imageName)
[6417]217end
[6737]218
[7403]219function noInputSheetCounter()
220    -- Count the number of sheets that don't need input till the first that does.
221    local counter = activeMenuSheets.size
222    while counter > 0 and activeMenuSheets[counter].bNoInput do
223        counter = counter - 1
224    end
225    return counter
226end
227
[6737]228----------------------
229--- Initialisation ---
230----------------------
231
232hideCursor()
Note: See TracBrowser for help on using the repository browser.