{*************************************************************************}
{                                                                         }
{ FontUnit.pas - Font 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 FontUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,dqe2_types, ComCtrls, StdCtrls, asphyrefontsalt, ExtCtrls,
  AsphyreSubsc, AsphyreDevices, Asphyre2D, AsphyreCanvas, AsphyreTimers, Spin,
  AsphyreDef,alkatraz;

type
  TForm_Fonts = class(TForm)
    GroupBox1: TGroupBox;
    Button1: TButton;
    Button2: TButton;
    ListView1: TListView;
    GroupBox2: TGroupBox;
    Button3: TButton;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    ColorBox1: TColorBox;
    FontDialog1: TFontDialog;
    GroupBox3: TGroupBox;
    Label3: TLabel;
    Button4: TButton;
    CheckBox1: TCheckBox;
    Button5: TButton;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    GroupBox4: TGroupBox;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    ColorBox2: TColorBox;
    CheckBox4: TCheckBox;
    GroupBox5: TGroupBox;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    ColorBox3: TColorBox;
    SpinEdit3: TSpinEdit;
    SpinEdit4: TSpinEdit;
    SpinEdit2: TSpinEdit;
    SpinEdit1: TSpinEdit;
    Timer1: TTimer;
    Label10: TLabel;
    procedure Timer1Timer(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ListView1Edited(Sender: TObject; Item: TListItem; var S: string);
    procedure ListView1Change(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure UpdateLabel;
    procedure UpdateForm;
    procedure UpdateObject;
    procedure UpdateList;
    procedure ColorBox1Change(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Localize;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form_Fonts: TForm_Fonts;
  CurFont:DQE2_Font=nil;
  CurX,CurY:Integer;
  CurCol:Cardinal;
  can:boolean=true;
  tmpFont:TFont=nil;
  cnt:integer;

implementation

uses MainUnit;

{$R *.dfm}

var lng:array of string;

procedure TForm_Fonts.Button1Click(Sender: TObject);
var
  s:string;
begin
  s:='';
  if InputQuery(lng[47],lng[48],s) then
  begin
    can:=false;
    UpdateObject;
    CurFont:=DQE2_Font.Create(world.Fonts);
    CurFont.Name:=s;
    ListView1.AddItem(CurFont.Name,CurFont);
    UpdateForm;
    can:=true;
  end;
end;

procedure TForm_Fonts.Button2Click(Sender: TObject);

procedure SystemDel(s:string);
begin
  MessageDlgRus(lng[49],
  Format(lng[50],[CurFont.Name,s])+#13#10+
  Format(lng[51],[s]),MB_OK or MB_ICONWARNING);
end;

begin
  if CurFont<>nil then
  begin
    if CurFont=World.FontAnswer then SystemDel(lng[52]) else
    if CurFont=World.FontPersonName then SystemDel(lng[53]) else
    if CurFont=World.FontQuestion then SystemDel(lng[54]) else
    if CurFont=World.FontItemName then SystemDel(lng[55]) else
    if CurFont=World.FontItemText then SystemDel(lng[56]) else
    if MessageDlgRus(lng[57],lng[58]+#13#10+CurFont.Name,MB_ICONEXCLAMATION or MB_OKCANCEL)=mrok then
    begin
      can:=false;
      World.Stop;
      CurFont.Free;CurFont:=nil;
      ListView1.Selected.Data:=nil;
      ListView1.Selected.Delete;
      ListView1.Selected:=nil;
      UpdateForm;
      can:=true;
    end;
  end;
end;

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

procedure TForm_Fonts.Button4Click(Sender: TObject);
begin
  FontDialog1.Font.Size:=CurFont.Font.Size;
  FontDialog1.Font.Name:=CurFont.Font.FontName;
  FontDialog1.Font.Charset:=CurFont.Font.CharSet;
  FontDialog1.Font.Style:=[];
  if feBold in CurFont.Font.Style then
    FontDialog1.Font.Style:=FontDialog1.Font.Style+[fsBold];
  if feItalic in CurFont.Font.Style then
    FontDialog1.Font.Style:=FontDialog1.Font.Style+[fsItalic];
  if FontDialog1.Execute then
  begin
    CurFont.Font.Size:=FontDialog1.Font.Size;
    CurFont.Font.FontName:=FontDialog1.Font.Name;
    CurFont.Font.CharSet:=FontDialog1.Font.Charset;
    CurFont.Font.Style:=[];
    if fsBold in FontDialog1.Font.Style then
      CurFont.Font.Style:=CurFont.Font.Style+[feBold];
    if fsItalic in FontDialog1.Font.Style then
      CurFont.Font.Style:=CurFont.Font.Style+[feItalic];
    UpdateObject;
    UpdateLabel;
  end;
end;

procedure TForm_Fonts.Button5Click(Sender: TObject);
begin
  if CurFont<>nil then
    UpdateObject;
end;

procedure TForm_Fonts.ColorBox1Change(Sender: TObject);
begin
  CurCol:=ColorBox1.Selected or $FF000000;
end;

procedure TForm_Fonts.FormCreate(Sender: TObject);
begin
  cnt:=0;
  SetLength(lng,60);
end;

procedure TForm_Fonts.FormShow(Sender: TObject);
begin
  UpdateForm;
end;

procedure TForm_Fonts.ListView1Change(Sender: TObject; Item: TListItem;
  Change: TItemChange);
begin
  if Application.Terminated then exit;
  if not can then exit;
  UpdateObject;
  UpdateForm;
end;

procedure TForm_Fonts.ListView1Edited(Sender: TObject; Item: TListItem;
  var S: string);
begin
  CurFont.Name:=S;s:=CurFont.Name;
end;

procedure TForm_Fonts.Localize;
begin
  LoadStringsFromIni(MainUnit.lang_ini,Name,lng);
  Caption:=lng[0];
  GroupBox1.Caption:=lng[1];
  GroupBox2.Caption:=lng[2];
  Label1.Caption:=lng[3];
  Label2.Caption:=lng[4];
  CheckBox1.Caption:=lng[5];
  Button5.Caption:=lng[6];
  GroupBox3.Caption:=lng[7];
  Button4.Caption:=lng[36];
  CheckBox2.Caption:=lng[37];
  CheckBox3.Caption:=lng[38];
  GroupBox4.Caption:=lng[39];
  CheckBox4.Caption:=lng[40];
  GroupBox5.Caption:=lng[41];
  Label4.Caption:=lng[42];Label5.Caption:=lng[43];Label6.Caption:=lng[44];
  Label7.Caption:=lng[42];Label8.Caption:=lng[43];Label9.Caption:=lng[44];
  Button1.Caption:=lng[45];
  Button2.Caption:=lng[46];
  Label10.Caption:=lng[59];
end;

procedure TForm_Fonts.Timer1Timer(Sender: TObject);
begin
  if not can then exit;
  if(Visible)And(CurFont<>nil)then
  begin
    if CheckBox1.Checked then
    begin
      inc(cnt);
      if cnt>20 then
      begin
        cnt:=0;
        UpdateObject;
        try
          CurFont.Font.Update;
        except
        end;
      end;
    end;
    world.Canvas.PutPixel(0,0,0,0);
    Label10.Visible:=not CurFont.TextOut(0,0,edit1.Text,CurCol);
  end;
end;

procedure TForm_Fonts.UpdateForm;
begin
  if ListView1.Selected=nil then
  begin
    GroupBox2.Visible:=False;
    GroupBox3.Visible:=False;
    Button2.Enabled:=false;
    CurFont:=nil;
  end
  else
  begin
    CurFont:=DQE2_Font(ListView1.Selected.Data);
    UpdateLabel;
    CheckBox2.Checked:=CurFont.Font.Antialias;
    CheckBox3.Checked:=(feShadow in CurFont.Font.Style);
    CheckBox4.Checked:=(feEmboss in CurFont.Font.Style);
    SpinEdit1.Value:=round(CurFont.Font.ShadowIntensity*100);
    SpinEdit2.Value:=CurFont.Font.ShadowDepth;
    SpinEdit3.Value:=round(CurFont.Font.EmbossStrength*100);
    SpinEdit4.Value:=CurFont.Font.EmbossDepth;
    ColorBox2.Selected:=TColor(CurFont.Font.ShadowColor and $FFFFFF);
    ColorBox3.Selected:=TColor(CurFont.Font.EmbossColor and $FFFFFF);
    ColorBox1Change(nil);
    GroupBox2.Visible:=true;
    GroupBox3.Visible:=true;
    Button2.Enabled:=true;
    Label10.Visible:=false;
  end;
end;

procedure TForm_Fonts.UpdateLabel;
var
  s:string;
begin
  s:=lng[8]+#13#10;
  s:=s+lng[9]+' '+CurFont.Font.FontName+#13#10;
  s:=s+lng[10]+' ';
  case CurFont.Font.CharSet of
    ANSI_CHARSET:s:=s+lng[13];
    DEFAULT_CHARSET:s:=s+lng[14];
    SYMBOL_CHARSET:s:=s+lng[15];
    SHIFTJIS_CHARSET:s:=s+lng[16];
    HANGEUL_CHARSET:s:=s+lng[17];
    GB2312_CHARSET:s:=s+lng[18];
    CHINESEBIG5_CHARSET:s:=s+lng[19];
    OEM_CHARSET:s:=s+lng[20];
    JOHAB_CHARSET:s:=s+lng[21];
    HEBREW_CHARSET:s:=s+lng[22];
    ARABIC_CHARSET:s:=s+lng[23];
    GREEK_CHARSET:s:=s+lng[24];
    TURKISH_CHARSET:s:=s+lng[25];
    VIETNAMESE_CHARSET:s:=s+lng[26];
    THAI_CHARSET:s:=s+lng[27];
    EASTEUROPE_CHARSET:s:=s+lng[28];
    RUSSIAN_CHARSET:s:=s+lng[29];
    MAC_CHARSET:s:=s+lng[30];
    BALTIC_CHARSET:s:=s+lng[31];
    else s:=s+lng[32];
  end;
  s:=s+#13#10;
  s:=s+lng[11]+' ';
  if feBold in CurFont.Font.Style then
  begin
    s:=s+lng[33];
    if feItalic in CurFont.Font.Style then s:=s+' '+lng[34];
  end
  else
    if feItalic in CurFont.Font.Style then s:=s+lng[34]
      else s:=s+lng[35];
  s:=s+#13#10;
  s:=s+lng[12]+' '+inttostr(CurFont.Font.Size);
  Label3.Caption:=s;
end;

procedure TForm_Fonts.UpdateList;
var
  a:integer;
begin
  CurFont:=nil;
  ListView1.Clear;
  for a:=0 to world.Fonts.Count-1 do
    ListView1.AddItem(DQE2_Font(world.Fonts[a]).name,world.Fonts[a]);
  ListView1.Selected:=nil;UpdateForm;
end;

procedure TForm_Fonts.UpdateObject;
begin
  if CurFont<>nil then
  begin
    if CheckBox3.Checked then
      CurFont.Font.Style:=CurFont.Font.Style+[feShadow]
    else
      CurFont.Font.Style:=CurFont.Font.Style-[feShadow];
    if CheckBox4.Checked then
      CurFont.Font.Style:=CurFont.Font.Style+[feEmboss]
    else
      CurFont.Font.Style:=CurFont.Font.Style-[feEmboss];
    CurFont.Font.Antialias:=CheckBox2.Checked;
    CurFont.Font.ShadowIntensity:=SpinEdit1.Value/100;
    CurFont.Font.ShadowColor:=ColorBox2.Selected or $FF000000;
    CurFont.Font.ShadowDepth:=SpinEdit2.Value;
    CurFont.Font.EmbossStrength:=SpinEdit3.Value/100;
    CurFont.Font.EmbossColor:=ColorBox3.Selected or $FF000000;
    CurFont.Font.EmbossDepth:=SpinEdit4.Value;
    try
      CurFont.Font.Update;
    except
    end;
  end;
end;

end.
