unit U_Navigator;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, PCPanel, PCDrawBox, PCDrawing, PowerCad,
  // PowerCad
  pcMsbar, PCTypesUtils, DrawObjects, DlgBase,  ExtDlgs, PCLayerDlg, OleCtnrs, PCgui,
  GuiStrings, DrawEngine, Math, siComp, siLngLnk;


type
  TF_Navigator = class(TForm)
    PCadNavigator: TPowerCad;
    lng_Forms: TsiLangLinked;
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure PCadNavigatorSurfaceMouseDown(Sender: TObject; Button: TMouseButton;
                                            Shift: TShiftState; X, Y: Double);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    ViewArea: TRectangle;
    Page: TRect;
    KoefNav_X, KoefNav_Y: Real;
    KoefCAD_X, KoefCAD_Y: Real;
    Nav_W, Nav_H: double;
    CAD_W, CAD_H: integer;
    Point: TPoint;
    Scroll_dx, Scroll_dy: integer;
    { Public declarations }
  end;

var
  F_Navigator: TF_Navigator;

implementation
uses USCS_Main, U_CAD, U_Common, Types, U_BaseCommon;

{$R *.dfm}

procedure TF_Navigator.FormShow(Sender: TObject);
var
  i: integer;
  CurLayer: TLayer;
  FFigure: TFigure;
  LHandle: Integer;
begin
  try
    FSCS_Main.aViewNavigator.Checked := true;
    ReAssignNavigatorParams;
  except
    on E: Exception do AddExceptionToLogEx('TF_Navigator.FormShow', E.Message);
  end;
end;

procedure TF_Navigator.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  try
    FSCS_Main.aViewNavigator.Execute;
  except
    on E: Exception do AddExceptionToLogEx('TF_Navigator.FormClose', E.Message);
  end;
end;

procedure TF_Navigator.PCadNavigatorSurfaceMouseDown(Sender: TObject;
          Button: TMouseButton; Shift: TShiftState; X, Y: Double);
var
  CenterCadZ, CenterCadX, CenterCadY: double;
  CurrentX, CurrentY: double;
  deltax, deltay: double;
  posx, posy: integer;
  SetCadX, SetCadY: integer;
begin
  try
(*
    CenterCadZ := 0;
    if (GCadForm <> nil) and (GCadForm.PCad <> nil) then
    begin
      GCadForm.PCad.DeselectAll(0);
      GCadForm.SetZoomScale(GCadForm.PCad.ZoomScale + 0);

      CenterCadX := GCadForm.PCad.WorkWidth / 2;
      CenterCadY := GCadForm.PCad.WorkHeight / 2;
      GCadForm.PCad.ConvertXY(CenterCadX, CenterCadY, CenterCadZ);
      CurrentX := X;
      CurrentY := Y;
      GCadForm.PCad.ConvertXY(CurrentX, CurrentY, CenterCadZ);
      deltax := round(CurrentX - CenterCadX);
      deltay := round(CurrentY - CenterCadY);

      SetCadX := GCadForm.PCad.HSCBarPosition + round(deltax);
      SetCadY := GCadForm.PCad.VSCBarPosition + round(deltay);
      GCadForm.PCad.SetHScrollPosition(SetCadX, True);
      GCadForm.PCad.SetVScrollPosition(SetCadY, True);
      GCadForm.Set_SCS_HorScroll;
      GCadForm.Set_SCS_VerScroll;
      RefreshCAD(GCadForm.PCad);
      RefreshCAD(PCadNavigator);
    end;
*)

    CenterCadZ := 0;
    if (GCadForm <> nil) and (GCadForm.PCad <> nil) then
    begin
      GCadForm.PCad.DeselectAll(0);
      GCadForm.SetZoomScale(GCadForm.PCad.ZoomScale + 0);

      posx := roundup(abs(x/GCadForm.PCad.WorkWidth) * 100);
      //posx := posx - roundup(GCadForm.PCad.WorkWidth / 100);
      //posx := posx - roundup(GCadForm.PCad.WorkWidth/ GCadForm.PCad.ZoomScale);
      posx := posx - roundup(GCadForm.HorScroll.PageSize / 2);
      posy := roundup(abs(y/GCadForm.PCad.WorkHeight) * 100);
      //posy := posy - roundup(GCadForm.PCad.WorkHeight / 100);
      //posy := posy - roundup(GCadForm.PCad.WorkHeight / GCadForm.PCad.ZoomScale);
      posy := posy - roundup(GCadForm.VerScroll.PageSize / 2);
      if posx > 100 then
        posx := 100;
      if posx < 0 then
        posx := 0;
      if posy > 100 then
        posy := 100;
      if posy < 0 then
        posy := 0;
      GCadForm.HorScroll.Position := posx;
      GCadForm.VerScroll.Position := posy;
      GCadForm.Set_PCad_HorScroll;
      GCadForm.Set_PCad_VerScroll;
      RefreshCAD(GCadForm.PCad);
      RefreshCAD(PCadNavigator);
    end;

  except
    on E: Exception do AddExceptionToLogEx('TF_Navigator.PCadNavigatorSurfaceMouseDown', E.Message);
  end;
end;

procedure TF_Navigator.FormDestroy(Sender: TObject);
begin
  try
//
  except
    on E: Exception do AddExceptionToLogEx('TF_Navigator.FormDestroy', E.Message);
  end;
end;

procedure TF_Navigator.FormCreate(Sender: TObject);
begin
  try
    PCadNavigator.OnMouseWheel := nil;
    if (GCadForm <> nil) and (GCadForm.PCad <> nil) then
      ReAssignNavigatorParams;
  except
    on E: Exception do AddExceptionToLogEx('TF_Navigator.FormCreate', E.Message);
  end;
end;

end.
