I just wrote a user configurable key mapping class and curious if this is how you would have done it. Everything in this class is configurable by loading a std::map.. Eventually I will load the map by a config file but for now its just setup to load it from the .cpp file. Anyway, let me know if you think I am doing something completely wrong..IE: this can be done much more efficiently! Only been programming for two weeks and need the advice!
At least it works ha
Code: Select all
/* InputMap.h
*/
#include <irrlicht.h>
#include <map>
#include <string>
#include <vector>
// Include files for whatever pointers you may need
#include "InputRecieved.h"
#include "Camera.h"
class InputAction {
public:
// Any startup info
InputAction(MyEventReceiver* input, Camera* cameraView);
// Contains a list of possible inputs
// This will be processed every loop
void KeyInput();
private:
// Goes through the current Inputs and does prescribed actions
void FindAction();
void Action(int whatcase);
void SetupMap();
int numkeys; // How many inputs are in input array
std::vector<std::string> myvector; // Current Pressed Keys
std::vector<std::string>::iterator vit; // Vector iterator
std::map<std::string, int> inputmap; // What to do if W is pressed.
std::map<std::string, int>::iterator mit; // Map iterator
// Any pointers you will need - such as Reciever or Movement stuff
MyEventReceiver* input;
Camera* cameraView;
};
Code: Select all
// InputMap.cpp
#include "InputMap.h"
// Any startup info
InputAction::InputAction(MyEventReceiver* Passedinput, Camera* PassedcameraView) {
// Here should run functions to load current keymappings
this->SetupMap();
// Setup our pointers
input = Passedinput;
cameraView = PassedcameraView;
}
// Contains a list of possible inputs
void InputAction::KeyInput() {
// camera movement control
if(input->key_pressed(KEY_KEY_W)) myvector.push_back ("KEY_KEY_W");
if(input->key_pressed(KEY_KEY_S)) myvector.push_back ("KEY_KEY_S");
if(input->key_pressed(KEY_KEY_D)) myvector.push_back ("KEY_KEY_D");
if(input->key_pressed(KEY_KEY_A)) myvector.push_back ("KEY_KEY_A");
this->FindAction();
}
// Uses vector to find a key in a map and then executes the prescribed key
void InputAction::FindAction() {
for ( vit=myvector.begin() ; vit < myvector.end(); vit++ ) {
mit=inputmap.find(*vit);
this->Action(mit->second);
}
myvector.clear();
}
// Takes the variable and processes the function or whatever you have in the switch
void InputAction::Action(int whatcase) {
switch ( whatcase ) {
case 1:
if(input->key_pressed(KEY_KEY_W)) cameraView->moveForward(1);
break;
case 2:
if(input->key_pressed(KEY_KEY_S)) cameraView->moveBack(1);
break;
case 3:
if(input->key_pressed(KEY_KEY_D)) cameraView->moveRight(1);
break;
case 4:
if(input->key_pressed(KEY_KEY_A)) cameraView->moveLeft(1);
break;
default:
// Do nothing as this key was not found!
break;
}
}
// Set up the map in whatever way - Eventually make user configurable via config file
void InputAction::SetupMap() {
std::pair<std::map<std::string,int>::iterator,bool> ret;
inputmap.insert (std::pair<std::string,int>("KEY_KEY_W",1) );
inputmap.insert (std::pair<std::string,int>("KEY_KEY_S",2) );
inputmap.insert (std::pair<std::string,int>("KEY_KEY_D",3) );
inputmap.insert (std::pair<std::string,int>("KEY_KEY_A",4) );
}