unit SConfig;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, DXPlay, DirectX, Spin;

type
  TConfigForm = class(TForm)
    ProviderSetting: TNotebook;
    HostButton: TButton;
    TCPIPHostName: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    ModemPhoneNumber: TEdit;
    Label7: TLabel;
    ModemComboBox: TComboBox;
    TCPIPConnectButton: TButton;
    ModemConnectButton: TButton;
    OtherConnectButton: TButton;
    Label8: TLabel;
    TCPIPPort: TSpinEdit;
    GroupBox1: TGroupBox;
    ProviderList: TComboBox;
    GroupBox2: TGroupBox;
    SessionListBox: TListBox;
    GroupBox3: TGroupBox;
    SessionNameEdit: TEdit;
    GroupBox4: TGroupBox;
    PlayerNameEdit: TEdit;
    procedure FormShow(Sender: TObject);
    procedure ProviderListChange(Sender: TObject);
    procedure TCPIPConnectButtonClick(Sender: TObject);
    procedure OtherConnectButtonClick(Sender: TObject);
    procedure HostButtonClick(Sender: TObject);
    procedure SessionListBoxClick(Sender: TObject);
    procedure ModemConnectButtonClick(Sender: TObject);
  private
    procedure Connect;
  public
    DXPlay: TDXPlay;
  end;

var
  ConfigForm: TConfigForm;

implementation

uses SComFunc;

{$R *.DFM}

procedure TConfigForm.FormShow(Sender: TObject);
begin
  ProviderList.Items := DXPlay.Providers;
  TCPIPHostName.Text := TheGameHostIs;
  TCPIPPort.Value := TheGamePortIs;
  SessionNameEdit.Text := TheGameNameIs;
  PlayerNameEdit.Text := MyPlayerShortNameIs;
  ProviderList.ItemIndex := 0;
  ProviderListChange(nil);
end;

procedure TConfigForm.ProviderListChange(Sender: TObject);
begin
  SessionListBox.Items.Clear;

  DXPlay.ProviderName := '';
  HostButton.Enabled := False;

  SessionListBox.Enabled := False;
  SessionListBox.Color := clBtnFace;
  SessionNameEdit.Enabled := False;
  SessionNameEdit.Color := clBtnFace;
  PlayerNameEdit.Enabled := False;
  PlayerNameEdit.Color := clBtnFace;

  if CompareMem(PGUID(ProviderList.Items.Objects[ProviderList.ItemIndex]), @DPSPGUID_TCPIP, SizeOf(TGUID)) then
  begin
    {  TCP/IP  }
    ProviderSetting.ActivePage := 'ProviderSettingTCPIP';
  end else
    if CompareMem(PGUID(ProviderList.Items.Objects[ProviderList.ItemIndex]), @DPSPGUID_MODEM, SizeOf(TGUID)) then
    begin
    {  Modem  }
      ProviderSetting.ActivePage := 'ProviderSettingModem';
      if ModemComboBox.Items.Count = 0 then
      begin
        Screen.Cursor := crHourGlass;
        try
          ModemComboBox.Items := DXPlay.ModemSetting.ModemNames;
          ModemComboBox.ItemIndex := 0;
        finally
          Screen.Cursor := crDefault;
        end;
      end;
    end else
    begin
    {  Other  }
      ProviderSetting.ActivePage := 'Default';
    end;
end;

procedure TConfigForm.Connect;
begin
  Screen.Cursor := crHourGlass;
  try
    DXPlay.GetSessions;
    SessionListBox.Items := DXPlay.Sessions;
  finally
    Screen.Cursor := crDefault;
  end;

  HostButton.Enabled := True;
  SessionNameEdit.Enabled := True;
  SessionNameEdit.Color := clWhite;
  PlayerNameEdit.Enabled := True;
  PlayerNameEdit.Color := clWhite;
end;

procedure TConfigForm.TCPIPConnectButtonClick(Sender: TObject);
begin
  SessionListBox.Items.Clear;

  DXPlay.TCPIPSetting.HostName := TCPIPHostName.Text;
  DXPlay.TCPIPSetting.Port := TCPIPPort.Value;
  DXPlay.TCPIPSetting.Enabled := True;
  DXPlay.ProviderName := ProviderList.Items[ProviderList.ItemIndex];

  Connect;
end;

procedure TConfigForm.ModemConnectButtonClick(Sender: TObject);
begin
  SessionListBox.Items.Clear;

  DXPlay.ModemSetting.PhoneNumber := ModemPhoneNumber.Text;
  DXPlay.ModemSetting.ModemName := ModemComboBox.Items[ModemComboBox.ItemIndex];
  DXPlay.ModemSetting.Enabled := True;
  DXPlay.ProviderName := ProviderList.Items[ProviderList.ItemIndex];

  Connect;
end;

procedure TConfigForm.OtherConnectButtonClick(Sender: TObject);
begin
  SessionListBox.Items.Clear;

  DXPlay.ProviderName := ProviderList.Items[ProviderList.ItemIndex];

  Connect;
end;

procedure TConfigForm.HostButtonClick(Sender: TObject);
begin
  ServerIsUp := True;
  MyPlayerShortNameIs := trim(PlayerNameEdit.Text);
  DXPlay.Open2(True, SessionNameEdit.Text, MyPlayerShortNameIs);
  Tag := 1;
  Close;
end;

procedure TConfigForm.SessionListBoxClick(Sender: TObject);
begin
  SessionNameEdit.Text := SessionListBox.Items[SessionListBox.ItemIndex];
end;

end.

