Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 20, 2008, 7:49:26 PM (16 years ago)
Author:
rgrieder
Message:

merged input branch into gui test branch (was about time)
svn save (it's still a mess and CMLs haven't been updated)
I'll have to create a special project to create the tolua_bind files for tolua itself anyway..

Location:
code/branches/gui
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/gui

  • code/branches/gui/src/core/input/KeyBinder.cc

    r1567 r1638  
    4343namespace orxonox
    4444{
    45   /**
    46     @brief Constructor that does as little as necessary.
    47   */
    48   KeyBinder::KeyBinder() : deriveTime_(0.0f)
    49   {
    50     mouseRelative_[0] = 0;
    51     mouseRelative_[1] = 0;
    52     mousePosition_[0] = 0;
    53     mousePosition_[1] = 0;
    54 
    55     RegisterRootObject(KeyBinder);
    56 
    57     // keys
    58     std::string keyNames[] = {
    59       "UNASSIGNED",
    60       "ESCAPE",
    61       "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
    62       "MINUS", "EQUALS", "BACK", "TAB",
    63       "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P",
    64       "LBRACKET", "RBRACKET",
    65       "RETURN", "LCONTROL",
    66       "A", "S", "D", "F", "G", "H", "J", "K", "L",
    67       "SEMICOLON", "APOSTROPHE", "GRAVE",
    68       "LSHIFT", "BACKSLASH",
    69       "Z", "X", "C", "V", "B", "N", "M",
    70       "COMMA", "PERIOD", "SLASH",
    71       "RSHIFT",
    72       "MULTIPLY",
    73       "LMENU",
    74       "SPACE",
    75       "CAPITAL",
    76       "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10",
    77       "NUMLOCK", "SCROLL",
    78       "NUMPAD7", "NUMPAD8", "NUMPAD9",
    79       "SUBTRACT",
    80       "NUMPAD4", "NUMPAD5", "NUMPAD6",
    81       "ADD",
    82       "NUMPAD1", "NUMPAD2", "NUMPAD3", "NUMPAD0",
    83       "DECIMAL",
    84       "","",
    85       "OEM_102",
    86       "F11", "F12",
    87       "","","","","","","","","","","",
    88       "F13", "F14", "F15",
    89       "","","","","","","","","","",
    90       "KANA",
    91       "","",
    92       "ABNT_C1",
    93       "","","","","",
    94       "CONVERT",
    95       "",
    96       "NOCONVERT",
    97       "",
    98       "YEN",
    99       "ABNT_C2",
    100       "","","","","","","","","","","","","","",
    101       "NUMPADEQUALS",
    102       "","",
    103       "PREVTRACK",
    104       "AT",
    105       "COLON", "UNDERLINE",
    106       "KANJI",
    107       "STOP",
    108       "AX",
    109       "UNLABELED",
    110       "NEXTTRACK",
    111       "","",
    112       "NUMPADENTER",
    113       "RCONTROL",
    114       "","",
    115       "MUTE",
    116       "CALCULATOR",
    117       "PLAYPAUSE",
    118       "",
    119       "MEDIASTOP",
    120       "","","","","","","","","",
    121       "VOLUMEDOWN",
    122       "",
    123       "VOLUMEUP",
    124       "",
    125       "WEBHOME",
    126       "NUMPADCOMMA",
    127       "",
    128       "DIVIDE",
    129       "",
    130       "SYSRQ",
    131       "RMENU",
    132       "","","","","","","","","","","","",
    133       "PAUSE",
    134       "",
    135       "HOME",
    136       "UP",
    137       "PGUP",
    138       "",
    139       "LEFT",
    140       "",
    141       "RIGHT",
    142       "",
    143       "END", "DOWN", "PGDOWN", "INSERT", "DELETE",
    144       "","","","","","","",
    145       "LWIN", "RWIN", "APPS",
    146       "POWER", "SLEEP",
    147       "","","",
    148       "WAKE",
    149       "",
    150       "WEBSEARCH", "WEBFAVORITES", "WEBREFRESH", "WEBSTOP", "WEBFORWARD", "WEBBACK",
    151       "MYCOMPUTER", "MAIL", "MEDIASELECT"
    152     };
    153     for (unsigned int i = 0; i < nKeys_s; i++)
    154       keys_[i].name_ = "Key" + keyNames[i];
    155 
    156     // mouse buttons
    157     std::string mouseButtonNames[] = {
    158       "MouseLeft", "MouseRight", "MouseMiddle",
    159       "MouseButton3", "MouseButton4", "MouseButton5",
    160       "MouseButton6", "MouseButton7",
    161       "MouseWheel1Up", "MouseWheel1Down",
    162       "MouseWheel2Up", "MouseWheel2Down" };
    163     for (unsigned int i = 0; i < nMouseButtons_s; i++)
    164       mouseButtons_[i].name_ = mouseButtonNames[i];
    165 
    166     // joy stick buttons
    167     for (unsigned int i = 0; i < 32; i++)
    168       joyStickButtons_[i].name_ = "JoyButton" + getConvertedValue<int, std::string>(i);
    169     for (unsigned int i = 32; i < nJoyStickButtons_s; i += 4)
    170     {
    171                   joyStickButtons_[i + 0].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "North";
    172                   joyStickButtons_[i + 1].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "South";
    173                   joyStickButtons_[i + 2].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "East";
    174                   joyStickButtons_[i + 3].name_ = "JoyPOV" + getConvertedValue<int, std::string>((i - 32)/4 + 1) + "West";
    175     }
    176 
    177     // half axes
    178     std::string rawNames[nHalfAxes_s/2];
    179     rawNames[0] = "MouseX";
    180     rawNames[1] = "MouseY";
    181     rawNames[2] = "Empty1";
    182     rawNames[3] = "Empty2";
    183     for (unsigned int i = 4; i < nHalfAxes_s/2; i++)
    184       rawNames[i] = "JoyAxis" + getConvertedValue<int, std::string>(i - 3);
    185     for (unsigned int i = 0; i < nHalfAxes_s/2; i++)
    186     {
    187       halfAxes_[i * 2 + 0].name_ = rawNames[i] + "Pos";
    188       halfAxes_[i * 2 + 1].name_ = rawNames[i] + "Neg";
    189     }
    190 
    191     for (unsigned int i = 0; i < this->nHalfAxes_s; i++)
    192       halfAxes_[i].buttonThreshold_ = buttonThreshold_;
    193   }
    194 
    195   /**
    196     @brief Destructor
    197   */
    198   KeyBinder::~KeyBinder()
    199   {
    200     // almost no destructors required because most of the arrays are static.
    201     clearBindings(); // does some destruction work
    202   }
    203 
    204   /**
    205     @brief Loads the key and button bindings.
    206     @return True if loading succeeded.
    207   */
    208   void KeyBinder::loadBindings()
    209   {
    210     COUT(3) << "KeyBinder: Loading key bindings..." << std::endl;
    211 
    212     clearBindings();
    213 
    214     std::ifstream infile;
    215     infile.open("keybindings.ini");
    216     if (!infile)
    217     {
    218       ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "def_keybindings.ini");
    219       ConfigFileManager::getSingleton()->save(CFT_Keybindings, "keybindings.ini");
    220     }
    221     else
    222       infile.close();
    223     ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "keybindings.ini");
    224 
    225     // parse key bindings
    226     setConfigValues();
    227 
    228     COUT(3) << "KeyBinder: Loading key bindings done." << std::endl;
    229   }
    230 
    231   /**
    232     @brief Loader for the key bindings, managed by config values.
    233   */
    234   void KeyBinder::setConfigValues()
    235   {
    236     SetConfigValueGeneric(KeyBinder, analogThreshold_, 0.05f)  .description("Threshold for analog axes until which the state is 0.");
    237     SetConfigValueGeneric(KeyBinder, mouseSensitivity_, 1.0f)  .description("Mouse sensitivity.");
    238     SetConfigValueGeneric(KeyBinder, bDeriveMouseInput_, false).description("Whether or not to derive moues movement for the absolute value.");
    239     SetConfigValueGeneric(KeyBinder, derivePeriod_, 0.05f).description("Accuracy of the mouse input deriver. The higher the more precise, but laggier.");
    240     SetConfigValueGeneric(KeyBinder, mouseSensitivityDerived_, 1.0f).description("Mouse sensitivity if mouse input is derived.");
    241     SetConfigValueGeneric(KeyBinder, bClipMouse_, true).description("Whether or not to clip absolute value of mouse in non derive mode.");
    242 
    243     float oldThresh = buttonThreshold_;
    244     SetConfigValueGeneric(KeyBinder, buttonThreshold_, 0.80f).description("Threshold for analog axes until which the button is not pressed.");
    245     if (oldThresh != buttonThreshold_)
    246       for (unsigned int i = 0; i < nHalfAxes_s; i++)
    247         if (halfAxes_[i].buttonThreshold_ == oldThresh)
    248           halfAxes_[i].buttonThreshold_ = buttonThreshold_;
    249 
    250     // keys
    251     for (unsigned int i = 0; i < nKeys_s; i++)
    252       readTrigger(keys_[i]);
    253     // mouse buttons
    254     for (unsigned int i = 0; i < nMouseButtons_s; i++)
    255       readTrigger(mouseButtons_[i]);
    256     // joy stick buttons
    257     for (unsigned int i = 0; i < nJoyStickButtons_s; i++)
    258       readTrigger(joyStickButtons_[i]);
    259     // half axes
    260     for (unsigned int i = 0; i < nHalfAxes_s; i++)
    261       readTrigger(halfAxes_[i]);
    262   }
    263 
    264   void KeyBinder::readTrigger(Button& button)
    265   {
    266     // config value stuff
    267     ConfigValueContainer* cont = ClassIdentifier<KeyBinder>::getIdentifier()->getConfigValueContainer(button.name_);
    268     if (!cont)
    269     {
    270       cont = new ConfigValueContainer(CFT_Keybindings, ClassIdentifier<KeyBinder>::getIdentifier(), button.name_, "");
    271       ClassIdentifier<KeyBinder>::getIdentifier()->addConfigValueContainer(button.name_, cont);
    272     }
    273     std::string old = button.bindingString_;
    274     cont->getValue(&button.bindingString_);
    275 
    276     // keybinder stuff
    277     if (old != button.bindingString_)
    278     {
    279       // clear everything so we don't get old axis ParamCommands mixed up
    280       button.clear();
    281 
    282       // binding has changed
    283       button.parse(paramCommandBuffer_);
    284     }
    285   }
    286 
    287   /**
    288     @brief Overwrites all bindings with ""
    289   */
    290   void KeyBinder::clearBindings()
    291   {
    292     for (unsigned int i = 0; i < nKeys_s; i++)
    293       keys_[i].clear();
    294 
    295     for (unsigned int i = 0; i < nMouseButtons_s; i++)
    296       mouseButtons_[i].clear();
    297 
    298     for (unsigned int i = 0; i < nJoyStickButtons_s; i++)
    299       joyStickButtons_[i].clear();
    300 
    301     for (unsigned int i = 0; i < nHalfAxes_s; i++)
    302       halfAxes_[i].clear();
    303 
    304     for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++)
    305       delete paramCommandBuffer_[i];
    306     paramCommandBuffer_.clear();
    307   }
    308 
    309   void KeyBinder::resetJoyStickAxes()
    310   {
    311     for (unsigned int i = 8; i < nHalfAxes_s; i++)
    312     {
    313       halfAxes_[i].absVal_ = 0.0f;
    314       halfAxes_[i].relVal_ = 0.0f;
    315     }
    316   }
    317 
    318   void KeyBinder::tickInput(float dt, const HandlerState& state)
    319   {
    320     // we have to process all the analog input since there is e.g. no 'mouseDoesntMove' event.
    321     unsigned int iBegin = 8;
    322     unsigned int iEnd   = 8;
    323     if (state.joyStick)
    324       iEnd = nHalfAxes_s;
    325     if (state.mouse)
    326       iBegin = 0;
    327     for (unsigned int i = iBegin; i < iEnd; i++)
    328     {
    329       if (halfAxes_[i].hasChanged_)
    330       {
    331         if (!halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ > halfAxes_[i].buttonThreshold_)
    332         {
    333           halfAxes_[i].wasDown_ = true;
    334           if (halfAxes_[i].nCommands_[KeybindMode::OnPress])
    335             halfAxes_[i].execute(KeybindMode::OnPress);
    336         }
    337         else if (halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ < halfAxes_[i].buttonThreshold_)
    338         {
    339           halfAxes_[i].wasDown_ = false;
    340           if (halfAxes_[i].nCommands_[KeybindMode::OnRelease])
    341             halfAxes_[i].execute(KeybindMode::OnRelease);
    342         }
    343         halfAxes_[i].hasChanged_ = false;
    344       }
    345 
    346       if (halfAxes_[i].wasDown_)
    347       {
    348         if (halfAxes_[i].nCommands_[KeybindMode::OnHold])
    349           halfAxes_[i].execute(KeybindMode::OnHold);
    350       }
    351 
    352       // these are the actually useful axis bindings for analog input AND output
    353       if (halfAxes_[i].relVal_ > analogThreshold_ || halfAxes_[i].absVal_ > analogThreshold_)
    354       {
    355         //COUT(3) << halfAxes_[i].name_ << "\t" << halfAxes_[i].absVal_ << std::endl;
    356         halfAxes_[i].execute();
    357       }
    358     }
    359 
    360     if (bDeriveMouseInput_ && state.mouse)
    361     {
    362       if (deriveTime_ > derivePeriod_)
    363       {
    364         //CCOUT(3) << "mouse abs: ";
     45    /**
     46    @brief
     47        Constructor that does as little as necessary.
     48    */
     49    KeyBinder::KeyBinder()
     50        : deriveTime_(0.0f)
     51    {
     52        mouseRelative_[0] = 0;
     53        mouseRelative_[1] = 0;
     54        mousePosition_[0] = 0;
     55        mousePosition_[1] = 0;
     56
     57        RegisterRootObject(KeyBinder);
     58
     59        // keys
     60        std::string keyNames[] = {
     61        "UNASSIGNED",
     62        "ESCAPE",
     63        "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
     64        "MINUS", "EQUALS", "BACK", "TAB",
     65        "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P",
     66        "LBRACKET", "RBRACKET",
     67        "RETURN", "LCONTROL",
     68        "A", "S", "D", "F", "G", "H", "J", "K", "L",
     69        "SEMICOLON", "APOSTROPHE", "GRAVE",
     70        "LSHIFT", "BACKSLASH",
     71        "Z", "X", "C", "V", "B", "N", "M",
     72        "COMMA", "PERIOD", "SLASH",
     73        "RSHIFT",
     74        "MULTIPLY",
     75        "LMENU",
     76        "SPACE",
     77        "CAPITAL",
     78        "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10",
     79        "NUMLOCK", "SCROLL",
     80        "NUMPAD7", "NUMPAD8", "NUMPAD9",
     81        "SUBTRACT",
     82        "NUMPAD4", "NUMPAD5", "NUMPAD6",
     83        "ADD",
     84        "NUMPAD1", "NUMPAD2", "NUMPAD3", "NUMPAD0",
     85        "DECIMAL",
     86        "","",
     87        "OEM_102",
     88        "F11", "F12",
     89        "","","","","","","","","","","",
     90        "F13", "F14", "F15",
     91        "","","","","","","","","","",
     92        "KANA",
     93        "","",
     94        "ABNT_C1",
     95        "","","","","",
     96        "CONVERT",
     97        "",
     98        "NOCONVERT",
     99        "",
     100        "YEN",
     101        "ABNT_C2",
     102        "","","","","","","","","","","","","","",
     103        "NUMPADEQUALS",
     104        "","",
     105        "PREVTRACK",
     106        "AT",
     107        "COLON", "UNDERLINE",
     108        "KANJI",
     109        "STOP",
     110        "AX",
     111        "UNLABELED",
     112        "NEXTTRACK",
     113        "","",
     114        "NUMPADENTER",
     115        "RCONTROL",
     116        "","",
     117        "MUTE",
     118        "CALCULATOR",
     119        "PLAYPAUSE",
     120        "",
     121        "MEDIASTOP",
     122        "","","","","","","","","",
     123        "VOLUMEDOWN",
     124        "",
     125        "VOLUMEUP",
     126        "",
     127        "WEBHOME",
     128        "NUMPADCOMMA",
     129        "",
     130        "DIVIDE",
     131        "",
     132        "SYSRQ",
     133        "RMENU",
     134        "","","","","","","","","","","","",
     135        "PAUSE",
     136        "",
     137        "HOME",
     138        "UP",
     139        "PGUP",
     140        "",
     141        "LEFT",
     142        "",
     143        "RIGHT",
     144        "",
     145        "END", "DOWN", "PGDOWN", "INSERT", "DELETE",
     146        "","","","","","","",
     147        "LWIN", "RWIN", "APPS",
     148        "POWER", "SLEEP",
     149        "","","",
     150        "WAKE",
     151        "",
     152        "WEBSEARCH", "WEBFAVORITES", "WEBREFRESH", "WEBSTOP", "WEBFORWARD", "WEBBACK",
     153        "MYCOMPUTER", "MAIL", "MEDIASELECT"
     154        };
     155        for (unsigned int i = 0; i < nKeys_s; i++)
     156            keys_[i].name_ = "Key" + keyNames[i];
     157
     158        // mouse buttons
     159        std::string mouseButtonNames[] = {
     160            "MouseLeft",     "MouseRight",   "MouseMiddle",
     161            "MouseButton3",  "MouseButton4", "MouseButton5",
     162            "MouseButton6",  "MouseButton7",
     163            "MouseWheel1Up", "MouseWheel1Down",
     164            "MouseWheel2Up", "MouseWheel2Down"
     165        };
     166        for (unsigned int i = 0; i < nMouseButtons_s; i++)
     167            mouseButtons_[i].name_ = mouseButtonNames[i];
     168
     169        // joy stick buttons
     170        for (unsigned int i = 0; i < 32; i++)
     171            joyStickButtons_[i].name_ = "JoyButton" + getConvertedValue<int, std::string>(i);
     172        for (unsigned int i = 32; i < nJoyStickButtons_s; i += 4)
     173        {
     174            joyStickButtons_[i + 0].name_ = "JoyPOV" + convertToString((i - 32)/4 + 1) + "North";
     175            joyStickButtons_[i + 1].name_ = "JoyPOV" + convertToString((i - 32)/4 + 1) + "South";
     176            joyStickButtons_[i + 2].name_ = "JoyPOV" + convertToString((i - 32)/4 + 1) + "East";
     177            joyStickButtons_[i + 3].name_ = "JoyPOV" + convertToString((i - 32)/4 + 1) + "West";
     178        }
     179
     180        // half axes
     181        std::string rawNames[nHalfAxes_s/2];
     182        rawNames[0] = "MouseX";
     183        rawNames[1] = "MouseY";
     184        rawNames[2] = "Empty1";
     185        rawNames[3] = "Empty2";
     186        for (unsigned int i = 4; i < nHalfAxes_s/2; i++)
     187            rawNames[i] = "JoyAxis" + convertToString(i - 3);
     188        for (unsigned int i = 0; i < nHalfAxes_s/2; i++)
     189        {
     190            halfAxes_[i * 2 + 0].name_ = rawNames[i] + "Pos";
     191            halfAxes_[i * 2 + 1].name_ = rawNames[i] + "Neg";
     192        }
     193
     194        for (unsigned int i = 0; i < this->nHalfAxes_s; i++)
     195            halfAxes_[i].buttonThreshold_ = buttonThreshold_;
     196    }
     197
     198    /**
     199    @brief
     200        Destructor
     201    */
     202    KeyBinder::~KeyBinder()
     203    {
     204        // almost no destructors required because most of the arrays are static.
     205        clearBindings(); // does some destruction work
     206    }
     207
     208    /**
     209    @brief
     210        Loads the key and button bindings.
     211    @return
     212        True if loading succeeded.
     213    */
     214    void KeyBinder::loadBindings()
     215    {
     216        COUT(3) << "KeyBinder: Loading key bindings..." << std::endl;
     217
     218        clearBindings();
     219
     220        std::ifstream infile;
     221        infile.open("keybindings.ini");
     222        if (!infile)
     223        {
     224            ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "def_keybindings.ini");
     225            ConfigFileManager::getSingleton()->save(CFT_Keybindings, "keybindings.ini");
     226        }
     227        else
     228            infile.close();
     229        ConfigFileManager::getSingleton()->setFile(CFT_Keybindings, "keybindings.ini");
     230
     231        // parse key bindings
     232        setConfigValues();
     233
     234        COUT(3) << "KeyBinder: Loading key bindings done." << std::endl;
     235    }
     236
     237    /**
     238    @brief
     239        Loader for the key bindings, managed by config values.
     240    */
     241    void KeyBinder::setConfigValues()
     242    {
     243        SetConfigValueGeneric(KeyBinder, analogThreshold_, 0.05f)
     244            .description("Threshold for analog axes until which the state is 0.");
     245        SetConfigValueGeneric(KeyBinder, mouseSensitivity_, 1.0f)
     246            .description("Mouse sensitivity.");
     247        SetConfigValueGeneric(KeyBinder, bDeriveMouseInput_, false)
     248            .description("Whether or not to derive moues movement for the absolute value.");
     249        SetConfigValueGeneric(KeyBinder, derivePeriod_, 0.05f)
     250            .description("Accuracy of the mouse input deriver. The higher the more precise, but laggier.");
     251        SetConfigValueGeneric(KeyBinder, mouseSensitivityDerived_, 1.0f)
     252            .description("Mouse sensitivity if mouse input is derived.");
     253        SetConfigValueGeneric(KeyBinder, bClipMouse_, true)
     254            .description("Whether or not to clip absolute value of mouse in non derive mode.");
     255
     256        float oldThresh = buttonThreshold_;
     257        SetConfigValueGeneric(KeyBinder, buttonThreshold_, 0.80f)
     258            .description("Threshold for analog axes until which the button is not pressed.");
     259        if (oldThresh != buttonThreshold_)
     260            for (unsigned int i = 0; i < nHalfAxes_s; i++)
     261                if (halfAxes_[i].buttonThreshold_ == oldThresh)
     262                    halfAxes_[i].buttonThreshold_ = buttonThreshold_;
     263
     264        // keys
     265        for (unsigned int i = 0; i < nKeys_s; i++)
     266            readTrigger(keys_[i]);
     267        // mouse buttons
     268        for (unsigned int i = 0; i < nMouseButtons_s; i++)
     269            readTrigger(mouseButtons_[i]);
     270        // joy stick buttons
     271        for (unsigned int i = 0; i < nJoyStickButtons_s; i++)
     272            readTrigger(joyStickButtons_[i]);
     273        // half axes
     274        for (unsigned int i = 0; i < nHalfAxes_s; i++)
     275            readTrigger(halfAxes_[i]);
     276    }
     277
     278    void KeyBinder::readTrigger(Button& button)
     279    {
     280        // config value stuff
     281        ConfigValueContainer* cont
     282            = ClassIdentifier<KeyBinder>::getIdentifier()->getConfigValueContainer(button.name_);
     283        if (!cont)
     284        {
     285            cont = new ConfigValueContainer
     286                (CFT_Keybindings, ClassIdentifier<KeyBinder>::getIdentifier(), button.name_, "");
     287            ClassIdentifier<KeyBinder>::getIdentifier()->addConfigValueContainer(button.name_, cont);
     288        }
     289        std::string old = button.bindingString_;
     290        cont->getValue(&button.bindingString_);
     291
     292        // keybinder stuff
     293        if (old != button.bindingString_)
     294        {
     295            // clear everything so we don't get old axis ParamCommands mixed up
     296            button.clear();
     297
     298            // binding has changed
     299            button.parse(paramCommandBuffer_);
     300        }
     301    }
     302
     303    /**
     304    @brief
     305        Overwrites all bindings with ""
     306    */
     307    void KeyBinder::clearBindings()
     308    {
     309        for (unsigned int i = 0; i < nKeys_s; i++)
     310            keys_[i].clear();
     311
     312        for (unsigned int i = 0; i < nMouseButtons_s; i++)
     313            mouseButtons_[i].clear();
     314
     315        for (unsigned int i = 0; i < nJoyStickButtons_s; i++)
     316            joyStickButtons_[i].clear();
     317
     318        for (unsigned int i = 0; i < nHalfAxes_s; i++)
     319            halfAxes_[i].clear();
     320
     321        for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++)
     322            delete paramCommandBuffer_[i];
     323        paramCommandBuffer_.clear();
     324    }
     325
     326    void KeyBinder::resetJoyStickAxes()
     327    {
     328        for (unsigned int i = 8; i < nHalfAxes_s; i++)
     329        {
     330            halfAxes_[i].absVal_ = 0.0f;
     331            halfAxes_[i].relVal_ = 0.0f;
     332        }
     333    }
     334
     335    void KeyBinder::tickMouse(float dt)
     336    {
     337        tickDevices(0, 8);
     338
     339        if (bDeriveMouseInput_)
     340        {
     341            if (deriveTime_ > derivePeriod_)
     342            {
     343                //CCOUT(3) << "mouse abs: ";
     344                for (int i = 0; i < 2; i++)
     345                {
     346                    if (mouseRelative_[i] > 0)
     347                    {
     348                        halfAxes_[2*i + 0].absVal_
     349                            =  mouseRelative_[i] / deriveTime_ * 0.0005 * mouseSensitivityDerived_;
     350                        halfAxes_[2*i + 1].absVal_ = 0.0f;
     351                    }
     352                    else if (mouseRelative_[i] < 0)
     353                    {
     354                        halfAxes_[2*i + 0].absVal_ = 0.0f;
     355                        halfAxes_[2*i + 1].absVal_
     356                            = -mouseRelative_[i] / deriveTime_ * 0.0005 * mouseSensitivityDerived_;
     357                    }
     358                    else
     359                    {
     360                        halfAxes_[2*i + 0].absVal_ = 0.0f;
     361                        halfAxes_[2*i + 1].absVal_ = 0.0f;
     362                    }
     363                    //COUT(3) << mouseRelative_[i] << " | ";
     364                    mouseRelative_[i] = 0;
     365                    halfAxes_[2*i + 0].hasChanged_ = true;
     366                    halfAxes_[2*i + 1].hasChanged_ = true;
     367                }
     368                deriveTime_ = 0.0f;
     369                //COUT(3) << std::endl;
     370            }
     371            else
     372                deriveTime_ += dt;
     373        }
     374    }
     375
     376    void KeyBinder::tickJoyStick(float dt, int device)
     377    {
     378        tickDevices(8, nHalfAxes_s);
     379    }
     380
     381    void KeyBinder::tickInput(float dt)
     382    {
     383        // execute all buffered bindings (additional parameter)
     384        for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++)
     385            paramCommandBuffer_[i]->execute();
     386
     387        // always reset the relative movement of the mouse
     388        for (unsigned int i = 0; i < 8; i++)
     389            halfAxes_[i].relVal_ = 0.0f;
     390    }
     391
     392    void KeyBinder::tickDevices(unsigned int begin, unsigned int end)
     393    {
     394        for (unsigned int i = begin; i < end; i++)
     395        {
     396            // button mode
     397            // TODO: optimize out all the half axes that don't act as a button at the moment
     398            if (halfAxes_[i].hasChanged_)
     399            {
     400                if (!halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ > halfAxes_[i].buttonThreshold_)
     401                {
     402                    halfAxes_[i].wasDown_ = true;
     403                    if (halfAxes_[i].nCommands_[KeybindMode::OnPress])
     404                        halfAxes_[i].execute(KeybindMode::OnPress);
     405                }
     406                else if (halfAxes_[i].wasDown_ && halfAxes_[i].absVal_ < halfAxes_[i].buttonThreshold_)
     407                {
     408                    halfAxes_[i].wasDown_ = false;
     409                    if (halfAxes_[i].nCommands_[KeybindMode::OnRelease])
     410                        halfAxes_[i].execute(KeybindMode::OnRelease);
     411                }
     412                halfAxes_[i].hasChanged_ = false;
     413            }
     414
     415            if (halfAxes_[i].wasDown_)
     416            {
     417                if (halfAxes_[i].nCommands_[KeybindMode::OnHold])
     418                    halfAxes_[i].execute(KeybindMode::OnHold);
     419            }
     420
     421            // these are the actually useful axis bindings for analog input
     422            if (halfAxes_[i].relVal_ > analogThreshold_ || halfAxes_[i].absVal_ > analogThreshold_)
     423            {
     424                //COUT(3) << halfAxes_[i].name_ << "\t" << halfAxes_[i].absVal_ << std::endl;
     425                halfAxes_[i].execute();
     426            }
     427        }
     428    }
     429
     430    void KeyBinder::keyPressed (const KeyEvent& evt)
     431    { keys_[evt.key].execute(KeybindMode::OnPress); }
     432
     433    void KeyBinder::keyReleased(const KeyEvent& evt)
     434    { keys_[evt.key].execute(KeybindMode::OnRelease); }
     435
     436    void KeyBinder::keyHeld    (const KeyEvent& evt)
     437    { keys_[evt.key].execute(KeybindMode::OnHold); }
     438
     439
     440    void KeyBinder::mouseButtonPressed (MouseButton::Enum id)
     441    { mouseButtons_[id].execute(KeybindMode::OnPress); }
     442
     443    void KeyBinder::mouseButtonReleased(MouseButton::Enum id)
     444    { mouseButtons_[id].execute(KeybindMode::OnRelease); }
     445
     446    void KeyBinder::mouseButtonHeld    (MouseButton::Enum id)
     447    { mouseButtons_[id].execute(KeybindMode::OnHold); }
     448
     449
     450    void KeyBinder::joyStickButtonPressed (unsigned int joyStickID, unsigned int button)
     451    { joyStickButtons_[button].execute(KeybindMode::OnPress); }
     452
     453    void KeyBinder::joyStickButtonReleased(unsigned int joyStickID, unsigned int button)
     454    { joyStickButtons_[button].execute(KeybindMode::OnRelease); }
     455
     456    void KeyBinder::joyStickButtonHeld    (unsigned int joyStickID, unsigned int button)
     457    { joyStickButtons_[button].execute(KeybindMode::OnHold); }
     458
     459    /**
     460    @brief
     461        Event handler for the mouseMoved Event.
     462    @param e
     463        Mouse state information
     464    */
     465    void KeyBinder::mouseMoved(IntVector2 abs_, IntVector2 rel_, IntVector2 clippingSize)
     466    {
     467        // y axis of mouse input is inverted
     468        int rel[] = { rel_.x, -rel_.y };
     469
     470        if (!bDeriveMouseInput_)
     471        {
     472            for (int i = 0; i < 2; i++)
     473            {
     474                if (rel[i])
     475                {
     476                    // absolute
     477                    halfAxes_[2*i + 0].hasChanged_ = true;
     478                    halfAxes_[2*i + 1].hasChanged_ = true;
     479                    mousePosition_[i] += rel[i];
     480
     481                    if (bClipMouse_)
     482                    {
     483                        if (mousePosition_[i] > 1024)
     484                            mousePosition_[i] =  1024;
     485                        if (mousePosition_[i] < -1024)
     486                            mousePosition_[i] = -1024;
     487                    }
     488
     489                    if (mousePosition_[i] >= 0)
     490                    {
     491                        halfAxes_[2*i + 0].absVal_ =   mousePosition_[i]/1024.0f * mouseSensitivity_;
     492                        halfAxes_[2*i + 1].absVal_ =  0.0f;
     493                    }
     494                    else
     495                    {
     496                        halfAxes_[2*i + 0].absVal_ =  0.0f;
     497                        halfAxes_[2*i + 1].absVal_ =  -mousePosition_[i]/1024.0f * mouseSensitivity_;
     498                    }
     499                }
     500            }
     501        }
     502        else
     503        {
     504            mouseRelative_[0] += rel[0];
     505            mouseRelative_[1] += rel[1];
     506        }
     507
     508        // relative
    365509        for (int i = 0; i < 2; i++)
    366510        {
    367           if (mouseRelative_[i] > 0)
    368           {
    369             halfAxes_[2*i + 0].absVal_ =  mouseRelative_[i] / deriveTime_ * 0.0005 * mouseSensitivityDerived_;
    370             halfAxes_[2*i + 1].absVal_ = 0.0f;
    371           }
    372           else if (mouseRelative_[i] < 0)
    373           {
    374             halfAxes_[2*i + 0].absVal_ = 0.0f;
    375             halfAxes_[2*i + 1].absVal_ = -mouseRelative_[i] / deriveTime_ * 0.0005 * mouseSensitivityDerived_;
    376           }
    377           else
    378           {
    379             halfAxes_[2*i + 0].absVal_ = 0.0f;
    380             halfAxes_[2*i + 1].absVal_ = 0.0f;
    381           }
    382           //COUT(3) << mouseRelative_[i] << " | ";
    383           mouseRelative_[i] = 0;
    384           halfAxes_[2*i + 0].hasChanged_ = true;
    385           halfAxes_[2*i + 1].hasChanged_ = true;
    386         }
    387         deriveTime_ = 0.0f;
    388         //COUT(3) << std::endl;
    389       }
    390       else
    391         deriveTime_ += dt;
    392     }
    393 
    394     // execute all buffered bindings (addional parameter)
    395     for (unsigned int i = 0; i < paramCommandBuffer_.size(); i++)
    396       paramCommandBuffer_[i]->execute();
    397 
    398     // always reset the relative movement of the mouse
    399     if (state.mouse)
    400       for (unsigned int i = 0; i < 8; i++)
    401         halfAxes_[i].relVal_ = 0.0f;
    402   }
    403 
    404   void KeyBinder::keyPressed (const KeyEvent& evt)
    405   { keys_[evt.key].execute(KeybindMode::OnPress); }
    406 
    407   void KeyBinder::keyReleased(const KeyEvent& evt)
    408   { keys_[evt.key].execute(KeybindMode::OnRelease); }
    409 
    410   void KeyBinder::keyHeld    (const KeyEvent& evt)
    411   { keys_[evt.key].execute(KeybindMode::OnHold); }
    412 
    413 
    414   void KeyBinder::mouseButtonPressed (MouseButton::Enum id)
    415   { mouseButtons_[id].execute(KeybindMode::OnPress); }
    416 
    417   void KeyBinder::mouseButtonReleased(MouseButton::Enum id)
    418   { mouseButtons_[id].execute(KeybindMode::OnRelease); }
    419 
    420   void KeyBinder::mouseButtonHeld    (MouseButton::Enum id)
    421   { mouseButtons_[id].execute(KeybindMode::OnHold); }
    422 
    423 
    424   void KeyBinder::joyStickButtonPressed (int joyStickID, int button)
    425   { joyStickButtons_[button].execute(KeybindMode::OnPress); }
    426 
    427   void KeyBinder::joyStickButtonReleased(int joyStickID, int button)
    428   { joyStickButtons_[button].execute(KeybindMode::OnRelease); }
    429 
    430   void KeyBinder::joyStickButtonHeld    (int joyStickID, int button)
    431   { joyStickButtons_[button].execute(KeybindMode::OnHold); }
    432 
    433   /**
    434     @brief Event handler for the mouseMoved Event.
    435     @param e Mouse state information
    436   */
    437   void KeyBinder::mouseMoved(IntVector2 abs_, IntVector2 rel_, IntVector2 clippingSize)
    438   {
    439     // y axis of mouse input is inverted
    440     int rel[] = { rel_.x, -rel_.y };
    441 
    442     if (!bDeriveMouseInput_)
    443     {
    444       for (int i = 0; i < 2; i++)
    445       {
    446         if (rel[i])
    447         {
    448           // absolute
    449           halfAxes_[2*i + 0].hasChanged_ = true;
    450           halfAxes_[2*i + 1].hasChanged_ = true;
    451           mousePosition_[i] += rel[i];
    452 
    453           if (bClipMouse_)
    454           {
    455             if (mousePosition_[i] > 1024)
    456               mousePosition_[i] =  1024;
    457             if (mousePosition_[i] < -1024)
    458               mousePosition_[i] = -1024;
    459           }
    460 
    461           if (mousePosition_[i] >= 0)
    462           {
    463             halfAxes_[2*i + 0].absVal_ =   mousePosition_[i]/1024.0f * mouseSensitivity_;
    464             halfAxes_[2*i + 1].absVal_ =  0.0f;
    465           }
    466           else
    467           {
    468             halfAxes_[2*i + 0].absVal_ =  0.0f;
    469             halfAxes_[2*i + 1].absVal_ =  -mousePosition_[i]/1024.0f * mouseSensitivity_;
    470           }
    471         }
    472       }
    473     }
    474     else
    475     {
    476       mouseRelative_[0] += rel[0];
    477       mouseRelative_[1] += rel[1];
    478     }
    479 
    480     // relative
    481     for (int i = 0; i < 2; i++)
    482     {
    483       if (rel[i] > 0)
    484         halfAxes_[0 + 2*i].relVal_ =  ((float)rel[i])/1024 * mouseSensitivity_;
    485       else
    486         halfAxes_[1 + 2*i].relVal_ = -((float)rel[i])/1024 * mouseSensitivity_;
    487     }
    488   }
    489 
    490   /**
     511            if (rel[i] > 0)
     512                halfAxes_[0 + 2*i].relVal_ =  ((float)rel[i])/1024 * mouseSensitivity_;
     513            else
     514                halfAxes_[1 + 2*i].relVal_ = -((float)rel[i])/1024 * mouseSensitivity_;
     515        }
     516    }
     517
     518    /**
    491519    @brief Event handler for the mouseScrolled Event.
    492520    @param e Mouse state information
    493   */
    494   void KeyBinder::mouseScrolled(int abs, int rel)
    495   {
    496     //COUT(3) << mouseButtons_[8].name_ << "   " << abs << " | " << rel << std::endl;
    497 
    498     if (rel > 0)
    499       for (int i = 0; i < rel/120; i++)
    500         mouseButtons_[8].execute(KeybindMode::OnPress, ((float)abs)/120.0f);
    501     else
    502       for (int i = 0; i < -rel/120; i++)
    503         mouseButtons_[9].execute(KeybindMode::OnPress, ((float)abs)/120.0f);
    504   }
    505 
    506   void KeyBinder::joyStickAxisMoved(int joyStickID, int axis, float value)
    507   {
    508     // TODO: Use proper calibration values instead of generally 16-bit integer
    509     int i = 8 + axis * 2;
    510     if (value >= 0)
    511     {
    512       //if (value > 10000)
    513       //{ CCOUT(3) << halfAxes_[i].name_ << std::endl; }
    514 
    515       halfAxes_[i].absVal_ = value;
    516       halfAxes_[i].relVal_ = value;
    517       halfAxes_[i].hasChanged_ = true;
    518       if (halfAxes_[i + 1].absVal_ > 0.0f)
    519       {
    520         halfAxes_[i + 1].absVal_ = -0.0f;
    521         halfAxes_[i + 1].relVal_ = -0.0f;
    522         halfAxes_[i + 1].hasChanged_ = true;
    523       }
    524     }
    525     else
    526     {
    527       //if (value < -10000)
    528       //{ CCOUT(3) << halfAxes_[i + 1].name_ << std::endl; }
    529 
    530       halfAxes_[i + 1].absVal_ = -value;
    531       halfAxes_[i + 1].relVal_ = -value;
    532       halfAxes_[i + 1].hasChanged_ = true;
    533       if (halfAxes_[i].absVal_ > 0.0f)
    534       {
    535         halfAxes_[i].absVal_ = -0.0f;
    536         halfAxes_[i].relVal_ = -0.0f;
    537         halfAxes_[i].hasChanged_ = true;
    538       }
    539     }
    540   }
     521    */
     522    void KeyBinder::mouseScrolled(int abs, int rel)
     523    {
     524        //COUT(3) << mouseButtons_[8].name_ << "   " << abs << " | " << rel << std::endl;
     525
     526        if (rel > 0)
     527            for (int i = 0; i < rel/120; i++)
     528                mouseButtons_[8].execute(KeybindMode::OnPress, ((float)abs)/120.0f);
     529        else
     530            for (int i = 0; i < -rel/120; i++)
     531                mouseButtons_[9].execute(KeybindMode::OnPress, ((float)abs)/120.0f);
     532    }
     533
     534    void KeyBinder::joyStickAxisMoved(unsigned int joyStickID, unsigned int axis, float value)
     535    {
     536        int i = 8 + axis * 2;
     537        if (value >= 0)
     538        {
     539            //if (value > 10000)
     540            //{ CCOUT(3) << halfAxes_[i].name_ << std::endl; }
     541
     542            halfAxes_[i].absVal_ = value;
     543            halfAxes_[i].relVal_ = value;
     544            halfAxes_[i].hasChanged_ = true;
     545            if (halfAxes_[i + 1].absVal_ > 0.0f)
     546            {
     547                halfAxes_[i + 1].absVal_ = -0.0f;
     548                halfAxes_[i + 1].relVal_ = -0.0f;
     549                halfAxes_[i + 1].hasChanged_ = true;
     550            }
     551        }
     552        else
     553        {
     554            //if (value < -10000)
     555            //{ CCOUT(3) << halfAxes_[i + 1].name_ << std::endl; }
     556
     557            halfAxes_[i + 1].absVal_ = -value;
     558            halfAxes_[i + 1].relVal_ = -value;
     559            halfAxes_[i + 1].hasChanged_ = true;
     560            if (halfAxes_[i].absVal_ > 0.0f)
     561            {
     562                halfAxes_[i].absVal_ = -0.0f;
     563                halfAxes_[i].relVal_ = -0.0f;
     564                halfAxes_[i].hasChanged_ = true;
     565            }
     566        }
     567    }
    541568}
Note: See TracChangeset for help on using the changeset viewer.