{*************************************************************************}
{                                                                         }
{ MusicUnit.pas - Music Editor window                                     }
{                                                                         }
{ This file is a part of the project:                                     }
{   Dizzy Quest Editor - the quest editor for Dizzy-like games            }
{                                                                         }
{ © Parfenov Alexey Andreevich, 2007                                      }
{                                                                         }
{ e-mail: AlkatrazStudio@yandex.ru                                        }
{                                                                         }
{ This program is free software; you can redistribute it and/or modify    }
{ it under the terms of the GNU General Public License as published by    }
{ the Free Software Foundation; either version 2 of the License, or       }
{ (at your option) any later version.                                     }
{                                                                         }
{ This program is distributed in the hope that it will be useful,         }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of          }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            }
{ GNU General Public License for more details.                            }
{                                                                         }
{ You may read GNU General Public License at:                             }
{   http://www.gnu.org/copyleft/gpl.html                                  }
{                                                                         }
{*************************************************************************}
unit MusicUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls,
  dqe2_types,alkatraz,gameplayer,gameplayervisual, Buttons, Menus;

type
  TForm_Music = class(TForm)
    Button3: TButton;
    GroupBox1: TGroupBox;
    Button1: TButton;
    Button2: TButton;
    Button4: TButton;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    GroupBox2: TGroupBox;
    Button5: TButton;
    Button6: TButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    ListView1: TListView;
    SpeedButton7: TSpeedButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    ListView2: TListView;
    CheckBox2: TCheckBox;
    ComboBox1: TComboBox;
    Button7: TButton;
    procedure Button6Click(Sender: TObject);
    procedure ListView1Change(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure FormCreate(Sender: TObject);
    procedure OnPlayEnd(Sender:TObject);
    procedure FormShow(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
    procedure SpeedButton7Click(Sender: TObject);
    procedure SpeedButton6Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure UpdateMusicView;
    procedure UpdateSoundView;
    procedure Button1Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure ChangeGlyph(Sender:TSpeedButton;NewTag:integer);
    procedure Localize;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form_Music: TForm_Music;
  can:boolean=true;
  CurMusic:TGPMusic;

implementation

uses EditSoundFileUnit, MainUnit;

{$R *.dfm}

var lng:array of string;

procedure TForm_Music.Button1Click(Sender: TObject);
begin
  World.MusicPlayer.Stop;
  Form_EditSoundFile.ShowIt(true);
  if Form_EditSoundFile.ok then
    if World.MusicPlayer.PlayList.AddFile(World.SharedDir+'\Music\'+
      Form_EditSoundFile.ListBox1.Items[Form_EditSoundFile.ListBox1.itemindex]) then
        UpdateMusicView;
end;

procedure TForm_Music.Button2Click(Sender: TObject);
var
  n:integer;
begin
  if ListView2.Selected<>nil then
  begin
    n:=ListView2.Selected.Index;
    if MessageDlgRus(lng[22],
      lng[23]+#13#10+ListView2.Items.Item[n].SubItems[0],
      MB_OKCANCEL or MB_ICONWARNING)=mrok then
    begin
      world.MusicPlayer.PlayList.Delete(n);
      World.Stop;
      UpdateMusicView;
    end;
  end;
end;

procedure TForm_Music.Button3Click(Sender: TObject);
begin
  close;
end;

procedure TForm_Music.Button4Click(Sender: TObject);
var
  p:tpoint;
begin
  p:=Mouse.CursorPos;
  PopupMenu1.Popup(p.x,p.Y);
end;

procedure TForm_Music.Button5Click(Sender: TObject);
var
  m:TGPMusic;
begin
  World.MusicPlayer.Stop;
  Form_EditSoundFile.ShowIt(false);
  if Form_EditSoundFile.ok then
  begin
    m:=TGPMusic.Create;
    if m.Open(World.SharedDir+'\Music\'+Form_EditSoundFile.ListBox1.Items[Form_EditSoundFile.ListBox1.itemindex]) then
    begin
      UpdateSoundView ;
      m.OnPlayEnd:=OnPlayEnd;
    end
    else
      m.Free;
  end;
end;

procedure TForm_Music.Button6Click(Sender: TObject);
var
  n:integer;

  procedure ShowErr(s:string);
  begin
    MessageDlgRus(lng[24],Format(lng[25],[s])+#13#10+Format(lng[26],[s]),MB_OK or MB_ICONWARNING);
  end;

begin
  if ListView1.Selected<>nil then
  begin
    n:=ListView1.Selected.Index;
    if World.MusicPlayer.Musics[n+1]=World.SoundWalk then ShowErr(lng[27])else
    if World.MusicPlayer.Musics[n+1]=World.SoundJump then ShowErr(lng[28])else
    if World.MusicPlayer.Musics[n+1]=World.SoundWound then ShowErr(lng[29])else
    if World.MusicPlayer.Musics[n+1]=World.SoundDie then ShowErr(lng[30])else
    if MessageDlgRus(lng[31],
      lng[32]+#13#10+ListView1.Items.Item[n].SubItems[0],
      MB_OKCANCEL or MB_ICONWARNING)=mrok then
    begin
      world.MusicPlayer.Musics[n+1].Free;
      World.Stop;
      UpdateSoundView;
    end;
  end;
end;

procedure TForm_Music.Button7Click(Sender: TObject);
begin
  if ListView1.Selected<>nil then
  begin
    World.MusicPlayer.Stop;
    Form_EditSoundFile.ShowIt(false);
    if Form_EditSoundFile.ok then
      if World.MusicPlayer.Musics[ListView1.Selected.Index+1].Open('Music\'+Form_EditSoundFile.ListBox1.Items[Form_EditSoundFile.ListBox1.itemindex]) then
        UpdateSoundView;
  end;
end;

procedure TForm_Music.ChangeGlyph(Sender: TSpeedButton; NewTag: integer);
var
  b:TBitmap;
begin
  if Sender.Tag<>NewTag then
  begin
    b:=TBitmap.Create;
    b.Canvas.Brush.Color:=Sender.Glyph.Canvas.Pixels[0,0];
    b.Canvas.FillRect(b.Canvas.ClipRect);
    if NewTag=0 then
      Form_Main_DQE.ImageList1.GetBitmap(39,b)
    else
      Form_Main_DQE.ImageList1.GetBitmap(40,b);
    Sender.Glyph.Assign(b);
    Sender.Tag:=NewTag;
    b.Free;
  end;
end;

procedure TForm_Music.CheckBox2Click(Sender: TObject);
begin
  world.MusicPlayer.Music.Tag:=integer(CheckBox2.Checked);
end;

procedure TForm_Music.ComboBox1Change(Sender: TObject);
begin
  case ComboBox1.ItemIndex of
    0:world.MusicPlayer.PlayList.Mode:=pmDisabled;
    1:world.MusicPlayer.PlayList.Mode:=pmSequence;
    2:world.MusicPlayer.PlayList.Mode:=pmLoopAll;
    3:world.MusicPlayer.PlayList.Mode:=pmLoopOne;
    4:world.MusicPlayer.PlayList.Mode:=pmShuffle;
  else world.MusicPlayer.PlayList.Mode:=pmDisabled;
  end;
end;

procedure TForm_Music.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if SpeedButton3.Tag=1 then SpeedButton3.Click;
  if SpeedButton6.Tag=1 then SpeedButton6.Click;
  World.MusicPlayer.Stop;
end;

procedure TForm_Music.FormCreate(Sender: TObject);
begin
  world.MusicPlayer.Music.OnPlayEnd:=OnPlayEnd;
  Form_Main_DQE.ImageList1.GetBitmap(35,SpeedButton1.Glyph);
  Form_Main_DQE.ImageList1.GetBitmap(36,SpeedButton2.Glyph);
  Form_Main_DQE.ImageList1.GetBitmap(38,SpeedButton4.Glyph);
  Form_Main_DQE.ImageList1.GetBitmap(38,SpeedButton5.Glyph);
  Form_Main_DQE.ImageList1.GetBitmap(37,SpeedButton7.Glyph);
  ChangeGlyph(SpeedButton3,0);
  ChangeGlyph(SpeedButton6,0);
  SetLength(lng,33);
end;

procedure TForm_Music.FormShow(Sender: TObject);
begin
  CheckBox2.Checked:=boolean(world.MusicPlayer.Music.Tag);
  ComboBox1.ItemIndex:=integer(world.MusicPlayer.PlayList.Mode);
end;

procedure TForm_Music.ListView1Change(Sender: TObject; Item: TListItem;
  Change: TItemChange);
var
  a:integer;
begin
  if Application.Terminated then exit;
  for a:=1 to world.MusicPlayer.MusicCount-1 do
    world.MusicPlayer.Musics[a].Stop;
  ChangeGlyph(SpeedButton6,0);
end;

procedure TForm_Music.Localize;
var
  n:integer;
begin
  LoadStringsFromIni(MainUnit.lang_ini,Name,lng);
  Caption:=lng[0];
  GroupBox1.Caption:=lng[1];
  n:=ComboBox1.ItemIndex;
  ComboBox1.Items[0]:=lng[2];
  ComboBox1.Items[1]:=lng[3];
  ComboBox1.Items[2]:=lng[4];
  ComboBox1.Items[3]:=lng[5];
  ComboBox1.Items[4]:=lng[6];
  ComboBox1.ItemIndex:=n;
  CheckBox2.Caption:=lng[7];
  ListView2.Columns[1].Caption:=lng[8];
  ListView2.Columns[2].Caption:=lng[9];
  Button1.Caption:=lng[10];
  Button2.Caption:=lng[11];
  Button4.Caption:=lng[12];
  N1.Caption:=lng[13];
  N2.Caption:=lng[14];
  N3.Caption:=lng[15];
  N4.Caption:=lng[16];
  N5.Caption:=lng[17];
  GroupBox2.Caption:=lng[18];
  ListView1.Columns[1].Caption:=lng[19];
  ListView1.Columns[2].Caption:=lng[20];
  Button5.Caption:=lng[10];
  Button7.Caption:=lng[21];
  Button6.Caption:=lng[11];
end;

procedure TForm_Music.N1Click(Sender: TObject);
begin
  world.MusicPlayer.PlayList.Sort(smTitle,false);
  UpdateMusicView;
end;

procedure TForm_Music.N2Click(Sender: TObject);
begin
  world.MusicPlayer.PlayList.Sort(smFileName,false);
  UpdateMusicView;
end;

procedure TForm_Music.N3Click(Sender: TObject);
begin
  world.MusicPlayer.PlayList.Sort(smShuffle,false);
  UpdateMusicView;
end;

procedure TForm_Music.N4Click(Sender: TObject);
begin
  world.MusicPlayer.PlayList.Sort(smTitle,true);
  UpdateMusicView;
end;

procedure TForm_Music.N5Click(Sender: TObject);
begin
  world.MusicPlayer.PlayList.Sort(smFileName,true);
  UpdateMusicView;
end;

procedure TForm_Music.OnPlayEnd(Sender:TObject);
begin
  if (Sender as TGPMusic).isSound then
  begin
    ChangeGlyph(SpeedButton6,0);
  end
  else
  begin
    ChangeGlyph(SpeedButton3,0);
  end;
end;

procedure TForm_Music.SpeedButton1Click(Sender: TObject);
var
  x:TListItem;
  n:integer;
begin
  x:=ListView2.Selected;
  if x<>nil then
  begin
    n:=x.Index;
    if x.Index>0 then
    begin
      world.MusicPlayer.PlayList.Exchange(n,n-1);
      UpdateMusicView;
      ListView2.Items[n-1].Selected:=true;
      ListView2.SetFocus;
    end;
  end;
end;

procedure TForm_Music.SpeedButton2Click(Sender: TObject);
var
  x:TListItem;
  n:integer;
begin
  x:=ListView2.Selected;
  if x<>nil then
  begin
    n:=x.Index;
    if n<(world.MusicPlayer.PlayList.Count-1) then
    begin
      world.MusicPlayer.PlayList.Exchange(n,n+1);
      UpdateMusicView;
      ListView2.Items[n+1].Selected:=true;
      ListView2.SetFocus;
    end;
  end;
end;

procedure TForm_Music.SpeedButton3Click(Sender: TObject);
begin
  if SpeedButton3.Tag=0 then
  begin
    if ListView2.Selected<>nil then
    begin
      world.MusicPlayer.Music.Open(world.MusicPlayer.PlayList.Files[ListView2.Selected.Index]);
      world.MusicPlayer.Music.Play;
      ChangeGlyph(SpeedButton3,1);
    end
  end
  else
  begin
    world.MusicPlayer.Music.Close;
    ChangeGlyph(SpeedButton3,0);
  end;
end;

procedure TForm_Music.SpeedButton4Click(Sender: TObject);
begin
  if ListView2.Selected<>nil then
  begin
    ChangeGlyph(SpeedButton3,0);
    world.MusicPlayer.Music.Open(world.MusicPlayer.PlayList.Files[ListView2.Selected.index]);
    ShowGPVisualForm(world.MusicPlayer.Music);
    world.MusicPlayer.Music.Stop;
  end;
end;

procedure TForm_Music.SpeedButton5Click(Sender: TObject);
begin
  if listview1.Selected<>nil then
  begin
    if CurMusic<>nil then SpeedButton6.Click;
    CurMusic:=world.MusicPlayer.Musics[Listview1.selected.index+1];
    ChangeGlyph(SpeedButton6,0);
    ShowGPVisualForm(CurMusic);
    CurMusic.Stop;
  end;
end;

procedure TForm_Music.SpeedButton6Click(Sender: TObject);
begin
  if curmusic<>nil then CurMusic.Stop;
  if SpeedButton6.Tag=0 then
  begin
    if ListView1.Selected=nil then exit;
    CurMusic:=world.MusicPlayer.Musics[Listview1.selected.index+1];
    CurMusic.Play;
    ChangeGlyph(SpeedButton6,1);
  end
  else
  begin
    ChangeGlyph(SpeedButton6,0);
    CurMusic:=nil;
  end;
end;

procedure TForm_Music.SpeedButton7Click(Sender: TObject);
var
  tmp:TGPMusic;
  x:TListItem;
begin
  x:=ListView1.Selected;
  if x<>nil then
  begin
    tmp:=world.MusicPlayer.Musics[x.Index+1];
    if tmp.Tag=0 then tmp.Tag:=1 else tmp.Tag:=0;
    UpdateSoundView;
    x.Selected:=true;
    ListView1.SetFocus;
  end;
end;

procedure TForm_Music.UpdateMusicView;
var
  a:integer;
  i:TListItem;
begin
  ListView2.Clear;
  for a:=0 to world.MusicPlayer.PlayList.Count-1 do
  begin
    i:=ListView2.Items.Add;
    i.Caption:=inttostr(a+1);
    i.SubItems.Add(ExtractFileName(world.MusicPlayer.PlayList.Files[a]));
    i.SubItems.Add(world.MusicPlayer.PlayList.Titles[a]);
  end;
  world.MusicPlayer.Music.OnPlayEnd:=OnPlayEnd;
end;

procedure TForm_Music.UpdateSoundView;
var
  a:integer;
  i:TListItem;
begin
  ListView1.Clear;
  for a:=1 to world.MusicPlayer.MusicCount-1 do
  begin
    i:=ListView1.Items.Add;
    i.Caption:=inttostr(a);
    i.SubItems.Add(ExtractFileName(world.MusicPlayer.Musics[a].FileName));
    world.MusicPlayer.Musics[a].OnPlayEnd:=OnPlayEnd;
    if world.MusicPlayer.Musics[a].Tag=1 then
      i.SubItems.Add('+')
    else
      i.SubItems.Add('-');
  end;
end;

end.
