unit UNewGame;
{ ********************************************************************************* }
{ (c) GNU                                                                           }
{ author: Heiko Tietze                                                              }
{ version: 1.0                                                                      }
{ date: 11.12.2004                                                                  }
{ description: a form to ask for player names and to retriev the playercount        }
{              for a new game GetPlayerNames is called; in network mode the data    }
{              are inserted by code and the form is not shown                       }
{ ********************************************************************************* }

{$I Def.inc}

interface

uses SysUtils, Types, Classes, Variants, IniFiles,
     {$IFDEF VCL}
     Controls, Graphics, Forms, Dialogs, StdCtrls, ExtCtrls,
     {$ELSE}
     QControls,QTypes, QGraphics, QForms, QDialogs, QStdCtrls, QExtCtrls,
     {$ENDIF}
     ULanguage;

type TNewGame = class(TForm)
         rgPlayers: TRadioGroup;
         btnOK: TButton;
         btnCancel: TButton;
         cbPlayer1: TComboBox;
         cbPlayer2: TComboBox;
         cbPlayer3: TComboBox;
         cbPlayer4: TComboBox;
         procedure rgPlayersClick(Sender: TObject);
         procedure FormHide(Sender: TObject);
         procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
         procedure FormShow(Sender: TObject);
       private
         function GetPlayerNames: string;
       public
         property PlayerNames : string read GetPlayerNames;
       end;

var NewGame: TNewGame;

implementation

{$IFDEF VCL}
  {$R *.dfm}
{$ELSE}
  {$R *.xfm}
{$ENDIF}


procedure TNewGame.FormShow(Sender: TObject);
var s:string;
begin
   with TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')) do
   try
     s:=ReadString('Players','Names','');
     if s<>'' then s:=s+',';
     cbPlayer1.Items.CommaText:=s+'1.'+Language.Translate['Player']+',Computer';
     cbPlayer2.Items.CommaText:=s+'2.'+Language.Translate['Player']+',Computer';
     cbPlayer3.Items.CommaText:=s+'3.'+Language.Translate['Player']+',Computer';
     cbPlayer4.Items.CommaText:=s+'4.'+Language.Translate['Player']+',Computer';
     cbPlayer1.ItemIndex:=0;
   finally
     Free;
   end;
end;

procedure TNewGame.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
   CanClose:=false;
   if cbPlayer1.Text<>'Computer' then CanClose:=true;
   if cbPlayer2.Enabled then if cbPlayer2.Text<>'Computer' then CanClose:=true;
   if cbPlayer3.Enabled then if cbPlayer3.Text<>'Computer' then CanClose:=true;
   if cbPlayer4.Enabled then if cbPlayer4.Text<>'Computer' then CanClose:=true;
   if not CanClose then ShowMessage(Language.Translate['At least one human partner should be specified']);
end;

procedure TNewGame.FormHide(Sender: TObject);
 procedure DoAdd(Value:string);
 var s:string;
 begin
   if (Value<>'') and (Value<>' ') then
   begin
     cbPlayer1.Items.Add(Value);
     cbPlayer2.Items.Add(Value);
     cbPlayer3.Items.Add(Value);
     cbPlayer4.Items.Add(Value);
     with TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')) do
     try
       s:=ReadString('Players','Names','');
       if s<>'' then s:=s+',';
       WriteString('Players','Names',s+Value);
     finally
       Free;
     end;
   end;
 end;
begin
   if cbPlayer1.Items.IndexOf(cbPlayer1.Text)=-1 then DoAdd(cbPlayer1.Text);
   if cbPlayer2.Items.IndexOf(cbPlayer2.Text)=-1 then DoAdd(cbPlayer2.Text);
   if cbPlayer3.Items.IndexOf(cbPlayer3.Text)=-1 then DoAdd(cbPlayer3.Text);
   if cbPlayer4.Items.IndexOf(cbPlayer4.Text)=-1 then DoAdd(cbPlayer4.Text);
end;

procedure TNewGame.rgPlayersClick(Sender: TObject);
begin
   cbPlayer2.Enabled:=rgPlayers.ItemIndex>0;
   cbPlayer3.Enabled:=rgPlayers.ItemIndex>1;
   cbPlayer4.Enabled:=rgPlayers.ItemIndex>2;
   if cbPlayer2.Enabled then begin if (cbPlayer2.Text='') then cbPlayer2.ItemIndex:=0 end
                        else cbPlayer2.ItemIndex:=-1;
   if cbPlayer3.Enabled then begin if (cbPlayer3.Text='') then cbPlayer3.ItemIndex:=0 end
                        else cbPlayer3.ItemIndex:=-1;
   if cbPlayer4.Enabled then begin if (cbPlayer4.Text='') then cbPlayer4.ItemIndex:=0 end
                        else cbPlayer4.ItemIndex:=-1;
end;

function TNewGame.GetPlayerNames: string;
begin
   Result:=cbPlayer1.Text;
   if cbPlayer2.Enabled then Result:=Result+#9+cbPlayer2.Text;
   if cbPlayer3.Enabled then Result:=Result+#9+cbPlayer3.Text;
   if cbPlayer4.Enabled then Result:=Result+#9+cbPlayer4.Text;
end;

end.
