Changeset 13 for trunk/Packages/Kernel/UGraphics.pas
- Timestamp:
- Sep 25, 2017, 2:48:08 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Kernel/UGraphics.pas
r11 r13 6 6 7 7 uses 8 Classes,SysUtils, fgl;8 SysUtils, fgl; 9 9 10 10 type 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 12 35 Position: TPoint; 13 36 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; 14 60 end; 15 61 … … 17 63 18 64 TGraphicObject = class 65 private 66 FCanvas: TCanvas; 67 function GetCanvas: TCanvas; 68 public 69 Desktop: TDesktop; 19 70 Id: Integer; 20 71 Visible: Boolean; 21 72 procedure Paint; virtual; 73 property Canvas: TCanvas read GetCanvas; 22 74 end; 23 75 … … 25 77 26 78 TWindow = class(TGraphicObject) 79 public 27 80 Title: string; 28 81 Bounds: TRectangle; … … 43 96 44 97 TDesktop = class 98 private 99 ObjectLastId: Integer; 100 CanvasLastId: Integer; 101 public 45 102 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; 46 108 procedure Paint; 47 109 constructor Create; … … 51 113 implementation 52 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 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 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 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.SetPixel(P: TPoint; Color: TColor); 218 begin 219 if Assigned(Parent) then 220 Parent.SetPixel(P.Add(Position), Color); 221 end; 222 53 223 { TGraphicObject } 54 224 225 function TGraphicObject.GetCanvas: TCanvas; 226 begin 227 FCanvas := Desktop.CreateCanvas; 228 Result := FCanvas; 229 end; 230 55 231 procedure TGraphicObject.Paint; 56 232 begin 57 58 233 end; 59 234 … … 63 238 begin 64 239 inherited Paint; 240 Canvas.DrawRect(Bounds, $ff0000); 65 241 end; 66 242 67 243 constructor TWindow.Create; 68 244 begin 69 Bounds := TRectangle.Create;70 245 end; 71 246 72 247 destructor TWindow.Destroy; 73 248 begin 74 Bounds.Free;75 249 inherited Destroy; 76 250 end; 77 251 78 252 { TDesktop } 253 254 function TDesktop.CreateWindow: TWindow; 255 begin 256 Inc(ObjectLastId); 257 Result := TWindow.Create; 258 Result.Id := ObjectLastId; 259 Objects.Add(Result); 260 end; 261 262 function TDesktop.CreateCanvas: TCanvas; 263 begin 264 Inc(CanvasLastId); 265 Result := TCanvas.Create; 266 Result.Id := CanvasLastId; 267 Canvases.Add(Result); 268 end; 269 270 function TDesktop.FindObjectById(Id: Integer): TGraphicObject; 271 var 272 I: Integer; 273 begin 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; 278 end; 279 280 function TDesktop.FindCanvasById(Id: Integer): TCanvas; 281 var 282 I: Integer; 283 begin 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; 288 end; 79 289 80 290 procedure TDesktop.Paint; … … 89 299 begin 90 300 Objects := TFPGObjectList<TGraphicObject>.Create; 301 Canvases := TFPGObjectList<TCanvas>.Create; 91 302 end; 92 303 93 304 destructor TDesktop.Destroy; 94 305 begin 95 Objects.Free; 306 FreeAndNil(Canvases); 307 FreeAndNil(Objects); 96 308 inherited Destroy; 97 309 end;
Note:
See TracChangeset
for help on using the changeset viewer.