class ZonerMenu extends GUI2K4.MidGamePanel
  config(HideMut);

//=================================================================Interface section
/*

var automated MOCheckBox
var automated MoComboBox
var automated moNumericEdit
var automated moFloatEdit

var     Automated   GUIListBox
var     Automated 	GUISectionBackground

var automated moNumericEdit
*/
var		automated	GUIButton b_SetStart, b_SetEnd, b_SaveConfig, b_DrawZones, b_ShowZonedStaticMeshes;

var automated GUISectionBackground sb_ZonerDraw;
//=================================================================Interface section
//=================================================================Structs start
struct MyZone
{
  var int ZoneKey;			//zone key
  var string ZoneName;		//Zone name
  var array<Vector> VectorsCoord;	//Coordinates of zone borders. Drawing from 0 to 1, 1 to 2 and etc.
  var float MinX, MinY, MaxX, MaxY;	//Minimal coords for setting zoned actors
  var array<int> PortalActorsRefs;	//Actors that are visible through "portal" aka hole between neighbouring zones. Neighbouring zones may have same set of this
  var array<int> DecorativeActorsRefs;	//Decorative stuff between zones
  var bool IgnoreXAxis;			//Ignore this axis when setting active zone
  var bool IgnoreYAxis;
  var bool IgnoreZAxis;
  var bool HideDecorativeActors;
  var bool HidePortalActors;
};

// Struct for map zoning
struct MyZonedMap
{
  var string MapName;
  var array<int> MyZoneRef;		//reference to my zone structure
  var int MinVectorRef;	//currently all vectors are 1 big array for all maps
  var int MaxVectorRef;
};

var config array<MyZonedMap> ZonedMaps;
var config array<MyZone> MyZones;

var PlayerController PC;
var vector LineStart, LineEnd;
var byte R, G, B;			//colors for vectors
var config array<Vector> ZVectors;
var config array<string> ZZones;
var config array<StaticMeshActor> SMAS;
var int LinesCount, NextZone;
//=================================================================Structs end

simulated function InitComponent(GUIController MyController, GUIComponent MyOwner)
{
  super.InitComponent(MyController,MyOwner);

  sb_ZonerDraw.ManageComponent(b_SetStart);
  sb_ZonerDraw.ManageComponent(b_SetEnd);
  sb_ZonerDraw.ManageComponent(b_SaveConfig);
  sb_ZonerDraw.ManageComponent(b_DrawZones);
  sb_ZonerDraw.ManageComponent(b_ShowZonedStaticMeshes);
  PC = PlayerOwner();
  R = 255;		//Line color
  G = 0;
  B = 0;
}

function ShowPanel(bool bShow)
{
  super.ShowPanel(bShow);
}

event Opened(GUIComponent Sender)
{
  super.Opened(Sender);
}

function StartDrawing()
{
  LineStart = PC.Pawn.Location;
}

function DrawLine()
{
  LineEnd = PC.Pawn.Location;
  PC.Pawn.DrawStayingDebugLine(LineStart, LineEnd, R, G, B);			//Draw line for visual zone representation
  ZVectors[LinesCount] = LineStart;		//Save our vectors
  LinesCount++;
  ZVectors[LinesCount] = LineEnd;
  LinesCount++;
  LineStart = LineEnd;			//Next line
}

function SaveConfigStr()
{
  local int i,k; //,l;

  ZonedMaps.length = ZonedMaps.length + 1;
  ZonedMaps[ZonedMaps.length-1].MapName = PC.Level.GetURLMap();	//save variables
  for (i=0; i<9;i++)
  {
    ZonedMaps[ZonedMaps.length-1].MyZoneRef[i] = i;
  }

  for (i=0; i<ZVectors.length;i++)
  {
    if (i==0 || i==5 || i==10 || i==15 || i==20 || i==25 || i==30 || i==35 || i==40 || i==45)
    {
      MyZones.Insert(MyZones.length, 1);
      MyZones[MyZones.length-1].ZoneName = "Zone" @ MyZones.length-1;		//Create new zone
      MyZones[MyZones.length-1].ZoneKey = MyZones.length-1;
      k=0;
    }
    MyZones[MyZones.length-1].VectorsCoord[k] = ZVectors[i];
    k++;
  }

  for (i=0; i<MyZones.length;i++)			//Let's add min|max values to structure
  {
    for (k=0; k<MyZones[i].VectorsCoord.length;k++)
    {
      if (MyZones[i].VectorsCoord[k].X < MyZones[i].MinX || MyZones[i].MinX==0)
        MyZones[i].MinX = MyZones[i].VectorsCoord[k].X;
      if (MyZones[i].VectorsCoord[k].Y < MyZones[i].MinY || MyZones[i].MinY==0)
        MyZones[i].MinY = MyZones[i].VectorsCoord[k].Y;
      if (MyZones[i].VectorsCoord[k].X > MyZones[i].MaxX || MyZones[i].MaxX==0)
        MyZones[i].MaxX = MyZones[i].VectorsCoord[k].X;
      if (MyZones[i].VectorsCoord[k].Y > MyZones[i].MaxY || MyZones[i].MaxY==0)
        MyZones[i].MaxY = MyZones[i].VectorsCoord[k].Y;
    }
  }

  SaveConfig();
}

function DrawZonesFromConfig()
{
  local int i;

  PC.Pawn.ClearStayingDebugLines();
  for (i=0; i<ZVectors.length; i++)
  {
    if (i==4 || i==9 || i==14 || i==19 || i==24 || i==29 || i==34 || i==39 || i==44)
      continue;
    PC.Pawn.DrawStayingDebugLine(ZVectors[i], ZVectors[i+1], R, G, B);
  }
}

function ShowZonedStaticMeshes()
{
  local int i; // k;
  local StaticMeshActor SMA;
  local float MaxX,MaxY,MinX,MinY;

  for (i=0; i<5; i++)	//For each 5 vectors get MaxX,MaxY,MinX,MinY
  {
    if (ZVectors[i].X > MaxX || MaxX==0)
    MaxX = ZVectors[i].X;
    if (ZVectors[i].Y > MaxY || MaxY==0)
    MaxY = ZVectors[i].Y;
    if (ZVectors[i].X < MinX || MinX==0)
    MinX = ZVectors[i].X;
    if (ZVectors[i].Y < MinY || MinY==0)
    MinY = ZVectors[i].Y;
  }
  PC.ClientMessage(MaxX @ MaxY @ MinX @ MinY);

  forEach PC.AllActors(class'StaticMeshActor', SMA)
  {
    if((SMA.Location.X <= MaxX && SMA.Location.Y <= MaxY) && (SMA.Location.X >= MinX && SMA.Location.Y >= MinY))
    {
      SMA.bHidden = False;
      SMA.ResetStaticFilterState();
    }
  }
}

simulated function bool ButtonClicked(GUIComponent Sender)
{
  switch (Sender)
  {
    case b_SetStart:
      StartDrawing();
      break;
    case b_SetEnd:
      DrawLine();
      break;
    case b_SaveConfig:
      SaveConfigStr();
      break;
    case b_DrawZones:
      DrawZonesFromConfig();
      break;
    case b_ShowZonedStaticMeshes:
      ShowZonedStaticMeshes();
      break;
    default:
      return false;
  }
  return true;
}

simulated function InternalOnChange(GUIComponent Sender){}

defaultproperties
{
  Begin Object Class=GUIButton Name=bSetStart
    Caption="Set Start vector"
    OnClick=ZonerMenu.ButtonClicked
    OnKeyEvent=bSetStart.InternalOnKeyEvent
  End Object
  b_SetStart=GUIButton'hidemut.ZonerMenu.bSetStart'

  Begin Object Class=GUIButton Name=bSetEnd
    Caption="Set End vector"
    OnClick=ZonerMenu.ButtonClicked
    OnKeyEvent=bSetEnd.InternalOnKeyEvent
  End Object
  b_SetEnd=GUIButton'hidemut.ZonerMenu.bSetEnd'

  Begin Object Class=GUIButton Name=bSaveConfig
    Caption="Save config"
    OnClick=ZonerMenu.ButtonClicked
    OnKeyEvent=bSaveConfig.InternalOnKeyEvent
  End Object
  b_SaveConfig=GUIButton'hidemut.ZonerMenu.bSaveConfig'

  Begin Object Class=GUIButton Name=bDrawZones
    Caption="Draw zones"
    OnClick=ZonerMenu.ButtonClicked
    OnKeyEvent=bDrawZones.InternalOnKeyEvent
  End Object
  b_DrawZones=GUIButton'hidemut.ZonerMenu.bDrawZones'

  Begin Object Class=GUIButton Name=bShowZonedStaticMeshes
    Caption="Draw zoned static meshes"
    OnClick=ZonerMenu.ButtonClicked
    OnKeyEvent=bShowZonedStaticMeshes.InternalOnKeyEvent
  End Object
  b_ShowZonedStaticMeshes=GUIButton'hidemut.ZonerMenu.bShowZonedStaticMeshes'

  Begin Object Class=GUISectionBackground Name=BGZonerDraw
    bFillClient=True
    Caption="Zoner Draw"
    WinWidth=0.900000
    WinHeight=0.900000
    OnPreDraw=BGZonerDraw.InternalPreDraw
  End Object
  sb_ZonerDraw=GUISectionBackground'hidemut.ZonerMenu.BGZonerDraw'
}