unit nkBox; { Class for a box in xy-plane. Box coordinates goes like in real xy-plane. Author: Vesa Lappalainen Date: 15.2.1998 Changes: } interface uses Windows,Classes,nkPoint, nkscale; type TDBox = class; TInformDBox = procedure (const p:TDBox) of object; TDBox = class(TPersistent) private Fp : TDPoint; Fdp : TDPoint; Ftp : TDPoint; FOrigo : TDPoint; FInform : TInformDBox; FDoInform : boolean; protected public constructor Create; virtual; constructor Create5(ix,iy,idx,idy:double;IInform:TInformDBox); virtual; destructor Destroy; override; procedure PChange(const p:TDPoint); virtual; procedure FromWin(ix,iy,idx,idy:integer; const scale:TScale); virtual; procedure ToWin(var ip,idp : TPoint; const scale:TScale); virtual; procedure ToWinDXY(var ip,idp : TPoint; const scale:TScale; idx,idy:integer); virtual; procedure SetDoInform(value:boolean); virtual; property DoInform : boolean read FDoInform write SetDoInform; published property p : TDPoint read Fp write Fp; property dp : TDPoint read Fdp write Fdp; property Origo : TDPoint read FOrigo write FOrigo; end; implementation //----------------------------------------------------------------------------- constructor TDBox.Create; // override; begin Create5(0,0,5,5,nil); end; //----------------------------------------------------------------------------- constructor TDBox.Create5(ix,iy,idx,idy:double;IInform:TInformDBox); // virtual; begin inherited Create; Fp := TDPoint.Create3(ix,iy,PChange); Fdp := TDPoint.Create3(idx,idy,PChange); FOrigo := TDPoint.Create3(0,0,PChange); Ftp := TDPoint.Create3(0,0,nil); Finform := IInform; FDoInform := true; end; //----------------------------------------------------------------------------- procedure TDBox.PChange(const p:TDPoint); // virtual; begin if Assigned(FInform) and FDoInform then FInform(self); end; //----------------------------------------------------------------------------- destructor TDBox.Destroy; // override; begin Ftp.Free; FOrigo.Free; Fdp.Free; Fp.Free; inherited; end; //----------------------------------------------------------------------------- procedure TDBox.FromWin(ix,iy,idx,idy:integer; const scale:TScale); // virtual; var ip,idp : TPoint; oldInfo : boolean; begin if ( Scale = nil ) then begin p.SetXYnu(ix+Origo.x,iy+Origo.y); dp.SetXY(idx,idy); exit; end; ip.x := ix ; ip.y := iy; idp.x := idx; idp.y := idy; if ( Scale.Fac.x < 0 ) then ip.x := ip.x + idx; if ( Scale.Fac.y > 0 ) then ip.y := ip.y + idy; oldInfo := DoInform; DoInform := false; Scale.UpScaleP(ip,p); p.AddV(Origo); Scale.UpScaleDP(idp,dp); DoInform := oldInfo; PChange(nil); end; //----------------------------------------------------------------------------- procedure TDBox.ToWinDXY(var ip,idp : TPoint; const scale:TScale; idx,idy:integer); // virtual; begin if ( Scale = nil ) then begin ip.x := trunc(p.x-Origo.x); ip.y := trunc(p.y-Origo.y); idp.x := trunc(dp.x); idp.y := trunc(dp.y); exit; end; Ftp.Assign(p); Ftp.SubV(Origo); ip := Scale.DownScaleP(Ftp); idp := Scale.DownScaleDP(dp); if ( Scale.Fac.x < 0 ) then begin idp.x := abs(idp.x); ip.x := ip.x - idp.x - idx; end else ip.x := ip.x + idx; if ( Scale.Fac.y > 0 ) then ip.y := ip.y - idp.y - idy else begin idp.y := abs(idp.y); ip.y := ip.y + idy; end; end; //----------------------------------------------------------------------------- procedure TDBox.ToWin(var ip,idp : TPoint; const scale:TScale); // virtual; begin ToWinDXY(ip,idp,scale,0,0); end; //----------------------------------------------------------------------------- procedure TDBox.SetDoInform(value:boolean); // virtual; begin FDoInform := value; PChange(nil); end; end.