Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamestates2/data/gui/scripts/InitialiseGUI.lua @ 6704

Last change on this file since 6704 was 6671, checked in by rgrieder, 14 years ago

Added Tools.lua and moved 'find' in InitialiseGUI.lua to 'table.findIndex' in Tools.lua.

  • Property svn:eol-style set to native
File size: 7.1 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
76
77------------------------
78--- Global functions ---
79------------------------
80
81-- ?
82function showGUI(name, bHidePrevious, bShowCursor, ptr)
83    gui = showGUI(name, bHidePrevious, bShowCursor)
84    gui.overlay = ptr
85end
86
87-- Shows the specified menu sheet and loads it if neccessary
88function showGUI(name, bHidePrevious, bShowCursor)
89    -- Handle default value for bShowCursor
90    if bShowCursor == nil then
91        if activeMenuSheets.size > 0 then
92            bShowCursor = activeMenuSheets.topSheet.bShowCursor
93        else
94            bShowCursor = true
95        end
96    end
97
98    -- Hide if already displayed (to make sure it is up front in the end)
99    if activeMenuSheets[name] ~= nil then
100        hideGUI(name)
101    end
102
103    if not root then
104        setBackground("")
105    end
106
107    -- Get sheet (or load it)
108    local menuSheet = loadSheet(name)
109    if not menuSheet then
110        return
111    end
112
113    -- Add sheet to the root window
114    root:addChildWindow(menuSheet.window)
115
116    -- Pause game control if this is the first menu to be displayed
117    -- HUGE HACK?
118    if activeMenuSheets.size == 0 then
119        orxonox.HumanController:pauseControl()
120    end
121
122    -- Handle input distribution
123    orxonox.InputManager:getInstance():enterState(menuSheet.inputState)
124
125    if bShowCursor then
126        showCursor()
127    else
128        hideCursor()
129    end
130
131    -- Add the sheet in a tuple of additional information
132    local sheetTuple =
133    {
134        ["menuSheet"]      = menuSheet,
135        ["name"]           = name,
136        ["bShowCursor"]    = bShowCursor,
137        ["bHidePrevious"]  = bHidePrevious
138    }
139    table.insert(activeMenuSheets, sheetTuple) -- indexed array access
140    activeMenuSheets[name] = sheetTuple -- name access
141    activeMenuSheets.size = activeMenuSheets.size + 1
142    activeMenuSheets.topSheet = sheetTuple
143
144    -- Hide all previous sheets if necessary
145    if bHidePrevious then
146        for i = 1, activeMenuSheets.size - 1 do
147            activeMenuSheets[i].menuSheet:hide()
148        end
149    end
150
151    menuSheet:show()
152    return menuSheet
153end
154
155function hideGUI(name)
156    local sheetTuple = activeMenuSheets[name]
157    if sheetTuple == nil then
158        return
159    end
160
161    -- Hide the sheet
162    sheetTuple.menuSheet:hide()
163
164    -- Show sheets that were hidden by the sheet to be removed
165    local i = activeMenuSheets.size
166    -- Only do something if all sheets on top of sheetTuple
167    -- have bHidePrevious == false and sheetTuple.bHidePrevious == true
168    while i > 0 do
169        if activeMenuSheets[i].bHidePrevious == true then
170            if activeMenuSheets[i] == sheetTuple then
171                i = i - 1
172                while i > 0 do
173                    activeMenuSheets[i].menuSheet:show()
174                    if activeMenuSheets[i].bHidePrevious == true then
175                        break
176                    end
177                    i = i - 1
178                end
179            end
180            break
181        end
182        i = i - 1
183    end
184
185    -- Remove sheet with its tuple from the table
186    root:removeChildWindow(sheetTuple.menuSheet.window)
187    table.remove(activeMenuSheets, table.findIndex(activeMenuSheets, sheetTuple))
188    activeMenuSheets[name] = nil
189    activeMenuSheets.size = activeMenuSheets.size - 1
190    activeMenuSheets.topSheet = activeMenuSheets[activeMenuSheets.size]
191
192    -- Leave the input state
193    orxonox.InputManager:getInstance():leaveState(sheetTuple.menuSheet.inputState)
194   
195    -- See whether to show or hide cursor
196    if activeMenuSheets.size > 0 and activeMenuSheets.topSheet.bShowCursor then
197        showCursor()
198    else
199        hideCursor()
200    end
201
202    -- Resume control if the last menu is hidden
203    if activeMenuSheets.size == 0 then
204        orxonox.HumanController:resumeControl()
205        hideCursor()
206    end
207end
208
209-- Hides all menu GUI sheets
210function hideAllGUIs()
211    while activeMenuSheets.size ~= 0 do
212        hideGUI(activeMenuSheets.topSheet.name)
213    end
214end
215
216function keyESC()
217    -- HUGE, very HUGE hacks!
218    if activeMenuSheets.size == 1 and activeMenuSheets[1].name == "MainMenu" then
219        orxonox.execute("exit")
220    elseif activeMenuSheets.size > 0 then
221        orxonox.execute("hideGUI "..activeMenuSheets.topSheet.name)
222    else
223        showGUI("InGameMenu")
224    end
225end
226
227function setBackground(name)
228    local newroot
229    if root ~= nil then
230        root:rename("oldRootWindow")
231    end
232    if name ~= "" then
233        newroot = winMgr:loadWindowLayout(name .. ".layout")
234        newroot:rename("AbsoluteRootWindow")
235        system:setGUISheet(newroot)
236    else
237        newroot = winMgr:createWindow("DefaultWindow", "AbsoluteRootWindow")
238        newroot:setProperty("Alpha", "0.0")
239        newroot:setSize(CEGUI.UVector2(CEGUI.UDim(1.0,0),CEGUI.UDim(1.0,0)))
240        system:setGUISheet(newroot)
241    end
242    if root ~= nil then
243        local child
244        while root:getChildCount()~=0 do
245            child = root:getChildAtIdx(0)
246            root:removeChildWindow(child)
247            newroot:addChildWindow(child)
248        end
249        winMgr:destroyWindow(root)
250    end
251    newroot:show()
252    root = newroot
253end
Note: See TracBrowser for help on using the repository browser.