unit VLFGraphForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, ComCtrls, ImgList;

type
  TGraphForm = class(TForm)
    ScrollBox1: TScrollBox;
    GraphImage: TImage;
    OrgAmountsView: TListView;
    Splitter: TSplitter;
    OrgAmountImages: TImageList;
    procedure FormCreate(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    PrevMaxOrgAmount: Integer;
    PrevTime: Integer;
  public
    { Public declarations }
    procedure UpdateGraph;
  end;

var
  GraphForm: TGraphForm;

implementation

uses VLFObjects, AppMainForm, Tahoma;

{$R *.DFM}

procedure TGraphForm.UpdateGraph;
const
  LineColor = $606060;
  ZeroColor = clSilver;
  TimeDensity = 50;
var
  W, H, I, J: Integer;
  AItem: TListItem;
  Density: Integer;
  C: Single;
begin
  if Visible then
    with MainForm.Document, GraphImage, Canvas do
    begin
      if Env.MaxOrgAmount <= 150 then
        Density := 10
      else if Env.MaxOrgAmount <= 1500 then
        Density := 100
      else if Env.MaxOrgAmount <= 15000 then
        Density := 1000               
      else
        Density := 10000;
      if (Env.Time = PrevTime + 1) and (Env.MaxOrgAmount = PrevMaxOrgAmount) then
      begin
        W := Width;
        H := Height;
        if Env.MaxOrgAmount <> 0 then
          C := (H - 20) / Env.MaxOrgAmount
        else
          C := 100;
        CopyRect(Rect(0, 0, W - 1, H), Canvas, Rect(1, 0, W, H - 1));
        Pen.Color := clBlack;
        MoveTo(OrgAmountCount - 1, 0);
        LineTo(OrgAmountCount - 1, H);
        if (OrgAmountCount - 1 + Env.Time) mod TimeDensity = 0 then
        begin
          MoveTo(OrgAmountCount - 1, 0);
          LineTo(OrgAmountCount - 1, H);
        end;
        for I := 0 to (Env.MaxOrgAmount + Round(20 / C)) div Density do
          Pixels[OrgAmountCount - 1, H - Round(I * Density * C) - 10] := LineColor;
        for I := 0 to Env.SpeciesCount - 1 do
        begin
          Pen.Color := Env.SpeciesColors[I];
          MoveTo(OrgAmountCount - 1, H - Round(Env.OrgAmounts[0, I] * C) - 10);
          LineTo(OrgAmountCount - 2, H - Round(Env.OrgAmounts[1, I] * C) - 10);
        end;
        Pixels[OrgAmountCount - 1, H - 10] := ZeroColor;
      end
      else
      begin
        Picture.Bitmap.Height := GraphForm.ScrollBox1.ClientHeight;
        Brush.Color := clBlack;
        W := Width;
        H := Height;
        if Env.MaxOrgAmount <> 0 then
          C := (H - 20) / Env.MaxOrgAmount
        else
          C := 100;
        FillRect(Rect(0, 0, W, H));
        Pen.Color := LineColor;
        for I := 0 to OrgAmountCount - 1 do  // OPT: cyklus jen přes ty, pro které platí podmínka
          if (I + Env.Time) mod TimeDensity = 0 then
          begin
            MoveTo(I, 0);
            LineTo(I, H);
          end;
        Pen.Color := LineColor;
        for I := 0 to (Env.MaxOrgAmount + Round(20 / C)) div Density do
        begin
          MoveTo(0, H - Round(I * Density * C) - 10);
          LineTo(OrgAmountCount - 1, H - Round(I * Density * C) - 10);
        end;
        for I := 0 to Env.SpeciesCount - 1 do
        begin
          Pen.Color := Env.SpeciesColors[I];
          MoveTo(OrgAmountCount - 1, H - Round(Env.OrgAmounts[0, I] * C) - 10);
          for J := 1 to OrgAmountCount - 1 do
            LineTo(OrgAmountCount - J - 1, H - Round(Env.OrgAmounts[J, I] * C) - 10);
        end;
        Pen.Color := ZeroColor;
        MoveTo(0, H - 10);
        LineTo(OrgAmountCount - 1, H - 10);
        PrevMaxOrgAmount := Env.MaxOrgAmount;
        PrevTime := Env.Time + 1;
      end;
      with OrgAmountsView.Items do
      begin
        BeginUpdate;
        Clear;
        for I := 0 to Env.SpeciesCount - 1 do
          if Env.OrgAmounts[0, I] <> 0 then
          begin
            AItem := Add;
            AItem.Caption := Env.SpeciesNames[I];
            AItem.SubItems.Add(IntToStr(Env.OrgAmounts[0, I]));
            AItem.ImageIndex := I;
          end;
        EndUpdate;
      end;
    end;
end;

procedure TGraphForm.FormCreate(Sender: TObject);
var
  I: Integer;
  Bitmap: TBitmap;
begin
  TahomizeForm(Self);
  for I := 0 to MainForm.Document.Env.SpeciesCount - 1 do
  begin
    Bitmap := TBitmap.Create;
    with Bitmap, Canvas do
    begin
      Width := 16;
      Height := 16;
      Brush.Color := MainForm.Document.Env.SpeciesColors[I];
      Rectangle(Rect(2, 2, Width - 2, Height - 2));
      OrgAmountImages.Add(Bitmap, nil)
    end;
  end ;
  OrgAmountsView.DoubleBuffered := True;
  ScrollBox1.DoubleBuffered := True;

  PrevMaxOrgAmount := - 10;
  PrevTime := - 10;
end;

procedure TGraphForm.FormResize(Sender: TObject);
begin
  UpdateGraph;
end;

procedure TGraphForm.FormShow(Sender: TObject);
begin
  UpdateGraph;
end;

end.
