1 | unit Os.Controls;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Os.Types, Os.Graphics, Generics.Collections, Os.Mouse;
|
---|
7 |
|
---|
8 | type
|
---|
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 |
|
---|
92 | implementation
|
---|
93 |
|
---|
94 | { TCanvasControl }
|
---|
95 |
|
---|
96 | procedure TCanvasControl.DrawLine(P1, P2: TPosition; Color: TColor);
|
---|
97 | begin
|
---|
98 | if Assigned(Control) and Assigned(Control.ParentControl) then
|
---|
99 | Control.ParentControl.Canvas.DrawLine(P1 + Control.Rectangle.Position, P2 + Control.Rectangle.Position, Color);
|
---|
100 | end;
|
---|
101 |
|
---|
102 | procedure TCanvasControl.DrawArea(Rect: TRectangle; Color: TColor);
|
---|
103 | begin
|
---|
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);
|
---|
107 | end;
|
---|
108 |
|
---|
109 | procedure TCanvasControl.DrawText(P: TPosition; Color: TColor; Text: string);
|
---|
110 | begin
|
---|
111 | if Assigned(Control) and Assigned(Control.ParentControl) then
|
---|
112 | Control.ParentControl.Canvas.DrawText(P + Control.Rectangle.Position, Color, Text);
|
---|
113 | end;
|
---|
114 |
|
---|
115 | function TCanvasControl.GetTextSize(Text: string): TSize;
|
---|
116 | begin
|
---|
117 | if Assigned(Control) and Assigned(Control.ParentControl) then
|
---|
118 | Result := Control.ParentControl.Canvas.GetTextSize(Text);
|
---|
119 | end;
|
---|
120 |
|
---|
121 | { TControl }
|
---|
122 |
|
---|
123 | procedure TControl.SetVisible(AValue: Boolean);
|
---|
124 | begin
|
---|
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;
|
---|
130 | end;
|
---|
131 |
|
---|
132 | procedure TControl.MouseButtonDown(Pos: TPosition; Button: TMouseButton);
|
---|
133 | var
|
---|
134 | I: Integer;
|
---|
135 | begin
|
---|
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;
|
---|
141 | end;
|
---|
142 |
|
---|
143 | procedure TControl.MouseButtonUp(Pos: TPosition; Button: TMouseButton);
|
---|
144 | var
|
---|
145 | I: Integer;
|
---|
146 | begin
|
---|
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);
|
---|
154 | end;
|
---|
155 |
|
---|
156 | procedure TControl.MouseMove(Pos: TPosition);
|
---|
157 | var
|
---|
158 | I: Integer;
|
---|
159 | begin
|
---|
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;
|
---|
165 | end;
|
---|
166 |
|
---|
167 | procedure TControl.SetParentControl(AValue: TControl);
|
---|
168 | begin
|
---|
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);
|
---|
175 | end;
|
---|
176 |
|
---|
177 | function TControl.GetPosition: TPosition;
|
---|
178 | begin
|
---|
179 | Result := FRectangle.Position;
|
---|
180 | end;
|
---|
181 |
|
---|
182 | function TControl.GetSize: TSize;
|
---|
183 | begin
|
---|
184 | Result := FRectangle.Size;
|
---|
185 | end;
|
---|
186 |
|
---|
187 | procedure TControl.SetPosition(AValue: TPosition);
|
---|
188 | begin
|
---|
189 | Rectangle := TRectangle.Create(AValue, FRectangle.Size);
|
---|
190 | end;
|
---|
191 |
|
---|
192 | procedure TControl.SetSize(AValue: TSize);
|
---|
193 | begin
|
---|
194 | Rectangle := TRectangle.Create(FRectangle.Position, AValue);
|
---|
195 | end;
|
---|
196 |
|
---|
197 | procedure TControl.SetRectangle(AValue: TRectangle);
|
---|
198 | begin
|
---|
199 | if FRectangle = AValue then Exit;
|
---|
200 | FRectangle := AValue;
|
---|
201 | end;
|
---|
202 |
|
---|
203 | procedure TControl.Paint;
|
---|
204 | var
|
---|
205 | I: Integer;
|
---|
206 | begin
|
---|
207 | if FVisible then begin
|
---|
208 | for I := 0 to Controls.Count - 1 do
|
---|
209 | Controls[I].Paint;
|
---|
210 | end;
|
---|
211 | end;
|
---|
212 |
|
---|
213 | constructor TControl.Create;
|
---|
214 | begin
|
---|
215 | Controls := TObjectList<TControl>.Create;
|
---|
216 | Controls.OwnsObjects := False;
|
---|
217 | Canvas := TCanvasControl.Create;
|
---|
218 | TCanvasControl(Canvas).Control := Self;
|
---|
219 | end;
|
---|
220 |
|
---|
221 | destructor TControl.Destroy;
|
---|
222 | var
|
---|
223 | I: Integer;
|
---|
224 | begin
|
---|
225 | for I := 0 to Controls.Count - 1 do
|
---|
226 | Controls[I].Free;
|
---|
227 | ParentControl := nil;
|
---|
228 | FreeAndNil(Canvas);
|
---|
229 | FreeAndNil(Controls);
|
---|
230 | inherited;
|
---|
231 | end;
|
---|
232 |
|
---|
233 | { TEdit }
|
---|
234 |
|
---|
235 | procedure TEdit.SetText(AValue: string);
|
---|
236 | begin
|
---|
237 | if FText = AValue then Exit;
|
---|
238 | FText := AValue;
|
---|
239 | Paint;
|
---|
240 | end;
|
---|
241 |
|
---|
242 | procedure TEdit.Paint;
|
---|
243 | begin
|
---|
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;
|
---|
250 | end;
|
---|
251 |
|
---|
252 | { TLabel }
|
---|
253 |
|
---|
254 | procedure TLabel.SetTitle(AValue: string);
|
---|
255 | begin
|
---|
256 | if FTitle = AValue then Exit;
|
---|
257 | FTitle := AValue;
|
---|
258 | Paint;
|
---|
259 | end;
|
---|
260 |
|
---|
261 | procedure TLabel.Paint;
|
---|
262 | begin
|
---|
263 | if FVisible then begin
|
---|
264 | Canvas.DrawText(TPosition.Create(0, 0), clWhite, FTitle);
|
---|
265 | end;
|
---|
266 | inherited;
|
---|
267 | end;
|
---|
268 |
|
---|
269 | { TButton }
|
---|
270 |
|
---|
271 | procedure TButton.SetTitle(AValue: string);
|
---|
272 | begin
|
---|
273 | if FTitle = AValue then Exit;
|
---|
274 | FTitle := AValue;
|
---|
275 | Paint;
|
---|
276 | end;
|
---|
277 |
|
---|
278 | procedure TButton.MouseButtonDown(Pos: TPosition; Button: TMouseButton);
|
---|
279 | begin
|
---|
280 | inherited;
|
---|
281 | Clicked := True;
|
---|
282 | end;
|
---|
283 |
|
---|
284 | procedure TButton.MouseButtonUp(Pos: TPosition; Button: TMouseButton);
|
---|
285 | begin
|
---|
286 | inherited;
|
---|
287 | Clicked := False;
|
---|
288 | end;
|
---|
289 |
|
---|
290 | procedure TButton.SetClicked(AValue: Boolean);
|
---|
291 | begin
|
---|
292 | if FClicked = AValue then Exit;
|
---|
293 | FClicked := AValue;
|
---|
294 | Paint;
|
---|
295 | end;
|
---|
296 |
|
---|
297 | procedure TButton.Paint;
|
---|
298 | var
|
---|
299 | Color: TColor;
|
---|
300 | TextSize: TSize;
|
---|
301 | begin
|
---|
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;
|
---|
312 | end;
|
---|
313 |
|
---|
314 | end.
|
---|
315 |
|
---|