| 1 | -- SheetManager.lua | 
|---|
| 2 |  | 
|---|
| 3 | local cursor = CEGUI.MouseCursor:getSingleton() | 
|---|
| 4 | local loadedSheets = {} | 
|---|
| 5 | local activeMenuSheets = {size = 0, topSheetTuple = nil} | 
|---|
| 6 | local menuSheetsRoot = guiMgr:getMenuRootWindow() | 
|---|
| 7 | local bInGameConsoleClosed = false | 
|---|
| 8 | orxonox.GUIManager:subscribeEventHelper(menuSheetsRoot, "KeyDown", "keyPressed") | 
|---|
| 9 |  | 
|---|
| 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() | 
|---|
| 21 |     if not cursor:isVisible() and inputMgr:isMouseExclusive() then | 
|---|
| 22 |         cursor:show() | 
|---|
| 23 |     end | 
|---|
| 24 | end | 
|---|
| 25 |  | 
|---|
| 26 |  | 
|---|
| 27 | ------------------------ | 
|---|
| 28 | --- Global functions --- | 
|---|
| 29 | ------------------------ | 
|---|
| 30 |  | 
|---|
| 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 |  | 
|---|
| 46 | -- ? | 
|---|
| 47 | function showMenuSheet(name, bHidePrevious, bNoInput, ptr) | 
|---|
| 48 |     local sheet = showMenuSheet(name, bHidePrevious, bNoInput) | 
|---|
| 49 |     sheet.overlay = ptr | 
|---|
| 50 |     return sheet | 
|---|
| 51 | end | 
|---|
| 52 |  | 
|---|
| 53 | -- Shows the specified menu sheet and loads it if neccessary | 
|---|
| 54 | function showMenuSheet(name, bHidePrevious, bNoInput) | 
|---|
| 55 |     if name == "" then | 
|---|
| 56 |         return nil | 
|---|
| 57 |     end | 
|---|
| 58 |     -- Get sheet (or load it) | 
|---|
| 59 |     local menuSheet = loadSheet(name) | 
|---|
| 60 |  | 
|---|
| 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 |  | 
|---|
| 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() | 
|---|
| 74 |     -- Pause game control if this is the first menu to be displayed | 
|---|
| 75 |     -- HUGE HACK? | 
|---|
| 76 |     if bNoInput == false and counter == 0 then | 
|---|
| 77 |         orxonox.HumanController:pauseControl() | 
|---|
| 78 |     end | 
|---|
| 79 |  | 
|---|
| 80 |     -- Hide if already displayed (to make sure it is up front in the end) | 
|---|
| 81 |     if activeMenuSheets[name] ~= nil then | 
|---|
| 82 |         hideMenuSheet(name) | 
|---|
| 83 |     end | 
|---|
| 84 |  | 
|---|
| 85 |     if bNoInput == true then | 
|---|
| 86 |         menuSheet.tShowCursor = TriBool.Dontcare | 
|---|
| 87 |     end | 
|---|
| 88 |  | 
|---|
| 89 |     -- Add the sheet in a tuple of additional information | 
|---|
| 90 |     local sheetTuple = | 
|---|
| 91 |     { | 
|---|
| 92 |         ["sheet"]          = menuSheet, | 
|---|
| 93 |         ["bHidePrevious"]  = bHidePrevious, | 
|---|
| 94 |         ["bNoInput"]       = bNoInput | 
|---|
| 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 |  | 
|---|
| 101 |     -- Add sheet to the root window | 
|---|
| 102 |     menuSheetsRoot:addChildWindow(menuSheet.window) | 
|---|
| 103 |  | 
|---|
| 104 |     -- Handle input distribution | 
|---|
| 105 |     if bNoInput == false then | 
|---|
| 106 |         inputMgr:enterState(menuSheet.inputState) | 
|---|
| 107 |     end | 
|---|
| 108 |  | 
|---|
| 109 |     -- Only change cursor situation if menuSheet.tShowCursor ~= TriBool.Dontcare | 
|---|
| 110 |     if menuSheet.tShowCursor == TriBool.True then | 
|---|
| 111 |         showCursor() | 
|---|
| 112 |     elseif menuSheet.tShowCursor == TriBool.False then | 
|---|
| 113 |         hideCursor() | 
|---|
| 114 |     end | 
|---|
| 115 |  | 
|---|
| 116 |     -- Hide all previous sheets if necessary | 
|---|
| 117 |     if bHidePrevious then | 
|---|
| 118 |         for i = 1, activeMenuSheets.size - 1 do | 
|---|
| 119 |             activeMenuSheets[i].sheet:hide() | 
|---|
| 120 |         end | 
|---|
| 121 |     end | 
|---|
| 122 |  | 
|---|
| 123 |     menuSheet:show() | 
|---|
| 124 |     menuSheetsRoot:activate() | 
|---|
| 125 |  | 
|---|
| 126 |     return menuSheet | 
|---|
| 127 | end | 
|---|
| 128 |  | 
|---|
| 129 | function hideMenuSheet(name) | 
|---|
| 130 |     local sheetTuple = activeMenuSheets[name] | 
|---|
| 131 |     if sheetTuple == nil then | 
|---|
| 132 |         return | 
|---|
| 133 |     end | 
|---|
| 134 |  | 
|---|
| 135 |     -- Hide the sheet | 
|---|
| 136 |     sheetTuple.sheet:hide() | 
|---|
| 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 | 
|---|
| 141 |     -- have bHidePrevious == true and sheetTuple.bHidePrevious == true | 
|---|
| 142 |     while i > 0 do | 
|---|
| 143 |         if activeMenuSheets[i].bHidePrevious then | 
|---|
| 144 |             if activeMenuSheets[i] == sheetTuple then | 
|---|
| 145 |                 i = i - 1 | 
|---|
| 146 |                 while i > 0 do | 
|---|
| 147 |                     activeMenuSheets[i].sheet:show() | 
|---|
| 148 |                     if activeMenuSheets[i].bHidePrevious then | 
|---|
| 149 |                         break | 
|---|
| 150 |                     end | 
|---|
| 151 |                     i = i - 1 | 
|---|
| 152 |                 end | 
|---|
| 153 |             end | 
|---|
| 154 |             break | 
|---|
| 155 |         end | 
|---|
| 156 |         i = i - 1 | 
|---|
| 157 |     end | 
|---|
| 158 |  | 
|---|
| 159 |     -- Remove sheet with its tuple from the table | 
|---|
| 160 |     menuSheetsRoot:removeChildWindow(sheetTuple.sheet.window) | 
|---|
| 161 |     table.remove(activeMenuSheets, table.findIndex(activeMenuSheets, sheetTuple)) | 
|---|
| 162 |     activeMenuSheets[name] = nil | 
|---|
| 163 |     activeMenuSheets.size = activeMenuSheets.size - 1 | 
|---|
| 164 |     activeMenuSheets.topSheetTuple = activeMenuSheets[activeMenuSheets.size] | 
|---|
| 165 |  | 
|---|
| 166 |     -- Leave the input state | 
|---|
| 167 |     if not sheetTuple.bNoInput then | 
|---|
| 168 |         inputMgr:leaveState(sheetTuple.sheet.inputState) | 
|---|
| 169 |     end | 
|---|
| 170 |      | 
|---|
| 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 | 
|---|
| 178 |         showCursor() | 
|---|
| 179 |     else | 
|---|
| 180 |         hideCursor() | 
|---|
| 181 |     end | 
|---|
| 182 |  | 
|---|
| 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 | 
|---|
| 187 |         orxonox.HumanController:resumeControl() | 
|---|
| 188 |         hideCursor() | 
|---|
| 189 |     end | 
|---|
| 190 |  | 
|---|
| 191 |     sheetTuple.sheet:afterHide() | 
|---|
| 192 | end | 
|---|
| 193 |  | 
|---|
| 194 | -- Hides all menu GUI sheets | 
|---|
| 195 | function hideAllMenuSheets() | 
|---|
| 196 |     while activeMenuSheets.size ~= 0 do | 
|---|
| 197 |         hideMenuSheet(activeMenuSheets.topSheetTuple.sheet.name) | 
|---|
| 198 |     end | 
|---|
| 199 | end | 
|---|
| 200 |  | 
|---|
| 201 | function keyESC() | 
|---|
| 202 |     -- HUGE, very HUGE hacks! | 
|---|
| 203 |  | 
|---|
| 204 |     -- If the InGameConsole is active, ignore the ESC command. | 
|---|
| 205 |     if bInGameConsoleClosed == true then | 
|---|
| 206 |         bInGameConsoleClosed = falses | 
|---|
| 207 |         return | 
|---|
| 208 |     end | 
|---|
| 209 |  | 
|---|
| 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 | 
|---|
| 215 |         orxonox.execute("exit") | 
|---|
| 216 |     -- If there is at least one sheet that needs input. | 
|---|
| 217 |     elseif counter > 0 then | 
|---|
| 218 |         orxonox.execute("hideGUI "..activeMenuSheets[counter].sheet.name) | 
|---|
| 219 |     else | 
|---|
| 220 |         showMenuSheet("InGameMenu") | 
|---|
| 221 |     end | 
|---|
| 222 | end | 
|---|
| 223 |  | 
|---|
| 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 |  | 
|---|
| 231 | function setBackgroundImage(imageSet, imageName) | 
|---|
| 232 |     guiMgr:setBackgroundImage(imageSet, imageName) | 
|---|
| 233 | end | 
|---|
| 234 |  | 
|---|
| 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 |  | 
|---|
| 244 | function inGameConsoleClosed() | 
|---|
| 245 |     bInGameConsoleClosed = not bInGameConsoleClosed; | 
|---|
| 246 | end | 
|---|
| 247 |  | 
|---|
| 248 | ---------------------- | 
|---|
| 249 | --- Initialisation --- | 
|---|
| 250 | ---------------------- | 
|---|
| 251 |  | 
|---|
| 252 | hideCursor() | 
|---|