| 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 (to display some information, or ask user for confirmation, |
| 17 |
or ask user to input a simple value) as a user-interface view (@link(TCastleView)). |
| 18 |
This unit defines the user-interface view classes |
| 19 |
(@link(TCastleView) descendants) to display given dialog. |
| 20 |
|
| 21 |
If you use @link(TCastleWindow) with most platforms (but not iOS), |
| 22 |
then it's usually more comfortable to use the unit @link(CastleMessages) |
| 23 |
instead of this unit. |
| 24 |
Using the @link(CastleMessages), you get comfortable functions |
| 25 |
like @link(MessageOK) and @link(MessageYesNo) that |
| 26 |
wait until user presses a button (like "OK", "Yes", "No") to close the dialog. |
| 27 |
This is comfortable to use, as you can write code like this: |
| 28 |
|
| 29 |
@longCode(# |
| 30 |
if MessageYesNo(Window, 'Are you sure you want to delete this file?') then |
| 31 |
DeleteFile(...); |
| 32 |
#) |
| 33 |
|
| 34 |
Underneath, the @link(MessageYesNo) will use a @link(TViewDialogYesNo), |
| 35 |
making sure that your normal window callbacks are redirected as appropriate. |
| 36 |
But you don't need to be concerned with this. |
| 37 |
|
| 38 |
However, if you need to work on iOS, then you cannot use most of routines |
| 39 |
from the @link(CastleMessages) unit. You can use @link(MessageOK) if you turn on |
| 40 |
the @link(MessageOKPushesView) flag, but nothing else. E.g. @link(MessageYesNo) |
| 41 |
cannot work on iOS now. Making modal boxes like that is not supported on iOS. |
| 42 |
|
| 43 |
In this case you should use this unit and instantiate the |
| 44 |
user-interface classes yourself, and you need to |
| 45 |
@italic(organize your whole game using TCastleView classes). |
| 46 |
See https://castle-engine.io/views about how to use @link(TCastleView). Like this: |
| 47 |
|
| 48 |
@longCode(# |
| 49 |
type |
| 50 |
TMyGameView = class(TCastleView) |
| 51 |
private |
| 52 |
DialogAskDeleteFile: TViewDialogYesNo; |
| 53 |
public |
| 54 |
function Press(const Event: TInputPressRelease): boolean; override; |
| 55 |
procedure Resume; override; |
| 56 |
end; |
| 57 |
|
| 58 |
function TMyGameView.Press(const Event: TInputPressRelease): boolean; override; |
| 59 |
begin |
| 60 |
Result := inherited; |
| 61 |
if Result then Exit; |
| 62 |
|
| 63 |
if Event.IsKey(keyEnter) then |
| 64 |
begin |
| 65 |
DialogAskDeleteFile := TViewDialogYesNo.Create(Self); |
| 66 |
DialogAskDeleteFile.Caption := 'Are you sure you want to delete this file?'; |
| 67 |
Container.PushView(DialogAskDeleteFile); |
| 68 |
end; |
| 69 |
end; |
| 70 |
|
| 71 |
procedure TMyGameView.Resume; |
| 72 |
begin |
| 73 |
inherited; |
| 74 |
if DialogAskDeleteFile <> nil then // returning from DialogAskDeleteFile |
| 75 |
begin |
| 76 |
if DialogAskDeleteFile.Answer then |
| 77 |
DeleteFile(...); |
| 78 |
FreeAndNil(DialogAskDeleteFile); |
| 79 |
end; |
| 80 |
end; |
| 81 |
#) |
| 82 |
*) |
| 83 |
|
| 84 |
unit CastleDialogViews; |
| 85 |
|
| 86 |
{$I castleconf.inc} |
| 87 |
|
| 88 |
interface |
| 89 |
|
| 90 |
uses Classes, Math, |
| 91 |
CastleGLUtils, CastleUtils, CastleImages, |
| 92 |
CastleStringUtils, CastleVectors, CastleKeysMouse, CastleControls, |
| 93 |
CastleRectangles, CastleColors, CastleUIControls, |
| 94 |
CastleFonts, CastleInternalRichText, CastleTimeUtils; |
| 95 |
|
| 96 |
type |
| 97 |
{ Abstract class for a modal dialog user-interface view. |
| 98 |
See unit @link(CastleDialogViews) documentation for example usage. } |
| 99 |
TViewDialog = class abstract(TCastleView) |
| 100 |
strict private |
| 101 |
type |
| 102 |
{$define read_interface} |
| 103 |
{$I castledialogviews_dialog.inc} |
| 104 |
{$undef read_interface} |
| 105 |
var |
| 106 |
FText: TStrings; |
| 107 |
FHtml: boolean; |
| 108 |
FAlignment: THorizontalPosition; |
| 109 |
FAnswered: boolean; |
| 110 |
FBackground: boolean; |
| 111 |
FBackgroundColor: TCastleColor; |
| 112 |
FBackgroundScreenshot: boolean; |
| 113 |
FPopOnAnswered: boolean; |
| 114 |
FDialog: TDialog; // non-nil only between Start and Stop |
| 115 |
FUnusedSaveScreen: TCastleImage; |
| 116 |
function GetCaption: String; |
| 117 |
procedure SetCaption(const Value: String); |
| 118 |
function GetInputText: String; |
| 119 |
procedure SetInputText(const Value: String); |
| 120 |
protected |
| 121 |
type |
| 122 |
TButtonArray = array of TCastleButton; |
| 123 |
procedure InitializeButtons(var Buttons: TButtonArray); virtual; |
| 124 |
function DrawInputText: boolean; virtual; |
| 125 |
procedure DoAnswered; |
| 126 |
property InputText: String read GetInputText write SetInputText; |
| 127 |
public |
| 128 |
const |
| 129 |
DefaultAlignment = hpLeft; |
| 130 |
constructor Create(AOwner: TComponent); override; |
| 131 |
destructor Destroy; override; |
| 132 |
procedure Start; override; |
| 133 |
procedure Stop; override; |
| 134 |
property InterceptInput default true; |
| 135 |
|
| 136 |
{ Save screen @italic(now) to be used by subsequent @link(Start). |
| 137 |
Does a screenshot only if our current properties (@link(Background), |
| 138 |
@link(BackgroundScreenshot), @link(BackgroundColor)) indicate screenshot is needed. |
| 139 |
It allows to make a screenshot earlier than at @link(Start) call. } |
| 140 |
procedure SaveScreenIfNecessary(const AContainer: TCastleContainer); |
| 141 |
|
| 142 |
{ When user answers the dialog, this is set to @true. |
| 143 |
The view also normally does TCastleContainer.PopView, so there's no need to check |
| 144 |
this property, unless you set @link(PopOnAnswered) to @false. } |
| 145 |
property Answered: boolean read FAnswered; |
| 146 |
|
| 147 |
{ Caption displayed in the dialog. } |
| 148 |
property Text: TStrings read FText; |
| 149 |
|
| 150 |
{ Caption displayed in the dialog, as a simple string. |
| 151 |
This is just a shortcut to get/set @link(Text) as a single string. |
| 152 |
|
| 153 |
Use LineEnding or NL constant when setting this to indicate a newline. |
| 154 |
The two examples below are equivalent: |
| 155 |
|
| 156 |
@longCode(# |
| 157 |
// one way |
| 158 |
ViewDialogOK.Text.Clear; |
| 159 |
ViewDialogOK.Text.Add('First line'); |
| 160 |
ViewDialogOK.Text.Add('Second line'); |
| 161 |
|
| 162 |
// alternative way to do the same |
| 163 |
ViewDialogOK.Caption := 'First line' + LineEnding + 'Second line'; |
| 164 |
#) |
| 165 |
} |
| 166 |
property Caption: String read GetCaption write SetCaption stored false; |
| 167 |
|
| 168 |
{ Horizontal alignment of the text. } |
| 169 |
property Alignment: THorizontalPosition |
| 170 |
read FAlignment write FAlignment default DefaultAlignment; |
| 171 |
|
| 172 |
{ Enable a subset of HTML to mark font changes inside the text. |
| 173 |
See the TCastleAbstractFont.PrintStrings for a description of supported |
| 174 |
HTML constructs. } |
| 175 |
property Html: boolean read FHtml write FHtml default false; |
| 176 |
|
| 177 |
{ Obscure the view underneath with our own background (using a color or screenshot). |
| 178 |
|
| 179 |
The obscuring background is defined by @link(BackgroundColor). |
| 180 |
It may be just a solid opaque color. |
| 181 |
Or it may be a partially-transparent or even completely transparent |
| 182 |
color, showing the view underneath (or showing the screenshot |
| 183 |
of the view underneath, if @link(BackgroundScreenshot)). } |
| 184 |
property Background: boolean read FBackground write FBackground default false; |
| 185 |
|
| 186 |
{ Color of the background obscuring the view underneath, |
| 187 |
if @link(Background) is @true. |
| 188 |
This color may be partially-transparent (e.g. to visually "dim" the view |
| 189 |
underneath) or even completely transparent (alpha 0). |
| 190 |
|
| 191 |
Default is Theme.BackgroundColor, |
| 192 |
which is dark with alpha = 0.5, so it will dim the view underneath. |
| 193 |
Sometimes (under exception handler) it's Theme.BackgroundOpaqueColor. } |
| 194 |
property BackgroundColor: TCastleColor read FBackgroundColor write FBackgroundColor; |
| 195 |
|
| 196 |
{ Initialize the background by taking a screenshot of the current screen |
| 197 |
when the view was started. |
| 198 |
This screenshot is shown, instead of actually rendering the view underneath, |
| 199 |
under the @link(BackgroundColor), when @link(Background) is @true. |
| 200 |
|
| 201 |
This is less functional (when the user scales the window, |
| 202 |
the screenshot is stretched too), but is safer: |
| 203 |
it means that the view underneath does not need to be "renderable" anymore. } |
| 204 |
property BackgroundScreenshot: boolean |
| 205 |
read FBackgroundScreenshot write FBackgroundScreenshot default false; |
| 206 |
|
| 207 |
{ Should the view do @link(TCastleContainer.PopView) when answered. |
| 208 |
This is usually most natural. } |
| 209 |
property PopOnAnswered: boolean |
| 210 |
read FPopOnAnswered write FPopOnAnswered default true; |
| 211 |
|
| 212 |
{$define read_interface_class} |
| 213 |
{$I auto_generated_persistent_vectors/tviewdialog_persistent_vectors.inc} |
| 214 |
{$undef read_interface_class} |
| 215 |
end; |
| 216 |
|
| 217 |
{ Wait for simple confirmation ("OK") from user. |
| 218 |
See unit @link(CastleDialogViews) documentation for example usage. } |
| 219 |
TViewDialogOK = class(TViewDialog) |
| 220 |
strict private |
| 221 |
procedure ButtonOKClick(Sender: TObject); |
| 222 |
protected |
| 223 |
procedure InitializeButtons(var Buttons: TViewDialog.TButtonArray); override; |
| 224 |
public |
| 225 |
function Press(const Event: TInputPressRelease): boolean; override; |
| 226 |
end; |
| 227 |
|
| 228 |
{ Ask user a simple "yes" / "no" question. |
| 229 |
See unit @link(CastleDialogViews) documentation for example usage. } |
| 230 |
TViewDialogYesNo = class(TViewDialog) |
| 231 |
strict private |
| 232 |
FAnswer: boolean; |
| 233 |
procedure ButtonYesClick(Sender: TObject); |
| 234 |
procedure ButtonNoClick(Sender: TObject); |
| 235 |
protected |
| 236 |
procedure InitializeButtons(var Buttons: TViewDialog.TButtonArray); override; |
| 237 |
public |
| 238 |
function Press(const Event: TInputPressRelease): boolean; override; |
| 239 |
{ User answer to the dialog question, defined when @link(Answered). } |
| 240 |
property Answer: boolean read FAnswer; |
| 241 |
end; |
| 242 |
|
| 243 |
{ Ask user to choose from a number of options. |
| 244 |
Each choice is shown as a button, user can also press the appropriate character. |
| 245 |
ButtonChars length must be always equal to ButtonCaptions. |
| 246 |
See unit @link(CastleDialogViews) documentation for example usage. } |
| 247 |
TViewDialogChoice = class(TViewDialog) |
| 248 |
strict private |
| 249 |
FAnswer: char; |
| 250 |
procedure ButtonClick(Sender: TObject); |
| 251 |
protected |
| 252 |
procedure InitializeButtons(var Buttons: TViewDialog.TButtonArray); override; |
| 253 |
public |
| 254 |
ButtonCaptions: array of string; |
| 255 |
ButtonChars: array of char; |
| 256 |
AllowCancel: boolean; |
| 257 |
function Press(const Event: TInputPressRelease): boolean; override; |
| 258 |
{ User answer to the dialog question, defined when @link(Answered). |
| 259 |
This is one of the ButtonChars. } |
| 260 |
property Answer: char read FAnswer; |
| 261 |
end; |
| 262 |
|
| 263 |
{ Ask user to input a string, or cancel. |
| 264 |
See unit @link(CastleDialogViews) documentation for example usage. } |
| 265 |
TViewDialogInput = class(TViewDialog) |
| 266 |
strict private |
| 267 |
FAllowedChars: TSetOfChars; |
| 268 |
FMinLength, FMaxLength: Cardinal; |
| 269 |
FCanCancel: boolean; |
| 270 |
FAnswerCancelled: boolean; |
| 271 |
procedure ButtonOKClick(Sender: TObject); |
| 272 |
procedure ButtonCancelClick(Sender: TObject); |
| 273 |
function GetAnswer: String; |
| 274 |
procedure SetAnswer(const Value: String); |
| 275 |
protected |
| 276 |
procedure InitializeButtons(var Buttons: TViewDialog.TButtonArray); override; |
| 277 |
function DrawInputText: boolean; override; |
| 278 |
public |
| 279 |
function Press(const Event: TInputPressRelease): boolean; override; |
| 280 |
|
| 281 |
{ Allowed characters that user can input. } |
| 282 |
property AllowedChars: TSetOfChars read FAllowedChars write FAllowedChars; |
| 283 |
|
| 284 |
{ Min length of user input. } |
| 285 |
property MinLength: Cardinal read FMinLength write FMinLength default 0; |
| 286 |
|
| 287 |
{ Max length of user input. |
| 288 |
Value of 0 (default) means "no limit". } |
| 289 |
property MaxLength: Cardinal read FMaxLength write FMaxLength default 0; |
| 290 |
|
| 291 |
{ User can cancel the input by pressing a button like "cancel". } |
| 292 |
property CanCancel: boolean read FCanCancel write FCanCancel default false; |
| 293 |
|
| 294 |
{ User clicked "cancel" instead of accepting an answer. |
| 295 |
This is defined only when @link(Answered). |
| 296 |
This is possible only if @link(CanCancel). } |
| 297 |
property AnswerCancelled: boolean read FAnswerCancelled; |
| 298 |
|
| 299 |
{ The user input. May be set before starting the view. |
| 300 |
After the view stopped, if @link(Answered), then this contains user answer. |
| 301 |
You should ignore it if @link(AnswerCancelled). } |
| 302 |
property Answer: String read GetAnswer write SetAnswer; |
| 303 |
end; |
| 304 |
|
| 305 |
{ Ask user a press any key, and return this key. |
| 306 |
See unit @link(CastleDialogViews) documentation for example usage. } |
| 307 |
TViewDialogKey = class(TViewDialog) |
| 308 |
strict private |
| 309 |
FAnswer: TKey; |
| 310 |
public |
| 311 |
function Press(const Event: TInputPressRelease): boolean; override; |
| 312 |
{ Key pressed by user, defined when @link(Answered). } |
| 313 |
property Answer: TKey read FAnswer; |
| 314 |
end; |
| 315 |
|
| 316 |
{ Ask user a press anything (key, mouse button, mouse wheel), |
| 317 |
for example to configure a keybinding for a game. |
| 318 |
See unit @link(CastleDialogViews) documentation for example usage. } |
| 319 |
TViewDialogPressEvent = class(TViewDialog) |
| 320 |
strict private |
| 321 |
FAnswer: TInputPressRelease; |
| 322 |
public |
| 323 |
function Press(const Event: TInputPressRelease): boolean; override; |
| 324 |
{ Key pressed by user, defined when @link(Answered). } |
| 325 |
property Answer: TInputPressRelease read FAnswer; |
| 326 |
end; |
| 327 |
|
| 328 |
implementation |
| 329 |
|
| 330 |
uses SysUtils; |
| 331 |
|
| 332 |
{$define read_implementation} |
| 333 |
{$I castledialogviews_dialog.inc} |
| 334 |
|
| 335 |
resourcestring |
| 336 |
SOk = 'OK'; |
| 337 |
SCancel = 'Cancel'; |
| 338 |
SYes = 'Yes'; |
| 339 |
SNo = 'No'; |
| 340 |
|
| 341 |
{ TViewDialog ------------------------------------------------------------- } |
| 342 |
|
| 343 |
constructor TViewDialog.Create(AOwner: TComponent); |
| 344 |
begin |
| 345 |
inherited; |
| 346 |
FText := TStringList.Create; |
| 347 |
FAlignment := DefaultAlignment; |
| 348 |
FBackground := true; |
| 349 |
if Theme.InternalMessageFallbackLook then |
| 350 |
FBackgroundColor := Vector4(Theme.BackgroundOpaqueColor, 1) |
| 351 |
else |
| 352 |
FBackgroundColor := Theme.BackgroundColor; |
| 353 |
FPopOnAnswered := true; |
| 354 |
FDialog := TDialog.Create(Self); |
| 355 |
|
| 356 |
{$define read_implementation_constructor} |
| 357 |
{$I auto_generated_persistent_vectors/tviewdialog_persistent_vectors.inc} |
| 358 |
{$undef read_implementation_constructor} |
| 359 |
end; |
| 360 |
|
| 361 |
destructor TViewDialog.Destroy; |
| 362 |
begin |
| 363 |
FreeAndNil(FText); |
| 364 |
FreeAndNil(FUnusedSaveScreen); |
| 365 |
|
| 366 |
{$define read_implementation_destructor} |
| 367 |
{$I auto_generated_persistent_vectors/tviewdialog_persistent_vectors.inc} |
| 368 |
{$undef read_implementation_destructor} |
| 369 |
inherited; |
| 370 |
end; |
| 371 |
|
| 372 |
function TViewDialog.GetCaption: String; |
| 373 |
begin |
| 374 |
Result := TrimEndingNewline(Text.Text); |
| 375 |
end; |
| 376 |
|
| 377 |
procedure TViewDialog.SetCaption(const Value: String); |
| 378 |
begin |
| 379 |
Text.Text := Value; |
| 380 |
end; |
| 381 |
|
| 382 |
function TViewDialog.GetInputText: String; |
| 383 |
begin |
| 384 |
Result := FDialog.InputText; |
| 385 |
end; |
| 386 |
|
| 387 |
procedure TViewDialog.SetInputText(const Value: String); |
| 388 |
begin |
| 389 |
FDialog.InputText := Value; |
| 390 |
end; |
| 391 |
|
| 392 |
procedure TViewDialog.SaveScreenIfNecessary(const AContainer: TCastleContainer); |
| 393 |
begin |
| 394 |
if Background and BackgroundScreenshot and (BackgroundColor[3] <> 1) then |
| 395 |
begin |
| 396 |
FUnusedSaveScreen := AContainer.SaveScreen; |
| 397 |
end; |
| 398 |
end; |
| 399 |
|
| 400 |
procedure TViewDialog.Start; |
| 401 |
const |
| 402 |
MinButtonWidth = 100; //< OK button looks too small without this |
| 403 |
var |
| 404 |
Buttons: TButtonArray; |
| 405 |
BackgroundImage: TCastleImageControl; |
| 406 |
BackgroundRect: TCastleRectangleControl; |
| 407 |
I: Integer; |
| 408 |
begin |
| 409 |
inherited; |
| 410 |
|
| 411 |
if Background then |
| 412 |
begin |
| 413 |
{ Avoid making screenshot when BackgroundColor[3] = 1. |
| 414 |
This means that Theme.InternalMessageFallbackLook prevents from taking a screenshot. } |
| 415 |
if BackgroundScreenshot and (BackgroundColor[3] <> 1) then |
| 416 |
begin |
| 417 |
BackgroundImage := TCastleImageControl.Create(FreeAtStop); |
| 418 |
BackgroundImage.Stretch := true; |
| 419 |
BackgroundImage.FullSize := true; |
| 420 |
if FUnusedSaveScreen <> nil then |
| 421 |
begin |
| 422 |
BackgroundImage.Image := FUnusedSaveScreen; |
| 423 |
FUnusedSaveScreen := nil; |
| 424 |
end else |
| 425 |
BackgroundImage.Image := Container.SaveScreen; |
| 426 |
InsertFront(BackgroundImage); |
| 427 |
end; |
| 428 |
|
| 429 |
BackgroundRect := TCastleRectangleControl.Create(FreeAtStop); |
| 430 |
BackgroundRect.Color := BackgroundColor; |
| 431 |
BackgroundRect.FullSize := true; |
| 432 |
InsertFront(BackgroundRect); |
| 433 |
end; |
| 434 |
|
| 435 |
SetLength(Buttons, 0); |
| 436 |
InitializeButtons(Buttons); |
| 437 |
for I := Low(Buttons) to High(Buttons) do |
| 438 |
Buttons[I].MinWidth := MinButtonWidth; |
| 439 |
|
| 440 |
FDialog.Initialize(Text, Alignment, Html, Buttons, DrawInputText); |
| 441 |
InsertFront(FDialog); |
| 442 |
|
| 443 |
if Theme.InternalMessageFallbackLook then |
| 444 |
ForceFallbackLook(FDialog); |
| 445 |
|
| 446 |
InterceptInput := true; |
| 447 |
end; |
| 448 |
|
| 449 |
procedure TViewDialog.Stop; |
| 450 |
begin |
| 451 |
// remove FDialog, to clearly reinsert it at next Start call |
| 452 |
RemoveControl(FDialog); |
| 453 |
inherited; |
| 454 |
end; |
| 455 |
|
| 456 |
procedure TViewDialog.InitializeButtons(var Buttons: TButtonArray); |
| 457 |
begin |
| 458 |
end; |
| 459 |
|
| 460 |
function TViewDialog.DrawInputText: boolean; |
| 461 |
begin |
| 462 |
Result := false; |
| 463 |
end; |
| 464 |
|
| 465 |
procedure TViewDialog.DoAnswered; |
| 466 |
begin |
| 467 |
FAnswered := true; |
| 468 |
if PopOnAnswered then |
| 469 |
Container.PopView(Self); |
| 470 |
end; |
| 471 |
|
| 472 |
{ TViewDialogOK ------------------------------------------------------------- } |
| 473 |
|
| 474 |
procedure TViewDialogOK.InitializeButtons(var Buttons: TViewDialog.TButtonArray); |
| 475 |
begin |
| 476 |
SetLength(Buttons, 1); |
| 477 |
Buttons[0] := TCastleButton.Create(Self); |
| 478 |
Buttons[0].OnClick := {$ifdef FPC}@{$endif}ButtonOKClick; |
| 479 |
Buttons[0].Caption := SOk; |
| 480 |
end; |
| 481 |
|
| 482 |
procedure TViewDialogOK.ButtonOKClick(Sender: TObject); |
| 483 |
begin |
| 484 |
DoAnswered; |
| 485 |
end; |
| 486 |
|
| 487 |
function TViewDialogOK.Press(const Event: TInputPressRelease): boolean; |
| 488 |
begin |
| 489 |
Result := inherited; |
| 490 |
// if Result then Exit; // ignore inherited Result, always true when InterceptInput |
| 491 |
|
| 492 |
if Event.IsKey(CharEnter) or Event.IsKey(CharEscape) then |
| 493 |
begin |
| 494 |
DoAnswered; |
| 495 |
Result := true; |
| 496 |
end; |
| 497 |
end; |
| 498 |
|
| 499 |
{ TViewDialogYesNo ---------------------------------------------------------- } |
| 500 |
|
| 501 |
procedure TViewDialogYesNo.InitializeButtons(var Buttons: TViewDialog.TButtonArray); |
| 502 |
begin |
| 503 |
SetLength(Buttons, 2); |
| 504 |
|
| 505 |
Buttons[0] := TCastleButton.Create(Self); |
| 506 |
Buttons[0].OnClick := {$ifdef FPC}@{$endif}ButtonNoClick; |
| 507 |
Buttons[0].Caption := SNo; |
| 508 |
|
| 509 |
Buttons[1] := TCastleButton.Create(Self); |
| 510 |
Buttons[1].OnClick := {$ifdef FPC}@{$endif}ButtonYesClick; |
| 511 |
Buttons[1].Caption := SYes; |
| 512 |
end; |
| 513 |
|
| 514 |
procedure TViewDialogYesNo.ButtonYesClick(Sender: TObject); |
| 515 |
begin |
| 516 |
FAnswer := true; |
| 517 |
DoAnswered; |
| 518 |
end; |
| 519 |
|
| 520 |
procedure TViewDialogYesNo.ButtonNoClick(Sender: TObject); |
| 521 |
begin |
| 522 |
FAnswer := false; |
| 523 |
DoAnswered; |
| 524 |
end; |
| 525 |
|
| 526 |
function TViewDialogYesNo.Press(const Event: TInputPressRelease): boolean; |
| 527 |
begin |
| 528 |
Result := inherited; |
| 529 |
// if Result then Exit; // ignore inherited Result, always true when InterceptInput |
| 530 |
|
| 531 |
if Event.IsKey(keyY) or Event.IsKey(keyEnter) then |
| 532 |
begin |
| 533 |
FAnswer := true; |
| 534 |
DoAnswered; |
| 535 |
Result := true; // set Result, in case developer changed our InterceptInput to false |
| 536 |
end; |
| 537 |
|
| 538 |
if Event.IsKey(keyN) or Event.IsKey(keyEscape) then |
| 539 |
begin |
| 540 |
FAnswer := false; |
| 541 |
DoAnswered; |
| 542 |
Result := true; // set Result, in case developer changed our InterceptInput to false |
| 543 |
end; |
| 544 |
end; |
| 545 |
|
| 546 |
{ TViewDialogChoice ---------------------------------------------------------- } |
| 547 |
|
| 548 |
procedure TViewDialogChoice.InitializeButtons(var Buttons: TViewDialog.TButtonArray); |
| 549 |
var |
| 550 |
I: Integer; |
| 551 |
begin |
| 552 |
Check(Length(ButtonCaptions) = Length(ButtonChars), 'In TViewDialogChoice, ButtonCaptions must have the same length as ButtonChars'); |
| 553 |
SetLength(Buttons, Length(ButtonCaptions)); |
| 554 |
|
| 555 |
for I := 0 to High(ButtonCaptions) do |
| 556 |
begin |
| 557 |
Buttons[I] := TCastleButton.Create(Self); |
| 558 |
Buttons[I].OnClick := {$ifdef FPC}@{$endif}ButtonClick; |
| 559 |
Buttons[I].Caption := ButtonCaptions[I]; |
| 560 |
Buttons[I].Tag := Ord(ButtonChars[I]); |
| 561 |
end; |
| 562 |
end; |
| 563 |
|
| 564 |
procedure TViewDialogChoice.ButtonClick(Sender: TObject); |
| 565 |
begin |
| 566 |
FAnswer := Chr((Sender as TCastleButton).Tag); |
| 567 |
DoAnswered; |
| 568 |
end; |
| 569 |
|
| 570 |
function TViewDialogChoice.Press(const Event: TInputPressRelease): boolean; |
| 571 |
var |
| 572 |
C: char; |
| 573 |
begin |
| 574 |
Result := inherited; |
| 575 |
// if Result then Exit; // ignore inherited Result, always true when InterceptInput |
| 576 |
|
| 577 |
for C in ButtonChars do |
| 578 |
if Event.IsKey(LoCase(C)) or Event.IsKey(UpCase(C)) then |
| 579 |
begin |
| 580 |
FAnswer := C; |
| 581 |
DoAnswered; |
| 582 |
Exit(true); |
| 583 |
end; |
| 584 |
|
| 585 |
if AllowCancel and Event.IsKey(CharEscape) then |
| 586 |
begin |
| 587 |
FAnswer := CharEscape; |
| 588 |
DoAnswered; |
| 589 |
Exit(true); |
| 590 |
end; |
| 591 |
end; |
| 592 |
|
| 593 |
{ TViewDialogInput ---------------------------------------------------------- } |
| 594 |
|
| 595 |
procedure TViewDialogInput.InitializeButtons(var Buttons: TViewDialog.TButtonArray); |
| 596 |
begin |
| 597 |
if CanCancel then |
| 598 |
begin |
| 599 |
SetLength(Buttons, 2); |
| 600 |
|
| 601 |
Buttons[0] := TCastleButton.Create(Self); |
| 602 |
Buttons[0].OnClick := {$ifdef FPC}@{$endif}ButtonCancelClick; |
| 603 |
Buttons[0].Caption := SCancel; |
| 604 |
|
| 605 |
Buttons[1] := TCastleButton.Create(Self); |
| 606 |
Buttons[1].OnClick := {$ifdef FPC}@{$endif}ButtonOKClick; |
| 607 |
Buttons[1].Caption := SOk; |
| 608 |
end else |
| 609 |
begin |
| 610 |
SetLength(Buttons, 1); |
| 611 |
Buttons[0] := TCastleButton.Create(Self); |
| 612 |
Buttons[0].OnClick := {$ifdef FPC}@{$endif}ButtonOKClick; |
| 613 |
Buttons[0].Caption := SOk; |
| 614 |
end; |
| 615 |
end; |
| 616 |
|
| 617 |
procedure TViewDialogInput.ButtonOKClick(Sender: TObject); |
| 618 |
begin |
| 619 |
Press(InputKey(Container.MousePosition, keyEnter, CharEnter, [])); |
| 620 |
end; |
| 621 |
|
| 622 |
procedure TViewDialogInput.ButtonCancelClick(Sender: TObject); |
| 623 |
begin |
| 624 |
Press(InputKey(Container.MousePosition, keyEscape, CharEscape, [])); |
| 625 |
end; |
| 626 |
|
| 627 |
function TViewDialogInput.Press(const Event: TInputPressRelease): boolean; |
| 628 |
{ TODO: copy-paste of TCastleEdit.Press here. |
| 629 |
We should instead reuse TCastleEdit? Although TDialog provides multiline display, |
| 630 |
which may be cool e.g. to edit long URLs in castle-model-viewer. } |
| 631 |
begin |
| 632 |
Result := inherited; |
| 633 |
// if Result then Exit; // ignore inherited Result, always true when InterceptInput |
| 634 |
|
| 635 |
{ Under Windows, pressing Ctrl + Backspace causes key = keyBackSpace with |
| 636 |
character = CharDelete (not CharBackSpace). |
| 637 |
That is, Windows automatically equates Ctrl + Backspace |
| 638 |
with CharDelete, assuming that we always want to do the same action for them. |
| 639 |
|
| 640 |
However, I want to detect Ctrl + Backspace, and not detect "real" delete key |
| 641 |
presses (that may be handled in the future to delete char in front of cursor). |
| 642 |
So I just query for both CharBackSpace and keyBackSpace to detect backspace. } |
| 643 |
if Event.IsKey(CharBackSpace) or Event.IsKey(keyBackSpace) then |
| 644 |
begin |
| 645 |
if InputText <> '' then |
| 646 |
if mkCtrl in Container.Pressed.Modifiers then |
| 647 |
InputText := '' |
| 648 |
else |
| 649 |
InputText := Copy(InputText, 1, Length(InputText) - 1); |
| 650 |
Result := true; |
| 651 |
end else |
| 652 |
if Event.IsKey(CharEnter) then |
| 653 |
begin |
| 654 |
if Length(InputText) >= MinLength then |
| 655 |
begin |
| 656 |
FAnswerCancelled := false; |
| 657 |
DoAnswered; |
| 658 |
end; |
| 659 |
|
| 660 |
{ How to show that we cannot accept the answer yet? |
| 661 |
This looks bad: |
| 662 |
MessageOk(Window, Format('You must enter at least %d characters.', |
| 663 |
[answerMinLen]), DefaultAlign); } |
| 664 |
Result := true; |
| 665 |
end else |
| 666 |
if Event.IsKey(CharEscape) then |
| 667 |
begin |
| 668 |
if CanCancel then |
| 669 |
begin |
| 670 |
FAnswerCancelled := true; |
| 671 |
DoAnswered; |
| 672 |
end; |
| 673 |
Result := true; |
| 674 |
end else |
| 675 |
{ Use keyC+mkCtrl to check for this, not CtrlC, this way on macOS we |
| 676 |
allow Command+C to copy text. } |
| 677 |
//if Event.IsKey(CtrlC) then |
| 678 |
if Event.IsKey(keyC, [mkCtrl]) then |
| 679 |
begin |
| 680 |
if InputText <> '' then |
| 681 |
Clipboard.AsText := InputText; |
| 682 |
Result := true; |
| 683 |
end else |
| 684 |
//if Event.IsKey(CtrlX) then |
| 685 |
if Event.IsKey(keyX, [mkCtrl]) then |
| 686 |
begin |
| 687 |
if InputText <> '' then |
| 688 |
begin |
| 689 |
Clipboard.AsText := InputText; |
| 690 |
InputText := ''; |
| 691 |
end; |
| 692 |
Result := true; |
| 693 |
end else |
| 694 |
//if Event.IsKey(CtrlV) then |
| 695 |
if Event.IsKey(keyV, [mkCtrl]) then |
| 696 |
begin |
| 697 |
InputText := SDeleteChars(Clipboard.AsText, AllChars - AllowedChars); |
| 698 |
Result := true; |
| 699 |
end else |
| 700 |
if (Event.EventType = itKey) and |
| 701 |
(Event.KeyString <> '') and |
| 702 |
CharInSet(Event.KeyCharacter, AllowedChars) and |
| 703 |
((MaxLength = 0) or (Length(InputText) < MaxLength)) then |
| 704 |
begin |
| 705 |
InputText := InputText + Event.KeyString; |
| 706 |
Result := true; |
| 707 |
end; |
| 708 |
end; |
| 709 |
|
| 710 |
function TViewDialogInput.DrawInputText: boolean; |
| 711 |
begin |
| 712 |
Result := true; |
| 713 |
end; |
| 714 |
|
| 715 |
function TViewDialogInput.GetAnswer: String; |
| 716 |
begin |
| 717 |
Result := InputText; |
| 718 |
end; |
| 719 |
|
| 720 |
procedure TViewDialogInput.SetAnswer(const Value: String); |
| 721 |
begin |
| 722 |
InputText := Value; |
| 723 |
end; |
| 724 |
|
| 725 |
{ TViewDialogKey ------------------------------------------------------------ } |
| 726 |
|
| 727 |
function TViewDialogKey.Press(const Event: TInputPressRelease): boolean; |
| 728 |
begin |
| 729 |
Result := inherited; |
| 730 |
// if Result then Exit; // ignore inherited Result, always true when InterceptInput |
| 731 |
|
| 732 |
if (Event.EventType = itKey) and (Event.Key <> keyNone) then |
| 733 |
begin |
| 734 |
FAnswer := Event.Key; |
| 735 |
DoAnswered; |
| 736 |
Result := true; |
| 737 |
end; |
| 738 |
end; |
| 739 |
|
| 740 |
{ TViewDialogPressEvent ------------------------------------------------------------ } |
| 741 |
|
| 742 |
function TViewDialogPressEvent.Press(const Event: TInputPressRelease): boolean; |
| 743 |
begin |
| 744 |
Result := inherited; |
| 745 |
// if Result then Exit; // ignore inherited Result, always true when InterceptInput |
| 746 |
|
| 747 |
FAnswer := Event; |
| 748 |
DoAnswered; |
| 749 |
Result := true; |
| 750 |
end; |
| 751 |
|
| 752 |
{$define read_implementation_methods} |
| 753 |
{$I auto_generated_persistent_vectors/tviewdialog_persistent_vectors.inc} |
| 754 |
{$undef read_implementation_methods} |
| 755 |
|
| 756 |
end. |