Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Small fix.

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