[Orxonox-commit 3168] r7861 - in code/trunk/src/libraries/core: command input

landauf at orxonox.net landauf at orxonox.net
Sat Feb 12 11:54:08 CET 2011


Author: landauf
Date: 2011-02-12 11:54:07 +0100 (Sat, 12 Feb 2011)
New Revision: 7861

Modified:
   code/trunk/src/libraries/core/command/ConsoleCommand.cc
   code/trunk/src/libraries/core/command/ConsoleCommand.h
   code/trunk/src/libraries/core/input/InputCommands.h
   code/trunk/src/libraries/core/input/KeyBinder.cc
   code/trunk/src/libraries/core/input/KeyBinder.h
Log:
added function to KeyBinder which allows to change the keybind mode (OnPress, OnRelease, OnHold) of a command which is bound to a key.
enhanced ConsoleCommand (+Manipulator) to use this feature.

input system experts, please review :D

Modified: code/trunk/src/libraries/core/command/ConsoleCommand.cc
===================================================================
--- code/trunk/src/libraries/core/command/ConsoleCommand.cc	2011-02-12 10:37:25 UTC (rev 7860)
+++ code/trunk/src/libraries/core/command/ConsoleCommand.cc	2011-02-12 10:54:07 UTC (rev 7861)
@@ -37,6 +37,8 @@
 #include "util/StringUtils.h"
 #include "core/Language.h"
 #include "core/GameMode.h"
+#include "core/input/KeyBinder.h"
+#include "core/input/KeyBinderManager.h"
 
 namespace orxonox
 {
@@ -574,6 +576,17 @@
     }
 
     /**
+        @brief Changes the keybind mode.
+    */
+    ConsoleCommand& ConsoleCommand::changeKeybindMode(KeybindMode::Value mode)
+    {
+        KeyBinderManager::getInstance().getCurrent()->changeMode(this, mode);
+
+        this->keybindMode(mode);
+        return *this;
+    }
+
+    /**
         @brief Returns the command with given group an name.
         @param group The group of the requested command
         @param name The group of the requested command

Modified: code/trunk/src/libraries/core/command/ConsoleCommand.h
===================================================================
--- code/trunk/src/libraries/core/command/ConsoleCommand.h	2011-02-12 10:37:25 UTC (rev 7860)
+++ code/trunk/src/libraries/core/command/ConsoleCommand.h	2011-02-12 10:54:07 UTC (rev 7861)
@@ -509,7 +509,7 @@
                         { if (this->command_) { this->command_->setAsInputCommand(); } return *this; }
                     /// Changes the keybind mode of the command.
                     inline ConsoleCommandManipulator& keybindMode(KeybindMode::Value mode)
-                        { if (this->command_) { this->command_->keybindMode(mode); } return *this; }
+                        { if (this->command_) { this->command_->changeKeybindMode(mode); } return *this; }
                     /// Sets the input configured param to the given index.
                     inline ConsoleCommandManipulator& inputConfiguredParam(int index)
                         { if (this->command_) { this->command_->inputConfiguredParam(index); } return *this; }
@@ -597,13 +597,15 @@
                 return *this;
             }
 
-            /// Changes the keybind mode.
+            /// Sets the keybind mode. Note: use changeKeybindMode if you intend to change the mode.
             inline ConsoleCommand& keybindMode(KeybindMode::Value mode)
                 { this->keybindMode_ = mode; return *this; }
             /// Returns the keybind mode
             inline KeybindMode::Value getKeybindMode() const
                 { return this->keybindMode_; }
 
+            ConsoleCommand& changeKeybindMode(KeybindMode::Value mode);
+
             /// Changes the input configured param to the given index.
             inline ConsoleCommand& inputConfiguredParam(int index)
                 { this->inputConfiguredParam_ = index; return *this; }

Modified: code/trunk/src/libraries/core/input/InputCommands.h
===================================================================
--- code/trunk/src/libraries/core/input/InputCommands.h	2011-02-12 10:37:25 UTC (rev 7860)
+++ code/trunk/src/libraries/core/input/InputCommands.h	2011-02-12 10:54:07 UTC (rev 7861)
@@ -57,12 +57,14 @@
     public:
         virtual ~BaseCommand() { }
         virtual bool execute(float abs = 1.0f, float rel = 1.0f) = 0;
+        virtual CommandEvaluation* getEvaluation() = 0;
     };
 
     class _CoreExport SimpleCommand : public BaseCommand
     {
     public:
         bool execute(float abs = 1.0f, float rel = 1.0f);
+        CommandEvaluation* getEvaluation();
 
         CommandEvaluation evaluation_;
     };
@@ -78,15 +80,31 @@
         return evaluation_.execute();
     }
 
+    /// Returns a pointer to the encapsuled evaluation.
+    inline CommandEvaluation* SimpleCommand::getEvaluation()
+    {
+        return &this->evaluation_;
+    }
+
     class _CoreExport ParamCommand : public BaseCommand
     {
     public:
         ParamCommand() : scale_(1.0f), paramCommand_(0) { }
         bool execute(float abs = 1.0f, float rel = 1.0f);
+        CommandEvaluation* getEvaluation();
 
         float scale_;
         BufferedParamCommand* paramCommand_;
     };
+
+    /// Returns a pointer to the encapsuled evaluation.
+    inline CommandEvaluation* ParamCommand::getEvaluation()
+    {
+        if (this->paramCommand_)
+            return &this->paramCommand_->evaluation_;
+        else
+            return 0;
+    }
 }
 
 #endif /* _InputCommands_H__ */

Modified: code/trunk/src/libraries/core/input/KeyBinder.cc
===================================================================
--- code/trunk/src/libraries/core/input/KeyBinder.cc	2011-02-12 10:37:25 UTC (rev 7860)
+++ code/trunk/src/libraries/core/input/KeyBinder.cc	2011-02-12 10:54:07 UTC (rev 7861)
@@ -384,6 +384,59 @@
         paramCommandBuffer_.clear();
     }
 
+    /**
+        @brief Changes the keybind mode of a given console command.
+    */
+    void KeyBinder::changeMode(ConsoleCommand* command, KeybindMode::Value new_mode)
+    {
+        // iterate over all buttons
+        for (std::map<std::string, Button*>::iterator it = this->allButtons_.begin(); it != this->allButtons_.end(); ++it)
+        {
+            Button* button = it->second;
+
+            // iterate over all modes
+            for (size_t mode_index = 0; mode_index < 3; ++mode_index)
+            {
+                if (mode_index == new_mode) // skip commands that are already in the desired mode
+                    continue;
+
+                // iterate over all commands of the given mode at the given button
+                for (size_t command_index = 0; command_index < button->nCommands_[mode_index]; ++command_index)
+                {
+                    CommandEvaluation* evaluation = button->commands_[mode_index][command_index]->getEvaluation();
+
+                    // compare the command
+                    if (evaluation && evaluation->getConsoleCommand() == command)
+                    {
+                        // increase array of new mode
+                        BaseCommand** array_new_mode = new BaseCommand*[button->nCommands_[new_mode] + 1];
+                        // copy array content
+                        for (size_t c = 0; c < button->nCommands_[new_mode]; ++c)
+                            array_new_mode[c] = button->commands_[new_mode][c];
+                        // insert changed command at the end
+                        array_new_mode[button->nCommands_[new_mode]] = button->commands_[mode_index][command_index];
+                        // delete old array
+                        delete[] button->commands_[new_mode];
+                        // assign new array
+                        button->commands_[new_mode] = array_new_mode;
+                        // increase counter
+                        button->nCommands_[new_mode]++;
+
+                        // erase command from old array
+                        for (size_t c = command_index; c < button->nCommands_[mode_index] - 1; ++c)
+                            button->commands_[mode_index][c] = button->commands_[mode_index][c + 1];
+                        // decrease counter
+                        button->nCommands_[mode_index]--;
+                        // note: we don't replace the old array - it's not one element too large, but no one cares since nCommands_ defines the size
+
+                        // decrement the index since we shifted the array and continue searching for more occurrences of the command
+                        command_index--;
+                    }
+                }
+            }
+        }
+    }
+
     void KeyBinder::resetJoyStickAxes()
     {
         for (unsigned int iDev = 0; iDev < joySticks_.size(); ++iDev)

Modified: code/trunk/src/libraries/core/input/KeyBinder.h
===================================================================
--- code/trunk/src/libraries/core/input/KeyBinder.h	2011-02-12 10:37:25 UTC (rev 7860)
+++ code/trunk/src/libraries/core/input/KeyBinder.h	2011-02-12 10:54:07 UTC (rev 7861)
@@ -75,6 +75,8 @@
         void resetJoyStickAxes();
         void resetMouseAxes();
 
+        void changeMode(ConsoleCommand* command, KeybindMode::Value mode);
+
     protected: // functions
         void loadBindings();
         void buttonThresholdChanged();




More information about the Orxonox-commit mailing list