Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Feb 12, 2011, 11:54:07 AM (14 years ago)
Author:
landauf
Message:

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

Location:
code/trunk/src/libraries/core
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/libraries/core/command/ConsoleCommand.cc

    r7401 r7861  
    3838#include "core/Language.h"
    3939#include "core/GameMode.h"
     40#include "core/input/KeyBinder.h"
     41#include "core/input/KeyBinderManager.h"
    4042
    4143namespace orxonox
     
    575577
    576578    /**
     579        @brief Changes the keybind mode.
     580    */
     581    ConsoleCommand& ConsoleCommand::changeKeybindMode(KeybindMode::Value mode)
     582    {
     583        KeyBinderManager::getInstance().getCurrent()->changeMode(this, mode);
     584
     585        this->keybindMode(mode);
     586        return *this;
     587    }
     588
     589    /**
    577590        @brief Returns the command with given group an name.
    578591        @param group The group of the requested command
  • code/trunk/src/libraries/core/command/ConsoleCommand.h

    r7401 r7861  
    510510                    /// Changes the keybind mode of the command.
    511511                    inline ConsoleCommandManipulator& keybindMode(KeybindMode::Value mode)
    512                         { if (this->command_) { this->command_->keybindMode(mode); } return *this; }
     512                        { if (this->command_) { this->command_->changeKeybindMode(mode); } return *this; }
    513513                    /// Sets the input configured param to the given index.
    514514                    inline ConsoleCommandManipulator& inputConfiguredParam(int index)
     
    598598            }
    599599
    600             /// Changes the keybind mode.
     600            /// Sets the keybind mode. Note: use changeKeybindMode if you intend to change the mode.
    601601            inline ConsoleCommand& keybindMode(KeybindMode::Value mode)
    602602                { this->keybindMode_ = mode; return *this; }
     
    604604            inline KeybindMode::Value getKeybindMode() const
    605605                { return this->keybindMode_; }
     606
     607            ConsoleCommand& changeKeybindMode(KeybindMode::Value mode);
    606608
    607609            /// Changes the input configured param to the given index.
  • code/trunk/src/libraries/core/input/InputCommands.h

    r7284 r7861  
    5858        virtual ~BaseCommand() { }
    5959        virtual bool execute(float abs = 1.0f, float rel = 1.0f) = 0;
     60        virtual CommandEvaluation* getEvaluation() = 0;
    6061    };
    6162
     
    6465    public:
    6566        bool execute(float abs = 1.0f, float rel = 1.0f);
     67        CommandEvaluation* getEvaluation();
    6668
    6769        CommandEvaluation evaluation_;
     
    7981    }
    8082
     83    /// Returns a pointer to the encapsuled evaluation.
     84    inline CommandEvaluation* SimpleCommand::getEvaluation()
     85    {
     86        return &this->evaluation_;
     87    }
     88
    8189    class _CoreExport ParamCommand : public BaseCommand
    8290    {
     
    8492        ParamCommand() : scale_(1.0f), paramCommand_(0) { }
    8593        bool execute(float abs = 1.0f, float rel = 1.0f);
     94        CommandEvaluation* getEvaluation();
    8695
    8796        float scale_;
    8897        BufferedParamCommand* paramCommand_;
    8998    };
     99
     100    /// Returns a pointer to the encapsuled evaluation.
     101    inline CommandEvaluation* ParamCommand::getEvaluation()
     102    {
     103        if (this->paramCommand_)
     104            return &this->paramCommand_->evaluation_;
     105        else
     106            return 0;
     107    }
    90108}
    91109
  • code/trunk/src/libraries/core/input/KeyBinder.cc

    r7859 r7861  
    385385    }
    386386
     387    /**
     388        @brief Changes the keybind mode of a given console command.
     389    */
     390    void KeyBinder::changeMode(ConsoleCommand* command, KeybindMode::Value new_mode)
     391    {
     392        // iterate over all buttons
     393        for (std::map<std::string, Button*>::iterator it = this->allButtons_.begin(); it != this->allButtons_.end(); ++it)
     394        {
     395            Button* button = it->second;
     396
     397            // iterate over all modes
     398            for (size_t mode_index = 0; mode_index < 3; ++mode_index)
     399            {
     400                if (mode_index == new_mode) // skip commands that are already in the desired mode
     401                    continue;
     402
     403                // iterate over all commands of the given mode at the given button
     404                for (size_t command_index = 0; command_index < button->nCommands_[mode_index]; ++command_index)
     405                {
     406                    CommandEvaluation* evaluation = button->commands_[mode_index][command_index]->getEvaluation();
     407
     408                    // compare the command
     409                    if (evaluation && evaluation->getConsoleCommand() == command)
     410                    {
     411                        // increase array of new mode
     412                        BaseCommand** array_new_mode = new BaseCommand*[button->nCommands_[new_mode] + 1];
     413                        // copy array content
     414                        for (size_t c = 0; c < button->nCommands_[new_mode]; ++c)
     415                            array_new_mode[c] = button->commands_[new_mode][c];
     416                        // insert changed command at the end
     417                        array_new_mode[button->nCommands_[new_mode]] = button->commands_[mode_index][command_index];
     418                        // delete old array
     419                        delete[] button->commands_[new_mode];
     420                        // assign new array
     421                        button->commands_[new_mode] = array_new_mode;
     422                        // increase counter
     423                        button->nCommands_[new_mode]++;
     424
     425                        // erase command from old array
     426                        for (size_t c = command_index; c < button->nCommands_[mode_index] - 1; ++c)
     427                            button->commands_[mode_index][c] = button->commands_[mode_index][c + 1];
     428                        // decrease counter
     429                        button->nCommands_[mode_index]--;
     430                        // note: we don't replace the old array - it's not one element too large, but no one cares since nCommands_ defines the size
     431
     432                        // decrement the index since we shifted the array and continue searching for more occurrences of the command
     433                        command_index--;
     434                    }
     435                }
     436            }
     437        }
     438    }
     439
    387440    void KeyBinder::resetJoyStickAxes()
    388441    {
  • code/trunk/src/libraries/core/input/KeyBinder.h

    r7859 r7861  
    7575        void resetJoyStickAxes();
    7676        void resetMouseAxes();
     77
     78        void changeMode(ConsoleCommand* command, KeybindMode::Value mode);
    7779
    7880    protected: // functions
Note: See TracChangeset for help on using the changeset viewer.