unit UFormNew;

{$mode delphi}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ComCtrls, Spin, ExtCtrls, ActnList, ExtDlgs, Menus, UGame, UGeometry;

type

  { TFormNew }

  TFormNew = class(TForm)
    APlayerAdd: TAction;
    APlayerRemove: TAction;
    APlayerModify: TAction;
    ActionList1: TActionList;
    ButtonImageBrowse: TButton;
    ButtonCancel: TButton;
    ButtonOk: TButton;
    ButtonPlayerAdd: TButton;
    ButtonPlayerModify: TButton;
    ButtonPlayerRemove: TButton;
    CheckBoxBridges: TCheckBox;
    CheckBoxFogOfWar: TCheckBox;
    CheckBoxCity: TCheckBox;
    CheckBoxSymetricMap: TCheckBox;
    CheckBoxVoid: TCheckBox;
    ComboBoxGridType: TComboBox;
    ComboBoxMapShape: TComboBox;
    ComboBoxWinObjective: TComboBox;
    EditImageFile: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    ListView1: TListView;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    OpenPictureDialog1: TOpenPictureDialog;
    PageControl1: TPageControl;
    PopupMenu1: TPopupMenu;
    RadioGroupGrowAmount: TRadioGroup;
    RadioGroupGrowCells: TRadioGroup;
    SpinEditCityPercent: TSpinEdit;
    SpinEditMapSizeX: TSpinEdit;
    SpinEditMapSizeY: TSpinEdit;
    SpinEditNeutralUnits: TSpinEdit;
    SpinEditVoidPercent: TSpinEdit;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    TrackBarSizeX: TTrackBar;
    TrackBarSizeY: TTrackBar;
    procedure APlayerAddExecute(Sender: TObject);
    procedure APlayerModifyExecute(Sender: TObject);
    procedure APlayerRemoveExecute(Sender: TObject);
    procedure ButtonImageBrowseClick(Sender: TObject);
    procedure CheckBoxCityChange(Sender: TObject);
    procedure CheckBoxVoidChange(Sender: TObject);
    procedure ComboBoxMapShapeChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ListView1CustomDrawSubItem(Sender: TCustomListView;
      Item: TListItem; SubItem: Integer; State: TCustomDrawState;
      var DefaultDraw: Boolean);
    procedure ListView1Data(Sender: TObject; Item: TListItem);
    procedure ListView1DblClick(Sender: TObject);
    procedure ListView1KeyPress(Sender: TObject; var Key: char);
    procedure ListView1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState
      );
    procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure SpinEditMapSizeXChange(Sender: TObject);
    procedure SpinEditMapSizeYChange(Sender: TObject);
    procedure TrackBarSizeYChange(Sender: TObject);
    procedure TrackBarSizeXChange(Sender: TObject);
  private
    Players: TPlayers;
  public
    procedure Translate;
    procedure ReloadView;
    procedure UpdateInterface;
    procedure Load(Game: TGame);
    procedure Save(Game: TGame);
  end;

var
  FormNew: TFormNew;

implementation

{$R *.lfm}

uses
  UFormPlayer;

resourcestring
  SGridTypeHexagon = 'Hexagonal';
  SGridTypeSquare = 'Square';
  SGridTypeTriangle = 'Triangonal';
  SGridTypeVoronoi = 'Voronoi random';
  SWinObjectiveDefeatAllOponents = 'Defeat all oponents';
  SWinObjectiveDefeatAllCities = 'Defeat all oponents cities';
  SWinObjectiveCapturePosition = 'Capture position';
  SWinObjectiveStayAliveForTurns = 'Stay alive for number of turns';
  SGrowNone = 'None';
  SGrowPlayerCities = 'Player cities';
  SGrowPlayerAllCells = 'Player all cells';
  SGrowAmountByOne = 'By one';
  SGrowAmountBySquareRoot = 'By square root';
  SRectangular = 'Rectangular';
  SRounded = 'Rounded';
  SFromImageFile = 'From image file';

{ TFormNew }

procedure TFormNew.ListView1Data(Sender: TObject; Item: TListItem);
begin
  if (Item.Index >= 0) and (Item.Index < Players.Count) then
  with TPlayer(Players[Item.Index]) do begin
    Item.Caption := Name;
    Item.Data := Players[Item.Index];
    Item.SubItems.Add(PlayerModeText[Mode]);
    Item.SubItems.Add(''); // Do not show color text
    Item.SubItems.Add(IntToStr(StartUnits));
  end;
end;

procedure TFormNew.ListView1DblClick(Sender: TObject);
begin
  APlayerModify.Execute;
end;

procedure TFormNew.ListView1KeyPress(Sender: TObject; var Key: char);
begin
  if (Key = #13) then APlayerModify.Execute;
end;

procedure TFormNew.ListView1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = 45 then APlayerAdd.Execute;
  if Key = 46 then APlayerRemove.Execute;
end;

procedure TFormNew.ListView1SelectItem(Sender: TObject; Item: TListItem;
  Selected: Boolean);
begin
  ReloadView;
end;

procedure TFormNew.SpinEditMapSizeXChange(Sender: TObject);
begin
  TrackBarSizeX.Position := SpinEditMapSizeX.Value;
end;

procedure TFormNew.SpinEditMapSizeYChange(Sender: TObject);
begin
  TrackBarSizeY.Position := SpinEditMapSizeY.Value;
end;

procedure TFormNew.TrackBarSizeYChange(Sender: TObject);
begin
  SpinEditMapSizeY.Value := TrackBarSizeY.Position;
end;

procedure TFormNew.TrackBarSizeXChange(Sender: TObject);
begin
  SpinEditMapSizeX.Value := TrackBarSizeX.Position;
end;

procedure TFormNew.Translate;
var
  LastIndex: Integer;
begin
  with ComboBoxGridType do begin
    LastIndex := ItemIndex;
    Clear;
    Items.Add(SGridTypeHexagon);
    Items.Add(SGridTypeSquare);
    Items.Add(SGridTypeTriangle);
    Items.Add(SGridTypeVoronoi);
    ItemIndex := LastIndex;
  end;
  with ComboBoxWinObjective do begin
    LastIndex := ItemIndex;
    Clear;
    Items.Add(SWinObjectiveDefeatAllOponents);
    Items.Add(SWinObjectiveDefeatAllCities);
    Items.Add(SWinObjectiveCapturePosition);
    Items.Add(SWinObjectiveStayAliveForTurns);
    ItemIndex := LastIndex;
  end;
  with ComboBoxMapShape do begin
    LastIndex := ItemIndex;
    Clear;
    Items.Add(SRectangular);
    Items.Add(SFromImageFile);
    Items.Add(SRounded);
    ItemIndex := LastIndex;
  end;
  with RadioGroupGrowCells do begin
    LastIndex := ItemIndex;
    Items.Clear;
    Items.Add(SGrowNone);
    Items.Add(SGrowPlayerCities);
    Items.Add(SGrowPlayerAllCells);
    ItemIndex := LastIndex;
  end;
  with RadioGroupGrowAmount do begin
    LastIndex := ItemIndex;
    Items.Clear;
    Items.Add(SGrowAmountByOne);
    Items.Add(SGrowAmountBySquareRoot);
    ItemIndex := LastIndex;
  end;
end;

procedure TFormNew.ReloadView;
begin
  ListView1.Items.Count := Players.Count;
  ListView1.Refresh;
  Translate;
  UpdateInterface;
end;

procedure TFormNew.UpdateInterface;
begin
  APlayerRemove.Enabled := Assigned(ListView1.Selected) and (Players.Count > 2);
  APlayerAdd.Enabled := Players.Count < MaxPlayerCount;
  APlayerModify.Enabled := Assigned(ListView1.Selected);
  EditImageFile.Enabled := ComboBoxMapShape.ItemIndex = Integer(msImage);
  ButtonImageBrowse.Enabled := ComboBoxMapShape.ItemIndex = Integer(msImage);
end;

procedure TFormNew.FormShow(Sender: TObject);
begin
  ReloadView;
  //Height := Trunc(1.5 * Height);
  PageControl1.TabIndex := 0;
end;

procedure TFormNew.ListView1CustomDrawSubItem(Sender: TCustomListView;
  Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  var DefaultDraw: Boolean);
begin
  if SubItem = 2 then
  with ListView1.Canvas do begin
    Brush.Color := TPlayer(Item.Data).Color;
    Brush.Style := bsSolid;
    FillRect(Item.DisplayRectSubItem(2, drBounds));
  end;
end;

procedure TFormNew.FormCreate(Sender: TObject);
begin
  Players := TPlayers.Create;
end;

procedure TFormNew.CheckBoxCityChange(Sender: TObject);
begin
  SpinEditCityPercent.Enabled := CheckBoxCity.Checked;
end;

procedure TFormNew.CheckBoxVoidChange(Sender: TObject);
begin
  SpinEditVoidPercent.Enabled := CheckBoxVoid.Checked;
end;

procedure TFormNew.ComboBoxMapShapeChange(Sender: TObject);
begin
  ReloadView;
end;

procedure TFormNew.APlayerModifyExecute(Sender: TObject);
begin
  if Assigned(ListView1.Selected) then begin
    FormPlayer.Load(TPlayer(ListView1.Selected.Data));
    if FormPlayer.ShowModal = mrOk then begin
      FormPlayer.Save(TPlayer(ListView1.Selected.Data));
      ReloadView;
    end;
  end;
end;

procedure TFormNew.APlayerRemoveExecute(Sender: TObject);
begin
  if Assigned(ListView1.Selected) then begin
    Players.Remove(ListView1.Selected.Data);
    ReloadView;
  end;
end;

procedure TFormNew.ButtonImageBrowseClick(Sender: TObject);
begin
  OpenPictureDialog1.FileName := EditImageFile.Text;
  if OpenPictureDialog1.Execute then begin
    EditImageFile.Text := OpenPictureDialog1.FileName;
  end;
end;

procedure TFormNew.APlayerAddExecute(Sender: TObject);
var
  NewPlayer: TPlayer;
begin
  if Players.Count <= (MaxPlayerCount - 1) then begin
    NewPlayer := TPlayer.Create;
    NewPlayer.Id := Players.GetNewPlayerId;
    NewPlayer.Name := SPlayer + ' ' + IntToStr(NewPlayer.Id);
    NewPlayer.Color := PlayerColors[Players.Count];
    NewPlayer.Mode := pmComputer;
    NewPlayer.Agressivity := caMedium;
    Players.Add(NewPlayer);
    ReloadView;
  end;
end;

procedure TFormNew.FormDestroy(Sender: TObject);
begin
  Players.Free;
end;

procedure TFormNew.Load(Game: TGame);
begin
  CheckBoxSymetricMap.Checked := Game.SymetricMap;
  CheckBoxVoid.Checked := Game.VoidEnabled;
  SpinEditVoidPercent.Value := Game.VoidPercentage;
  SpinEditVoidPercent.Enabled := Game.VoidEnabled;
  Players.Assign(Game.PlayersSetting);
  CheckBoxCity.Checked := Game.CityEnabled;
  SpinEditCityPercent.Value := Game.CityPercentage;
  SpinEditMapSizeX.Value := Game.Map.Size.X;
  SpinEditMapSizeY.Value := Game.Map.Size.Y;
  TrackBarSizeX.Position := Game.Map.Size.X;
  TrackBarSizeY.Position := Game.Map.Size.Y;
  RadioGroupGrowAmount.ItemIndex := Integer(Game.GrowAmount);
  RadioGroupGrowCells.ItemIndex := Integer(Game.GrowCells);
  ComboBoxGridType.ItemIndex := Integer(Game.MapType) - 1;
  ComboBoxWinObjective.ItemIndex := Integer(Game.WinObjective);
  SpinEditNeutralUnits.Value := Game.MaxNeutralUnits;
  ComboBoxMapShape.ItemIndex := Integer(Game.Map.Shape);
  EditImageFile.Text := Game.MapImageFileName;
  CheckBoxFogOfWar.Checked := Game.FogOfWar;
  CheckBoxBridges.Checked := Game.BridgeEnabled;
end;

procedure TFormNew.Save(Game: TGame);
begin
  Game.PlayersSetting.Assign(Players);
  Game.SymetricMap := CheckBoxSymetricMap.Checked;
  Game.VoidEnabled := CheckBoxVoid.Checked;
  Game.VoidPercentage := SpinEditVoidPercent.Value;
  Game.CityEnabled := CheckBoxCity.Checked;
  Game.CityPercentage := SpinEditCityPercent.Value;
  Game.Map.Size := TPoint.Create(SpinEditMapSizeX.Value, SpinEditMapSizeY.Value);
  Game.GrowAmount := TGrowAmount(RadioGroupGrowAmount.ItemIndex);
  Game.GrowCells := TGrowCells(RadioGroupGrowCells.ItemIndex);
  Game.MapType := TMapType(ComboBoxGridType.ItemIndex + 1);
  Game.WinObjective := TWinObjective(ComboBoxWinObjective.ItemIndex);
  Game.MaxNeutralUnits := SpinEditNeutralUnits.Value;
  Game.Map.Shape := TMapShape(ComboBoxMapShape.ItemIndex);
  Game.MapImageFileName := EditImageFile.Text;
  Game.FogOfWar := CheckBoxFogOfWar.Checked;
  Game.BridgeEnabled := CheckBoxBridges.Checked;
end;

end.

