/** 
 *	Debug library
 */

#Const Version		"2014-07-04"
#Const ScriptName	"Debug.Script.txt"

#Include "TextLib" as TL

// ---------------------------------- //
// Constants
// ---------------------------------- //
#Const C_Labels_Style ["AvatarButtonNormal","BgMainMenuTitleHeader","Default","FrameTransitionFromLeft","FrameTransitionsFromRight","ListItemMedal","Manialink_Body","ProgressBar","ProgressBarSmall","SliderSmall","SliderVolume", "StyleTextScriptEditor","StyleValueYellowSmall","TextActionMaker","TextButtonBig","TextButtonMedium","TextButtonNav","TextButtonNavBack","TextButtonSmall","TextCardInfoSmall","TextCardInfoVerySmall","TextCardMedium","TextCardMediumWhite","TextCardRaceRank","TextCardScores2","TextCardSmall","TextCardSmallScores2","TextCardSmallScores2Rank","TextChallengeNameMedal","TextChallengeNameMedalNone","TextChallengeNameMedium","TextChallengeNameSmall","TextCongratsBig","TextCredits","TextCreditsTitle","TextEditorArticle","TextInfoMedium","TextInfoSmall","TextPlayerCardName","TextPlayerCardScore","TextRaceChat","TextRaceChrono","TextRaceChronoError","TextRaceChronoOfficial","TextRaceChronoWarning","TextRaceMessage","TextRaceMessageBig","TextRaceStaticSmall","TextRaceValueSmall","TextRankingsBig","TextSPScoreBig","TextSPScoreMedium","TextSPScoreSmall","TextStaticMedium","TextStaticSmall","TextStaticVerySmall","TextSubTitle1","TextSubTitle2","TextTips","TextTitle1","TextTitle2","TextTitle2Blink","TextTitle3","TextTitle3Header","TextTitleError","TextToolTipAM","TextToolTipAMBig","TextValueBig","TextValueMedium","TextValueMediumSm","TextValueSmall","TextValueSmallSm","TrackerText","TrackerTextBig","TrackListItem","TrackListLine","UiDriving_BgBottom","UiDriving_BgCard","UiDriving_BgCenter"]

#Const C_Buttons_Style ["CardButtonMedium","CardButtonMediumL","CardButtonMediumS","CardButtonMediumWide","CardButtonMediumXL","CardButtonMediumXS","CardButtonMediumXXL","CardButtonMediumXXXL","CardButtonSmall","CardButtonSmallL","CardButtonSmallS","CardButtonSmallWide","CardButtonSmallXL","CardButtonSmallXS","CardButtonSmallXXL","CardButtonSmallXXXL","CardMain_Quit","CardMain_Tool","CardMain_Tool_NoBg","CardMain_Tool_NoBg2"]

#Const C_Sounds_Style ["Bonus"=>[0,1,2,3,4,5],"Capture"=>[0],"Checkpoint"=>[0,1],"Default"=>[0],"EndMatch"=>[0,1],"EndRound"=>[0],"Finish"=>[0,1],"FirstHit"=>[0],"Notice"=>[0],"PhaseChange"=>[0,1],"PlayerEliminated"=>[0],"PlayerHit"=>[0],"RankChange"=>[0,1],"Record"=>[0,1,2,3],"ScoreProgress"=>[0,1,2,3],"Silence"=>[0],"StartMatch"=>[0,1,2],"StartRound"=>[0],"TieBreakPoint"=>[0,1,2,3,4],"TiePoint"=>[0,1,2,3,4],"TimeOut"=>[0,1],"VictoryPoint"=>[0],"Warning"=>[0]]

// ---------------------------------- //
// Globales
// ---------------------------------- //
declare Ident G_LibDebug_LayerMenuId;	///< Id of the menu layer
declare Ident G_LibDebug_LayerStylesId;	///< Id of the styles layer
declare Ident[] G_LibDebug_Admins;		///< List of players allowed to use the debug panel

// ---------------------------------- //
// Functions
// ---------------------------------- //

// ---------------------------------- //
// Private
// ---------------------------------- //
Text Private_CreateManialinkMenu() {
	declare BS = "style=\"CardButtonMedium\"";
	
	return """
<manialink version="1" name="Lib_Debug:Menu">
<frame posn="155 82 20" id="Frame_Global">
	<frame posn="0 0" id="Frame_SectionSelection">
		<label posn="0 0" scale="0.5" halign="right" valign="center" {{{BS}}} text="Mode" scriptevents="1" id="Button_SectionMode" />
		<label posn="-20 0" scale="0.5" halign="right" valign="center" {{{BS}}} text="Bots" scriptevents="1" id="Button_SectionBot" />
		<label posn="-40 0" scale="0.5" halign="right" valign="center" {{{BS}}} text="Players" scriptevents="1" id="Button_SectionPlayers" />
		<label posn="-60 0" scale="0.5" halign="right" valign="center" {{{BS}}} text="Links" scriptevents="1" id="Button_SectionLinks" />
	</frame>
	<frame posn="0 -8" hidden="1" id="Frame_SectionLinks">
		<quad posn="2 4 -1" sizen="50 30" halign="right" style="Bgs1InRace" substyle="BgWindow3" />
		<label posn="-1 2" sizen="50 5" halign="right" textemboss="1" text="Links" />
		<quad posn="0 -3" sizen="45 0.3" halign="right" bgcolor="000" />
		<frame posn="0 -6">
			<format textemboss="1" />
			<label posn="0 0" sizen="18 5" halign="right" text="$hml-styles$h" />
			<label posn="0 -5" sizen="18 5" halign="right" text="$hexemple$h" />
			<label posn="0 -10" sizen="18 5" halign="right" text="$hlabels$h" />
		</frame>
		<frame posn="-20 -6">
			
		</frame>
	</frame>
	<frame posn="0 -8" hidden="1" id="Frame_SectionPlayers">
		<quad posn="2 4 -1" sizen="50 30" halign="right" style="Bgs1InRace" substyle="BgWindow3" />
		<label posn="-1 2" sizen="50 5" halign="right" textemboss="1" text="Players" />
		<quad posn="0 -3" sizen="45 0.3" halign="right" bgcolor="000" />
		<frame posn="0 -6">
			
		</frame>
		<frame posn="-20 -6">
			
		</frame>
	</frame>
	<frame posn="0 -8" hidden="1" id="Frame_SectionBot">
		<quad posn="2 4 -1" sizen="50 35" halign="right" style="Bgs1InRace" substyle="BgWindow3" />
		<label posn="-1 2" sizen="50 5" halign="right" textemboss="1" text="Bots" />
		<quad posn="0 -3" sizen="45 0.3" halign="right" bgcolor="000" />
		<frame posn="0 -6">
			<label posn="0 0" scale="0.5" halign="right" valign="center" {{{BS}}} text="Add bot" scriptevents="1" id="Button_AddBot" />
			<label posn="0 -5" scale="0.5" halign="right" valign="center" {{{BS}}} text="Remove bot" scriptevents="1" id="Button_RemoveBot" />
			<label posn="0 -10" scale="0.5" halign="right" valign="center" {{{BS}}} text="Remove bots" scriptevents="1" id="Button_RemoveBots" />
			<label posn="0 -15" scale="0.5" halign="right" valign="center" {{{BS}}} text="Remove all bots" scriptevents="1" id="Button_RemoveAllBots" />
			<quad posn="-18 -20" sizen="3 3" halign="left" valign="center" style="Icons64x64_1" substyle="Check" scriptevents="1" id="Quad_KeepBot" />
		</frame>
		<frame posn="-20 -6">
			<entry posn="0 0" sizen="7 5" scale="0.8" halign="right" valign="center" textemboss="1" id="Entry_AddBotClan" />
			<label posn="-7 0" sizen="6 5" halign="right" valign="center2" textsize="2" textemboss="1" text="Clan:" />
			<entry posn="-14 0" sizen="7 5" scale="0.8" halign="right" valign="center" textemboss="1" id="Entry_AddBotNb" />
			<label posn="-21 0" sizen="6 5" halign="right" valign="center2" textsize="2" textemboss="1" text="Nb:" />
			<entry posn="0 -5" sizen="7 5" scale="0.8" halign="right" valign="center" textemboss="1" id="Entry_RemoveBotName" />
			<label posn="-7 -5" sizen="10 5" halign="right" valign="center2" textsize="2" textemboss="1" text="#" />
			<entry posn="0 -10" sizen="7 5" scale="0.8" halign="right" valign="center" textemboss="1" id="Entry_RemoveBotsNb" />
			<label posn="-7 -10" sizen="10 5" halign="right" valign="center2" textsize="2" textemboss="1" text="Nb:" />
			<label posn="0 -20" sizen="26 5" halign="right" valign="center2" textsize="2" textemboss="1" text="Restore bots at restart" />
		</frame>
	</frame>
	<frame posn="0 -8" hidden="1" id="Frame_SectionMode">
		<quad posn="2 4 -1" sizen="50 30" halign="right" style="Bgs1InRace" substyle="BgWindow3" />
		<label posn="-1 2" sizen="50 5" halign="right" textemboss="1" text="Mode" />
		<quad posn="0 -3" sizen="45 0.3" halign="right" bgcolor="000" />
		<frame posn="0 -6">
			<quad posn="-18 0" sizen="3 3" halign="left" valign="center" style="Icons64x64_1" substyle="LvlGreen" scriptevents="1" id="Quad_UsePvPCollisions" />
			<quad posn="-18 -5" sizen="3 3" halign="left" valign="center" style="Icons64x64_1" substyle="LvlGreen" scriptevents="1" id="Quad_UsePvPWeapons" />
			<quad posn="-18 -10" sizen="3 3" halign="left" valign="center" style="Icons64x64_1" substyle="Check" scriptevents="1" id="Quad_UseAllies" />
			<label posn="0 -15" scale="0.5" halign="right" valign="center" {{{BS}}} text="Set timer" scriptevents="1" id="Button_SetTimer" />
		</frame>
		<frame posn="-20 -6">
			<label posn="0 0" sizen="26 5" halign="right" valign="center2" textsize="2" textemboss="1" text="Use PvP collisions" />
			<label posn="0 -5" sizen="26 5" halign="right" valign="center2" textsize="2" textemboss="1" text="Use PvP weapons" />
			<label posn="0 -10" sizen="26 5" halign="right" valign="center2" textsize="2" textemboss="1" text="Use allies" />
			<entry posn="0 -15" sizen="20 5" scale="0.8" halign="right" valign="center" textemboss="1" id="Entry_SetTimer" />
			<label posn="-17 -15" sizen="10 5" halign="right" valign="center2" textsize="2" textemboss="1" text="(ms)" />
		</frame>
	</frame>
</frame>
<script><!--
declare CMlFrame[Text] Frame_Sections;

Void UpdateSectionVisibility(Text _SectionName) {
	declare CurrentSection for This = "";
	
	foreach (SectionName => Frame_Section in Frame_Sections) {
		if (SectionName == _SectionName) {
			if (!Frame_Section.Visible) {
				Frame_Section.Show();
				CurrentSection = _SectionName;
			}
			else {
				Frame_Section.Hide();
				CurrentSection = "";
			}
		} else {
			if (Frame_Section.Visible) Frame_Section.Hide();
		}
	}
}

Void UpdateCheckbox(CMlQuad _Checkbox, Boolean _Value) {
	if (_Checkbox == Null) return;
	
	if (_Value) _Checkbox.Substyle = "LvlGreen";
	else _Checkbox.Substyle="Check";
}

main() {
	Frame_Sections = [
		"Links"		=> (Page.GetFirstChild("Frame_SectionLinks")	as CMlFrame),
		"Players"	=> (Page.GetFirstChild("Frame_SectionPlayers")	as CMlFrame),
		"Bot"		=> (Page.GetFirstChild("Frame_SectionBot")		as CMlFrame),
		"Mode"		=> (Page.GetFirstChild("Frame_SectionMode")		as CMlFrame)
	];
	
	declare Entry_AddBotClan	<=> (Page.GetFirstChild("Entry_AddBotClan")		as CMlEntry);
	declare Entry_AddBotNb		<=> (Page.GetFirstChild("Entry_AddBotNb")		as CMlEntry);
	declare Entry_RemoveBotName	<=> (Page.GetFirstChild("Entry_RemoveBotName")	as CMlEntry);
	declare Entry_RemoveBotsNb	<=> (Page.GetFirstChild("Entry_RemoveBotsNb")	as CMlEntry);
	declare Quad_KeepBot		<=> (Page.GetFirstChild("Quad_KeepBot")			as CMlQuad);
	declare Entry_SetTimer		<=> (Page.GetFirstChild("Entry_SetTimer")		as CMlEntry);
	
	declare netwrite Integer Net_LibDebug_ActionUpdate for UI;
	declare netwrite Text Net_LibDebug_ActionName for UI;
	declare netwrite Text Net_LibDebug_ActionParams for UI;
	Net_LibDebug_ActionUpdate = 0;
	Net_LibDebug_ActionName = "";
	Net_LibDebug_ActionParams = "";
	
	declare CurrentSection for This = "";
	declare KeepBot for This = False;
	
	UpdateSectionVisibility(CurrentSection);
	UpdateCheckbox(Quad_KeepBot, KeepBot);
	
	declare UsePvPCollisions = True;
	declare UsePvPWeapons = True;
	declare UseAllies = False;
	
	while (True) {
		yield;
		
		if (InputPlayer == Null) continue;
		if (!PageIsVisible) continue;
		
		foreach (Event in PendingEvents) {
			if (Event.Type == CMlEvent::Type::MouseClick) {
				switch (Event.ControlId) {
					case "Button_SectionLinks": {
						UpdateSectionVisibility("Links");
					}
					case "Button_SectionPlayers": {
						UpdateSectionVisibility("Players");
					}
					case "Button_SectionBot": {
						UpdateSectionVisibility("Bot");
					}
					case "Button_SectionMode": {
						UpdateSectionVisibility("Mode");
					}
					case "Button_AddBot": {
						Net_LibDebug_ActionUpdate = Now;
						Net_LibDebug_ActionName = "AddBot";
						Net_LibDebug_ActionParams = Entry_AddBotClan.Value^"|"^Entry_AddBotNb.Value;
					}
					case "Button_RemoveBot": {
						Net_LibDebug_ActionUpdate = Now;
						Net_LibDebug_ActionName = "RemoveBot";
						Net_LibDebug_ActionParams = Entry_RemoveBotName.Value;
					}
					case "Button_RemoveBots": {
						Net_LibDebug_ActionUpdate = Now;
						Net_LibDebug_ActionName = "RemoveBots";
						Net_LibDebug_ActionParams = Entry_RemoveBotsNb.Value;
					}
					case "Button_RemoveAllBots": {
						Net_LibDebug_ActionUpdate = Now;
						Net_LibDebug_ActionName = "RemoveAllBots";
						Net_LibDebug_ActionParams = "";
					}
					case "Quad_KeepBot": {
						KeepBot = !KeepBot;
						UpdateCheckbox((Event.Control as CMlQuad), KeepBot);
						Net_LibDebug_ActionUpdate = Now;
						Net_LibDebug_ActionName = "KeepBot";
						if (KeepBot) Net_LibDebug_ActionParams = "True";
						else Net_LibDebug_ActionParams = "False";
					}
					case "Quad_UsePvPCollisions": {
						UsePvPCollisions = !UsePvPCollisions;
						UpdateCheckbox((Event.Control as CMlQuad), UsePvPCollisions);
						Net_LibDebug_ActionUpdate = Now;
						Net_LibDebug_ActionName = "UsePvPCollisions";
						if (UsePvPCollisions) Net_LibDebug_ActionParams = "True";
						else Net_LibDebug_ActionParams = "False";
					}
					case "Quad_UsePvPWeapons": {
						UsePvPWeapons = !UsePvPWeapons;
						UpdateCheckbox((Event.Control as CMlQuad), UsePvPWeapons);
						Net_LibDebug_ActionUpdate = Now;
						Net_LibDebug_ActionName = "UsePvPWeapons";
						if (UsePvPWeapons) Net_LibDebug_ActionParams = "True";
						else Net_LibDebug_ActionParams = "False";
					}
					case "Button_SetTimer": {
						Net_LibDebug_ActionUpdate = Now;
						Net_LibDebug_ActionName = "SetTimer";
						Net_LibDebug_ActionParams = Entry_SetTimer.Value;
					}
					case "Quad_UseAllies": {
						UseAllies = !UseAllies;
						UpdateCheckbox((Event.Control as CMlQuad), UseAllies);
						Net_LibDebug_ActionUpdate = Now;
						Net_LibDebug_ActionName = "UseAllies";
						if (UseAllies) Net_LibDebug_ActionParams = "True";
						else Net_LibDebug_ActionParams = "False";
					}
				}
			}
		}
	}
}
--></script>
</manialink>
""";
}

Text Private_CreateManialinkLog() {
	/// Une fenêtre qui affiche les derniers logs créés avec la fonction Log() de la lib
	return "";
}

Text Private_CreateManialinkStyles() {
	declare ItemNbX = 15;
	declare ItemNbY = 3;
	declare ItemsSizeX = 300.;
	declare ItemsSizeY = 60.;
	declare ItemMarginX = 1.;
	declare ItemMarginY = 1.;
	declare ItemSizeX = (ItemsSizeX - ((ItemNbX - 1) * ItemMarginX)) / ItemNbX;
	declare ItemSizeY = (ItemsSizeY - ((ItemNbY - 1) * ItemMarginY)) / ItemNbY;
	
	declare PosnX = 0.;
	declare PosnY = 0.;
	declare K = 0;
	
	declare Quads_Style = "";
	for (I, 1, ItemNbY) {
		for (J, 1, ItemNbX) {
			PosnX = (J - 1) * (ItemSizeX + ItemMarginX);
			PosnY = (I - 1) * (ItemSizeY + ItemMarginY) * -1.;
			Quads_Style ^= """<quad posn="{{{PosnX^" "^PosnY}}}" sizen="{{{ItemSizeX^" "^ItemSizeY}}}" style="Bgs1" substyle="BgWindow2" scriptevents="1" class="Quad_Style" id="{{{K}}}" />""";
			K += 1;
		}
	}
	
	declare Gauges_Style = "";
	K = 0;
	for (I, 1, ItemNbY*2) {
		for (J, 1, ItemNbX) {
			if (J % 2 == 0) continue;
			PosnX = (J - 1) * (ItemSizeX + ItemMarginX);
			PosnY = (I - 1) * (ItemSizeY + ItemMarginY) * -0.5;
			Gauges_Style ^= """<quad posn="{{{PosnX^" "^PosnY}}}" sizen="{{{ItemSizeX*2.^" "^ItemSizeY*0.5}}}" scriptevents="1" class="Gauge_Style" id="{{{K}}}" /><gauge posn="{{{PosnX^" "^PosnY}}} 1" sizen="{{{ItemSizeX*2.^" "^ItemSizeY*0.5}}}" style="" ratio="0.5" drawbg="1" drawblockbg="1" />""";
			K += 1;
		}
	}
	
	declare SampleText = "SampleText";
	K = 0;
	declare Labels_Style = "";
	declare Labels_Style2 = "";
	for (I, 1, 8) {
		for (J, 1, 10) {
			PosnX = (J - 1) * 30.;
			PosnY = (I - 1) * -8.;
			Labels_Style ^= """<label posn="{{{PosnX^" "^PosnY}}}" sizen="28 5" style="{{{C_Labels_Style[K]}}}" text="{{{SampleText}}}" scriptevents="1" class="Label_Style" id="{{{C_Labels_Style[K]}}}" />""";
			Labels_Style2 ^= """<label posn="{{{PosnX^" "^PosnY}}}" sizen="28 5" style="{{{C_Labels_Style[K]}}}" text="{{{SampleText}}}" />""";
			Labels_Style2 ^= """<quad posn="{{{PosnX^" "^PosnY}}}" sizen="28 5" scriptevents="1" class="Label_Style" id="{{{C_Labels_Style[K]}}}" />""";
			K += 1;
			if (!C_Labels_Style.existskey(K)) break;
		}
		if (!C_Labels_Style.existskey(K)) break;
	}
	
	K = 0;
	declare Buttons_Style = "";
	for (I, 1, 8) {
		for (J, 1, 4) {
			PosnX = (J - 1) * 70.;
			PosnY = (I - 1) * -12.;
			Buttons_Style ^= """<label posn="{{{PosnX^" "^PosnY}}}" sizen="28 5" style="{{{C_Buttons_Style[K]}}}" text="{{{SampleText}}}" scriptevents="1" class="Label_Style" id="{{{C_Buttons_Style[K]}}}" />""";
			K += 1;
			if (!C_Buttons_Style.existskey(K)) break;
		}
		if (!C_Buttons_Style.existskey(K)) break;
	}
	
	K = 0;
	
	declare Sounds_Style = "";
	declare Sounds_Switch = "";
	declare PlayButtonSize = <10., 7.>;
	declare L = 0;
	declare M = 0;
	foreach (SoundName => SoundVariants in C_Sounds_Style) {
		L += 1;
		PosnX = (L - 1) * (PlayButtonSize.X + 3.2);
		Sounds_Switch ^= """case "{{{SoundName}}}": SoundName = ::EUISound::{{{SoundName}}};""";
		Sounds_Style ^= """<label posn="{{{PosnX+(PlayButtonSize.X/2.)}}} -3" sizen="{{{PlayButtonSize.X+2.}}} {{{PlayButtonSize.X}}}" halign="center" textsize="1" textemboss="1" text="{{{SoundName}}}" />""";
		foreach (SoundVariant in SoundVariants) {
			M += 1;
			PosnY = M * (PlayButtonSize.Y + 2.) * -1.;
			Sounds_Style ^= """<quad posn="{{{PosnX^" "^PosnY}}}" sizen="{{{PlayButtonSize.X^" "^PlayButtonSize.Y}}}" image="http://utils.bside-community.com/maniastyles/img/Play_1.png" imagefocus="http://utils.bside-community.com/maniastyles/img/Play_2.png" scriptevents="1" class="Sound_Style" id="{{{SoundName}}}|{{{SoundVariant}}}" />""";
		}
		M = 0;
	}
	
	return """
<manialink version="1" name="Lib_Debug:Styles">
<frame posn="-157 88 57" id="Frame_Visibility">
	<label style="CardButtonSmallS" text="Open ML Styles" scriptevents="1" id="Button_Visibility" />
</frame>
<frame posn="0 0 56" hidden="1" id="Frame_Global">
	<quad posn="0 0 1" sizen="320 180" halign="center" valign="center" bgcolor="777" />
	<quad posn="-152 80 2" sizen="56 58" bgcolor="555" />
	<quad posn="-92 80 2" sizen="110 58" bgcolor="555" />
	<quad posn="22 80 2" sizen="56 28" bgcolor="555" />
	<quad posn="22 48 2" sizen="56 26" bgcolor="555" />
	<quad posn="82 66 2" sizen="70 14" bgcolor="555" />
	<quad posn="82 48 2" sizen="56 26" bgcolor="555" />
	<quad posn="-152 -6 2" sizen="304 10" bgcolor="555" />
	<quad posn="-152 -18 2" sizen="{{{ItemsSizeX+4.}}} {{{ItemsSizeY+4.}}}" image="http://utils.bside-community.com/maniastyles/img/full_1.png" colorize="555" id="Quad_StyleBg" />
	<quad posn="-152 -18 2" sizen="{{{ItemsSizeX+4.}}} {{{ItemsSizeY+4.}}}" image="http://utils.bside-community.com/maniastyles/img/lalala.bik" hidden="1" id="Quad_StyleBg2" />
	<frame posn="0 0 3">
		<frame posn="112 80" id="Frame_Bookmark">
			<quad sizen="40 10" bgcolor="007" />
			<include url="http://maniahome.maniaplanet.com/add/?url=ml-styles"/>
		</frame>
		<frame posn="150 64" hidden="1" id="Frame_Stats">
			<format textemboss="1" />
			<label posn="0 0" sizen="60" halign="right" text="Manialink displayed: 0" />
		</frame>
		<frame posn="-150 72" id="Frame_StyleSelector">
			<frame posn="0 0">
				<label posn="0 4" scale="0.5" valign="center2" style="TextRaceMessageBig" text="Background" />
				<label posn="0 0" style="CardButtonSmallXL" text="Bgs1" scriptevents="1" class="Button_QuadStyleSelector" id="Bgs1" />
				<label posn="0 -6" style="CardButtonSmallXL" text="Bgs1InRace" scriptevents="1" class="Button_QuadStyleSelector" id="Bgs1InRace" />
				<label posn="0 -12" style="CardButtonSmallXL" text="BgsPlayerCard" scriptevents="1" class="Button_QuadStyleSelector" id="BgsPlayerCard" />
				<label posn="0 -18" style="CardButtonSmallXL" text="UiSMSpectatorScoreBig" scriptevents="1" class="Button_QuadStyleSelector" id="UiSMSpectatorScoreBig" />
				<label posn="0 -24" style="CardButtonSmallXL" text="EnergyBar" scriptevents="1" class="Button_QuadStyleSelector" id="EnergyBar" />
				<label posn="0 -30" style="CardButtonSmallXL" text="BgsButtons" scriptevents="1" class="Button_QuadStyleSelector" id="BgsButtons" />
			</frame>
			<frame posn="60 0">
				<label posn="0 4" scale="0.5" valign="center2" style="TextRaceMessageBig" text="Icons" />
				<label posn="0 0" style="CardButtonSmall" text="Icons128x128_1" scriptevents="1" class="Button_QuadStyleSelector" id="Icons128x128_1" />
				<label posn="0 -6" style="CardButtonSmall" text="Icons128x128_Blink" scriptevents="1" class="Button_QuadStyleSelector" id="Icons128x128_Blink" />
				<label posn="0 -12" style="CardButtonSmall" text="Icons128x32_1" scriptevents="1" class="Button_QuadStyleSelector" id="Icons128x32_1" />
				<label posn="0 -18" style="CardButtonSmall" text="Icons64x64_1" scriptevents="1" class="Button_QuadStyleSelector" id="Icons64x64_1" />
				<label posn="0 -24" style="CardButtonSmall" text="Icons64x64_2" scriptevents="1" class="Button_QuadStyleSelector" id="Icons64x64_2" />
				<label posn="0 -30" style="CardButtonSmall" text="321Go" scriptevents="1" class="Button_QuadStyleSelector" id="321Go" />
				<label posn="0 -36" style="CardButtonSmall" text="BgsChallengeMedals" scriptevents="1" class="Button_QuadStyleSelector" id="BgsChallengeMedals" />
				<label posn="0 -42" style="CardButtonSmall" text="BgRaceScore2" scriptevents="1" class="Button_QuadStyleSelector" id="BgRaceScore2" />
			</frame>
			<frame posn="99 0">
				<label posn="0 0" style="CardButtonSmall" text="UIConstruction_Buttons" scriptevents="1" class="Button_QuadStyleSelector" id="UIConstruction_Buttons" />
				<label posn="0 -6" style="CardButtonSmall" text="UIConstruction_Buttons2" scriptevents="1" class="Button_QuadStyleSelector" id="UIConstruction_Buttons2" />
				<label posn="0 -12" style="CardButtonSmall" text="UIConstructionBullet_Buttons" scriptevents="1" class="Button_QuadStyleSelector" id="UIConstructionBullet_Buttons" />
				<label posn="0 -18" style="CardButtonSmall" text="ManiaPlanetLogos" scriptevents="1" class="Button_QuadStyleSelector" id="ManiaPlanetLogos" />
				<label posn="0 -24" style="CardButtonSmall" text="ManiaplanetSystem" scriptevents="1" class="Button_QuadStyleSelector" id="ManiaplanetSystem" />
				<label posn="0 -30" style="CardButtonSmall" text="ManiaPlanetMainMenu" scriptevents="1" class="Button_QuadStyleSelector" id="ManiaPlanetMainMenu" />
				<label posn="0 -36" style="CardButtonSmall" text="Hud3dIcons" scriptevents="1" class="Button_QuadStyleSelector" id="Hud3dIcons" />
				<label posn="0 -42" style="CardButtonSmall" text="Hud3dEchelons" scriptevents="1" class="Button_QuadStyleSelector" id="Hud3dEchelons" />
			</frame>
			<frame posn="138 0">
				<label posn="0 0" style="CardButtonSmallS" text="Copilot" scriptevents="1" class="Button_QuadStyleSelector" id="Copilot" />
				<label posn="0 -6" style="CardButtonSmallS" text="MedalsBig" scriptevents="1" class="Button_QuadStyleSelector" id="MedalsBig" />
				<label posn="0 -12" style="CardButtonSmallS" text="Emblems" scriptevents="1" class="Button_QuadStyleSelector" id="Emblems" />
				<label posn="0 -18" style="CardButtonSmallS" text="TitleLogos" scriptevents="1" class="Button_QuadStyleSelector" id="TitleLogos" />
			</frame>
			<frame posn="174 0">
				<label posn="0 4" scale="0.5" valign="center2" style="TextRaceMessageBig" text="Text" />
				<label posn="0 0" style="CardButtonSmallXL" text="With events" scriptevents="1" class="Button_TextStyleSelector" id="WithEvents" />
				<label posn="0 -6" style="CardButtonSmallXL" text="Without events" scriptevents="1" class="Button_TextStyleSelector" id="WithoutEvents" />
				<label posn="0 -12" style="CardButtonSmallXL" text="Button" scriptevents="1" class="Button_TextStyleSelector" id="Button" />
			</frame>
			<frame posn="174 -32">
				<label posn="0 4" scale="0.5" valign="center2" style="TextRaceMessageBig" text="Gauge" />
				<label posn="0 0" style="CardButtonSmallXL" text="All" scriptevents="1" class="Button_GaugeStyleSelector" id="Gauge_All" />
			</frame>
			<frame posn="234 -32">
				<label posn="0 4" scale="0.5" valign="center2" style="TextRaceMessageBig" text="Audio" />
				<label posn="0 0" style="CardButtonSmallXL" text="EUISound" scriptevents="1" class="Button_SoundStyleSelector" id="EUISound" />
			</frame>
		</frame>
		<frame posn="142 0" id="Frame_Pager">
			<label posn="-8 0" sizen="12 10" halign="right" valign="center2" textemboss="1" text="1/1" id="Pager_Count" />
			<quad posn="-8 0 1" sizen="10 10" valign="center" style="Icons64x64_1" substyle="ArrowPrev" scriptevents="1" class="Button_Pager" id="Button_PagerPrev" />
			<quad posn="0 0 1" sizen="10 10" valign="center" style="Icons64x64_1" substyle="ArrowNext" scriptevents="1" class="Button_Pager" id="Button_PagerNext" />
			<quad posn="-8 0 0" sizen="10 10" valign="center" style="Icons64x64_1" substyle="ClipPause" />
			<quad posn="0 0 0" sizen="10 10" valign="center" style="Icons64x64_1" substyle="ClipPause" />
		</frame>
		<frame posn="-150 -11" id="Frame_Properties">
			<format textemboss="1" />
			<label posn="0 0" sizen="30 6" valign="center2" text="Style:" />
			<label posn="12 0" sizen="44 6" valign="center2" textprefix="$i" text="..." id="Label_Style" />
			<label posn="60 0" sizen="30 6" valign="center2" text="Substyle:" />
			<label posn="80 0" sizen="58 6" valign="center2" textprefix="$i" text="..." id="Label_Substyle" />
			<entry posn="140 0" sizen="160 6" valign="center2" id="Entry_Properties" />
		</frame>
		<frame posn="-150 19" id="Frame_Background">
			<frame pons="0 0">
				<quad posn="0 0" sizen="10 10" bgcolor="fff" scriptevents="1" class="Quad_StyleBg" id="full_1.png" />
				<quad posn="12 0" sizen="10 10" image="http://utils.bside-community.com/maniastyles/img/checker_2.png" scriptevents="1" class="Quad_StyleBg" id="checker_1.png" />
				<quad posn="24 0" sizen="10 10" image="http://utils.bside-community.com/maniastyles/img/checker_4.png" scriptevents="1" class="Quad_StyleBg" id="checker_3.png" />
				<quad posn="36 0" sizen="10 10" image="http://utils.bside-community.com/maniastyles/img/maze_2.png" scriptevents="1" class="Quad_StyleBg" id="maze_1.png" />
				<quad posn="48 0" sizen="10 10" image="http://utils.bside-community.com/maniastyles/img/lalala.png" hidden="1" scriptevents="1" class="Quad_StyleBg" id="lalala.bik" />
			</frame>
			<frame posn="0 -12">
				<quad posn="0 0" sizen="10 10" bgcolor="000" scriptevents="1" class="Quad_BgColor" id="000" />
				<quad posn="12 0" sizen="10 10" bgcolor="555" scriptevents="1" class="Quad_BgColor" id="555" />
				<quad posn="24 0" sizen="10 10" bgcolor="aaa" scriptevents="1" class="Quad_BgColor" id="aaa" />
				<quad posn="36 0" sizen="10 10" bgcolor="fff" scriptevents="1" class="Quad_BgColor" id="fff" />
			</frame>
			<frame posn="50 -13">
				<quad posn="5 1.2 0" sizen="102 5" image="http://utils.bside-community.com/maniastyles/img/hue.png" />
				<quad posn="0 0 1" sizen="10 10" style="Icons128x32_1" substyle="SliderCursor" scriptevents="1" id="Slider_Cursor" />
			</frame>
		</frame>
		<frame posn="-150 -20" id="Frame_QuadStylesDisplay">
			{{{Quads_Style}}}
		</frame>
		<frame posn="-150 -20" hidden="1" id="Frame_GaugeStylesDisplay">
			{{{Gauges_Style}}}
		</frame>
		<frame posn="-150 -20" hidden="1" id="Frame_LabelStylesDisplay">
			{{{Labels_Style}}}
		</frame>
		<frame posn="-150 -20" hidden="1" id="Frame_LabelStyles2Display">
			{{{Labels_Style2}}}
		</frame>
		<frame posn="-150 -20" hidden="1" id="Frame_ButtonStylesDisplay">
			{{{Buttons_Style}}}
		</frame>
		<frame posn="-150 -20" hidden="1" id="Frame_SoundStylesDisplay">
			{{{Sounds_Style}}}
		</frame>
	</frame>
</frame>
<script><!--
#Include "TextLib" as TL
#Include "MathLib" as ML

#Const C_Quad_Styles ["321Go"=>["1","2","3","Go!"],"BgRaceScore2"=>["BgCardPlayer","BgCardServer","BgScores","Cartouche","CartoucheLine","CupFinisher","CupPotentialFinisher","Fame","Handle","HandleBlue","HandleRed","HandleSelectable","IsLadderDisabled","IsLocalPlayer","LadderPoints","LadderRank","Laps","Podium","Points","SandTimer","ScoreLink","ScoreReplay","SendScore","Speaking","Spectator","Tv","Warmup"],"Bgs1"=>["ArrowDown","ArrowLeft","ArrowRight","ArrowUp","BgButton","BgButtonBig","BgButtonGlow","BgButtonGrayed","BgButtonOff","BgButtonShadow","BgButtonSmall","BgCard","BgCard1","BgCard2","BgCard3","BgCardBuddy","BgCardChallenge","BgCardFolder","BgCardInventoryItem","BgCardList","BgCardOnline","BgCardPlayer","BgCardProperty","BgCardSystem","BgCardZone","BgColorContour","BgDialogBlur","BgEmpty","BgGlow2","BgGradBottom","BgGradLeft","BgGradRight","BgGradTop","BgGradV","BgHealthBar","BgIconBorder","BgList","BgListLine","BgMetalBar","BgPager","BgProgressBar","BgShadow","BgSlider","BgSystemBar","BgTitle","BgTitle2","BgTitle3","BgTitle3_1","BgTitle3_2","BgTitle3_3","BgTitle3_4","BgTitle3_5","BgTitleGlow","BgTitlePage","BgTitleShadow","BgWindow1","BgWindow2","BgWindow3","BgWindow4","EnergyBar","EnergyTeam2","Glow","HealthBar","NavButton","NavButtonBlink","NavButtonQuit","ProgressBar","ProgressBarSmall","Shadow"],"Bgs1InRace"=>["ArrowDown","ArrowLeft","ArrowRight","ArrowUp","BgButton","BgButtonBig","BgButtonGlow","BgButtonGrayed","BgButtonOff","BgButtonShadow","BgButtonSmall","BgCard","BgCard1","BgCard2","BgCard3","BgCardBuddy","BgCardChallenge","BgCardFolder","BgCardInventoryItem","BgCardList","BgCardOnline","BgCardPlayer","BgCardProperty","BgCardSystem","BgCardZone","BgColorContour","BgDialogBlur","BgEmpty","BgGlow2","BgGradBottom","BgGradLeft","BgGradRight","BgGradTop","BgGradV","BgHealthBar","BgIconBorder","BgList","BgListLine","BgMetalBar","BgPager","BgProgressBar","BgShadow","BgSlider","BgSystemBar","BgTitle","BgTitle2","BgTitle3","BgTitle3_1","BgTitle3_2","BgTitle3_3","BgTitle3_4","BgTitle3_5","BgTitleGlow","BgTitlePage","BgTitleShadow","BgWindow1","BgWindow2","BgWindow3","BgWindow4","EnergyBar","EnergyTeam2","Glow","HealthBar","NavButton","NavButtonBlink","NavButtonQuit","ProgressBar","ProgressBarSmall","Shadow"],"BgsChallengeMedals"=>["BgBronze","BgGold","BgNadeo","BgNotPlayed","BgPlayed","BgSilver"],"BgsPlayerCard"=>["BgActivePlayerCard","BgActivePlayerName","BgActivePlayerScore","BgCard","BgCardSystem","BgMediaTracker","BgPlayerCard","BgPlayerCardBig","BgPlayerCardSmall","BgPlayerName","BgPlayerScore","BgRacePlayerLine","BgRacePlayerName","ProgressBar"],"Copilot"=>["Down","DownGood","DownWrong","Left","LeftGood","LeftWrong","Right","RightGood","RightWrong","Up","UpGood","UpWrong"],"Emblems"=>["#0","#1","#2"],"EnergyBar"=>["BgText","EnergyBar","EnergyBar_0.25","EnergyBar_Thin","HeaderGaugeLeft","HeaderGaugeRight"],"Hud3dEchelons"=>["EchelonBronze1","EchelonBronze2","EchelonBronze3","EchelonGold1","EchelonGold2","EchelonGold3","EchelonSilver1","EchelonSilver2","EchelonSilver3"],"Icons128x128_1"=>["Advanced","Back","BackFocusable","Beginner","Browse","Buddies","Challenge","ChallengeAuthor","Coppers","Create","Credits","Custom","CustomStars","Default","Download","Easy","Editor","Event","Extreme","Forever","GhostEditor","Hard","Hotseat","Inputs","Invite","LadderPoints","Lan","Launch","Load","LoadTrack","Manialink","ManiaZones","MedalCount","MediaTracker","Medium","Multiplayer","Nations","NewTrack","Options","Padlock","Paint","Platform","PlayerPage","Profile","ProfileAdvanced","ProfileVehicle","Puzzle","Quit","Race","Rankings","Replay","Save","ServersAll","ServersFavorites","ServersSuggested","Share","ShareBlink","SkillPoints","Solo","Statistics","Stunts","United","Upload","Vehicles"],"Icons128x128_Blink"=>["Advanced","Back","BackFocusable","Beginner","Browse","Buddies","Challenge","ChallengeAuthor","Coppers","Create","Credits","Custom","CustomStars","Default","Download","Easy","Editor","Event","Extreme","Forever","GhostEditor","Hard","Hotseat","Inputs","Invite","LadderPoints","Lan","Launch","Load","LoadTrack","Manialink","ManiaZones","MedalCount","MediaTracker","Medium","Multiplayer","Nations","NewTrack","Options","Padlock","Paint","Platform","PlayerPage","Profile","ProfileAdvanced","ProfileVehicle","Puzzle","Quit","Race","Rankings","Replay","Save","ServersAll","ServersFavorites","ServersSuggested","Share","ShareBlink","SkillPoints","Solo","Statistics","Stunts","United","Upload","Vehicles"],"Icons128x32_1"=>["ArrowUp","BgQuadWhite","Close","Empty","ManiaLinkHome","ManiaLinkSwitch","ManiaPlanet","Minimize","Music","PainterBrush","PainterFill","PainterLayer","PainterMirror","PainterSticker","PainterTeam","RT_Cup","RT_Laps","RT_Rounds","RT_Script","RT_Stunts","RT_Team","RT_TimeAttack","Settings","SliderBar","SliderBar2","SliderCursor","Sound","UrlBg","Windowed"],"Icons64x64_1"=>["3DStereo","Add","ArrowBlue","ArrowDisabled","ArrowDown","ArrowFastNext","ArrowFastPrev","ArrowFirst","ArrowGreen","ArrowLast","ArrowNext","ArrowPrev","ArrowRed","ArrowUp","Browser","Buddy","ButtonLeagues","Camera","CameraLocal","Check","ClipPause","ClipPlay","ClipRewind","Close","Empty","Finish","FinishGrey","First","GenericButton","Green","IconLeaguesLadder","IconPlayers","IconPlayersLadder","IconServers","Inbox","LvlGreen","LvlRed","LvlYellow","ManiaLinkNext","ManiaLinkPrev","Maximize","MediaAudioDownloading","MediaPlay","MediaStop","MediaVideoDownloading","NewMessage","NotBuddy","OfficialRace","Opponents","Outbox","QuitRace","RedHigh","RedLow","Refresh","RestartRace","Save","Second","ShowDown","ShowDown2","ShowLeft","ShowLeft2","ShowRight","ShowRight2","ShowUp","ShowUp2","ShowUpChanging","SliderCursor","SliderCursor2","StateFavourite","StatePrivate","StateSuggested","Sub","TagTypeBronze","TagTypeGold","TagTypeNadeo","TagTypeNone","TagTypeSilver","Third","ToolLeague1","ToolRoot","ToolTree","ToolUp","TrackInfo","TV","YellowHigh","YellowLow"],"Icons64x64_2"=>["ArrowElimination","ArrowHit","Calendar","Disconnected","DisconnectedLight","LaserElimination","LaserHit","NucleusElimination","NucleusHit","RocketElimination","RocketHit","ServerNotice","SortBy","UnknownElimination","UnknownHit"],"ManiaPlanetLogos"=>["IconPlanets","IconPlanetsPerspective","IconPlanetsSmall","ManiaPlanetLogoBlack","ManiaPlanetLogoBlackSmall","ManiaPlanetLogoWhite","ManiaPlanetLogoWhiteSmall"],"ManiaplanetSystem"=>["BgDialog","BgDialogAnchor","BgFloat","Events","Medals","Statistics"],"MedalsBig"=>["MedalBronze","MedalGold","MedalGoldPerspective","MedalNadeo","MedalNadeoPerspective","MedalSilver","MedalSlot"],"TitleLogos"=>["Author","Collection","Icon","Title"],"UIConstruction_Buttons"=>["ActionMaker","Add","AirMapping","Author","AuthorTime","BgEditors","BgIcons","BgMain","BgTools","BlockEditor","Camera","Challenge","CopyPaste","DecalEditor","Delete","Directory","Down","Drive","Erase","FreeItems","GhostBlocks","Help","Item","Left","MacroBlockEditor","MediaTracker","ObjectEditor","OffZone","Options","Paint","Pick","Plugins","Quit","Redo","Reload","Right","Save","SaveAs","ScriptEditor","SpotModelClearUnused","SpotModelDuplicate","SpotModelNew","SpotModelRename","Square","Stats","Sub","TerrainEditor","TestSm","Text","Tools","Underground","Undo","Up","Validate","Validate_Step1","Validate_Step2","Validate_Step3"],"UiSMSpectatorScoreBig"=>["BotLeft","BotLeftGlass","BotRight","BotRightGlass","CenterShield","CenterShieldSmall","HandleLeft","HandleRight","PlayerGlass","PlayerIconBg","PlayerJunction","PlayerSlot","PlayerSlotCenter","PlayerSlotRev","PlayerSlotSmall","PlayerSlotSmallRev","TableBgHoriz","TableBgVert","Top","UIRange1Bg","UIRange2Bg"],"UIConstruction_Buttons2"=>["AirMapping","BlockEditor","Copy","Cut","GhostBlocks","KeysAdd","KeysCopy","KeysDelete","KeysPaste","New","Open","Symmetry"],"UIConstructionBullet_Buttons"=>["AnimMode","BlocMode","BulletEditorMode","Circle","Core","CoreFolder","DeltaMode","Disc","ExplosionMode","NewBullet","NewRandomBullet","Point","Power","PowerFolder","PreviewMode","RootMotionMode","SmokeDebris","SmokeDebrisFolder","SoundMode","SphereEmpty","SphereFull","Trail","TrailFolder"],"Hud3dIcons"=>["Cross","CrossTargeted","Player1","Player2","Player3","PointA","PointB","PointC"],"ManiaPlanetMainMenu"=>["BottomBar","Highlight","IconAdd","IconHome","IconPlay","IconQuit","IconSettings","IconStore","MainBg","TitleBg","TopBar"],"BgsButtons"=>["BgButtonLarge","BgButtonMedium","BgButtonMediumSelector","BgButtonMediumSpecial","BgButtonSmall","BgButtonSmall2","BgButtonXSmall"]]

#Const C_Gauge_Styles ["EnergyBar","BgCard","ProgressBar","ProgressBarSmall"]

declare CMlQuad Button_PagerPrev;
declare CMlQuad Button_PagerNext;
declare CMlLabel Pager_Count;
declare CMlLabel Label_Style;
declare CMlLabel Label_Substyle;
declare CMlEntry Entry_Properties;
declare CMlFrame Frame_QuadStylesDisplay;
declare CMlFrame Frame_GaugeStylesDisplay;

declare Text G_CurrentStyle;
declare Integer G_PageRange;
declare Integer G_PageStart;
declare Integer G_PageEnd;
declare Text G_MLEvents;

Void UpdateQuadStyleDisplay() {
	declare Key = G_PageStart;
	foreach (Control in Frame_QuadStylesDisplay.Controls) {
		declare Quad_Style <=> (Control as CMlQuad);
		if (C_Quad_Styles[G_CurrentStyle].existskey(Key)) {
			if (!Quad_Style.Visible) Quad_Style.Show();
			Quad_Style.Style = G_CurrentStyle;
			Quad_Style.Substyle = C_Quad_Styles[G_CurrentStyle][Key];
		} else {
			if (Quad_Style.Visible) Quad_Style.Hide();
		}
		
		Key += 1;
	}
}

Void UpdateGaugeStyleDisplay() {
	declare Key = G_PageStart;
	foreach (Control in Frame_GaugeStylesDisplay.Controls) {
		if (Control is CMlGauge) {
			declare Gauge_Style <=> (Control as CMlGauge);
			if (C_Gauge_Styles.existskey(Key)) {
				if (!Gauge_Style.Visible) Gauge_Style.Show();
				Gauge_Style.Style = C_Gauge_Styles[Key];
			} else {
				if (Gauge_Style.Visible) Gauge_Style.Hide();
			}
			Key += 1;
		} else {
			if (C_Gauge_Styles.existskey(Key)) {
				Control.Show();
			} else {
				Control.Hide();
			}
		}
		
	}
}

Void SelectQuadStyle(Text _ControlId) {
	declare ControlId = TL::ToInteger(_ControlId);
	declare Key = G_PageStart + ControlId;
	
	if (!C_Quad_Styles[G_CurrentStyle].existskey(Key)) return;
	
	declare Substyle = C_Quad_Styles[G_CurrentStyle][Key];
	Label_Style.SetText(G_CurrentStyle);
	Label_Substyle.SetText(Substyle);
	Entry_Properties.Value = "<quad style=\""^G_CurrentStyle^"\" substyle=\""^Substyle^"\" />";
}

Void SelectGaugeStyle(Text _ControlId) {
	declare ControlId = TL::ToInteger(_ControlId);
	declare Key = G_PageStart + ControlId;
	
	if (!C_Gauge_Styles.existskey(Key)) return;
	
	declare Style = C_Gauge_Styles[Key];
	Label_Style.SetText(Style);
	Entry_Properties.Value = "<gauge style=\""^Style^"\" drawbg=\"1\" drawblockbg=\"1\" />";
}

Void SelectLabelStyle(Text _ControlId) {
	Label_Style.SetText(_ControlId);
	Entry_Properties.Value = "<label style=\""^_ControlId^"\" "^G_MLEvents^" />";
}

Void SelectSoundStyle(Text _ControlId) {
	declare ControlIdSplit = TL::Split("|", _ControlId);
	declare SoundName = ::EUISound::Default;
	declare SoundVariant = 0;
	
	if (ControlIdSplit.existskey(0)) {
		switch (ControlIdSplit[0]) {
			{{{Sounds_Switch}}}
		}
	}
	
	if (ControlIdSplit.existskey(1)) {
		SoundVariant = TL::ToInteger(ControlIdSplit[1]);
	}
	
	Entry_Properties.Value = "PlayUiSound(::"^SoundName^", "^SoundVariant^", 1.0);";
	PlayUiSound(SoundName, SoundVariant, 1.0);
}

Void UpdatePager(Integer _Shift, Integer _Total) {
	declare NewPageStart = G_PageStart;
	NewPageStart += _Shift;
	if (NewPageStart >= _Total) return;
	if (NewPageStart <= 0) {
		NewPageStart = 0;
		if (Button_PagerPrev.Visible) Button_PagerPrev.Hide();
	} else {
		if (!Button_PagerPrev.Visible) Button_PagerPrev.Show();
	}
	
	declare NewPageEnd = NewPageStart + G_PageRange - 1;
	if (NewPageEnd >= _Total - 1) {
		NewPageEnd = _Total - 1;
		if (Button_PagerNext.Visible) Button_PagerNext.Hide();
	} else {
		if (!Button_PagerNext.Visible) Button_PagerNext.Show();
	}
	
	G_PageStart = NewPageStart;
	G_PageEnd = NewPageEnd;
	
	declare PageCurrent = (G_PageStart / G_PageRange) + 1;
	declare PageMax = (_Total / G_PageRange);
	if (_Total % G_PageRange != 0) PageMax += 1;
	
	Pager_Count.SetText(PageCurrent^"/"^PageMax);
}

Vec3 HsvToRgb(Vec3 _HSV) {
	declare RGB = <0., 0., 0.>;
	declare H = _HSV.X;
	declare S = _HSV.Y;
	declare V = _HSV.Z;
	
	declare Hi = ML::FloorInteger(H * 6.);
	declare F = (H * 6.) - Hi;
	declare P = V * (1. - S);
	declare Q = V * (1. - (F * S));
	declare T = V * (1. - ((1. - F) * S));
	
	switch(Hi) {
		case 0: RGB = <V, T, P>;
		case 1: RGB = <Q, V, P>;
		case 2: RGB = <P, V, T>;
		case 3: RGB = <P, Q, V>;
		case 4: RGB = <T, P, V>;
		default: RGB = <V, P, Q>;
	}
	
	return RGB;
}

main() {
	Button_PagerPrev		<=> (Page.GetFirstChild("Button_PagerPrev")			as CMlQuad);
	Button_PagerNext		<=> (Page.GetFirstChild("Button_PagerNext")			as CMlQuad);
	Pager_Count				<=> (Page.GetFirstChild("Pager_Count")				as CMlLabel);
	Label_Style				<=> (Page.GetFirstChild("Label_Style")				as CMlLabel);
	Label_Substyle			<=> (Page.GetFirstChild("Label_Substyle")			as CMlLabel);
	Entry_Properties		<=> (Page.GetFirstChild("Entry_Properties")			as CMlEntry);
	Frame_QuadStylesDisplay	<=> (Page.GetFirstChild("Frame_QuadStylesDisplay")	as CMlFrame);
	Frame_GaugeStylesDisplay<=> (Page.GetFirstChild("Frame_GaugeStylesDisplay")	as CMlFrame);
	
	declare Frame_Global				<=> (Page.GetFirstChild("Frame_Global")					as CMlFrame);
	declare Button_Visibility			<=> (Page.GetFirstChild("Button_Visibility")			as CMlLabel);
	declare Frame_LabelStylesDisplay	<=> (Page.GetFirstChild("Frame_LabelStylesDisplay")		as CMlFrame);
	declare Frame_LabelStyles2Display	<=> (Page.GetFirstChild("Frame_LabelStyles2Display")	as CMlFrame);
	declare Frame_ButtonStylesDisplay	<=> (Page.GetFirstChild("Frame_ButtonStylesDisplay")	as CMlFrame);
	declare Frame_SoundStylesDisplay	<=> (Page.GetFirstChild("Frame_SoundStylesDisplay")		as CMlFrame);
	declare Quad_StyleBg				<=> (Page.GetFirstChild("Quad_StyleBg")					as CMlQuad);
	declare Quad_StyleBg2				<=> (Page.GetFirstChild("Quad_StyleBg2")				as CMlQuad);
	declare Slider_Cursor				<=> (Page.GetFirstChild("Slider_Cursor")				as CMlQuad);
	declare Quad_BgLalala				<=> (Page.GetFirstChild("lalala.bik")					as CMlQuad);
	
	Entry_Properties.Value = "<quad style=\"...\" substyle=\"...\" />";
	
	G_CurrentStyle = "Bgs1";
	G_PageRange = {{{ItemNbX * ItemNbY}}};
	G_PageStart = 0;
	G_PageEnd = G_PageStart + G_PageRange - 1;
	G_MLEvents = "";
	
	UpdatePager(0, C_Quad_Styles[G_CurrentStyle].count);
	UpdateQuadStyleDisplay();
	
	declare Sliding = False;
	declare SliderShiftX = 0.;
	declare SliderShiftY = 0.;
	declare PrevSliderPosnX = 0.;
	declare A = [128, 128, 32, 32, 66, 110, 66, 110, 19, 11];
	declare B = 0;
	
	while (True) {
		yield;
		
		if (Sliding) {
			Slider_Cursor.PosnX = MouseX - SliderShiftX;
			//Slider_Cursor.PosnY = MouseY - SliderShiftY;
			if (Slider_Cursor.PosnX > 102.) Slider_Cursor.PosnX = 102.;
			else if (Slider_Cursor.PosnX < 0.) Slider_Cursor.PosnX = 0.;
			
			if (PrevSliderPosnX != Slider_Cursor.PosnX) {
				PrevSliderPosnX = Slider_Cursor.PosnX;
				declare H = PrevSliderPosnX / 102.;
				if (H == 1.) H = 0.;
				declare RGB = HsvToRgb(<H, 1., 0.5>);
				Quad_StyleBg.Colorize = RGB;
			}
			
			if (!MouseLeftButton) Sliding = False;
		}
		
		foreach (Event in PendingEvents) {
			if (Event.Type == CMlEvent::Type::MouseClick) {
				// ---------------------------------- //
				// Show/Hide the manialink
				if (Event.ControlId == "Button_Visibility") {
					Frame_Global.Visible = !Frame_Global.Visible;
					if (Frame_Global.Visible) {
						Button_Visibility.SetText("Close ML Styles");
					} else {
						Button_Visibility.SetText("Open ML Styles");
					}
				}
				// ---------------------------------- //
				// Click on a quad substyle
				else if (Event.Control.HasClass("Quad_Style")) {
					SelectQuadStyle(Event.ControlId);
				} 
				// ---------------------------------- //
				// Click on a gauge style
				else if (Event.Control.HasClass("Gauge_Style")) {
					SelectGaugeStyle(Event.ControlId);
				} 
				// ---------------------------------- //
				// Click on a label substyle
				else if (Event.Control.HasClass("Label_Style")) {
					SelectLabelStyle(Event.ControlId);
				}
				// ---------------------------------- //
				// Click on a sound substyle
				else if (Event.Control.HasClass("Sound_Style")) {
					SelectSoundStyle(Event.ControlId);
				}
				// ---------------------------------- //
				// Next/Prev page
				else if (Event.Control.HasClass("Button_Pager")) {
					switch (Event.ControlId) {
						case "Button_PagerPrev": {
							UpdatePager(-G_PageRange, C_Quad_Styles[G_CurrentStyle].count);
							UpdateQuadStyleDisplay();
						}
						case "Button_PagerNext": {
							UpdatePager(G_PageRange, C_Quad_Styles[G_CurrentStyle].count);
							UpdateQuadStyleDisplay();
						}
					}
				}
				// ---------------------------------- //
				// Select a quad style
				else if (Event.Control.HasClass("Button_QuadStyleSelector")) {
					G_CurrentStyle = Event.ControlId;
					Label_Style.SetText(G_CurrentStyle);
					Label_Substyle.SetText("...");
					Entry_Properties.Value = "<quad style=\""^G_CurrentStyle^"\" substyle=\"...\" />";
					
					G_PageStart = 0;
					UpdatePager(0, C_Quad_Styles[G_CurrentStyle].count);
					UpdateQuadStyleDisplay();
					
					if (!Frame_QuadStylesDisplay.Visible) {
						Frame_QuadStylesDisplay.Show();
						Frame_GaugeStylesDisplay.Hide();
						Frame_LabelStylesDisplay.Hide();
						Frame_LabelStyles2Display.Hide();
						Frame_ButtonStylesDisplay.Hide();
						Frame_SoundStylesDisplay.Hide();
					}
				} 
				// ---------------------------------- //
				// Select a gauge style
				else if (Event.Control.HasClass("Button_GaugeStyleSelector")) {
					G_CurrentStyle = Event.ControlId;
					Label_Style.SetText("...");
					Label_Substyle.SetText("-");
					Entry_Properties.Value = "<gauge style=\"...\" drawbg=\"1\" drawblockbg=\"1\" />";
					
					G_PageStart = 0;
					UpdatePager(0, C_Gauge_Styles.count);
					UpdateGaugeStyleDisplay();
					
					if (!Frame_GaugeStylesDisplay.Visible) {
						Frame_QuadStylesDisplay.Hide();
						Frame_GaugeStylesDisplay.Show();
						Frame_LabelStylesDisplay.Hide();
						Frame_LabelStyles2Display.Hide();
						Frame_ButtonStylesDisplay.Hide();
						Frame_SoundStylesDisplay.Hide();
					}
				} 
				// ---------------------------------- //
				// Select a text style
				else if (Event.Control.HasClass("Button_TextStyleSelector")) {
					if (Frame_QuadStylesDisplay.Visible) Frame_QuadStylesDisplay.Hide();
					if (Frame_GaugeStylesDisplay.Visible) Frame_GaugeStylesDisplay.Hide();
					if (Frame_SoundStylesDisplay.Visible) Frame_SoundStylesDisplay.Hide();
					switch (Event.ControlId) {
						case "WithEvents": {
							Frame_LabelStylesDisplay.Show();
							Frame_LabelStyles2Display.Hide();
							Frame_ButtonStylesDisplay.Hide();
							G_MLEvents = "scriptevents=\"1\"";
						}
						case "WithoutEvents": {
							Frame_LabelStylesDisplay.Hide();
							Frame_LabelStyles2Display.Show();
							Frame_ButtonStylesDisplay.Hide();
							G_MLEvents = "";
						}
						case "Button": {
							Frame_LabelStylesDisplay.Hide();
							Frame_LabelStyles2Display.Hide();
							Frame_ButtonStylesDisplay.Show();
							G_MLEvents = "";
						}
					}
					
					Label_Style.SetText("...");
					Label_Substyle.SetText("-");
					Entry_Properties.Value = "<label style=\"...\" "^G_MLEvents^" />";
					
					G_PageStart = 0;
					UpdatePager(0, 1);
					
				}
				// ---------------------------------- //
				// Select a sound style
				else if (Event.Control.HasClass("Button_SoundStyleSelector")) {
					Label_Style.SetText("-");
					Label_Substyle.SetText("-");
					Entry_Properties.Value = "PlayUiSound(::EUISound::Default, 0, 1.0);";
					
					G_PageStart = 0;
					UpdatePager(0, 1);
					
					if (!Frame_SoundStylesDisplay.Visible) {
						Frame_QuadStylesDisplay.Hide();
						Frame_GaugeStylesDisplay.Hide();
						Frame_LabelStylesDisplay.Hide();
						Frame_LabelStyles2Display.Hide();
						Frame_ButtonStylesDisplay.Hide();
						Frame_SoundStylesDisplay.Show();
					}					
				}
				// ---------------------------------- //
				// Select a predefined color
				else if (Event.Control.HasClass("Quad_BgColor")) {
					Quad_StyleBg.Colorize = TL::ToColor(Event.ControlId);
				} 
				// ---------------------------------- //
				// Select a background type
				else if (Event.Control.HasClass("Quad_StyleBg")) {
					if (Event.ControlId == "lalala.bik") {
						if (Quad_StyleBg.Visible) Quad_StyleBg.Hide();
						if (!Quad_StyleBg2.Visible) Quad_StyleBg2.Show();
					} else {
						if (!Quad_StyleBg.Visible) Quad_StyleBg.Show();
						if (Quad_StyleBg2.Visible) Quad_StyleBg2.Hide();
						Quad_StyleBg.ChangeImageUrl("http://utils.bside-community.com/maniastyles/img/"^Event.ControlId);
					}
				}
				// ---------------------------------- //
				// Move the cursor
				else if (Event.ControlId == "Slider_Cursor") {
					Sliding = True;
					SliderShiftX = MouseX - Slider_Cursor.PosnX;
					//SliderShiftY = MouseY - Slider_Cursor.PosnY;
				}
			} else if (Event.Type == CMlEvent::Type::KeyPress) {
				if (!Quad_BgLalala.Visible && A.existskey(B)) {
					if (A[B] == Event.KeyCode) {
						B += 1;
						if (B >= A.count) {
							Quad_BgLalala.Show();
						}
					} else {
						B = 0;
					}
				}
			}
		}
	}
}
--></script>
</manialink>
""";
}

Void Private_AddBot(Text _Params) {
	declare Params = TL::Split("|", _Params);
	declare BotClan = 0;
	declare BotNb = 1;
	
	if (Params.existskey(0)) BotClan = TL::ToInteger(Params[0]);
	if (BotClan < 0 || BotClan > 2) BotClan = 0;
	
	if (Params.existskey(1)) BotNb = TL::ToInteger(Params[1]);
	if (BotNb < 1) BotNb = 1;
	
	declare BotCount for This = Integer[Integer];
	for (I, 1, BotNb) {
		Users_CreateFake("", BotClan);
		if (BotCount.existskey(BotClan)) BotCount[BotClan] += 1;
		else BotCount[BotClan] = 1;
	}
	
}

Void Private_RemoveBot(Text _Number) {
	declare BotLogin = "*fakeplayer"^_Number^"*";
	declare BotCount for This = Integer[Integer];
	
	foreach (Player in AllPlayers) {
		if (Player.Login == BotLogin) {
			Users_DestroyFake(Player.User);
			
			if (BotCount.existskey(Player.CurrentClan) && BotCount[Player.CurrentClan] > 0) BotCount[Player.CurrentClan] -= 1;
			else BotCount[Player.CurrentClan] = 0;
			
			return;
		}
	}
}

Void Private_RemoveBots(Text _Number) {
	declare Number = TL::ToInteger(_Number);
	declare DestroyedNb = 0;
	declare BotCount for This = Integer[Integer];
		
	foreach (Player in AllPlayers) {
		if (DestroyedNb >= Number) break;
		if (!Player.IsFakePlayer) continue;
		Users_DestroyFake(Player.User);
		
		if (BotCount.existskey(Player.CurrentClan) && BotCount[Player.CurrentClan] > 0) BotCount[Player.CurrentClan] -= 1;
		else BotCount[Player.CurrentClan] = 0;
			
		DestroyedNb += 1;
	}
}

Void Private_RemoveAllBots() {
	Users_DestroyAllFakes();
	declare BotCount for This = Integer[Integer];
	BotCount.clear();
}

Void Private_KeepBot(Text _KeepBot) {
	declare KeepBot for This = False;
	if (_KeepBot == "True") {
		KeepBot = True;
	} else {
		KeepBot = False;
	}
}

Void Private_UsePvPCollisions(Text _UsePvPCollisions) {
	if (_UsePvPCollisions == "True") {
		UsePvPCollisions = True;
	} else {
		UsePvPCollisions = False;
	}
}

Void Private_UsePvPWeapons(Text _UsePvPWeapons) {
	if (_UsePvPWeapons == "True") {
		UsePvPWeapons = True;
	} else {
		UsePvPWeapons = False;
	}
}

Void Private_SetTimer(Text _Time) {
	declare Time = TL::ToInteger(_Time);
	if (Time < 0) {
		EndTime = -1;
	} else {
		EndTime = Now + Time;
	}
}

Void Private_UseAllies(Text _UseAllies) {
	if (_UseAllies == "True") {
		UseAllies = True;
	} else {
		UseAllies = False;
	}
}

// ---------------------------------- //
// Public
// ---------------------------------- //

// ---------------------------------- //
/** Return the version number of the script
 *
 *	@return		The version number of the script
 */
Text GetScriptVersion() {
	return Version;
}

// ---------------------------------- //
/** Return the name of the script
 *
 *	@return		The name of the script
 */
Text GetScriptName() {
	return ScriptName;
}

Void Log(Text _Log) {
	log(Now^"> "^_Log);
}

// ---------------------------------- //
/// Unload the library
Void Unload() {
	// Destroy the layers
	if (UIManager.UILayers.existskey(G_LibDebug_LayerMenuId)) {
		declare LayerMenu = UIManager.UILayers[G_LibDebug_LayerMenuId];
		declare Removed = UIManager.UIAll.UILayers.remove(LayerMenu);
		UIManager.UILayerDestroy(LayerMenu);
	}
	if (UIManager.UILayers.existskey(G_LibDebug_LayerStylesId)) {
		declare LayerStyles = UIManager.UILayers[G_LibDebug_LayerStylesId];
		declare Removed = UIManager.UIAll.UILayers.remove(LayerStyles);
		UIManager.UILayerDestroy(LayerStyles);
	}
	
	G_LibDebug_LayerMenuId = NullId;
	G_LibDebug_LayerStylesId = NullId;
	G_LibDebug_Admins.clear();
}

// ---------------------------------- //
/// Load the library
Void Load() {
	Unload();
	
	// Create the layers
	declare LayerMenu = UIManager.UILayerCreate();
	G_LibDebug_LayerMenuId = LayerMenu.Id;
	LayerMenu.ManialinkPage = Private_CreateManialinkMenu();
	UIManager.UIAll.UILayers.add(LayerMenu);
	
	declare LayerStyles = UIManager.UILayerCreate();
	G_LibDebug_LayerStylesId = LayerStyles.Id;
	LayerStyles.ManialinkPage = Private_CreateManialinkStyles();
	UIManager.UIAll.UILayers.add(LayerStyles);
}

Void StartMap() {
	// Restore the bot after a script restart
	declare KeepBot for This = False;
	declare BotCount for This = Integer[Integer];
	if (KeepBot) {
		declare Clan1Nb = 0;
		declare Clan2Nb = 0;
		foreach (Clan => BotNb in BotCount) {
			if (Clan == 2) Clan2Nb += BotNb;
			else Clan1Nb += BotNb;
		}
		Users_SetNbFakeUsers(Clan1Nb, Clan2Nb);
	} else {
		BotCount.clear();
	}
}

Void AddAdmin(CSmPlayer _Player) {
	if (_Player == Null) return;
	if (!G_LibDebug_Admins.exists(_Player.Id)) G_LibDebug_Admins.add(_Player.Id);
}

Void RemoveAdmin(CSmPlayer _Player) {
	if (_Player == Null) return;
	declare Removed = G_LibDebug_Admins.remove(_Player.Id);
}

Void Loop() {
	foreach (Player in AllPlayers) {
		if (G_LibDebug_Admins.count > 0 && !G_LibDebug_Admins.exists(Player.Id)) continue;
		
		declare UI <=> UIManager.GetUI(Player);
		if (UI != Null) {
			declare netread Integer Net_LibDebug_ActionUpdate for UI;
			declare Integer LibDebug_PrevActionUpdate for Player;
			
			if (LibDebug_PrevActionUpdate != Net_LibDebug_ActionUpdate) {
				LibDebug_PrevActionUpdate = Net_LibDebug_ActionUpdate;
				
				declare netread Text Net_LibDebug_ActionName for UI;
				declare netread Text Net_LibDebug_ActionParams for UI;
				
				switch (Net_LibDebug_ActionName) {
					case "AddBot"			: Private_AddBot(Net_LibDebug_ActionParams);
					case "RemoveBot"		: Private_RemoveBot(Net_LibDebug_ActionParams);
					case "RemoveBots"		: Private_RemoveBots(Net_LibDebug_ActionParams);
					case "RemoveAllBots"	: Private_RemoveAllBots();
					case "KeepBot"			: Private_KeepBot(Net_LibDebug_ActionParams);
					case "UsePvPCollisions"	: Private_UsePvPCollisions(Net_LibDebug_ActionParams);
					case "UsePvPWeapons"	: Private_UsePvPWeapons(Net_LibDebug_ActionParams);
					case "SetTimer"			: Private_SetTimer(Net_LibDebug_ActionParams);
					case "UseAllies"		: Private_UseAllies(Net_LibDebug_ActionParams);
				}
			}
		}
	}
}