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