Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ogre1.9/data/gui/scripts/SheetManager.lua @ 12068

Last change on this file since 12068 was 11211, checked in by bknecht, 8 years ago

Merging cegui0.8 branch into ogre1.9 branch

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