Debian Codegraph @

src/ui/castlecontrols_label.inc

raw
1 {%MainUnit castlecontrols.pas}
2 {
3 Copyright 2010-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 { Label with possibly multiline text.
20 See @url(https://castle-engine.io/manual_text.php
21 manual about displaying text and fonts). }
22 TCastleLabel = class(TCastleUserInterfaceFont)
23 strict private
24 FText: TStrings;
25 FPaddingHorizontal, FPaddingVertical, FPadding: Single;
26 FLineSpacing: Single;
27 FColor: TCastleColor;
28 FHtml: boolean;
29 FFrame: boolean;
30 FFrameColor: TCastleColor;
31 FMaxWidth: Single;
32 FAlignment: THorizontalPosition;
33 FVerticalAlignment: TVerticalPosition;
34 FAutoSize: boolean;
35 FMaxDisplayChars: Integer;
36 CachedRichText: TRichText;
37 FCaptionTranslate: Boolean;
38 function GetTextToRender: TRichText;
39 procedure SetAutoSize(const Value: boolean);
40 function GetCaption: String;
41 procedure SetCaption(const Value: String);
42 procedure SetAlignment(const Value: THorizontalPosition);
43 procedure SetVerticalAlignment(const Value: TVerticalPosition);
44 procedure SetMaxDisplayChars(const Value: Integer);
45 procedure SetMaxWidth(const Value: Single);
46 procedure TextChange(Sender: TObject);
47 procedure SetText(const Value: TStrings);
48 procedure SetPaddingHorizontal(const Value: Single);
49 procedure SetPaddingVertical(const Value: Single);
50 procedure SetPadding(const Value: Single);
51 procedure SetHtml(const Value: Boolean);
52 private
53 { For internal use by tooltip rendering. In normal circumstances,
54 leave this at tiLabel. }
55 ImageType: TThemeImage;
56 protected
57 procedure PreferredSize(var PreferredWidth, PreferredHeight: Single); override;
58 function GetInternalText: String; override;
59 procedure SetInternalText(const Value: String); override;
60 procedure UIScaleChanged; override;
61 procedure TranslateProperties(const TranslatePropertyEvent: TTranslatePropertyEvent); override;
62 procedure Loaded; override;
63 public
64 const
65 DefaultLineSpacing = 2;
66
67 constructor Create(AOwner: TComponent); override;
68 destructor Destroy; override;
69 procedure Render; override;
70 procedure EditorAllowResize(
71 out ResizeWidth, ResizeHeight: Boolean; out Reason: String); override;
72 function PropertySections(const PropertyName: String): TPropertySections; override;
73 procedure FontChanged; override;
74
75 { Text color. By default it's opaque black. }
76 property Color: TCastleColor read FColor write FColor;
77
78 { Color tint of the background image, see @link(Frame). By default white. }
79 property FrameColor: TCastleColor read FFrameColor write FFrameColor;
80 {$ifdef FPC}deprecated 'display frame around label using Border or parent UI control';{$endif}
81
82 function DisplayChars: Cardinal;
83 published
84 { Should we automatically adjust size to the text size.
85 The size of the label determines where does it display the @link(Frame),
86 where does it catch events, to what width is it aligned (see @link(Alignment))
87 and so on.
88
89 When this is @true (the default) then
90 @link(Width), @link(Height), @link(FullSize) values are ignored. }
91 property AutoSize: boolean read FAutoSize write SetAutoSize default true;
92
93 { Caption displayed on the label, each line as a String.
94 Setting this property merely copies the contents using TStrings.Assign. }
95 property Text: TStrings read FText write SetText;
96
97 { Caption displayed on the label.
98 This is just a shortcut to get/set @link(Text) as a single String.
99
100 Use LineEnding or NL constant when setting this to indicate a newline.
101 The two examples below are equivalent:
102
103 @longCode(#
104 // one way
105 Label1.Text.Clear;
106 Label1.Text.Add('First line');
107 Label1.Text.Add('Second line');
108
109 // alternative way to do the same
110 Label1.Caption := 'First line' + LineEnding + 'Second line';
111 #)
112 }
113 property Caption: String read GetCaption write SetCaption stored false;
114
115 { Inside the label rectangle, padding between rect borders and text.
116 Total horizontal padding is the sum @code(PaddingHorizontal + Padding),
117 total vertical padding is the sum @code(PaddingVertical + Padding).
118 @groupBegin }
119 property PaddingHorizontal: Single
120 read FPaddingHorizontal write SetPaddingHorizontal {$ifdef FPC}default 0{$endif};
121 property PaddingVertical: Single
122 read FPaddingVertical write SetPaddingVertical {$ifdef FPC}default 0{$endif};
123 property Padding: Single
124 read FPadding write SetPadding {$ifdef FPC}default 0{$endif};
125 { @groupEnd }
126
127 { Extra spacing between lines.
128 May be negative to squeeze lines tighter together. }
129 property LineSpacing: Single read FLineSpacing write FLineSpacing {$ifdef FPC}default DefaultLineSpacing{$endif};
130
131 { Does the text use HTML markup.
132 This allows to easily change colors or use bold, italic text.
133
134 See the example examples/fonts/html_text.lpr and
135 examples/fonts/html_text_demo.html for a demo of what HTML tags can do.
136 See @link(TCastleAbstractFont.PrintStrings) documentation for a list of support HTML markup.
137
138 Note that to see the bold/italic font variants in the HTML markup,
139 you need to set the font to be TCastleFontFamily with bold/italic variants.
140 See the example mentioned above, examples/fonts/html_text.lpr,
141 for a code how to do it. }
142 property Html: boolean read FHtml write SetHtml default false;
143
144 {$ifdef FPC}
145 property Tags: boolean read FHtml write SetHtml stored false default false;
146 deprecated 'use Html instead';
147 {$endif}
148
149 { Draw frame around the text. Frame uses theme image tiLabel,
150 see TCastleTheme.Images if you want to customize it. }
151 property Frame: boolean read FFrame write FFrame default false;
152 {$ifdef FPC}deprecated 'display frame around label using Border or parent UI control';{$endif}
153
154 { If non-zero, limit the width of resulting label.
155 The text will be broken in the middle of lines, to make it fit
156 (together with @link(PaddingHorizontal)) inside MaxWidth. }
157 property MaxWidth: Single read FMaxWidth write SetMaxWidth {$ifdef FPC}default 0{$endif};
158
159 { Horizontal alignment of the text. }
160 property Alignment: THorizontalPosition
161 read FAlignment write SetAlignment default hpLeft;
162
163 { Vertical alignment of the text. Usually you don't want to use this,
164 instead leave @link(AutoSize) = @true and align the label to the parent
165 using anchors, like @code(MyLabel.Anchor(vpMiddle);)
166 or @code(MyLabel.Anchor(vpTop);).
167
168 This property is useful if you really need to manually control the size.
169 It only matters when @link(AutoSize) is @false.
170 Then it controls where the text is, with respect to it's rectangle defined
171 by properties like @link(Height) or @link(FullSize). }
172 property VerticalAlignment: TVerticalPosition
173 read FVerticalAlignment write SetVerticalAlignment default vpBottom;
174
175 { Limit the displayed label text, if not -1.
176 This doesn't affect the label size, only the rendered text.
177 It's nice to show the animation of text "expanding", filling some area.
178 Use DisplayChars as the maximum sensible value for this. }
179 property MaxDisplayChars: Integer
180 read FMaxDisplayChars write SetMaxDisplayChars default -1;
181
182 { Should the @link(Caption) be localized (translated into other languages).
183 Determines if the property is enumerated by @link(TCastleComponent.TranslateProperties),
184 which affects the rest of localization routines. }
185 property CaptionTranslate: Boolean read FCaptionTranslate write FCaptionTranslate default true;
186
187 {$warnings off} // silence the fact that FrameColor is deprecated
188 {$define read_interface_class}
189 {$I auto_generated_persistent_vectors/tcastlelabel_persistent_vectors.inc}
190 {$undef read_interface_class}
191 {$warnings on}
192 end;
193
194 {$endif read_interface}
195
196 {$ifdef read_implementation}
197
198 { TCastleLabel --------------------------------------------------------------- }
199
200 constructor TCastleLabel.Create(AOwner: TComponent);
201 begin
202 inherited;
203
204 FText := TStringList.Create;
205 TStringList(FText).OnChange := {$ifdef FPC}@{$endif} TextChange;
206 {$warnings off}
207 if Theme.DefaultLabelWhite then
208 {$warnings on}
209 FColor := White
210 else
211 FColor := Black;
212 FFrame := false;
213 FFrameColor := White;
214 FLineSpacing := DefaultLineSpacing;
215 FAutoSize := true;
216 ImageType := tiLabel;
217 FMaxDisplayChars := -1;
218 FCaptionTranslate := true;
219
220 {$warnings off} // silence the fact that FrameColor is deprecated
221 {$define read_implementation_constructor}
222 {$I auto_generated_persistent_vectors/tcastlelabel_persistent_vectors.inc}
223 {$undef read_implementation_constructor}
224 {$warnings on}
225 end;
226
227 destructor TCastleLabel.Destroy;
228 begin
229 {$warnings off} // silence the fact that FrameColor is deprecated
230 {$define read_implementation_destructor}
231 {$I auto_generated_persistent_vectors/tcastlelabel_persistent_vectors.inc}
232 {$undef read_implementation_destructor}
233 {$warnings on}
234
235 FreeAndNil(FText);
236 FreeAndNil(CachedRichText);
237
238 inherited;
239 end;
240
241 function TCastleLabel.GetTextToRender: TRichText;
242 var
243 PaddingHorizontalScaled, MaxWidthScaled, WrapWidth: Single;
244 US: Single;
245 begin
246 if CachedRichText <> nil then
247 { Since GetTextToRender is called very often, we want it to usually return
248 instantly fast. }
249 Result := CachedRichText
250 else
251 begin
252 Result := TRichText.Create(Font, Text, Html);
253 if MaxWidth <> 0 then
254 begin
255 US := UIScale;
256 PaddingHorizontalScaled := US * (PaddingHorizontal + Padding);
257 MaxWidthScaled := US * MaxWidth;
258 WrapWidth := MaxWidthScaled - 2 * PaddingHorizontalScaled;
259 if WrapWidth > 0 then
260 Result.Wrap(WrapWidth);
261 end;
262 CachedRichText := Result;
263 end;
264 end;
265
266 procedure TCastleLabel.PreferredSize(var PreferredWidth, PreferredHeight: Single);
267 var
268 TextToRender: TRichText;
269 TextToRenderWidth: Single;
270 { FPC: TStrings.Count is Integer, TObjectList.Count is SizeInt
271 Delphi: TStrings.Count is Integer, TObjectList.Count is Integer so we use
272 TListSize in TextToRenderCount }
273 TextToRenderCount: TListSize;
274 PaddingHorizontalScaled, PaddingVerticalScaled, LineSpacingScaled: Single;
275 US: Single;
276 begin
277 inherited;
278
279 if AutoSize then
280 begin
281 if (not Html) and (MaxWidth = 0) then
282 begin
283 { fast case: no need to use TRichText in this case }
284 TextToRenderWidth := Font.MaxTextWidth(Text);
285 TextToRenderCount := Text.Count;
286 end else
287 begin
288 TextToRender := GetTextToRender;
289 TextToRenderWidth := TextToRender.Width;
290 TextToRenderCount := TextToRender.Count;
291 end;
292
293 US := UIScale;
294 PaddingHorizontalScaled := US * (PaddingHorizontal + Padding);
295 PaddingVerticalScaled := US * (PaddingVertical + Padding);
296 { When text is empty there is 0 lines so we need check that
297 not simply LineSpacingScaled * (TextToRenderCount - 1)}
298 if TextToRenderCount = 0 then
299 LineSpacingScaled := 0
300 else
301 LineSpacingScaled := US * LineSpacing * (TextToRenderCount - 1);
302
303 PreferredWidth := TextToRenderWidth + 2 * PaddingHorizontalScaled;
304 PreferredHeight :=
305 Font.Height * TextToRenderCount +
306 LineSpacingScaled +
307 2 * PaddingVerticalScaled +
308 { This additional Font.DescenderHeight is actually always an empty space on top of
309 the text. So we do not have to increase PreferredHeight by this Font.DescenderHeight,
310 strictly speaking (if we treat PreferredHeight as a "minimum bounding box height").
311
312 But adding it makes the text looks nicer centered vertically in the bbox.
313 Visually you have an ~empty space at the bottom (not really empty,
314 but usually empty) of height Font.DescenderHeight and empty space at top
315 (really always empty) of height Font.DescenderHeight.
316
317 So anchoring this TCastleLabel with vpMiddle to something else looks better. }
318 Font.DescenderHeight;
319 end;
320 end;
321
322 procedure TCastleLabel.EditorAllowResize(
323 out ResizeWidth, ResizeHeight: Boolean; out Reason: String);
324 begin
325 inherited;
326 if AutoSize then
327 begin
328 ResizeWidth := false;
329 ResizeHeight := false;
330 Reason := SAppendPart(Reason, NL, 'Turn off "TCastleLabel.AutoSize" to change size.');
331 end;
332 end;
333
334 procedure TCastleLabel.Render;
335 var
336 TextToRender: TRichText;
337 LineSpacingScaled: Single;
338
339 function TextHeight: Single;
340 begin
341 Result := TextToRender.Count * (Font.Height + LineSpacingScaled);
342 end;
343
344 var
345 SR: TFloatRectangle;
346 TextX, TextBottom, PaddingHorizontalScaled, PaddingVerticalScaled: Single;
347 US: Single;
348 begin
349 inherited;
350 if Text.Count = 0 then Exit; // early exit in case of easy, fast case
351
352 SR := RenderRect;
353 US := UIScale;
354 PaddingHorizontalScaled := US * (PaddingHorizontal + Padding);
355 PaddingVerticalScaled := US * (PaddingVertical + Padding);
356 LineSpacingScaled := US * LineSpacing;
357
358 {$warnings off} // using deprecated just to keep it working
359 if Frame then
360 Theme.Draw(SR, ImageType, UIScale, FrameColor);
361 {$warnings on}
362
363 { calculate TextX }
364 case Alignment of
365 hpLeft : TextX := SR.Left + PaddingHorizontalScaled;
366 hpMiddle: TextX := (SR.Left + SR.Right) / 2;
367 hpRight : TextX := SR.Right - PaddingHorizontalScaled;
368 {$ifndef COMPILER_CASE_ANALYSIS}
369 else raise EInternalError.Create('TCastleLabel.Render: Alignment?');
370 {$endif}
371 end;
372
373 { calculate TextBottom }
374 TextBottom := SR.Bottom + PaddingVerticalScaled + Font.DescenderHeight;
375
376 if (not Html) and (MaxWidth = 0) and
377 (AutoSize or (VerticalAlignment = vpBottom)) and
378 (MaxDisplayChars = -1) then
379 begin
380 { fast case: no need to use TRichText in this case }
381 Font.PrintStrings(TextX, TextBottom, Color, Text, false, LineSpacingScaled, Alignment);
382 end else
383 begin
384 TextToRender := GetTextToRender;
385
386 { fix TextBottom, in case of non-trivial VerticalAlignment }
387 if not AutoSize then
388 case VerticalAlignment of
389 vpMiddle: TextBottom := SR.Bottom + (SR.Height - TextHeight) / 2;
390 vpTop : TextBottom := SR.Top - PaddingVerticalScaled - Font.DescenderHeight - TextHeight;
391 else ;
392 end;
393
394 TextToRender.Print(TextX, TextBottom, Color, LineSpacingScaled, Alignment,
395 MaxDisplayChars);
396 end;
397 end;
398
399 procedure TCastleLabel.SetAutoSize(const Value: boolean);
400 begin
401 if FAutoSize <> Value then
402 begin
403 FAutoSize := Value;
404 VisibleChange([chRectangle]);
405 end;
406 end;
407
408 function TCastleLabel.GetCaption: String;
409 begin
410 Result := TrimEndingNewline(Text.Text);
411 end;
412
413 procedure TCastleLabel.SetCaption(const Value: String);
414 begin
415 Text.Text := Value;
416 end;
417
418 procedure TCastleLabel.SetAlignment(const Value: THorizontalPosition);
419 begin
420 if FAlignment <> Value then
421 begin
422 FAlignment := Value;
423 VisibleChange([chRender]);
424 end;
425 end;
426
427 procedure TCastleLabel.SetVerticalAlignment(const Value: TVerticalPosition);
428 begin
429 if FVerticalAlignment <> Value then
430 begin
431 FVerticalAlignment := Value;
432 VisibleChange([chRender]);
433 end;
434 end;
435
436 procedure TCastleLabel.SetMaxDisplayChars(const Value: Integer);
437 begin
438 if FMaxDisplayChars <> Value then
439 begin
440 FMaxDisplayChars := Value;
441 VisibleChange([chRectangle]);
442 end;
443 end;
444
445 procedure TCastleLabel.SetMaxWidth(const Value: Single);
446 begin
447 if FMaxWidth <> Value then
448 begin
449 FMaxWidth := Value;
450 // invalidate CachedRichText, as it depends on this property
451 FreeAndNil(CachedRichText);
452 VisibleChange([chRectangle]);
453 end;
454 end;
455
456 function TCastleLabel.DisplayChars: Cardinal;
457 var
458 TextToRender: TRichText;
459 begin
460 if Text.Count = 0 then Exit(0); // early exit in case of easy, fast case
461
462 TextToRender := GetTextToRender;
463 Result := TextToRender.DisplayChars;
464 end;
465
466 procedure TCastleLabel.TextChange(Sender: TObject);
467 begin
468 VisibleChange([chRectangle]);
469 // invalidate CachedRichText, as it depends on Text value
470 FreeAndNil(CachedRichText);
471 end;
472
473 procedure TCastleLabel.SetText(const Value: TStrings);
474 begin
475 FText.Assign(Value);
476 end;
477
478 function TCastleLabel.GetInternalText: String;
479 begin
480 Result := Caption;
481 end;
482
483 procedure TCastleLabel.SetInternalText(const Value: String);
484 begin
485 Caption := Value;
486 end;
487
488 function TCastleLabel.PropertySections(const PropertyName: String): TPropertySections;
489 begin
490 // Note that Caption expresses the same thing as Text, but easier to access
491 if (PropertyName = 'Text') or
492 (PropertyName = 'Caption') or
493 (PropertyName = 'MaxWidth') or
494 (PropertyName = 'ColorPersistent') or
495 (PropertyName = 'Html') then
496 Result := [psBasic]
497 else if (PropertyName = 'Alignment') or
498 (PropertyName = 'VerticalAlignment') or
499 (PropertyName = 'LineSpacing') or
500 (PropertyName = 'AutoSize') then
501 Result := [psBasic, psLayout]
502 else
503 Result := inherited PropertySections(PropertyName);
504 end;
505
506 procedure TCastleLabel.SetPaddingHorizontal(const Value: Single);
507 begin
508 if FPaddingHorizontal <> Value then
509 begin
510 FPaddingHorizontal := Value;
511 // invalidate CachedRichText, as it depends on this property
512 FreeAndNil(CachedRichText);
513 VisibleChange([chRender]);
514 end;
515 end;
516
517 procedure TCastleLabel.SetPaddingVertical(const Value: Single);
518 begin
519 if FPaddingVertical <> Value then
520 begin
521 FPaddingVertical := Value;
522 VisibleChange([chRender]);
523 end;
524 end;
525
526 procedure TCastleLabel.SetPadding(const Value: Single);
527 begin
528 if FPadding <> Value then
529 begin
530 FPadding := Value;
531 // invalidate CachedRichText, as it depends on this property
532 FreeAndNil(CachedRichText);
533 VisibleChange([chRender]);
534 end;
535 end;
536
537 procedure TCastleLabel.SetHtml(const Value: Boolean);
538 begin
539 if FHtml <> Value then
540 begin
541 FHtml := Value;
542 // invalidate CachedRichText, as it depends on this property
543 FreeAndNil(CachedRichText);
544 VisibleChange([chRender]);
545 end;
546 end;
547
548 procedure TCastleLabel.UIScaleChanged;
549 begin
550 inherited;
551 // invalidate CachedRichText, as it depends on UIScale
552 FreeAndNil(CachedRichText);
553 end;
554
555 procedure TCastleLabel.FontChanged;
556 begin
557 inherited;
558 // invalidate CachedRichText, as it depends on Font
559 FreeAndNil(CachedRichText);
560 end;
561
562 procedure TCastleLabel.TranslateProperties(
563 const TranslatePropertyEvent: TTranslatePropertyEvent);
564 var
565 S: String;
566 begin
567 if CaptionTranslate and (Caption <> '') then
568 begin
569 S := Caption;
570 TranslatePropertyEvent(Self, 'Caption', S);
571 Caption := S;
572 end;
573 end;
574
575 procedure TCastleLabel.Loaded;
576 begin
577 inherited;
578 {$warnings off} // using deprecated just to make a warning
579 if Frame or (not TVector4.PerfectlyEquals(White, FrameColor)) then
580 WritelnWarning('TCastleLabel.Frame and FrameColor are deprecated. Instead: Use TCastleLabel.Border, or place TCastleLabel under a component like TCastleRectangleControl or TCastleImageControl that defines a background.');
581 {$warnings on}
582 end;
583
584 {$warnings off} // silence the fact that FrameColor is deprecated
585 {$define read_implementation_methods}
586 {$I auto_generated_persistent_vectors/tcastlelabel_persistent_vectors.inc}
587 {$undef read_implementation_methods}
588 {$warnings on}
589
590 {$endif read_implementation}