Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/data/gui/scripts/SheetManager.lua @ 12036

Last change on this file since 12036 was 11052, checked in by landauf, 9 years ago

merged branch presentationHS15 back to trunk

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