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, 14 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
Line 
1winMgr   = CEGUI.WindowManager:getSingleton()
2guiMgr   = orxonox.GUIManager:getInstance()
3inputMgr = orxonox.InputManager:getInstance()
4
5local schemeMgr = CEGUI.SchemeManager:getSingleton()
6local system    = CEGUI.System:getSingleton()
7local cursor    = CEGUI.MouseCursor:getSingleton()
8
9-- Load all required skins
10schemeMgr: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
19schemeMgr:loadScheme("TaharezGreenMenuWidgets.scheme")
20menuImageSet = "TaharezGreenLook"
21schemeMgr:loadScheme("TaharezGreenHUDWidgets.scheme")
22hudImageSet = "TaharezGreenLook"
23
24-- Just a remaining test hack
25schemeMgr:loadScheme("OrxonoxGUIScheme.scheme")
26
27system:setDefaultMouseCursor(menuImageSet, "MouseArrow")
28system:setDefaultFont("BlueHighway-12")
29system:setDefaultTooltip("MenuWidgets/Tooltip")
30
31local loadedSheets = {}
32local activeMenuSheets = {size = 0, topSheet = nil}
33--activeHUDSheets  = {size = 0, topSheet = nil}
34local root = nil
35
36-- Require all tools
37require("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
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
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
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
80        end
81    end
82    return nil
83end
84
85
86------------------------
87--- Global functions ---
88------------------------
89
90-- ?
91function showGUI(name, bHidePrevious, bShowCursor, ptr)
92    gui = showGUI(name, bHidePrevious, bShowCursor)
93    gui.overlay = ptr
94end
95
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
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
162end
163
164function 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
216end
217
218-- Hides all menu GUI sheets
219function hideAllGUIs()
220    while activeMenuSheets.size ~= 0 do
221        hideGUI(activeMenuSheets.topSheet.name)
222    end
223end
224
225function 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
234end
235
236function 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
262end
Note: See TracBrowser for help on using the repository browser.