Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8032 was 8015, checked in by rgrieder, 14 years ago

Fixed bug occurred during bugfix (new bug is worse) by merging r8013 of the usability branch.

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