User Config key mapping

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
trixs
Posts: 26
Joined: Fri Jan 11, 2008 5:19 am
Location: Georgia
Contact:

User Config key mapping

Post by trixs »

Hey All,

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) );
}
:oops:
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Urgh, maps and iterators and bears, oh my.

I prefer to keep it simple, because I'm a bear of very little brain.
Last edited by rogerborg on Mon Jan 14, 2008 6:32 pm, edited 1 time in total.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
trixs
Posts: 26
Joined: Fri Jan 11, 2008 5:19 am
Location: Georgia
Contact:

Post by trixs »

lol - you forgot the vectors!

Seriously, I couldn't think of a way to keep it simple and functional. How have you accomplished it in the past?
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Oh, snap. Sorry, I linked to the the image rather than the post...

That's my Irrlicht-based simple solution. I'm currently swithering over SDL or OIS for a real solution, but in either case I'll try to keep the mapping as simple as possible.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Post Reply