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
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.