Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamestate/data/gui/scripts/InitialiseGUI.lua @ 6658

Last change on this file since 6658 was 6658, checked in by rgrieder, 15 years ago

Restructured InitialiseGUI.lua a little bit. Also sorted out cursor showing issues.

  • Property svn:eol-style set to native
File size: 7.3 KB
RevLine 
[6537]1winMgr   = CEGUI.WindowManager:getSingleton()
2guiMgr   = orxonox.GUIManager:getInstance()
3inputMgr = orxonox.InputManager:getInstance()
4
[5491]5local schemeMgr = CEGUI.SchemeManager:getSingleton()
[6537]6local system    = CEGUI.System:getSingleton()
7local cursor    = CEGUI.MouseCursor:getSingleton()
[5491]8
[6566]9-- Load all required skins
[6572]10schemeMgr:loadScheme("TaharezGreenLook.scheme")
11--schemeMgr:loadScheme("TaharezLook.scheme")
[6569]12--schemeMgr:loadScheme("WindowsLook.scheme")
13--schemeMgr:loadScheme("VanillaLook.scheme")
14--schemeMgr:loadScheme("SleekSpaceLook.scheme")
[6566]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
[6572]19schemeMgr:loadScheme("TaharezGreenMenuWidgets.scheme")
20menuImageSet = "TaharezGreenLook"
21schemeMgr:loadScheme("TaharezGreenHUDWidgets.scheme")
22hudImageSet = "TaharezGreenLook"
[6566]23
24-- Just a remaining test hack
[5491]25schemeMgr:loadScheme("OrxonoxGUIScheme.scheme")
26
[6571]27system:setDefaultMouseCursor(menuImageSet, "MouseArrow")
[5491]28system:setDefaultFont("BlueHighway-12")
[6564]29system:setDefaultTooltip("MenuWidgets/Tooltip")
[5491]30
[6658]31local loadedSheets = {}
32local activeMenuSheets = {size = 0, topSheet = nil}
33--activeHUDSheets  = {size = 0, topSheet = nil}
34local root = nil
[5491]35
[6417]36-- Require all tools
37require("GUITools")
38
[6658]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
47local 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
[6417]54            return
55        end
[6658]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
62end
63
64local function hideCursor()
65    if cursor:isVisible() then
66        cursor:hide()
67    end
68end
69
70local function showCursor()
71    if not cursor:isVisible() and orxonox.InputManager:getInstance():isMouseExclusive() then
72        cursor:show()
73    end
74end
75
76local function find(table, value)
77    for i, v in ipairs(table) do
78        if v == value then
79            return i
[5491]80        end
81    end
[6658]82    return nil
[5491]83end
84
[6658]85
86------------------------
87--- Global functions ---
88------------------------
89
90-- ?
91function showGUI(name, bHidePrevious, bShowCursor, ptr)
92    gui = showGUI(name, bHidePrevious, bShowCursor)
[5491]93    gui.overlay = ptr
94end
95
[6658]96-- Shows the specified menu sheet and loads it if neccessary
97function 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
[6417]102        else
[6658]103            bShowCursor = true
[5491]104        end
105    end
106
[6658]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
[6417]113        setBackground("")
114    end
115
[6658]116    -- Get sheet (or load it)
117    local menuSheet = loadSheet(name)
118    if not menuSheet then
119        return
[6417]120    end
121
[6658]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()
[6417]129    end
130
[6658]131    -- Handle input distribution
132    orxonox.InputManager:getInstance():enterState(menuSheet.inputState)
133
134    if bShowCursor then
[6417]135        showCursor()
[5491]136    else
[6417]137        hideCursor()
[5491]138    end
[6417]139
[6658]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
[6537]152
[6658]153    -- Hide all previous sheets if necessary
154    if bHidePrevious then
155        for i = 1, activeMenuSheets.size - 1 do
156            activeMenuSheets[i].menuSheet:hide()
[6417]157        end
158    end
[5491]159
[6658]160    menuSheet:show()
161    return menuSheet
[5491]162end
163
[6658]164function hideGUI(name)
165    local sheetTuple = activeMenuSheets[name]
166    if sheetTuple == nil then
167        return
[6417]168    end
[5491]169
[6658]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
[6417]187                end
188            end
[6658]189            break
[6417]190        end
[6658]191        i = i - 1
[6417]192    end
[6658]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
[6417]213        orxonox.HumanController:resumeControl()
214        hideCursor()
215    end
[5491]216end
[6417]217
[6658]218-- Hides all menu GUI sheets
[6417]219function hideAllGUIs()
[6658]220    while activeMenuSheets.size ~= 0 do
221        hideGUI(activeMenuSheets.topSheet.name)
[6417]222    end
223end
224
225function keyESC()
[6658]226    -- HUGE, very HUGE hacks!
227    if activeMenuSheets.size == 1 and activeMenuSheets[1].name == "MainMenu" then
[6417]228        orxonox.execute("exit")
[6658]229    elseif activeMenuSheets.size > 0 then
230        orxonox.execute("hideGUI "..activeMenuSheets.topSheet.name)
[6417]231    else
232        showGUI("InGameMenu")
233    end
234end
235
[6658]236function setBackground(name)
[6417]237    local newroot
238    if root ~= nil then
239        root:rename("oldRootWindow")
240    end
[6658]241    if name ~= "" then
242        newroot = winMgr:loadWindowLayout(name .. ".layout")
[6417]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
262end
Note: See TracBrowser for help on using the repository browser.