//-----------------------------------------------------------
//
//-----------------------------------------------------------
class Menu_CarSelection extends UT2K4GUIPage;

#exec OBJ LOAD FILE=..\Textures\Lupow_Skins

var automated GUIButton		bt_Select;
var automated GUIListBox	lst_VehicleList;
var automated GUIImage      img_Car;
var automated GUIComboBox   cbo_SkinList;

var SpinnyWeap				SpinnyCar;
var int						SpinnySkinOffset;
var vector					SpinnyCarOffset;
var float SpinnyAdjuster;

var bool 					bSelected, bBusy;

var config string 			LastVehicleChosen;

var array<string> DisallowVehicleList;

var array<string> Vehicles;
var array<string> description;

var vector CamPos, X, Y, Z;
var rotator CamRot;

function bool InternalDraw(Canvas Canvas)
//function InternalOnRendered(Canvas Canvas)
{
 	if (SpinnyCar == None)
		return false;

	Canvas.GetCameraLocation(CamPos, CamRot);

	GetAxes(CamRot, X, Y, Z);

   	Canvas.DrawActor(SpinnyCar, false, true, 90.0);

//	Canvas.DrawActorClipped(SpinnyCar, false, 0, 0.25, 0.62, 1, true, 90.0);


	return false;
}

function InitComponent(GUIController MyController, GUIComponent MyOwner)
{
    //local string Entry;
    local int index;

	Super.Initcomponent(MyController, MyOwner);
    Index = 0;

	vehicles[0]="R.Car_Beetle";
	description[0]="Beetle";

                     //Change number below to number of vehicles
	while (index!= 1)
	{
            lst_VehicleList.List.Add(description[index],,vehicles[index]);

			lst_VehicleList.List.SetIndex(Index);

			Lastvehiclechosen = vehicles[index];

			Index++;
	}
	lst_VehicleList.List.SetIndex(0);
	//ChangeSelection(lst_VehicleList);
}

function bool VehicleAllowed(string ClassName, array<string> Disallowed)
{
	return True;
}

function ChangeSelection(GUIComponent Sender)
{
  	local class<Car_R> TestCar;
    local material SkinToUse;
    local int i;
	bBusy = True;

	if (SpinnyCar != None)
		SpinnyCar.Destroy();

	// Spawn spinning vehicle actor
	SpinnyCar = PlayerOwner().spawn(class'XInterface.SpinnyWeap');
	SpinnyCar.bHidden = false;
	SpinnyCar.SetRotation(rot(0,0,0));
	SpinnyCar.SetDrawType(DT_Mesh);
	SpinnyCar.bPlayRandomAnims = false;
	SpinnyCar.SetDrawScale(0.37);
	SpinnyCar.SpinRate = 5000;
    SpinnyCar.SetLocation(PlayerOwner().Location-vect(35,0,0));

	LastVehicleChosen = lst_VehicleList.List.GetExtra();
	TestCar = class<Car_R>(DynamicLoadObject(LastVehicleChosen, class'Class'));
    Player_Controller(PlayerOwner()).SetVehicleClass(TestCar);
	class'Player_Controller'.default.VehicleClass = TestCar;
    class'Player_Controller'.static.StaticSaveConfig();

    if (TestCar.Default.VehicleSkin.Length < 1)
    	SkinToUse = TestCar.default.Skins[0];
	else
		SkinToUse = Material(DynamicLoadObject(TestCar.Default.VehicleSkin[TestCar.Default.CurrentSkin], class'Material'));

	SpinnyCar.LinkMesh(TestCar.default.Mesh);
    //SpinnyCar.SetDrawScale((TestCar.default.DrawScale / 2) * TestCar.default.SpinnyScaler);
    SpinnyCar.SetDrawScale((TestCar.default.DrawScale / 1.75) * TestCar.default.SpinnyScaler);

	for (i = 0; i < TestCar.Default.Skins.Length; i++)
		SpinnyCar.Skins[i] = TestCar.default.Skins[i];
	SpinnySkinOffset = TestCar.default.SkinOffset;
	SpinnyCar.Skins[SpinnySkinOffset] = SkinToUse;//TestCar.default.Skins[0];
	if (SpinnyCar.Skins[SpinnySkinOffset].IsA('Shader'))
	{
//		Shader(SpinnyCar.Skins[SpinnySkinOffset]).Specular = Shader(SpinnyCar.Skins[SpinnySkinOffset]).default.Specular;
		Shader(SpinnyCar.Skins[SpinnySkinOffset]).SelfIllumination = None;
		Shader(SpinnyCar.Skins[SpinnySkinOffset]).SelfIlluminationMask = None;
	}

    cbo_SkinList.RemoveItem(0, cbo_SkinList.ItemCount());
	for (i = 0; i < TestCar.Default.VehicleDesc.Length; i++)
		cbo_SkinList.AddItem(TestCar.Default.VehicleDesc[i],,TestCar.Default.VehicleSkin[i]);
	if (cbo_SkinList.ItemCount() > 0)
		cbo_SkinList.SetIndex(TestCar.Default.CurrentSkin);
    SaveConfig();
    bBusy=false ;
}

function ChangeSkin(GUIComponent Sender)
{
	local material SkinToUse;

	if ((cbo_SkinList.ItemCount() < 1) || bBusy)
		return;

	SkinToUse = Material(DynamicLoadObject(class'Player_Controller'.default.VehicleClass.Default.VehicleSkin[cbo_SkinList.GetIndex()], class'Material'));

	SpinnyCar.Skins[SpinnySkinOffset] = SkinToUse;

	class'Player_Controller'.default.VehicleClass.default.CurrentSkin = cbo_SkinList.GetIndex();
	class'Player_Controller'.default.VehicleClass.static.StaticSaveConfig();
}


function bool ClickSelect(GUIComponent Sender)
{
	Player_Controller(PlayerOwner()).GetCar();
	Player_Controller(PlayerOwner()).bSelectingCar = true;
	bSelected = True;
	if (SpinnyCar != None)
		SpinnyCar.Destroy();
	Controller.CloseAll(true);
	return True;
}

function bool CanClose(optional bool bCanceled)
{
	return bSelected; //&& SpinnyCar.Mesh != None;
}

function CloseWindow(optional Bool bCanceled)
{
	if (SpinnyCar != None)
		SpinnyCar.Destroy();
}

defaultproperties
{
     Begin Object Class=GUIButton Name=OkButton
         Caption="Ok"
         StyleName="RoundButton"
         WinTop=0.764583
         WinLeft=0.125000
         WinWidth=0.251562
         OnClick=Menu_CarSelection.ClickSelect
         OnKeyEvent=OkButton.InternalOnKeyEvent
     End Object
     bt_Select=GUIButton'R.Menu_CarSelection.OkButton'

     Begin Object Class=GUIListBox Name=CarList
         OnCreateComponent=CarList.InternalOnCreateComponent
         StyleName="IRCText"
         WinTop=0.314583
         WinLeft=0.126562
         WinWidth=0.250000
         WinHeight=0.450000
         RenderWeight=1.500000
         OnChange=Menu_CarSelection.ChangeSelection
     End Object
     lst_VehicleList=GUIListBox'R.Menu_CarSelection.CarList'

     Begin Object Class=GUIImage Name=Bk1
         Image=Texture'2K4Menus.NewControls.Display99'
         ImageStyle=ISTY_Stretched
         ImageRenderStyle=MSTY_Translucent
         WinTop=0.182064
         WinLeft=0.086969
         WinWidth=0.787864
         WinHeight=0.692166
     End Object
     img_Car=GUIImage'R.Menu_CarSelection.Bk1'

     Begin Object Class=GUIComboBox Name=cbox1
         bReadOnly=True
         Hint="Select car skin"
         WinTop=0.762503
         WinLeft=0.475313
         WinWidth=0.250000
         WinHeight=0.040000
         TabOrder=0
         OnChange=Menu_CarSelection.ChangeSkin
         OnKeyEvent=cbox1.InternalOnKeyEvent
     End Object
     cbo_SkinList=GUIComboBox'R.Menu_CarSelection.cbox1'

     SpinnyCarOffset=(X=220.000000,Y=-200.000000,Z=10.000000)
     LastVehicleChosen="R.Car_Cardette"
     bRenderWorld=True
     bAllowedAsLast=True
     OnCanClose=Menu_CarSelection.CanClose
     WinTop=0.227083
     WinLeft=0.103125
     WinWidth=0.771878
     WinHeight=0.620000
}
