| [6737] | 1 | -- SheetManager.lua | 
|---|
| [6595] | 2 |  | 
|---|
| [6737] | 3 | local cursor = CEGUI.MouseCursor:getSingleton() | 
|---|
| [6662] | 4 | local loadedSheets = {} | 
|---|
| [6718] | 5 | local activeMenuSheets = {size = 0, topSheetTuple = nil} | 
|---|
| [6737] | 6 | local menuSheetsRoot = guiMgr:getMenuRootWindow() | 
|---|
| [7649] | 7 | local bInGameConsoleClosed = false | 
|---|
| [7607] | 8 | orxonox.GUIManager:subscribeEventHelper(menuSheetsRoot, "KeyDown", "keyPressed") | 
|---|
| [5491] | 9 |  | 
|---|
| [6662] | 10 | ----------------------- | 
|---|
 | 11 | --- Local functions --- | 
|---|
 | 12 | ----------------------- | 
|---|
 | 13 |  | 
|---|
 | 14 | local function hideCursor() | 
|---|
 | 15 |     if cursor:isVisible() then | 
|---|
 | 16 |         cursor:hide() | 
|---|
 | 17 |     end | 
|---|
 | 18 | end | 
|---|
 | 19 |  | 
|---|
 | 20 | local function showCursor() | 
|---|
| [6737] | 21 |     if not cursor:isVisible() and inputMgr:isMouseExclusive() then | 
|---|
| [6662] | 22 |         cursor:show() | 
|---|
 | 23 |     end | 
|---|
 | 24 | end | 
|---|
 | 25 |  | 
|---|
| [5491] | 26 |  | 
|---|
| [6662] | 27 | ------------------------ | 
|---|
 | 28 | --- Global functions --- | 
|---|
 | 29 | ------------------------ | 
|---|
 | 30 |  | 
|---|
| [6737] | 31 | -- Loads the GUI with the specified name | 
|---|
 | 32 | -- The name corresponds to the filename of the *.lua and *.layout files | 
|---|
 | 33 | -- but without the extension | 
|---|
 | 34 | function loadSheet(name) | 
|---|
 | 35 |     -- Check if it has already been loaded | 
|---|
 | 36 |     local sheet = loadedSheets[name] | 
|---|
 | 37 |     if sheet == nil then | 
|---|
 | 38 |         -- Load the sheet | 
|---|
 | 39 |         sheet = require(name) | 
|---|
 | 40 |         sheet:load() | 
|---|
 | 41 |         loadedSheets[name] = sheet | 
|---|
 | 42 |     end | 
|---|
 | 43 |     return sheet | 
|---|
 | 44 | end | 
|---|
 | 45 |  | 
|---|
| [6662] | 46 | -- ? | 
|---|
| [7403] | 47 | function showMenuSheet(name, bHidePrevious, bNoInput, ptr) | 
|---|
 | 48 |     local sheet = showMenuSheet(name, bHidePrevious, bNoInput) | 
|---|
| [6718] | 49 |     sheet.overlay = ptr | 
|---|
 | 50 |     return sheet | 
|---|
| [5491] | 51 | end | 
|---|
 | 52 |  | 
|---|
| [6662] | 53 | -- Shows the specified menu sheet and loads it if neccessary | 
|---|
| [7403] | 54 | function showMenuSheet(name, bHidePrevious, bNoInput) | 
|---|
| [6737] | 55 |     if name == "" then | 
|---|
 | 56 |         return nil | 
|---|
 | 57 |     end | 
|---|
| [6718] | 58 |     -- Get sheet (or load it) | 
|---|
 | 59 |     local menuSheet = loadSheet(name) | 
|---|
| [5491] | 60 |  | 
|---|
| [6718] | 61 |     -- Use sheet's value if nil was provided | 
|---|
 | 62 |     if bHidePrevious == nil then | 
|---|
 | 63 |         bHidePrevious = menuSheet.bHidePrevious | 
|---|
 | 64 |         assert(bHidePrevious ~= nil) | 
|---|
 | 65 |     end | 
|---|
 | 66 |  | 
|---|
| [7403] | 67 |     -- Set bNoInput to false if it hasn't been set. | 
|---|
 | 68 |     if bNoInput == nil then | 
|---|
 | 69 |         bNoInput = false | 
|---|
 | 70 |     end | 
|---|
 | 71 |  | 
|---|
 | 72 |     -- Count the number of sheets that don't need input till the first that does. | 
|---|
 | 73 |     local counter = noInputSheetCounter() | 
|---|
| [6721] | 74 |     -- Pause game control if this is the first menu to be displayed | 
|---|
 | 75 |     -- HUGE HACK? | 
|---|
| [7403] | 76 |     if bNoInput == false and counter == 0 then | 
|---|
| [6721] | 77 |         orxonox.HumanController:pauseControl() | 
|---|
 | 78 |     end | 
|---|
 | 79 |  | 
|---|
| [6662] | 80 |     -- Hide if already displayed (to make sure it is up front in the end) | 
|---|
 | 81 |     if activeMenuSheets[name] ~= nil then | 
|---|
| [6722] | 82 |         hideMenuSheet(name) | 
|---|
| [6662] | 83 |     end | 
|---|
 | 84 |  | 
|---|
| [7403] | 85 |     if bNoInput == true then | 
|---|
 | 86 |         menuSheet.tShowCursor = TriBool.Dontcare | 
|---|
 | 87 |     end | 
|---|
 | 88 |  | 
|---|
| [6718] | 89 |     -- Add the sheet in a tuple of additional information | 
|---|
 | 90 |     local sheetTuple = | 
|---|
 | 91 |     { | 
|---|
 | 92 |         ["sheet"]          = menuSheet, | 
|---|
| [7403] | 93 |         ["bHidePrevious"]  = bHidePrevious, | 
|---|
 | 94 |         ["bNoInput"]       = bNoInput | 
|---|
| [6718] | 95 |     } | 
|---|
 | 96 |     table.insert(activeMenuSheets, sheetTuple) -- indexed array access | 
|---|
 | 97 |     activeMenuSheets[name] = sheetTuple -- name access | 
|---|
 | 98 |     activeMenuSheets.size = activeMenuSheets.size + 1 | 
|---|
 | 99 |     activeMenuSheets.topSheetTuple = sheetTuple | 
|---|
 | 100 |  | 
|---|
| [6662] | 101 |     -- Add sheet to the root window | 
|---|
| [6737] | 102 |     menuSheetsRoot:addChildWindow(menuSheet.window) | 
|---|
| [6662] | 103 |  | 
|---|
 | 104 |     -- Handle input distribution | 
|---|
| [7403] | 105 |     if bNoInput == false then | 
|---|
 | 106 |         inputMgr:enterState(menuSheet.inputState) | 
|---|
 | 107 |     end | 
|---|
| [6662] | 108 |  | 
|---|
| [6718] | 109 |     -- Only change cursor situation if menuSheet.tShowCursor ~= TriBool.Dontcare | 
|---|
 | 110 |     if menuSheet.tShowCursor == TriBool.True then | 
|---|
| [6417] | 111 |         showCursor() | 
|---|
| [6718] | 112 |     elseif menuSheet.tShowCursor == TriBool.False then | 
|---|
| [6417] | 113 |         hideCursor() | 
|---|
| [5491] | 114 |     end | 
|---|
| [6417] | 115 |  | 
|---|
| [6662] | 116 |     -- Hide all previous sheets if necessary | 
|---|
 | 117 |     if bHidePrevious then | 
|---|
 | 118 |         for i = 1, activeMenuSheets.size - 1 do | 
|---|
| [6718] | 119 |             activeMenuSheets[i].sheet:hide() | 
|---|
| [6417] | 120 |         end | 
|---|
 | 121 |     end | 
|---|
| [5491] | 122 |  | 
|---|
| [6662] | 123 |     menuSheet:show() | 
|---|
| [7607] | 124 |     menuSheetsRoot:activate() | 
|---|
| [6718] | 125 |  | 
|---|
| [6662] | 126 |     return menuSheet | 
|---|
| [5491] | 127 | end | 
|---|
 | 128 |  | 
|---|
| [6722] | 129 | function hideMenuSheet(name) | 
|---|
| [6662] | 130 |     local sheetTuple = activeMenuSheets[name] | 
|---|
 | 131 |     if sheetTuple == nil then | 
|---|
 | 132 |         return | 
|---|
| [6417] | 133 |     end | 
|---|
| [5491] | 134 |  | 
|---|
| [6662] | 135 |     -- Hide the sheet | 
|---|
| [6718] | 136 |     sheetTuple.sheet:hide() | 
|---|
| [6662] | 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 | 
|---|
| [6718] | 141 |     -- have bHidePrevious == true and sheetTuple.bHidePrevious == true | 
|---|
| [6662] | 142 |     while i > 0 do | 
|---|
| [6718] | 143 |         if activeMenuSheets[i].bHidePrevious then | 
|---|
| [6662] | 144 |             if activeMenuSheets[i] == sheetTuple then | 
|---|
 | 145 |                 i = i - 1 | 
|---|
 | 146 |                 while i > 0 do | 
|---|
| [6718] | 147 |                     activeMenuSheets[i].sheet:show() | 
|---|
 | 148 |                     if activeMenuSheets[i].bHidePrevious then | 
|---|
| [6662] | 149 |                         break | 
|---|
 | 150 |                     end | 
|---|
 | 151 |                     i = i - 1 | 
|---|
| [6417] | 152 |                 end | 
|---|
 | 153 |             end | 
|---|
| [6662] | 154 |             break | 
|---|
| [6417] | 155 |         end | 
|---|
| [6662] | 156 |         i = i - 1 | 
|---|
| [6417] | 157 |     end | 
|---|
| [6662] | 158 |  | 
|---|
 | 159 |     -- Remove sheet with its tuple from the table | 
|---|
| [6737] | 160 |     menuSheetsRoot:removeChildWindow(sheetTuple.sheet.window) | 
|---|
| [6671] | 161 |     table.remove(activeMenuSheets, table.findIndex(activeMenuSheets, sheetTuple)) | 
|---|
| [6662] | 162 |     activeMenuSheets[name] = nil | 
|---|
 | 163 |     activeMenuSheets.size = activeMenuSheets.size - 1 | 
|---|
| [6718] | 164 |     activeMenuSheets.topSheetTuple = activeMenuSheets[activeMenuSheets.size] | 
|---|
| [6662] | 165 |  | 
|---|
 | 166 |     -- Leave the input state | 
|---|
| [7403] | 167 |     if not sheetTuple.bNoInput then | 
|---|
 | 168 |         inputMgr:leaveState(sheetTuple.sheet.inputState) | 
|---|
 | 169 |     end | 
|---|
| [6662] | 170 |      | 
|---|
| [6718] | 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 | 
|---|
| [6662] | 178 |         showCursor() | 
|---|
 | 179 |     else | 
|---|
 | 180 |         hideCursor() | 
|---|
 | 181 |     end | 
|---|
 | 182 |  | 
|---|
| [7403] | 183 |     -- Count the number of sheets that don't need input till the first that does. | 
|---|
 | 184 |     local counter = noInputSheetCounter() | 
|---|
 | 185 |     -- Resume control if the last (non-noInput) menu is hidden | 
|---|
 | 186 |     if counter == 0 then | 
|---|
| [6417] | 187 |         orxonox.HumanController:resumeControl() | 
|---|
 | 188 |         hideCursor() | 
|---|
 | 189 |     end | 
|---|
| [7403] | 190 |  | 
|---|
 | 191 |     sheetTuple.sheet:afterHide() | 
|---|
| [5491] | 192 | end | 
|---|
| [6417] | 193 |  | 
|---|
| [6662] | 194 | -- Hides all menu GUI sheets | 
|---|
| [6722] | 195 | function hideAllMenuSheets() | 
|---|
| [6662] | 196 |     while activeMenuSheets.size ~= 0 do | 
|---|
| [6722] | 197 |         hideMenuSheet(activeMenuSheets.topSheetTuple.sheet.name) | 
|---|
| [6417] | 198 |     end | 
|---|
 | 199 | end | 
|---|
 | 200 |  | 
|---|
 | 201 | function keyESC() | 
|---|
| [6662] | 202 |     -- HUGE, very HUGE hacks! | 
|---|
| [7403] | 203 |  | 
|---|
| [7649] | 204 |     -- If the InGameConsole is active, ignore the ESC command. | 
|---|
 | 205 |     if bInGameConsoleClosed == true then | 
|---|
 | 206 |         bInGameConsoleClosed = falses | 
|---|
 | 207 |         return | 
|---|
 | 208 |     end | 
|---|
 | 209 |  | 
|---|
| [7403] | 210 |     -- Count the number of sheets that don't need input till the first that does. | 
|---|
 | 211 |     local counter = noInputSheetCounter() | 
|---|
 | 212 |  | 
|---|
 | 213 |     -- If the first sheet that needs input is the MainMenu. | 
|---|
 | 214 |     if counter == 1 and activeMenuSheets[1].sheet.name == "MainMenu" then | 
|---|
| [6417] | 215 |         orxonox.execute("exit") | 
|---|
| [7403] | 216 |     -- If there is at least one sheet that needs input. | 
|---|
 | 217 |     elseif counter > 0 then | 
|---|
 | 218 |         orxonox.execute("hideGUI "..activeMenuSheets[counter].sheet.name) | 
|---|
| [6417] | 219 |     else | 
|---|
| [6722] | 220 |         showMenuSheet("InGameMenu") | 
|---|
| [6417] | 221 |     end | 
|---|
 | 222 | end | 
|---|
 | 223 |  | 
|---|
| [7607] | 224 | function keyPressed(e) | 
|---|
 | 225 |     local we = tolua.cast(e, "CEGUI::KeyEventArgs") | 
|---|
 | 226 |     local sheet = activeMenuSheets[activeMenuSheets.size] | 
|---|
 | 227 |     code = tostring(we.scancode) | 
|---|
 | 228 |     sheet.sheet:onKeyPressed() | 
|---|
 | 229 | end | 
|---|
 | 230 |  | 
|---|
| [6737] | 231 | function setBackgroundImage(imageSet, imageName) | 
|---|
 | 232 |     guiMgr:setBackgroundImage(imageSet, imageName) | 
|---|
| [6417] | 233 | end | 
|---|
| [6737] | 234 |  | 
|---|
| [7403] | 235 | function noInputSheetCounter() | 
|---|
 | 236 |     -- Count the number of sheets that don't need input till the first that does. | 
|---|
 | 237 |     local counter = activeMenuSheets.size | 
|---|
 | 238 |     while counter > 0 and activeMenuSheets[counter].bNoInput do | 
|---|
 | 239 |         counter = counter - 1 | 
|---|
 | 240 |     end | 
|---|
 | 241 |     return counter | 
|---|
 | 242 | end | 
|---|
 | 243 |  | 
|---|
| [7649] | 244 | function inGameConsoleClosed() | 
|---|
 | 245 |     bInGameConsoleClosed = not bInGameConsoleClosed; | 
|---|
 | 246 | end | 
|---|
 | 247 |  | 
|---|
| [6737] | 248 | ---------------------- | 
|---|
 | 249 | --- Initialisation --- | 
|---|
 | 250 | ---------------------- | 
|---|
 | 251 |  | 
|---|
 | 252 | hideCursor() | 
|---|