Debian Codegraph @

src/window/castlemessages.pas

raw
1 {
2 Copyright 2001-2023 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 (* Dialog windows (asking user for confirmation, question,
17 simple text input and such) displayed within an OpenGL context (TCastleWindow).
18
19 Features:
20
21 @unorderedList(
22 @item(All the MessageXxx routines display a modal dialog.
23 They return only when the user accepted / answered the dialog box.
24 This way they are comfortable to use anywhere in your program.)
25
26 @item(MessageInputXxx family of functions ask user to enter some text.)
27
28 @item(All the dialog boxes have vertical scroll bar, displayed when needed.
29 So it's OK to use really long text. Scroll bar can be operated
30 with keys (up/down, ctrl+up/down, page up/down, home/end) and mouse
31 (drag the scroll bar, or click below/above it).)
32
33 @item(Long text lines are automatically broken. So it's OK to use
34 text with long lines.
35 We will try to break text only at whitespace.
36
37 If you pass a text as a single string parameter, then our "line breaking"
38 works correctly even for text that already contains newline characters
39 (they are correctly recognized as forcing line break).
40
41 If you pass a text as an "array of string" or TStringList, it's expected
42 that strings inside don't contain newline characters anymore. It's undefined
43 what will happen (i.e. whether they will be correctly broken) otherwise.
44 Of course, TStringList contents used to pass text to MessageXxx will never
45 be modified in any way.)
46
47 @item(User is allowed to resize the window while MessageXxx works.
48 (As long as TCastleWindow.ResizeAllowed = raAllowed, of course.)
49 Long lines are automatically broken taking into account current window
50 width.)
51
52 @item(You can configure dialog boxes look using TCastleTheme.
53 Various parts of the dialog use scaled images.
54 This way you can change the border, background of the dialog,
55 you can also make the dialog box partially transparent.)
56 )
57
58 Call MessageXxx functions only when Window.Closed = false.
59
60 Notes about implementation:
61
62 @unorderedList(
63 @item(We temporary replace normal window callbacks and controls
64 using TGLMode. This allows you to call MessageXxx procedures
65 in any place of your program, and things will just work,
66 the MessageXxx will return only once user answers the dialog box.)
67
68 @item(If you use @link(TCastleApplicationProperties.OnUpdate ApplicationProperties.OnUpdate):
69
70 As these events are not tied
71 to a particular window, they continue to work even
72 while we're inside MessageXxx procedure.
73 Be sure to implement them such that they make sense also when
74 we're inside a dialog box.
75
76 In particular, remember that you cannot close the Window
77 when the message box in running. So do not call
78 TCastleWindow.Close from
79 @link(TCastleApplicationProperties.OnUpdate ApplicationProperties.OnUpdate) callbacks,
80 at least not without checking whether we're not inside a dialog box.)
81 )
82 *)
83
84 unit CastleMessages;
85
86 {$I castleconf.inc}
87
88 interface
89
90 uses Classes, CastleWindow, CastleGLUtils, CastleUtils,
91 CastleStringUtils, CastleVectors, CastleKeysMouse, CastleControls,
92 CastleRectangles;
93
94 type
95 { Position of text in message dialogs.
96 Deprecated, use THorizontalPosition instead. }
97 TTextAlign = THorizontalPosition deprecated;
98 const
99 DefaultAlign = hpLeft;
100 taRight = hpRight deprecated;
101 taMiddle = hpMiddle deprecated;
102 taLeft = hpLeft deprecated;
103
104 { Ask user for simple confirmation. This is the simplest "OK" dialog box.
105
106 @groupBegin }
107 procedure MessageOK(Window: TCastleWindow; const s: string;
108 const Alignment: THorizontalPosition = DefaultAlign;
109 const Html: boolean = false); overload;
110 procedure MessageOK(Window: TCastleWindow; const SArray: array of string;
111 const Alignment: THorizontalPosition = DefaultAlign;
112 const Html: boolean = false); overload;
113 procedure MessageOK(Window: TCastleWindow; TextList: TStringList;
114 const Alignment: THorizontalPosition = DefaultAlign;
115 const Html: boolean = false); overload;
116 { @groupEnd }
117
118 { Ask user to input a string.
119 User must give an answer (there is no "Cancel" button),
120 use MessageInputQuery if you want a version with "Cancel" button.
121 @param AnswerMaxLen 0 (zero) means that there's no maximum answer length.
122
123 @groupBegin }
124 function MessageInput(Window: TCastleWindow; const s: string;
125 const answerDefault: string = '';
126 const MinLength: integer = 0;
127 const MaxLength: integer = 0;
128 const AllowedChars: TSetOfChars = AllChars;
129 const Alignment: THorizontalPosition = DefaultAlign;
130 const Html: boolean = false): string; overload;
131 function MessageInput(Window: TCastleWindow; TextList: TStringList;
132 const answerDefault: string = '';
133 const MinLength: integer = 0;
134 const MaxLength: integer = 0;
135 const AllowedChars: TSetOfChars = AllChars;
136 const Alignment: THorizontalPosition = DefaultAlign;
137 const Html: boolean = false): string; overload;
138 { @groupEnd }
139
140 { Ask user to input a string, or cancel.
141 Returns @true and sets Answer if user accepted some text.
142 Note that initial Answer value is the answer proposed to the user.
143 @param AnswerMaxLen 0 (zero) means that there's no maximum answer length.
144
145 @groupBegin }
146 function MessageInputQuery(Window: TCastleWindow; const s: string;
147 var Answer: string;
148 const MinLength: integer = 0;
149 const MaxLength: integer = 0;
150 const AllowedChars: TSetOfChars = AllChars;
151 const Alignment: THorizontalPosition = DefaultAlign;
152 const Html: boolean = false): boolean; overload;
153 function MessageInputQuery(Window: TCastleWindow; TextList: TStringList;
154 var Answer: string;
155 const MinLength: integer = 0;
156 const MaxLength: integer = 0;
157 const AllowedChars: TSetOfChars = AllChars;
158 const Alignment: THorizontalPosition = DefaultAlign;
159 const Html: boolean = false): boolean; overload;
160 { @groupEnd }
161
162 { Ask user to choose one option from many.
163 ButtonCaptions contain a list of button captions.
164
165 ButtonChars (must have always the same length as ButtonCaptions)
166 contains the chars that are returned for each corresponding button press.
167 The user can also directly press the given key. ButtonChars is not case
168 sensitive, all letters on this list must be different (not only in case).
169 We always return a lowercase letter corresponding to one of ButtonChars letters.
170
171 Example usage:
172
173 @longCode(#
174 case MessageChoice(Window, 'Which fruit to you want to eat?',
175 ['Apple', 'Banana', 'Cancel'],
176 ['a', 'b', CharEscape]) of
177 'a': // ... user pressed "Apple" button or "A" key -> likes apples
178 'b': // ... user pressed "Banana" button or "B" key -> likes bananas
179 CharEscape: // ... user pressed "Cancel" button or "Escape" key -> cancelled
180 end;
181 #)
182
183 When AllowCancel, user can always press Escape
184 to easily cancel the dialog. Regardless if CharEscape is among ButtonChars.
185 We return CharEscape then.
186
187 @groupBegin }
188 function MessageChoice(Window: TCastleWindow; const s: string;
189 const ButtonCaptions: array of string; const ButtonChars: array of char;
190 const Alignment: THorizontalPosition = DefaultAlign;
191 const Html: boolean = false;
192 const AllowCancel: boolean = false): char; overload;
193 function MessageChoice(Window: TCastleWindow; const SArray: array of string;
194 const ButtonCaptions: array of string; const ButtonChars: array of char;
195 const Alignment: THorizontalPosition = DefaultAlign;
196 const Html: boolean = false;
197 const AllowCancel: boolean = false): char; overload;
198 function MessageChoice(Window: TCastleWindow; TextList: TStringList;
199 const ButtonCaptions: array of string; const ButtonChars: array of char;
200 const Alignment: THorizontalPosition = DefaultAlign;
201 const Html: boolean = false;
202 const AllowCancel: boolean = false): char; overload;
203 { @groupEnd }
204
205 { Ask user to press any key, return this key as Keys.TKey.
206
207 Never returns keyNone (which means that keys that cannot be interpreted
208 as Keys.TKey will be ignored, and will not close the dialog box).
209
210 @groupBegin }
211 function MessageKey(Window: TCastleWindow; const S: string;
212 const Alignment: THorizontalPosition = DefaultAlign;
213 const Html: boolean = false): TKey; overload;
214 function MessageKey(Window: TCastleWindow; const SArray: array of string;
215 const Alignment: THorizontalPosition = DefaultAlign;
216 const Html: boolean = false): TKey; overload;
217 function MessageKey(Window: TCastleWindow; TextList: TStringList;
218 const Alignment: THorizontalPosition = DefaultAlign;
219 const Html: boolean = false): TKey; overload;
220 { @groupEnd }
221
222 { Ask user to press anything (key or mouse button or mouse wheel),
223 and return it.
224 The natural use for this is to allow user to configure
225 keybindings of your program, like for TInputShortcut.
226 @groupBegin }
227 procedure MessageKeyMouse(Window: TCastleWindow; const S: string;
228 out Event: TInputPressRelease;
229 const Alignment: THorizontalPosition = DefaultAlign;
230 const Html: boolean = false); overload; deprecated 'use the function form of MessageKeyMouse';
231 procedure MessageKeyMouse(Window: TCastleWindow; TextList: TStringList;
232 out Event: TInputPressRelease;
233 const Alignment: THorizontalPosition = DefaultAlign;
234 const Html: boolean = false); overload; deprecated 'use the function form of MessageKeyMouse';
235 function MessageKeyMouse(Window: TCastleWindow; const S: string;
236 const Alignment: THorizontalPosition = DefaultAlign;
237 const Html: boolean = false): TInputPressRelease; overload;
238 function MessageKeyMouse(Window: TCastleWindow; TextList: TStringList;
239 const Alignment: THorizontalPosition = DefaultAlign;
240 const Html: boolean = false): TInputPressRelease; overload;
241 { @groupEnd }
242
243 function MessageYesNo(Window: TCastleWindow; const s: string;
244 const Alignment: THorizontalPosition = DefaultAlign;
245 const Html: boolean = false): boolean; overload;
246 function MessageYesNo(Window: TCastleWindow; const SArray: array of string;
247 const Alignment: THorizontalPosition = DefaultAlign;
248 const Html: boolean = false): boolean; overload;
249 function MessageYesNo(Window: TCastleWindow; TextList: TStringList;
250 const Alignment: THorizontalPosition = DefaultAlign;
251 const Html: boolean = false): boolean; overload;
252
253 { Ask user to input an unsigned integer.
254
255 Note that AnswerDefault below may be given as Cardinal or as a string.
256 The latter is useful if you want the default answer to be '', i.e. empty string
257 --- no default answer.
258
259 @groupBegin }
260 function MessageInputCardinal(Window: TCastleWindow; const s: string;
261 const AnswerDefault: string;
262 const Alignment: THorizontalPosition = DefaultAlign;
263 const Html: boolean = false): Cardinal; overload;
264 function MessageInputCardinal(Window: TCastleWindow; const s: string;
265 const AnswerDefault: Cardinal;
266 const Alignment: THorizontalPosition = DefaultAlign;
267 const Html: boolean = false): Cardinal; overload;
268 function MessageInputQueryCardinal(Window: TCastleWindow; const Title: string;
269 var Value: Cardinal;
270 const Alignment: THorizontalPosition = DefaultAlign;
271 const Html: boolean = false): boolean;
272 { @groupEnd }
273
274 { Ask user to input a value in hexadecimal.
275 Give MaxWidth = 0 to say that there is no maximum width. }
276 function MessageInputQueryCardinalHex(Window: TCastleWindow; const Title: string;
277 var Value: Cardinal; const MaxWidth: Cardinal;
278 const Alignment: THorizontalPosition = DefaultAlign;
279 const Html: boolean = false): boolean;
280
281 { Ask user to input a floating-point number.
282 Number can use CastleScript expressions, so e.g. "1/30", "2+2" or "pi/2" are allowed too.
283
284 If you give non-empty ValueAsString, it will be used to show
285 the initial value for the user. Otherwise, we will just show
286 FloatToStrDot(Value), which sometimes may be too ugly.
287 For example Value = 0.01 cannot be precisely represented as a floating point
288 number, and FloatToStrDot shows that this is really something like 0.0099xxxxx.
289
290 @groupBegin }
291 function MessageInputQuery(Window: TCastleWindow; const Title: string;
292 var Value: Extended;
293 const ValueAsString: string = '';
294 const Alignment: THorizontalPosition = DefaultAlign;
295 const Html: boolean = false): boolean; overload;
296 function MessageInputQuery(Window: TCastleWindow; const Title: string;
297 var Value: Single;
298 const ValueAsString: string = '';
299 const Alignment: THorizontalPosition = DefaultAlign;
300 const Html: boolean = false): boolean; overload;
301 {$ifndef EXTENDED_EQUALS_DOUBLE}
302 function MessageInputQuery(Window: TCastleWindow; const Title: string;
303 var Value: Double;
304 const ValueAsString: string = '';
305 const Alignment: THorizontalPosition = DefaultAlign;
306 const Html: boolean = false): boolean; overload;
307 {$endif not EXTENDED_EQUALS_DOUBLE}
308 { @groupEnd }
309
310 function MessageInputQueryVector3(
311 Window: TCastleWindow; const Title: string;
312 var Value: TVector3;
313 const Alignment: THorizontalPosition = DefaultAlign;
314 const Html: boolean = false): boolean;
315
316 function MessageInputQueryVector4(
317 Window: TCastleWindow; const Title: string;
318 var Value: TVector4;
319 const Alignment: THorizontalPosition = DefaultAlign;
320 const Html: boolean = false): boolean;
321
322 var
323 { Change MessageOK behavior to create @link(TViewDialogOK)
324 and push it (using @link(TCastleContainer.PushView))
325 and immediately return, without waiting for user confirmation.
326
327 Why you may want to use this (or not to use this)?
328
329 @unorderedList(
330 @item(
331 This is the only way to make MessageOK working on iOS
332 (where Application.ProcessMessages is not available).)
333
334 @item(
335 This looks a little better in case user may resize the window while
336 the MessageOK is running, or when something animates under MessageOK.
337 When this is @true, the view underneath redraws properly, so it can adjust
338 to window size and animate. Otherwise, all MessageXxx methods in this unit
339 only show a static screenshot underneath, that is scaled if needed.)
340
341 @item(
342 On the other hand, the notification about unhandled exceptions
343 (done by CastleWindow automatically, using MessageOK) is a little safer
344 when this is @false. When this is @false, there's a greater chance that
345 the problematic code (e.g. the update method of some other TCastleView)
346 it disabled during the display of the error messsage.)
347 )
348
349 If you turn this on, then you should organize your whole application
350 into views using TCastleView. You can even pause the running game
351 in overridden @link(TCastleView.Pause), to make game paused when
352 the message dialog is displayed.
353
354 Note that this feature doesn't change other routines in CastleMessages.
355 For example @link(MessageYesNo) is still a modal function (it waits
356 for user input), and it simply doesn't work on iOS.
357 If you want to use TCastleView to manage all dialogs,
358 then use explicitly views like @link(TViewDialogYesNo) from
359 the @link(CastleDialogViews) unit.
360 }
361 MessageOKPushesView: boolean;
362
363 implementation
364
365 uses SysUtils,
366 CastleImages, CastleClassUtils, CastleInternalWindowModes, CastleLog,
367 CastleUIControls, CastleDialogViews, CastleScriptParser;
368
369 { MessageCore ---------------------------------------------------------------- }
370
371 { Show a modal dialog window. Uses TGLMode to temporarily replace
372 normal event processing (on this Window),
373 to wait until the dialog is answered by the user. }
374 procedure MessageCore(const Window: TCastleWindow; const View: TViewDialog);
375 var
376 SavedMode: TGLMode;
377 begin
378 // WritelnLogMultiline('Message', TextList.Text);
379
380 { TODO:
381 This way of starting the View manually is a hack.
382
383 This hack avoids calling InternalStart / InternalStop,
384 which modify container view stack (we don't want this)
385 and do other useful things (we don't need it *for now*) like
386
387 - handling WaitingForRender
388 - handling DesignUrl
389 - freeing FFreeAtStop
390 - setting FStartContainer
391
392 A cleaner way would be to use it like a proper view.
393 - set View.PopOnAnswered := true;
394 - do not call
395 View.Start;
396 ...
397 Window.Controls.InsertFront(View);
398 explicitly.
399 - do call
400 Window.Container.View := View;
401
402 Problem of the cleaner solution: it means we'll change container FViewStack,
403 wel'll do stop (and later start) on the user TCastleView descendants.
404 We don't want this, MessageOK (esp. when it is used to display a debug message
405 in case of unhandled exception on mobile) should avoid calling user code of views
406 (as user views may be in bad view). }
407
408 View.BackgroundScreenshot := true;
409 View.PopOnAnswered := false;
410 View.SaveScreenIfNecessary(Window.Container); // get a screenshot before TGLMode.CreateReset
411 View.Start;
412
413 { Using @NoClose below allows to safely use MessageXxx inside own OnCloseQuery,
414 like "if MessageYesNo('Are you sure ?') then Window.Close;" }
415 SavedMode := TGLMode.CreateReset(Window, nil, nil, @NoClose);
416 try
417 { use View directly as UI control, not using TCastleContainer.PushView
418 nor setting TCastleContainer.View,
419 because we can (TGLMode already took care to pause everything),
420 and this way we don't mess TCastleView stack (in case game is using it). }
421 Window.Controls.InsertFront(View);
422
423 repeat
424 { WaitForMessage = false is necessary, otherwise SecondsPassed
425 for update would be large. }
426 Application.ProcessMessage(false, true)
427 until View.Answered;
428 finally
429 FreeAndNil(SavedMode);
430 { Message boxes should not leave the keys in false/strange pressed view. }
431 Window.Pressed.Clear;
432 end;
433
434 View.Stop;
435 end;
436
437 { MessageOK ------------------------------------------------------------------ }
438
439 procedure MessageOK(Window: TCastleWindow; const SArray: array of string;
440 const Alignment: THorizontalPosition = DefaultAlign;
441 const Html: boolean = false);
442 var
443 TextList: TStringList;
444 begin
445 TextList := TStringList.Create;
446 try
447 AddStrArrayToStrings(SArray, TextList);
448 MessageOK(Window, TextList, Alignment, Html);
449 finally TextList.Free end;
450 end;
451
452 procedure MessageOK(Window: TCastleWindow; const s: string;
453 const Alignment: THorizontalPosition = DefaultAlign;
454 const Html: boolean = false);
455 var
456 TextList: TStringList;
457 begin
458 TextList := TStringList.Create;
459 try
460 Strings_SetText(TextList, s);
461 MessageOK(Window, TextList, Alignment, Html);
462 finally TextList.free end;
463 end;
464
465 procedure MessageOK(Window: TCastleWindow; TextList: TStringList;
466 const Alignment: THorizontalPosition = DefaultAlign;
467 const Html: boolean = false);
468 var
469 View: TViewDialogOK;
470 begin
471 View := TViewDialogOK.Create(Window);
472 View.Text.Assign(TextList);
473 View.Alignment := Alignment;
474 View.Html := Html;
475 if MessageOKPushesView then
476 begin
477 Window.Container.PushView(View);
478 end else
479 begin
480 MessageCore(Window, View);
481 FreeAndNil(View);
482 end;
483 end;
484
485 { MessageInput --------------------------------------------------------------- }
486
487 function MessageInput(Window: TCastleWindow; const s: string;
488 const answerDefault: string;
489 const MinLength: integer; const MaxLength: integer;
490 const AllowedChars: TSetOfChars;
491 const Alignment: THorizontalPosition;
492 const Html: boolean): string;
493 var
494 TextList: TStringList;
495 begin
496 TextList := TStringList.Create;
497 try
498 Strings_SetText(TextList, s);
499 result := MessageInput(Window, TextList, answerDefault,
500 MinLength, MaxLength, AllowedChars, Alignment, Html);
501 finally TextList.free end;
502 end;
503
504 function MessageInput(Window: TCastleWindow; TextList: TStringList;
505 const answerDefault: string;
506 const MinLength: integer; const MaxLength: integer;
507 const AllowedChars: TSetOfChars;
508 const Alignment: THorizontalPosition;
509 const Html: boolean): string;
510 var
511 View: TViewDialogInput;
512 begin
513 View := TViewDialogInput.Create(Window);
514 try
515 View.Text.Assign(TextList);
516 View.Alignment := Alignment;
517 View.Html := Html;
518 View.MinLength := MinLength;
519 View.MaxLength := MaxLength;
520 View.AllowedChars := AllowedChars;
521 View.Answer := AnswerDefault;
522 View.CanCancel := false;
523
524 MessageCore(Window, View);
525 Result := View.Answer;
526 finally FreeAndNil(View) end;
527 end;
528
529 function MessageInputQuery(Window: TCastleWindow; const s: string;
530 var Answer: string;
531 const MinLength: integer; const MaxLength: integer;
532 const AllowedChars: TSetOfChars;
533 const Alignment: THorizontalPosition;
534 const Html: boolean): boolean;
535 var
536 TextList: TStringList;
537 begin
538 TextList := TStringList.Create;
539 try
540 Strings_SetText(TextList, s);
541 result := MessageInputQuery(Window, TextList, answer, MinLength,
542 MaxLength, AllowedChars, Alignment, Html);
543 finally TextList.free end;
544 end;
545
546 function MessageInputQuery(Window: TCastleWindow; TextList: TStringList;
547 var Answer: string;
548 const MinLength: integer; const MaxLength: integer;
549 const AllowedChars: TSetOfChars;
550 const Alignment: THorizontalPosition;
551 const Html: boolean): boolean;
552 var
553 View: TViewDialogInput;
554 begin
555 View := TViewDialogInput.Create(Window);
556 try
557 View.Text.Assign(TextList);
558 View.Alignment := Alignment;
559 View.Html := Html;
560 View.MinLength := MinLength;
561 View.MaxLength := MaxLength;
562 View.AllowedChars := AllowedChars;
563 View.Answer := Answer;
564 View.CanCancel := true;
565
566 MessageCore(Window, View);
567 Result := not View.AnswerCancelled;
568 if Result then // modify Answer only if not cancelled
569 Answer := View.Answer;
570 finally FreeAndNil(View) end;
571 end;
572
573 { MessageChoice -------------------------------------------------------------- }
574
575 function MessageChoice(Window: TCastleWindow;
576 const s: string;
577 const ButtonCaptions: array of string; const ButtonChars: array of char;
578 const Alignment: THorizontalPosition;
579 const Html, AllowCancel: boolean): char;
580 var
581 TextList: TStringList;
582 begin
583 TextList := TStringList.Create;
584 try
585 Strings_SetText(TextList, s);
586 Result := MessageChoice(Window, TextList, ButtonCaptions, ButtonChars, Alignment, Html, AllowCancel);
587 finally TextList.free end;
588 end;
589
590 function MessageChoice(Window: TCastleWindow;
591 const SArray: array of string;
592 const ButtonCaptions: array of string; const ButtonChars: array of char;
593 const Alignment: THorizontalPosition;
594 const Html, AllowCancel: boolean): char; overload;
595 var
596 TextList: TStringList;
597 begin
598 TextList := TStringList.Create;
599 try
600 AddStrArrayToStrings(SArray, TextList);
601 Result := MessageChoice(Window, TextList, ButtonCaptions, ButtonChars, Alignment, Html, AllowCancel);
602 finally TextList.Free end;
603 end;
604
605 function MessageChoice(Window: TCastleWindow; TextList: TStringList;
606 const ButtonCaptions: array of string; const ButtonChars: array of char;
607 const Alignment: THorizontalPosition;
608 const Html, AllowCancel: boolean): char; overload;
609 var
610 View: TViewDialogChoice;
611 I: Integer;
612 begin
613 View := TViewDialogChoice.Create(Window);
614 try
615 View.Text.Assign(TextList);
616 View.Alignment := Alignment;
617 View.Html := Html;
618 //View.ButtonCaptions := ButtonCaptions;
619 SetLength(View.ButtonCaptions, High(ButtonCaptions) + 1);
620 for I := 0 to High(ButtonCaptions) do
621 View.ButtonCaptions[I] := ButtonCaptions[I];
622 //View.ButtonChars := ButtonChars;
623 SetLength(View.ButtonChars, High(ButtonChars) + 1);
624 for I := 0 to High(ButtonChars) do
625 View.ButtonChars[I] := ButtonChars[I];
626 View.AllowCancel := AllowCancel;
627 MessageCore(Window, View);
628 Result := View.Answer;
629 finally FreeAndNil(View) end;
630 end;
631
632 { MessageKey ----------------------------------------------------------------- }
633
634 function MessageKey(Window: TCastleWindow; const S: string;
635 const Alignment: THorizontalPosition;
636 const Html: boolean): TKey;
637 var
638 TextList: TStringList;
639 begin
640 TextList := TStringList.Create;
641 try
642 Strings_SetText(TextList, S);
643 Result := MessageKey(Window, TextList, Alignment, Html);
644 finally TextList.free end;
645 end;
646
647 function MessageKey(Window: TCastleWindow; const SArray: array of string;
648 const Alignment: THorizontalPosition;
649 const Html: boolean): TKey;
650 var
651 TextList: TStringList;
652 begin
653 TextList := TStringList.Create;
654 try
655 AddStrArrayToStrings(SArray, TextList);
656 Result := MessageKey(Window, TextList, Alignment, Html);
657 finally TextList.Free end;
658 end;
659
660 function MessageKey(Window: TCastleWindow; TextList: TStringList;
661 const Alignment: THorizontalPosition;
662 const Html: boolean): TKey;
663 var
664 View: TViewDialogKey;
665 begin
666 View := TViewDialogKey.Create(Window);
667 try
668 View.Text.Assign(TextList);
669 View.Alignment := Alignment;
670 View.Html := Html;
671 MessageCore(Window, View);
672 Result := View.Answer;
673 finally FreeAndNil(View) end;
674 end;
675
676 { MessageKeyMouse ------------------------------------------------------------ }
677
678 function MessageKeyMouse(Window: TCastleWindow; const S: string;
679 const Alignment: THorizontalPosition; const Html: boolean): TInputPressRelease;
680 var
681 TextList: TStringList;
682 begin
683 TextList := TStringList.Create;
684 try
685 Strings_SetText(TextList, S);
686 Result := MessageKeyMouse(Window, TextList, Alignment, Html);
687 finally TextList.Free end;
688 end;
689
690 function MessageKeyMouse(Window: TCastleWindow; TextList: TStringList;
691 const Alignment: THorizontalPosition; const Html: boolean): TInputPressRelease;
692 var
693 View: TViewDialogPressEvent;
694 begin
695 View := TViewDialogPressEvent.Create(Window);
696 try
697 View.Text.Assign(TextList);
698 View.Alignment := Alignment;
699 View.Html := Html;
700 MessageCore(Window, View);
701 Result := View.Answer;
702 finally FreeAndNil(View) end;
703 end;
704
705 procedure MessageKeyMouse(Window: TCastleWindow; const S: string;
706 out Event: TInputPressRelease; const Alignment: THorizontalPosition;
707 const Html: boolean);
708 begin
709 Event := MessageKeyMouse(Window, S, Alignment, Html);
710 end;
711
712 procedure MessageKeyMouse(Window: TCastleWindow; TextList: TStringList;
713 out Event: TInputPressRelease; const Alignment: THorizontalPosition;
714 const Html: boolean);
715 begin
716 Event := MessageKeyMouse(Window, TextList, Alignment, Html);
717 end;
718
719 { MessageYesNo --------------------------------------------------------------- }
720
721 function MessageYesNo(Window: TCastleWindow; const s: string;
722 const Alignment: THorizontalPosition;
723 const Html: boolean): boolean; overload;
724 var
725 TextList: TStringList;
726 begin
727 TextList := TStringList.Create;
728 try
729 Strings_SetText(TextList, S);
730 Result := MessageYesNo(Window, TextList, Alignment, Html);
731 finally TextList.free end;
732 end;
733
734 function MessageYesNo(Window: TCastleWindow; const SArray: array of string;
735 const Alignment: THorizontalPosition;
736 const Html: boolean): boolean; overload;
737 var
738 TextList: TStringList;
739 begin
740 TextList := TStringList.Create;
741 try
742 AddStrArrayToStrings(SArray, TextList);
743 Result := MessageYesNo(Window, TextList, Alignment, Html);
744 finally TextList.Free end;
745 end;
746
747 function MessageYesNo(Window: TCastleWindow; TextList: TStringList;
748 const Alignment: THorizontalPosition;
749 const Html: boolean): boolean; overload;
750 var
751 View: TViewDialogYesNo;
752 begin
753 View := TViewDialogYesNo.Create(Window);
754 try
755 View.Text.Assign(TextList);
756 View.Alignment := Alignment;
757 View.Html := Html;
758 MessageCore(Window, View);
759 Result := View.Answer;
760 finally FreeAndNil(View) end;
761 end;
762
763 { MessageInputCardinal ------------------------------------------------------- }
764
765 function MessageInputCardinal(Window: TCastleWindow; const s: string;
766 const AnswerDefault: string; const Alignment: THorizontalPosition;
767 const Html: boolean): Cardinal;
768 begin
769 result := StrToInt( MessageInput(Window, s, AnswerDefault,
770 1, 0, ['0'..'9'], Alignment, Html) );
771 end;
772
773 function MessageInputCardinal(Window: TCastleWindow; const s: string;
774 const AnswerDefault: Cardinal; const Alignment: THorizontalPosition;
775 const Html: boolean): Cardinal;
776 begin
777 result := MessageInputCardinal(Window, s, IntToStr(AnswerDefault), Alignment, Html);
778 end;
779
780 function MessageInputQueryCardinal(Window: TCastleWindow;
781 const Title: string;
782 var Value: Cardinal; const Alignment: THorizontalPosition;
783 const Html: boolean): boolean;
784 var
785 ValueStr: string;
786 begin
787 ValueStr := IntToStr(Value);
788 Result := MessageInputQuery(Window, Title, ValueStr, 1, 0, ['0'..'9'], Alignment, Html);
789 if Result then
790 Value := StrToInt(ValueStr);
791 end;
792
793 function MessageInputQueryCardinalHex(Window: TCastleWindow; const Title: string;
794 var Value: Cardinal; const MaxWidth: Cardinal;
795 const Alignment: THorizontalPosition;
796 const Html: boolean): boolean;
797 var
798 ValueStr: string;
799 begin
800 ValueStr := IntToHex(Value, 4);
801 Result := MessageInputQuery(Window, Title, ValueStr, 1, MaxWidth,
802 ['0'..'9', 'a'..'f', 'A'..'F'], Alignment, Html);
803 if Result then
804 Value := StrHexToInt(ValueStr);
805 end;
806
807 { MessageInputQuery on floats ------------------------------------------------ }
808
809 function MessageInputQuery(Window: TCastleWindow; const Title: string;
810 var Value: Extended; const ValueAsString: string;
811 const Alignment: THorizontalPosition;
812 const Html: boolean): boolean;
813 var
814 s: string;
815 begin
816 Result := false;
817 if ValueAsString <> '' then
818 S := ValueAsString
819 else
820 S := FloatToStrDot(Value);
821 if MessageInputQuery(Window, Title, S, 0, 0, AllChars, Alignment, Html) then
822 begin
823 try
824 Value := ParseConstantFloatExpression(s);
825 Result := true;
826 except
827 on E: EConvertError do
828 MessageOK(Window, 'Invalid floating point value : ' +E.Message);
829 end;
830 end;
831 end;
832
833 function MessageInputQuery(Window: TCastleWindow; const Title: string;
834 var Value: Single; const ValueAsString: string;
835 const Alignment: THorizontalPosition;
836 const Html: boolean): boolean;
837 var
838 ValueExtended: Extended;
839 begin
840 ValueExtended := Value;
841 Result := MessageInputQuery(Window, Title, ValueExtended, ValueAsString, Alignment, Html);
842 if Result then
843 Value := ValueExtended;
844 end;
845
846 {$ifndef EXTENDED_EQUALS_DOUBLE}
847 function MessageInputQuery(Window: TCastleWindow; const Title: string;
848 var Value: Double; const ValueAsString: string;
849 const Alignment: THorizontalPosition;
850 const Html: boolean): boolean;
851 var
852 ValueExtended: Extended;
853 begin
854 ValueExtended := Value;
855 Result := MessageInputQuery(Window, Title, ValueExtended, ValueAsString, Alignment, Html);
856 if Result then
857 Value := ValueExtended;
858 end;
859 {$endif not EXTENDED_EQUALS_DOUBLE}
860
861 { MessageInputQueryVector3 --------------------------------------------- }
862
863 function MessageInputQueryVector3(
864 Window: TCastleWindow; const Title: string;
865 var Value: TVector3; const Alignment: THorizontalPosition;
866 const Html: boolean): boolean;
867 var s: string;
868 begin
869 Result := false;
870 s := Format('%g %g %g', [Value[0], Value[1], Value[2]]);
871 if MessageInputQuery(Window, Title, s, 0, 0, AllChars, Alignment, Html) then
872 begin
873 try
874 Value := Vector3FromStr(s);
875 Result := true;
876 except
877 on E: EConvertError do
878 MessageOK(Window, 'Invalid vector 3 value : ' + E.Message);
879 end;
880 end;
881 end;
882
883 { MessageInputQueryVector4 --------------------------------------------- }
884
885 function MessageInputQueryVector4(
886 Window: TCastleWindow; const Title: string;
887 var Value: TVector4; const Alignment: THorizontalPosition;
888 const Html: boolean): boolean;
889 var
890 s: string;
891 begin
892 Result := false;
893 s := Format('%g %g %g %g', [Value[0], Value[1], Value[2], Value[3]]);
894 if MessageInputQuery(Window, Title, s, 0, 0, AllChars, Alignment, Html) then
895 begin
896 try
897 Value := Vector4FromStr(s);
898 Result := true;
899 except
900 on E: EConvertError do
901 MessageOK(Window, 'Invalid vector 4 value : ' + E.Message);
902 end;
903 end;
904 end;
905
906 end.