Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentationHS15/data/gui/scripts/SheetManager.lua @ 11046

Last change on this file since 11046 was 11046, checked in by landauf, 8 years ago

added argument completion function for active gui sheets.
TODO

a) there should be a better way to read back values from lua (i.e. by using LuaState instead of plain lua.h functions)
b) it shouldn't be necessary to call lua anyway to get the active gui sheets. the GUIManager should always know this.

  • Property svn:eol-style set to native
File size: 9.4 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()
[8484]7local bInGameConsoleClosed = false
[7689]8local mainMenuLoaded = false
9orxonox.GUIManager:subscribeEventHelper(menuSheetsRoot, "KeyDown", "keyPressed")
[8079]10orxonox.GUIManager:subscribeEventHelper(menuSheetsRoot, "Sized", "windowResized")
[5491]11
[8079]12------------------------
13--- Global functions ---
14------------------------
[6662]15
[8079]16function hideCursor()
[6662]17    if cursor:isVisible() then
18        cursor:hide()
19    end
20end
21
[8079]22function showCursor()
[6737]23    if not cursor:isVisible() and inputMgr:isMouseExclusive() then
[6662]24        cursor:show()
25    end
26end
27
[11046]28function getLoadedSheets()
29    local names = ""
30    for name, sheet in pairs(loadedSheets) do
31        names = names .. name
32        names = names .. ","
33    end
34    return names
35end
36
[6737]37-- Loads the GUI with the specified name
38-- The name corresponds to the filename of the *.lua and *.layout files
39-- but without the extension
40function loadSheet(name)
41    -- Check if it has already been loaded
42    local sheet = loadedSheets[name]
43    if sheet == nil then
44        -- Load the sheet
45        sheet = require(name)
46        sheet:load()
47        loadedSheets[name] = sheet
48    end
49    return sheet
50end
51
[6662]52-- ?
[7403]53function showMenuSheet(name, bHidePrevious, bNoInput, ptr)
54    local sheet = showMenuSheet(name, bHidePrevious, bNoInput)
[6718]55    sheet.overlay = ptr
56    return sheet
[5491]57end
58
[6662]59-- Shows the specified menu sheet and loads it if neccessary
[7403]60function showMenuSheet(name, bHidePrevious, bNoInput)
[6737]61    if name == "" then
62        return nil
63    end
[6718]64    -- Get sheet (or load it)
65    local menuSheet = loadSheet(name)
[5491]66
[6718]67    -- Use sheet's value if nil was provided
68    if bHidePrevious == nil then
69        bHidePrevious = menuSheet.bHidePrevious
70        assert(bHidePrevious ~= nil)
71    end
72
[7403]73    -- Set bNoInput to false if it hasn't been set.
74    if bNoInput == nil then
75        bNoInput = false
76    end
77
78    -- Count the number of sheets that don't need input till the first that does.
[7689]79    local counter = noInputSheetIndex()
[6721]80    -- Pause game control if this is the first menu to be displayed
81    -- HUGE HACK?
[7403]82    if bNoInput == false and counter == 0 then
[6721]83        orxonox.HumanController:pauseControl()
84    end
85
[6662]86    -- Hide if already displayed (to make sure it is up front in the end)
87    if activeMenuSheets[name] ~= nil then
[6722]88        hideMenuSheet(name)
[6662]89    end
90
[7403]91    if bNoInput == true then
[8729]92        menuSheet.tShowCursor = tribool(dontcare)
[7403]93    end
94
[6718]95    -- Add the sheet in a tuple of additional information
96    local sheetTuple =
97    {
98        ["sheet"]          = menuSheet,
[7403]99        ["bHidePrevious"]  = bHidePrevious,
[8079]100        ["bNoInput"]       = bNoInput,
101        ["name"]           = name
[6718]102    }
103    table.insert(activeMenuSheets, sheetTuple) -- indexed array access
104    activeMenuSheets[name] = sheetTuple -- name access
105    activeMenuSheets.size = activeMenuSheets.size + 1
106    activeMenuSheets.topSheetTuple = sheetTuple
107
[6662]108    -- Add sheet to the root window
[6737]109    menuSheetsRoot:addChildWindow(menuSheet.window)
[6662]110
[7689]111    -- If sheet is the MainMenu
112    if name == "MainMenu" then
113        mainMenuLoaded = true
114    end
115
[6662]116    -- Handle input distribution
[7403]117    if bNoInput == false then
118        inputMgr:enterState(menuSheet.inputState)
119    end
[6662]120
[8729]121    -- Only change cursor situation if menuSheet.tShowCursor ~= tribool(dontcare)
122    if menuSheet.tShowCursor == tribool(true) then
[6417]123        showCursor()
[8729]124    elseif menuSheet.tShowCursor == tribool(false) then
[6417]125        hideCursor()
[5491]126    end
[6417]127
[6662]128    -- Hide all previous sheets if necessary
[8079]129    local previous
[6662]130    if bHidePrevious then
131        for i = 1, activeMenuSheets.size - 1 do
[8079]132            previous = activeMenuSheets[i].sheet
133            previous:hide()
[6417]134        end
135    end
[8079]136
[6662]137    menuSheet:show()
[7689]138    menuSheetsRoot:activate()
[6718]139
[8079]140    -- select first button if the menu was opened with the keyboard
141    if previous and previous.pressedEnter and menuSheet:hasSelection() == false then
142        menuSheet:setSelectionNear(1, 1)
143    end
144
[6662]145    return menuSheet
[5491]146end
147
[6722]148function hideMenuSheet(name)
[6662]149    local sheetTuple = activeMenuSheets[name]
150    if sheetTuple == nil then
151        return
[6417]152    end
[5491]153
[6662]154    -- Hide the sheet
[6718]155    sheetTuple.sheet:hide()
[6662]156
157    -- Show sheets that were hidden by the sheet to be removed
158    local i = activeMenuSheets.size
159    -- Only do something if all sheets on top of sheetTuple
[6718]160    -- have bHidePrevious == true and sheetTuple.bHidePrevious == true
[6662]161    while i > 0 do
[6718]162        if activeMenuSheets[i].bHidePrevious then
[6662]163            if activeMenuSheets[i] == sheetTuple then
164                i = i - 1
165                while i > 0 do
[6718]166                    activeMenuSheets[i].sheet:show()
167                    if activeMenuSheets[i].bHidePrevious then
[6662]168                        break
169                    end
170                    i = i - 1
[6417]171                end
172            end
[6662]173            break
[6417]174        end
[6662]175        i = i - 1
[6417]176    end
[6662]177
178    -- Remove sheet with its tuple from the table
[6737]179    menuSheetsRoot:removeChildWindow(sheetTuple.sheet.window)
[6671]180    table.remove(activeMenuSheets, table.findIndex(activeMenuSheets, sheetTuple))
[6662]181    activeMenuSheets[name] = nil
182    activeMenuSheets.size = activeMenuSheets.size - 1
[6718]183    activeMenuSheets.topSheetTuple = activeMenuSheets[activeMenuSheets.size]
[6662]184
[7689]185    -- If sheet is the MainMenu
186    if name == "MainMenu" then
187        mainMenuLoaded = false
188    end
189
[6662]190    -- Leave the input state
[7403]191    if not sheetTuple.bNoInput then
192        inputMgr:leaveState(sheetTuple.sheet.inputState)
193    end
[8079]194
[6718]195    -- CURSOR SHOWING
196    local i = activeMenuSheets.size
[8729]197    -- Find top most sheet that doesn't have tShowCusor == tribool(dontcare)
198    while i > 0 and activeMenuSheets[i].sheet.tShowCursor == tribool(dontcare) do
[6718]199        i = i - 1
200    end
[8729]201    if i > 0 and activeMenuSheets[i].sheet.tShowCursor == tribool(true) then
[6662]202        showCursor()
203    else
204        hideCursor()
205    end
206
[7403]207    -- Count the number of sheets that don't need input till the first that does.
[7689]208    local counter = noInputSheetIndex()
[7403]209    -- Resume control if the last (non-noInput) menu is hidden
210    if counter == 0 then
[6417]211        orxonox.HumanController:resumeControl()
212        hideCursor()
213    end
[7403]214
[8079]215    sheetTuple.sheet:quit()
[5491]216end
[6417]217
[6662]218-- Hides all menu GUI sheets
[6722]219function hideAllMenuSheets()
[6662]220    while activeMenuSheets.size ~= 0 do
[6722]221        hideMenuSheet(activeMenuSheets.topSheetTuple.sheet.name)
[6417]222    end
223end
224
225function keyESC()
[6662]226    -- HUGE, very HUGE hacks!
[8529]227
[8484]228    -- If the InGameConsole is active, ignore the ESC command.
229    if bInGameConsoleClosed == true then
230        bInGameConsoleClosed = false
[8529]231        if activeMenuSheets[1] and activeMenuSheets[1].sheet.name == "MainMenu" then
[8484]232            return
233        end
234    end
[7403]235
[8485]236    -- Count the number of sheets that don't need input till the first that does.
237    local counter = noInputSheetIndex()
238
[7403]239    -- If the first sheet that needs input is the MainMenu.
[7689]240    if noInputSheetCounter() == 1 and activeMenuSheets[counter].sheet.name == "MainMenu" then
[6417]241        orxonox.execute("exit")
[7403]242    -- If there is at least one sheet that needs input.
243    elseif counter > 0 then
244        orxonox.execute("hideGUI "..activeMenuSheets[counter].sheet.name)
[6417]245    else
[6722]246        showMenuSheet("InGameMenu")
[6417]247    end
248end
249
[7689]250function keyPressed(e)
251    local we = tolua.cast(e, "CEGUI::KeyEventArgs")
252    local sheet = activeMenuSheets[activeMenuSheets.size]
253    code = tostring(we.scancode)
254    -- Some preprocessing
255    if not mainMenuLoaded and not sheet.bNoInput then
256        if code == "1" then
257            keyESC()
258        elseif code == "0"then
[8079]259            orxonox.CommandExecutor:execute("InGameConsole openConsole")
[7689]260        end
261    end
[8079]262    sheet.sheet:keyPressed()
[7689]263end
264
[8079]265function windowResized(e)
266    for name, sheet in pairs(loadedSheets) do
[8729]267        if orxonox.GraphicsManager:getInstance():isFullScreen() or sheet.tShowCursor == tribool(false) then
268            inputMgr:setMouseExclusive(sheet.inputState, tribool(true))
[8079]269        else
[8729]270            inputMgr:setMouseExclusive(sheet.inputState, tribool(false))
[8079]271        end
272    end
273    local sheetTuple = activeMenuSheets[activeMenuSheets.size]
274    if sheetTuple then
[8729]275        if orxonox.GraphicsManager:getInstance():isFullScreen() and sheetTuple.sheet.tShowCursor ~= tribool(false) then
[8079]276            showCursor()
277        else
278            hideCursor()
279        end
280        sheetTuple.sheet:windowResized()
281    end
282end
283
[6737]284function setBackgroundImage(imageSet, imageName)
285    guiMgr:setBackgroundImage(imageSet, imageName)
[6417]286end
[6737]287
[7689]288function noInputSheetIndex()
289    -- Count the number of sheets that don't need input till the first that does.
290    local index = activeMenuSheets.size
291    while index > 0 and activeMenuSheets[index].bNoInput do
292        index = index - 1
293    end
294    return index
295end
296
[7403]297function noInputSheetCounter()
[7689]298    -- Count the number of sheets that do need input.
[7403]299    local counter = activeMenuSheets.size
[7689]300    for i = 1,activeMenuSheets.size do
301        if activeMenuSheets[i].bNoInput then
302            counter = counter - 1
303        end
[7403]304    end
305    return counter
306end
307
[8484]308function inGameConsoleClosed()
309    bInGameConsoleClosed = not bInGameConsoleClosed;
310end
311
[8079]312function getGUIFirstActive(name, bHidePrevious, bNoInput)
313    local sheet = activeMenuSheets.topSheetTuple
314    -- If the topmost gui sheet has the input name
315    if sheet ~= nil and sheet.name == name then
316        guiMgr:toggleGUIHelper(name, bHidePrevious, bNoInput, false);
317    else
318        guiMgr:toggleGUIHelper(name, bHidePrevious, bNoInput, true);
319    end
320end
321
[6737]322----------------------
323--- Initialisation ---
324----------------------
325
326hideCursor()
Note: See TracBrowser for help on using the repository browser.