unit uFamilyTree;
{$mode objfpc}{$H+}
interface
uses
Classes, Graphics, LCLIntf, SysUtils;
type
TPerson =
class(TObject)
private
FirstPos,
LastPos: TPoint;
Canvas: TCanvas;
Region: THandle;
public
MouseOver: Boolean;
constructor Create(ACanvas: TCanvas; AFirstPos: TPoint);
procedure Draw;
procedure DrawSelection;
procedure SetRegion;
function GetRegion(X,Y: Integer): Boolean;
end;
TFamilyTree =
class(TList)
private
Width,
Height,
Objekt: Integer;
Canvas: TCanvas;
Offset: TBitmap;
public
constructor Create(AOffset: TBitmap; ACanvas: TCanvas);
procedure Paint(SelectPerson: Boolean);
procedure SetPerson(Person: Integer);
function GetObj: Integer;
function GetObjekt: TPerson;
function ObjektRegion(Punkt: TPoint; Select: Boolean): Boolean;
end;
implementation
constructor TPerson.Create(ACanvas: TCanvas; AFirstPos: TPoint);
begin
Self.Canvas:= ACanvas;
Self.FirstPos:= AFirstPos;
Self.LastPos:= AFirstPos;
end;
procedure TPerson.SetRegion;
begin
Region:= CreateRectRgn(FirstPos.X, FirstPos.Y, LastPos.X, LastPos.Y);
end;
procedure TPerson.Draw;
begin
Canvas.Brush.Style:= bsSolid;
if MouseOver
then
Canvas.Brush.Color:= clYellow
else
Canvas.Brush.Color:= clWhite;
Canvas.Rectangle(FirstPos.X, FirstPos.Y, (LastPos.X-FirstPos.X), (LastPos.Y-FirstPos.Y));
end;
procedure TPerson.DrawSelection;
begin
Canvas.Brush.Style:= bsSolid;
Canvas.Brush.Color:= clBlue;
Canvas.Rectangle(FirstPos.X, FirstPos.Y, (LastPos.X-FirstPos.X), (LastPos.Y-FirstPos.Y));
end;
function TPerson.GetRegion(X,Y: Integer): Boolean;
begin
if PTinRegion(Region,X,Y)
then
Result:= True
else Result:= False;
end;
constructor TFamilyTree.Create(AOffset: TBitmap; ACanvas: TCanvas);
var
i: Integer;
Person: TPerson;
begin
inherited Create;
Self.Canvas:= ACanvas;
Self.Offset:= AOffset;
Self.Offset.PixelFormat:= pf32Bit;
Self.Width:= 800;
Self.Height:= 550;
Self.Objekt:= -1;
for i:= 0
to 2
do
begin
Person:= TPerson.Create(ACanvas, Point(0,0));
//Person.SetRegion;
SetPerson(Add(Person));
end;
TPerson(Self.Items[1]).FirstPos:= Point(10, 10);
TPerson(Self.Items[1]).LastPos:= Point(200, 50);
TPerson(Self.Items[2]).FirstPos:= Point(10, 80);
TPerson(Self.Items[2]).LastPos:= Point(200, 130);
end;
procedure TFamilyTree.Paint(SelectPerson: Boolean);
var
i: Integer;
begin
Self.Offset.Width:= Self.Width;
Self.Offset.Height:= Self.Height;
with Self.Offset.Canvas
do begin
Self.Offset.Canvas.Brush.Color:= clGreen;
//fOffset.Canvas.Rectangle(Rect(0,0,fWidth,fHeight));
{ Wenn wenigstens ein Objekt vorhanden ist }
if Self.Count > 0
then
{ dann alle der entsprechenden Reihenfolge nach zeichnen }
for i:= 0
to Self.Count - 1
do
TPerson(Self[i]).Draw;
end;
if SelectPerson = True
then
TPerson(Self[Objekt]).DrawSelection;
//Self.Canvas.Draw(0, 0, Offset);
end;
procedure TFamilyTree.SetPerson(Person: Integer);
begin
Self.Objekt:= Person;
end;
function TFamilyTree.GetObj: Integer;
begin
if Objekt <> - 1
then
Result:= Objekt;
end;
function TFamilyTree.GetObjekt: TPerson;
begin
{ Wenn überhaupt ein aktuelles Objekt makiert }
if Objekt <> - 1
then
Result:= TPerson(Self[Objekt])
else
Result:=
NIL;
end;
function TFamilyTree.ObjektRegion(Punkt: TPoint; Select: Boolean): Boolean;
var
i: Integer;
begin
Result:= False;
i:= Self.Count - 1;
if Self.Count > 0
then
repeat
{ wenn gefunden, dann ... }
if TPerson(Self.items[i]).GetRegion(Punkt.X, Punkt.Y) = True
then begin
if Select = True
then Objekt:= i;
Result:= True;
end;
Dec(i);
until (Result = True)
or (i = -1);
end;
end.