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