Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 6, 2008, 12:31:32 AM (16 years ago)
Author:
rgrieder
Message:

FIRST THINGS FIRST: Delete or rename your keybindings.ini (def_keybindings.ini already has the most important bindings) or else you won't be able to do anything!

Changes:

  • Multiple joy stick support should now fully work with KeyBinder too (only tested with 0/1 joystick)
  • Reloading the OIS Devices now works with KeyBinder too
  • Modified ConfigValueContainer to accept arbitrary section names
  • added tkeybind to temporary bind a command to a key
  • Fixed dlleport issue in ArgumentCompletionFunctions.h

Internal changes:

  • General cleanup in initialisation of KeyBinder
  • All names of keys/buttons/axes are now statically saved in InputInterfaces.h
  • Move a magic value in KeyBinder to a configValue (MouseWheelStepSize_)
  • Separated ConfigValues from Keybinding ConfigValueContainer in KeyBinder (looks much nicer now ;))
  • Moved some performance critical small function to the inline section
  • Removed the ugly keybind function construct from the InputManager
  • More 'harmonising' work in KeyBinder
File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/core/input/KeyBinder.cc

    r1795 r1887  
    4141#include "core/ConfigFileManager.h"
    4242#include "InputCommands.h"
     43#include "InputManager.h"
    4344
    4445namespace orxonox
     
    4950    */
    5051    KeyBinder::KeyBinder()
    51         : deriveTime_(0.0f)
     52        : numberOfJoySticks_(0)
     53        , deriveTime_(0.0f)
    5254    {
    5355        mouseRelative_[0] = 0;
     
    5860        RegisterRootObject(KeyBinder);
    5961
     62        // intialise all buttons and half axes to avoid creating everything with 'new'
    6063        // keys
    61         std::string keyNames[] = {
    62         "UNASSIGNED",
    63         "ESCAPE",
    64         "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
    65         "MINUS", "EQUALS", "BACK", "TAB",
    66         "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P",
    67         "LBRACKET", "RBRACKET",
    68         "RETURN", "LCONTROL",
    69         "A", "S", "D", "F", "G", "H", "J", "K", "L",
    70         "SEMICOLON", "APOSTROPHE", "GRAVE",
    71         "LSHIFT", "BACKSLASH",
    72         "Z", "X", "C", "V", "B", "N", "M",
    73         "COMMA", "PERIOD", "SLASH",
    74         "RSHIFT",
    75         "MULTIPLY",
    76         "LMENU",
    77         "SPACE",
    78         "CAPITAL",
    79         "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10",
    80         "NUMLOCK", "SCROLL",
    81         "NUMPAD7", "NUMPAD8", "NUMPAD9",
    82         "SUBTRACT",
    83         "NUMPAD4", "NUMPAD5", "NUMPAD6",
    84         "ADD",
    85         "NUMPAD1", "NUMPAD2", "NUMPAD3", "NUMPAD0",
    86         "DECIMAL",
    87         "","",
    88         "OEM_102",
    89         "F11", "F12",
    90         "","","","","","","","","","","",
    91         "F13", "F14", "F15",
    92         "","","","","","","","","","",
    93         "KANA",
    94         "","",
    95         "ABNT_C1",
    96         "","","","","",
    97         "CONVERT",
    98         "",
    99         "NOCONVERT",
    100         "",
    101         "YEN",
    102         "ABNT_C2",
    103         "","","","","","","","","","","","","","",
    104         "NUMPADEQUALS",
    105         "","",
    106         "PREVTRACK",
    107         "AT",
    108         "COLON", "UNDERLINE",
    109         "KANJI",
    110         "STOP",
    111         "AX",
    112         "UNLABELED",
    113         "NEXTTRACK",
    114         "","",
    115         "NUMPADENTER",
    116         "RCONTROL",
    117         "","",
    118         "MUTE",
    119         "CALCULATOR",
    120         "PLAYPAUSE",
    121         "",
    122         "MEDIASTOP",
    123         "","","","","","","","","",
    124         "VOLUMEDOWN",
    125         "",
    126         "VOLUMEUP",
    127         "",
    128         "WEBHOME",
    129         "NUMPADCOMMA",
    130         "",
    131         "DIVIDE",
    132         "",
    133         "SYSRQ",
    134         "RMENU",
    135         "","","","","","","","","","","","",
    136         "PAUSE",
    137         "",
    138         "HOME",
    139         "UP",
    140         "PGUP",
    141         "",
    142         "LEFT",
    143         "",
    144         "RIGHT",
    145         "",
    146         "END", "DOWN", "PGDOWN", "INSERT", "DELETE",
    147         "","","","","","","",
    148         "LWIN", "RWIN", "APPS",
    149         "POWER", "SLEEP",
    150         "","","",
    151         "WAKE",
    152         "",
    153         "WEBSEARCH", "WEBFAVORITES", "WEBREFRESH", "WEBSTOP", "WEBFORWARD", "WEBBACK",
    154         "MYCOMPUTER", "MAIL", "MEDIASELECT"
    155         };
    156         for (unsigned int i = 0; i < nKeys_s; i++)
    157             keys_[i].name_ = "Key" + keyNames[i];
    158 
    159         // mouse buttons
    160         std::string mouseButtonNames[] = {
    161             "MouseLeft",     "MouseRight",   "MouseMiddle",
    162             "MouseButton3",  "MouseButton4", "MouseButton5",
    163             "MouseButton6",  "MouseButton7",
    164             "MouseWheel1Up", "MouseWheel1Down",
    165             "MouseWheel2Up", "MouseWheel2Down"
    166         };
    167         for (unsigned int i = 0; i < nMouseButtons_s; i++)
    168             mouseButtons_[i].name_ = mouseButtonNames[i];
    169 
    170         // joy stick buttons
    171         for (unsigned int i = 0; i < 32; i++)
    172             joyStickButtons_[i].name_ = "JoyButton" + getConvertedValue<int, std::string>(i);
    173         for (unsigned int i = 32; i < nJoyStickButtons_s; i += 4)
    174         {
    175             joyStickButtons_[i + 0].name_ = "JoyPOV" + convertToString((i - 32)/4 + 1) + "North";
    176             joyStickButtons_[i + 1].name_ = "JoyPOV" + convertToString((i - 32)/4 + 1) + "South";
    177             joyStickButtons_[i + 2].name_ = "JoyPOV" + convertToString((i - 32)/4 + 1) + "East";
    178             joyStickButtons_[i + 3].name_ = "JoyPOV" + convertToString((i - 32)/4 + 1) + "West";
    179         }
    180 
    181         // half axes
    182         std::string rawNames[nHalfAxes_s/2];
    183         rawNames[0] = "MouseX";
    184         rawNames[1] = "MouseY";
    185         rawNames[2] = "Empty1";
    186         rawNames[3] = "Empty2";
    187         for (unsigned int i = 4; i < nHalfAxes_s/2; i++)
    188             rawNames[i] = "JoyAxis" + convertToString(i - 3);
    189         for (unsigned int i = 0; i < nHalfAxes_s/2; i++)
    190         {
    191             halfAxes_[i * 2 + 0].name_ = rawNames[i] + "Pos";
    192             halfAxes_[i * 2 + 1].name_ = rawNames[i] + "Neg";
    193         }
    194 
    195         for (unsigned int i = 0; i < this->nHalfAxes_s; i++)
    196             halfAxes_[i].buttonThreshold_ = buttonThreshold_;
     64        for (unsigned int i = 0; i < KeyCode::numberOfKeys; i++)
     65        {
     66            std::string keyname = KeyCode::ByString[i];
     67            if (!keyname.empty())
     68            {
     69                keys_[i].name_ = std::string("Key") + keyname;
     70            }
     71            else
     72            {
     73                // some keys have name "" because the code is not occupied by OIS
     74                // Use "Key_" plus the number as name to put it at the end of the config file section
     75                std::string number = convertToString(i);
     76                if (i < 100)
     77                    number.insert(0, "0");
     78                keys_[i].name_ = std::string("Key_") + number;
     79            }
     80            keys_[i].paramCommandBuffer_ = &paramCommandBuffer_;
     81            keys_[i].groupName_ = "Keys";
     82        }
     83        // mouse buttons plus 4 mouse wheel buttons only 'generated' by KeyBinder
     84        const char* const mouseWheelNames[] = { "Wheel1Down", "Wheel1Up", "Wheel2Down", "Wheel2Up" };
     85        for (unsigned int i = 0; i < numberOfMouseButtons_; i++)
     86        {
     87            std::string nameSuffix;
     88            if (i < MouseButtonCode::numberOfButtons)
     89                nameSuffix = MouseButtonCode::ByString[i];
     90            else
     91                nameSuffix = mouseWheelNames[i - MouseButtonCode::numberOfButtons];
     92            mouseButtons_[i].name_ = std::string("Mouse") + nameSuffix;
     93            mouseButtons_[i].paramCommandBuffer_ = &paramCommandBuffer_;
     94            mouseButtons_[i].groupName_ = "MouseButtons";
     95        }
     96        // mouse axes
     97        for (unsigned int i = 0; i < MouseAxisCode::numberOfAxes * 2; i++)
     98        {
     99            mouseAxes_[i].name_ = std::string("Mouse") + MouseAxisCode::ByString[i >> 1];
     100            if (i & 1)
     101                mouseAxes_[i].name_ += "Pos";
     102            else
     103                mouseAxes_[i].name_ += "Neg";
     104            mouseAxes_[i].paramCommandBuffer_ = &paramCommandBuffer_;
     105            mouseAxes_[i].groupName_ = "MouseAxes";
     106        }
     107
     108        // initialise joy sticks separatly to allow for reloading
     109        numberOfJoySticks_ = InputManager::getInstance().numberOfJoySticks();
     110        initialiseJoyStickBindings();
     111
     112        // collect all Buttons and HalfAxes
     113        compilePointerLists();
     114
     115        // set them here to use allHalfAxes_
     116        setConfigValues();
    197117    }
    198118
     
    209129    /**
    210130    @brief
    211         Loads the key and button bindings.
    212     @return
    213         True if loading succeeded.
    214     */
    215     void KeyBinder::loadBindings()
    216     {
    217         COUT(3) << "KeyBinder: Loading key bindings..." << std::endl;
    218 
    219         clearBindings();
    220 
    221         std::ifstream infile;
    222         infile.open("keybindings.ini");
    223         if (!infile)
    224         {
    225             ConfigFileManager::getInstance().setFile(CFT_Keybindings, "def_keybindings.ini");
    226             ConfigFileManager::getInstance().save(CFT_Keybindings, "keybindings.ini");
    227         }
    228         else
    229             infile.close();
    230         ConfigFileManager::getInstance().setFile(CFT_Keybindings, "keybindings.ini");
    231 
    232         // parse key bindings
    233         setConfigValues();
    234 
    235         COUT(3) << "KeyBinder: Loading key bindings done." << std::endl;
    236     }
    237 
    238     /**
    239     @brief
    240131        Loader for the key bindings, managed by config values.
    241132    */
    242133    void KeyBinder::setConfigValues()
    243134    {
     135        SetConfigValue(defaultKeybindings_, "def_keybindings.ini")
     136            .description("Filename of default keybindings.");
    244137        SetConfigValue(analogThreshold_, 0.05f)
    245138            .description("Threshold for analog axes until which the state is 0.");
     
    252145        SetConfigValue(mouseSensitivityDerived_, 1.0f)
    253146            .description("Mouse sensitivity if mouse input is derived.");
    254         SetConfigValue(bClipMouse_, true)
    255             .description("Whether or not to clip absolute value of mouse in non derive mode.");
    256 
    257         float oldThresh = buttonThreshold_;
     147        SetConfigValue(mouseWheelStepSize_, 120.0f)
     148            .description("Equals one step of the mousewheel.");
    258149        SetConfigValue(buttonThreshold_, 0.80f)
    259             .description("Threshold for analog axes until which the button is not pressed.");
    260         if (oldThresh != buttonThreshold_)
    261             for (unsigned int i = 0; i < nHalfAxes_s; i++)
    262                 if (halfAxes_[i].buttonThreshold_ == oldThresh)
    263                     halfAxes_[i].buttonThreshold_ = buttonThreshold_;
    264 
    265         // keys
    266         for (unsigned int i = 0; i < nKeys_s; i++)
    267             readTrigger(keys_[i]);
    268         // mouse buttons
    269         for (unsigned int i = 0; i < nMouseButtons_s; i++)
    270             readTrigger(mouseButtons_[i]);
    271         // joy stick buttons
    272         for (unsigned int i = 0; i < nJoyStickButtons_s; i++)
    273             readTrigger(joyStickButtons_[i]);
    274         // half axes
    275         for (unsigned int i = 0; i < nHalfAxes_s; i++)
    276             readTrigger(halfAxes_[i]);
    277     }
    278 
    279     void KeyBinder::readTrigger(Button& button)
    280     {
    281         // config value stuff
    282         ConfigValueContainer* cont
    283             = ClassIdentifier<KeyBinder>::getIdentifier()->getConfigValueContainer(button.name_);
    284         if (!cont)
    285         {
    286             cont = new ConfigValueContainer
    287                 (CFT_Keybindings, ClassIdentifier<KeyBinder>::getIdentifier(), button.name_, "", button.name_);
    288             ClassIdentifier<KeyBinder>::getIdentifier()->addConfigValueContainer(button.name_, cont);
    289         }
    290         std::string old = button.bindingString_;
    291         cont->getValue(&button.bindingString_, this);
    292 
    293         // keybinder stuff
    294         if (old != button.bindingString_)
    295         {
    296             // clear everything so we don't get old axis ParamCommands mixed up
    297             button.clear();
    298 
    299             // binding has changed
    300             button.parse(paramCommandBuffer_);
     150            .description("Threshold for analog axes until which the button is not pressed.")
     151            .callback(this, &KeyBinder::buttonThresholdChanged);
     152    }
     153
     154    void KeyBinder::buttonThresholdChanged()
     155    {
     156        for (unsigned int i = 0; i < allHalfAxes_.size(); i++)
     157            if (!allHalfAxes_[i]->bButtonThresholdUser_)
     158                allHalfAxes_[i]->buttonThreshold_ = this->buttonThreshold_;
     159    }
     160
     161    void KeyBinder::JoyStickDeviceNumberChanged(unsigned int value)
     162    {
     163        unsigned int oldValue = numberOfJoySticks_;
     164        numberOfJoySticks_ = value;
     165
     166        // initialise joy stick bindings
     167        initialiseJoyStickBindings();
     168
     169        // collect all Buttons and HalfAxes again
     170        compilePointerLists();
     171
     172        // load the bindings if required
     173        if (!configFile_.empty())
     174        {
     175            for (unsigned int iDev = oldValue; iDev < numberOfJoySticks_; ++iDev)
     176            {
     177                for (unsigned int i = 0; i < JoyStickButtonCode::numberOfButtons; ++i)
     178                    joyStickButtons_[iDev][i].readConfigValue();
     179                for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; ++i)
     180                    joyStickAxes_[iDev][i].readConfigValue();
     181            }
     182        }
     183
     184        // Set the button threshold for potential new axes
     185        buttonThresholdChanged();
     186    }
     187
     188    void KeyBinder::initialiseJoyStickBindings()
     189    {
     190        this->joyStickAxes_.resize(numberOfJoySticks_);
     191        this->joyStickButtons_.resize(numberOfJoySticks_);
     192
     193        // reinitialise all joy stick binings (doesn't overwrite the old ones)
     194        for (unsigned int iDev = 0; iDev < numberOfJoySticks_; iDev++)
     195        {
     196            std::string deviceNumber = convertToString(iDev);
     197            // joy stick buttons
     198            for (unsigned int i = 0; i < JoyStickButtonCode::numberOfButtons; i++)
     199            {
     200                joyStickButtons_[iDev][i].name_ = std::string("JoyStick") + deviceNumber + JoyStickButtonCode::ByString[i];
     201                joyStickButtons_[iDev][i].paramCommandBuffer_ = &paramCommandBuffer_;
     202                joyStickButtons_[iDev][i].groupName_ = std::string("JoyStick") + deviceNumber + "Buttons";
     203            }
     204            // joy stick axes
     205            for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; i++)
     206            {
     207                joyStickAxes_[iDev][i].name_ = std::string("JoyStick") + deviceNumber + JoyStickAxisCode::ByString[i >> 1];
     208                if (i & 1)
     209                    joyStickAxes_[iDev][i].name_ += "Pos";
     210                else
     211                    joyStickAxes_[iDev][i].name_ += "Neg";
     212                joyStickAxes_[iDev][i].paramCommandBuffer_ = &paramCommandBuffer_;
     213                joyStickAxes_[iDev][i].groupName_ = std::string("JoyStick") + deviceNumber + "Axes";
     214            }
     215        }
     216    }
     217
     218    void KeyBinder::compilePointerLists()
     219    {
     220        allButtons_.clear();
     221        allHalfAxes_.clear();
     222
     223        for (unsigned int i = 0; i < KeyCode::numberOfKeys; i++)
     224            allButtons_[keys_[i].name_] = keys_ + i;
     225        for (unsigned int i = 0; i < numberOfMouseButtons_; i++)
     226            allButtons_[mouseButtons_[i].name_] = mouseButtons_ + i;
     227        for (unsigned int i = 0; i < MouseAxisCode::numberOfAxes * 2; i++)
     228        {
     229            allButtons_[mouseAxes_[i].name_] = mouseAxes_ + i;
     230            allHalfAxes_.push_back(mouseAxes_ + i);
     231        }
     232        for (unsigned int iDev = 0; iDev < numberOfJoySticks_; iDev++)
     233        {
     234            for (unsigned int i = 0; i < JoyStickButtonCode::numberOfButtons; i++)
     235                allButtons_[joyStickButtons_[iDev][i].name_] = &(joyStickButtons_[iDev][i]);
     236            for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; i++)
     237            {
     238                allButtons_[joyStickAxes_[iDev][i].name_] = &(joyStickAxes_[iDev][i]);
     239                allHalfAxes_.push_back(&(joyStickAxes_[iDev][i]));
     240            }
     241        }
     242    }
     243
     244    /**
     245    @brief
     246        Loads the key and button bindings.
     247    @return
     248        True if loading succeeded.
     249    */
     250    void KeyBinder::loadBindings(const std::string& filename)
     251    {
     252        COUT(3) << "KeyBinder: Loading key bindings..." << std::endl;
     253
     254        configFile_ = filename;
     255        if (configFile_.empty())
     256            return;
     257
     258        // get bindings from default file if filename doesn't exist.
     259        std::ifstream infile;
     260        infile.open(configFile_.c_str());
     261        if (!infile)
     262        {
     263            ConfigFileManager::getInstance().setFile(CFT_Keybindings, defaultKeybindings_);
     264            ConfigFileManager::getInstance().save(CFT_Keybindings, configFile_);
     265        }
     266        else
     267            infile.close();
     268        ConfigFileManager::getInstance().setFile(CFT_Keybindings, configFile_);
     269
     270        // Parse bindings and create the ConfigValueContainers if necessary
     271        clearBindings();
     272        for (std::map<std::string, Button*>::const_iterator it = allButtons_.begin(); it != allButtons_.end(); ++it)
     273            it->second->readConfigValue();
     274
     275        COUT(3) << "KeyBinder: Loading key bindings done." << std::endl;
     276    }
     277
     278    bool KeyBinder::setBinding(const std::string& binding, const std::string& name, bool bTemporary)
     279    {
     280        std::map<std::string, Button*>::iterator it = allButtons_.find(name);
     281        if (it != allButtons_.end())
     282        {
     283            if (bTemporary)
     284                it->second->configContainer_->tset(binding);
     285            else
     286                it->second->configContainer_->set(binding);
     287            it->second->configContainer_->getValue(&(it->second->bindingString_), it->second);
     288            return true;
     289        }
     290        else
     291        {
     292            COUT(2) << "Could not find key/button/axis with name '" << name << "'." << std::endl;
     293            return false;
    301294        }
    302295    }
     
    308301    void KeyBinder::clearBindings()
    309302    {
    310         for (unsigned int i = 0; i < nKeys_s; i++)
    311             keys_[i].clear();
    312 
    313         for (unsigned int i = 0; i < nMouseButtons_s; i++)
    314             mouseButtons_[i].clear();
    315 
    316         for (unsigned int i = 0; i < nJoyStickButtons_s; i++)
    317             joyStickButtons_[i].clear();
    318 
    319         for (unsigned int i = 0; i < nHalfAxes_s; i++)
    320             halfAxes_[i].clear();
     303        for (std::map<std::string, Button*>::const_iterator it = allButtons_.begin(); it != allButtons_.end(); ++it)
     304            it->second->clear();
    321305
    322306        for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++)
     
    327311    void KeyBinder::resetJoyStickAxes()
    328312    {
    329         for (unsigned int i = 8; i < nHalfAxes_s; i++)
    330         {
    331             halfAxes_[i].absVal_ = 0.0f;
    332             halfAxes_[i].relVal_ = 0.0f;
     313        for (unsigned int iDev = 0; iDev < numberOfJoySticks_; ++iDev)
     314        {
     315            for (unsigned int i = 0; i < JoyStickAxisCode::numberOfAxes * 2; i++)
     316            {
     317                joyStickAxes_[iDev][i].absVal_ = 0.0f;
     318                joyStickAxes_[iDev][i].relVal_ = 0.0f;
     319            }
    333320        }
    334321    }
     
    336323    void KeyBinder::tickMouse(float dt)
    337324    {
    338         tickDevices(0, 8);
     325        tickDevices(mouseAxes_, mouseAxes_ + MouseAxisCode::numberOfAxes * 2);
    339326
    340327        if (bDeriveMouseInput_)
     
    342329            if (deriveTime_ > derivePeriod_)
    343330            {
    344                 //CCOUT(3) << "mouse abs: ";
    345331                for (int i = 0; i < 2; i++)
    346332                {
    347333                    if (mouseRelative_[i] > 0)
    348334                    {
    349                         halfAxes_[2*i + 0].absVal_
     335                        mouseAxes_[2*i + 0].absVal_
    350336                            =  mouseRelative_[i] / deriveTime_ * 0.0005 * mouseSensitivityDerived_;
    351                         halfAxes_[2*i + 1].absVal_ = 0.0f;
     337                        mouseAxes_[2*i + 1].absVal_ = 0.0f;
    352338                    }
    353339                    else if (mouseRelative_[i] < 0)
    354340                    {
    355                         halfAxes_[2*i + 0].absVal_ = 0.0f;
    356                         halfAxes_[2*i + 1].absVal_
     341                        mouseAxes_[2*i + 0].absVal_ = 0.0f;
     342                        mouseAxes_[2*i + 1].absVal_
    357343                            = -mouseRelative_[i] / deriveTime_ * 0.0005 * mouseSensitivityDerived_;
    358344                    }
    359345                    else
    360346                    {
    361                         halfAxes_[2*i + 0].absVal_ = 0.0f;
    362                         halfAxes_[2*i + 1].absVal_ = 0.0f;
     347                        mouseAxes_[2*i + 0].absVal_ = 0.0f;
     348                        mouseAxes_[2*i + 1].absVal_ = 0.0f;
    363349                    }
    364                     //COUT(3) << mouseRelative_[i] << " | ";
    365350                    mouseRelative_[i] = 0;
    366                     halfAxes_[2*i + 0].hasChanged_ = true;
    367                     halfAxes_[2*i + 1].hasChanged_ = true;
     351                    mouseAxes_[2*i + 0].hasChanged_ = true;
     352                    mouseAxes_[2*i + 1].hasChanged_ = true;
    368353                }
    369354                deriveTime_ = 0.0f;
    370                 //COUT(3) << std::endl;
    371355            }
    372356            else
     
    375359    }
    376360
    377     void KeyBinder::tickJoyStick(float dt, unsigned int joyStick)
    378     {
    379         tickDevices(8, nHalfAxes_s);
    380     }
    381 
    382     void KeyBinder::tickInput(float dt)
    383     {
    384         // execute all buffered bindings (additional parameter)
    385         for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++)
    386             paramCommandBuffer_[i]->execute();
    387 
    388         // always reset the relative movement of the mouse
    389         for (unsigned int i = 0; i < 8; i++)
    390             halfAxes_[i].relVal_ = 0.0f;
    391     }
    392 
    393     void KeyBinder::tickDevices(unsigned int begin, unsigned int end)
    394     {
    395         for (unsigned int i = begin; i < end; i++)
     361    void KeyBinder::tickDevices(HalfAxis* begin, HalfAxis* end)
     362    {
     363        for (HalfAxis* current = begin; current < end; ++current) // pointer arithmetic
    396364        {
    397365            // button mode
    398366            // TODO: optimize out all the half axes that don't act as a button at the moment
    399             if (halfAxes_[i].hasChanged_)
    400             {
    401                 if (!halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ > halfAxes_[i].buttonThreshold_)
     367            if (current->hasChanged_)
     368            {
     369                if (!current->wasDown_ && current->absVal_ > current->buttonThreshold_)
    402370                {
    403                     halfAxes_[i].wasDown_ = true;
    404                     if (halfAxes_[i].nCommands_[KeybindMode::OnPress])
    405                         halfAxes_[i].execute(KeybindMode::OnPress);
     371                    current->wasDown_ = true;
     372                    if (current->nCommands_[KeybindMode::OnPress])
     373                        current->execute(KeybindMode::OnPress);
    406374                }
    407                 else if (halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ < halfAxes_[i].buttonThreshold_)
     375                else if (current->wasDown_ && current->absVal_ < current->buttonThreshold_)
    408376                {
    409                     halfAxes_[i].wasDown_ = false;
    410                     if (halfAxes_[i].nCommands_[KeybindMode::OnRelease])
    411                         halfAxes_[i].execute(KeybindMode::OnRelease);
     377                    current->wasDown_ = false;
     378                    if (current->nCommands_[KeybindMode::OnRelease])
     379                        current->execute(KeybindMode::OnRelease);
    412380                }
    413                 halfAxes_[i].hasChanged_ = false;
    414             }
    415 
    416             if (halfAxes_[i].wasDown_)
    417             {
    418                 if (halfAxes_[i].nCommands_[KeybindMode::OnHold])
    419                     halfAxes_[i].execute(KeybindMode::OnHold);
     381                current->hasChanged_ = false;
     382            }
     383
     384            if (current->wasDown_)
     385            {
     386                if (current->nCommands_[KeybindMode::OnHold])
     387                    current->execute(KeybindMode::OnHold);
    420388            }
    421389
    422390            // these are the actually useful axis bindings for analog input
    423             if (halfAxes_[i].relVal_ > analogThreshold_ || halfAxes_[i].absVal_ > analogThreshold_)
    424             {
    425                 //COUT(3) << halfAxes_[i].name_ << "\t" << halfAxes_[i].absVal_ << std::endl;
    426                 halfAxes_[i].execute();
    427             }
    428         }
    429     }
    430 
    431     void KeyBinder::keyPressed (const KeyEvent& evt)
    432     { keys_[evt.key].execute(KeybindMode::OnPress); }
    433 
    434     void KeyBinder::keyReleased(const KeyEvent& evt)
    435     { keys_[evt.key].execute(KeybindMode::OnRelease); }
    436 
    437     void KeyBinder::keyHeld    (const KeyEvent& evt)
    438     { keys_[evt.key].execute(KeybindMode::OnHold); }
    439 
    440 
    441     void KeyBinder::mouseButtonPressed (MouseButton::Enum id)
    442     { mouseButtons_[id].execute(KeybindMode::OnPress); }
    443 
    444     void KeyBinder::mouseButtonReleased(MouseButton::Enum id)
    445     { mouseButtons_[id].execute(KeybindMode::OnRelease); }
    446 
    447     void KeyBinder::mouseButtonHeld    (MouseButton::Enum id)
    448     { mouseButtons_[id].execute(KeybindMode::OnHold); }
    449 
    450 
    451     void KeyBinder::joyStickButtonPressed (unsigned int joyStickID, JoyStickButton::Enum id)
    452     { joyStickButtons_[id].execute(KeybindMode::OnPress); }
    453 
    454     void KeyBinder::joyStickButtonReleased(unsigned int joyStickID, JoyStickButton::Enum id)
    455     { joyStickButtons_[id].execute(KeybindMode::OnRelease); }
    456 
    457     void KeyBinder::joyStickButtonHeld    (unsigned int joyStickID, JoyStickButton::Enum id)
    458     { joyStickButtons_[id].execute(KeybindMode::OnHold); }
     391            if (current->relVal_ > analogThreshold_ || current->absVal_ > analogThreshold_)
     392            {
     393                current->execute();
     394            }
     395        }
     396    }
    459397
    460398    /**
     
    473411            for (int i = 0; i < 2; i++)
    474412            {
    475                 if (rel[i])
     413                if (rel[i]) // performance opt. if rel[i] == 0
    476414                {
    477                     // absolute
    478                     halfAxes_[2*i + 0].hasChanged_ = true;
    479                     halfAxes_[2*i + 1].hasChanged_ = true;
     415                    // write absolute values
     416                    mouseAxes_[2*i + 0].hasChanged_ = true;
     417                    mouseAxes_[2*i + 1].hasChanged_ = true;
    480418                    mousePosition_[i] += rel[i];
    481419
    482                     if (bClipMouse_)
    483                     {
    484                         if (mousePosition_[i] > 1024)
    485                             mousePosition_[i] =  1024;
    486                         if (mousePosition_[i] < -1024)
    487                             mousePosition_[i] = -1024;
    488                     }
     420                    // clip absolute position
     421                    if (mousePosition_[i] > mouseClippingSize_)
     422                        mousePosition_[i] =  mouseClippingSize_;
     423                    if (mousePosition_[i] < -mouseClippingSize_)
     424                        mousePosition_[i] = -mouseClippingSize_;
    489425
    490426                    if (mousePosition_[i] >= 0)
    491427                    {
    492                         halfAxes_[2*i + 0].absVal_ =   mousePosition_[i]/1024.0f * mouseSensitivity_;
    493                         halfAxes_[2*i + 1].absVal_ =  0.0f;
     428                        mouseAxes_[2*i + 0].absVal_ =   mousePosition_[i]/(float)mouseClippingSize_ * mouseSensitivity_;
     429                        mouseAxes_[2*i + 1].absVal_ =  0.0f;
    494430                    }
    495431                    else
    496432                    {
    497                         halfAxes_[2*i + 0].absVal_ =  0.0f;
    498                         halfAxes_[2*i + 1].absVal_ =  -mousePosition_[i]/1024.0f * mouseSensitivity_;
     433                        mouseAxes_[2*i + 0].absVal_ =  0.0f;
     434                        mouseAxes_[2*i + 1].absVal_ =  -mousePosition_[i]/(float)mouseClippingSize_ * mouseSensitivity_;
    499435                    }
    500436                }
     
    511447        {
    512448            if (rel[i] > 0)
    513                 halfAxes_[0 + 2*i].relVal_ =  ((float)rel[i])/1024 * mouseSensitivity_;
    514             else
    515                 halfAxes_[1 + 2*i].relVal_ = -((float)rel[i])/1024 * mouseSensitivity_;
     449                mouseAxes_[0 + 2*i].relVal_ =  ((float)rel[i])/(float)mouseClippingSize_ * mouseSensitivity_;
     450            else
     451                mouseAxes_[1 + 2*i].relVal_ = -((float)rel[i])/(float)mouseClippingSize_ * mouseSensitivity_;
    516452        }
    517453    }
     
    523459    void KeyBinder::mouseScrolled(int abs, int rel)
    524460    {
    525         //COUT(3) << mouseButtons_[8].name_ << "   " << abs << " | " << rel << std::endl;
    526 
    527461        if (rel > 0)
    528             for (int i = 0; i < rel/120; i++)
    529                 mouseButtons_[8].execute(KeybindMode::OnPress, ((float)abs)/120.0f);
     462            for (int i = 0; i < rel/mouseWheelStepSize_; i++)
     463                mouseButtons_[8].execute(KeybindMode::OnPress, ((float)abs)/mouseWheelStepSize_);
    530464        else
    531             for (int i = 0; i < -rel/120; i++)
    532                 mouseButtons_[9].execute(KeybindMode::OnPress, ((float)abs)/120.0f);
     465            for (int i = 0; i < -rel/mouseWheelStepSize_; i++)
     466                mouseButtons_[9].execute(KeybindMode::OnPress, ((float)abs)/mouseWheelStepSize_);
    533467    }
    534468
    535469    void KeyBinder::joyStickAxisMoved(unsigned int joyStickID, unsigned int axis, float value)
    536470    {
    537         int i = 8 + axis * 2;
     471        int i = axis * 2;
    538472        if (value >= 0)
    539473        {
    540             //if (value > 10000)
    541             //{ CCOUT(3) << halfAxes_[i].name_ << std::endl; }
    542 
    543             halfAxes_[i].absVal_ = value;
    544             halfAxes_[i].relVal_ = value;
    545             halfAxes_[i].hasChanged_ = true;
    546             if (halfAxes_[i + 1].absVal_ > 0.0f)
    547             {
    548                 halfAxes_[i + 1].absVal_ = -0.0f;
    549                 halfAxes_[i + 1].relVal_ = -0.0f;
    550                 halfAxes_[i + 1].hasChanged_ = true;
     474            joyStickAxes_[joyStickID][i].absVal_ = value;
     475            joyStickAxes_[joyStickID][i].relVal_ = value;
     476            joyStickAxes_[joyStickID][i].hasChanged_ = true;
     477            if (joyStickAxes_[joyStickID][i + 1].absVal_ > 0.0f)
     478            {
     479                joyStickAxes_[joyStickID][i + 1].absVal_ = -0.0f;
     480                joyStickAxes_[joyStickID][i + 1].relVal_ = -0.0f;
     481                joyStickAxes_[joyStickID][i + 1].hasChanged_ = true;
    551482            }
    552483        }
    553484        else
    554485        {
    555             //if (value < -10000)
    556             //{ CCOUT(3) << halfAxes_[i + 1].name_ << std::endl; }
    557 
    558             halfAxes_[i + 1].absVal_ = -value;
    559             halfAxes_[i + 1].relVal_ = -value;
    560             halfAxes_[i + 1].hasChanged_ = true;
    561             if (halfAxes_[i].absVal_ > 0.0f)
    562             {
    563                 halfAxes_[i].absVal_ = -0.0f;
    564                 halfAxes_[i].relVal_ = -0.0f;
    565                 halfAxes_[i].hasChanged_ = true;
     486            joyStickAxes_[joyStickID][i + 1].absVal_ = -value;
     487            joyStickAxes_[joyStickID][i + 1].relVal_ = -value;
     488            joyStickAxes_[joyStickID][i + 1].hasChanged_ = true;
     489            if (joyStickAxes_[joyStickID][i].absVal_ > 0.0f)
     490            {
     491                joyStickAxes_[joyStickID][i].absVal_ = -0.0f;
     492                joyStickAxes_[joyStickID][i].relVal_ = -0.0f;
     493                joyStickAxes_[joyStickID][i].hasChanged_ = true;
    566494            }
    567495        }
Note: See TracChangeset for help on using the changeset viewer.