Debian Codegraph @

src/scene/castlescene_text.inc

raw
1 {%MainUnit castlescene.pas}
2 {
3 Copyright 2021-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 { TCastleText. }
18
19 {$ifdef read_interface}
20
21 type
22 { Text that is displayed and transformed as TCastleTransform,
23 inside TCastleViewport, and can be manipulated in 3D.
24
25 See @url(https://castle-engine.io/manual_text.php
26 manual about displaying text and fonts).
27 See @url(https://castle-engine.io/viewport_and_scenes#_text_tcastletext
28 manual about TCastleText in a viewport). }
29 TCastleText = class(TCastleAbstractPrimitive)
30 strict private
31 FText: TStrings;
32 FCaptionTranslate: Boolean;
33 FDoubleSided: Boolean;
34 TextNode: TTextNode;
35 FontStyleNode: TFontStyleNode;
36 FAlignment: THorizontalPosition;
37 FVerticalAlignment: TVerticalPosition;
38 FSize, FSpacing: Single;
39 FCustomFont: TCastleFont;
40 FCustomFontObserver: TFreeNotificationObserver;
41 function GetCaption: String;
42 procedure SetCaption(const Value: String);
43 procedure TextChange(Sender: TObject);
44 procedure SetText(const Value: TStrings);
45 procedure SetDoubleSided(const Value: Boolean);
46 procedure SetAlignment(const Value: THorizontalPosition);
47 procedure SetVerticalAlignment(const Value: TVerticalPosition);
48 procedure SetSize(const Value: Single);
49 procedure SetSpacing(const Value: Single);
50 procedure SetCustomFont(const Value: TCastleFont);
51 procedure CustomFontFreeNotification(const Sender: TFreeNotificationObserver);
52 procedure CustomFontSizeChanged(Sender: TObject);
53 procedure FontStyleFontChanged;
54 protected
55 procedure TranslateProperties(const TranslatePropertyEvent: TTranslatePropertyEvent); override;
56 function GetInternalText: String; override;
57 procedure SetInternalText(const Value: String); override;
58 public
59 constructor Create(AOwner: TComponent); override;
60 destructor Destroy; override;
61 function PropertySections(const PropertyName: String): TPropertySections; override;
62 published
63 { Text displayed, expressed by a simple String.
64 This is just a shortcut to get/set @link(Text) as a single String.
65
66 Use LineEnding or NL constant when setting this to indicate a newline.
67 The two examples below are equivalent:
68
69 @longCode(#
70 // one way
71 MyText.Text.Clear;
72 MyText.Text.Add('First line');
73 MyText.Text.Add('Second line');
74
75 // alternative way to do the same
76 MyText.Caption := 'First line' + LineEnding + 'Second line';
77 #)
78 }
79 property Caption: String read GetCaption write SetCaption stored false;
80
81 { Text displayed, with each line as a separate String.
82 Setting this property merely copies the contents using TStrings.Assign. }
83 property Text: TStrings read FText write SetText;
84
85 { Should the @link(Caption) be localized (translated into other languages).
86 Determines if the property is enumerated by @link(TCastleComponent.TranslateProperties),
87 which affects the rest of localization routines. }
88 property CaptionTranslate: Boolean read FCaptionTranslate write FCaptionTranslate default true;
89
90 { Text is by default unlit. }
91 property Material default pmUnlit;
92
93 { When this is @true, the object is visible (and lit) from both sides.
94
95 Note that the default for TCastleText is different than the default for most other primitives,
96 like TCastleBox and TCastleSphere: by default text is double-sided,
97 while most other primitives by default are not double-sided.
98 Set this property to @false for more performance in case camera may be behind the text.
99 The reason for different default is that text object doesn't have an obvious "interior",
100 unlike a box of sphere,
101 so you can more easily land behind the text (and get confused why is it not displayed).
102 This follows X3D TTextNode.Solid default. }
103 property DoubleSided: Boolean read FDoubleSided write SetDoubleSided default true;
104
105 { Horizontal alignment of the text. }
106 property Alignment: THorizontalPosition
107 read FAlignment write SetAlignment default hpLeft;
108
109 { Vertical alignment of the text. }
110 property VerticalAlignment: TVerticalPosition
111 read FVerticalAlignment write SetVerticalAlignment default vpBottom;
112
113 { Size of the text, given as height of a single line of text.
114 See https://www.web3d.org/specifications/X3Dv4Draft/ISO-IEC19775-1v4-CD/Part01/components/text.html#FontStyle
115 for an illustrative image. }
116 property Size: Single read FSize write SetSize {$ifdef FPC}default 1.0{$endif};
117
118 { Spacing between lines of text, to make lines more distant (with values > 1) or more squeezed (with values < 1).
119 See https://www.web3d.org/specifications/X3Dv4Draft/ISO-IEC19775-1v4-CD/Part01/components/text.html#FontStyle
120 for an illustrative image. }
121 property Spacing: Single read FSpacing write SetSpacing {$ifdef FPC}default 1.0{$endif};
122
123 { Adjust the font. }
124 property CustomFont: TCastleFont read FCustomFont write SetCustomFont;
125 end;
126
127 {$endif read_interface}
128
129 {$ifdef read_implementation}
130
131 { TCastleText ---------------------------------------------------------------- }
132
133 constructor TCastleText.Create(AOwner: TComponent);
134 begin
135 inherited;
136
137 TextNode := TTextNode.Create;
138 ShapeNode.Geometry := TextNode;
139
140 FontStyleNode := TFontStyleNode.Create;
141 FontStyleNode.Family := ffSans; // not modifiable now, as we may disable loading by default serif/typewriter fonts in X3D
142 FontStyleNode.Justify := fjBegin;
143 FontStyleNode.JustifyMinor := fjEnd;
144 TextNode.FontStyle := FontStyleNode;
145
146 { default properties values }
147 Material := pmUnlit;
148 FText := TCastleStringList.Create;
149 (FText as TCastleStringList).OnChange := {$ifdef FPC}@{$endif} TextChange;
150 FCaptionTranslate := true;
151 FDoubleSided := true;
152 FSize := 1;
153 FSpacing := 1;
154 FAlignment := hpLeft;
155 FVerticalAlignment := vpBottom;
156
157 FCustomFontObserver := TFreeNotificationObserver.Create(Self);
158 FCustomFontObserver.OnFreeNotification := {$ifdef FPC}@{$endif}CustomFontFreeNotification;
159 end;
160
161 destructor TCastleText.Destroy;
162 begin
163 { Since our instance will become invalid soon,
164 FCustomFont should not notify us anymore.
165 Testcase: examples/localization/gettext/ }
166 if FCustomFont <> nil then
167 FCustomFont.RemoveFontSizeChangeNotification(
168 {$ifdef FPC}@{$endif}CustomFontSizeChanged);
169
170 FreeAndNil(FText);
171 inherited;
172 end;
173
174 function TCastleText.PropertySections(const PropertyName: String): TPropertySections;
175 begin
176 if (PropertyName = 'Caption') or
177 (PropertyName = 'Text') or
178 (PropertyName = 'DoubleSided') or
179 (PropertyName = 'Size') or
180 (PropertyName = 'Spacing') or
181 (PropertyName = 'Alignment') or
182 (PropertyName = 'VerticalAlignment') or
183 (PropertyName = 'CustomFont') then
184 Result := [psBasic]
185 else
186 Result := inherited PropertySections(PropertyName);
187 end;
188
189 function TCastleText.GetCaption: String;
190 begin
191 Result := TrimEndingNewline(Text.Text);
192 end;
193
194 procedure TCastleText.SetCaption(const Value: String);
195 begin
196 Text.Text := Value;
197 end;
198
199 procedure TCastleText.TextChange(Sender: TObject);
200 begin
201 TextNode.SetText(FText as TCastleStringList);
202 end;
203
204 procedure TCastleText.SetText(const Value: TStrings);
205 begin
206 FText.Assign(Value);
207 end;
208
209 procedure TCastleText.TranslateProperties(
210 const TranslatePropertyEvent: TTranslatePropertyEvent);
211 var
212 S: String;
213 begin
214 if CaptionTranslate and (Caption <> '') then
215 begin
216 S := Caption;
217 TranslatePropertyEvent(Self, 'Caption', S);
218 Caption := S;
219 end;
220 end;
221
222 procedure TCastleText.SetDoubleSided(const Value: Boolean);
223 begin
224 if FDoubleSided <> Value then
225 begin
226 FDoubleSided := Value;
227 TextNode.Solid := not Value;
228 end;
229 end;
230
231 procedure TCastleText.SetAlignment(const Value: THorizontalPosition);
232 begin
233 if FAlignment <> Value then
234 begin
235 FAlignment := Value;
236 case Value of
237 hpLeft : FontStyleNode.Justify := fjBegin;
238 hpMiddle: FontStyleNode.Justify := fjMiddle;
239 hpRight : FontStyleNode.Justify := fjEnd;
240 {$ifndef COMPILER_CASE_ANALYSIS}
241 else raise EInternalError.Create('TCastleText.SetAlignment:Value?');
242 {$endif}
243 end;
244 end;
245 end;
246
247 procedure TCastleText.SetVerticalAlignment(const Value: TVerticalPosition);
248 begin
249 if FVerticalAlignment <> Value then
250 begin
251 FVerticalAlignment := Value;
252 case Value of
253 vpBottom: FontStyleNode.JustifyMinor := fjEnd;
254 vpMiddle: FontStyleNode.JustifyMinor := fjMiddle;
255 vpTop : FontStyleNode.JustifyMinor := fjBegin;
256 { Note that achieving fjFirst is not possible, and that is deliberate.
257 I think fjFirst is a needless complication.
258 It is much more important to have API similar to TCastleLabel. }
259 {$ifndef COMPILER_CASE_ANALYSIS}
260 else raise EInternalError.Create('TCastleText.SetVerticalAlignment:Value?');
261 {$endif}
262 end;
263 end;
264 end;
265
266 procedure TCastleText.SetSize(const Value: Single);
267 begin
268 if FSize <> Value then
269 begin
270 FSize := Value;
271 FontStyleNode.Size := Value;
272 end;
273 end;
274
275 procedure TCastleText.SetSpacing(const Value: Single);
276 begin
277 if FSpacing <> Value then
278 begin
279 FSpacing := Value;
280 FontStyleNode.Spacing := Value;
281 end;
282 end;
283
284 function TCastleText.GetInternalText: String;
285 begin
286 Result := Caption;
287 end;
288
289 procedure TCastleText.SetInternalText(const Value: String);
290 begin
291 Caption := Value;
292 end;
293
294 procedure TCastleText.CustomFontFreeNotification(const Sender: TFreeNotificationObserver);
295 begin
296 FCustomFont := nil;
297 end;
298
299 procedure TCastleText.CustomFontSizeChanged(Sender: TObject);
300 begin
301 { When CustomFont properties, like OptimalSize, changed -- rebuild also displayed font texture. }
302 FontStyleFontChanged;
303 end;
304
305 procedure TCastleText.FontStyleFontChanged;
306 var
307 I: Integer;
308 Shape: TShape;
309 begin
310 // TextNode.FontChanged; // no need to, TShape.Changed will call it, also freeing TextProxy
311 for I := 0 to TShapeTree.AssociatedShapesCount(TextNode) - 1 do
312 begin
313 Shape := TShapeTree.AssociatedShape(TextNode, I) as TShape;
314 Shape.Changed(false, [chFontStyleFontChanged]);
315 end;
316 end;
317
318 procedure TCastleText.SetCustomFont(const Value: TCastleFont);
319 begin
320 if FCustomFont <> Value then
321 begin
322 if FCustomFont <> nil then
323 FCustomFont.RemoveFontSizeChangeNotification(
324 {$ifdef FPC}@{$endif}CustomFontSizeChanged);
325 FCustomFont := Value;
326 if FCustomFont <> nil then
327 FCustomFont.AddFontSizeChangeNotification(
328 {$ifdef FPC}@{$endif}CustomFontSizeChanged);
329
330 FCustomFontObserver.Observed := Value;
331
332 // modify X3D nodes
333 FontStyleNode.CustomFont := Value;
334 FontStyleFontChanged;
335 end;
336 end;
337
338 {$endif read_implementation}