Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/menu/data/gui/scripts/SheetManager.lua @ 7621

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

Some improvement on keypressed behaviour. Lua is acting up a little, though, don't yet know why exactly…

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