unit MainMenu;
//----------------------------------------------------------------------------------------------------------------------
// Alex 'Shirson' Nevskiy 2012-08
//----------------------------------------------------------------------------------------------------------------------
interface
uses Classes, SysUtils, _scd, _render, _var, LevelSys, GameSys; //, eXgine;

procedure DrawMainMenu;
procedure mMainMenu;

var MenuPart:byte=0;
    MMBackTo: TGameState=gsNone;

implementation
uses HelpScreen;

const
  ScrollDelay=6;
  //MenuCount=3;

var TSL:TStringList;
    TSLMenu:TStringList;
    TMS:TMemoryStream;
    isFirstTime:boolean=true;
    TitleLevel:TLevel;
    TitleLevelW, TitleLevelH:integer;
    dx, dy:integer;
    WorldX, WorldY:integer;
    TitleLevelX, TitleLevelY:integer;
    Timer:integer=0;

    MenuItem:byte=0;
    MouseMenuItem:byte=$ff;
    Bloody:integer=0;


//----------------------------------------------------------------------------------------------------------------------

procedure ShowLevel(GUIX, GUIY, w, h :integer; var Level:TLevel);
var x, y:integer;
begin
  dec(Timer);
  if Timer = 0 then
  begin
    inc(WorldX, dx);
    if (WorldX + GUI_W >= TitleLevelW) or (WorldX <= 0) then dx:=-dx;
    inc(WorldY, dy);
    if (WorldY + GUI_H >= TitleLevelH) or (WorldY <= 0) then dy:=-dy;
    Timer:=ScrollDelay;
  end;

  for x:=0 to w-1 do
    for y:=0 to h-1 do
    begin
      GUIMatrix[x, y].FG:=colDarkGray;
      GUIMatrix[x, y].BG:=colBlack;
      if Level[WorldX + x, WorldY + y].Block=blNone then
        //GUIMatrix[x, y].Ch:=blWall-1
        GUIMatrix[x, y].Ch:=blNone-1
      else
        GUIMatrix[x, y].BG:=colDarkGray;
        //GUIMatrix[x, y].BG:=colBlack
    end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure ShowMenu(GUI_X, GUI_Y:integer);
var FG:integer;
    Idx_Menu:integer;
    s,s1,s2:string;
begin
//  MouseMenuItem:=$ff;
  for Idx_Menu:=0 to TSLMenu.Count-1 do
  begin
    s1:='  '; s2:='  ';
    FG:=colLightGray;
    if Idx_Menu=MenuItem then
      begin
        FG:=colWhite;
        s1:='< '; s2:=' >'
      end;
    s:=s1+TSLMenu[Idx_Menu]+s2;
    PrintString(s, GUI_X, GUI_Y + Idx_Menu*2, FG, ' ');
//    if (GameInput.Mouse.X in [GUI_X..GUI_X+Length(s)]) and (GameInput.Mouse.Y=GUI_Y+ Idx_Menu*2) then
//      MouseMenuItem:= Idx_Menu;
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure CreateTitleLevel;
begin
  TitleLevelW:=64;
  TitleLevelH:=32;
  CreateLevel(TitleLevelW, TitleLevelH, 2, 2, '00000011111', TitleLevel);
  //SaveArr('Test', TitleLevel);
  dx:=1;
  dy:=1;
  TitleLevelX:=0;
  TitleLevelY:=0;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure LoadMenu(Menu:string);
begin
  GetData(Menu, TMS);
  TSLMenu.Clear;
  TSLMenu.LoadFromStream(TMS);
  MenuItem:=0;
  while TSLMenu[MenuItem]='' do
    inc(MenuItem)
end;

//----------------------------------------------------------------------------------------------------------------------

procedure mMainMenu;
begin
  LoadMenu('MainMenu');
  MenuPart:=0;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure mGetDif;
begin
  LoadMenu('DifLevel');
  MenuPart:=1;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure mNewGame(DifLevel:integer);
begin
  isFirstTimeGame:=true;
  GameDifficult:=DifLevel;
  GameState:=gsGame;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure mContinue;
begin
  if MMBackTo=gsNone then exit;
  GMBackTo:=gsMenu;
  GameState:=gsGame;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure mHelp;
begin
  HSBackTo:=gsMenu;
  GameState:=gsHelp;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure mClose;
begin
  CloseGame;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure MenuControl;
var oldMS:integer;
    isSelected:boolean;
begin
  OldMS:=MenuItem;
  isSelected:=false;
  if IsControl(keyUp) and (MenuItem > 0) and (TSLMenu[MenuItem-1]<>'') then dec(MenuItem);
  if IsControl(keyDown) and (MenuItem < TSLMenu.Count-1) then inc(MenuItem);
  if IsControl(keyEscape) and (MenuPart=1) then mMainMenu;

//  if IsControl(keyLMB) and (MouseMenuItem<>$FF) and (MouseDelay=0) then
//  begin
//    MouseDelay:=cDelay;
//    if MenuItem<>MouseMenuItem then
//      MenuItem:=MouseMenuItem
//    else
//      isSelected:=True;
//  end;
  if IsControl(keyEnter) and (MouseDelay=0) then
    begin
      isSelected:=true;
      MouseDelay:=cDelay;
    end;
  if (OldMS<>MenuItem) or isSelected then PlaySnd(s_Select, 0);
  if MouseDelay>0 then dec(MouseDelay);

  if isSelected then
    if MenuPart=0 then
      case MenuItem of
        0:;
        1: mGetDif;
        2: mContinue;
        3: mHelp;
        4: mClose;
      end
    else
      case MenuItem of
        0..3: mNewGame(MenuItem);
           4: mMainMenu;
      end;

//  case MenuState of
//    0: mNewGame;
//    1: ;
//    2: mClose;
//  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure DrawMainMenu;
var Idx:integer;
    Ns:integer;
begin
  if isFirstTime then
  begin
    isFirstTime:=false;
    GetData('Title', TMS);
    TSL.LoadFromStream(TMS);
    LoadMenu('MainMenu');
    CreateTitleLevel;
    timer:=ScrollDelay;
  end;

  MenuControl;
  ShowLevel(0, 0, GUI_W, GUI_H, TitleLevel);
  Ns:=0;
  case Bloody of
     0: Bloody:= random(45)+10;
  1..4: Ns:=12;
     5: PlaySnd(s_Distort, 0);
  end;
  if Bloody>0 then  dec(Bloody);

  for Idx:=0 to 11 do
    //PrintString(stringreplace(TSL[Idx],'#',chr(blWall-1),[rfReplaceAll, rfIgnoreCase]) , 0, Idx, colLightGray, ' ');
    PrintColoredString(stringreplace(TSL[Idx+Ns],'#',chr(blWall-1),[rfReplaceAll, rfIgnoreCase]),0 , Idx+1, ' ');

  ShowMenu(53, 12);

  PrintString('(c) Alexander "Shirson" Nevskiy 2012', 2, 23, colLightGray, ' ');
end;

//----------------------------------------------------------------------------------------------------------------------

initialization
  TSL:=TStringList.Create;
  TSLMenu:=TStringList.Create;
  TMS:=TMemoryStream.Create;

//----------------------------------------------------------------------------------------------------------------------

finalization
 TSL.Free;
 TSLMenu.Free;
 TMS.Free;

//----------------------------------------------------------------------------------------------------------------------

end.
