Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Changing from OrxonoxOverlays to CEGUI as means of displaying Notifications.
Still messy and not working completely but it's a start.

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