Debian Codegraph @

src/ui/castledialogviews_dialog.inc

raw
1 {%MainUnit castledialogviews.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 { Dialog box that can display a long text, with automatic vertical scrollbar.
20 You can also add buttons at the bottom.
21 You can also have an input text area. }
22 TDialog = class(TCastleUserInterfaceFont)
23 strict private
24 const
25 BoxMargin = 10;
26 WindowMargin = 10;
27 ButtonHorizontalMargin = 10;
28 type
29 TDialogScrollArea = class(TCastleUserInterface)
30 strict private
31 Dialog: TDialog;
32 public
33 constructor Create(AOwner: TComponent); override;
34 procedure Render; override;
35 end;
36 var
37 FInputText: String;
38
39 { Broken Text. }
40 Broken_Text: TRichText;
41 { Ignored (not visible) if not DrawInputText.
42 Else broken InputText. }
43 Broken_InputText: TStringList;
44
45 MaxLineWidth: Single;
46 { Sum of all Broken_Text.Count + Broken_InputText.Count.
47 In other words, all lines that are scrolled by the scrollbar. }
48 AllScrolledLinesCount: integer;
49
50 { Things below set in MessageCore, readonly afterwards. }
51 { Main text to display. Read-only contents. }
52 Text: TStringList;
53 TextAlign: THorizontalPosition;
54 { Should we display InputText }
55 DrawInputText: boolean;
56 Buttons: array of TCastleButton;
57 LifeTime: TFloatTime;
58 FHtml: boolean;
59 ScrollView: TCastleScrollView;
60
61 function BoxMarginScaled: Single;
62 function WindowMarginScaled: Single;
63 function ButtonHorizontalMarginScaled: Single;
64
65 procedure SetInputText(const Value: String);
66
67 { Calculate height in pixels needed to draw Buttons.
68 Returns 0 if there are no Buttons. }
69 function ButtonsHeightScaled: Single;
70 function ButtonsHeight: Single;
71 procedure UpdateSizes;
72 protected
73 procedure PreferredSize(var PreferredWidth, PreferredHeight: Single); override;
74 public
75 { Set this to @true to signal that modal dialog window should be closed.
76 This is not magically handled --- if you implement a modal dialog box,
77 you should check in your loop whether something set Answered to @true. }
78 Answered: boolean;
79
80 { Input text. Displayed only if DrawInputText. }
81 property InputText: String read FInputText write SetInputText;
82
83 constructor Create(AOwner: TComponent); override;
84 destructor Destroy; override;
85 { Assign display stuff. Call this before adding control to Controls list. }
86 procedure Initialize(
87 const TextList: TStrings; const ATextAlign: THorizontalPosition;
88 const AHtml: boolean;
89 const AButtons: array of TCastleButton;
90 const ADrawInputText: boolean);
91 procedure Resize; override;
92 procedure Update(const SecondsPassed: Single; var HandleInput: boolean); override;
93 procedure Render; override;
94 function CapturesEventsAtPosition(const Position: TVector2): boolean; override;
95 end;
96
97 {$endif read_interface}
98
99 {$ifdef read_implementation}
100
101 const
102 CaretChar = '|';
103
104 { TDialogScrollArea -------------------------------------------------------------- }
105
106 constructor TViewDialog.TDialog.TDialogScrollArea.Create(AOwner: TComponent);
107 begin
108 inherited;
109 Dialog := AOwner as TViewDialog.TDialog;
110 FullSize := true; // we want our RenderRect to be equal to parent
111 end;
112
113 procedure TViewDialog.TDialog.TDialogScrollArea.Render;
114 type
115 TCaretMode = (cmNone, cmVisible, cmInvisible);
116
117 { Render a Text line, and move Y up to the line above. }
118 procedure DrawString(X: Single; var Y: Single; const Color: TCastleColor;
119 Text: String; const TextAlign: THorizontalPosition;
120 const Caret: TCaretMode);
121 var
122 CaretWidth: Single;
123 begin
124 if Caret <> cmNone then
125 CaretWidth := Dialog.Font.TextWidth(CaretChar) else
126 CaretWidth := 0;
127 { change X only locally, to take TextAlign into account }
128 case TextAlign of
129 hpMiddle: X := X + ((Dialog.MaxLineWidth - (Dialog.Font.TextWidth(Text) + CaretWidth)) / 2);
130 hpRight : X := X + ( Dialog.MaxLineWidth - (Dialog.Font.TextWidth(Text) + CaretWidth));
131 else ;
132 end;
133 if Caret = cmVisible then
134 Text := Text + CaretChar;
135 Dialog.Font.Print(X, Y, Color, Text);
136 { change Y for caller, to print next line higher }
137 Y := Y + Dialog.Font.Height;
138 end;
139
140 { Render all lines in S, and move Y up to the line above. }
141 procedure DrawStrings(const X: Single; var Y: Single;
142 const Color: TCastleColor; const s: TStrings; TextAlign: THorizontalPosition;
143 const AddCaret: boolean);
144 const
145 CaretSpeed = 1; //< how many blinks per second
146 var
147 I: Integer;
148 Caret: TCaretMode;
149 begin
150 for i := S.Count - 1 downto 0 do
151 begin
152 if AddCaret and (I = S.Count - 1) then
153 begin
154 if FloatModulo(Dialog.LifeTime * CaretSpeed, 1.0) < 0.5 then
155 Caret := cmVisible else
156 Caret := cmInvisible;
157 end else
158 Caret := cmNone;
159 { each DrawString call will move Y up }
160 DrawString(X, Y, Color, s[i], TextAlign, Caret);
161 end;
162 end;
163
164 var
165 TextX, TextY: Single;
166 SR: TFloatRectangle;
167 begin
168 inherited;
169
170 SR := RenderRect; // screen rectangle of ScrollView.ScrollArea
171 TextX := SR.Left + Dialog.BoxMarginScaled;
172 TextY := SR.Bottom + Dialog.BoxMarginScaled;
173
174 { draw Broken_InputText and Broken_Text.
175 Order matters, as it's drawn from bottom to top. }
176 if Dialog.DrawInputText then
177 DrawStrings(TextX, TextY, Theme.MessageInputTextColor,
178 Dialog.Broken_InputText, Dialog.TextAlign, true);
179
180 { adjust TextX for TRichText.Print call }
181 case Dialog.TextAlign of
182 hpMiddle: TextX := (SR.Left + SR.Right) / 2;
183 hpRight : TextX := SR.Right - Dialog.BoxMarginScaled;
184 else ;
185 end;
186 Dialog.Broken_Text.Print(TextX, TextY, Theme.MessageTextColor, 0, Dialog.TextAlign);
187 end;
188
189 { TViewDialog.TDialog -------------------------------------------------------------- }
190
191 constructor TViewDialog.TDialog.Create(AOwner: TComponent);
192 begin
193 inherited;
194 { use Theme.MessageFont this way }
195 CustomFont := Theme.MessageFont;
196 Text := TStringList.Create;
197 { Contents of Broken_InputText will be initialized in TViewDialog.TDialog.UpdateSizes. }
198 Broken_InputText := TStringList.Create;
199 Anchor(hpMiddle);
200 Anchor(vpMiddle);
201
202 ScrollView := TCastleScrollView.Create(Self);
203 ScrollView.ScrollArea.InsertFront(TDialogScrollArea.Create(Self));
204 // we will set size explicitly in TViewDialog.TDialog.UpdateSizes
205 ScrollView.ScrollArea.AutoSizeToChildren := false;
206 ScrollView.EnableDragging := true;
207 InsertFront(ScrollView);
208 end;
209
210 procedure TViewDialog.TDialog.Initialize(const TextList: TStrings;
211 const ATextAlign: THorizontalPosition; const AHtml: boolean;
212 const AButtons: array of TCastleButton;
213 const ADrawInputText: boolean);
214 var
215 I: Integer;
216 begin
217 Text.Assign(TextList);
218 TextAlign := ATextAlign;
219 FHtml := AHtml;
220 DrawInputText := ADrawInputText;
221 SetLength(Buttons, Length(AButtons));
222 for I := 0 to High(AButtons) do
223 begin
224 Buttons[I] := AButtons[I];
225 InsertFront(Buttons[I]);
226 end;
227 end;
228
229 destructor TViewDialog.TDialog.Destroy;
230 begin
231 FreeAndNil(Broken_Text);
232 FreeAndNil(Broken_InputText);
233 FreeAndNil(Text);
234 inherited;
235 end;
236
237 procedure TViewDialog.TDialog.SetInputText(const Value: String);
238 begin
239 FInputText := value;
240 if Container <> nil then
241 begin
242 VisibleChange([chRectangle]);
243 UpdateSizes;
244 end;
245 end;
246
247 function TViewDialog.TDialog.ButtonsHeightScaled: Single;
248 var
249 Button: TCastleButton;
250 begin
251 Result := 0;
252 for Button in Buttons do
253 MaxVar(Result, Button.EffectiveHeight * UIScale + 2 * BoxMarginScaled);
254 end;
255
256 function TViewDialog.TDialog.ButtonsHeight: Single;
257 var
258 Button: TCastleButton;
259 begin
260 Result := 0;
261 for Button in Buttons do
262 MaxVar(Result, Button.EffectiveHeight + 2 * BoxMargin);
263 end;
264
265 procedure TViewDialog.TDialog.Resize;
266 begin
267 inherited;
268 UpdateSizes;
269 end;
270
271 procedure TViewDialog.TDialog.UpdateSizes;
272
273 { Reposition Buttons. }
274 procedure UpdateButtons;
275 var
276 X, Y: Single;
277 I: Integer;
278 Button: TCastleButton;
279 begin
280 if Length(Buttons) <> 0 then
281 begin
282 X := -BoxMargin;
283 Y := BoxMargin;
284 for I := Length(Buttons) - 1 downto 0 do
285 begin
286 Button := Buttons[I];
287 Button.Anchor(vpBottom, Y);
288 Button.Anchor(hpRight, X);
289 X := X - (Button.EffectiveWidth + ButtonHorizontalMargin);
290 end;
291 end;
292 end;
293
294 var
295 BreakWidth, ButtonsWidth: Single;
296 Button: TCastleButton;
297 PR: TFloatRectangle;
298 begin
299 { TODO: Using PR variable, otherwise on iOS directly writing ParentRect.Width
300 causes range check error. Investigating,
301 looks like FPC bug (seen on FPC 3.0.2, other versions not tested yet).
302
303 - Writeln(ParentRect.Width) is some huge (impossible for 32-bit actually,
304 and yes, it's 32-bit on iOS too) value (4879082848896).
305 - But Writeln(Format('%d', [ParentRect.Width])) shows normal value (640).
306 - Using ParentRect.Width in expression causes range check error.
307
308 Using PR.Width always works OK.
309 }
310 PR := ParentRect;
311
312 { calculate BreakWidth, which is the width at which we should break
313 our string lists Broken_Xxx. We must here always subtract
314 EffectiveScrollBarWidth to be on the safe side, because we don't know
315 yet is ScrollBarVisible. }
316 BreakWidth := Max(0, PR.Width - BoxMarginScaled * 2
317 - WindowMarginScaled * 2 - ScrollView.EffectiveScrollBarWidth * UIScale);
318
319 { calculate MaxLineWidth and AllScrolledLinesCount }
320
321 { calculate Broken_Text }
322 FreeAndNil(Broken_Text);
323 Broken_Text := TRichText.Create(Font, Text, FHtml);
324 Broken_Text.Wrap(BreakWidth);
325 MaxLineWidth := Broken_Text.Width;
326 AllScrolledLinesCount := Broken_Text.Count;
327
328 ButtonsWidth := 0;
329 for Button in Buttons do
330 ButtonsWidth := ButtonsWidth + (Button.EffectiveWidth * UIScale + ButtonHorizontalMarginScaled);
331 if ButtonsWidth > 0 then
332 ButtonsWidth := ButtonsWidth - ButtonHorizontalMarginScaled; // extract margin from last button
333 MaxVar(MaxLineWidth, ButtonsWidth);
334
335 if DrawInputText then
336 begin
337 { calculate Broken_InputText }
338 Broken_InputText.Clear;
339 Font.BreakLines(InputText, Broken_InputText, BreakWidth);
340 { It's our intention that if DrawInputText then *always*
341 at least 1 line of InputText (even if it's empty) will be shown.
342 That's because InputText is the editable text for the user,
343 so there should be indication of "empty line". }
344 if Broken_InputText.count = 0 then Broken_InputText.Add('');
345 MaxVar(MaxLineWidth, Font.MaxTextWidth(Broken_InputText) + Font.TextWidth(CaretChar));
346 AllScrolledLinesCount := AllScrolledLinesCount + Broken_InputText.count;
347 end;
348
349 { Now we have MaxLineWidth and AllScrolledLinesCount calculated,
350 so our Rect and RenderRect return valid values. }
351
352 ScrollView.Anchor(hpLeft, 0);
353 ScrollView.Anchor(vpBottom, ButtonsHeight);
354 ScrollView.Width := EffectiveWidth;
355 ScrollView.Height := EffectiveHeight - ButtonsHeight;
356
357 { add Font.DescenderHeight, to be able to see the descend of the bottom line when Scroll is ScrollMax. }
358 ScrollView.ScrollArea.Height :=
359 (Font.Height * AllScrolledLinesCount + Font.DescenderHeight) / UIScale
360 + 2 * BoxMargin;
361 ScrollView.ScrollArea.Width := EffectiveWidth;
362
363 UpdateButtons;
364 end;
365
366 procedure TViewDialog.TDialog.Update(const SecondsPassed: Single;
367 var HandleInput: boolean);
368 begin
369 inherited;
370 LifeTime := LifeTime + SecondsPassed;
371 { when we have input text, we display blinking caret, so keep redrawing }
372 if DrawInputText then
373 VisibleChange([chRender]);
374 end;
375
376 procedure TViewDialog.TDialog.Render;
377 begin
378 inherited;
379
380 Theme.Draw(RenderRect, tiWindow, UIScale);
381 end;
382
383 function TViewDialog.TDialog.CapturesEventsAtPosition(const Position: TVector2): boolean;
384 begin
385 Result := true; // always capture
386 end;
387
388 procedure TViewDialog.TDialog.PreferredSize(var PreferredWidth, PreferredHeight: Single);
389 var
390 PR: TFloatRectangle;
391 begin
392 PR := ParentRect;
393 PreferredWidth :=
394 Min(MaxLineWidth + BoxMarginScaled * 2 +
395 ScrollView.ScrollBarWidth * UIScale,
396 PR.Width - WindowMarginScaled * 2);
397 PreferredHeight :=
398 Min(Font.Height * AllScrolledLinesCount +
399 Font.DescenderHeight + BoxMarginScaled * 2 + ButtonsHeightScaled
400 { adding here + 2 is a hack to make sure that TCastleScrollView will
401 not show scrollbars when not necessary. That's because we set
402 ScrollView.ScrollArea.Height using a similar equation as above
403 "Font.Height * AllScrolledLinesCount + Font.DescenderHeight...",
404 but it's in unscaled size (/ UIScale), and sometimes (with wild UIScale
405 values) it seems like scrollbars are needed (EffectiveHeight < ScrollArea.EffectiveHeight)
406 even though they actually are not.
407
408 Reproducible if you try to resize to small sizes the demo on
409 examples/mobile/simple_3d_demo/ . }
410 + 2,
411 PR.Height - WindowMarginScaled * 2);
412 end;
413
414 function TViewDialog.TDialog.BoxMarginScaled: Single;
415 begin
416 Result := BoxMargin * UIScale;
417 end;
418
419 function TViewDialog.TDialog.WindowMarginScaled: Single;
420 begin
421 Result := WindowMargin * UIScale;
422 end;
423
424 function TViewDialog.TDialog.ButtonHorizontalMarginScaled: Single;
425 begin
426 Result := ButtonHorizontalMargin * UIScale;
427 end;
428
429 {$endif read_implementation}