Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Merging latest changes in usability branch into tutorial branch.

  • 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
[8051]8--orxonox.GUIManager:subscribeEventHelper(menuSheetsRoot, "KeyDown", "keyPressed")
9orxonox.GUIManager:subscribeEventHelper(menuSheetsRoot, "Sized", "windowResized")
[5491]10
[8051]11------------------------
12--- Global functions ---
13------------------------
[6662]14
[8051]15function hideCursor()
[6662]16    if cursor:isVisible() then
17        cursor:hide()
18    end
19end
20
[8051]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,
[8051]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
[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
[8051]135    if activeMenuSheets.size > 0 then
136        guiMgr:guisActiveChanged(true)
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
[8051]209    if activeMenuSheets.size == 0 then
210        guiMgr:guisActiveChanged(false)
211    end
212
[7927]213    sheetTuple.sheet:quit()
[5491]214end
[6417]215
[6662]216-- Hides all menu GUI sheets
[6722]217function hideAllMenuSheets()
[6662]218    while activeMenuSheets.size ~= 0 do
[6722]219        hideMenuSheet(activeMenuSheets.topSheetTuple.sheet.name)
[6417]220    end
221end
222
223function keyESC()
[6662]224    -- HUGE, very HUGE hacks!
[7403]225
[8051]226    -- Count the number of sheets that don't need input until the first that does.
[7689]227    local counter = noInputSheetIndex()
[7403]228
229    -- If the first sheet that needs input is the MainMenu.
[7689]230    if noInputSheetCounter() == 1 and activeMenuSheets[counter].sheet.name == "MainMenu" then
[6417]231        orxonox.execute("exit")
[7403]232    -- If there is at least one sheet that needs input.
233    elseif counter > 0 then
234        orxonox.execute("hideGUI "..activeMenuSheets[counter].sheet.name)
[6417]235    else
[6722]236        showMenuSheet("InGameMenu")
[6417]237    end
238end
239
[8051]240-- Function to navigate the GUI, is called by the GUIManager, whenever a relevant key is pressed.
241-- The mode specifies the action to be taken.
242function navigateGUI(mode)
[7689]243    local sheet = activeMenuSheets[activeMenuSheets.size]
[8051]244    sheet.sheet:keyPressed(mode)
245end
246
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)
[7689]253        end
254    end
[8051]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
[7689]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
[8051]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
[7689]298end
299
[6737]300----------------------
301--- Initialisation ---
302----------------------
303
304hideCursor()
Note: See TracBrowser for help on using the repository browser.