Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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