source: branches/overos/Os.Controls.pas

Last change on this file was 39, checked in by chronos, 11 months ago
  • Modified: Simplified unit names.
File size: 7.7 KB
Line 
1unit Os.Controls;
2
3interface
4
5uses
6 Classes, SysUtils, Os.Types, Os.Graphics, Generics.Collections, Os.Mouse;
7
8type
9
10 { TControl }
11
12 TControl = class
13 private
14 FOnClick: TNotifyEvent;
15 FParentControl: TControl;
16 FRectangle: TRectangle;
17 FVisible: Boolean;
18 function GetPosition: TPosition;
19 function GetSize: TSize;
20 procedure SetParentControl(AValue: TControl);
21 procedure SetPosition(AValue: TPosition);
22 procedure SetSize(AValue: TSize);
23 protected
24 procedure SetRectangle(AValue: TRectangle); virtual;
25 procedure SetVisible(AValue: Boolean); virtual;
26 public
27 Canvas: TCanvas;
28 Controls: TObjectList<TControl>;
29 procedure MouseButtonDown(Pos: TPosition; Button: TMouseButton); virtual;
30 procedure MouseButtonUp(Pos: TPosition; Button: TMouseButton); virtual;
31 procedure MouseMove(Pos: TPosition); virtual;
32 procedure Paint; virtual;
33 constructor Create; virtual;
34 destructor Destroy; override;
35 property ParentControl: TControl read FParentControl write SetParentControl;
36 property Visible: Boolean read FVisible write SetVisible;
37 property Position: TPosition read GetPosition write SetPosition;
38 property Size: TSize read GetSize write SetSize;
39 property Rectangle: TRectangle read FRectangle write SetRectangle;
40 property OnClick: TNotifyEvent read FOnClick write FOnClick;
41 end;
42
43 { TCanvasControl }
44
45 TCanvasControl = class(TCanvas)
46 Control: TControl;
47 procedure DrawLine(P1, P2: TPosition; Color: TColor); override;
48 procedure DrawArea(Rect: TRectangle; Color: TColor); override;
49 procedure DrawText(P: TPosition; Color: TColor; Text: string); override;
50 function GetTextSize(Text: string): TSize; override;
51 end;
52
53 { TButton }
54
55 TButton = class(TControl)
56 private
57 FClicked: Boolean;
58 FTitle: string;
59 procedure SetClicked(AValue: Boolean);
60 procedure SetTitle(AValue: string);
61 public
62 procedure MouseButtonDown(Pos: TPosition; Button: TMouseButton); override;
63 procedure MouseButtonUp(Pos: TPosition; Button: TMouseButton); override;
64 procedure Paint; override;
65 property Clicked: Boolean read FClicked write SetClicked;
66 property Title: string read FTitle write SetTitle;
67 end;
68
69 { TLabel }
70
71 TLabel = class(TControl)
72 private
73 FTitle: string;
74 procedure SetTitle(AValue: string);
75 public
76 procedure Paint; override;
77 property Title: string read FTitle write SetTitle;
78 end;
79
80 { TEdit }
81
82 TEdit = class(TControl)
83 private
84 FText: string;
85 procedure SetText(AValue: string);
86 public
87 procedure Paint; override;
88 property Text: string read FText write SetText;
89 end;
90
91
92implementation
93
94{ TCanvasControl }
95
96procedure TCanvasControl.DrawLine(P1, P2: TPosition; Color: TColor);
97begin
98 if Assigned(Control) and Assigned(Control.ParentControl) then
99 Control.ParentControl.Canvas.DrawLine(P1 + Control.Rectangle.Position, P2 + Control.Rectangle.Position, Color);
100end;
101
102procedure TCanvasControl.DrawArea(Rect: TRectangle; Color: TColor);
103begin
104 if Assigned(Control) and Assigned(Control.ParentControl) then
105 Control.ParentControl.Canvas.DrawArea(TRectangle.Create(Rect.Position + Control.Rectangle.Position,
106 Rect.Size), Color);
107end;
108
109procedure TCanvasControl.DrawText(P: TPosition; Color: TColor; Text: string);
110begin
111 if Assigned(Control) and Assigned(Control.ParentControl) then
112 Control.ParentControl.Canvas.DrawText(P + Control.Rectangle.Position, Color, Text);
113end;
114
115function TCanvasControl.GetTextSize(Text: string): TSize;
116begin
117 if Assigned(Control) and Assigned(Control.ParentControl) then
118 Result := Control.ParentControl.Canvas.GetTextSize(Text);
119end;
120
121{ TControl }
122
123procedure TControl.SetVisible(AValue: Boolean);
124begin
125 if FVisible = AValue then Exit;
126 FVisible := AValue;
127 if not FVisible then begin
128 if Assigned(ParentControl) then ParentControl.Paint;
129 end else Paint;
130end;
131
132procedure TControl.MouseButtonDown(Pos: TPosition; Button: TMouseButton);
133var
134 I: Integer;
135begin
136 for I := Controls.Count - 1 downto 0 do
137 if Controls[I].Rectangle.Contains(Pos) then begin
138 Controls[I].MouseButtonDown(Pos - Controls[I].Rectangle.Position, Button);
139 Break;
140 end;
141end;
142
143procedure TControl.MouseButtonUp(Pos: TPosition; Button: TMouseButton);
144var
145 I: Integer;
146begin
147 for I := Controls.Count - 1 downto 0 do
148 if Controls[I].Rectangle.Contains(Pos) then begin
149 Controls[I].MouseButtonUp(Pos - Controls[I].Rectangle.Position, Button);
150 Break;
151 end;
152 if Assigned(FOnClick) then
153 FOnClick(Self);
154end;
155
156procedure TControl.MouseMove(Pos: TPosition);
157var
158 I: Integer;
159begin
160 for I := Controls.Count - 1 downto 0 do
161 if Controls[I].Rectangle.Contains(Pos) then begin
162 Controls[I].MouseMove(Pos - Controls[I].Rectangle.Position);
163 Break;
164 end;
165end;
166
167procedure TControl.SetParentControl(AValue: TControl);
168begin
169 if FParentControl = AValue then Exit;
170 if Assigned(FParentControl) then
171 FParentControl.Controls.Remove(Self);
172 FParentControl := AValue;
173 if Assigned(FParentControl) then
174 FParentControl.Controls.Add(Self);
175end;
176
177function TControl.GetPosition: TPosition;
178begin
179 Result := FRectangle.Position;
180end;
181
182function TControl.GetSize: TSize;
183begin
184 Result := FRectangle.Size;
185end;
186
187procedure TControl.SetPosition(AValue: TPosition);
188begin
189 Rectangle := TRectangle.Create(AValue, FRectangle.Size);
190end;
191
192procedure TControl.SetSize(AValue: TSize);
193begin
194 Rectangle := TRectangle.Create(FRectangle.Position, AValue);
195end;
196
197procedure TControl.SetRectangle(AValue: TRectangle);
198begin
199 if FRectangle = AValue then Exit;
200 FRectangle := AValue;
201end;
202
203procedure TControl.Paint;
204var
205 I: Integer;
206begin
207 if FVisible then begin
208 for I := 0 to Controls.Count - 1 do
209 Controls[I].Paint;
210 end;
211end;
212
213constructor TControl.Create;
214begin
215 Controls := TObjectList<TControl>.Create;
216 Controls.OwnsObjects := False;
217 Canvas := TCanvasControl.Create;
218 TCanvasControl(Canvas).Control := Self;
219end;
220
221destructor TControl.Destroy;
222var
223 I: Integer;
224begin
225 for I := 0 to Controls.Count - 1 do
226 Controls[I].Free;
227 ParentControl := nil;
228 FreeAndNil(Canvas);
229 FreeAndNil(Controls);
230 inherited;
231end;
232
233{ TEdit }
234
235procedure TEdit.SetText(AValue: string);
236begin
237 if FText = AValue then Exit;
238 FText := AValue;
239 Paint;
240end;
241
242procedure TEdit.Paint;
243begin
244 if FVisible then begin
245 Canvas.DrawArea(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), clBlack);
246 Canvas.DrawFrame(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), clWhite);
247 Canvas.DrawText(TPosition.Create(4, 4), clWhite, FText);
248 end;
249 inherited;
250end;
251
252{ TLabel }
253
254procedure TLabel.SetTitle(AValue: string);
255begin
256 if FTitle = AValue then Exit;
257 FTitle := AValue;
258 Paint;
259end;
260
261procedure TLabel.Paint;
262begin
263 if FVisible then begin
264 Canvas.DrawText(TPosition.Create(0, 0), clWhite, FTitle);
265 end;
266 inherited;
267end;
268
269{ TButton }
270
271procedure TButton.SetTitle(AValue: string);
272begin
273 if FTitle = AValue then Exit;
274 FTitle := AValue;
275 Paint;
276end;
277
278procedure TButton.MouseButtonDown(Pos: TPosition; Button: TMouseButton);
279begin
280 inherited;
281 Clicked := True;
282end;
283
284procedure TButton.MouseButtonUp(Pos: TPosition; Button: TMouseButton);
285begin
286 inherited;
287 Clicked := False;
288end;
289
290procedure TButton.SetClicked(AValue: Boolean);
291begin
292 if FClicked = AValue then Exit;
293 FClicked := AValue;
294 Paint;
295end;
296
297procedure TButton.Paint;
298var
299 Color: TColor;
300 TextSize: TSize;
301begin
302 if FVisible then begin
303 if Clicked then Color := clBlack
304 else Color := clGray;
305 Canvas.DrawArea(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), Color);
306 Canvas.DrawFrame(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), clWhite);
307 TextSize := Canvas.GetTextSize(FTitle);
308 Canvas.DrawText(TPosition.Create((Size.Width - TextSize.Width) div 2,
309 (Size.Height - TextSize.Height) div 2), clWhite, FTitle);
310 end;
311 inherited;
312end;
313
314end.
315
Note: See TracBrowser for help on using the repository browser.