Debian Codegraph @

src/ui/castlecontrols_groups.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 { Abstract ancestor for containers that pack children,
20 like @link(TCastleHorizontalGroup) and @link(TCastleVerticalGroup). }
21 TCastlePackedGroup = class(TCastleUserInterface)
22 strict private
23 FFrame: boolean;
24 FFrameColor: TCastleColor;
25 FPadding: Single;
26 FSpacing: Single;
27 FInsideWidth, FInsideHeight: Single;
28 FAutoSizeWidth, FAutoSizeHeight: Boolean;
29 procedure SetFrameColor(const Value: TCastleColor);
30 procedure SetFrame(const Value: Boolean);
31 procedure SetPadding(const Value: Single);
32 procedure SetSpacing(const Value: Single);
33 procedure SetAutoSizeWidth(const Value: Boolean);
34 procedure SetAutoSizeHeight(const Value: Boolean);
35 procedure PackChildren;
36 protected
37 FPackingValid: boolean;
38 procedure DoPackChildren(out InsideWidth, InsideHeight: Single); virtual; abstract;
39 procedure PreferredSize(var PreferredWidth, PreferredHeight: Single); override;
40 procedure Loaded; override;
41 public
42 const
43 DefaultPadding = 0;
44 DefaultSpacing = 0;
45
46 constructor Create(AOwner: TComponent); override;
47 destructor Destroy; override;
48 procedure BeforeRender; override;
49 procedure Render; override;
50 procedure VisibleChange(const Changes: TCastleUserInterfaceChanges;
51 const ChangeInitiatedByChildren: boolean = false); override;
52 procedure EditorAllowResize(out ResizeWidth, ResizeHeight: Boolean;
53 out Reason: String); override;
54 function PropertySections(const PropertyName: String): TPropertySections; override;
55
56 { If @link(Frame), this is the frame's color. By default white. }
57 property FrameColor: TCastleColor read FFrameColor write SetFrameColor;
58 {$ifdef FPC}deprecated 'display frame using Border or parent UI control';{$endif}
59 published
60 property Frame: boolean read FFrame write SetFrame default false;
61 {$ifdef FPC}deprecated 'display frame using Border or parent UI control';{$endif}
62
63 property Padding: Single read FPadding write SetPadding {$ifdef FPC}default DefaultPadding{$endif};
64 property Spacing: Single read FSpacing write SetSpacing {$ifdef FPC}default DefaultSpacing{$endif};
65
66 { Whether the control's size is automatically adjusted to wrap children
67 tightly.
68
69 Be careful to not create a recursive loop! Group's size calculation
70 always looks at children size (even when auto-size is off,
71 since it needs to arrange children next to each other anyway).
72 This means that children size @italic(cannot) depend on a parent.
73 So a direct child of @link(TCastleVerticalGroup) or
74 @link(TCastleHorizontalGroup) @italic(cannot)
75 use @link(FullSize), or @link(WidthFraction) or @link(HeightFraction).
76 This would create a loop in calculations, which would be forcefully
77 resolved by some control having undefined size.
78
79 So, you can only deactivate auto-size on a group if all children
80 have explicitly defined size using their @link(Width) and @link(Height)
81 properties.
82 }
83 property AutoSizeWidth: Boolean
84 read FAutoSizeWidth write SetAutoSizeWidth default true;
85 property AutoSizeHeight: Boolean
86 read FAutoSizeHeight write SetAutoSizeHeight default true;
87
88 {$warnings off} // silence the fact that FrameColor is deprecated
89 {$define read_interface_class}
90 {$I auto_generated_persistent_vectors/tcastlepackedgroup_persistent_vectors.inc}
91 {$undef read_interface_class}
92 {$warnings on}
93 end;
94
95 { Container that packs the children horizontally.
96
97 The children are packed from left-to-right by default,
98 in the order determined by their back-to-front relation.
99 E.g. if you add all the children using @link(InsertFront),
100 then they will appear in the same order, left-to-right, in the control.
101 You can invert this by setting @link(LeftToRight) to @false. }
102 TCastleHorizontalGroup = class(TCastlePackedGroup)
103 strict private
104 FAlignment: TVerticalPosition;
105 FLeftToRight: boolean;
106 procedure SetLeftToRight(const Value: boolean);
107 procedure SetAlignment(const Value: TVerticalPosition);
108 protected
109 procedure DoPackChildren(out W, H: Single); override;
110 public
111 constructor Create(AOwner: TComponent); override;
112 function PropertySections(const PropertyName: String): TPropertySections; override;
113 published
114 { The alignment of children inside this control. }
115 property Alignment: TVerticalPosition
116 read FAlignment write SetAlignment default vpMiddle;
117 property LeftToRight: boolean read FLeftToRight write SetLeftToRight
118 default true;
119 end;
120
121 { Container that packs the children vertically.
122
123 The children are packed from top-to-bottom,
124 in the order determined by their back-to-front relation.
125 E.g. if you add all the children using @link(InsertFront),
126 then they will appear in the same order, top-to-bottom, in the control.
127 You can invert this by setting @link(TopToBottom) to @false. }
128 TCastleVerticalGroup = class(TCastlePackedGroup)
129 strict private
130 FAlignment: THorizontalPosition;
131 FTopToBottom: boolean;
132 procedure SetTopToBottom(const Value: boolean);
133 procedure SetAlignment(const Value: THorizontalPosition);
134 protected
135 procedure DoPackChildren(out W, H: Single); override;
136 public
137 constructor Create(AOwner: TComponent); override;
138 function PropertySections(const PropertyName: String): TPropertySections; override;
139 published
140 { The alignment of children inside this control. }
141 property Alignment: THorizontalPosition
142 read FAlignment write SetAlignment default hpLeft;
143 property TopToBottom: boolean read FTopToBottom write SetTopToBottom
144 default true;
145 end;
146
147 {$endif read_interface}
148
149 {$ifdef read_implementation}
150
151 { TCastlePackedGroup --------------------------------------------------------- }
152
153 constructor TCastlePackedGroup.Create(AOwner: TComponent);
154 begin
155 inherited;
156 FFrameColor := White;
157 FPadding := DefaultPadding;
158 FSpacing := DefaultSpacing;
159 FAutoSizeWidth := true;
160 FAutoSizeHeight := true;
161
162 {$warnings off} // silence the fact that FrameColor is deprecated
163 {$define read_implementation_constructor}
164 {$I auto_generated_persistent_vectors/tcastlepackedgroup_persistent_vectors.inc}
165 {$undef read_implementation_constructor}
166 {$warnings on}
167 end;
168
169 destructor TCastlePackedGroup.Destroy;
170 begin
171 {$define read_implementation_destructor}
172 {$I auto_generated_persistent_vectors/tcastlepackedgroup_persistent_vectors.inc}
173 {$undef read_implementation_destructor}
174 inherited;
175 end;
176
177 procedure TCastlePackedGroup.BeforeRender;
178 begin
179 inherited;
180
181 { We usually pack children inside TCastleXxxGroup.PreferredSize,
182 to set size early correctly.
183 That is, usually TCastleXxxGroup.PreferredSize calls PackChildren.
184
185 But in case group has FullSize=true, or AutoSizeWidth=AutoSizeHeight=false,
186 then PackChildren is not called from TCastleXxxGroup.PreferredSize.
187 And that's good, we don't want to call PackChildren then.
188 Because
189
190 - PackChildren adjusts children layout using anchors,
191 that refer to parent (group) sizes.
192
193 - using PackChildren from TCastleXxxGroup.PreferredSize means that children
194 need to know group size during TCastleXxxGroup.PreferredSize.
195 But the group size is now in the middle of being calculated...
196 so this causes warning "UI control ... encountered an endless loop
197 when trying to calculate it's size".
198
199 But in case of FullSize=true or AutoSizeWidth=AutoSizeHeight=false
200 the warning is actually not justified.
201 Group can calculate its size in this case *without calling PackChildren*.
202 And it does, so no loop occurs.
203
204 So in cases when we don't need to know FInsideWidth, FInsideHeight
205 during PreferredSize, we defer the call to PackChildren. }
206
207 PackChildren;
208 end;
209
210 procedure TCastlePackedGroup.Render;
211 begin
212 inherited;
213
214 {$warnings off} // using deprecated just to keep it working
215 if Frame then
216 Theme.Draw(RenderRect, tiGroup, UIScale, FrameColor);
217 {$warnings on}
218 end;
219
220 procedure TCastlePackedGroup.PackChildren;
221 begin
222 if FPackingValid then Exit;
223 { setting this to true early also prevents recursively entering DoPackChildren. }
224 FPackingValid := true;
225 DoPackChildren(FInsideWidth, FInsideHeight);
226 end;
227
228 procedure TCastlePackedGroup.PreferredSize(var PreferredWidth, PreferredHeight: Single);
229 begin
230 inherited;
231 if FAutoSizeWidth or FAutoSizeHeight then
232 begin
233 { When AutoSizeWidth = AutoSizeHeight = false,
234 then we don't need to know FInsideWidth/Height here,
235 and then we don't need to call PackChildren here.
236 It would actually cause problems: as it would that we mistakenly detect as
237 "UI calculation loop" something that is not:
238
239 - when TCastleXxxGroup uses WidthFraction <> 0 and HeightFraction <> 0
240 - and it has a child that uses WidthFraction <> 0 and HeightFraction <> 0
241 }
242 PackChildren;
243 if FAutoSizeWidth then
244 PreferredWidth := FInsideWidth * UIScale;
245 if FAutoSizeHeight then
246 PreferredHeight := FInsideHeight * UIScale;
247 end;
248
249 { Note that we don't set "FPackingValid := false" otherwise
250 (when "if FAutoSizeWidth or FAutoSizeHeight" is not satisfied).
251
252 As PreferredSize is called very often, calling "FPackingValid := false"
253 here would mean that we do "DoPackChildren" very often,
254 defeating the purpose of FPackingValid flag (which tries to conserve
255 DoPackChildren calls).
256
257 The "FPackingValid := false" will be set at resize and other events. }
258 end;
259
260 procedure TCastlePackedGroup.EditorAllowResize(
261 out ResizeWidth, ResizeHeight: Boolean; out Reason: String);
262 begin
263 inherited;
264 if AutoSizeWidth then
265 begin
266 ResizeWidth := false;
267 Reason := SAppendPart(Reason, NL, 'Turn off "TCastlePackedGroup.AutoSizeWidth" to change width.');
268 end;
269 if AutoSizeHeight then
270 begin
271 ResizeHeight := false;
272 Reason := SAppendPart(Reason, NL, 'Turn off "TCastlePackedGroup.AutoSizeHeight" to change height.');
273 end;
274 end;
275
276 procedure TCastlePackedGroup.SetFrameColor(const Value: TCastleColor);
277 begin
278 if not TCastleColor.PerfectlyEquals(FFrameColor, Value) then
279 begin
280 FFrameColor := Value;
281 VisibleChange([chRender]);
282 end;
283 end;
284
285 procedure TCastlePackedGroup.SetFrame(const Value: boolean);
286 begin
287 if FFrame <> Value then
288 begin
289 FFrame := Value;
290 VisibleChange([chRender]);
291 end;
292 end;
293
294 procedure TCastlePackedGroup.SetPadding(const Value: Single);
295 begin
296 if FPadding <> Value then
297 begin
298 FPadding := Value;
299 VisibleChange([chRectangle]);
300 end;
301 end;
302
303 procedure TCastlePackedGroup.SetSpacing(const Value: Single);
304 begin
305 if FSpacing <> Value then
306 begin
307 FSpacing := Value;
308 VisibleChange([chRectangle]);
309 end;
310 end;
311
312 procedure TCastlePackedGroup.VisibleChange(const Changes: TCastleUserInterfaceChanges;
313 const ChangeInitiatedByChildren: boolean);
314 begin
315 inherited;
316 if [chRectangle, chChildren, chChildrenExists] * Changes <> [] then
317 FPackingValid := false;
318 end;
319
320 procedure TCastlePackedGroup.SetAutoSizeWidth(const Value: Boolean);
321 begin
322 if FAutoSizeWidth <> Value then
323 begin
324 FAutoSizeWidth := Value;
325 VisibleChange([chRectangle]);
326 end;
327 end;
328
329 procedure TCastlePackedGroup.SetAutoSizeHeight(const Value: Boolean);
330 begin
331 if FAutoSizeHeight <> Value then
332 begin
333 FAutoSizeHeight := Value;
334 VisibleChange([chRectangle]);
335 end;
336 end;
337
338 function TCastlePackedGroup.PropertySections(
339 const PropertyName: String): TPropertySections;
340 begin
341 if (PropertyName = 'Spacing') then
342 Result := [psBasic, psLayout]
343 else
344 if (PropertyName = 'AutoSizeWidth') or
345 (PropertyName = 'AutoSizeHeight') then
346 Result := [psLayout]
347 else
348 Result := inherited PropertySections(PropertyName);
349 end;
350
351 procedure TCastlePackedGroup.Loaded;
352 begin
353 inherited;
354 {$warnings off} // using deprecated just to make a warning
355 if Frame or (not TVector4.PerfectlyEquals(White, FrameColor)) then
356 WritelnWarning('TCastle(Vertical|Horizontal)Group.Frame and FrameColor are deprecated. Instead: Use TCastleUserInterface.Border, or place the control under a component like TCastleRectangleControl or TCastleImageControl that defines a background.');
357 {$warnings on}
358 end;
359
360 {$warnings off} // silence the fact that FrameColor is deprecated
361 {$define read_implementation_methods}
362 {$I auto_generated_persistent_vectors/tcastlepackedgroup_persistent_vectors.inc}
363 {$undef read_implementation_methods}
364 {$warnings on}
365
366 { TCastleHorizontalGroup ----------------------------------------------------- }
367
368 constructor TCastleHorizontalGroup.Create(AOwner: TComponent);
369 begin
370 inherited;
371 FAlignment := vpMiddle;
372 FLeftToRight := true;
373 end;
374
375 procedure TCastleHorizontalGroup.SetLeftToRight(const Value: boolean);
376 begin
377 if FLeftToRight <> Value then
378 begin
379 FLeftToRight := Value;
380 FPackingValid := false;
381 end;
382 end;
383
384 procedure TCastleHorizontalGroup.SetAlignment(const Value: TVerticalPosition);
385 begin
386 if FAlignment <> Value then
387 begin
388 FAlignment := Value;
389 FPackingValid := false;
390 end;
391 end;
392
393 procedure TCastleHorizontalGroup.DoPackChildren(out W, H: Single);
394 var
395 I: Integer;
396 C: TCastleUserInterface;
397 AlignmentDelta: Single;
398 begin
399 W := Padding;
400 H := 0;
401
402 for I := 0 to ControlsCount - 1 do
403 begin
404 C := Controls[I];
405 if not C.Exists then Continue;
406
407 if LeftToRight then
408 C.Anchor(hpLeft, W)
409 else
410 C.Anchor(hpRight, -W);
411
412 case Alignment of
413 vpBottom: AlignmentDelta := Padding;
414 vpTop : AlignmentDelta := -Padding;
415 vpMiddle: AlignmentDelta := 0;
416 {$ifndef COMPILER_CASE_ANALYSIS}
417 else raise EInternalError.Create('TCastleHorizontalGroup.DoPackChildren:Alignment');
418 {$endif}
419 end;
420 C.Anchor(Alignment, AlignmentDelta);
421
422 W := W + C.EffectiveWidth + Spacing;
423 MaxVar(H, C.EffectiveHeight);
424 end;
425
426 W := W - Spacing + Padding;
427 H := H + 2 * Padding;
428 end;
429
430 function TCastleHorizontalGroup.PropertySections(
431 const PropertyName: String): TPropertySections;
432 begin
433 if ArrayContainsString(PropertyName, ['Alignment', 'Padding']) then
434 Result := [psBasic, psLayout]
435 else
436 Result := inherited PropertySections(PropertyName);
437 end;
438
439 { TCastleVerticalGroup ----------------------------------------------------- }
440
441 constructor TCastleVerticalGroup.Create(AOwner: TComponent);
442 begin
443 inherited;
444 FAlignment := hpLeft;
445 FTopToBottom := true;
446 end;
447
448 procedure TCastleVerticalGroup.SetTopToBottom(const Value: boolean);
449 begin
450 if FTopToBottom <> Value then
451 begin
452 FTopToBottom := Value;
453 FPackingValid := false;
454 end;
455 end;
456
457 procedure TCastleVerticalGroup.SetAlignment(const Value: THorizontalPosition);
458 begin
459 if FAlignment <> Value then
460 begin
461 FAlignment := Value;
462 FPackingValid := false;
463 end;
464 end;
465
466 procedure TCastleVerticalGroup.DoPackChildren(out W, H: Single);
467 var
468 I: Integer;
469 C: TCastleUserInterface;
470 AlignmentDelta: Single;
471 begin
472 W := 0;
473 H := Padding;
474
475 for I := 0 to ControlsCount - 1 do
476 begin
477 C := Controls[I];
478 if not C.Exists then Continue;
479
480 case Alignment of
481 hpLeft : AlignmentDelta := Padding;
482 hpRight : AlignmentDelta := -Padding;
483 hpMiddle: AlignmentDelta := 0;
484 {$ifndef COMPILER_CASE_ANALYSIS}
485 else raise EInternalError.Create('TCastleVerticalGroup.DoPackChildren:Alignment');
486 {$endif}
487 end;
488 C.Anchor(Alignment, AlignmentDelta);
489
490 if TopToBottom then
491 C.Anchor(vpTop, -H)
492 else
493 C.Anchor(vpBottom, H);
494
495 MaxVar(W, C.EffectiveWidth);
496 H := H + C.EffectiveHeight + Spacing;
497 end;
498
499 W := W + 2 * Padding;
500 H := H - Spacing + Padding;
501 end;
502
503 function TCastleVerticalGroup.PropertySections(
504 const PropertyName: String): TPropertySections;
505 begin
506 if ArrayContainsString(PropertyName, ['Alignment', 'Padding']) then
507 Result := [psBasic, psLayout]
508 else
509 Result := inherited PropertySections(PropertyName);
510 end;
511
512 {$endif read_implementation}