[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. |
---|
| 71 | local counter = activeMenuSheets.size |
---|
| 72 | while counter > 0 and activeMenuSheets[counter].bNoInput do |
---|
| 73 | counter = counter - 1 |
---|
| 74 | end |
---|
[6721] | 75 | -- Pause game control if this is the first menu to be displayed |
---|
| 76 | -- HUGE HACK? |
---|
[7340] | 77 | if bNoInput == false and counter == 0 then |
---|
[6721] | 78 | orxonox.HumanController:pauseControl() |
---|
| 79 | end |
---|
| 80 | |
---|
[6662] | 81 | -- Hide if already displayed (to make sure it is up front in the end) |
---|
| 82 | if activeMenuSheets[name] ~= nil then |
---|
[6722] | 83 | hideMenuSheet(name) |
---|
[6662] | 84 | end |
---|
| 85 | |
---|
[7340] | 86 | if bNoInput == true then |
---|
[7339] | 87 | menuSheet.tShowCursor = TriBool.Dontcare |
---|
| 88 | end |
---|
[7338] | 89 | |
---|
[6718] | 90 | -- Add the sheet in a tuple of additional information |
---|
| 91 | local sheetTuple = |
---|
| 92 | { |
---|
| 93 | ["sheet"] = menuSheet, |
---|
[7338] | 94 | ["bHidePrevious"] = bHidePrevious, |
---|
| 95 | ["bNoInput"] = bNoInput |
---|
[6718] | 96 | } |
---|
| 97 | table.insert(activeMenuSheets, sheetTuple) -- indexed array access |
---|
| 98 | activeMenuSheets[name] = sheetTuple -- name access |
---|
| 99 | activeMenuSheets.size = activeMenuSheets.size + 1 |
---|
| 100 | activeMenuSheets.topSheetTuple = sheetTuple |
---|
| 101 | |
---|
[6662] | 102 | -- Add sheet to the root window |
---|
[6737] | 103 | menuSheetsRoot:addChildWindow(menuSheet.window) |
---|
[6662] | 104 | |
---|
| 105 | -- Handle input distribution |
---|
[7338] | 106 | if bNoInput == false then |
---|
| 107 | inputMgr:enterState(menuSheet.inputState) |
---|
| 108 | end |
---|
[6662] | 109 | |
---|
[6718] | 110 | -- Only change cursor situation if menuSheet.tShowCursor ~= TriBool.Dontcare |
---|
| 111 | if menuSheet.tShowCursor == TriBool.True then |
---|
[6417] | 112 | showCursor() |
---|
[6718] | 113 | elseif menuSheet.tShowCursor == TriBool.False then |
---|
[6417] | 114 | hideCursor() |
---|
[5491] | 115 | end |
---|
[6417] | 116 | |
---|
[6662] | 117 | -- Hide all previous sheets if necessary |
---|
| 118 | if bHidePrevious then |
---|
| 119 | for i = 1, activeMenuSheets.size - 1 do |
---|
[6718] | 120 | activeMenuSheets[i].sheet:hide() |
---|
[6417] | 121 | end |
---|
| 122 | end |
---|
[5491] | 123 | |
---|
[6662] | 124 | menuSheet:show() |
---|
[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 |
---|
[7338] | 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 | |
---|
[7340] | 183 | -- Count the number of sheets that don't need input till the first that does. |
---|
| 184 | local counter = activeMenuSheets.size |
---|
| 185 | while counter > 0 and activeMenuSheets[counter].bNoInput do |
---|
| 186 | counter = counter - 1 |
---|
| 187 | end |
---|
| 188 | -- Resume control if the last (non-noInput) menu is hidden |
---|
| 189 | if counter == 0 then |
---|
[6417] | 190 | orxonox.HumanController:resumeControl() |
---|
| 191 | hideCursor() |
---|
| 192 | end |
---|
[5491] | 193 | end |
---|
[6417] | 194 | |
---|
[6662] | 195 | -- Hides all menu GUI sheets |
---|
[6722] | 196 | function hideAllMenuSheets() |
---|
[6662] | 197 | while activeMenuSheets.size ~= 0 do |
---|
[6722] | 198 | hideMenuSheet(activeMenuSheets.topSheetTuple.sheet.name) |
---|
[6417] | 199 | end |
---|
| 200 | end |
---|
| 201 | |
---|
| 202 | function keyESC() |
---|
[6662] | 203 | -- HUGE, very HUGE hacks! |
---|
[7338] | 204 | |
---|
| 205 | -- Count the number of sheets that don't need input till the first that does. |
---|
| 206 | local counter = activeMenuSheets.size |
---|
| 207 | while counter > 0 and activeMenuSheets[counter].bNoInput do |
---|
| 208 | counter = counter - 1 |
---|
| 209 | end |
---|
| 210 | |
---|
| 211 | -- If the first sheet that needs input is the MainMenu. |
---|
[7341] | 212 | if counter == 1 and activeMenuSheets[1].sheet.name == "MainMenu" then |
---|
[6417] | 213 | orxonox.execute("exit") |
---|
[7338] | 214 | -- If there is at least one sheet that needs input. |
---|
| 215 | elseif counter > 0 then |
---|
| 216 | orxonox.execute("hideGUI "..activeMenuSheets[counter].sheet.name) |
---|
[6417] | 217 | else |
---|
[6722] | 218 | showMenuSheet("InGameMenu") |
---|
[6417] | 219 | end |
---|
| 220 | end |
---|
| 221 | |
---|
[6737] | 222 | function setBackgroundImage(imageSet, imageName) |
---|
| 223 | guiMgr:setBackgroundImage(imageSet, imageName) |
---|
[6417] | 224 | end |
---|
[6737] | 225 | |
---|
| 226 | ---------------------- |
---|
| 227 | --- Initialisation --- |
---|
| 228 | ---------------------- |
---|
| 229 | |
---|
| 230 | hideCursor() |
---|