Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/notifications/data/gui/scripts/SheetManager.lua @ 7341

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

Fixed Esc Problem in MainMenu.

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