| 1 | winMgr   = CEGUI.WindowManager:getSingleton() | 
|---|
| 2 | guiMgr   = orxonox.GUIManager:getInstance() | 
|---|
| 3 | inputMgr = orxonox.InputManager:getInstance() | 
|---|
| 4 |  | 
|---|
| 5 | local schemeMgr = CEGUI.SchemeManager:getSingleton() | 
|---|
| 6 | local system    = CEGUI.System:getSingleton() | 
|---|
| 7 | local cursor    = CEGUI.MouseCursor:getSingleton() | 
|---|
| 8 |  | 
|---|
| 9 | -- Load all required skins | 
|---|
| 10 | schemeMgr:loadScheme("TaharezGreenLook.scheme") | 
|---|
| 11 | --schemeMgr:loadScheme("TaharezLook.scheme") | 
|---|
| 12 | --schemeMgr:loadScheme("WindowsLook.scheme") | 
|---|
| 13 | --schemeMgr:loadScheme("VanillaLook.scheme") | 
|---|
| 14 | --schemeMgr:loadScheme("SleekSpaceLook.scheme") | 
|---|
| 15 |  | 
|---|
| 16 | -- Connect skin specific window types with our own window types | 
|---|
| 17 | -- By loading a different file (if there is) you can change the skin | 
|---|
| 18 | -- of the menus or the HUD independently | 
|---|
| 19 | schemeMgr:loadScheme("TaharezGreenMenuWidgets.scheme") | 
|---|
| 20 | menuImageSet = "TaharezGreenLook" | 
|---|
| 21 | schemeMgr:loadScheme("TaharezGreenHUDWidgets.scheme") | 
|---|
| 22 | hudImageSet = "TaharezGreenLook" | 
|---|
| 23 |  | 
|---|
| 24 | -- Just a remaining test hack | 
|---|
| 25 | schemeMgr:loadScheme("OrxonoxGUIScheme.scheme") | 
|---|
| 26 |  | 
|---|
| 27 | system:setDefaultMouseCursor(menuImageSet, "MouseArrow") | 
|---|
| 28 | system:setDefaultFont("BlueHighway-12") | 
|---|
| 29 | system:setDefaultTooltip("MenuWidgets/Tooltip") | 
|---|
| 30 |  | 
|---|
| 31 | local loadedSheets = {} | 
|---|
| 32 | local activeMenuSheets = {size = 0, topSheet = nil} | 
|---|
| 33 | --activeHUDSheets  = {size = 0, topSheet = nil} | 
|---|
| 34 | local root = nil | 
|---|
| 35 |  | 
|---|
| 36 | -- Require all tools | 
|---|
| 37 | require("GUITools") | 
|---|
| 38 |  | 
|---|
| 39 |  | 
|---|
| 40 | ----------------------- | 
|---|
| 41 | --- Local functions --- | 
|---|
| 42 | ----------------------- | 
|---|
| 43 |  | 
|---|
| 44 | -- Loads the GUI with the specified name | 
|---|
| 45 | -- The name corresponds to the filename of the *.lua and *.layout files | 
|---|
| 46 | -- but without the extension | 
|---|
| 47 | local function loadSheet(name) | 
|---|
| 48 |     -- Check if it has already been loaded | 
|---|
| 49 |     local sheet = loadedSheets[name] | 
|---|
| 50 |     if sheet == nil then | 
|---|
| 51 |         -- Load the sheet | 
|---|
| 52 |         sheet = require(name) | 
|---|
| 53 |         if sheet == nil then | 
|---|
| 54 |             return | 
|---|
| 55 |         end | 
|---|
| 56 |         sheet:load() | 
|---|
| 57 |         loadedSheets[name] = sheet | 
|---|
| 58 |         -- Hide new GUI as we do not want to show it accidentally | 
|---|
| 59 |         sheet:hide() | 
|---|
| 60 |     end | 
|---|
| 61 |     return sheet | 
|---|
| 62 | end | 
|---|
| 63 |  | 
|---|
| 64 | local function hideCursor() | 
|---|
| 65 |     if cursor:isVisible() then | 
|---|
| 66 |         cursor:hide() | 
|---|
| 67 |     end | 
|---|
| 68 | end | 
|---|
| 69 |  | 
|---|
| 70 | local function showCursor() | 
|---|
| 71 |     if not cursor:isVisible() and orxonox.InputManager:getInstance():isMouseExclusive() then | 
|---|
| 72 |         cursor:show() | 
|---|
| 73 |     end | 
|---|
| 74 | end | 
|---|
| 75 |  | 
|---|
| 76 | local function find(table, value) | 
|---|
| 77 |     for i, v in ipairs(table) do | 
|---|
| 78 |         if v == value then | 
|---|
| 79 |             return i | 
|---|
| 80 |         end | 
|---|
| 81 |     end | 
|---|
| 82 |     return nil | 
|---|
| 83 | end | 
|---|
| 84 |  | 
|---|
| 85 |  | 
|---|
| 86 | ------------------------ | 
|---|
| 87 | --- Global functions --- | 
|---|
| 88 | ------------------------ | 
|---|
| 89 |  | 
|---|
| 90 | -- ? | 
|---|
| 91 | function showGUI(name, bHidePrevious, bShowCursor, ptr) | 
|---|
| 92 |     gui = showGUI(name, bHidePrevious, bShowCursor) | 
|---|
| 93 |     gui.overlay = ptr | 
|---|
| 94 | end | 
|---|
| 95 |  | 
|---|
| 96 | -- Shows the specified menu sheet and loads it if neccessary | 
|---|
| 97 | function showGUI(name, bHidePrevious, bShowCursor) | 
|---|
| 98 |     -- Handle default value for bShowCursor | 
|---|
| 99 |     if bShowCursor == nil then | 
|---|
| 100 |         if activeMenuSheets.size > 0 then | 
|---|
| 101 |             bShowCursor = activeMenuSheets.topSheet.bShowCursor | 
|---|
| 102 |         else | 
|---|
| 103 |             bShowCursor = true | 
|---|
| 104 |         end | 
|---|
| 105 |     end | 
|---|
| 106 |  | 
|---|
| 107 |     -- Hide if already displayed (to make sure it is up front in the end) | 
|---|
| 108 |     if activeMenuSheets[name] ~= nil then | 
|---|
| 109 |         hideGUI(name) | 
|---|
| 110 |     end | 
|---|
| 111 |  | 
|---|
| 112 |     if not root then | 
|---|
| 113 |         setBackground("") | 
|---|
| 114 |     end | 
|---|
| 115 |  | 
|---|
| 116 |     -- Get sheet (or load it) | 
|---|
| 117 |     local menuSheet = loadSheet(name) | 
|---|
| 118 |     if not menuSheet then | 
|---|
| 119 |         return | 
|---|
| 120 |     end | 
|---|
| 121 |  | 
|---|
| 122 |     -- Add sheet to the root window | 
|---|
| 123 |     root:addChildWindow(menuSheet.window) | 
|---|
| 124 |  | 
|---|
| 125 |     -- Pause game control if this is the first menu to be displayed | 
|---|
| 126 |     -- HUGE HACK? | 
|---|
| 127 |     if activeMenuSheets.size == 0 then | 
|---|
| 128 |         orxonox.HumanController:pauseControl() | 
|---|
| 129 |     end | 
|---|
| 130 |  | 
|---|
| 131 |     -- Handle input distribution | 
|---|
| 132 |     orxonox.InputManager:getInstance():enterState(menuSheet.inputState) | 
|---|
| 133 |  | 
|---|
| 134 |     if bShowCursor then | 
|---|
| 135 |         showCursor() | 
|---|
| 136 |     else | 
|---|
| 137 |         hideCursor() | 
|---|
| 138 |     end | 
|---|
| 139 |  | 
|---|
| 140 |     -- Add the sheet in a tuple of additional information | 
|---|
| 141 |     local sheetTuple = | 
|---|
| 142 |     { | 
|---|
| 143 |         ["menuSheet"]      = menuSheet, | 
|---|
| 144 |         ["name"]           = name, | 
|---|
| 145 |         ["bShowCursor"]    = bShowCursor, | 
|---|
| 146 |         ["bHidePrevious"]  = bHidePrevious | 
|---|
| 147 |     } | 
|---|
| 148 |     table.insert(activeMenuSheets, sheetTuple) -- indexed array access | 
|---|
| 149 |     activeMenuSheets[name] = sheetTuple -- name access | 
|---|
| 150 |     activeMenuSheets.size = activeMenuSheets.size + 1 | 
|---|
| 151 |     activeMenuSheets.topSheet = sheetTuple | 
|---|
| 152 |  | 
|---|
| 153 |     -- Hide all previous sheets if necessary | 
|---|
| 154 |     if bHidePrevious then | 
|---|
| 155 |         for i = 1, activeMenuSheets.size - 1 do | 
|---|
| 156 |             activeMenuSheets[i].menuSheet:hide() | 
|---|
| 157 |         end | 
|---|
| 158 |     end | 
|---|
| 159 |  | 
|---|
| 160 |     menuSheet:show() | 
|---|
| 161 |     return menuSheet | 
|---|
| 162 | end | 
|---|
| 163 |  | 
|---|
| 164 | function hideGUI(name) | 
|---|
| 165 |     local sheetTuple = activeMenuSheets[name] | 
|---|
| 166 |     if sheetTuple == nil then | 
|---|
| 167 |         return | 
|---|
| 168 |     end | 
|---|
| 169 |  | 
|---|
| 170 |     -- Hide the sheet | 
|---|
| 171 |     sheetTuple.menuSheet:hide() | 
|---|
| 172 |  | 
|---|
| 173 |     -- Show sheets that were hidden by the sheet to be removed | 
|---|
| 174 |     local i = activeMenuSheets.size | 
|---|
| 175 |     -- Only do something if all sheets on top of sheetTuple | 
|---|
| 176 |     -- have bHidePrevious == false and sheetTuple.bHidePrevious == true | 
|---|
| 177 |     while i > 0 do | 
|---|
| 178 |         if activeMenuSheets[i].bHidePrevious == true then | 
|---|
| 179 |             if activeMenuSheets[i] == sheetTuple then | 
|---|
| 180 |                 i = i - 1 | 
|---|
| 181 |                 while i > 0 do | 
|---|
| 182 |                     activeMenuSheets[i].menuSheet:show() | 
|---|
| 183 |                     if activeMenuSheets[i].bHidePrevious == true then | 
|---|
| 184 |                         break | 
|---|
| 185 |                     end | 
|---|
| 186 |                     i = i - 1 | 
|---|
| 187 |                 end | 
|---|
| 188 |             end | 
|---|
| 189 |             break | 
|---|
| 190 |         end | 
|---|
| 191 |         i = i - 1 | 
|---|
| 192 |     end | 
|---|
| 193 |  | 
|---|
| 194 |     -- Remove sheet with its tuple from the table | 
|---|
| 195 |     root:removeChildWindow(sheetTuple.menuSheet.window) | 
|---|
| 196 |     table.remove(activeMenuSheets, find(activeMenuSheets, sheetTuple)) | 
|---|
| 197 |     activeMenuSheets[name] = nil | 
|---|
| 198 |     activeMenuSheets.size = activeMenuSheets.size - 1 | 
|---|
| 199 |     activeMenuSheets.topSheet = activeMenuSheets[activeMenuSheets.size] | 
|---|
| 200 |  | 
|---|
| 201 |     -- Leave the input state | 
|---|
| 202 |     orxonox.InputManager:getInstance():leaveState(sheetTuple.menuSheet.inputState) | 
|---|
| 203 |      | 
|---|
| 204 |     -- See whether to show or hide cursor | 
|---|
| 205 |     if activeMenuSheets.size > 0 and activeMenuSheets.topSheet.bShowCursor then | 
|---|
| 206 |         showCursor() | 
|---|
| 207 |     else | 
|---|
| 208 |         hideCursor() | 
|---|
| 209 |     end | 
|---|
| 210 |  | 
|---|
| 211 |     -- Resume control if the last menu is hidden | 
|---|
| 212 |     if activeMenuSheets.size == 0 then | 
|---|
| 213 |         orxonox.HumanController:resumeControl() | 
|---|
| 214 |         hideCursor() | 
|---|
| 215 |     end | 
|---|
| 216 | end | 
|---|
| 217 |  | 
|---|
| 218 | -- Hides all menu GUI sheets | 
|---|
| 219 | function hideAllGUIs() | 
|---|
| 220 |     while activeMenuSheets.size ~= 0 do | 
|---|
| 221 |         hideGUI(activeMenuSheets.topSheet.name) | 
|---|
| 222 |     end | 
|---|
| 223 | end | 
|---|
| 224 |  | 
|---|
| 225 | function keyESC() | 
|---|
| 226 |     -- HUGE, very HUGE hacks! | 
|---|
| 227 |     if activeMenuSheets.size == 1 and activeMenuSheets[1].name == "MainMenu" then | 
|---|
| 228 |         orxonox.execute("exit") | 
|---|
| 229 |     elseif activeMenuSheets.size > 0 then | 
|---|
| 230 |         orxonox.execute("hideGUI "..activeMenuSheets.topSheet.name) | 
|---|
| 231 |     else | 
|---|
| 232 |         showGUI("InGameMenu") | 
|---|
| 233 |     end | 
|---|
| 234 | end | 
|---|
| 235 |  | 
|---|
| 236 | function setBackground(name) | 
|---|
| 237 |     local newroot | 
|---|
| 238 |     if root ~= nil then | 
|---|
| 239 |         root:rename("oldRootWindow") | 
|---|
| 240 |     end | 
|---|
| 241 |     if name ~= "" then | 
|---|
| 242 |         newroot = winMgr:loadWindowLayout(name .. ".layout") | 
|---|
| 243 |         newroot:rename("AbsoluteRootWindow") | 
|---|
| 244 |         system:setGUISheet(newroot) | 
|---|
| 245 |     else | 
|---|
| 246 |         newroot = winMgr:createWindow("DefaultWindow", "AbsoluteRootWindow") | 
|---|
| 247 |         newroot:setProperty("Alpha", "0.0") | 
|---|
| 248 |         newroot:setSize(CEGUI.UVector2(CEGUI.UDim(1.0,0),CEGUI.UDim(1.0,0))) | 
|---|
| 249 |         system:setGUISheet(newroot) | 
|---|
| 250 |     end | 
|---|
| 251 |     if root ~= nil then | 
|---|
| 252 |         local child | 
|---|
| 253 |         while root:getChildCount()~=0 do | 
|---|
| 254 |             child = root:getChildAtIdx(0) | 
|---|
| 255 |             root:removeChildWindow(child) | 
|---|
| 256 |             newroot:addChildWindow(child) | 
|---|
| 257 |         end | 
|---|
| 258 |         winMgr:destroyWindow(root) | 
|---|
| 259 |     end | 
|---|
| 260 |     newroot:show() | 
|---|
| 261 |     root = newroot | 
|---|
| 262 | end | 
|---|