Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/usability/data/gui/scripts/SheetManager.lua @ 8032

Last change on this file since 8032 was 8032, checked in by landauf, 15 years ago
  • removed console command shortcuts for InGameConsole openConsole and closeConsole - hope I adjusted all references to this command correctly
  • moved printFPS, printTickTime, and printRTT to "Stats" console command group (couldn't find any references to this, so hopefully nothing is broken)
  • Property svn:eol-style set to native
File size: 8.7 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()
[8013]7local bInGameConsoleClosed = false
[7689]8local mainMenuLoaded = false
9orxonox.GUIManager:subscribeEventHelper(menuSheetsRoot, "KeyDown", "keyPressed")
[8018]10orxonox.GUIManager:subscribeEventHelper(menuSheetsRoot, "Sized", "windowResized")
[5491]11
[8023]12------------------------
13--- Global functions ---
14------------------------
[6662]15
[8023]16function hideCursor()
[6662]17    if cursor:isVisible() then
18        cursor:hide()
19    end
20end
21
[8023]22function showCursor()
[6737]23    if not cursor:isVisible() and inputMgr:isMouseExclusive() then
[6662]24        cursor:show()
25    end
26end
27
[6737]28-- Loads the GUI with the specified name
29-- The name corresponds to the filename of the *.lua and *.layout files
30-- but without the extension
31function loadSheet(name)
32    -- Check if it has already been loaded
33    local sheet = loadedSheets[name]
34    if sheet == nil then
35        -- Load the sheet
36        sheet = require(name)
37        sheet:load()
38        loadedSheets[name] = sheet
39    end
40    return sheet
41end
42
[6662]43-- ?
[7403]44function showMenuSheet(name, bHidePrevious, bNoInput, ptr)
45    local sheet = showMenuSheet(name, bHidePrevious, bNoInput)
[6718]46    sheet.overlay = ptr
47    return sheet
[5491]48end
49
[6662]50-- Shows the specified menu sheet and loads it if neccessary
[7403]51function showMenuSheet(name, bHidePrevious, bNoInput)
[6737]52    if name == "" then
53        return nil
54    end
[6718]55    -- Get sheet (or load it)
56    local menuSheet = loadSheet(name)
[5491]57
[6718]58    -- Use sheet's value if nil was provided
59    if bHidePrevious == nil then
60        bHidePrevious = menuSheet.bHidePrevious
61        assert(bHidePrevious ~= nil)
62    end
63
[7403]64    -- Set bNoInput to false if it hasn't been set.
65    if bNoInput == nil then
66        bNoInput = false
67    end
68
69    -- Count the number of sheets that don't need input till the first that does.
[7689]70    local counter = noInputSheetIndex()
[6721]71    -- Pause game control if this is the first menu to be displayed
72    -- HUGE HACK?
[7403]73    if bNoInput == false and counter == 0 then
[6721]74        orxonox.HumanController:pauseControl()
75    end
76
[6662]77    -- Hide if already displayed (to make sure it is up front in the end)
78    if activeMenuSheets[name] ~= nil then
[6722]79        hideMenuSheet(name)
[6662]80    end
81
[7403]82    if bNoInput == true then
83        menuSheet.tShowCursor = TriBool.Dontcare
84    end
85
[6718]86    -- Add the sheet in a tuple of additional information
87    local sheetTuple =
88    {
89        ["sheet"]          = menuSheet,
[7403]90        ["bHidePrevious"]  = bHidePrevious,
91        ["bNoInput"]       = bNoInput
[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
[7925]119    local previous
[6662]120    if bHidePrevious then
121        for i = 1, activeMenuSheets.size - 1 do
[7925]122            previous = activeMenuSheets[i].sheet
123            previous:hide()
[6417]124        end
125    end
[7922]126
[6662]127    menuSheet:show()
[7689]128    menuSheetsRoot:activate()
[6718]129
[7925]130    -- select first button if the menu was opened with the keyboard
[7928]131    if previous and previous.pressedEnter and menuSheet:hasSelection() == false then
132        menuSheet:setSelectionNear(1, 1)
[7925]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
[7922]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
[7927]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
[7689]218    -- If the InGameConsole is active, ignore the ESC command.
[8013]219    if bInGameConsoleClosed == true then
220        bInGameConsoleClosed = false
[7689]221        return
222    end
223
[7403]224    -- Count the number of sheets that don't need input till the first that does.
[7689]225    local counter = noInputSheetIndex()
[7403]226
227    -- If the first sheet that needs input is the MainMenu.
[7689]228    if noInputSheetCounter() == 1 and activeMenuSheets[counter].sheet.name == "MainMenu" then
[6417]229        orxonox.execute("exit")
[7403]230    -- If there is at least one sheet that needs input.
231    elseif counter > 0 then
232        orxonox.execute("hideGUI "..activeMenuSheets[counter].sheet.name)
[6417]233    else
[6722]234        showMenuSheet("InGameMenu")
[6417]235    end
236end
237
[7689]238function keyPressed(e)
239    local we = tolua.cast(e, "CEGUI::KeyEventArgs")
240    local sheet = activeMenuSheets[activeMenuSheets.size]
241    code = tostring(we.scancode)
242    -- Some preprocessing
243    if not mainMenuLoaded and not sheet.bNoInput then
244        if code == "1" then
245            keyESC()
246        elseif code == "0"then
[8032]247            orxonox.CommandExecutor:execute("InGameConsole openConsole")
[7689]248        end
249    end
[7922]250    sheet.sheet:keyPressed()
[7689]251end
252
[8018]253function windowResized(e)
[8023]254    for name, sheet in pairs(loadedSheets) do
255        if orxonox.GraphicsManager:getInstance():isFullScreen() or sheet.tShowCursor == TriBool.False then
256            inputMgr:setMouseExclusive(sheet.inputState, TriBool.True)
257        else
258            inputMgr:setMouseExclusive(sheet.inputState, TriBool.False)
259        end
[8018]260    end
[8023]261    local sheetTuple = activeMenuSheets[activeMenuSheets.size]
262    if sheetTuple then
263        if orxonox.GraphicsManager:getInstance():isFullScreen() and sheetTuple.sheet.tShowCursor ~= TriBool.False then
264            showCursor()
265        else
266            hideCursor()
267        end
268        sheetTuple.sheet:windowResized()
269    end
[8018]270end
271
[6737]272function setBackgroundImage(imageSet, imageName)
273    guiMgr:setBackgroundImage(imageSet, imageName)
[6417]274end
[6737]275
[7689]276function noInputSheetIndex()
277    -- Count the number of sheets that don't need input till the first that does.
278    local index = activeMenuSheets.size
279    while index > 0 and activeMenuSheets[index].bNoInput do
280        index = index - 1
281    end
282    return index
283end
284
[7403]285function noInputSheetCounter()
[7689]286    -- Count the number of sheets that do need input.
[7403]287    local counter = activeMenuSheets.size
[7689]288    for i = 1,activeMenuSheets.size do
289        if activeMenuSheets[i].bNoInput then
290            counter = counter - 1
291        end
[7403]292    end
293    return counter
294end
295
[7689]296function inGameConsoleClosed()
[8013]297    bInGameConsoleClosed = not bInGameConsoleClosed;
[7689]298end
299
[6737]300----------------------
301--- Initialisation ---
302----------------------
303
304hideCursor()
Note: See TracBrowser for help on using the repository browser.