unit UMainMenu;

interface     

uses Controls,Classes,GLGameMenu;

procedure MouseDownMenu(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);  
function PosInMenu(X,Y:Integer; Menu:TGLGameMenu):Boolean;
procedure SwitchToMenu(Menu:TGLGameMenu);  
procedure MenuSelect2(Menu:TGLGameMenu; X,Y:Integer);

implementation

uses Unit1,SysUtils,Dialogs,UNetwork,UIni,UMessageThread,Forms,UMyUtils;    

procedure MenuSelect2(Menu:TGLGameMenu; X,Y:Integer);
begin
  Y:=Y+26;
  if (X >= Menu.BoxLeft)  and (Y >= Menu.MenuTop) and
     (X <= Menu.BoxRight) and (Y <= Menu.BoxBottom) then
  begin
    Menu.Selected := (Y - Menu.MenuTop)
    div (Menu.Font.CharHeight + Menu.Spacing);
  end;
end;
       
procedure SwitchToMenu(Menu:TGLGameMenu);
var C1:Integer;
begin
  for C1:=0 to Form1.Menu.Count-1 do
  Form1.Menu.Children[C1].Visible:=false;

  if not Assigned(Menu) then ShowMessage('error 27: menu not assigned')
  else 
  Menu.Visible:=true;
end;
                                
function PosInMenu(X,Y:Integer; Menu:TGLGameMenu):Boolean;
begin    
  Y:=Y+26;
  Result:=(X>=Menu.BoxLeft)
  and (Y>=Menu.MenuTop)
  and (X<=Menu.BoxRight)
  and (Y<=Menu.BoxBottom)
end;


procedure MouseDownMenu(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var C1:Integer; C2:Char;
begin
  if (Button<>TMouseButton(0))
  and (Button<>TMouseButton(3)) then Exit;

  
  if Form1.MainMenu.Visible
  and (PosInMenu(X,Y,Form1.MainMenu)
  or (Button=TMouseButton(3))) then begin
    if Form1.MainMenu.Selected=0 then begin
      C2:='p';
      Form1.FormKeyPress(Sender,C2);
    end;
    if Form1.MainMenu.Selected=1 then begin
      C2:='n';
      Form1.FormKeyPress(Sender,C2);
    end;
    if Form1.MainMenu.Selected=2 then begin
      SwitchToMenu(Form1.SettingsMenu);
    end;
    if Form1.MainMenu.Selected=3 then begin
      SwitchToMenu(Form1.MPMenu);
    end;
    if Form1.MainMenu.Selected=4 then begin
      C2:='r';
      Form1.FormKeyPress(Sender,C2);
    end;
    if Form1.MainMenu.Selected=5 then begin
      C2:='b';
      Form1.FormKeyPress(Sender,C2);
    end;
    Exit;
  end;


  if Form1.MPMenu.Visible
  and (PosInMenu(X,Y,Form1.MPMenu)
  or (Button=TMouseButton(3))) then begin
    if Form1.MPMenu.Selected=0 then
    SwitchToMenu(Form1.MPHostMenu);
    if Form1.MPMenu.Selected=1 then
    SwitchToMenu(Form1.MPJoinMenu);  
    if Form1.MPMenu.Selected=2 then
    SwitchToMenu(Form1.MainMenu);
    Exit;
  end;


  if Form1.MPHostMenu.Visible
  and (PosInMenu(X,Y,Form1.MPHostMenu)
  or (Button=TMouseButton(3))) then begin
    if Form1.MPHostMenu.Selected=0 then begin
      MyInputBox(
      'Multiplayer-Port',
      'Port (1024-49151):',
      IntToStr(MPPort),
      1025,
      49150,
      MPPort);
      Form3.ClientSocket.Port:=MPPort;
      Form3.ServerSocket.Port:=MPPort;
    end;
    if Form1.MPHostMenu.Selected=1 then begin
      if Form3.ServerSocket.Active then
      MessageOut('Server is already open!','Invalid Command')
      else begin
        if Form3.ClientSocket.Active then begin
          if (MessageDlg('Client socket will be closed. Continue?', mtWarning,
          [mbYes, mbNo], 0) = mrYes) then begin
            Form3.ClientSocket.Close;
            Form3.ServerSocket.Open;
          end;
        end else Form3.ServerSocket.Open;
        MessageOut('Server is open to join!','Server open');
      end;
    end;
    if Form1.MPHostMenu.Selected=2 then
    SwitchToMenu(Form1.MPMenu);
    Exit;
  end;


  if Form1.MPJoinMenu.Visible
  and (PosInMenu(X,Y,Form1.MPJoinMenu)
  or (Button=TMouseButton(3))) then begin
    if Form1.MPJoinMenu.Selected=0 then begin
      MyInputBox(
      'Server-IP',
      'Servername/IP-Adress:',
      MPServerAddress,
      '',
      MPServerAddress);
      ini.WriteString('Multiplayer','MPServerAddress',MPServerAddress);
      Form3.ClientSocket.Host:=MPServerAddress;
    end;
    if Form1.MPJoinMenu.Selected=1 then begin
      MyInputBox(
      'Multiplayer-Port',
      'Port (1024-49151):',
      IntToStr(MPPort),
      1025,
      49150,
      MPPort);
      Form3.ClientSocket.Port:=MPPort;
      Form3.ServerSocket.Port:=MPPort;
    end;
    if Form1.MPJoinMenu.Selected=2 then begin
      if Form3.ClientSocket.Active then
      MessageOut('You are already connected to a game!','Invalid Command')
      else begin
        if Form3.ServerSocket.Active then begin
          if (MessageDlg('Server socket will be closed. Continue?', mtWarning,
          [mbYes, mbNo], 0) = mrYes) then begin
            Form3.ServerSocket.Close;
            Form3.ClientSocket.Open;
          end;
        end else Form3.ClientSocket.Open;
      end;
    end;
    if Form1.MPJoinMenu.Selected=3 then
    SwitchToMenu(Form1.MPMenu);
    Exit;
  end;


  if Form1.SettingsMenu.Visible
  and (PosInMenu(X,Y,Form1.SettingsMenu)
  or (Button=TMouseButton(3))) then begin
//    if Form1.SettingsMenu.Selected=0 then
//    SwitchToMenu(Form1.DiffMenu);
    if Form1.SettingsMenu.Selected=0 then
    SwitchToMenu(Form1.VideoMenu);
    if Form1.SettingsMenu.Selected=1 then
    SwitchToMenu(Form1.MainMenu);
    Exit;
  end;

  if Form1.VideoMenu.Visible
  and (PosInMenu(X,Y,Form1.VideoMenu)
  or (Button=TMouseButton(3))) then begin
    if Form1.VideoMenu.Selected=0 then begin
      C2:='m';
      Form1.FormKeyPress(Sender,C2);
    end;
    if Form1.VideoMenu.Selected=1 then begin
      MessageOut('Resize yourself by dragging the window`s borders.',
      'You lazy ass!');
    end;
    if Form1.VideoMenu.Selected=2 then begin
      MyInputBox(
      'Shading Distance',
      'Distance:',
      IntToStr(ShaderWidth),
      0,
      RaumRange,
      ShaderWidth);
      Form1.ReloadWallShaders;
    end;
    if Form1.VideoMenu.Selected=3 then
    SwitchToMenu(Form1.SettingsMenu);
    Exit;
  end;
end;

end.
