Changeset 22 for branches/overos/UControls.pas
- Timestamp:
- Dec 30, 2018, 1:01:14 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/overos/UControls.pas
r21 r22 14 14 TControl = class 15 15 private 16 FOnClick: TNotifyEvent; 16 17 FParentControl: TControl; 18 FRectangle: TRectangle; 17 19 FVisible: Boolean; 20 function GetPosition: TPosition; 21 function GetSize: TSize; 18 22 procedure SetParentControl(AValue: TControl); 19 procedure SetVisible(AValue: Boolean); 23 procedure SetPosition(AValue: TPosition); 24 procedure SetSize(AValue: TSize); 25 protected 26 procedure SetRectangle(AValue: TRectangle); virtual; 27 procedure SetVisible(AValue: Boolean); virtual; 20 28 public 21 29 Canvas: TCanvas; 22 Rectangle: TRectangle;23 30 Controls: TFPGObjectList<TControl>; 24 31 procedure MouseButtonDown(Pos: TPosition; Button: TMouseButton); virtual; … … 30 37 property ParentControl: TControl read FParentControl write SetParentControl; 31 38 property Visible: Boolean read FVisible write SetVisible; 39 property Position: TPosition read GetPosition write SetPosition; 40 property Size: TSize read GetSize write SetSize; 41 property Rectangle: TRectangle read FRectangle write SetRectangle; 42 property OnClick: TNotifyEvent read FOnClick write FOnClick; 32 43 end; 33 44 … … 39 50 procedure DrawArea(Rect: TRectangle; Color: TColor); override; 40 51 procedure DrawText(P: TPosition; Color: TColor; Text: string); override; 52 function GetTextSize(Text: string): TSize; override; 41 53 end; 42 54 … … 47 59 FClicked: Boolean; 48 60 FTitle: string; 49 procedure MouseButtonDown(Pos: TPosition; Button: TMouseButton); override;50 procedure MouseButtonUp(Pos: TPosition; Button: TMouseButton); override;51 61 procedure SetClicked(AValue: Boolean); 52 62 procedure SetTitle(AValue: string); 53 63 public 64 procedure MouseButtonDown(Pos: TPosition; Button: TMouseButton); override; 65 procedure MouseButtonUp(Pos: TPosition; Button: TMouseButton); override; 54 66 procedure Paint; override; 55 67 property Clicked: Boolean read FClicked write SetClicked; … … 85 97 procedure TCanvasControl.DrawLine(P1, P2: TPosition; Color: TColor); 86 98 begin 87 Control.ParentControl.Canvas.DrawLine(P1 + Control.Rectangle.Position, P2 + Control.Rectangle.Position, Color); 99 if Assigned(Control) and Assigned(Control.ParentControl) then 100 Control.ParentControl.Canvas.DrawLine(P1 + Control.Rectangle.Position, P2 + Control.Rectangle.Position, Color); 88 101 end; 89 102 90 103 procedure TCanvasControl.DrawArea(Rect: TRectangle; Color: TColor); 91 104 begin 92 Control.ParentControl.Canvas.DrawArea(TRectangle.Create(Rect.Position + Control.Rectangle.Position, 93 Rect.Size), Color); 105 if Assigned(Control) and Assigned(Control.ParentControl) then 106 Control.ParentControl.Canvas.DrawArea(TRectangle.Create(Rect.Position + Control.Rectangle.Position, 107 Rect.Size), Color); 94 108 end; 95 109 96 110 procedure TCanvasControl.DrawText(P: TPosition; Color: TColor; Text: string); 97 111 begin 98 Control.ParentControl.Canvas.DrawText(P + Control.Rectangle.Position, Color, Text); 112 if Assigned(Control) and Assigned(Control.ParentControl) then 113 Control.ParentControl.Canvas.DrawText(P + Control.Rectangle.Position, Color, Text); 114 end; 115 116 function TCanvasControl.GetTextSize(Text: string): TSize; 117 begin 118 if Assigned(Control) and Assigned(Control.ParentControl) then 119 Result := Control.ParentControl.Canvas.GetTextSize(Text); 99 120 end; 100 121 … … 105 126 if FVisible = AValue then Exit; 106 127 FVisible := AValue; 107 Paint; 128 if not FVisible then begin 129 if Assigned(ParentControl) then ParentControl.Paint; 130 end else Paint; 108 131 end; 109 132 … … 128 151 Break; 129 152 end; 153 if Assigned(FOnClick) then 154 FOnClick(Self); 130 155 end; 131 156 … … 151 176 end; 152 177 178 function TControl.GetPosition: TPosition; 179 begin 180 Result := FRectangle.Position; 181 end; 182 183 function TControl.GetSize: TSize; 184 begin 185 Result := FRectangle.Size; 186 end; 187 188 procedure TControl.SetPosition(AValue: TPosition); 189 begin 190 Rectangle := TRectangle.Create(AValue, FRectangle.Size); 191 end; 192 193 procedure TControl.SetSize(AValue: TSize); 194 begin 195 Rectangle := TRectangle.Create(FRectangle.Position, AValue); 196 end; 197 198 procedure TControl.SetRectangle(AValue: TRectangle); 199 begin 200 if FRectangle = AValue then Exit; 201 FRectangle := AValue; 202 end; 203 153 204 procedure TControl.Paint; 154 205 var 155 206 I: Integer; 156 207 begin 157 for I := 0 to Controls.Count - 1 do 158 Controls[I].Paint; 208 if FVisible then begin 209 for I := 0 to Controls.Count - 1 do 210 Controls[I].Paint; 211 end; 159 212 end; 160 213 … … 168 221 169 222 destructor TControl.Destroy; 170 begin 223 var 224 I: Integer; 225 begin 226 for I := 0 to Controls.Count - 1 do 227 Controls[I].Free; 228 ParentControl := nil; 171 229 Canvas.Free; 172 230 Controls.Free; … … 185 243 procedure TEdit.Paint; 186 244 begin 187 Canvas.DrawArea(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), clBlack); 188 Canvas.DrawFrame(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), clWhite); 189 Canvas.DrawText(TPosition.Create(4, 4), clWhite, FText); 245 if FVisible then begin 246 Canvas.DrawArea(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), clBlack); 247 Canvas.DrawFrame(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), clWhite); 248 Canvas.DrawText(TPosition.Create(4, 4), clWhite, FText); 249 end; 190 250 inherited; 191 251 end; … … 202 262 procedure TLabel.Paint; 203 263 begin 204 Canvas.DrawText(TPosition.Create(0, 0), clWhite, FTitle); 264 if FVisible then begin 265 Canvas.DrawText(TPosition.Create(0, 0), clWhite, FTitle); 266 end; 205 267 inherited; 206 268 end; … … 237 299 var 238 300 Color: TColor; 239 begin 240 if Clicked then Color := clBlack 241 else Color := clGray; 242 Canvas.DrawArea(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), Color); 243 Canvas.DrawFrame(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), clWhite); 244 Canvas.DrawText(TPosition.Create(8, 8), clWhite, FTitle); 301 TextSize: TSize; 302 begin 303 if FVisible then begin 304 if Clicked then Color := clBlack 305 else Color := clGray; 306 Canvas.DrawArea(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), Color); 307 Canvas.DrawFrame(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), clWhite); 308 TextSize := Canvas.GetTextSize(FTitle); 309 Canvas.DrawText(TPosition.Create((Size.Width - TextSize.Width) div 2, 310 (Size.Height - TextSize.Height) div 2), clWhite, FTitle); 311 end; 245 312 inherited; 246 313 end;
Note:
See TracChangeset
for help on using the changeset viewer.