Debian Codegraph @

src/ui/castlecontrols_checkbox.inc

raw
1 {%MainUnit castlecontrols.pas}
2 {
3 Copyright 2018-2023 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 {$ifdef read_interface}
18
19 { Checkbox with a caption. }
20 TCastleCheckbox = class(TCastleUserInterface)
21 strict private
22 FChecked, FPressed: Boolean;
23 FCaption: String;
24 FOnChange: TNotifyEvent;
25 FCaptionMargin: Single;
26 FImageContainer: TCastleUserInterface;
27 FImageSquareEmpty, FImageSquareChecked, FImagePressedBackground: TCastleImageControl;
28 FLabelCaption: TCastleLabel;
29 FTextColor, FCheckboxColor: TCastleColor;
30 FAutoSize: Boolean;
31 FFontSize: Single;
32 FCheckboxSize: Single;
33 FCaptionTranslate: Boolean;
34 procedure SetChecked(const Value: Boolean);
35 procedure SetCaption(const Value: String);
36 procedure SetTextColor(const Value: TCastleColor);
37 procedure SetCheckboxColor(const Value: TCastleColor);
38 procedure SetCaptionMargin(const Value: Single);
39 procedure SetAutoSize(const Value: Boolean);
40 procedure SetFontSize(const Value: Single);
41 procedure SetCheckboxSize(const Value: Single);
42 function GetCustomFont: TCastleAbstractFont;
43 procedure SetCustomFont(const Value: TCastleAbstractFont);
44 protected
45 procedure PreferredSize(var PreferredWidth, PreferredHeight: Single); override;
46 function GetInternalText: String; override;
47 procedure SetInternalText(const Value: String); override;
48 procedure DoChange; virtual;
49 procedure TranslateProperties(const TranslatePropertyEvent: TTranslatePropertyEvent); override;
50 public
51 const
52 DefaultCaptionMargin = 10;
53 DefaultCheckboxSize = 24;
54 constructor Create(AOwner: TComponent); override;
55 destructor Destroy; override;
56 function Press(const Event: TInputPressRelease): boolean; override;
57 function Release(const Event: TInputPressRelease): boolean; override;
58 function PropertySections(const PropertyName: String): TPropertySections; override;
59 procedure EditorAllowResize(
60 out ResizeWidth, ResizeHeight: Boolean; out Reason: String); override;
61
62 { Color of the label. By default opaque black. }
63 property TextColor: TCastleColor read FTextColor write SetTextColor;
64 { Color of the checkbox square and checkmark. By default opaque black. }
65 property CheckboxColor: TCastleColor read FCheckboxColor write SetCheckboxColor;
66 published
67 property Checked: Boolean read FChecked write SetChecked default false;
68
69 property Caption: String read FCaption write SetCaption;
70 { Should the @link(Caption) be localized (translated into other languages).
71 Determines if the property is enumerated by @link(TCastleComponent.TranslateProperties),
72 which affects the rest of localization routines. }
73 property CaptionTranslate: Boolean read FCaptionTranslate write FCaptionTranslate default true;
74 { Margin between checkbox square and a text @link(Caption). }
75 property CaptionMargin: Single read FCaptionMargin write SetCaptionMargin
76 {$ifdef FPC}default DefaultCaptionMargin{$endif};
77
78 { Event sent when @link(Checked) value was changed by a user click.
79 Note that this is @italic(not) called when you change @link(Checked)
80 property programmatically. }
81 property OnChange: TNotifyEvent read FOnChange write FOnChange;
82 { Should our size be determined by checkbox image size and caption. }
83 property AutoSize: Boolean read FAutoSize write SetAutoSize default true;
84 { Caption font size. }
85 property FontSize: Single read FFontSize write SetFontSize {$ifdef FPC}default 0.0{$endif};
86 { Size of the square checkbox. }
87 property CheckboxSize: Single read FCheckboxSize write SetCheckboxSize
88 {$ifdef FPC}default DefaultCheckboxSize{$endif};
89
90 { Font used for the caption.
91 When this is @nil then the @link(TCastleContainer.DefaultFont) is used,
92 or (when @link(TCastleContainer.DefaultFont) is also @nil) the global @link(UIFont). }
93 property CustomFont: TCastleAbstractFont
94 read GetCustomFont write SetCustomFont;
95
96 {$define read_interface_class}
97 {$I auto_generated_persistent_vectors/tcastlecheckbox_persistent_vectors.inc}
98 {$undef read_interface_class}
99 end;
100
101 {$endif read_interface}
102
103 {$ifdef read_implementation}
104
105 { TCastleCheckbox ---------------------------------------------------------- }
106
107 constructor TCastleCheckbox.Create(AOwner: TComponent);
108 begin
109 inherited;
110 FCaptionTranslate := true;
111 FCaptionMargin := DefaultCaptionMargin;
112 FAutoSize := true;
113 FTextColor := Black;
114 FCheckboxColor := Black;
115 FCheckboxSize := DefaultCheckboxSize;
116
117 FImageContainer := TCastleUserInterface.Create(Self);
118 FImageContainer.SetTransient;
119 FImageContainer.Width := FCheckboxSize;
120 FImageContainer.Height := FCheckboxSize;
121 FImageContainer.Anchor(vpMiddle);
122 InsertFront(FImageContainer);
123
124 { TODO: Use
125 Theme.Corners[tiSquarePressedBackground]
126 Theme.Corners[tiSquareEmpty]
127 Theme.Corners[tiSquareChecked]
128 Make sure they cost nothing when corners are 0. }
129
130 FImagePressedBackground := TCastleImageControl.Create(Self);
131 FImagePressedBackground.SetTransient;
132 FImagePressedBackground.Image := Theme.ImagesPersistent[tiSquarePressedBackground].Image;
133 FImagePressedBackground.OwnsImage := false;
134 FImagePressedBackground.Color := FCheckboxColor;
135 FImagePressedBackground.Exists := FPressed;
136 FImagePressedBackground.Stretch := true;
137 FImagePressedBackground.FullSize := true; // size is determined by FImageContainer
138 FImageContainer.InsertFront(FImagePressedBackground);
139
140 FImageSquareEmpty := TCastleImageControl.Create(Self);
141 FImageSquareEmpty.SetTransient;
142 FImageSquareEmpty.Image := Theme.ImagesPersistent[tiSquareEmpty].Image;
143 FImageSquareEmpty.OwnsImage := false;
144 FImageSquareEmpty.Color := FCheckboxColor;
145 FImageSquareEmpty.Exists := not FChecked;
146 FImageSquareEmpty.Stretch := true;
147 FImageSquareEmpty.FullSize := true; // size is determined by FImageContainer
148 FImageContainer.InsertFront(FImageSquareEmpty);
149
150 FImageSquareChecked := TCastleImageControl.Create(Self);
151 FImageSquareChecked.SetTransient;
152 FImageSquareChecked.Image := Theme.ImagesPersistent[tiSquareChecked].Image;
153 FImageSquareChecked.OwnsImage := false;
154 FImageSquareChecked.Color := FCheckboxColor;
155 FImageSquareChecked.Exists := FChecked;
156 FImageSquareChecked.Stretch := true;
157 FImageSquareChecked.FullSize := true; // size is determined by FImageContainer
158 FImageContainer.InsertFront(FImageSquareChecked);
159
160 FLabelCaption := TCastleLabel.Create(Self);
161 FLabelCaption.SetTransient;
162 FLabelCaption.Color := FTextColor;
163 FLabelCaption.Anchor(hpLeft, FImageContainer.EffectiveWidth + CaptionMargin);
164 FLabelCaption.Anchor(vpMiddle);
165 InsertFront(FLabelCaption);
166
167 {$define read_implementation_constructor}
168 {$I auto_generated_persistent_vectors/tcastlecheckbox_persistent_vectors.inc}
169 {$undef read_implementation_constructor}
170 end;
171
172 destructor TCastleCheckbox.Destroy;
173 begin
174 {$define read_implementation_destructor}
175 {$I auto_generated_persistent_vectors/tcastlecheckbox_persistent_vectors.inc}
176 {$undef read_implementation_destructor}
177 inherited;
178 end;
179
180 procedure TCastleCheckbox.PreferredSize(var PreferredWidth, PreferredHeight: Single);
181 begin
182 if AutoSize then
183 begin
184 PreferredWidth := UIScale * (
185 FImageContainer.EffectiveWidth + CaptionMargin +
186 FLabelCaption.EffectiveWidth);
187 PreferredHeight := UIScale *
188 Max(FImageContainer.EffectiveHeight, FLabelCaption.EffectiveHeight);
189 end;
190 end;
191
192 procedure TCastleCheckbox.EditorAllowResize(
193 out ResizeWidth, ResizeHeight: Boolean; out Reason: String);
194 begin
195 inherited;
196 if AutoSize then
197 begin
198 ResizeWidth := false;
199 ResizeHeight := false;
200 Reason := SAppendPart(Reason, NL, 'Turn off "TCastleCheckbox.AutoSize" to change size.');
201 end;
202 end;
203
204 procedure TCastleCheckbox.SetChecked(const Value: boolean);
205 begin
206 if FChecked <> Value then
207 begin
208 FChecked := Value;
209 FImageSquareEmpty.Exists := not FChecked;
210 FImageSquareChecked.Exists := FChecked;
211 { Note that we don't call "VisibleChange([chRender])" here,
212 because the change to "FImageSquareChecked.Exists" should already call it.
213 In general, since TCastleCheckbox doesn't override Render method,
214 it doesn't need to worry about calling "VisibleChange([chRender])". }
215 end;
216 end;
217
218 function TCastleCheckbox.Press(const Event: TInputPressRelease): boolean;
219 begin
220 Result := inherited;
221 if Result then Exit;
222
223 if Event.IsMouseButton(buttonLeft) or
224 Event.IsKey(keySpace) or
225 Event.IsKey(keyEnter) then
226 begin
227 FPressed := true;
228 FImagePressedBackground.Exists := FPressed;
229 Exit(true);
230 end;
231 end;
232
233 function TCastleCheckbox.Release(const Event: TInputPressRelease): boolean;
234 begin
235 Result := inherited;
236 if Result then Exit;
237
238 if FPressed and (
239 Event.IsMouseButton(buttonLeft) or
240 Event.IsKey(keySpace) or
241 Event.IsKey(keyEnter)) then
242 begin
243 FPressed := false;
244 FImagePressedBackground.Exists := FPressed;
245
246 { Because of mouse capture mechanism in TCastleContainer,
247 we receive Release notifications about all clicks that started
248 within our rect, regardless where they finished.
249
250 Just like TCastleButton, the click only counts
251 if it happens over our rect. }
252 if CapturesEventsAtPosition(Event.Position) then
253 begin
254 // change using property, so FImageSquareChecked/Empty.Exists is switched
255 Checked := not Checked;
256 DoChange;
257 end;
258
259 Exit(true);
260 end;
261 end;
262
263 procedure TCastleCheckbox.SetCaption(const Value: String);
264 begin
265 if FCaption <> Value then
266 begin
267 FCaption := Value;
268 FLabelCaption.Caption := Value;
269 end;
270 end;
271
272 procedure TCastleCheckbox.SetTextColor(const Value: TCastleColor);
273 begin
274 if not TCastleColor.PerfectlyEquals(FTextColor, Value) then
275 begin
276 FTextColor := Value;
277 FLabelCaption.Color := Value;
278 end;
279 end;
280
281 procedure TCastleCheckbox.SetCheckboxColor(const Value: TCastleColor);
282 begin
283 if not TCastleColor.PerfectlyEquals(FCheckboxColor, Value) then
284 begin
285 FCheckboxColor := Value;
286 FImageSquareEmpty.Color := Value;
287 FImageSquareChecked.Color := Value;
288 FImagePressedBackground.Color := Value;
289 end;
290 end;
291
292 procedure TCastleCheckbox.SetCheckboxSize(const Value: Single);
293 begin
294 if FCheckboxSize <> Value then
295 begin
296 FCheckboxSize := Value;
297 FImageContainer.Width := Value;
298 FImageContainer.Height := Value;
299 FLabelCaption.Anchor(hpLeft, FImageContainer.EffectiveWidth + CaptionMargin);
300 end;
301 end;
302
303 procedure TCastleCheckbox.SetCaptionMargin(const Value: Single);
304 begin
305 if FCaptionMargin <> Value then
306 begin
307 FCaptionMargin := Value;
308 FLabelCaption.Anchor(hpLeft, FImageContainer.EffectiveWidth + CaptionMargin);
309 end;
310 end;
311
312 procedure TCastleCheckbox.SetFontSize(const Value: Single);
313 begin
314 if FFontSize <> Value then
315 begin
316 FFontSize := Value;
317 FLabelCaption.FontSize := Value;
318 end;
319 end;
320
321 procedure TCastleCheckbox.SetAutoSize(const Value: Boolean);
322 begin
323 if FAutoSize <> Value then
324 begin
325 FAutoSize := Value;
326 VisibleChange([chRectangle]); // our effective size changed now
327 end;
328 end;
329
330 function TCastleCheckbox.GetInternalText: String;
331 begin
332 Result := Caption;
333 end;
334
335 procedure TCastleCheckbox.SetInternalText(const Value: String);
336 begin
337 Caption := Value;
338 end;
339
340 function TCastleCheckbox.PropertySections(
341 const PropertyName: String): TPropertySections;
342 begin
343 if ArrayContainsString(PropertyName, [
344 'Caption', 'Checked', 'CustomFont', 'FontSize',
345 'CheckboxColorPersistent', 'TextColorPersistent'
346 ]) then
347 Result := [psBasic]
348 else
349 if ArrayContainsString(PropertyName, [
350 'AutoSize'
351 ]) then
352 Result := [psLayout]
353 else
354 Result := inherited PropertySections(PropertyName);
355 end;
356
357 procedure TCastleCheckbox.DoChange;
358 begin
359 if Assigned(FOnChange) then
360 FOnChange(Self);
361 end;
362
363 procedure TCastleCheckbox.TranslateProperties(
364 const TranslatePropertyEvent: TTranslatePropertyEvent);
365 var
366 S: String;
367 begin
368 if CaptionTranslate and (Caption <> '') then
369 begin
370 S := Caption;
371 TranslatePropertyEvent(Self, 'Caption', S);
372 Caption := S;
373 end;
374 end;
375
376 function TCastleCheckbox.GetCustomFont: TCastleAbstractFont;
377 begin
378 Result := FLabelCaption.CustomFont;
379 end;
380
381 procedure TCastleCheckbox.SetCustomFont(const Value: TCastleAbstractFont);
382 begin
383 { Note: we do not have local FCustomFont field in TCastleCheckbox,
384 as then we'd need free notification to nil it.
385 It is easier to just get/set FLabelCaption.CustomFont now. }
386 FLabelCaption.CustomFont := Value;
387 end;
388
389 {$define read_implementation_methods}
390 {$I auto_generated_persistent_vectors/tcastlecheckbox_persistent_vectors.inc}
391 {$undef read_implementation_methods}
392
393 {$endif read_implementation}