Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8021 was 8018, checked in by landauf, 14 years ago

added new graphics menu

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