Ignore:
Timestamp:
Sep 25, 2017, 2:48:08 PM (7 years ago)
Author:
chronos
Message:
  • Added: Drawing relative to Canvas. In case of Window drawing relative to Window.
  • Modified: Use custom TPoint and TRectangle if possible.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Kernel/UGraphics.pas

    r11 r13  
    66
    77uses
    8   Classes, SysUtils, fgl;
     8  SysUtils, fgl;
    99
    1010type
    11   TRectangle = class
     11  TDesktop = class;
     12
     13  { TPoint }
     14
     15  TPoint = record
     16    X: Integer;
     17    Y: Integer;
     18    function Create(X, Y: Integer): TPoint;
     19    function Add(P: TPoint): TPoint;
     20  end;
     21
     22  { TRectangle }
     23
     24  TRectangle = record
     25  private
     26    function GetBottom: Integer;
     27    function GetLeft: Integer;
     28    function GetRight: Integer;
     29    function GetTop: Integer;
     30    procedure SetBottom(AValue: Integer);
     31    procedure SetLeft(AValue: Integer);
     32    procedure SetRight(AValue: Integer);
     33    procedure SetTop(AValue: Integer);
     34  public
    1235    Position: TPoint;
    1336    Size: TPoint;
     37    function PointInside(P: TPoint): Boolean;
     38    function Create(Left, Top, Width, Height: Integer): TRectangle; overload;
     39    function Create(Position, Size: TPoint): TRectangle; overload;
     40    function AddPoint(P: TPoint): TRectangle;
     41    property Left: Integer read GetLeft write SetLeft;
     42    property Top: Integer read GetTop write SetTop;
     43    property Right: Integer read GetRight write SetRight;
     44    property Bottom: Integer read GetBottom write SetBottom;
     45  end;
     46
     47  TColorFormat = (cfRGBA8, cfGray8);
     48  TColor = Integer;
     49
     50  { TCanvas }
     51
     52  TCanvas = class
     53    Parent: TCanvas;
     54    Position: TPoint;
     55    Id: Integer;
     56    procedure DrawText(Pos: TPoint; Text: string; Color: TColor); virtual;
     57    procedure DrawLine(P1, P2: TPoint; Color: TColor); virtual;
     58    procedure DrawRect(Rect: TRectangle; Color: TColor); virtual;
     59    procedure SetPixel(P: TPoint; Color: TColor); virtual;
    1460  end;
    1561
     
    1763
    1864  TGraphicObject = class
     65  private
     66    FCanvas: TCanvas;
     67    function GetCanvas: TCanvas;
     68  public
     69    Desktop: TDesktop;
    1970    Id: Integer;
    2071    Visible: Boolean;
    2172    procedure Paint; virtual;
     73    property Canvas: TCanvas read GetCanvas;
    2274  end;
    2375
     
    2577
    2678  TWindow = class(TGraphicObject)
     79  public
    2780    Title: string;
    2881    Bounds: TRectangle;
     
    4396
    4497  TDesktop = class
     98  private
     99    ObjectLastId: Integer;
     100    CanvasLastId: Integer;
     101  public
    45102    Objects: TFPGObjectList<TGraphicObject>;
     103    Canvases: TFPGObjectList<TCanvas>;
     104    function CreateWindow: TWindow;
     105    function CreateCanvas: TCanvas;
     106    function FindObjectById(Id: Integer): TGraphicObject;
     107    function FindCanvasById(Id: Integer): TCanvas;
    46108    procedure Paint;
    47109    constructor Create;
     
    51113implementation
    52114
     115{ TRectangle }
     116
     117function TRectangle.GetBottom: Integer;
     118begin
     119  Result := Position.Y + Size.Y;
     120end;
     121
     122function TRectangle.GetLeft: Integer;
     123begin
     124  Result := Position.X;
     125end;
     126
     127function TRectangle.GetRight: Integer;
     128begin
     129  Result := Position.X + Size.X;
     130end;
     131
     132function TRectangle.GetTop: Integer;
     133begin
     134  Result := Position.Y;
     135end;
     136
     137procedure TRectangle.SetBottom(AValue: Integer);
     138begin
     139  Size.Y := AValue - Position.Y;
     140end;
     141
     142procedure TRectangle.SetLeft(AValue: Integer);
     143begin
     144  Position.X := AValue;
     145end;
     146
     147procedure TRectangle.SetRight(AValue: Integer);
     148begin
     149  Size.X := AValue - Position.X;
     150end;
     151
     152procedure TRectangle.SetTop(AValue: Integer);
     153begin
     154  Size.Y := AValue;
     155end;
     156
     157function TRectangle.PointInside(P: TPoint): Boolean;
     158begin
     159  Result := (P.X >= Position.X) and (P.Y >= Position.Y) and
     160    (P.X < (Position.X + Size.X)) and (P.Y < (Position.Y + Size.Y))
     161end;
     162
     163function TRectangle.Create(Left, Top, Width, Height: Integer): TRectangle;
     164begin
     165  Result.Position.X := Left;
     166  Result.Position.Y := Top;
     167  Result.Size.X := Width;
     168  Result.Size.Y := Height;
     169end;
     170
     171function TRectangle.Create(Position, Size: TPoint): TRectangle;
     172begin
     173  Result.Position := Position;
     174  Result.Size := Size;
     175end;
     176
     177function TRectangle.AddPoint(P: TPoint): TRectangle;
     178begin
     179  Result.Size := Size;
     180  Result.Position := Position.Add(P);
     181end;
     182
     183{ TPoint }
     184
     185function TPoint.Create(X, Y: Integer): TPoint;
     186begin
     187  Result.X := X;
     188  Result.Y := Y;
     189end;
     190
     191function TPoint.Add(P: TPoint): TPoint;
     192begin
     193  Result.X := X + P.X;
     194  Result.Y := Y + P.Y;
     195end;
     196
     197{ TCanvas }
     198
     199procedure TCanvas.DrawText(Pos: TPoint; Text: string; Color: TColor);
     200begin
     201  if Assigned(Parent) then
     202    Parent.DrawText(Pos.Add(Position), Text, Color);
     203end;
     204
     205procedure TCanvas.DrawLine(P1, P2: TPoint; Color: TColor);
     206begin
     207  if Assigned(Parent) then
     208    Parent.DrawLine(P1.Add(Position), P2.Add(Position), Color);
     209end;
     210
     211procedure TCanvas.DrawRect(Rect: TRectangle; Color: TColor);
     212begin
     213  if Assigned(Parent) then
     214    Parent.DrawRect(Rect.AddPoint(Position), Color);
     215end;
     216
     217procedure TCanvas.SetPixel(P: TPoint; Color: TColor);
     218begin
     219  if Assigned(Parent) then
     220    Parent.SetPixel(P.Add(Position), Color);
     221end;
     222
    53223{ TGraphicObject }
    54224
     225function TGraphicObject.GetCanvas: TCanvas;
     226begin
     227  FCanvas := Desktop.CreateCanvas;
     228  Result := FCanvas;
     229end;
     230
    55231procedure TGraphicObject.Paint;
    56232begin
    57 
    58233end;
    59234
     
    63238begin
    64239  inherited Paint;
     240  Canvas.DrawRect(Bounds, $ff0000);
    65241end;
    66242
    67243constructor TWindow.Create;
    68244begin
    69   Bounds := TRectangle.Create;
    70245end;
    71246
    72247destructor TWindow.Destroy;
    73248begin
    74   Bounds.Free;
    75249  inherited Destroy;
    76250end;
    77251
    78252{ TDesktop }
     253
     254function TDesktop.CreateWindow: TWindow;
     255begin
     256  Inc(ObjectLastId);
     257  Result := TWindow.Create;
     258  Result.Id := ObjectLastId;
     259  Objects.Add(Result);
     260end;
     261
     262function TDesktop.CreateCanvas: TCanvas;
     263begin
     264  Inc(CanvasLastId);
     265  Result := TCanvas.Create;
     266  Result.Id := CanvasLastId;
     267  Canvases.Add(Result);
     268end;
     269
     270function TDesktop.FindObjectById(Id: Integer): TGraphicObject;
     271var
     272  I: Integer;
     273begin
     274  I := 0;
     275  while (I < Objects.Count) and (Objects[I].Id <> Id) do Inc(I);
     276  if I < Objects.Count then Result := Objects[I]
     277    else Result := nil;
     278end;
     279
     280function TDesktop.FindCanvasById(Id: Integer): TCanvas;
     281var
     282  I: Integer;
     283begin
     284  I := 0;
     285  while (I < Canvases.Count) and (Canvases[I].Id <> Id) do Inc(I);
     286  if I < Canvases.Count then Result := Canvases[I]
     287    else Result := nil;
     288end;
    79289
    80290procedure TDesktop.Paint;
     
    89299begin
    90300  Objects := TFPGObjectList<TGraphicObject>.Create;
     301  Canvases := TFPGObjectList<TCanvas>.Create;
    91302end;
    92303
    93304destructor TDesktop.Destroy;
    94305begin
    95   Objects.Free;
     306  FreeAndNil(Canvases);
     307  FreeAndNil(Objects);
    96308  inherited Destroy;
    97309end;
Note: See TracChangeset for help on using the changeset viewer.