//Copyright (C) Ben Parbury - All Rights Reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Written by Ben Parbury <ben@gridlockinteractive.com> 2014
var gRemapButtons = new Array();

function RemapButton(keycode, callback)
{
	this.SetName = function()
	{
		this.Redraw();
		
		//Key been mapped to something else
		var keycode = gKeyboard.GetUnmap(this.keycode);
		if(keycode)
		{
			this.name = KeyCodeToString(keycode);
			return;
		}
		
		//Key has been mapped away (another key is now mapped to it)
		var map = gKeyboard.GetMap(this.keycode);
		if(map)
		{
			this.name = "";
			return;
		}
		
		this.name = KeyCodeToString(this.keycode);
	}
	
	this.__proto__ = new Button("", callback);
	this.keycode = keycode;	//Always the orignal keycode setup with button
	this.SetName();
	gRemapButtons.push(this);
	
	this.SetKeyCode = function(keycode)
	{
		//Remove previous remap...
		var map = gKeyboard.GetUnmap(this.keycode);
		if(map)
		{
			gKeyboard.Demap(map);
		}

		gKeyboard.Remap(keycode, this.keycode);
		this.SetName();
	}

	this.DrawText = function(selected)
	{
		var fontScale = this.name.length == 1 ? 1 : 0.5;
		var fontSize = this.fontSize * fontScale;
		
		var additional = selected ? 1 : 0;
		gDraw.menu.font	 = GetFont(fontSize);
		gDraw.menu.textAlign	 = 'center';
		gDraw.menu.textBaseline	 = 'middle';

		var spaceIndex = this.name.indexOf(" ");
		if(spaceIndex > -1)
		{
			var top = this.name.substring(0, spaceIndex);
			var bottom = this.name.substring(spaceIndex + 1);
			var spacing = fontSize/1.5;
			gDraw.menu.fillTextShadow(top, this.x + additional, this.y + additional - spacing, this.GetTextColor(!this.locked), this.GetTextColor(this.locked));
			gDraw.menu.fillTextShadow(bottom, this.x + additional, this.y + additional + spacing, this.GetTextColor(!this.locked), this.GetTextColor(this.locked));
		}
		else
		{
			gDraw.menu.fillTextShadow(this.name, this.x + additional, this.y + additional, this.GetTextColor(!this.locked), this.GetTextColor(this.locked));
		}
	}
}