Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 30, 2010, 3:41:15 PM (15 years ago)
Author:
rgrieder
Message:

Restructured InitialiseGUI.lua a little bit. Also sorted out cursor showing issues.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/gamestate/data/gui/scripts/InitialiseGUI.lua

    r6572 r6658  
    2929system:setDefaultTooltip("MenuWidgets/Tooltip")
    3030
    31 loadedGUIs = {}
    32 cursorVisibility = {}
    33 activeSheets = {}
    34 nrOfActiveSheets = 0
    35 root = nil
    36 bShowsCursor = false
    37 bHidePrevious = {}
     31local loadedSheets = {}
     32local activeMenuSheets = {size = 0, topSheet = nil}
     33--activeHUDSheets  = {size = 0, topSheet = nil}
     34local root = nil
    3835
    3936-- Require all tools
    4037require("GUITools")
    4138
    42 -- loads the GUI with the specified filename
    43 -- be sure to set the global variable "filename" before calling this function
    44 function loadGUI(filename)
    45     -- check if it already exists
    46     loadedGui = loadedGUIs[filename]
    47     if loadedGui == nil then
    48         loadedGuiNS = require(filename)
    49         if loadedGuiNS == nil then
     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
    5054            return
    5155        end
    52         loadedGui = loadedGuiNS:load()
    53         loadedGUIs[filename] = loadedGui
    54         -- if there has no GUI been loaded yet, set new GUI as current
    55         if table.getn(loadedGUIs) == 1 then
    56             current = loadedGUIs[1]
    57         end
    58         -- hide new GUI as we do not want to show it accidentially
    59         loadedGui:hide()
    60     end
    61     return loadedGui
    62 end
    63 
    64 function showGUI(filename, hidePrevious, bCursorVisible, ptr)
    65     gui = showGUI(filename, hidePrevious, bCursorVisible)
     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
     76local function find(table, value)
     77    for i, v in ipairs(table) do
     78        if v == value then
     79            return i
     80        end
     81    end
     82    return nil
     83end
     84
     85
     86------------------------
     87--- Global functions ---
     88------------------------
     89
     90-- ?
     91function showGUI(name, bHidePrevious, bShowCursor, ptr)
     92    gui = showGUI(name, bHidePrevious, bShowCursor)
    6693    gui.overlay = ptr
    6794end
    6895
    69 -- shows the specified GUI sheet and loads it if not loaded already
    70 function showGUI(filename, hidePrevious, bCursorVisible)
    71     if bCursorVisible == nil then
    72         if nrOfActiveSheets > 0 then
    73             bCursorVisible = cursorVisibility[activeSheets[nrOfActiveSheets]]
     96-- Shows the specified menu sheet and loads it if neccessary
     97function showGUI(name, bHidePrevious, bShowCursor)
     98    -- Handle default value for bShowCursor
     99    if bShowCursor == nil then
     100        if activeMenuSheets.size > 0 then
     101            bShowCursor = activeMenuSheets.topSheet.bShowCursor
    74102        else
    75             bCursorVisible = true
    76         end
    77     end
    78 
    79     if root == nil then
     103            bShowCursor = true
     104        end
     105    end
     106
     107    -- Hide if already displayed (to make sure it is up front in the end)
     108    if activeMenuSheets[name] ~= nil then
     109        hideGUI(name)
     110    end
     111
     112    if not root then
    80113        setBackground("")
    81114    end
    82115
    83     local currentGUI = loadedGUIs[filename]
    84     if(currentGUI == nil) then
    85         currentGUI = loadGUI(filename)
    86     end
    87 
    88     if(root:isChild(currentGUI.window)) then
    89         root:removeChildWindow(currentGUI.window)
    90     end
    91     root:addChildWindow(currentGUI.window)
    92 
    93     if bCursorVisible then
     116    -- Get sheet (or load it)
     117    local menuSheet = loadSheet(name)
     118    if not menuSheet then
     119        return
     120    end
     121
     122    -- Add sheet to the root window
     123    root:addChildWindow(menuSheet.window)
     124
     125    -- Pause game control if this is the first menu to be displayed
     126    -- HUGE HACK?
     127    if activeMenuSheets.size == 0 then
     128        orxonox.HumanController:pauseControl()
     129    end
     130
     131    -- Handle input distribution
     132    orxonox.InputManager:getInstance():enterState(menuSheet.inputState)
     133
     134    if bShowCursor then
    94135        showCursor()
    95136    else
     
    97138    end
    98139
    99     if find( activeSheets, filename ) ~= nil then
    100         table.remove( activeSheets, find( activeSheets, filename ) )
    101         nrOfActiveSheets = nrOfActiveSheets - 1
    102     else
    103         if nrOfActiveSheets == 0 then
    104             --orxonox.InputManager:getInstance():enterState("guiMouseOnly")
    105             orxonox.HumanController:pauseControl()
    106         end
    107     end
    108     orxonox.InputManager:getInstance():enterState(currentGUI.inputState)
    109 
    110     nrOfActiveSheets = nrOfActiveSheets + 1
    111     table.insert(activeSheets, filename)
    112     activeSheets[nrOfActiveSheets] = filename
    113     bHidePrevious[filename]=hidePrevious
    114     cursorVisibility[filename] = bCursorVisible
    115 
    116     if hidePrevious == true then
    117         for i=1,nrOfActiveSheets-1 do
    118             loadedGUIs[ activeSheets[i] ]:hide()
    119         end
    120     end
    121     currentGUI:show()
    122     return currentGUI
    123 end
    124 
    125 function hideCursor()
    126     if bShowsCursor==true then
    127         bShowsCursor=false
    128         cursor:hide()
    129     end
    130 end
    131 
    132 function showCursor()
    133     if bShowsCursor==false then
    134         bShowsCursor=true
    135         cursor:show()
    136     end
    137 end
    138 
    139 function hideGUI(filename)
    140     local currentGUI = loadedGUIs[filename]
    141     if currentGUI == nil then
     140    -- Add the sheet in a tuple of additional information
     141    local sheetTuple =
     142    {
     143        ["menuSheet"]      = menuSheet,
     144        ["name"]           = name,
     145        ["bShowCursor"]    = bShowCursor,
     146        ["bHidePrevious"]  = bHidePrevious
     147    }
     148    table.insert(activeMenuSheets, sheetTuple) -- indexed array access
     149    activeMenuSheets[name] = sheetTuple -- name access
     150    activeMenuSheets.size = activeMenuSheets.size + 1
     151    activeMenuSheets.topSheet = sheetTuple
     152
     153    -- Hide all previous sheets if necessary
     154    if bHidePrevious then
     155        for i = 1, activeMenuSheets.size - 1 do
     156            activeMenuSheets[i].menuSheet:hide()
     157        end
     158    end
     159
     160    menuSheet:show()
     161    return menuSheet
     162end
     163
     164function hideGUI(name)
     165    local sheetTuple = activeMenuSheets[name]
     166    if sheetTuple == nil then
    142167        return
    143168    end
    144     currentGUI:hide()
    145     if bHidePrevious[filename] == true then
    146         local i = nrOfActiveSheets-1
    147         while i>0 do
    148             loadedGUIs[ activeSheets[i] ]:show()
    149             if bHidePrevious[filename]==true then
    150                 break
    151             else
    152                 i=i-1
     169
     170    -- Hide the sheet
     171    sheetTuple.menuSheet:hide()
     172
     173    -- Show sheets that were hidden by the sheet to be removed
     174    local i = activeMenuSheets.size
     175    -- Only do something if all sheets on top of sheetTuple
     176    -- have bHidePrevious == false and sheetTuple.bHidePrevious == true
     177    while i > 0 do
     178        if activeMenuSheets[i].bHidePrevious == true then
     179            if activeMenuSheets[i] == sheetTuple then
     180                i = i - 1
     181                while i > 0 do
     182                    activeMenuSheets[i].menuSheet:show()
     183                    if activeMenuSheets[i].bHidePrevious == true then
     184                        break
     185                    end
     186                    i = i - 1
     187                end
    153188            end
    154         end
    155     end
    156     root:removeChildWindow(currentGUI.window)
    157     local i=1
    158     while activeSheets[i] do
    159         if activeSheets[i+1] == nil then
    160             if activeSheets[i-1] ~= nil then
    161                 if cursorVisibility[ activeSheets[i-1] ] == true then
    162                     showCursor()
    163                 else
    164                     hideCursor()
    165                 end
    166             else
    167                 hideCursor()
    168             end
    169         end
    170         if activeSheets[i] == filename then
    171             table.remove( activeSheets, i )
    172             nrOfActiveSheets = nrOfActiveSheets-1
    173         else
    174             i = i+1
    175         end
    176     end
    177     cursorVisibility[filename] = nil -- remove the cursor visibility of the current gui from the table
    178     bHidePrevious[filename] = nil
    179     if nrOfActiveSheets == 0 then
    180         --orxonox.InputManager:getInstance():leaveState("guiMouseOnly")
     189            break
     190        end
     191        i = i - 1
     192    end
     193
     194    -- Remove sheet with its tuple from the table
     195    root:removeChildWindow(sheetTuple.menuSheet.window)
     196    table.remove(activeMenuSheets, find(activeMenuSheets, sheetTuple))
     197    activeMenuSheets[name] = nil
     198    activeMenuSheets.size = activeMenuSheets.size - 1
     199    activeMenuSheets.topSheet = activeMenuSheets[activeMenuSheets.size]
     200
     201    -- Leave the input state
     202    orxonox.InputManager:getInstance():leaveState(sheetTuple.menuSheet.inputState)
     203   
     204    -- See whether to show or hide cursor
     205    if activeMenuSheets.size > 0 and activeMenuSheets.topSheet.bShowCursor then
     206        showCursor()
     207    else
     208        hideCursor()
     209    end
     210
     211    -- Resume control if the last menu is hidden
     212    if activeMenuSheets.size == 0 then
    181213        orxonox.HumanController:resumeControl()
    182214        hideCursor()
    183215    end
    184     orxonox.InputManager:getInstance():leaveState(currentGUI.inputState)
    185 end
    186 
     216end
     217
     218-- Hides all menu GUI sheets
    187219function hideAllGUIs()
    188     while nrOfActiveSheets ~= 0 do
    189         hideGUI(activeSheets[nrOfActiveSheets])
     220    while activeMenuSheets.size ~= 0 do
     221        hideGUI(activeMenuSheets.topSheet.name)
    190222    end
    191223end
    192224
    193225function keyESC()
    194     if nrOfActiveSheets == 1 and activeSheets[1] == "MainMenu" then
     226    -- HUGE, very HUGE hacks!
     227    if activeMenuSheets.size == 1 and activeMenuSheets[1].name == "MainMenu" then
    195228        orxonox.execute("exit")
    196     elseif nrOfActiveSheets > 0 then
    197         orxonox.execute("hideGUI "..activeSheets[nrOfActiveSheets])
     229    elseif activeMenuSheets.size > 0 then
     230        orxonox.execute("hideGUI "..activeMenuSheets.topSheet.name)
    198231    else
    199232        showGUI("InGameMenu")
     
    201234end
    202235
    203 function setBackground(filename)
     236function setBackground(name)
    204237    local newroot
    205238    if root ~= nil then
    206239        root:rename("oldRootWindow")
    207240    end
    208     if filename ~= "" then
    209         newroot = winMgr:loadWindowLayout(filename .. ".layout")
     241    if name ~= "" then
     242        newroot = winMgr:loadWindowLayout(name .. ".layout")
    210243        newroot:rename("AbsoluteRootWindow")
    211244        system:setGUISheet(newroot)
     
    228261    root = newroot
    229262end
    230 
    231 function find(table, value)
    232     local i=0
    233     while table[i] ~= nil do
    234         if table[i]==value then
    235             return i
    236         else
    237             i=i+1
    238         end
    239     end
    240     return nil
    241 end
    242 
    243 function test(e)
    244     debug(0, "Blubb")
    245 end
Note: See TracChangeset for help on using the changeset viewer.