[Orxonox-commit 1941] r6658 - in code/branches/gamestate: data/gui/scripts src/libraries/core src/libraries/core/input src/orxonox/gamestates

rgrieder at orxonox.net rgrieder at orxonox.net
Tue Mar 30 15:41:15 CEST 2010


Author: rgrieder
Date: 2010-03-30 15:41:15 +0200 (Tue, 30 Mar 2010)
New Revision: 6658

Modified:
   code/branches/gamestate/data/gui/scripts/InitialiseGUI.lua
   code/branches/gamestate/src/libraries/core/GUIManager.cc
   code/branches/gamestate/src/libraries/core/GUIManager.h
   code/branches/gamestate/src/libraries/core/input/InputManager.h
   code/branches/gamestate/src/orxonox/gamestates/GSMainMenu.cc
Log:
Restructured InitialiseGUI.lua a little bit. Also sorted out cursor showing issues.

Modified: code/branches/gamestate/data/gui/scripts/InitialiseGUI.lua
===================================================================
--- code/branches/gamestate/data/gui/scripts/InitialiseGUI.lua	2010-03-30 10:13:33 UTC (rev 6657)
+++ code/branches/gamestate/data/gui/scripts/InitialiseGUI.lua	2010-03-30 13:41:15 UTC (rev 6658)
@@ -28,185 +28,218 @@
 system:setDefaultFont("BlueHighway-12")
 system:setDefaultTooltip("MenuWidgets/Tooltip")
 
-loadedGUIs = {}
-cursorVisibility = {}
-activeSheets = {}
-nrOfActiveSheets = 0
-root = nil
-bShowsCursor = false
-bHidePrevious = {}
+local loadedSheets = {}
+local activeMenuSheets = {size = 0, topSheet = nil}
+--activeHUDSheets  = {size = 0, topSheet = nil}
+local root = nil
 
 -- Require all tools
 require("GUITools")
 
--- loads the GUI with the specified filename
--- be sure to set the global variable "filename" before calling this function
-function loadGUI(filename)
-    -- check if it already exists
-    loadedGui = loadedGUIs[filename]
-    if loadedGui == nil then
-        loadedGuiNS = require(filename)
-        if loadedGuiNS == nil then
+
+-----------------------
+--- Local functions ---
+-----------------------
+
+-- Loads the GUI with the specified name
+-- The name corresponds to the filename of the *.lua and *.layout files
+-- but without the extension
+local function loadSheet(name)
+    -- Check if it has already been loaded
+    local sheet = loadedSheets[name]
+    if sheet == nil then
+        -- Load the sheet
+        sheet = require(name)
+        if sheet == nil then
             return
         end
-        loadedGui = loadedGuiNS:load()
-        loadedGUIs[filename] = loadedGui
-        -- if there has no GUI been loaded yet, set new GUI as current
-        if table.getn(loadedGUIs) == 1 then
-            current = loadedGUIs[1]
+        sheet:load()
+        loadedSheets[name] = sheet
+        -- Hide new GUI as we do not want to show it accidentally
+        sheet:hide()
+    end
+    return sheet
+end
+
+local function hideCursor()
+    if cursor:isVisible() then
+        cursor:hide()
+    end
+end
+
+local function showCursor()
+    if not cursor:isVisible() and orxonox.InputManager:getInstance():isMouseExclusive() then
+        cursor:show()
+    end
+end
+
+local function find(table, value)
+    for i, v in ipairs(table) do
+        if v == value then
+            return i
         end
-        -- hide new GUI as we do not want to show it accidentially
-        loadedGui:hide()
     end
-    return loadedGui
+    return nil
 end
 
-function showGUI(filename, hidePrevious, bCursorVisible, ptr)
-    gui = showGUI(filename, hidePrevious, bCursorVisible)
+
+------------------------
+--- Global functions ---
+------------------------
+
+-- ?
+function showGUI(name, bHidePrevious, bShowCursor, ptr)
+    gui = showGUI(name, bHidePrevious, bShowCursor)
     gui.overlay = ptr
 end
 
--- shows the specified GUI sheet and loads it if not loaded already
-function showGUI(filename, hidePrevious, bCursorVisible)
-    if bCursorVisible == nil then
-        if nrOfActiveSheets > 0 then
-            bCursorVisible = cursorVisibility[activeSheets[nrOfActiveSheets]]
+-- Shows the specified menu sheet and loads it if neccessary
+function showGUI(name, bHidePrevious, bShowCursor)
+    -- Handle default value for bShowCursor
+    if bShowCursor == nil then
+        if activeMenuSheets.size > 0 then
+            bShowCursor = activeMenuSheets.topSheet.bShowCursor
         else
-            bCursorVisible = true
+            bShowCursor = true
         end
     end
 
-    if root == nil then
+    -- Hide if already displayed (to make sure it is up front in the end)
+    if activeMenuSheets[name] ~= nil then
+        hideGUI(name)
+    end
+
+    if not root then
         setBackground("")
     end
 
-    local currentGUI = loadedGUIs[filename]
-    if(currentGUI == nil) then
-        currentGUI = loadGUI(filename)
+    -- Get sheet (or load it)
+    local menuSheet = loadSheet(name)
+    if not menuSheet then
+        return
     end
 
-    if(root:isChild(currentGUI.window)) then
-        root:removeChildWindow(currentGUI.window)
+    -- Add sheet to the root window
+    root:addChildWindow(menuSheet.window)
+
+    -- Pause game control if this is the first menu to be displayed
+    -- HUGE HACK?
+    if activeMenuSheets.size == 0 then
+        orxonox.HumanController:pauseControl()
     end
-    root:addChildWindow(currentGUI.window)
 
-    if bCursorVisible then
+    -- Handle input distribution
+    orxonox.InputManager:getInstance():enterState(menuSheet.inputState)
+
+    if bShowCursor then
         showCursor()
     else
         hideCursor()
     end
 
-    if find( activeSheets, filename ) ~= nil then
-        table.remove( activeSheets, find( activeSheets, filename ) )
-        nrOfActiveSheets = nrOfActiveSheets - 1
-    else
-        if nrOfActiveSheets == 0 then
-            --orxonox.InputManager:getInstance():enterState("guiMouseOnly")
-            orxonox.HumanController:pauseControl()
-        end
-    end
-    orxonox.InputManager:getInstance():enterState(currentGUI.inputState)
+    -- Add the sheet in a tuple of additional information
+    local sheetTuple =
+    {
+        ["menuSheet"]      = menuSheet,
+        ["name"]           = name,
+        ["bShowCursor"]    = bShowCursor,
+        ["bHidePrevious"]  = bHidePrevious
+    }
+    table.insert(activeMenuSheets, sheetTuple) -- indexed array access
+    activeMenuSheets[name] = sheetTuple -- name access
+    activeMenuSheets.size = activeMenuSheets.size + 1
+    activeMenuSheets.topSheet = sheetTuple
 
-    nrOfActiveSheets = nrOfActiveSheets + 1
-    table.insert(activeSheets, filename)
-    activeSheets[nrOfActiveSheets] = filename
-    bHidePrevious[filename]=hidePrevious
-    cursorVisibility[filename] = bCursorVisible
-
-    if hidePrevious == true then
-        for i=1,nrOfActiveSheets-1 do
-            loadedGUIs[ activeSheets[i] ]:hide()
+    -- Hide all previous sheets if necessary
+    if bHidePrevious then
+        for i = 1, activeMenuSheets.size - 1 do
+            activeMenuSheets[i].menuSheet:hide()
         end
     end
-    currentGUI:show()
-    return currentGUI
-end
 
-function hideCursor()
-    if bShowsCursor==true then
-        bShowsCursor=false
-        cursor:hide()
-    end
+    menuSheet:show()
+    return menuSheet
 end
 
-function showCursor()
-    if bShowsCursor==false then
-        bShowsCursor=true
-        cursor:show()
+function hideGUI(name)
+    local sheetTuple = activeMenuSheets[name]
+    if sheetTuple == nil then
+        return
     end
-end
 
-function hideGUI(filename)
-    local currentGUI = loadedGUIs[filename]
-    if currentGUI == nil then
-        return
-    end
-    currentGUI:hide()
-    if bHidePrevious[filename] == true then
-        local i = nrOfActiveSheets-1
-        while i>0 do
-            loadedGUIs[ activeSheets[i] ]:show()
-            if bHidePrevious[filename]==true then
-                break
-            else
-                i=i-1
-            end
-        end
-    end
-    root:removeChildWindow(currentGUI.window)
-    local i=1
-    while activeSheets[i] do
-        if activeSheets[i+1] == nil then
-            if activeSheets[i-1] ~= nil then
-                if cursorVisibility[ activeSheets[i-1] ] == true then
-                    showCursor()
-                else
-                    hideCursor()
+    -- Hide the sheet
+    sheetTuple.menuSheet:hide()
+
+    -- Show sheets that were hidden by the sheet to be removed
+    local i = activeMenuSheets.size
+    -- Only do something if all sheets on top of sheetTuple
+    -- have bHidePrevious == false and sheetTuple.bHidePrevious == true
+    while i > 0 do
+        if activeMenuSheets[i].bHidePrevious == true then
+            if activeMenuSheets[i] == sheetTuple then
+                i = i - 1
+                while i > 0 do
+                    activeMenuSheets[i].menuSheet:show()
+                    if activeMenuSheets[i].bHidePrevious == true then
+                        break
+                    end
+                    i = i - 1
                 end
-            else
-                hideCursor()
             end
+            break
         end
-        if activeSheets[i] == filename then
-            table.remove( activeSheets, i )
-            nrOfActiveSheets = nrOfActiveSheets-1
-        else
-            i = i+1
-        end
+        i = i - 1
     end
-    cursorVisibility[filename] = nil -- remove the cursor visibility of the current gui from the table
-    bHidePrevious[filename] = nil
-    if nrOfActiveSheets == 0 then
-        --orxonox.InputManager:getInstance():leaveState("guiMouseOnly")
+
+    -- Remove sheet with its tuple from the table
+    root:removeChildWindow(sheetTuple.menuSheet.window)
+    table.remove(activeMenuSheets, find(activeMenuSheets, sheetTuple))
+    activeMenuSheets[name] = nil
+    activeMenuSheets.size = activeMenuSheets.size - 1
+    activeMenuSheets.topSheet = activeMenuSheets[activeMenuSheets.size]
+
+    -- Leave the input state
+    orxonox.InputManager:getInstance():leaveState(sheetTuple.menuSheet.inputState)
+    
+    -- See whether to show or hide cursor
+    if activeMenuSheets.size > 0 and activeMenuSheets.topSheet.bShowCursor then
+        showCursor()
+    else
+        hideCursor()
+    end
+
+    -- Resume control if the last menu is hidden
+    if activeMenuSheets.size == 0 then
         orxonox.HumanController:resumeControl()
         hideCursor()
     end
-    orxonox.InputManager:getInstance():leaveState(currentGUI.inputState)
 end
 
+-- Hides all menu GUI sheets
 function hideAllGUIs()
-    while nrOfActiveSheets ~= 0 do
-        hideGUI(activeSheets[nrOfActiveSheets])
+    while activeMenuSheets.size ~= 0 do
+        hideGUI(activeMenuSheets.topSheet.name)
     end
 end
 
 function keyESC()
-    if nrOfActiveSheets == 1 and activeSheets[1] == "MainMenu" then
+    -- HUGE, very HUGE hacks!
+    if activeMenuSheets.size == 1 and activeMenuSheets[1].name == "MainMenu" then
         orxonox.execute("exit")
-    elseif nrOfActiveSheets > 0 then
-        orxonox.execute("hideGUI "..activeSheets[nrOfActiveSheets])
+    elseif activeMenuSheets.size > 0 then
+        orxonox.execute("hideGUI "..activeMenuSheets.topSheet.name)
     else
         showGUI("InGameMenu")
     end
 end
 
-function setBackground(filename)
+function setBackground(name)
     local newroot
     if root ~= nil then
         root:rename("oldRootWindow")
     end
-    if filename ~= "" then
-        newroot = winMgr:loadWindowLayout(filename .. ".layout")
+    if name ~= "" then
+        newroot = winMgr:loadWindowLayout(name .. ".layout")
         newroot:rename("AbsoluteRootWindow")
         system:setGUISheet(newroot)
     else
@@ -227,19 +260,3 @@
     newroot:show()
     root = newroot
 end
-
-function find(table, value)
-    local i=0
-    while table[i] ~= nil do
-        if table[i]==value then
-            return i
-        else
-            i=i+1
-        end
-    end
-    return nil
-end
-
-function test(e)
-    debug(0, "Blubb")
-end

Modified: code/branches/gamestate/src/libraries/core/GUIManager.cc
===================================================================
--- code/branches/gamestate/src/libraries/core/GUIManager.cc	2010-03-30 10:13:33 UTC (rev 6657)
+++ code/branches/gamestate/src/libraries/core/GUIManager.cc	2010-03-30 13:41:15 UTC (rev 6658)
@@ -252,19 +252,27 @@
         GUIManager::getInstance().executeCode("hideGUI(\"" + name + "\")");
     }
 
-    const std::string& GUIManager::createInputState(const std::string& name, TriBool::Value showMouse, TriBool::Value useKeyboard, bool bBlockJoyStick)
+    const std::string& GUIManager::createInputState(const std::string& name, TriBool::Value showCursor, TriBool::Value useKeyboard, bool bBlockJoyStick)
     {
         InputState* state = InputManager::getInstance().createInputState(name);
 
-        if (GraphicsManager::getInstance().isFullScreen() && showMouse == TriBool::True ||
-           !GraphicsManager::getInstance().isFullScreen() && showMouse == TriBool::False)
+        /* Table that maps isFullScreen() and showCursor to mouseExclusive
+        isFullscreen / showCursor | True  | False | Dontcare
+        ----------------------------------------------------
+        true                      | True  | True  | Dontcare
+        ----------------------------------------------------
+        false                     | False | True  | Dontcare
+        */
+        if (showCursor == TriBool::Dontcare)
+            state->setMouseExclusive(TriBool::Dontcare);
+        else if (GraphicsManager::getInstance().isFullScreen() || showCursor == TriBool::False)
             state->setMouseExclusive(TriBool::True);
         else
-            state->setMouseExclusive(TriBool::Dontcare);
+            state->setMouseExclusive(TriBool::False);
 
-        if (showMouse == TriBool::True)
+        if (showCursor == TriBool::True)
             state->setMouseHandler(this);
-        else if (showMouse == TriBool::False)
+        else if (showCursor == TriBool::False)
             state->setMouseHandler(&InputHandler::EMPTY);
 
         if (useKeyboard == TriBool::True)

Modified: code/branches/gamestate/src/libraries/core/GUIManager.h
===================================================================
--- code/branches/gamestate/src/libraries/core/GUIManager.h	2010-03-30 10:13:33 UTC (rev 6657)
+++ code/branches/gamestate/src/libraries/core/GUIManager.h	2010-03-30 13:41:15 UTC (rev 6658)
@@ -80,7 +80,7 @@
         void keyESC();
         void setBackground(const std::string& name);
 
-        const std::string& createInputState(const std::string& name, TriBool::Value showMouse = TriBool::True, TriBool::Value useKeyboard = TriBool::True, bool bBlockJoyStick = false); // tolua_export
+        const std::string& createInputState(const std::string& name, TriBool::Value showCursor = TriBool::True, TriBool::Value useKeyboard = TriBool::True, bool bBlockJoyStick = false); // tolua_export
 
         void setCamera(Ogre::Camera* camera);
         Ogre::Camera* getCamera() { return this->camera_; }

Modified: code/branches/gamestate/src/libraries/core/input/InputManager.h
===================================================================
--- code/branches/gamestate/src/libraries/core/input/InputManager.h	2010-03-30 10:13:33 UTC (rev 6657)
+++ code/branches/gamestate/src/libraries/core/input/InputManager.h	2010-03-30 13:41:15 UTC (rev 6658)
@@ -167,7 +167,10 @@
             { return devices_.size() - InputDeviceEnumerator::FirstJoyStick; }
         //! Returns a pointer to the OIS InputManager. Only you if you know what you're doing!
         OIS::InputManager* getOISInputManager() { return this->oisInputManager_; }
+        //! Returns the position of the cursor as std::pair of ints
         std::pair<int, int> getMousePosition() const;
+        //! Tells whether the mouse is used exclusively to the game
+        bool isMouseExclusive() const { return this->exclusiveMouse_; } // tolua_export
 
         //-------------------------------
         // Function call caching

Modified: code/branches/gamestate/src/orxonox/gamestates/GSMainMenu.cc
===================================================================
--- code/branches/gamestate/src/orxonox/gamestates/GSMainMenu.cc	2010-03-30 10:13:33 UTC (rev 6657)
+++ code/branches/gamestate/src/orxonox/gamestates/GSMainMenu.cc	2010-03-30 13:41:15 UTC (rev 6658)
@@ -84,7 +84,7 @@
     void GSMainMenu::activate()
     {
         // show main menu
-        GUIManager::getInstance().showGUI("MainMenu", true, GraphicsManager::getInstance().isFullScreen());
+        GUIManager::getInstance().showGUI("MainMenu", true, true);
         GUIManager::getInstance().setCamera(this->camera_);
         GUIManager::getInstance().setBackground("MainMenuBackground");
 //         GUIManager::getInstance().setBackground("");




More information about the Orxonox-commit mailing list