/** 
 * Get image for buttons
 */

#Const Version		"2016-01-14"
#Const ScriptName	"UIPadButtons.Script.txt"

#Include "TextLib" as TextLib

// ---------------------------------- //
// Constants
// ---------------------------------- //
#Const C_DefaultBaseDir			"file://Media/Manialinks/Common/PadButtons/"
#Const C_DefaultPlatform		2 // PC
#Const C_DefaultButtonsFolder	"Default/"
#Const C_DefaultButtonsFormat	".dds"
#Const C_DefaultButtonName		"Default"
// Plaforms
#Const C_Platforms [
	"PS4"	=> 0,	///< Take care before changing this number, it is linked to the index of the BUTTONS_MENU, etc arrays !
	"XB1"	=> 1,	///< Take care before changing this number, it is linked to the index of the BUTTONS_MENU, etc arrays !
	"PC"	=> 2	///< Take care before changing this number, it is linked to the index of the BUTTONS_MENU, etc arrays !
]

// Buttons in the menus
#Const BUTTONS_MENU [
	CManiaAppEvent::EMenuNavAction::Select		=>["Cross"			, "A"			, "Enter"],
	CManiaAppEvent::EMenuNavAction::Cancel		=>["Circle"			, "B"			, "Backspace"],
	CManiaAppEvent::EMenuNavAction::Action2		=>["Triangle"		, "Y"			, "Fin"],
	CManiaAppEvent::EMenuNavAction::Action1		=>["Square"			, "X"			, "Home"],
	CManiaAppEvent::EMenuNavAction::PageUp		=>["R1"				, "RB"			, "Pagedown"],
	CManiaAppEvent::EMenuNavAction::PageDown	=>["L1"				, "LB"			, "Pageup"],
	CManiaAppEvent::EMenuNavAction::AppMenu		=>["Options"		, "Menu"		, "Echap"],
	CManiaAppEvent::EMenuNavAction::Down		=>["Editor_DPadDown"		, "Editor_DPadDown"		, ""]
]

// Buttons in the editor
#Const BUTTONS_EDITOR [
	CEditorPluginEvent::EInput::Unknown			=>	[""						, ""					, ""],
	CEditorPluginEvent::EInput::Menu			=>	["Options"				, "Menu"				, "Echap"],
	CEditorPluginEvent::EInput::SwitchToRace	=>	["Editor_Square"		, "Editor_X"			, "Enter"],
	CEditorPluginEvent::EInput::Undo			=>	["L2"					, "LT"					, "U"],
	CEditorPluginEvent::EInput::Redo			=>	["R2"					, "RT"					, "R"],
	CEditorPluginEvent::EInput::CursorUp		=>	["Editor_LStickUp"		, "Editor_LStickUp"		, "Up"],
	CEditorPluginEvent::EInput::CursorRight		=>	["Editor_LStickRight"	, "Editor_LStickRight"	, "Right"],
	CEditorPluginEvent::EInput::CursorDown		=>	["Editor_LStickDown"	, "Editor_LStickDown"	, "Down"],
	CEditorPluginEvent::EInput::CursorLeft		=>	["Editor_LStickLeft"	, "Editor_LStickLeft"	, "Left"],
	CEditorPluginEvent::EInput::CursorRaise		=>	["R1"					, "RB"					, "PageDown"],
	CEditorPluginEvent::EInput::CursorLower		=>	["L1"					, "LB"					, "PageUp"],
	CEditorPluginEvent::EInput::CursorTurn		=>	["Editor_Triangle"		, "Editor_Y"			, "RCtrl"],
	CEditorPluginEvent::EInput::CursorPick		=>	["Editor_LStickClick"	, "Editor_LStickClick"	, "E"],
	CEditorPluginEvent::EInput::CursorPlace		=>	["Editor_Cross"			, "Editor_A"			, "Space"],
	CEditorPluginEvent::EInput::CursorDelete	=>	["Editor_Circle"		, "Editor_B"			, "Del"],
	CEditorPluginEvent::EInput::CameraUp		=>	["Editor_RStickUp"		, "Editor_RStickUp"		, "Numpad8"],
	CEditorPluginEvent::EInput::CameraRight		=>	["Editor_RStickRight"	, "Editor_RStickRight"	, "Numpad4"],
	CEditorPluginEvent::EInput::CameraDown		=>	["Editor_RStickDown"	, "Editor_RStickDown"	, "Numpad2"],
	CEditorPluginEvent::EInput::CameraLeft		=>	["Editor_RStickLeft"	, "Editor_RStickLeft"	, "Numpad6"],
	CEditorPluginEvent::EInput::CameraZoomNext	=>	["Editor_RStickClick"	, "Editor_RStickClick"	, ""],
	CEditorPluginEvent::EInput::IconUp			=>	["Editor_DPadUp"		, "Editor_DPadUp"		, ""],
	CEditorPluginEvent::EInput::IconRight		=>	["Editor_DPadRight"		, "Editor_DPadRight"	, ""],
	CEditorPluginEvent::EInput::IconDown		=>	["Editor_DPadDown"		, "Editor_DPadDown"		, ""],
	CEditorPluginEvent::EInput::IconLeft		=>	["Editor_DPadLeft"		, "Editor_DPadLeft"		, ""]
]
// Named button in the editor
#Const BUTTONS_NAMED [
	"SelectModel"		=> ["Editor_RStick"			, "Editor_RStick"		, ""],
	"SelectFamily"		=> ["Editor_LStick"			, "Editor_LStick"		, ""],
	"Camera"			=> ["Editor_RStick"			, "Editor_RStick"		, ""],
	"MoveBlock"			=> ["Editor_DPad"			, "Editor_DPad"			, ""],
	"MoveBlock2"		=> ["Editor_LStick"			, "Editor_LStick"		, ""],
	"LeftStick"			=> ["Editor_LStick"			, "Editor_LStick"		, "Editor_LStick"],
	"RightStick"		=> ["Editor_RStick"			, "Editor_RStick"		, "Editor_RStick"],
	"Zoom"				=> ["Editor_RStickClick"	, "Editor_RStickClick"	, ""],
	"RandomModel"		=> ["Editor_LStickClick"	, "Editor_LStickClick"	, ""],
	"PingModel"			=> ["Editor_LStickClick"	, "Editor_LStickClick"	, "Editor_LStickClick"],
	"LeftStickMulti"	=> ["Editor_LStickMulti"	, "Editor_LStickMulti"	, ""],
	"RightStickMulti"	=> ["Editor_RStickMulti"	, "Editor_RStickMulti"	, ""],
	"PrevMacroblock"	=> ["L1"					, "LB"					, ""],
	"PrevFamily"		=> ["L2"					, "LT"					, ""],
	"NextMacroblock"	=> ["R1"					, "RB"					, ""],
	"NextFamily"		=> ["R2"					, "RT"					, ""],
	// UI
	"UI_Next2"			=> ["R2"					, "RT"					, "R"],
	"UI_Prev2"			=> ["L2"					, "LT"					, "U"],
	"UI_Next1"			=> ["R1"					, "RB"					, "PageDown"],
	"UI_Prev1"			=> ["L1"					, "LB"					, "PageUp"],
	// Helper
	"Helper_A"			=> ["Editor_Cross_Helper"		, "Editor_A_Helper"			, ""],
	"Helper_B"			=> ["Editor_Circle_Helper"		, "Editor_B_Helper"			, ""],
	"Helper_X"			=> ["Editor_Square_Helper"		, "Editor_X_Helper"			, ""],
	"Helper_Y"			=> ["Editor_Triangle_Helper"	, "Editor_Y_Helper"			, ""],
	"Helper_Up"			=> ["Editor_LStickUp_Helper"	, "Editor_LStickUp_Helper"	, ""],
	"Helper_Down"		=> ["Editor_LStickDown_Helper"	, "Editor_LStickDown_Helper", ""],
	"Helper_DPad"		=> ["Editor_DPad_Helper"		, "Editor_DPad_Helper"		, ""]
]
// Named button outside the editor
#Const ALTERNATE_BUTTONS_NAMED [
	"PageUp_2"   => ["R1_alt.dds", "RB_alt.dds", "PageUp"],
	"PageDown_2" => ["L1_alt.dds", "LB_alt.dds", "PageDown"],
	"View"       => ["TouchPad.dds"  , "View.dds"		, "F1.dds"]]

// ---------------------------------- //
// Globales
// ---------------------------------- //
declare Integer	G_Private_Platform; ///< Current platform, always use Private_SetPlatform() and GetPlatform() to set and get its value

// ---------------------------------- //
// Functions
// ---------------------------------- //
// ---------------------------------- //
// Private
// ---------------------------------- //


Boolean IsJapan() {
	return (""^SystemSkuIdentifier == "::ESystemSkuIdentifier::JP");
}


// ---------------------------------- //
/** Set the current platform
 *
 *	@param	_Platform				The platform to use
 */
Void Private_SetPlatform(Integer _Platform) {
	G_Private_Platform = _Platform;
}

// ---------------------------------- //
// Public
// ---------------------------------- //
// ---------------------------------- //
/** Get platform index from name
 *
 *	@param	_Platform				Name of the platform
 *
 *	@return							Platform index if found, default platform index otherwise
 */
Integer GetPlatform(Text _Platform) {

	if (!C_Platforms.existskey(_Platform)) return C_DefaultPlatform;
	return C_Platforms[_Platform];
}

// ---------------------------------- //
/** Get current platform index
 *
 *	@return							The current platform index
 */
Integer GetPlatform() {
	if (!C_Platforms.exists(G_Private_Platform)) return C_DefaultPlatform;
	return G_Private_Platform;
}

// ---------------------------------- //
/** Get platform name from index
 *
 *	@param	_PlatformIndex			Index of the platform
 *
 *	@return							Platform name if found, default platform name otherwise
 */
Text GetPlatformName(Integer _PlatformIndex) {
	if (!C_Platforms.exists(_PlatformIndex)) return C_Platforms.keyof(C_DefaultPlatform);
	return C_Platforms.keyof(_PlatformIndex);
}

// ---------------------------------- //
/** Get the folder stocking the images for the given platform
 *
 *	@param	_Platform				The platform to get
 *
 *	@return							The folder
 */
Text GetPlatformFolder(Integer _Platform) {
	return GetPlatformName(_Platform)^"/";
}

// ---------------------------------- //
/** Get the platform index associated to the given system
 *
 *	@param	_System					The system to check
 *
 *	@return							The platform index of the given system
 */
Integer GetPlatformIndexFromSystem(Text _System) {
	switch (_System) {
		case "::ESystemPlatform::PS4"	: return C_Platforms["PS4"];
		case "::ESystemPlatform::XBoxOne"	: return C_Platforms["XB1"];
		// Display XB1 controller buttons on PC for now
		case "::ESystemPlatform::Steam"	: return C_Platforms["PC"];//return C_Platforms["PC"];
		case "::ESystemPlatform::Uplay"	: return C_Platforms["PC"];//return C_Platforms["PC"];
		case "::ESystemPlatform::None"	: return C_Platforms["PC"];//return C_Platforms["PC"];
	}
	
	return C_DefaultPlatform;
}

Integer GetPlatformIndexFromSystem(CManiaApp::ESystemPlatform _System) {
	return GetPlatformIndexFromSystem(""^_System);
}
// ---------------------------------- //
// Buttons named
// ---------------------------------- //
// ---------------------------------- //
/** Get the name of the image associated to the given named button
 *
 *	@param	_Platform				The platform used
 *	@param	_Button					The button to find
 *
 *	@return							The image associated to the button
 */
Text Private_GetButtonName(Integer _Platform, Text _Button) {
	// TODO LATER : recuprer ici le mapping custom si besoin
	
	if (BUTTONS_NAMED.existskey(_Button)) {
		declare ButtonByPf = BUTTONS_NAMED[_Button];
		if (ButtonByPf.existskey(_Platform)) return ButtonByPf[_Platform];
		if (ButtonByPf.count > 0) return ButtonByPf[0];
	}
	
	return "";
}
// ---------------------------------- //
/// Overload Private_GetButtonName()
Text Private_GetButtonName(Text _Button) {
	return Private_GetButtonName(GetPlatform(), _Button);
}

// ---------------------------------- //
/** Get the path of the image associated to the given named button
 *
 *	@param	_Platform				The platform used
 *	@param	_Button					The button to find
 *
 *	@return							The path to the image associated to the button
 */
Text GetImage(Integer _Platform, Text _Button) {
	declare ButtonName = Private_GetButtonName(_Platform, _Button);
	
	if (ButtonName != "") {
		declare PlatformFolder	= GetPlatformFolder(_Platform);
		return C_DefaultBaseDir^PlatformFolder^ButtonName^C_DefaultButtonsFormat;
	} else {
		return C_DefaultBaseDir^C_DefaultButtonsFolder^C_DefaultButtonName^C_DefaultButtonsFormat;
	}
	
	return "";
}
// ---------------------------------- //
/// Overload GetImage()
Text GetImage(Text _Button) {
	return GetImage(GetPlatform(), _Button);
}

// ---------------------------------- //
// Buttons menu
// ---------------------------------- //
// ---------------------------------- //
/** Get the name of the image associated to the given menu button
 *
 *	@param	_Platform				The platform used
 *	@param	_Button					The button to find
 *
 *	@return							The image associated to the button
 */
Text Private_GetButtonName(Integer _Platform, CManiaAppEvent::EMenuNavAction _Button) {
	declare CManiaAppEvent::EMenuNavAction Truebutton = _Button;
	if(IsJapan()) {
		if (_Button == CManiaAppEvent::EMenuNavAction::Select) Truebutton = CManiaAppEvent::EMenuNavAction::Cancel;
		if (_Button == CManiaAppEvent::EMenuNavAction::Cancel) Truebutton = CManiaAppEvent::EMenuNavAction::Select;
	}
	
	if (! BUTTONS_MENU.existskey(Truebutton)) return "";
	
	declare ButtonByPf = BUTTONS_MENU[Truebutton];
	
	if (ButtonByPf.existskey(_Platform)) return ButtonByPf[_Platform];
	if (ButtonByPf.count > 0) return ButtonByPf[0];
	
	return "";
}
// ---------------------------------- //
/// Overload Private_GetButtonName()
Text Private_GetButtonName(CManiaAppEvent::EMenuNavAction _Button) {
	return Private_GetButtonName(GetPlatform(), _Button);
}

// ---------------------------------- //
/** Get the default name of the button if not defined in the CONST
 *
 *	@param	_Button					The button to check
 *
 *	@return							The default name for this button
 */
Text Private_Get_DefaultButtonName(CManiaAppEvent::EMenuNavAction _Button) {
	switch (_Button) {
		case CManiaAppEvent::EMenuNavAction::Up    : return "Up";
		case CManiaAppEvent::EMenuNavAction::Right : return "Right";
		case CManiaAppEvent::EMenuNavAction::Left  : return "Left";
		case CManiaAppEvent::EMenuNavAction::Down  : return "Down";
	}

	declare Splitted = TextLib::Split("::", ""^_Button);
	if (Splitted.count > 0) return Splitted[Splitted.count-1];
	
	return "";
}

// ---------------------------------- //
/** Get the path of the image associated to the given menu button
 *
 *	@param	_Platform				The platform used
 *	@param	_Button					The button to find
 *
 *	@return							The path to the image associated to the button
 */
Text GetImage(Integer _Platform, CManiaAppEvent::EMenuNavAction _Button) {
	declare ButtonName = Private_GetButtonName(_Platform, _Button);
	//log("ButtonName: " ^ ButtonName);
	
	if (ButtonName != "") {
		declare PlatformFolder = GetPlatformFolder(_Platform);
		return C_DefaultBaseDir^PlatformFolder^ButtonName^C_DefaultButtonsFormat;
	} else {
		ButtonName = Private_Get_DefaultButtonName(_Button);
		return C_DefaultBaseDir^C_DefaultButtonsFolder^ButtonName^C_DefaultButtonsFormat;
	}
	
	return "";
}
// ---------------------------------- //
/// Overload GetImage()
Text GetImage(CManiaAppEvent::EMenuNavAction _Button) {
	return GetImage(GetPlatform(), _Button);
}

// ---------------------------------- //
// Buttons editor
// ---------------------------------- //
// ---------------------------------- //
/** Get the name of the image associated to the given editor button
 *
 *	@param	_Platform				The platform used
 *	@param	_Button					The button to find
 *
 *	@return							The image associated to the button
 */
Text Private_GetButtonName(Integer _Platform, CEditorPluginEvent::EInput _Button) {
	declare CEditorPluginEvent::EInput Truebutton = _Button;
	
	if(IsJapan()) {
		if (_Button == CEditorPluginEvent::EInput::CursorPlace) Truebutton = CEditorPluginEvent::EInput::CursorDelete;
		if (_Button == CEditorPluginEvent::EInput::CursorDelete) Truebutton = CEditorPluginEvent::EInput::CursorPlace;
	}
	
	if (! BUTTONS_EDITOR.existskey(Truebutton)) return "";
	declare ButtonByPf = BUTTONS_EDITOR[Truebutton];
		
	if (ButtonByPf.existskey(_Platform)) return ButtonByPf[_Platform];
	if (ButtonByPf.count > 0) return ButtonByPf[0];
	
	return "";
}
// ---------------------------------- //
/// Overload Private_GetButtonName()
Text Private_GetButtonName(CEditorPluginEvent::EInput _Button) {
	return Private_GetButtonName(GetPlatform(), _Button);
}

// ---------------------------------- //
/** Get the default name of the button if not defined in the CONST
 *
 *	@param	_Button					The button to check
 *
 *	@return							The default name for this button
 */
Text Private_Get_DefaultButtonName(CEditorPluginEvent::EInput _Button) {
	declare Splitted = TextLib::Split("::", ""^_Button);
	if (Splitted.count > 0) return Splitted[Splitted.count-1];
	
	return "";
}

// ---------------------------------- //
/** Get the path of the image associated to the given editor button
 *
 *	@param	_Platform				The platform used
 *	@param	_Button					The button to find
 *
 *	@return							The path to the image associated to the button
 */
Text GetImage(Integer _Platform, CEditorPluginEvent::EInput _Button) {
	declare ButtonName = Private_GetButtonName(_Platform, _Button);
	
	if (ButtonName != "") {
		declare PlatformFolder = GetPlatformFolder(_Platform);
		return C_DefaultBaseDir^PlatformFolder^ButtonName^C_DefaultButtonsFormat;
	} else {
		ButtonName = Private_Get_DefaultButtonName(_Button);
		return C_DefaultBaseDir^C_DefaultButtonsFolder^ButtonName^C_DefaultButtonsFormat;
	}
	
	return "";
}
// ---------------------------------- //
/// Overload GetImage()
Text GetImage(CEditorPluginEvent::EInput _Button) {
	return GetImage(GetPlatform(), _Button);
}

// ---------------------------------- //
// Buttons alternate
// ---------------------------------- //
Text GetAlternateImage(Text _Button) {
	declare PlatformFolder = GetPlatformFolder(GetPlatform());
	
	if (ALTERNATE_BUTTONS_NAMED.existskey(_Button)) {
		return C_DefaultBaseDir^PlatformFolder^ALTERNATE_BUTTONS_NAMED[_Button][GetPlatform()];
	}
	
	return "";
}

Text GetAlternateImage(Integer _PlatformIndex, Text _Button) {
	declare PlatformFolder = GetPlatformFolder(_PlatformIndex);
	// log(PlatformFolder);
	if (ALTERNATE_BUTTONS_NAMED.existskey(_Button)) {
		return C_DefaultBaseDir^PlatformFolder^ALTERNATE_BUTTONS_NAMED[_Button][_PlatformIndex];
	}
	
	return "";
}

// ---------------------------------- //
/// Unload the library
Void Unload() {

}

// ---------------------------------- //
/// Load the library
Void Load(CInputPad::EPadType _PadModelName) {

	Unload();
	
	declare PlatformIndex = GetPlatformIndexFromSystem(""^SystemPlatform);
	//HACK - Force la version ps4
	//PlatformIndex = 0;
	//HACKEND
	
	if(_PadModelName == CInputPad::EPadType::PlayStation)
	{
		PlatformIndex = 0;
	}
	else if(_PadModelName == CInputPad::EPadType::XBox)
	{
		PlatformIndex = 1;
	}
	Private_SetPlatform(PlatformIndex);
}
Void Load() {
	Load(CInputPad::EPadType::Keyboard);
}

Text GetPadName() {
	declare Integer Platform = GetPlatform();
	switch(Platform) {
		// ne pas modifier, c'est TRC.
		case C_Platforms["PS4"] : return _("DUALSHOCK®4 Wireless Controller");
		case C_Platforms["XB1"] : return _("Xbox One Wireless Controller");
	}
	
	return _("controller");
}

Text GetLeftStickName() {
	switch(GetPlatform()) {
		// ne pas modifier, c'est TRC.
		case C_Platforms["PS4"] : return _("|PS4|the left stick");
		case C_Platforms["XB1"] : return _("|XB1|the left stick");
	}
	
	return _("|PC|the left stick");
}

Text GetRightStickName() {
	switch(GetPlatform()) {
		// ne pas modifier, c'est TRC.
		case C_Platforms["PS4"] : return _("|PS4|the right stick");
		case C_Platforms["XB1"] : return _("|XB1|the right stick");
	}
	
	return _("|PC|the right stick");
}

Text GetDirectionalButtonsName() {
	switch(GetPlatform()) {
		// ne pas modifier, c'est TRC.
		case C_Platforms["PS4"] : return _("|PS4|the directional buttons");
		case C_Platforms["XB1"] : return _("|XB1|the directional buttons");
	}
	
	return _("|PC|the directional buttons");
}

// EG : arg, bon, je vais pas tout changer pour ça, mais c'est un peu moche.
Text GetChangeCamButtonName() {
	declare Integer Platform = GetPlatform();
	switch(Platform) {
		case C_Platforms["PS4"] : return _("R3 button");
		case C_Platforms["XB1"] : return "RS";
	}
	
	return _("The Right Stick");
}

Text GetChangeEditorModeName() {
	declare Integer Platform = GetPlatform();
	switch(Platform) {
		case C_Platforms["PS4"] : return _("L3 button");
		case C_Platforms["XB1"] : return "LS";
	}
	
	return _("The Left Stick");
}

Text GetL1ButtonName() {
	declare Integer Platform = GetPlatform();
	switch(Platform) {
		case C_Platforms["PS4"] : return _("L1 button");
		case C_Platforms["XB1"] : return "LB";
	}
	
	return "LB";
}

Text GetR1ButtonName() {
	declare Integer Platform = GetPlatform();
	switch(Platform) {
		case C_Platforms["PS4"] : return _("R1 button");
		case C_Platforms["XB1"] : return "RB";
		// case C_Platforms["PC"]	: 
	}
	
	return "RB";
}

Text GetSelectButtonName() {
	declare Integer Platform = GetPlatform();
	switch(Platform) {
		case C_Platforms["PS4"] : {
			if(IsJapan()) 	return _("|PS4 textual button name|Circle button");
			else 			return _("|PS4 textual button name|Cross button");
		}
		case C_Platforms["XB1"] : return "A";
	}
	
	return "A";
}


Text InjectCountryCodeToFlagConverter() {
	declare Text TaiwanCase = "";
	if(IsJapan()) {
		TaiwanCase = """if(_CountryCode == "ROC") return "CHN.dds";""";
	}
	
	return """
		Text GetCountryFlag(Text _CountryCode) {
			{{{TaiwanCase}}}
			return _CountryCode^".dds";
		}
		""";
}

Text Inject_SetInputInfoTextSelected() {
	declare Text Function = """
		Void SetInputInfoTextSelected(CMlLabel _Label, Boolean _Selected, Vec3 _DefaultColor) {
			if (_Selected)
				_Label.TextColor = <1.000, 0.285, 0.000>;
			else
				_Label.TextColor = _DefaultColor;
		}
		Void SetInputInfoTextSelected(CMlLabel _Label, Boolean _Selected) {
			SetInputInfoTextSelected(_Label, _Selected, <0.87, 0.87, 0.87>); //ddd
		}
	""";
	
	return Function;
}

Text GetIngameRestartImage() {
	if(IsJapan()) return GetImage(CManiaAppEvent::EMenuNavAction::Select);
	
	return GetImage(CManiaAppEvent::EMenuNavAction::Cancel);
}

Text GetMLFunctionPcPing() {
	declare MLText = """
	Text GetImageForButton(Text _ImagePath, CInputPad::EPadType _PadType) {
		if(_ImagePath == "{{{GetImage("PingModel")}}}") {
			if(_PadType == CInputPad::EPadType::XBox) {
				return "{{{GetImage(1, "PingModel")}}}";
			} else if(_PadType == CInputPad::EPadType::PlayStation) {
				return "{{{GetImage(0, "PingModel")}}}";
			} else if(_PadType == CInputPad::EPadType::Keyboard) {
				return "{{{GetImage(2, "PingModel")}}}";
			} else {
				return "{{{GetImage(0, "PingModel")}}}";
			}
		}
		return "{{{GetImage(1, CManiaAppEvent::EMenuNavAction::Select)}}}";
	}
	Text GetImageForButton(Text _ImagePath) {
		declare CInputPad::EPadType Net_LibLastDeviceUsed_LastDeviceTypeUsed 	for UI;
		return GetImageForButton(_ImagePath, Net_LibLastDeviceUsed_LastDeviceTypeUsed);
	}
	""";
	return MLText;
}

//CB: Tout ca est un gros hack qui appelle manuellement les platforms comme des pads quand on détecte le type utilisé
Text GetMLFunctionPcButtons() {
	declare Text CurrentPcSelectButton 			= GetImage(2, CManiaAppEvent::EMenuNavAction::Select);
	declare Text CurrentPcCancelButton			= GetImage(2, CManiaAppEvent::EMenuNavAction::Cancel);
	declare Text CurrentPcAction1Button			= GetImage(2, CManiaAppEvent::EMenuNavAction::Action1);
	declare Text CurrentPcAction2Button			= GetImage(2, CManiaAppEvent::EMenuNavAction::Action2);
	declare Text CurrentPcPageUpButton			= GetImage(2, CManiaAppEvent::EMenuNavAction::PageUp);
	declare Text CurrentPcPageDownButton		= GetImage(2, CManiaAppEvent::EMenuNavAction::PageDown);
	declare Text CurrentPcRightButton			= GetImage(2, CManiaAppEvent::EMenuNavAction::Right);
	declare Text CurrentPcLeftButton			= GetImage(2, CManiaAppEvent::EMenuNavAction::Left);
	declare Text CurrentPcUpButton				= GetImage(2, CManiaAppEvent::EMenuNavAction::Up);
	declare Text CurrentPcDownButton			= GetImage(2, CManiaAppEvent::EMenuNavAction::Down);
	declare Text CurrentPcAppMenuButton			= GetImage(2, CManiaAppEvent::EMenuNavAction::AppMenu);
	declare MLText =
	"""
		
		Text GetPcImageForButton(Text _ImagePath) {
			if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::Select)}}}") {
				return "{{{CurrentPcSelectButton}}}";
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::Cancel)}}}") {
				return "{{{CurrentPcCancelButton}}}";
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::Action1)}}}") {
				return "{{{CurrentPcAction1Button}}}";
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::Action2)}}}") {
				return "{{{CurrentPcAction2Button}}}";
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::PageUp)}}}") {
				return "{{{CurrentPcPageUpButton}}}";
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::PageDown)}}}") {
				return "{{{CurrentPcPageDownButton}}}";
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::Right)}}}") {
				return "{{{CurrentPcRightButton}}}";
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::Left)}}}") {
				return "{{{CurrentPcLeftButton}}}";
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::Up)}}}") {
				return "{{{CurrentPcUpButton}}}";
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::Down)}}}") {
				return "{{{CurrentPcDownButton}}}";
			}
			return "{{{CurrentPcSelectButton}}}";
		}
		Text GetImageForButton(Text _ImagePath, CInputPad::EPadType _PadType) {
			if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::Select)}}}") {
				if(_PadType == CInputPad::EPadType::XBox) {
					return "{{{GetImage(1, CManiaAppEvent::EMenuNavAction::Select)}}}";
				} else if(_PadType == CInputPad::EPadType::PlayStation) {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::Select)}}}";
				} else if(_PadType == CInputPad::EPadType::Keyboard) {
					return "{{{GetImage(2, CManiaAppEvent::EMenuNavAction::Select)}}}";
				} else {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::Select)}}}";
				}
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::Cancel)}}}") {
				if(_PadType == CInputPad::EPadType::XBox) {
					return "{{{GetImage(1, CManiaAppEvent::EMenuNavAction::Cancel)}}}";
				} else if(_PadType == CInputPad::EPadType::PlayStation) {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::Cancel)}}}";
				} else if(_PadType == CInputPad::EPadType::Keyboard) {
					return "{{{GetImage(2, CManiaAppEvent::EMenuNavAction::Cancel)}}}";
				} else {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::Cancel)}}}";
				}
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::Action1)}}}") {
				if(_PadType == CInputPad::EPadType::XBox) {
					return "{{{GetImage(1, CManiaAppEvent::EMenuNavAction::Action1)}}}";
				} else if(_PadType == CInputPad::EPadType::PlayStation) {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::Action1)}}}";
				} else if(_PadType == CInputPad::EPadType::Keyboard) {
					return "{{{GetImage(2, CManiaAppEvent::EMenuNavAction::Action1)}}}";
				} else {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::Action1)}}}";
				}
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::Action2)}}}") {
				if(_PadType == CInputPad::EPadType::XBox) {
					return "{{{GetImage(1, CManiaAppEvent::EMenuNavAction::Action2)}}}";
				} else if(_PadType == CInputPad::EPadType::PlayStation) {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::Action2)}}}";
				} else if(_PadType == CInputPad::EPadType::Keyboard) {
					return "{{{GetImage(2, CManiaAppEvent::EMenuNavAction::Action2)}}}";
				} else {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::Action2)}}}";
				}
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::PageUp)}}}") {
				if(_PadType == CInputPad::EPadType::XBox) {
					return "{{{GetImage(1, CManiaAppEvent::EMenuNavAction::PageUp)}}}";
				} else if(_PadType == CInputPad::EPadType::PlayStation) {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::PageUp)}}}";
				} else if(_PadType == CInputPad::EPadType::Keyboard) {
					return "{{{GetImage(2, CManiaAppEvent::EMenuNavAction::PageUp)}}}";
				} else {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::PageUp)}}}";
				}
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::PageDown)}}}") {
				if(_PadType == CInputPad::EPadType::XBox) {
					return "{{{GetImage(1, CManiaAppEvent::EMenuNavAction::PageDown)}}}";
				} else if(_PadType == CInputPad::EPadType::PlayStation) {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::PageDown)}}}";
				} else if(_PadType == CInputPad::EPadType::Keyboard) {
					return "{{{GetImage(2, CManiaAppEvent::EMenuNavAction::PageDown)}}}";
				} else {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::PageDown)}}}";
				}
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::Right)}}}") {
				if(_PadType == CInputPad::EPadType::XBox) {
					return "{{{GetImage(1, CManiaAppEvent::EMenuNavAction::Right)}}}";
				} else if(_PadType == CInputPad::EPadType::PlayStation) {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::Right)}}}";
				} else if(_PadType == CInputPad::EPadType::Keyboard) {
					return "{{{GetImage(2, CManiaAppEvent::EMenuNavAction::Right)}}}";
				} else {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::Right)}}}";
				}
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::Left)}}}") {
				if(_PadType == CInputPad::EPadType::XBox) {
					return "{{{GetImage(1, CManiaAppEvent::EMenuNavAction::Left)}}}";
				} else if(_PadType == CInputPad::EPadType::PlayStation) {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::Left)}}}";
				} else if(_PadType == CInputPad::EPadType::Keyboard) {
					return "{{{GetImage(2, CManiaAppEvent::EMenuNavAction::Left)}}}";
				} else {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::Left)}}}";
				}
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::Up)}}}") {
				if(_PadType == CInputPad::EPadType::XBox) {
					return "{{{GetImage(1, CManiaAppEvent::EMenuNavAction::Up)}}}";
				} else if(_PadType == CInputPad::EPadType::PlayStation) {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::Up)}}}";
				} else if(_PadType == CInputPad::EPadType::Keyboard) {
					return "{{{GetImage(2, CManiaAppEvent::EMenuNavAction::Up)}}}";
				} else {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::Up)}}}";
				}
			} else if(_ImagePath == "{{{GetImage(CManiaAppEvent::EMenuNavAction::Down)}}}") {
				if(_PadType == CInputPad::EPadType::XBox) {
					return "{{{GetImage(1, CManiaAppEvent::EMenuNavAction::Down)}}}";
				} else if(_PadType == CInputPad::EPadType::PlayStation) {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::Down)}}}";
				} else if(_PadType == CInputPad::EPadType::Keyboard) {
					return "{{{GetImage(2, CManiaAppEvent::EMenuNavAction::Down)}}}";
				} else {
					return "{{{GetImage(0, CManiaAppEvent::EMenuNavAction::Down)}}}";
				}
			} else if(_ImagePath == "{{{GetAlternateImage("View")}}}") {
				if(_PadType == CInputPad::EPadType::XBox) {
					return "{{{GetAlternateImage(1, "View")}}}";
				} else if(_PadType == CInputPad::EPadType::PlayStation) {
					return "{{{GetAlternateImage(0, "View")}}}";
				} else if(_PadType == CInputPad::EPadType::Keyboard) {
					return "{{{GetAlternateImage(2, "View")}}}";
				} else {
					return "{{{GetAlternateImage(0, "View")}}}";
				}
			} else if(_ImagePath == "{{{GetImage(CEditorPluginEvent::EInput::CameraDown)}}}") {
				if(_PadType == CInputPad::EPadType::XBox) {
					return "{{{GetImage(1, CEditorPluginEvent::EInput::CameraDown)}}}";
				} else if(_PadType == CInputPad::EPadType::PlayStation) {
					return "{{{GetImage(0, CEditorPluginEvent::EInput::CameraDown)}}}";
				} else if(_PadType == CInputPad::EPadType::Keyboard) {
					return "{{{GetImage(2, CEditorPluginEvent::EInput::CameraDown)}}}";
				} else {
					return "{{{GetImage(0, CEditorPluginEvent::EInput::CameraDown)}}}";
				}
			} else if(_ImagePath == "{{{GetImage(CEditorPluginEvent::EInput::CameraUp)}}}") {
				if(_PadType == CInputPad::EPadType::XBox) {
					return "{{{GetImage(1, CEditorPluginEvent::EInput::CameraUp)}}}";
				} else if(_PadType == CInputPad::EPadType::PlayStation) {
					return "{{{GetImage(0, CEditorPluginEvent::EInput::CameraUp)}}}";
				} else if(_PadType == CInputPad::EPadType::Keyboard) {
					return "{{{GetImage(2, CEditorPluginEvent::EInput::CameraUp)}}}";
				} else {
					return "{{{GetImage(0, CEditorPluginEvent::EInput::CameraUp)}}}";
				}
			} else if(_ImagePath == "{{{GetImage("LeftStick")}}}") {
				if(_PadType == CInputPad::EPadType::XBox) {
					return "{{{GetImage(1, "LeftStick")}}}";
				} else if(_PadType == CInputPad::EPadType::PlayStation) {
					return "{{{GetImage(0, "LeftStick")}}}";
				} else if(_PadType == CInputPad::EPadType::Keyboard) {
					return "{{{GetImage(2, "LeftStick")}}}";
				} else {
					return "{{{GetImage(0, "LeftStick")}}}";
				}
			} else if(_ImagePath == "{{{GetImage("RightStick")}}}") {
				if(_PadType == CInputPad::EPadType::XBox) {
					return "{{{GetImage(1, "RightStick")}}}";
				} else if(_PadType == CInputPad::EPadType::PlayStation) {
					return "{{{GetImage(0, "RightStick")}}}";
				} else if(_PadType == CInputPad::EPadType::Keyboard) {
					return "{{{GetImage(2, "RightStick")}}}";
				} else {
					return "{{{GetImage(0, "RightStick")}}}";
				}
			} 
			return "{{{GetImage(1, CManiaAppEvent::EMenuNavAction::Select)}}}";
		}
		Text GetImageForButton(Text _ImagePath) {
			declare CInputPad::EPadType Net_LibLastDeviceUsed_LastDeviceTypeUsed 	for UI;
			return GetImageForButton(_ImagePath, Net_LibLastDeviceUsed_LastDeviceTypeUsed);
		}
		Text GetL1ButtonName() {
			declare CInputPad::EPadType Net_LibLastDeviceUsed_LastDeviceTypeUsed 	for UI;
			if(Net_LibLastDeviceUsed_LastDeviceTypeUsed == CInputPad::EPadType::XBox) {
				return "LB";
			} else if(Net_LibLastDeviceUsed_LastDeviceTypeUsed == CInputPad::EPadType::PlayStation) {
				return _("L1 button");
			} else if(Net_LibLastDeviceUsed_LastDeviceTypeUsed == CInputPad::EPadType::Keyboard) {
				//L16N. Name of the key page up
				return _("Page Up");
			}
			return "LB";
		}
		Text GetR1ButtonName() {
			declare CInputPad::EPadType Net_LibLastDeviceUsed_LastDeviceTypeUsed 	for UI;
			if(Net_LibLastDeviceUsed_LastDeviceTypeUsed == CInputPad::EPadType::XBox) {
				return "RB";
			} else if(Net_LibLastDeviceUsed_LastDeviceTypeUsed == CInputPad::EPadType::PlayStation) {
				return _("R1 button");
			} else if(Net_LibLastDeviceUsed_LastDeviceTypeUsed == CInputPad::EPadType::Keyboard) {
				//L16N. Name of the key page down
				return _("Page Down");
			}
			return "RB";
		}
	""";
	
	return MLText;
}