source: trunk/Packages/Kernel/Kernel.Graphics.pas

Last change on this file was 60, checked in by chronos, 2 months ago
  • Modified: Remove U prefix from unit names.
File size: 6.8 KB
Line 
1unit Kernel.Graphics;
2
3interface
4
5uses
6 SysUtils, Generics.Collections;
7
8type
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
113implementation
114
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
163class function 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
171class function 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
185class function 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.DrawFrame(Rect: TRectangle; Color: TColor);
218begin
219 if Assigned(Parent) then
220 Parent.DrawFrame(Rect.AddPoint(Position), Color);
221end;
222
223procedure TCanvas.SetPixel(P: TPoint; Color: TColor);
224begin
225 if Assigned(Parent) then
226 Parent.SetPixel(P.Add(Position), Color);
227end;
228
229{ TGraphicObject }
230
231function TGraphicObject.GetCanvas: TCanvas;
232begin
233 if not Assigned(FCanvas) then
234 FCanvas := Desktop.CreateCanvas;
235 Result := FCanvas;
236end;
237
238constructor TGraphicObject.Create;
239begin
240 FCanvas := nil;
241end;
242
243procedure TGraphicObject.Paint;
244begin
245end;
246
247{ TWindow }
248
249procedure TWindow.Paint;
250begin
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);
254end;
255
256constructor TWindow.Create;
257begin
258end;
259
260destructor TWindow.Destroy;
261begin
262 inherited;
263end;
264
265{ TDesktop }
266
267function TDesktop.CreateWindow: TWindow;
268begin
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;
276end;
277
278function TDesktop.CreateCanvas: TCanvas;
279begin
280 Inc(CanvasLastId);
281 Result := TCanvas.Create;
282 Result.Id := CanvasLastId;
283 Canvases.Add(Result);
284end;
285
286function TDesktop.FindObjectById(Id: Integer): TGraphicObject;
287var
288 I: Integer;
289begin
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;
294end;
295
296function TDesktop.FindCanvasById(Id: Integer): TCanvas;
297var
298 I: Integer;
299begin
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;
304end;
305
306procedure TDesktop.Paint;
307var
308 I: Integer;
309begin
310 for I := 0 to Objects.Count - 1 do
311 TGraphicObject(Objects[I]).Paint;
312end;
313
314constructor TDesktop.Create;
315begin
316 Objects := TObjectList<TGraphicObject>.Create;
317 Canvases := TObjectList<TCanvas>.Create;
318end;
319
320destructor TDesktop.Destroy;
321begin
322 FreeAndNil(Canvases);
323 FreeAndNil(Objects);
324 inherited;
325end;
326
327end.
328
Note: See TracBrowser for help on using the repository browser.