class UIOptions_QuickSoldierInfoAdjust extends UIScreen;

var int							Padding;
var int NUM_LISTITEMS;

var UINavigationHelp			NavHelp;
var UIList List;
var array<UIMechaListItem> m_arrMechaItems; 
var UIButton	SaveAndExitButton;
var UIButton	ExitWithoutChangesButton;

var int m_iCurrentTab;

var localized string m_strTitle;
var localized string m_strTabPosition;
var localized string m_strTabGeneral;
var localized string m_strExitWithoutChangesBtn;

var localized string m_strAnchorTopRight;
var localized string m_strAnchorBottomRight;
var localized string m_strAnchorTopLeft;
var localized string m_strAnchorBottomLeft;

var localized string m_strSpecialIcons;
var localized string m_strSpecialIcons_Desc;
var localized string m_strListSize;
var localized string m_strOffsetX;
var localized string m_strOffsetY;
var localized string m_strIconSize;
var localized string m_strPadding;
var localized string m_strListHorizontal;
var localized string m_strAnchor;
var localized string m_strFontSize;
var localized string m_strListSize_Desc;
var localized string m_strOffsetX_Desc;
var localized string m_strOffsetY_Desc;
var localized string m_strIconSize_Desc;
var localized string m_strPadding_Desc;
var localized string m_strListHorizontal_Desc;
var localized string m_strAnchor_Desc;
var localized string m_strFontSize_Desc;

var UISoldierQuickIconPanel SoldierPanelPreview, SettingsCache;
var UIBGBox PanelMaxSize;

// Temp settings here

simulated function AS_SetTitle( string title )
{
	Movie.ActionScriptVoid(MCPath$".SetTitle");
	//MC.FunctionString("SetTitle", title);
}

simulated function AS_SetTabData( string title0, string title1 )
{
	Movie.ActionScriptVoid(MCPath$".SetTabData");
	//MC.FunctionString("SetTabData", title0);
}

simulated function RefreshData()
{
	AS_SetTitle(m_strTitle);	
	
	AS_SetTabData(m_strTabPosition, m_strTabGeneral);

	Show();
}

simulated function MakePreviewBox()
{
	local SoldierQuickIconObject SoldierIcon;
	local XComGameState_Unit Unit;

	if (SoldierPanelPreview == none)
		SoldierPanelPreview = Spawn(class'UISoldierQuickIconPanel', self);
	if (!SettingsCache.ANCHOR_TOP)
	{
		SoldierPanelPreview.OFFSET_Y = -SettingsCache.OFFSET_Y;
		if (SoldierPanelPreview.ORIENT_HORIZONTAL)
			SoldierPanelPreview.OFFSET_Y -= SoldierPanelPreview.ICON_SIZE * 1.15 + 38;
	}

	if (SettingsCache.ANCHOR_RIGHT)
	{
		SoldierPanelPreview.OFFSET_X = -SettingsCache.OFFSET_X;
		if (!SoldierPanelPreview.ORIENT_HORIZONTAL)
			SoldierPanelPreview.OFFSET_X -= SoldierPanelPreview.ICON_SIZE * 1.16;
	}

	PanelMaxSize = Spawn(class'UIBGBox', self).InitBG();
	PanelMaxSize.SetAlpha(50);

	SoldierPanelPreview.InitList(, SoldierPanelPreview.OFFSET_X, SoldierPanelPreview.OFFSET_Y, SoldierPanelPreview.ICON_SIZE, SoldierPanelPreview.ICON_SIZE + 38, SoldierPanelPreview.ORIENT_HORIZONTAL);
	SoldierPanelPreview.SetAlpha(50);

	if (SettingsCache.ANCHOR_TOP && SettingsCache.ANCHOR_RIGHT)
	{
		SoldierPanelPreview.AnchorTopRight();
		PanelMaxSize.AnchorTopRight();
	}
	else if (!SettingsCache.ANCHOR_TOP && SettingsCache.ANCHOR_RIGHT)
	{
		SoldierPanelPreview.AnchorBottomRight();
		PanelMaxSize.AnchorBottomRight();
	}
	else if (SettingsCache.ANCHOR_TOP && !SettingsCache.ANCHOR_RIGHT)
	{
		SoldierPanelPreview.AnchorTopLeft();
		PanelMaxSize.AnchorTopLeft();
	}
	else if (!SettingsCache.ANCHOR_TOP && !SettingsCache.ANCHOR_RIGHT)
	{
		SoldierPanelPreview.AnchorBottomLeft();
		PanelMaxSize.AnchorBottomLeft();
	}

	SoldierPanelPreview.ItemPadding = SoldierPanelPreview.ICON_PADDING;

	Unit = XComGameState_Unit(`XCOMHISTORY.GetSingleGameStateObjectForClass(class'XComGameState_Unit', false));
	SoldierIcon = SoldierQuickIconObject(SoldierPanelPreview.CreateItem(class'SoldierQuickIconObject')).InitQuickIcon(Unit);
	SoldierIcon.SetFocus(true);

	SoldierPanelPreview.SoldierIcons.AddItem(SoldierIcon);

	SoldierIcon = SoldierQuickIconObject(SoldierPanelPreview.CreateItem(class'SoldierQuickIconObject')).InitQuickIcon(Unit);

	SoldierPanelPreview.SoldierIcons.AddItem(SoldierIcon);

	RealizePreviewSizePos();
}

simulated function InitScreen(XComPlayerController InitController, UIMovie InitMovie, optional name InitName)
{
	super.InitScreen(InitController, InitMovie, InitName);
	
	SettingsCache = Spawn(class'UISoldierQuickIconPanel', self);
	SettingsCache.Hide();
	MakePreviewBox();

	List = Spawn(class'UIList', self);
	List.InitList('listMC');
	List.AnimateIn(0);
}

simulated function ExitScreenHelp()
{
	NoSaveExit(none);
}

simulated function OnInit()
{
	local UIMechaListItem ListItem;
	local int i;
	super.OnInit();
		
	NavHelp = PC.Pres.GetNavHelp();
	if (NavHelp == none)
		NavHelp = Spawn(class'UINavigationHelp',self).InitNavHelp();
	NavHelp.ClearButtonHelp();
	NavHelp.AddBackButton(ExitScreenHelp);

	RefreshHelp();

	for(i= NUM_LISTITEMS - 1; i >= 0; i--) 
	{
		ListItem = Spawn( class'UIMechaListItem', List.itemContainer );
		ListItem.bAnimateOnInit = false;
		ListItem.InitListItem();
		ListItem.SetY(i * class'UIMechaListItem'.default.Height);
		m_arrMechaItems.InsertItem(0, ListItem);
	}
	
	AnimateIn();
	Show();

	ResetMechaListItems();

	// Tab 1
	RefreshData();
	SetSelectedTab(1);
}

simulated function RefreshHelp()
{
	if( SaveAndExitButton == none )
	{
		SaveAndExitButton = Spawn(class'UIButton', self);
		SaveAndExitButton.InitButton(, class'UIOptionsPCScreen'.default.m_strSaveAndExit, SaveAndExit);
		//SaveAndExitButton.AnchorBottomRight();
		SaveAndExitButton.SetPosition(690, 850); //Relative to this screen panel
		SaveAndExitButton.AnimateIn(0);
	}
	if( ExitWithoutChangesButton == none )
	{
		ExitWithoutChangesButton = Spawn(class'UIButton', self);
		ExitWithoutChangesButton.InitButton(, m_strExitWithoutChangesBtn, NoSaveExit);
		//ExitWithoutChangesButton.AnchorBottomLeft();
		ExitWithoutChangesButton.SetPosition(100, 850); //Relative to this screen panel
		ExitWithoutChangesButton.AnimateIn(0);
	}
}

simulated function OnReceiveFocus() 
{
	Show(); 
	
	NavHelp.ClearButtonHelp();
	NavHelp.AddBackButton(ExitScreen);
}	
simulated function OnLoseFocus()    
{
	Hide(); 
	NavHelp.ClearButtonHelp();
}

simulated function bool OnUnrealCommand(int cmd, int ActionMask)
{
	if (!bIsInited)
		return true; 

	// Ignore releases, only pay attention to presses.
	if (!CheckInputIsReleaseOrDirectionRepeat(cmd, ActionMask))
		return true; // Consume All Input!

	switch(cmd)
	{
		case class'UIUtilities_Input'.const.FXS_BUTTON_B:
		case class'UIUtilities_Input'.const.FXS_KEY_ESCAPE:
		case class'UIUtilities_Input'.const.FXS_R_MOUSE_DOWN:
			NoSaveExit(none);
			break;
			
		default:
			// Do not reset handled, consume input since this
			// is (an) options menu which stops any other systems.
			break;
	}

	// Assume input is handled unless told otherwise
	return super.OnUnrealCommand(cmd, ActionMask);
}

simulated function OnMouseEvent(int cmd, array<string> args)
{
	local string button;
	if (bShouldPlayGenericUIAudioEvents)
	{
		switch( cmd )
		{
			case class'UIUtilities_Input'.const.FXS_L_MOUSE_UP:
			case class'UIUtilities_Input'.const.FXS_L_MOUSE_DOUBLE_UP:
				`SOUNDMGR.PlaySoundEvent("Generic_Mouse_Click");
				break;
			case class'UIUtilities_Input'.const.FXS_L_MOUSE_IN:
			case class'UIUtilities_Input'.const.FXS_L_MOUSE_OVER:
			case class'UIUtilities_Input'.const.FXS_L_MOUSE_DRAG_OVER:
				`SOUNDMGR.PlaySoundEvent("Play_Mouseover");
				break;
		}
	}

	if(cmd == class'UIUtilities_Input'.const.FXS_L_MOUSE_UP)
	{
		button = args[args.Length - 1];
		if(InStr(button, "Tab") != -1)
		{
			SetSelectedTab(int(Right(button, 1)));
		}
	}
}

function ResetMechaListItems()
{
	local int i;
	for( i = 0; i < NUM_LISTITEMS; i++ )
	{
		m_arrMechaItems[i].OnLoseFocus();
		m_arrMechaItems[i].Hide();
		m_arrMechaItems[i].BG.RemoveTooltip();
		m_arrMechaItems[i].DisableNavigation();
	}
}

function RenableMechaListItems(int maxItems)
{
	local int i;
	for( i = 0; i < maxItems; i++)
	{
		m_arrMechaItems[i].Show();
		m_arrMechaItems[i].EnableNavigation();
	}
}

simulated function SetSelectedTab( int iSelect )
{
	if(m_iCurrentTab == iSelect)
	{
		Movie.Pres.PlayUISound(eSUISound_MenuClickNegative);
		return;
	}

	m_iCurrentTab = iSelect;

	switch(m_iCurrentTab)
	{
		case 0:
			SetPositionalTabSelected();
			break;
		case 1:
		default:
			SetGeneralTabSelected();
			break;
	}

	List.Navigator.SetSelected(m_arrMechaItems[0]);

	MC.FunctionNum("SetSelectedTab", m_iCurrentTab);

	Movie.Pres.PlayUISound(eSUISound_MenuSelect);
}

simulated function SetItemAnchor(int i)
{
	switch(i)
	{
		case 0:
			SettingsCache.ANCHOR_TOP = true;
			SettingsCache.ANCHOR_RIGHT = true;
			break;
		case 1:
			SettingsCache.ANCHOR_TOP = false;
			SettingsCache.ANCHOR_RIGHT = true;
			break;
		case 2:
			SettingsCache.ANCHOR_TOP = true;
			SettingsCache.ANCHOR_RIGHT = false;
			break;
		case 3:
			SettingsCache.ANCHOR_TOP = false;
			SettingsCache.ANCHOR_RIGHT = false;
			break;
	}
}

simulated function int Anchor2Int()
{
	if (SettingsCache.ANCHOR_TOP && SettingsCache.ANCHOR_RIGHT)
		return 0;
	else if (!SettingsCache.ANCHOR_TOP && SettingsCache.ANCHOR_RIGHT)
		return 1;
	else if (SettingsCache.ANCHOR_TOP && !SettingsCache.ANCHOR_RIGHT)
		return 2;
	else if (!SettingsCache.ANCHOR_TOP && !SettingsCache.ANCHOR_RIGHT)
		return 3;
}

simulated function string Anchor2Text()
{
	if (SettingsCache.ANCHOR_TOP && SettingsCache.ANCHOR_RIGHT)
		return m_strAnchorTopRight;
	else if (!SettingsCache.ANCHOR_TOP && SettingsCache.ANCHOR_RIGHT)
		return m_strAnchorBottomRight;
	else if (SettingsCache.ANCHOR_TOP && !SettingsCache.ANCHOR_RIGHT)
		return m_strAnchorTopLeft;
	else if (!SettingsCache.ANCHOR_TOP && !SettingsCache.ANCHOR_RIGHT)
		return m_strAnchorBottomLeft;
}

simulated function SetPositionalTabSelected()
{
	ResetMechaListItems();

	// List Size
	m_arrMechaItems[0].UpdateDataSlider(m_strListSize, string(SettingsCache.MAX_SIZE), , , UpdateEListSize);
	m_arrMechaItems[0].BG.SetTooltipText(m_strListSize_Desc, , , 10, , , , 0.0f);
	m_arrMechaItems[0].Slider.SetStepSize(1);
	m_arrMechaItems[0].Slider.SetPercent(SettingsCache.MAX_SIZE / 20);

	// Offset X
	m_arrMechaItems[1].UpdateDataSlider(m_strOffsetX, string(SettingsCache.OFFSET_X), , , UpdateOffsetX);
	m_arrMechaItems[1].BG.SetTooltipText(m_strOffsetX_Desc, , , 10, , , , 0.0f);
	m_arrMechaItems[1].Slider.SetStepSize(1);
	m_arrMechaItems[1].Slider.SetPercent(SettingsCache.OFFSET_X / 10);

	// Offset Y
	m_arrMechaItems[2].UpdateDataSlider(m_strOffsetY, string(SettingsCache.OFFSET_Y), , , UpdateOffsetY);
	m_arrMechaItems[2].BG.SetTooltipText(m_strOffsetY_Desc, , , 10, , , , 0.0f);
	m_arrMechaItems[2].Slider.SetStepSize(1);
	m_arrMechaItems[2].Slider.SetPercent(SettingsCache.OFFSET_Y / 10);

	// Icon Size
	m_arrMechaItems[3].UpdateDataSlider(m_strIconSize, string(SettingsCache.ICON_SIZE), , , UpdateIconSize);
	m_arrMechaItems[3].BG.SetTooltipText(m_strIconSize_Desc, , , 10, , , , 0.0f);
	m_arrMechaItems[3].Slider.SetStepSize(1);
	m_arrMechaItems[3].Slider.SetPercent((SettingsCache.ICON_SIZE - 20) / 3);

	// Icon Padding
	m_arrMechaItems[4].UpdateDataSlider(m_strPadding, string(SettingsCache.ICON_PADDING), , , UpdatePadding);
	m_arrMechaItems[4].BG.SetTooltipText(m_strPadding_Desc, , , 10, , , , 0.0f);
	m_arrMechaItems[4].Slider.SetStepSize(1);
	m_arrMechaItems[4].Slider.SetPercent(SettingsCache.ICON_PADDING);

	// List Orientation
	m_arrMechaItems[5].UpdateDataCheckbox(m_strListHorizontal,"" , SettingsCache.ORIENT_HORIZONTAL, UpdateOrientation);
	m_arrMechaItems[5].BG.SetTooltipText(m_strListHorizontal_Desc, , , 10, , , , 0.0f);

	// Anchor
	m_arrMechaItems[6].UpdateDataSpinner(m_strAnchor, Anchor2Text(), UpdateAnchors);
	m_arrMechaItems[6].BG.SetTooltipText(m_strAnchor_Desc, , , 10, , , , 0.0f);

	// Font Size
	m_arrMechaItems[7].UpdateDataSlider(m_strFontSize, string(SettingsCache.FONT_SIZE), , , UpdateFontSize);
	m_arrMechaItems[7].Slider.SetStepSize(5);
	m_arrMechaItems[7].Slider.SetPercent((SettingsCache.FONT_SIZE - 5) * 5);
	m_arrMechaItems[7].BG.SetTooltipText(m_strFontSize_Desc, , , 10, , , , 0.0f);

	RenableMechaListItems(8);
}

simulated function SetGeneralTabSelected()
{
	ResetMechaListItems();
	
	m_arrMechaItems[0].UpdateDataCheckbox(m_strSpecialIcons,"" , SettingsCache.TEMP_ICONS, UpdateSpecialIcon);
	m_arrMechaItems[0].BG.SetTooltipText(m_strSpecialIcons_Desc, , , 10, , , , 0.0f);

	RenableMechaListItems(1);
}

function RealizePreviewSizePos()
{
	local int PosAdjust;
	SoldierPanelPreview.RealizeItems();
	if (SoldierPanelPreview.ORIENT_HORIZONTAL)
	{
		SoldierPanelPreview.SetWidth(SoldierPanelPreview.MAX_SIZE);
		if (SoldierPanelPreview.ANCHOR_RIGHT)
			PosAdjust = SoldierPanelPreview.MAX_SIZE;
		SoldierPanelPreview.SetPosition(SoldierPanelPreview.OFFSET_X - PosAdjust, SoldierPanelPreview.OFFSET_Y);
		PanelMaxSize.SetSize(SoldierPanelPreview.MAX_SIZE, SoldierPanelPreview.ICON_SIZE * 1.15 + 38);
	}
	else
	{
		SoldierPanelPreview.SetHeight(SoldierPanelPreview.MAX_SIZE);
		if (!SoldierPanelPreview.ANCHOR_TOP)
			PosAdjust = SoldierPanelPreview.MAX_SIZE;
		SoldierPanelPreview.SetPosition(SoldierPanelPreview.OFFSET_X, SoldierPanelPreview.OFFSET_Y - PosAdjust);
		PanelMaxSize.SetSize(SoldierPanelPreview.ICON_SIZE * 1.16, SoldierPanelPreview.MAX_SIZE);
	}
	PanelMaxSize.SetPosition(SoldierPanelPreview.X, SoldierPanelPreview.Y);
}

public function UpdateEListSize(UISlider sliderControl)
{
	SettingsCache.MAX_SIZE = sliderControl.percent * 20;
	sliderControl.SetText(string(SettingsCache.MAX_SIZE));
	SoldierPanelPreview.MAX_SIZE = SettingsCache.MAX_SIZE;
	RealizePreviewSizePos();
}

public function UpdateOffsetX(UISlider sliderControl)
{
	SettingsCache.OFFSET_X = sliderControl.percent * 10;
	sliderControl.SetText(string(SettingsCache.OFFSET_X));
	if (SettingsCache.ANCHOR_RIGHT)
	{
		SoldierPanelPreview.OFFSET_X = -SettingsCache.OFFSET_X;
		if (!SettingsCache.ORIENT_HORIZONTAL)
			SoldierPanelPreview.OFFSET_X -= SettingsCache.ICON_SIZE * 1.16;
	}
	else
	{
		SoldierPanelPreview.OFFSET_X = SettingsCache.OFFSET_X;
	}
	RealizePreviewSizePos();
}

public function UpdateOffsetY(UISlider sliderControl)
{
	SettingsCache.OFFSET_Y = sliderControl.percent * 10;
	sliderControl.SetText(string(SettingsCache.OFFSET_Y));
	if (SettingsCache.ANCHOR_TOP)
	{
		SoldierPanelPreview.OFFSET_Y = SettingsCache.OFFSET_Y;
	}
	else
	{
		SoldierPanelPreview.OFFSET_Y = -SettingsCache.OFFSET_Y;
		if (SettingsCache.ORIENT_HORIZONTAL)
			SoldierPanelPreview.OFFSET_Y -= SettingsCache.ICON_SIZE * 1.15 + 38;
	}
	RealizePreviewSizePos();
}

public function UpdateIconSize(UISlider sliderControl)
{
	local SoldierQuickIconObject SoldierIcon;
	local XComGameState_Unit Unit;
	SettingsCache.ICON_SIZE = sliderControl.percent * 3 + 20;
	sliderControl.SetText(string(SettingsCache.ICON_SIZE));
	SoldierPanelPreview.ICON_SIZE = SettingsCache.ICON_SIZE;

	foreach SoldierPanelPreview.SoldierIcons(SoldierIcon)
	{
		SoldierIcon.Cleanup();
	}
	SoldierPanelPreview.SoldierIcons.Length = 0;

	Unit = XComGameState_Unit(`XCOMHISTORY.GetSingleGameStateObjectForClass(class'XComGameState_Unit', false));
	SoldierIcon = SoldierQuickIconObject(SoldierPanelPreview.CreateItem(class'SoldierQuickIconObject')).InitQuickIcon(Unit, SettingsCache.ICON_SIZE, SettingsCache.FONT_SIZE);
	SoldierIcon.SetFocus(true);
	SoldierPanelPreview.SoldierIcons.AddItem(SoldierIcon);
	SoldierIcon = SoldierQuickIconObject(SoldierPanelPreview.CreateItem(class'SoldierQuickIconObject')).InitQuickIcon(Unit, SettingsCache.ICON_SIZE, SettingsCache.FONT_SIZE);
	SoldierPanelPreview.SoldierIcons.AddItem(SoldierIcon);

	RealizePreviewSizePos();
	SoldierPanelPreview.RealizeList();
}

public function UpdateFontSize(UISlider sliderControl)
{
	local SoldierQuickIconObject SoldierIcon;
	local XComGameState_Unit Unit;
	SettingsCache.FONT_SIZE = sliderControl.percent / 5 + 5;
	sliderControl.SetText(string(SettingsCache.FONT_SIZE));
	SoldierPanelPreview.FONT_SIZE = SettingsCache.FONT_SIZE;

	foreach SoldierPanelPreview.SoldierIcons(SoldierIcon)
	{
		SoldierIcon.Cleanup();
	}
	SoldierPanelPreview.SoldierIcons.Length = 0;

	Unit = XComGameState_Unit(`XCOMHISTORY.GetSingleGameStateObjectForClass(class'XComGameState_Unit', false));
	SoldierIcon = SoldierQuickIconObject(SoldierPanelPreview.CreateItem(class'SoldierQuickIconObject')).InitQuickIcon(Unit, SettingsCache.ICON_SIZE, SettingsCache.FONT_SIZE);
	SoldierIcon.SetFocus(true);
	SoldierPanelPreview.SoldierIcons.AddItem(SoldierIcon);
	SoldierIcon = SoldierQuickIconObject(SoldierPanelPreview.CreateItem(class'SoldierQuickIconObject')).InitQuickIcon(Unit, SettingsCache.ICON_SIZE, SettingsCache.FONT_SIZE);
	SoldierPanelPreview.SoldierIcons.AddItem(SoldierIcon);

	RealizePreviewSizePos();
	SoldierPanelPreview.RealizeList();
}

public function UpdatePadding(UISlider sliderControl)
{
	SettingsCache.ICON_PADDING = sliderControl.percent;
	sliderControl.SetText(string(SettingsCache.ICON_PADDING));
	SoldierPanelPreview.ICON_PADDING = SettingsCache.ICON_PADDING;
	SoldierPanelPreview.ItemPadding = SettingsCache.ICON_PADDING;
	RealizePreviewSizePos();
	SoldierPanelPreview.RealizeList();
}

public function UpdateOrientation(UICheckbox checkboxControl)
{
	local SoldierQuickIconObject SoldierIcon;
	local XComGameState_Unit Unit;
	SettingsCache.ORIENT_HORIZONTAL = checkboxControl.bChecked;
	SoldierPanelPreview.ORIENT_HORIZONTAL = SettingsCache.ORIENT_HORIZONTAL;
	SoldierPanelPreview.bIsHorizontal = SettingsCache.ORIENT_HORIZONTAL;

	foreach SoldierPanelPreview.SoldierIcons(SoldierIcon)
	{
		SoldierIcon.Cleanup();
	}
	SoldierPanelPreview.SoldierIcons.Length = 0;

	Unit = XComGameState_Unit(`XCOMHISTORY.GetSingleGameStateObjectForClass(class'XComGameState_Unit', false));
	SoldierIcon = SoldierQuickIconObject(SoldierPanelPreview.CreateItem(class'SoldierQuickIconObject')).InitQuickIcon(Unit, SettingsCache.ICON_SIZE, SettingsCache.FONT_SIZE);
	SoldierIcon.SetFocus(true);
	SoldierPanelPreview.SoldierIcons.AddItem(SoldierIcon);
	SoldierIcon = SoldierQuickIconObject(SoldierPanelPreview.CreateItem(class'SoldierQuickIconObject')).InitQuickIcon(Unit, SettingsCache.ICON_SIZE, SettingsCache.FONT_SIZE);
	SoldierPanelPreview.SoldierIcons.AddItem(SoldierIcon);

	RealizePreviewSizePos();
	SoldierPanelPreview.RealizeList();
}

public function UpdateAnchors(UIListItemSpinner SpinnerControl, int Direction)
{
	local int anchorIndex;

	anchorIndex = Anchor2Int();
	anchorIndex = (anchorIndex + Direction) % 4;
	if (anchorIndex < 0)
		anchorIndex = 3;
	SetItemAnchor(anchorIndex);
	SoldierPanelPreview.ANCHOR_TOP = SettingsCache.ANCHOR_TOP;
	SoldierPanelPreview.ANCHOR_RIGHT = SettingsCache.ANCHOR_RIGHT;
	if (SettingsCache.ANCHOR_TOP && SettingsCache.ANCHOR_RIGHT)
	{
		SoldierPanelPreview.AnchorTopRight();
		PanelMaxSize.AnchorTopRight();
	}
	else if (!SettingsCache.ANCHOR_TOP && SettingsCache.ANCHOR_RIGHT)
	{
		SoldierPanelPreview.AnchorBottomRight();
		PanelMaxSize.AnchorBottomRight();
	}
	else if (SettingsCache.ANCHOR_TOP && !SettingsCache.ANCHOR_RIGHT)
	{
		SoldierPanelPreview.AnchorTopLeft();
		PanelMaxSize.AnchorTopLeft();
	}
	else if (!SettingsCache.ANCHOR_TOP && !SettingsCache.ANCHOR_RIGHT)
	{
		SoldierPanelPreview.AnchorBottomLeft();
		PanelMaxSize.AnchorBottomLeft();
	}
	SpinnerControl.SetValue(Anchor2Text());
	if (SettingsCache.ANCHOR_TOP)
	{
		SoldierPanelPreview.OFFSET_Y = SettingsCache.OFFSET_Y;
	}
	else
	{
		SoldierPanelPreview.OFFSET_Y = -SettingsCache.OFFSET_Y;
		if (SettingsCache.ORIENT_HORIZONTAL)
			SoldierPanelPreview.OFFSET_Y -= SettingsCache.ICON_SIZE * 1.15 + 38;
	}
	if (SettingsCache.ANCHOR_RIGHT)
	{
		SoldierPanelPreview.OFFSET_X = -SettingsCache.OFFSET_X;
		if (!SettingsCache.ORIENT_HORIZONTAL)
			SoldierPanelPreview.OFFSET_X -= SettingsCache.ICON_SIZE * 1.16;
	}
	else
	{
		SoldierPanelPreview.OFFSET_X = SettingsCache.OFFSET_X;
	}
	RealizePreviewSizePos();
}

public function UpdateSpecialIcon(UICheckbox checkboxControl)
{
	SettingsCache.TEMP_ICONS = checkboxControl.bChecked;
	SoldierPanelPreview.TEMP_ICONS = SettingsCache.TEMP_ICONS;
}

simulated function ApplyOptions(UIButton Button)
{
	// To-do: Use the checkboxes to do some sort of Favorites filtering for long mod options lists
}

simulated function ExitScreen()
{
	NavHelp.ClearButtonHelp();

	Movie.Stack.Pop(self);
	Movie.Pres.PlayUISound(eSUISound_MenuClose);
}

simulated function bool IsSettingsChanged()
{
	if (SettingsCache.MAX_SIZE != class'UISoldierQuickIconPanel'.default.MAX_SIZE)
		return true;
	if (SettingsCache.OFFSET_X != class'UISoldierQuickIconPanel'.default.OFFSET_X)
		return true;
	if (SettingsCache.OFFSET_Y != class'UISoldierQuickIconPanel'.default.OFFSET_Y)
		return true;
	if (SettingsCache.ICON_SIZE != class'UISoldierQuickIconPanel'.default.ICON_SIZE)
		return true;
	if (SettingsCache.ICON_PADDING != class'UISoldierQuickIconPanel'.default.ICON_PADDING)
		return true;
	if (SettingsCache.ANCHOR_TOP != class'UISoldierQuickIconPanel'.default.ANCHOR_TOP)
		return true;
	if (SettingsCache.ANCHOR_RIGHT != class'UISoldierQuickIconPanel'.default.ANCHOR_RIGHT)
		return true;
	if (SettingsCache.ORIENT_HORIZONTAL != class'UISoldierQuickIconPanel'.default.ORIENT_HORIZONTAL)
		return true;
	if (SettingsCache.FONT_SIZE != class'UISoldierQuickIconPanel'.default.FONT_SIZE)
		return true;
	if (SettingsCache.TEMP_ICONS != class'UISoldierQuickIconPanel'.default.TEMP_ICONS)
		return true;

	return false;
}

simulated public function NoSaveExit(UIButton Button)
{
	local TDialogueBoxData kDialogData;
	// Put up a confirmation
	if (IsSettingsChanged())
	{
		kDialogData.eType       = eDialog_Warning;
		kDialogData.strText = class'UIOptionsPCScreen'.default.m_strIgnoreChangesDialogue;
		kDialogData.strCancel = class'UIOptionsPCScreen'.default.m_strIgnoreChangesCancel;
		kDialogData.strAccept = class'UIOptionsPCScreen'.default.m_strIgnoreChangesConfirm;	
		kDialogData.fnCallback  = NoSaveCallback;
		Movie.Pres.UIRaiseDialog( kDialogData );
	}
	else
		ExitScreen();
}

simulated public function NoSaveCallback(Name eAction)
{
	if (eAction == 'eUIAction_Accept')
		ExitScreen();
}

simulated function SaveAll()
{
	class'UISoldierQuickIconPanel'.default.MAX_SIZE = SettingsCache.MAX_SIZE;
	class'UISoldierQuickIconPanel'.default.OFFSET_X = SettingsCache.OFFSET_X;
	class'UISoldierQuickIconPanel'.default.OFFSET_Y = SettingsCache.OFFSET_Y;
	class'UISoldierQuickIconPanel'.default.ICON_SIZE = SettingsCache.ICON_SIZE;
	class'UISoldierQuickIconPanel'.default.ICON_PADDING = SettingsCache.ICON_PADDING;
	class'UISoldierQuickIconPanel'.default.ANCHOR_TOP = SettingsCache.ANCHOR_TOP;
	class'UISoldierQuickIconPanel'.default.ANCHOR_RIGHT = SettingsCache.ANCHOR_RIGHT;
	class'UISoldierQuickIconPanel'.default.ORIENT_HORIZONTAL = SettingsCache.ORIENT_HORIZONTAL;
	class'UISoldierQuickIconPanel'.default.TEMP_ICONS = SettingsCache.TEMP_ICONS;
	class'UISoldierQuickIconPanel'.default.FONT_SIZE = SettingsCache.FONT_SIZE;
	class'UISoldierQuickIconPanel'.default.ConfigVersion = class'UISL_InitSettings'.default.CURRENT_VERSION;
}

simulated public function SaveAndExit(UIButton Button)
{
	local UITacticalHUD TacticalScreen;
	local UIPanel RealSoldierIconPanel;
	local array<UIPanel> PossibleList;
	local Object IconPanelObj;

	if (IsSettingsChanged())
	{
		SaveAll();
		class'UISoldierQuickIconPanel'.static.StaticSaveConfig();
		// Also change the real one
		TacticalScreen = UITacticalHUD(`PRES.ScreenStack.GetFirstInstanceOf(class'UITacticalHUD'));
		if (TacticalScreen != none)
		{
			TacticalScreen.GetChildrenOfType(class'UISoldierQuickIconPanel', PossibleList);
			foreach PossibleList(RealSoldierIconPanel)
			{
				RealSoldierIconPanel.Hide();
				`XCOMVISUALIZATIONMGR.RemoveObserver(RealSoldierIconPanel);
				IconPanelObj = RealSoldierIconPanel;
				`XEVENTMGR.UnRegisterFromAllEvents(IconPanelObj);
				RealSoldierIconPanel.Remove();
			}
			TacticalScreen.Movie.Pres.Spawn(class'UISoldierQuickIconPanel', TacticalScreen).InitSoldierQuickInfo();
		}
	}
	ExitScreen();
}

DefaultProperties
{
	bConsumeMouseEvents=true
	Padding=10

	Package   = "/ package/gfxOptionsScreen/OptionsScreen";
	MCName      = "theScreen";
	LibID       = "OptionsScreen"
	m_iCurrentTab = -1;
	NUM_LISTITEMS = 10;
}