| 1 | unit Kernel.Graphics;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | SysUtils, Generics.Collections;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 9 | TDesktop = class;
|
|---|
| 10 |
|
|---|
| 11 | { TPoint }
|
|---|
| 12 |
|
|---|
| 13 | TPoint = record
|
|---|
| 14 | X: Integer;
|
|---|
| 15 | Y: Integer;
|
|---|
| 16 | class function Create(X, Y: Integer): TPoint; inline; static;
|
|---|
| 17 | function Add(P: TPoint): TPoint;
|
|---|
| 18 | end;
|
|---|
| 19 |
|
|---|
| 20 | { TRectangle }
|
|---|
| 21 |
|
|---|
| 22 | TRectangle = record
|
|---|
| 23 | private
|
|---|
| 24 | function GetBottom: Integer;
|
|---|
| 25 | function GetLeft: Integer;
|
|---|
| 26 | function GetRight: Integer;
|
|---|
| 27 | function GetTop: Integer;
|
|---|
| 28 | procedure SetBottom(AValue: Integer);
|
|---|
| 29 | procedure SetLeft(AValue: Integer);
|
|---|
| 30 | procedure SetRight(AValue: Integer);
|
|---|
| 31 | procedure SetTop(AValue: Integer);
|
|---|
| 32 | public
|
|---|
| 33 | Position: TPoint;
|
|---|
| 34 | Size: TPoint;
|
|---|
| 35 | function PointInside(P: TPoint): Boolean;
|
|---|
| 36 | class function Create(Left, Top, Width, Height: Integer): TRectangle; overload; inline; static;
|
|---|
| 37 | class function Create(Position, Size: TPoint): TRectangle; overload; inline; static;
|
|---|
| 38 | function AddPoint(P: TPoint): TRectangle;
|
|---|
| 39 | property Left: Integer read GetLeft write SetLeft;
|
|---|
| 40 | property Top: Integer read GetTop write SetTop;
|
|---|
| 41 | property Right: Integer read GetRight write SetRight;
|
|---|
| 42 | property Bottom: Integer read GetBottom write SetBottom;
|
|---|
| 43 | end;
|
|---|
| 44 |
|
|---|
| 45 | TColorFormat = (cfRGBA8, cfGray8);
|
|---|
| 46 | TColor = Integer;
|
|---|
| 47 |
|
|---|
| 48 | { TCanvas }
|
|---|
| 49 |
|
|---|
| 50 | TCanvas = class
|
|---|
| 51 | Parent: TCanvas;
|
|---|
| 52 | Position: TPoint;
|
|---|
| 53 | Id: Integer;
|
|---|
| 54 | procedure DrawText(Pos: TPoint; Text: string; Color: TColor); virtual;
|
|---|
| 55 | procedure DrawLine(P1, P2: TPoint; Color: TColor); virtual;
|
|---|
| 56 | procedure DrawRect(Rect: TRectangle; Color: TColor); virtual;
|
|---|
| 57 | procedure DrawFrame(Rect: TRectangle; Color: TColor); virtual;
|
|---|
| 58 | procedure SetPixel(P: TPoint; Color: TColor); virtual;
|
|---|
| 59 | end;
|
|---|
| 60 |
|
|---|
| 61 | { TGraphicObject }
|
|---|
| 62 |
|
|---|
| 63 | TGraphicObject = class
|
|---|
| 64 | private
|
|---|
| 65 | FCanvas: TCanvas;
|
|---|
| 66 | function GetCanvas: TCanvas;
|
|---|
| 67 | public
|
|---|
| 68 | Desktop: TDesktop;
|
|---|
| 69 | Id: Integer;
|
|---|
| 70 | Visible: Boolean;
|
|---|
| 71 | constructor Create;
|
|---|
| 72 | procedure Paint; virtual;
|
|---|
| 73 | property Canvas: TCanvas read GetCanvas;
|
|---|
| 74 | end;
|
|---|
| 75 |
|
|---|
| 76 | { TWindow }
|
|---|
| 77 |
|
|---|
| 78 | TWindow = class(TGraphicObject)
|
|---|
| 79 | public
|
|---|
| 80 | Title: string;
|
|---|
| 81 | Bounds: TRectangle;
|
|---|
| 82 | procedure Paint; override;
|
|---|
| 83 | constructor Create;
|
|---|
| 84 | destructor Destroy; override;
|
|---|
| 85 | end;
|
|---|
| 86 |
|
|---|
| 87 | TRaster = class(TGraphicObject)
|
|---|
| 88 | end;
|
|---|
| 89 |
|
|---|
| 90 | TFont = class(TGraphicObject)
|
|---|
| 91 | end;
|
|---|
| 92 |
|
|---|
| 93 | { TDesktop }
|
|---|
| 94 |
|
|---|
| 95 | TDesktop = class
|
|---|
| 96 | private
|
|---|
| 97 | ObjectLastId: Integer;
|
|---|
| 98 | CanvasLastId: Integer;
|
|---|
| 99 | public
|
|---|
| 100 | Objects: TObjectList<TGraphicObject>;
|
|---|
| 101 | Canvases: TObjectList<TCanvas>;
|
|---|
| 102 | ParentCanvas: TCanvas;
|
|---|
| 103 | function CreateWindow: TWindow;
|
|---|
| 104 | function CreateCanvas: TCanvas;
|
|---|
| 105 | function FindObjectById(Id: Integer): TGraphicObject;
|
|---|
| 106 | function FindCanvasById(Id: Integer): TCanvas;
|
|---|
| 107 | procedure Paint;
|
|---|
| 108 | constructor Create;
|
|---|
| 109 | destructor Destroy; override;
|
|---|
| 110 | end;
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 | implementation
|
|---|
| 114 |
|
|---|
| 115 | { TRectangle }
|
|---|
| 116 |
|
|---|
| 117 | function TRectangle.GetBottom: Integer;
|
|---|
| 118 | begin
|
|---|
| 119 | Result := Position.Y + Size.Y;
|
|---|
| 120 | end;
|
|---|
| 121 |
|
|---|
| 122 | function TRectangle.GetLeft: Integer;
|
|---|
| 123 | begin
|
|---|
| 124 | Result := Position.X;
|
|---|
| 125 | end;
|
|---|
| 126 |
|
|---|
| 127 | function TRectangle.GetRight: Integer;
|
|---|
| 128 | begin
|
|---|
| 129 | Result := Position.X + Size.X;
|
|---|
| 130 | end;
|
|---|
| 131 |
|
|---|
| 132 | function TRectangle.GetTop: Integer;
|
|---|
| 133 | begin
|
|---|
| 134 | Result := Position.Y;
|
|---|
| 135 | end;
|
|---|
| 136 |
|
|---|
| 137 | procedure TRectangle.SetBottom(AValue: Integer);
|
|---|
| 138 | begin
|
|---|
| 139 | Size.Y := AValue - Position.Y;
|
|---|
| 140 | end;
|
|---|
| 141 |
|
|---|
| 142 | procedure TRectangle.SetLeft(AValue: Integer);
|
|---|
| 143 | begin
|
|---|
| 144 | Position.X := AValue;
|
|---|
| 145 | end;
|
|---|
| 146 |
|
|---|
| 147 | procedure TRectangle.SetRight(AValue: Integer);
|
|---|
| 148 | begin
|
|---|
| 149 | Size.X := AValue - Position.X;
|
|---|
| 150 | end;
|
|---|
| 151 |
|
|---|
| 152 | procedure TRectangle.SetTop(AValue: Integer);
|
|---|
| 153 | begin
|
|---|
| 154 | Size.Y := AValue;
|
|---|
| 155 | end;
|
|---|
| 156 |
|
|---|
| 157 | function TRectangle.PointInside(P: TPoint): Boolean;
|
|---|
| 158 | begin
|
|---|
| 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))
|
|---|
| 161 | end;
|
|---|
| 162 |
|
|---|
| 163 | class function TRectangle.Create(Left, Top, Width, Height: Integer): TRectangle;
|
|---|
| 164 | begin
|
|---|
| 165 | Result.Position.X := Left;
|
|---|
| 166 | Result.Position.Y := Top;
|
|---|
| 167 | Result.Size.X := Width;
|
|---|
| 168 | Result.Size.Y := Height;
|
|---|
| 169 | end;
|
|---|
| 170 |
|
|---|
| 171 | class function TRectangle.Create(Position, Size: TPoint): TRectangle;
|
|---|
| 172 | begin
|
|---|
| 173 | Result.Position := Position;
|
|---|
| 174 | Result.Size := Size;
|
|---|
| 175 | end;
|
|---|
| 176 |
|
|---|
| 177 | function TRectangle.AddPoint(P: TPoint): TRectangle;
|
|---|
| 178 | begin
|
|---|
| 179 | Result.Size := Size;
|
|---|
| 180 | Result.Position := Position.Add(P);
|
|---|
| 181 | end;
|
|---|
| 182 |
|
|---|
| 183 | { TPoint }
|
|---|
| 184 |
|
|---|
| 185 | class function TPoint.Create(X, Y: Integer): TPoint;
|
|---|
| 186 | begin
|
|---|
| 187 | Result.X := X;
|
|---|
| 188 | Result.Y := Y;
|
|---|
| 189 | end;
|
|---|
| 190 |
|
|---|
| 191 | function TPoint.Add(P: TPoint): TPoint;
|
|---|
| 192 | begin
|
|---|
| 193 | Result.X := X + P.X;
|
|---|
| 194 | Result.Y := Y + P.Y;
|
|---|
| 195 | end;
|
|---|
| 196 |
|
|---|
| 197 | { TCanvas }
|
|---|
| 198 |
|
|---|
| 199 | procedure TCanvas.DrawText(Pos: TPoint; Text: string; Color: TColor);
|
|---|
| 200 | begin
|
|---|
| 201 | if Assigned(Parent) then
|
|---|
| 202 | Parent.DrawText(Pos.Add(Position), Text, Color);
|
|---|
| 203 | end;
|
|---|
| 204 |
|
|---|
| 205 | procedure TCanvas.DrawLine(P1, P2: TPoint; Color: TColor);
|
|---|
| 206 | begin
|
|---|
| 207 | if Assigned(Parent) then
|
|---|
| 208 | Parent.DrawLine(P1.Add(Position), P2.Add(Position), Color);
|
|---|
| 209 | end;
|
|---|
| 210 |
|
|---|
| 211 | procedure TCanvas.DrawRect(Rect: TRectangle; Color: TColor);
|
|---|
| 212 | begin
|
|---|
| 213 | if Assigned(Parent) then
|
|---|
| 214 | Parent.DrawRect(Rect.AddPoint(Position), Color);
|
|---|
| 215 | end;
|
|---|
| 216 |
|
|---|
| 217 | procedure TCanvas.DrawFrame(Rect: TRectangle; Color: TColor);
|
|---|
| 218 | begin
|
|---|
| 219 | if Assigned(Parent) then
|
|---|
| 220 | Parent.DrawFrame(Rect.AddPoint(Position), Color);
|
|---|
| 221 | end;
|
|---|
| 222 |
|
|---|
| 223 | procedure TCanvas.SetPixel(P: TPoint; Color: TColor);
|
|---|
| 224 | begin
|
|---|
| 225 | if Assigned(Parent) then
|
|---|
| 226 | Parent.SetPixel(P.Add(Position), Color);
|
|---|
| 227 | end;
|
|---|
| 228 |
|
|---|
| 229 | { TGraphicObject }
|
|---|
| 230 |
|
|---|
| 231 | function TGraphicObject.GetCanvas: TCanvas;
|
|---|
| 232 | begin
|
|---|
| 233 | if not Assigned(FCanvas) then
|
|---|
| 234 | FCanvas := Desktop.CreateCanvas;
|
|---|
| 235 | Result := FCanvas;
|
|---|
| 236 | end;
|
|---|
| 237 |
|
|---|
| 238 | constructor TGraphicObject.Create;
|
|---|
| 239 | begin
|
|---|
| 240 | FCanvas := nil;
|
|---|
| 241 | end;
|
|---|
| 242 |
|
|---|
| 243 | procedure TGraphicObject.Paint;
|
|---|
| 244 | begin
|
|---|
| 245 | end;
|
|---|
| 246 |
|
|---|
| 247 | { TWindow }
|
|---|
| 248 |
|
|---|
| 249 | procedure TWindow.Paint;
|
|---|
| 250 | begin
|
|---|
| 251 | inherited Paint;
|
|---|
| 252 | Canvas.DrawRect(TRectangle.Create(TPoint.Create(0, 0), Bounds.Size), $a0a0a0);
|
|---|
| 253 | Canvas.DrawFrame(TRectangle.Create(TPoint.Create(0, 0), Bounds.Size), $000000);
|
|---|
| 254 | end;
|
|---|
| 255 |
|
|---|
| 256 | constructor TWindow.Create;
|
|---|
| 257 | begin
|
|---|
| 258 | end;
|
|---|
| 259 |
|
|---|
| 260 | destructor TWindow.Destroy;
|
|---|
| 261 | begin
|
|---|
| 262 | inherited;
|
|---|
| 263 | end;
|
|---|
| 264 |
|
|---|
| 265 | { TDesktop }
|
|---|
| 266 |
|
|---|
| 267 | function TDesktop.CreateWindow: TWindow;
|
|---|
| 268 | begin
|
|---|
| 269 | Inc(ObjectLastId);
|
|---|
| 270 | Result := TWindow.Create;
|
|---|
| 271 | Result.Id := ObjectLastId;
|
|---|
| 272 | Result.Desktop := Self;
|
|---|
| 273 | Result.Canvas.Parent := ParentCanvas;
|
|---|
| 274 | Objects.Add(Result);
|
|---|
| 275 | Paint;
|
|---|
| 276 | end;
|
|---|
| 277 |
|
|---|
| 278 | function TDesktop.CreateCanvas: TCanvas;
|
|---|
| 279 | begin
|
|---|
| 280 | Inc(CanvasLastId);
|
|---|
| 281 | Result := TCanvas.Create;
|
|---|
| 282 | Result.Id := CanvasLastId;
|
|---|
| 283 | Canvases.Add(Result);
|
|---|
| 284 | end;
|
|---|
| 285 |
|
|---|
| 286 | function TDesktop.FindObjectById(Id: Integer): TGraphicObject;
|
|---|
| 287 | var
|
|---|
| 288 | I: Integer;
|
|---|
| 289 | begin
|
|---|
| 290 | I := 0;
|
|---|
| 291 | while (I < Objects.Count) and (Objects[I].Id <> Id) do Inc(I);
|
|---|
| 292 | if I < Objects.Count then Result := Objects[I]
|
|---|
| 293 | else Result := nil;
|
|---|
| 294 | end;
|
|---|
| 295 |
|
|---|
| 296 | function TDesktop.FindCanvasById(Id: Integer): TCanvas;
|
|---|
| 297 | var
|
|---|
| 298 | I: Integer;
|
|---|
| 299 | begin
|
|---|
| 300 | I := 0;
|
|---|
| 301 | while (I < Canvases.Count) and (Canvases[I].Id <> Id) do Inc(I);
|
|---|
| 302 | if I < Canvases.Count then Result := Canvases[I]
|
|---|
| 303 | else Result := nil;
|
|---|
| 304 | end;
|
|---|
| 305 |
|
|---|
| 306 | procedure TDesktop.Paint;
|
|---|
| 307 | var
|
|---|
| 308 | I: Integer;
|
|---|
| 309 | begin
|
|---|
| 310 | for I := 0 to Objects.Count - 1 do
|
|---|
| 311 | TGraphicObject(Objects[I]).Paint;
|
|---|
| 312 | end;
|
|---|
| 313 |
|
|---|
| 314 | constructor TDesktop.Create;
|
|---|
| 315 | begin
|
|---|
| 316 | Objects := TObjectList<TGraphicObject>.Create;
|
|---|
| 317 | Canvases := TObjectList<TCanvas>.Create;
|
|---|
| 318 | end;
|
|---|
| 319 |
|
|---|
| 320 | destructor TDesktop.Destroy;
|
|---|
| 321 | begin
|
|---|
| 322 | FreeAndNil(Canvases);
|
|---|
| 323 | FreeAndNil(Objects);
|
|---|
| 324 | inherited;
|
|---|
| 325 | end;
|
|---|
| 326 |
|
|---|
| 327 | end.
|
|---|
| 328 |
|
|---|