Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 23, 2008, 3:37:29 PM (16 years ago)
Author:
rgrieder
Message:
  • changed the static interface of the InputManager to a member one with getInstance()
  • converted InputManager and InGameConsole to Ogre Singletons (c'tor and d'tor public, but assert in c'tor to prevent multiple instances at runtime)
  • added toluabind_orxonox files to tolua folder that contains a pimped version of tolua (I'll write a little cmake project soon to automate it; Currently that only works with msvc)
  • commented out Loader::unload() from Orxonox destructor because that deleted the ParticleSpawners, which were using a method of a sceneNode that belonged to a already destroyed SpaceShip. —> Results in a memory leak. Previously Loader::unload() was called for all BaseObjects (now calling unload(level_)). And since 'P' from ParticleSpawner comes before 'S' like SpaceShip, the order was correct.
  • Added factory feature for InputStates (can now be created by string if there is Factory entry for it)
  • Created factory entries for SimpleInputState and ExtendedInputState
Location:
code/branches/gui/src/core
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • code/branches/gui/src/core/Core.cc

    r1638 r1642  
    196196    {
    197197        TclThreadManager::getInstance().tick(dt);
    198         InputManager::tick(dt);
     198        InputManager::getInstance().tick(dt);
    199199    }
    200200}
  • code/branches/gui/src/core/Exception.h

    r1638 r1642  
    4040#include <string>
    4141#include <exception>
     42#include <cassert>
    4243#include "core/Debug.h"
    4344
     
    129130    throw SpecificException<Exception::type>(description, __LINE__, __FILE__, __FUNCTIONNAME__)
    130131
     132    // define an assert macro that can display a message
     133#ifndef NDEBUG
     134#define OrxAssert(condition, errorMessage) \
     135    condition ? ((void)0) : (orxonox::OutputHandler::getOutStream().setOutputLevel(ORX_ERROR) << errorMessage << std::endl); \
     136    assert(condition);
     137#else
     138#define OrxAssert(condition, errorMessage)  ((void)0)
     139#endif
     140
    131141}
    132142
  • code/branches/gui/src/core/input/ExtendedInputState.cc

    r1641 r1642  
    3737#include <assert.h>
    3838#include "core/Debug.h"
     39#include "core/CoreIncludes.h"
    3940
    4041namespace orxonox
    4142{
     43    CreateFactory(ExtendedInputState);
     44
    4245    using namespace InputDevice;
     46
     47    ExtendedInputState::ExtendedInputState()
     48    {
     49        RegisterObject(ExtendedInputState);
     50    }
    4351
    4452    void ExtendedInputState::numberOfJoySticksChanged(unsigned int n)
  • code/branches/gui/src/core/input/ExtendedInputState.h

    r1641 r1642  
    4848    {
    4949    public:
    50         ExtendedInputState() { }
     50        ExtendedInputState();
    5151        ~ExtendedInputState() { }
    5252
  • code/branches/gui/src/core/input/InputManager.cc

    r1641 r1642  
    6262    SetConsoleCommandShortcut(InputManager, calibrate);
    6363
     64    std::string InputManager::bindingCommmandString_s = "";
     65    InputManager* InputManager::singletonRef_s = 0;
     66
    6467    using namespace InputDevice;
    6568
    66     // ###############################
    67     // ###    Internal Methods     ###
    68     // ###############################
    69     // ###############################
     69    // ############################################################
     70    // #####                  Initialisation                  #####
     71    // ##########                                        ##########
     72    // ############################################################
    7073
    7174    /**
     
    8790    {
    8891        RegisterRootObject(InputManager);
    89     }
    90 
    91     /**
    92     @brief
    93         Destructor itself only called at the end of the program, after main.
    94         Instance gets registered for destruction with atexit(.).
    95     */
    96     InputManager::~InputManager()
    97     {
    98         _destroy();
    99     }
    100 
    101     /**
    102     @brief
    103         The one instance of the InputManager is stored in this function.
    104         Only for internal use. Public Interface ist static.
    105     @return
    106         A reference to the only instance of the InputManager
    107     */
    108     InputManager& InputManager::_getInstance()
    109     {
    110         static InputManager theOnlyInstance;
    111         return theOnlyInstance;
    112     }
    113 
    114 
    115     // ############################################################
    116     // #####                  Initialisation                  #####
    117     // ##########                                        ##########
    118     // ############################################################
     92
     93        assert(singletonRef_s == 0);
     94        singletonRef_s = this;
     95    }
    11996
    12097    /**
     
    129106        The height of the render window
    130107    */
    131     bool InputManager::_initialise(const size_t windowHnd, int windowWidth, int windowHeight,
     108    bool InputManager::initialise(const size_t windowHnd, int windowWidth, int windowHeight,
    132109                                   bool createKeyboard, bool createMouse, bool createJoySticks)
    133110    {
     
    431408    /**
    432409    @brief
    433         Destroys all the created input devices. InputManager will be ready for a
    434         new initialisation.
    435     */
    436     void InputManager::_destroy()
     410        Destroys all the created input devices and states.
     411    */
     412    InputManager::~InputManager()
    437413    {
    438414        if (inputSystem_)
     
    550526        Delta time
    551527    */
    552     void InputManager::_tick(float dt)
     528    void InputManager::tick(float dt)
    553529    {
    554530        if (inputSystem_ == 0)
     
    983959
    984960    // ############################################################
    985     // #####            Static Interface Methods              #####
     961    // #####         Other Public Interface Methods           #####
    986962    // ##########                                        ##########
    987963    // ############################################################
    988 
    989     std::string InputManager::bindingCommmandString_s = "";
    990 
    991     bool InputManager::initialise(const size_t windowHnd, int windowWidth, int windowHeight,
    992         bool createKeyboard, bool createMouse, bool createJoySticks)
    993     {
    994         return _getInstance()._initialise(windowHnd, windowWidth, windowHeight,
    995             createKeyboard, createMouse, createJoySticks);
    996     }
    997 
    998     int InputManager::numberOfKeyboards()
    999     {
    1000         if (_getInstance().keyboard_ != 0)
    1001             return 1;
    1002         else
    1003             return 0;
    1004     }
    1005 
    1006     int InputManager::numberOfMice()
    1007     {
    1008         if (_getInstance().mouse_ != 0)
    1009             return 1;
    1010         else
    1011             return 0;
    1012     }
    1013 
    1014     int InputManager::numberOfJoySticks()
    1015     {
    1016         return _getInstance().joySticksSize_;
    1017     }
    1018 
    1019     /*bool InputManager::isKeyDown(KeyCode::Enum key)
    1020     {
    1021     if (_getInstance().keyboard_)
    1022         return _getInstance().keyboard_->isKeyDown((OIS::KeyCode)key);
    1023     else
    1024         return false;
    1025     }*/
    1026 
    1027     /*bool InputManager::isModifierDown(KeyboardModifier::Enum modifier)
    1028     {
    1029     if (_getInstance().keyboard_)
    1030         return isModifierDown(modifier);
    1031     else
    1032         return false;
    1033     }*/
    1034 
    1035     /*const MouseState InputManager::getMouseState()
    1036     {
    1037     if (_getInstance().mouse_)
    1038         return _getInstance().mouse_->getMouseState();
    1039     else
    1040         return MouseState();
    1041     }*/
    1042 
    1043     /*const JoyStickState InputManager::getJoyStickState(unsigned int ID)
    1044     {
    1045     if (ID < _getInstance().joySticksSize_)
    1046         return JoyStickState(_getInstance().joySticks_[ID]->getJoyStickState(), ID);
    1047     else
    1048         return JoyStickState();
    1049     }*/
    1050 
    1051     void InputManager::destroy()
    1052     {
    1053         _getInstance()._destroy();
    1054     }
    1055 
    1056964
    1057965    /**
     
    1066974    void InputManager::setWindowExtents(const int width, const int height)
    1067975    {
    1068         if (_getInstance().mouse_)
     976        if (mouse_)
    1069977        {
    1070978            // Set mouse region (if window resizes, we should alter this to reflect as well)
    1071             const OIS::MouseState &mouseState = _getInstance().mouse_->getMouseState();
    1072             mouseState.width  = width;
    1073             mouseState.height = height;
    1074         }
    1075     }
    1076 
    1077     /**
    1078     @brief
    1079         Method for easily storing a string with the command executor. It is used by the
    1080         KeyDetector to get assign commands. The KeyDetector simply executes
    1081         the command 'storeKeyStroke myName' for each button/axis.
    1082     @remarks
    1083         This is only a temporary hack until we thourouhgly support multiple KeyBinders.
    1084     @param name
    1085         The name of the button/axis.
    1086     */
    1087     void InputManager::storeKeyStroke(const std::string& name)
    1088     {
    1089         requestLeaveState("detector");
    1090         COUT(0) << "Binding string \"" << bindingCommmandString_s << "\" on key '" << name << "'" << std::endl;
    1091         CommandExecutor::execute("config KeyBinder " + name + " " + bindingCommmandString_s, false);
    1092     }
    1093 
    1094     /**
    1095     @brief
    1096         Assigns a command string to a key/button/axis. The name is determined via KeyDetector
    1097         and InputManager::storeKeyStroke(.).
    1098     @param command
    1099         Command string that can be executed by the CommandExecutor
    1100     */
    1101     void InputManager::keyBind(const std::string& command)
    1102     {
    1103         bindingCommmandString_s = command;
    1104         requestEnterState("detector");
    1105         COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
    1106     }
    1107 
    1108     /**
    1109     @brief
    1110         Starts joy stick calibration.
    1111     */
    1112     void InputManager::calibrate()
    1113     {
    1114         _getInstance().bCalibrating_ = true;
    1115         requestEnterState("calibrator");
    1116     }
    1117 
    1118     void InputManager::tick(float dt)
    1119     {
    1120         _getInstance()._tick(dt);
    1121     }
     979            mouse_->getMouseState().width  = width;
     980            mouse_->getMouseState().height = height;
     981        }
     982    }
     983
    1122984
    1123985    // ###### InputStates ######
     
    11391001        if (name == "")
    11401002            return false;
    1141         if (_getInstance().inputStatesByName_.find(name) == _getInstance().inputStatesByName_.end())
    1142         {
    1143             if (_getInstance().inputStatesByPriority_.find(priority)
    1144                 == _getInstance().inputStatesByPriority_.end())
    1145             {
    1146                 _getInstance().inputStatesByName_[name] = state;
    1147                 _getInstance().inputStatesByPriority_[priority] = state;
     1003        if (!state)
     1004            return false;
     1005        if (inputStatesByName_.find(name) == inputStatesByName_.end())
     1006        {
     1007            if (inputStatesByPriority_.find(priority)
     1008                == inputStatesByPriority_.end())
     1009            {
     1010                inputStatesByName_[name] = state;
     1011                inputStatesByPriority_[priority] = state;
    11481012                state->setNumOfJoySticks(numberOfJoySticks());
    11491013                state->setName(name);
     
    11721036    {
    11731037        SimpleInputState* state = new SimpleInputState();
    1174         if (_getInstance()._configureInputState(state, name, priority))
     1038        if (_configureInputState(state, name, priority))
    11751039            return state;
    11761040        else
     
    11881052    {
    11891053        ExtendedInputState* state = new ExtendedInputState();
    1190         if (_getInstance()._configureInputState(state, name, priority))
     1054        if (_configureInputState(state, name, priority))
     1055            return state;
     1056        else
     1057        {
     1058            delete state;
     1059            return 0;
     1060        }
     1061    }
     1062
     1063    /**
     1064    @brief
     1065        Returns a new InputState of type 'type' and configures it first.
     1066    @param type
     1067        String name of the class (used by the factory)
     1068    */
     1069    InputState* InputManager::createInputState(const std::string& type, const std::string &name, int priority)
     1070    {
     1071        InputState* state = dynamic_cast<InputState*>(Factory::getIdentifier(type)->fabricate());
     1072        if (_configureInputState(state, name, priority))
    11911073            return state;
    11921074        else
     
    12141096            return false;
    12151097        }
    1216         std::map<std::string, InputState*>::iterator it = _getInstance().inputStatesByName_.find(name);
    1217         if (it != _getInstance().inputStatesByName_.end())
    1218         {
    1219             _getInstance()._destroyState((*it).second);
     1098        std::map<std::string, InputState*>::iterator it = inputStatesByName_.find(name);
     1099        if (it != inputStatesByName_.end())
     1100        {
     1101            _destroyState((*it).second);
    12201102            return true;
    12211103        }
     
    12331115    InputState* InputManager::getState(const std::string& name)
    12341116    {
    1235         std::map<std::string, InputState*>::iterator it = _getInstance().inputStatesByName_.find(name);
    1236         if (it != _getInstance().inputStatesByName_.end())
     1117        std::map<std::string, InputState*>::iterator it = inputStatesByName_.find(name);
     1118        if (it != inputStatesByName_.end())
    12371119            return (*it).second;
    12381120        else
     
    12481130    InputState* InputManager::getCurrentState()
    12491131    {
    1250         return (*_getInstance().activeStates_.rbegin()).second;
     1132        return (*activeStates_.rbegin()).second;
    12511133    }
    12521134
     
    12631145    {
    12641146        // get pointer from the map with all stored handlers
    1265         std::map<std::string, InputState*>::const_iterator it = _getInstance().inputStatesByName_.find(name);
    1266         if (it != _getInstance().inputStatesByName_.end())
    1267         {
    1268             _getInstance().stateEnterRequests_.push_back((*it).second);
     1147        std::map<std::string, InputState*>::const_iterator it = inputStatesByName_.find(name);
     1148        if (it != inputStatesByName_.end())
     1149        {
     1150            stateEnterRequests_.push_back((*it).second);
    12691151            return true;
    12701152        }
     
    12831165    {
    12841166        // get pointer from the map with all stored handlers
    1285         std::map<std::string, InputState*>::const_iterator it = _getInstance().inputStatesByName_.find(name);
    1286         if (it != _getInstance().inputStatesByName_.end())
    1287         {
    1288             _getInstance().stateLeaveRequests_.push_back((*it).second);
     1167        std::map<std::string, InputState*>::const_iterator it = inputStatesByName_.find(name);
     1168        if (it != inputStatesByName_.end())
     1169        {
     1170            stateLeaveRequests_.push_back((*it).second);
    12891171            return true;
    12901172        }
    12911173        return false;
    12921174    }
     1175
     1176
     1177    // ############################################################
     1178    // #####                Console Commands                  #####
     1179    // ##########                                        ##########
     1180    // ############################################################
     1181
     1182    /**
     1183    @brief
     1184        Method for easily storing a string with the command executor. It is used by the
     1185        KeyDetector to get assign commands. The KeyDetector simply executes
     1186        the command 'storeKeyStroke myName' for each button/axis.
     1187    @remarks
     1188        This is only a temporary hack until we thourouhgly support multiple KeyBinders.
     1189    @param name
     1190        The name of the button/axis.
     1191    */
     1192    void InputManager::storeKeyStroke(const std::string& name)
     1193    {
     1194        getInstance().requestLeaveState("detector");
     1195        COUT(0) << "Binding string \"" << bindingCommmandString_s << "\" on key '" << name << "'" << std::endl;
     1196        CommandExecutor::execute("config KeyBinder " + name + " " + bindingCommmandString_s, false);
     1197    }
     1198
     1199    /**
     1200    @brief
     1201        Assigns a command string to a key/button/axis. The name is determined via KeyDetector
     1202        and InputManager::storeKeyStroke(.).
     1203    @param command
     1204        Command string that can be executed by the CommandExecutor
     1205    */
     1206    void InputManager::keyBind(const std::string& command)
     1207    {
     1208        bindingCommmandString_s = command;
     1209        getInstance().requestEnterState("detector");
     1210        COUT(0) << "Press any button/key or move a mouse/joystick axis" << std::endl;
     1211    }
     1212
     1213    /**
     1214    @brief
     1215        Starts joy stick calibration.
     1216    */
     1217    void InputManager::calibrate()
     1218    {
     1219        getInstance().bCalibrating_ = true;
     1220        getInstance().requestEnterState("calibrator");
     1221    }
    12931222}
  • code/branches/gui/src/core/input/InputManager.h

    r1641 r1642  
    8686        // --> setConfigValues is private
    8787        friend ClassIdentifier<InputManager>;
    88 
    89     public: // static functions
    90         static bool initialise(const size_t windowHnd, int windowWidth, int windowHeight,
     88        // let Core class use tick(.)
     89        friend Core;
     90
     91    public:
     92        InputManager ();
     93        ~InputManager();
     94
     95        bool initialise(const size_t windowHnd, int windowWidth, int windowHeight,
    9196                               bool createKeyboard = true, bool createMouse = true, bool createJoySticks = false);
    92         static int  numberOfKeyboards();
    93         static int  numberOfMice();
    94         static int  numberOfJoySticks();
    95 
    96         static void destroy();
    97 
    98         //static bool isModifierDown(KeyboardModifier::Enum modifier);
    99         //static bool isKeyDown(KeyCode::Enum key);
    100         //static const MouseState getMouseState();
    101         //static const JoyStickState getJoyStickState(unsigned int ID);
    102 
    103         static void setWindowExtents(const int width, const int height);
    104 
     97
     98        int  numberOfKeyboards() { return keyboard_ ? 1 : 0; }
     99        int  numberOfMice()      { return mouse_    ? 1 : 0; }
     100        int  numberOfJoySticks() { return joySticksSize_; }
     101
     102        void setWindowExtents(const int width, const int height);
     103
     104        SimpleInputState*   createSimpleInputState  (const std::string& name, int priority);
     105        ExtendedInputState* createExtendedInputState(const std::string& name, int priority);
     106        InputState*         createInputState(const std::string& type, const std::string &name, int priority);
     107        bool destroyState          (const std::string& name);
     108        InputState* getState       (const std::string& name);
     109        InputState* getCurrentState();
     110        bool requestEnterState     (const std::string& name);
     111        bool requestLeaveState     (const std::string& name);
     112
     113        static InputManager& getInstance()    { assert(singletonRef_s); return *singletonRef_s; }
     114        static InputManager* getInstancePtr() { return singletonRef_s; }
     115
     116    public: // console commands
    105117        static void storeKeyStroke(const std::string& name);
    106118        static void keyBind(const std::string& command);
    107 
    108119        static void calibrate();
    109 
    110         static void tick(float dt);
    111 
    112         static SimpleInputState*   createSimpleInputState  (const std::string& name, int priority);
    113         static ExtendedInputState* createExtendedInputState(const std::string& name, int priority);
    114         static bool destroyState (const std::string& name);
    115         static InputState* getState       (const std::string& name);
    116         static InputState* getCurrentState();
    117         static bool requestEnterState     (const std::string& name);
    118         static bool requestLeaveState     (const std::string& name);
    119120
    120121    private: // functions
    121122        // don't mess with a Singleton
    122         InputManager ();
    123123        InputManager (const InputManager&);
    124         ~InputManager();
    125124
    126125        // Intenal methods
    127         bool _initialise(const size_t, int, int, bool, bool, bool);
    128126        bool _initialiseKeyboard();
    129127        bool _initialiseMouse();
     
    131129        void _redimensionLists();
    132130
    133         void _destroy();
    134131        void _destroyKeyboard();
    135132        void _destroyMouse();
     
    142139        unsigned int _getJoystick(const OIS::JoyStickEvent& arg);
    143140
    144         void _tick(float dt);
    145 
    146141        void _updateActiveStates();
    147142        bool _configureInputState(InputState* state, const std::string& name, int priority);
     143
     144        void tick(float dt);
    148145
    149146        // input events
     
    158155        bool sliderMoved   (const OIS::JoyStickEvent &arg, int id);
    159156        bool povMoved      (const OIS::JoyStickEvent &arg, int id);
     157        // don't remove that! Or else add OIS as dependency library to orxonox.
     158        bool vector3Moved  (const OIS::JoyStickEvent &arg, int id) { return true; }
    160159
    161160        void setConfigValues();
    162 
    163         static InputManager& _getInstance();
    164161
    165162    private: // variables
     
    204201
    205202        static std::string                  bindingCommmandString_s;
    206     };
    207 
     203        static InputManager*                singletonRef_s;
     204    };
    208205}
    209206
  • code/branches/gui/src/core/input/InputState.h

    r1641 r1642  
    4040#include <vector>
    4141#include "core/Executor.h"
     42#include "core/BaseObject.h"
     43#include "core/CoreIncludes.h"
    4244#include "InputInterfaces.h"
    4345
    4446namespace orxonox
    4547{
    46     class _CoreExport InputState
     48    class _CoreExport InputState : public BaseObject
    4749    {
    4850        friend class InputManager;
    4951
    5052    public:
    51         InputState() : priority_(0), executorOnEnter_(0), executorOnLeave_(0) { }
     53        InputState() : priority_(0), executorOnEnter_(0), executorOnLeave_(0)
     54        { RegisterObject(InputState); }
    5255        virtual ~InputState() { }
    5356
  • code/branches/gui/src/core/input/SimpleInputState.cc

    r1641 r1642  
    3838#include "core/Debug.h"
    3939#include "core/Executor.h"
     40#include "core/CoreIncludes.h"
    4041
    4142namespace orxonox
    4243{
     44    CreateFactory(SimpleInputState);
     45
    4346    using namespace InputDevice;
    4447
     
    4851        , joyStickHandlerAll_(0)
    4952    {
     53        RegisterObject(SimpleInputState);
    5054    }
    5155
  • code/branches/gui/src/core/input/SimpleInputState.h

    r1641 r1642  
    4343namespace orxonox
    4444{
    45 
    4645    class _CoreExport SimpleInputState : public InputState
    4746    {
    48 
    4947    public:
    5048        SimpleInputState();
Note: See TracChangeset for help on using the changeset viewer.