Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 16, 2010, 2:50:16 PM (14 years ago)
Author:
rgrieder
Message:

Merged gamestates2 branch back to trunk.
This brings in some heavy changes in the GUI framework.
It should also fix problems with triggered asserts in the InputManager.

Note: PickupInventory does not seem to work —> Segfault when showing because before, the owner in GUIOverlay::setGUIName is already NULL.
I haven't tested it before, so I can't tell whether it's my changes.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/libraries/core/GUIManager.cc

    r6502 r6746  
    3030#include "GUIManager.h"
    3131
     32#include <boost/bind.hpp>
    3233#include <memory>
    3334extern "C" {
     
    4142#include <CEGUISystem.h>
    4243#include <CEGUIWindow.h>
     44#include <CEGUIWindowManager.h>
    4345#include <ogreceguirenderer/OgreCEGUIRenderer.h>
    4446
     
    5759#include "ConsoleCommand.h"
    5860#include "Core.h"
     61#include "GraphicsManager.h"
    5962#include "LuaState.h"
    6063#include "PathConfig.h"
    6164#include "Resource.h"
     65#include "input/InputManager.h"
     66#include "input/InputState.h"
     67#include "input/KeyBinderManager.h"
    6268
    6369namespace orxonox
     
    108114    @return true if success, otherwise false
    109115    */
    110     GUIManager::GUIManager(Ogre::RenderWindow* renderWindow, const std::pair<int, int>& mousePosition, bool bFullScreen)
    111         : renderWindow_(renderWindow)
    112         , resourceProvider_(0)
     116    GUIManager::GUIManager(const std::pair<int, int>& mousePosition)
     117        : resourceProvider_(NULL)
    113118        , camera_(NULL)
    114         , bShowIngameGUI_(false)
    115119    {
    116120        using namespace CEGUI;
     
    119123
    120124        // Note: No SceneManager specified yet
    121         guiRenderer_.reset(new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_OVERLAY, false, 3000));
     125        guiRenderer_.reset(new OgreCEGUIRenderer(GraphicsManager::getInstance().getRenderWindow(), Ogre::RENDER_QUEUE_OVERLAY, false, 3000));
    122126        resourceProvider_ = guiRenderer_->createResourceProvider();
    123127        resourceProvider_->setDefaultResourceGroup("GUI");
     
    141145        guiSystem_.reset(new System(guiRenderer_.get(), resourceProvider_, 0, scriptModule_.get()));
    142146
    143         // Initialise the basic Lua code
    144         this->luaState_->doFile("InitialiseGUI.lua");
    145 
    146147        // Align CEGUI mouse with OIS mouse
    147148        guiSystem_->injectMousePosition((float)mousePosition.first, (float)mousePosition.second);
    148149
    149         // Hide the mouse cursor unless playing in full screen mode
    150         if (!bFullScreen)
    151             CEGUI::MouseCursor::getSingleton().hide();
     150        // Initialise the Lua framework and load the schemes
     151        this->luaState_->doFile("InitialiseGUI.lua");
     152
     153        // Create the root nodes
     154        this->rootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("MenuWidgets/StaticImage", "AbsoluteRootWindow");
     155        this->rootWindow_->setProperty("FrameEnabled", "False");
     156        this->hudRootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "HUDRootWindow");
     157        this->menuRootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "MenuRootWindow");
     158        // And connect them
     159        CEGUI::System::getSingleton().setGUISheet(this->rootWindow_);
     160        this->rootWindow_->addChildWindow(this->hudRootWindow_);
     161        this->rootWindow_->addChildWindow(this->menuRootWindow_);
     162
     163        // Set up the sheet manager in the Lua framework
     164        this->luaState_->doFile("SheetManager.lua");
    152165    }
    153166
     
    173186    {
    174187        assert(guiSystem_);
    175         guiSystem_->injectTimePulse(time.getDeltaTime());
     188        this->protectedCall(boost::bind(&CEGUI::System::injectTimePulse, _1, time.getDeltaTime()));
    176189    }
    177190
     
    200213    @param str
    201214        reference to string object holding the Lua code which is to be executed
    202 
    203         This function gives total access to the GUI. You can execute ANY Lua code here.
    204215    */
    205216    void GUIManager::executeCode(const std::string& str)
    206217    {
    207218        this->luaState_->doString(str, rootFileInfo_);
     219    }
     220
     221    /** Loads a GUI sheet by Lua script
     222    @param name
     223        The name of the GUI (like the script name, but without the extension)
     224    */
     225    void GUIManager::loadGUI(const std::string& name)
     226    {
     227        this->executeCode("loadSheet(\"" + name + "\")");
    208228    }
    209229
     
    215235
    216236        The function executes the Lua function with the same name in case the GUIManager is ready.
    217         For more details check out loadGUI_2.lua where the function presides.
    218     */
    219     /*static*/ void GUIManager::showGUI(const std::string& name, bool hidePrevious, bool showCursor)
    220     {
    221         GUIManager::getInstance().executeCode("showGUI(\"" + name + "\", " + multi_cast<std::string>(hidePrevious) + ", " + multi_cast<std::string>(showCursor) + ")");
     237    */
     238    /*static*/ void GUIManager::showGUI(const std::string& name, bool bHidePrevious)
     239    {
     240        GUIManager::getInstance().executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ")");
    222241    }
    223242
     
    226245        Hack-ish. Needed for GUIOverlay.
    227246    */
    228     void GUIManager::showGUIExtra(const std::string& name, const std::string& ptr, bool hidePrevious, bool showCursor)
    229     {
    230         this->executeCode("showGUI(\"" + name + "\", " + multi_cast<std::string>(hidePrevious) + ", " + multi_cast<std::string>(showCursor) + ", " + ptr + ")");
     247    void GUIManager::showGUIExtra(const std::string& name, const std::string& ptr, bool bHidePrevious)
     248    {
     249        this->executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + ptr + ")");
    231250    }
    232251
     
    239258    /*static*/ void GUIManager::hideGUI(const std::string& name)
    240259    {
    241         GUIManager::getInstance().executeCode("hideGUI(\"" + name + "\")");
     260        GUIManager::getInstance().executeCode("hideMenuSheet(\"" + name + "\")");
     261    }
     262
     263    const std::string& GUIManager::createInputState(const std::string& name, TriBool::Value showCursor, TriBool::Value useKeyboard, bool bBlockJoyStick)
     264    {
     265        InputState* state = InputManager::getInstance().createInputState(name);
     266
     267        /* Table that maps isFullScreen() and showCursor to mouseExclusive
     268        isFullscreen / showCursor | True  | False | Dontcare
     269        ----------------------------------------------------
     270        true                      | True  | True  | Dontcare
     271        ----------------------------------------------------
     272        false                     | False | True  | Dontcare
     273        */
     274        if (showCursor == TriBool::Dontcare)
     275            state->setMouseExclusive(TriBool::Dontcare);
     276        else if (GraphicsManager::getInstance().isFullScreen() || showCursor == TriBool::False)
     277            state->setMouseExclusive(TriBool::True);
     278        else
     279            state->setMouseExclusive(TriBool::False);
     280
     281        if (showCursor == TriBool::True)
     282            state->setMouseHandler(this);
     283        else if (showCursor == TriBool::False)
     284            state->setMouseHandler(&InputHandler::EMPTY);
     285
     286        if (useKeyboard == TriBool::True)
     287            state->setKeyHandler(this);
     288        else if (useKeyboard == TriBool::False)
     289            state->setKeyHandler(&InputHandler::EMPTY);
     290
     291        if (bBlockJoyStick)
     292            state->setJoyStickHandler(&InputHandler::EMPTY);
     293
     294        return state->getName();
    242295    }
    243296
     
    247300    }
    248301
    249     void GUIManager::setBackground(const std::string& name)
    250     {
    251         this->executeCode("setBackground(\"" + name + "\")");
     302    void GUIManager::setBackgroundImage(const std::string& imageSet, const std::string imageName)
     303    {
     304        if (imageSet.empty() || imageName.empty())
     305            this->setBackgroundImage("");
     306        else
     307            this->setBackgroundImage("set: " + imageSet + " image: " + imageName);
     308    }
     309
     310    void GUIManager::setBackgroundImage(const std::string& image)
     311    {
     312        if (image.empty())
     313            this->rootWindow_->setProperty("Alpha", "0.0");
     314        else
     315            this->rootWindow_->setProperty("Alpha", "1.0");
     316        this->rootWindow_->setProperty("Image", image);
    252317    }
    253318
    254319    void GUIManager::keyPressed(const KeyEvent& evt)
    255320    {
    256         guiSystem_->injectKeyDown(evt.getKeyCode());
    257         guiSystem_->injectChar(evt.getText());
    258     }
     321        this->protectedCall(boost::bind(&CEGUI::System::injectKeyDown, _1, evt.getKeyCode()));
     322        this->protectedCall(boost::bind(&CEGUI::System::injectChar, _1, evt.getText()));
     323    }
     324
    259325    void GUIManager::keyReleased(const KeyEvent& evt)
    260326    {
    261         guiSystem_->injectKeyUp(evt.getKeyCode());
     327        this->protectedCall(boost::bind(&CEGUI::System::injectKeyUp, _1, evt.getKeyCode()));
    262328    }
    263329
     
    273339    void GUIManager::buttonPressed(MouseButtonCode::ByEnum id)
    274340    {
    275         try
    276         {
    277             guiSystem_->injectMouseButtonDown(convertButton(id));
    278         }
    279         catch (CEGUI::ScriptException& ex)
    280         {
    281             // We simply ignore the exception and proceed
    282             COUT(1) << ex.getMessage() << std::endl;
    283         }
     341        this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonDown, _1, convertButton(id)));
    284342    }
    285343
     
    295353    void GUIManager::buttonReleased(MouseButtonCode::ByEnum id)
    296354    {
    297         try
    298         {
    299             guiSystem_->injectMouseButtonUp(convertButton(id));
    300         }
    301         catch (CEGUI::ScriptException& ex)
    302         {
    303             // We simply ignore the exception and proceed
    304             COUT(1) << ex.getMessage() << std::endl;
    305         }
     355        this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonUp, _1, convertButton(id)));
    306356    }
    307357
    308358    void GUIManager::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
    309359    {
    310         guiSystem_->injectMousePosition(static_cast<float>(abs.x), static_cast<float>(abs.y));
    311     }
     360        this->protectedCall(boost::bind(&CEGUI::System::injectMousePosition, _1, (float)abs.x, (float)abs.y));
     361    }
     362
    312363    void GUIManager::mouseScrolled(int abs, int rel)
    313364    {
    314         guiSystem_->injectMouseWheelChange(static_cast<float>(rel));
     365        this->protectedCall(boost::bind(&CEGUI::System::injectMouseWheelChange, _1, (float)rel));
    315366    }
    316367
     
    349400    }
    350401
     402    /** Executes a CEGUI function normally, but catches CEGUI::ScriptException.
     403        When a ScriptException occurs, the error message will be displayed and
     404        the program carries on.
     405    @remarks
     406        The exception behaviour may pose problems if the code is not written
     407        exception-safe (and you can forget about that in Lua). The program might
     408        be left in an undefined state. But otherwise one script error would
     409        terminate the whole program...
     410    @note
     411        Your life gets easier if you use boost::bind to create the object/function.
     412    @param function
     413        Any callable object/function that takes this->guiSystem_ as its only parameter.
     414    @return
     415        True if input was handled, false otherwise. A caught exception yields true.
     416    */
     417    template <typename FunctionType>
     418    bool GUIManager::protectedCall(FunctionType function)
     419    {
     420        try
     421        {
     422            return function(this->guiSystem_);
     423        }
     424        catch (CEGUI::ScriptException& ex)
     425        {
     426            // Display the error and proceed. See @remarks why this can be dangerous.
     427            COUT(1) << ex.getMessage() << std::endl;
     428            return true;
     429        }
     430    }
     431
    351432    void GUIManager::subscribeEventHelper(CEGUI::Window* window, const std::string& event, const std::string& function)
    352433    {
Note: See TracChangeset for help on using the changeset viewer.