Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8018 was 8018, checked in by landauf, 13 years ago

added new graphics menu

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