Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/notifications/data/gui/scripts/SheetManager.lua @ 7425

Last change on this file since 7425 was 7395, checked in by dafrick, 14 years ago

Finished EditMode, needs some polish (no not the language…) though.
Took better advantage of lua tables in NotificationLayer.lua
Fixed a lot of bugs, it really is a miracle that the Notifications worked as well as they did.

  • Property svn:eol-style set to native
File size: 6.5 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, bNoInput, ptr)
46    local sheet = showMenuSheet(name, bHidePrevious, bNoInput)
47    sheet.overlay = ptr
48    return sheet
49end
50
51-- Shows the specified menu sheet and loads it if neccessary
52function showMenuSheet(name, bHidePrevious, bNoInput)
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    -- Set bNoInput to false if it hasn't been set.
66    if bNoInput == nil then
67        bNoInput = false
68    end
69
70    -- Count the number of sheets that don't need input till the first that does.
71    local counter = noInputSheetCounter()
72    -- Pause game control if this is the first menu to be displayed
73    -- HUGE HACK?
74    if bNoInput == false and counter == 0 then
75        orxonox.HumanController:pauseControl()
76    end
77
78    -- Hide if already displayed (to make sure it is up front in the end)
79    if activeMenuSheets[name] ~= nil then
80        hideMenuSheet(name)
81    end
82
83    if bNoInput == true then
84        menuSheet.tShowCursor = TriBool.Dontcare
85    end
86
87    -- Add the sheet in a tuple of additional information
88    local sheetTuple =
89    {
90        ["sheet"]          = menuSheet,
91        ["bHidePrevious"]  = bHidePrevious,
92        ["bNoInput"]       = bNoInput
93    }
94    table.insert(activeMenuSheets, sheetTuple) -- indexed array access
95    activeMenuSheets[name] = sheetTuple -- name access
96    activeMenuSheets.size = activeMenuSheets.size + 1
97    activeMenuSheets.topSheetTuple = sheetTuple
98
99    -- Add sheet to the root window
100    menuSheetsRoot:addChildWindow(menuSheet.window)
101
102    -- Handle input distribution
103    if bNoInput == false then
104        inputMgr:enterState(menuSheet.inputState)
105    end
106
107    -- Only change cursor situation if menuSheet.tShowCursor ~= TriBool.Dontcare
108    if menuSheet.tShowCursor == TriBool.True then
109        showCursor()
110    elseif menuSheet.tShowCursor == TriBool.False then
111        hideCursor()
112    end
113
114    -- Hide all previous sheets if necessary
115    if bHidePrevious then
116        for i = 1, activeMenuSheets.size - 1 do
117            activeMenuSheets[i].sheet:hide()
118        end
119    end
120
121    menuSheet:show()
122
123    return menuSheet
124end
125
126function hideMenuSheet(name)
127    local sheetTuple = activeMenuSheets[name]
128    if sheetTuple == nil then
129        return
130    end
131
132    -- Hide the sheet
133    sheetTuple.sheet:hide()
134
135    -- Show sheets that were hidden by the sheet to be removed
136    local i = activeMenuSheets.size
137    -- Only do something if all sheets on top of sheetTuple
138    -- have bHidePrevious == true and sheetTuple.bHidePrevious == true
139    while i > 0 do
140        if activeMenuSheets[i].bHidePrevious then
141            if activeMenuSheets[i] == sheetTuple then
142                i = i - 1
143                while i > 0 do
144                    activeMenuSheets[i].sheet:show()
145                    if activeMenuSheets[i].bHidePrevious then
146                        break
147                    end
148                    i = i - 1
149                end
150            end
151            break
152        end
153        i = i - 1
154    end
155
156    -- Remove sheet with its tuple from the table
157    menuSheetsRoot:removeChildWindow(sheetTuple.sheet.window)
158    table.remove(activeMenuSheets, table.findIndex(activeMenuSheets, sheetTuple))
159    activeMenuSheets[name] = nil
160    activeMenuSheets.size = activeMenuSheets.size - 1
161    activeMenuSheets.topSheetTuple = activeMenuSheets[activeMenuSheets.size]
162
163    -- Leave the input state
164    if not sheetTuple.bNoInput then
165        inputMgr:leaveState(sheetTuple.sheet.inputState)
166    end
167   
168    -- CURSOR SHOWING
169    local i = activeMenuSheets.size
170    -- Find top most sheet that doesn't have tShowCusor == TriBool.Dontcare
171    while i > 0 and activeMenuSheets[i].sheet.tShowCursor == TriBool.Dontcare do
172        i = i - 1
173    end
174    if i > 0 and activeMenuSheets[i].sheet.tShowCursor == TriBool.True then
175        showCursor()
176    else
177        hideCursor()
178    end
179
180    -- Count the number of sheets that don't need input till the first that does.
181    local counter = noInputSheetCounter()
182    -- Resume control if the last (non-noInput) menu is hidden
183    if counter == 0 then
184        orxonox.HumanController:resumeControl()
185        hideCursor()
186    end
187
188    sheetTuple.sheet:afterHide()
189end
190
191-- Hides all menu GUI sheets
192function hideAllMenuSheets()
193    while activeMenuSheets.size ~= 0 do
194        hideMenuSheet(activeMenuSheets.topSheetTuple.sheet.name)
195    end
196end
197
198function keyESC()
199    -- HUGE, very HUGE hacks!
200
201    -- Count the number of sheets that don't need input till the first that does.
202    local counter = noInputSheetCounter()
203
204    -- If the first sheet that needs input is the MainMenu.
205    if counter == 1 and activeMenuSheets[1].sheet.name == "MainMenu" then
206        orxonox.execute("exit")
207    -- If there is at least one sheet that needs input.
208    elseif counter > 0 then
209        orxonox.execute("hideGUI "..activeMenuSheets[counter].sheet.name)
210    else
211        showMenuSheet("InGameMenu")
212    end
213end
214
215function setBackgroundImage(imageSet, imageName)
216    guiMgr:setBackgroundImage(imageSet, imageName)
217end
218
219function noInputSheetCounter()
220    -- Count the number of sheets that don't need input till the first that does.
221    local counter = activeMenuSheets.size
222    while counter > 0 and activeMenuSheets[counter].bNoInput do
223        counter = counter - 1
224    end
225    return counter
226end
227
228----------------------
229--- Initialisation ---
230----------------------
231
232hideCursor()
Note: See TracBrowser for help on using the repository browser.