/*
   client/key.cc
   This file is part of the Osirion project and is distributed under
   the terms and conditions of the GNU General Public License version 2
*/

#include "auxiliary/functions.h"
#include "client/key.h"

namespace client
{

Key::Key(const char *name, int keysym, char ascii)
{
	key_sym = keysym;
	key_ascii = ascii;
	key_pressed = 0;
	key_lastpressed = 0;
	key_waspressed = 0;

	key_name.assign(name);

	clear();
}

Key::~Key()
{
	clear();
}

std::string const & Key::bind(Modifier mod) const
{
	switch (mod) {
		case None:
			return key_bind;
			break;
		case Shift:
			return key_bind_shift;
			break;
		case Ctrl:
			return key_bind_ctrl;
			break;
		case Alt:
			return key_bind_alt;
			break;
	}

	return key_bind;
}

void Key::clear()
{
	key_bind.clear();
	key_bind_shift.clear();
	key_bind_ctrl.clear();
	key_bind_alt.clear();
	
	key_action = 0;
}

void Key::clear(Modifier mod)
{
	switch (mod) {
		case None:
			key_action = 0;
			key_bind.clear();
			break;
		case Shift:
			key_bind_shift.clear();
			break;
		case Ctrl:
			key_bind_ctrl.clear();
			break;
		case Alt:
			key_bind_alt.clear();
			break;
	}
}

void Key::assign(Modifier mod, const char *bind, Action *action)
{
	if (!bind) {
		clear(mod);
		return;
	}
	
	switch (mod) {
		case None:
			key_action = action;
			key_bind.assign(bind);
			aux::trim(key_bind);
			break;
		case Shift:
			key_bind_shift.assign(bind);
			aux::trim(key_bind_shift);
			break;
		case Ctrl:
			key_bind_ctrl.assign(bind);
			aux::trim(key_bind_ctrl);
			break;
		case Alt:
			key_bind_alt.assign(bind);
			aux::trim(key_bind_alt);
			break;
	}
}

}