Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamestates3/data/gui/scripts/SheetManager.lua @ 6907

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

Merged gamestates2 branch back to trunk.
This brings in some heavy changes in the GUI framework.
It should also fix problems with triggered asserts in the InputManager.

Note: PickupInventory does not seem to work —> Segfault when showing because before, the owner in GUIOverlay::setGUIName is already NULL.
I haven't tested it before, so I can't tell whether it's my changes.

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1-- SheetManager.lua
2
3local cursor = CEGUI.MouseCursor:getSingleton()
4local loadedSheets = {}
5local activeMenuSheets = {size = 0, topSheetTuple = nil}
6local menuSheetsRoot = guiMgr:getMenuRootWindow()
7
8-----------------------
9--- Local functions ---
10-----------------------
11
12local function hideCursor()
13    if cursor:isVisible() then
14        cursor:hide()
15    end
16end
17
18local function showCursor()
19    if not cursor:isVisible() and inputMgr:isMouseExclusive() then
20        cursor:show()
21    end
22end
23
24
25------------------------
26--- Global functions ---
27------------------------
28
29-- Loads the GUI with the specified name
30-- The name corresponds to the filename of the *.lua and *.layout files
31-- but without the extension
32function loadSheet(name)
33    -- Check if it has already been loaded
34    local sheet = loadedSheets[name]
35    if sheet == nil then
36        -- Load the sheet
37        sheet = require(name)
38        sheet:load()
39        loadedSheets[name] = sheet
40    end
41    return sheet
42end
43
44-- ?
45function showMenuSheet(name, bHidePrevious, ptr)
46    local sheet = showMenuSheet(name, bHidePrevious)
47    sheet.overlay = ptr
48    return sheet
49end
50
51-- Shows the specified menu sheet and loads it if neccessary
52function showMenuSheet(name, bHidePrevious)
53    if name == "" then
54        return nil
55    end
56    -- Get sheet (or load it)
57    local menuSheet = loadSheet(name)
58
59    -- Use sheet's value if nil was provided
60    if bHidePrevious == nil then
61        bHidePrevious = menuSheet.bHidePrevious
62        assert(bHidePrevious ~= nil)
63    end
64
65    -- Pause game control if this is the first menu to be displayed
66    -- HUGE HACK?
67    if activeMenuSheets.size == 0 then
68        orxonox.HumanController:pauseControl()
69    end
70
71    -- Hide if already displayed (to make sure it is up front in the end)
72    if activeMenuSheets[name] ~= nil then
73        hideMenuSheet(name)
74    end
75
76    -- Add the sheet in a tuple of additional information
77    local sheetTuple =
78    {
79        ["sheet"]          = menuSheet,
80        ["bHidePrevious"]  = bHidePrevious
81    }
82    table.insert(activeMenuSheets, sheetTuple) -- indexed array access
83    activeMenuSheets[name] = sheetTuple -- name access
84    activeMenuSheets.size = activeMenuSheets.size + 1
85    activeMenuSheets.topSheetTuple = sheetTuple
86
87    -- Add sheet to the root window
88    menuSheetsRoot:addChildWindow(menuSheet.window)
89
90    -- Handle input distribution
91    inputMgr:enterState(menuSheet.inputState)
92
93    -- Only change cursor situation if menuSheet.tShowCursor ~= TriBool.Dontcare
94    if menuSheet.tShowCursor == TriBool.True then
95        showCursor()
96    elseif menuSheet.tShowCursor == TriBool.False then
97        hideCursor()
98    end
99
100    -- Hide all previous sheets if necessary
101    if bHidePrevious then
102        for i = 1, activeMenuSheets.size - 1 do
103            activeMenuSheets[i].sheet:hide()
104        end
105    end
106
107    menuSheet:show()
108
109    return menuSheet
110end
111
112function hideMenuSheet(name)
113    local sheetTuple = activeMenuSheets[name]
114    if sheetTuple == nil then
115        return
116    end
117
118    -- Hide the sheet
119    sheetTuple.sheet:hide()
120
121    -- Show sheets that were hidden by the sheet to be removed
122    local i = activeMenuSheets.size
123    -- Only do something if all sheets on top of sheetTuple
124    -- have bHidePrevious == true and sheetTuple.bHidePrevious == true
125    while i > 0 do
126        if activeMenuSheets[i].bHidePrevious then
127            if activeMenuSheets[i] == sheetTuple then
128                i = i - 1
129                while i > 0 do
130                    activeMenuSheets[i].sheet:show()
131                    if activeMenuSheets[i].bHidePrevious then
132                        break
133                    end
134                    i = i - 1
135                end
136            end
137            break
138        end
139        i = i - 1
140    end
141
142    -- Remove sheet with its tuple from the table
143    menuSheetsRoot:removeChildWindow(sheetTuple.sheet.window)
144    table.remove(activeMenuSheets, table.findIndex(activeMenuSheets, sheetTuple))
145    activeMenuSheets[name] = nil
146    activeMenuSheets.size = activeMenuSheets.size - 1
147    activeMenuSheets.topSheetTuple = activeMenuSheets[activeMenuSheets.size]
148
149    -- Leave the input state
150    inputMgr:leaveState(sheetTuple.sheet.inputState)
151   
152    -- CURSOR SHOWING
153    local i = activeMenuSheets.size
154    -- Find top most sheet that doesn't have tShowCusor == TriBool.Dontcare
155    while i > 0 and activeMenuSheets[i].sheet.tShowCursor == TriBool.Dontcare do
156        i = i - 1
157    end
158    if i > 0 and activeMenuSheets[i].sheet.tShowCursor == TriBool.True then
159        showCursor()
160    else
161        hideCursor()
162    end
163
164    -- Resume control if the last menu is hidden
165    if activeMenuSheets.size == 0 then
166        orxonox.HumanController:resumeControl()
167        hideCursor()
168    end
169end
170
171-- Hides all menu GUI sheets
172function hideAllMenuSheets()
173    while activeMenuSheets.size ~= 0 do
174        hideMenuSheet(activeMenuSheets.topSheetTuple.sheet.name)
175    end
176end
177
178function keyESC()
179    -- HUGE, very HUGE hacks!
180    if activeMenuSheets.size == 1 and activeMenuSheets[1].sheet.name == "MainMenu" then
181        orxonox.execute("exit")
182    elseif activeMenuSheets.size > 0 then
183        orxonox.execute("hideGUI "..activeMenuSheets.topSheetTuple.sheet.name)
184    else
185        showMenuSheet("InGameMenu")
186    end
187end
188
189function setBackgroundImage(imageSet, imageName)
190    guiMgr:setBackgroundImage(imageSet, imageName)
191end
192
193----------------------
194--- Initialisation ---
195----------------------
196
197hideCursor()
Note: See TracBrowser for help on using the repository browser.