Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Fixing minor bug. When the InGameConsole was closed you would need to push ESC two times to get into the InGameMenu.

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