Debian Codegraph @

src/ui/castlecontrols_edit.inc

raw
1 {%MainUnit castlecontrols.pas}
2 {
3 Copyright 2017-2024 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 { Edit box to input a single line of text.
20 The contents are in the @link(Text) property, you can get or set them
21 at any time.
22
23 Note that you should set a suitable
24 @link(Width TCastleUserInterface.Width) of the edit box,
25 to nicely display a typical content.
26 The height is by default auto-calculated (see @link(AutoSizeHeight))
27 looking at font size (and other properties, like padding and frame size),
28 and usually is reasonable automatically.
29 }
30 TCastleEdit = class(TCastleUserInterfaceFont)
31 strict private
32 FText: String;
33 FPaddingHorizontal, FPaddingVertical, FPadding: Single;
34 FBackgroundColor, FFocusedColor, FUnfocusedColor: TCastleColor;
35 FPlaceholderColor: TCastleColor;
36 FFrame: boolean;
37 LifeTime: TFloatTime;
38 FMaxLength: Cardinal;
39 FAllowedChars: TSetOfChars;
40 FCaptureAllInput: boolean;
41 FAutoOnScreenKeyboard: Boolean;
42 Scissor: TScissor;
43 FAutoSizeHeight: Boolean;
44 FPlaceholder: String;
45 FOnChange: TNotifyEvent;
46 FPasswordChar: Char;
47 FTextTranslate, FPlaceholderTranslate: Boolean;
48 FAlignment: THorizontalPosition;
49 FEnabled: boolean;
50 procedure SetText(const Value: String);
51 procedure SetFocusedColor(const Value: TCastleColor);
52 procedure SetUnfocusedColor(const Value: TCastleColor);
53 procedure SetPlaceholderColor(const Value: TCastleColor);
54 procedure SetBackgroundColor(const Value: TCastleColor);
55 procedure SetPadding(const Value: Single);
56 procedure SetPaddingHorizontal(const Value: Single);
57 procedure SetPaddingVertical(const Value: Single);
58 procedure SetFrame(const Value: boolean);
59 procedure SetAutoSizeHeight(const Value: Boolean);
60 procedure SetPlaceholder(const Value: String);
61 procedure SetPasswordChar(const Value: Char);
62 procedure SetAlignment(const Value: THorizontalPosition);
63 procedure SetEnabled(const Value: boolean);
64 protected
65 procedure PreferredSize(var PreferredWidth, PreferredHeight: Single); override;
66 function GetInternalText: String; override;
67 procedure SetInternalText(const Value: String); override;
68 procedure DoChange; virtual;
69 procedure TranslateProperties(const TranslatePropertyEvent: TTranslatePropertyEvent); override;
70 public
71 constructor Create(AOwner: TComponent); override;
72 destructor Destroy; override;
73 procedure Render; override;
74 function Press(const Event: TInputPressRelease): boolean; override;
75 procedure Update(const SecondsPassed: Single;
76 var HandleInput: boolean); override;
77 function CapturesEventsAtPosition(const Position: TVector2): boolean; override;
78 procedure SetFocused(const Value: boolean); override;
79 procedure EditorAllowResize(
80 out ResizeWidth, ResizeHeight: Boolean; out Reason: String); override;
81 function PropertySections(const PropertyName: String): TPropertySections; override;
82
83 { Focused text color. By default it's black. }
84 property FocusedColor: TCastleColor read FFocusedColor write SetFocusedColor;
85
86 { Unfocused text color. By default it's dark gray. }
87 property UnfocusedColor: TCastleColor read FUnfocusedColor write SetUnfocusedColor;
88
89 { Color for @link(Placeholder). By default it's light gray. }
90 property PlaceholderColor: TCastleColor read FPlaceholderColor write SetPlaceholderColor;
91
92 { Background color. Fills the area under @link(Text) and the padding.
93 By default it's white.
94 Note that it can be transparent, in which case it will show the
95 tiEdit image underneath (if @link(Frame) is @true),
96 or UI control underneath (if @link(Frame) is @false). }
97 property BackgroundColor: TCastleColor read FBackgroundColor write SetBackgroundColor;
98
99 { Allowed characters that user can input.
100 Note that this @italic(only restricts the user input),
101 it does not prevent from programmatically setting @link(Text)
102 to include disallowed characters. }
103 property AllowedChars: TSetOfChars read FAllowedChars write FAllowedChars;
104
105 { Max length of user input.
106 Value of 0 (default) means "no limit".
107 Note that this @italic(only restricts the user input),
108 it does not prevent from programmatically setting @link(Text)
109 to be longer. }
110 property MaxLength: Cardinal read FMaxLength write FMaxLength;
111 published
112 const
113 DefaultPadding = 4;
114 DefaultAutoOnScreenKeyboard = false;
115
116 { Currently input text. }
117 property Text: String read FText write SetText;
118
119 { Inside the box rectangle, padding between the borders (or frame, if @link(Frame)) and text.
120 Total horizontal padding is the sum @code(PaddingHorizontal + Padding),
121 total vertical padding is the sum @code(PaddingVertical + Padding).
122 @groupBegin }
123 property PaddingHorizontal: Single
124 read FPaddingHorizontal write SetPaddingHorizontal {$ifdef FPC}default 0{$endif};
125 property PaddingVertical: Single
126 read FPaddingVertical write SetPaddingVertical {$ifdef FPC}default 0{$endif};
127 property Padding: Single
128 read FPadding write SetPadding {$ifdef FPC}default DefaultPadding{$endif};
129 { @groupEnd }
130
131 { Draw frame around the box. Frame uses theme image tiEdit,
132 see TCastleTheme.Images if you want to customize it. }
133 property Frame: boolean read FFrame write SetFrame default true;
134
135 {$ifdef FPC}
136 { Should we capture input regardless of the currently focused control.
137 This is in practice only sensible if this is the only edit box currently
138 visible on the screen. }
139 property CaptureAllInput: boolean read FCaptureAllInput write FCaptureAllInput;
140 deprecated 'use Container.ForceCaptureInput instead of this';
141 {$endif}
142
143 { Shows the software (on-screen) keyboard on mobile devices automatically
144 when the edit box gets focused.
145
146 For now, this only works on Android (not iOS yet -- see
147 https://github.com/castle-engine/castle-engine/issues/554).
148
149 This requires adding @code(<service name="keyboard" />)
150 to the Android services in @code(CastleEngineManifest.xml).
151 See example
152 @url(https://github.com/castle-engine/castle-engine/tree/master/examples/mobile/on_screen_keyboard examples/mobile/on_screen_keyboard)
153 and the @url(https://castle-engine.io/android_services Android service) documentation
154 for @url(https://github.com/castle-engine/castle-engine/tree/master/tools/build-tool/data/android/integrated-services/keyboard/README.adoc keyboard)
155 service. }
156 property AutoOnScreenKeyboard: Boolean
157 read FAutoOnScreenKeyboard write FAutoOnScreenKeyboard
158 default DefaultAutoOnScreenKeyboard;
159
160 property AutoSizeHeight: Boolean
161 read FAutoSizeHeight write SetAutoSizeHeight default true;
162
163 { Displayed when @link(Text) is empty. }
164 property Placeholder: String
165 read FPlaceholder write SetPlaceholder;
166
167 { Event sent when @link(Text) value was changed by a user.
168 Note that this is @italic(not) called when you change @link(Text)
169 property programmatically. }
170 property OnChange: TNotifyEvent read FOnChange write FOnChange;
171
172 { Show given character (usually "*") instead of actual input characters.
173 Useful for password input fields.
174
175 Setting the PasswordChar to something else than #0 also informs
176 the system that the input is sensitive (e.g. it is a password)
177 and should not be stored in any system-wide dictionary for easy future reuse.
178 This is meaningful for on-screen keyboards on mobile systems,
179 that by default (when PasswordChar is #0) may store entered text in some
180 system-wide dictionary and propose user to use the same words in future inputs.
181 If the user inputs something sensitive (like a password) you want to disable this
182 using PasswordChar <> #0. }
183 property PasswordChar: Char
184 read FPasswordChar write SetPasswordChar default #0;
185
186 { Should the @link(Text) be localized (translated into other languages).
187 Determines if the property is enumerated by @link(TCastleComponent.TranslateProperties),
188 which affects the rest of localization routines. }
189 property TextTranslate: Boolean read FTextTranslate write FTextTranslate default true;
190
191 { Should the @link(Placeholder) be localized (translated into other languages).
192 Determines if the property is enumerated by @link(TCastleComponent.TranslateProperties),
193 which affects the rest of localization routines. }
194 property PlaceholderTranslate: Boolean read FPlaceholderTranslate write FPlaceholderTranslate default true;
195
196 { Horizontal alignment of the text. }
197 property Alignment: THorizontalPosition
198 read FAlignment write SetAlignment default hpLeft;
199
200 { Enabled control can be focused and edited, is not grayed-out. }
201 property Enabled: boolean read FEnabled write SetEnabled default true;
202
203 {$define read_interface_class}
204 {$I auto_generated_persistent_vectors/tcastleedit_persistent_vectors.inc}
205 {$undef read_interface_class}
206 end;
207
208 { Descendant of TCastleEdit specialized for editing floating-point numbers.
209 Use @link(Value) instead of @link(Text). }
210 TCastleFloatEdit = class(TCastleEdit)
211 strict private
212 FDecimalDigits: Integer;
213 FMax: Single;
214 FMin: Single;
215 function GetValue: Single;
216 procedure SetValue(const AValue: Single);
217 procedure SetDecimalDigits(const AValue: Integer);
218 public
219 constructor Create(AOwner: TComponent); override;
220 procedure SetFocused(const AValue: Boolean); override;
221 function PropertySections(const PropertyName: String): TPropertySections; override;
222 published
223 { In case of this descendant, Text is always derived from Value. }
224 property Text stored false;
225 property TextTranslate default false;
226
227 property Value: Single read GetValue write SetValue {$ifdef FPC}default 0{$endif};
228 { Minimum and maximum limits.
229 Applied only if Min <= Max, which is not the case by default. }
230 property Min: Single read FMin write FMin {$ifdef FPC}default 1.0{$endif};
231 property Max: Single read FMax write FMax {$ifdef FPC}default 0.0{$endif};
232 { How many decimal digits to display (and preserve).
233 Set to -1 to display (and preserve) all digits,
234 but beware that many numbers will then look hard to
235 read, because they cannot be represented perfectly as a float.
236 E.g. inputting "0.2" will change (in SetFocused) to something like
237 "0.200000003" since the number 0.2 cannot be represented precisely. }
238 property DecimalDigits: Integer
239 read FDecimalDigits write SetDecimalDigits default 2;
240 end;
241
242 { Descendant of TCastleEdit specialized for editing integer numbers.
243 Use @link(Value) instead of @link(Text). }
244 TCastleIntegerEdit = class(TCastleEdit)
245 strict private
246 FMax: Int64;
247 FMin: Int64;
248 function GetValue: Int64;
249 procedure SetValue(const AValue: Int64);
250 { The Int64 by default is always shown as modified in Object Inspector.
251 We need "stored" methods to make it right.
252 We also want to save Min to JSON when you set it to zero
253 (but we don't need it when you set it to one). }
254 function IsStoredValue: Boolean;
255 function IsStoredMin: Boolean;
256 function IsStoredMax: Boolean;
257 public
258 constructor Create(AOwner: TComponent); override;
259 procedure SetFocused(const AValue: Boolean); override;
260 function PropertySections(const PropertyName: String): TPropertySections; override;
261 published
262 { In case of this descendant, Text is always derived from Value. }
263 property Text stored false;
264 property TextTranslate default false;
265
266 { Current value, default 0. }
267 property Value: Int64 read GetValue write SetValue stored IsStoredValue nodefault;
268
269 { Minimum value, default 1.
270 Applied only if Min <= Max, which is not the case by default. }
271 property Min: Int64 read FMin write FMin stored IsStoredMin nodefault;
272
273 { Minimum value, default 1.
274 Applied only if Min <= Max, which is not the case by default. }
275 property Max: Int64 read FMax write FMax stored IsStoredMax nodefault;
276 end;
277
278 {$endif read_interface}
279
280 {$ifdef read_implementation}
281
282 const
283 CaretChar = '|';
284
285 { These routines may be public at some point. }
286
287 { Displays the keyboard on Android. It requires the current text to be edited
288 and the target user interface to which the events will be send. }
289 procedure ShowOnScreenKeyboard(const KeyboardTarget: TCastleUserInterface;
290 const CurrentText: String; PasswordMode: Boolean); forward;
291
292 { Hides keyboard on Android but only when KeyboardTarget and ForceCaptureInput
293 is the same object. }
294 procedure HideOnScreenKeyboard(const KeyboardTarget: TCastleUserInterface); forward;
295
296 procedure ShowOnScreenKeyboard(const KeyboardTarget: TCastleUserInterface;
297 const CurrentText: String; PasswordMode: Boolean);
298 begin
299 { Remove focus from another control }
300 if (KeyboardTarget.Container.ForceCaptureInput <> nil) and
301 (KeyboardTarget.Container.ForceCaptureInput <> KeyboardTarget) then
302 KeyboardTarget.Container.ForceCaptureInput.Focused := false;
303
304 { Set ForceCaptureInput to control that will show keyboard }
305 KeyboardTarget.Container.ForceCaptureInput := KeyboardTarget;
306
307 { Send message to open keyboard. }
308 Messaging.Send(['castle-show-keyboard', CurrentText,
309 Iff(PasswordMode, 'true', 'false')]);
310 end;
311
312 procedure HideOnScreenKeyboard(const KeyboardTarget: TCastleUserInterface);
313 begin
314 { Don't hide keyboard if another TCastleEdit has displayed it }
315 if KeyboardTarget.Container.ForceCaptureInput = KeyboardTarget then
316 begin
317 Messaging.Send(['castle-hide-keyboard']);
318 KeyboardTarget.Container.ForceCaptureInput := nil;
319 end;
320 end;
321
322
323 { TCastleEdit --------------------------------------------------------------- }
324
325 constructor TCastleEdit.Create(AOwner: TComponent);
326 begin
327 inherited;
328 FFocusedColor := Black;
329 FUnfocusedColor := Vector4(0.25, 0.25, 0.25, 1);
330 FPlaceholderColor := Vector4(0.75, 0.75, 0.75, 1);
331 FBackgroundColor := White;
332 FPadding := DefaultPadding;
333 FFrame := true;
334 FAllowedChars := AllChars;
335 FMaxLength := 0;
336 FAutoOnScreenKeyboard := DefaultAutoOnScreenKeyboard;
337 Scissor := TScissor.Create;
338 FAutoSizeHeight := true;
339 FTextTranslate := true;
340 FPlaceholderTranslate := true;
341 FEnabled := true;
342
343 {$define read_implementation_constructor}
344 {$I auto_generated_persistent_vectors/tcastleedit_persistent_vectors.inc}
345 {$undef read_implementation_constructor}
346 end;
347
348 destructor TCastleEdit.Destroy;
349 begin
350 FreeAndNil(Scissor);
351
352 {$define read_implementation_destructor}
353 {$I auto_generated_persistent_vectors/tcastleedit_persistent_vectors.inc}
354 {$undef read_implementation_destructor}
355 inherited;
356 end;
357
358 procedure TCastleEdit.SetText(const Value: String);
359 begin
360 if FText <> Value then
361 begin
362 FText := Value;
363 VisibleChange([chRender]);
364 end;
365 end;
366
367 procedure TCastleEdit.SetFocusedColor(const Value: TCastleColor);
368 begin
369 if not TCastleColor.PerfectlyEquals(FFocusedColor, Value) then
370 begin
371 FFocusedColor := Value;
372 VisibleChange([chRender]);
373 end;
374 end;
375
376 procedure TCastleEdit.SetUnfocusedColor(const Value: TCastleColor);
377 begin
378 if not TCastleColor.PerfectlyEquals(FUnfocusedColor, Value) then
379 begin
380 FUnfocusedColor := Value;
381 VisibleChange([chRender]);
382 end;
383 end;
384
385 procedure TCastleEdit.SetPlaceholderColor(const Value: TCastleColor);
386 begin
387 if not TCastleColor.PerfectlyEquals(FPlaceholderColor, Value) then
388 begin
389 FPlaceholderColor := Value;
390 VisibleChange([chRender]);
391 end;
392 end;
393
394 procedure TCastleEdit.SetBackgroundColor(const Value: TCastleColor);
395 begin
396 if not TCastleColor.PerfectlyEquals(FBackgroundColor, Value) then
397 begin
398 FBackgroundColor := Value;
399 VisibleChange([chRender]);
400 end;
401 end;
402
403 procedure TCastleEdit.SetPadding(const Value: Single);
404 begin
405 if FPadding <> Value then
406 begin
407 FPadding := Value;
408 VisibleChange([chRectangle]);
409 end;
410 end;
411
412 procedure TCastleEdit.SetPaddingHorizontal(const Value: Single);
413 begin
414 if FPaddingHorizontal <> Value then
415 begin
416 FPaddingHorizontal := Value;
417 VisibleChange([chRectangle]);
418 end;
419 end;
420
421 procedure TCastleEdit.SetPaddingVertical(const Value: Single);
422 begin
423 if FPaddingVertical <> Value then
424 begin
425 FPaddingVertical := Value;
426 VisibleChange([chRectangle]);
427 end;
428 end;
429
430 procedure TCastleEdit.SetFrame(const Value: boolean);
431 begin
432 if FFrame <> Value then
433 begin
434 FFrame := Value;
435 VisibleChange([chRectangle]);
436 end;
437 end;
438
439 procedure TCastleEdit.SetAutoSizeHeight(const Value: Boolean);
440 begin
441 if FAutoSizeHeight <> Value then
442 begin
443 FAutoSizeHeight := Value;
444 VisibleChange([chRectangle]);
445 end;
446 end;
447
448 procedure TCastleEdit.PreferredSize(var PreferredWidth, PreferredHeight: Single);
449 var
450 PaddingVerticalScaled, HeightScaled: Single;
451 US: Single;
452 begin
453 inherited;
454
455 if AutoSizeHeight then
456 begin
457 US := UIScale;
458 PaddingVerticalScaled := US * (PaddingVertical + Padding);
459
460 HeightScaled := Font.Height + 2 * PaddingVerticalScaled;
461 if Frame then
462 begin
463 HeightScaled := HeightScaled +
464 US * Theme.ImagesPersistent[tiEdit].ProtectedSides.TotalTop +
465 US * Theme.ImagesPersistent[tiEdit].ProtectedSides.TotalBottom;
466 end;
467
468 PreferredHeight := HeightScaled;
469 end;
470 end;
471
472 procedure TCastleEdit.DoChange;
473 begin
474 { Every time the contents change, make sure caret is visible.
475 This matches what other GUIs (like GTK) do. }
476 LifeTime := 0;
477 if Assigned(OnChange) then
478 OnChange(Self);
479 end;
480
481 procedure TCastleEdit.EditorAllowResize(
482 out ResizeWidth, ResizeHeight: Boolean; out Reason: String);
483 begin
484 inherited;
485 if AutoSizeHeight then
486 begin
487 ResizeHeight := false;
488 Reason := SAppendPart(Reason, NL, 'Turn off "TCastleEdit.AutoSizeHeight" to change height.');
489 end;
490 end;
491
492 function TCastleEdit.PropertySections(const PropertyName: String): TPropertySections;
493 begin
494 if ArrayContainsString(PropertyName, [
495 'Text', 'Alignment', 'Enabled', 'Placeholder',
496 'PasswordChar', 'MaxLength', 'FocusedColorPersistent', 'UnfocusedColorPersistent',
497 'PlaceholderColorPersistent', 'BackgroundColorPersistent', 'AutoOnScreenKeyboard'
498 ]) then
499 Result := [psBasic]
500 else
501 Result := inherited PropertySections(PropertyName);
502 end;
503
504 procedure TCastleEdit.Render;
505 const
506 CaretSpeed = 1; //< how many blinks per second
507 var
508 SR, RectInside: TFloatRectangle;
509 PaddingHorizontalScaled, PaddingVerticalScaled: Single;
510 US, TextLeft, TextBottom: Single;
511 FrameSides: TBorder;
512 CaretVisible: boolean;
513 S: String;
514 Color: TCastleColor;
515 CaretShift: Single;
516 begin
517 inherited;
518
519 SR := RenderRect;
520 US := UIScale;
521 RectInside := SR;
522
523 if Frame then
524 begin
525 { make smaller RectInside, by frame size }
526 FrameSides := Theme.ImagesPersistent[tiEdit].ProtectedSides;
527 RectInside := RectInside.
528 RemoveTop (FrameSides.TotalTop * US).
529 RemoveRight (FrameSides.TotalRight * US).
530 RemoveBottom(FrameSides.TotalBottom * US).
531 RemoveLeft (FrameSides.TotalLeft * US);
532
533 { draw frame }
534 Theme.Draw(SR, tiEdit, US);
535 end;
536
537 { fill padding and area under Text with BackgroundColor }
538 DrawRectangle(RectInside, BackgroundColor);
539
540 { make smaller RectInside, by padding }
541 PaddingHorizontalScaled := US * (PaddingHorizontal + Padding);
542 PaddingVerticalScaled := US * (PaddingVertical + Padding);
543 RectInside := RectInside.Grow(-PaddingHorizontalScaled, -PaddingVerticalScaled);
544
545 // calculate S, Color, CaretShift
546 CaretShift := 0;
547 if Text <> '' then
548 begin
549 S := Text;
550
551 { in case PasswordChar is non-zero, replace S with repeated PasswordChar }
552 if PasswordChar <> #0 then
553 S := StringOfChar(PasswordChar, Length(S));
554
555 if Focused and Enabled then
556 begin
557 { Note that S here may be already processed by PasswordChar. }
558 CaretShift := Font.TextWidth(S);
559 Color := FocusedColor
560 end else
561 begin
562 Color := UnfocusedColor;
563 { CaretShift doesn't matter in this case,
564 so we don't have to calculate TextWidth, saving a small time. }
565 end;
566 end else
567 begin
568 S := Placeholder;
569 Color := PlaceholderColor;
570 end;
571
572 Scissor.Rect := RectInside.Round;
573 Scissor.Enabled := true;
574
575 case Alignment of
576 hpLeft : TextLeft := RectInside.Left;
577 hpMiddle: TextLeft := (RectInside.Left + RectInside.Right - Font.TextWidth(S)) / 2;
578 hpRight :
579 begin
580 TextLeft := RectInside.Right - Font.TextWidth(S);
581 if Focused and Enabled then
582 TextLeft := TextLeft - Font.TextWidth(CaretChar);
583 end;
584 {$ifndef COMPILER_CASE_ANALYSIS}
585 else raise EInternalError.Create('TCastleLabel.Render: Alignment?');
586 {$endif}
587 end;
588
589 // center in RectInside, important when AutoSize = false
590 TextBottom := RectInside.Bottom +
591 (RectInside.Height - Font.Height) / 2 + Font.DescenderHeight;
592 Font.Print(TextLeft, TextBottom, Color, S);
593
594 { Note that we have to display Caret even when Text = '' and S = Placeholder. }
595 if Focused and Enabled then
596 begin
597 CaretVisible := FloatModulo(LifeTime * CaretSpeed, 1.0) < 0.5;
598 if CaretVisible then
599 Font.Print(TextLeft + CaretShift, TextBottom, FocusedColor, CaretChar);
600 end;
601
602 Scissor.Enabled := false;
603 end;
604
605 procedure TCastleEdit.Update(const SecondsPassed: Single;
606 var HandleInput: boolean);
607 begin
608 inherited;
609 LifeTime := LifeTime + SecondsPassed;
610
611 // cursor is blinking based on LifeTime, so redisplay each frame
612 VisibleChange([chRender]);
613
614 { do not allow other controls to handle key presses
615 (and e.g. move camera when "A" is pressed down), TCastleEdit handles all keys }
616 HandleInput := false;
617 end;
618
619 function TCastleEdit.Press(const Event: TInputPressRelease): boolean;
620 begin
621 Result := inherited;
622 if Result or (not Exists) or (not Enabled) then Exit;
623
624 if Event.IsKey(CharBackSpace) or Event.IsKey(keyBackSpace) then
625 begin
626 if Text <> '' then
627 begin
628 Text := StringCopy(Text, 1, StringLength(Text) - 1);
629 DoChange;
630 end;
631 Result := true;
632 end else
633 { Use keyC+mkCtrl to check for this, not CtrlC, this way on macOS we
634 allow Command+C to copy text. }
635 //if Event.IsKey(CtrlC) then
636 if Event.IsKey(keyC, [mkCtrl]) then
637 begin
638 if Text <> '' then
639 Clipboard.AsText := Text;
640 Result := true;
641 end else
642 //if Event.IsKey(CtrlX) then
643 if Event.IsKey(keyX, [mkCtrl]) then
644 begin
645 if Text <> '' then
646 begin
647 Clipboard.AsText := Text;
648 Text := '';
649 DoChange;
650 end;
651 Result := true;
652 end else
653 //if Event.IsKey(CtrlV) then
654 if Event.IsKey(keyV, [mkCtrl]) then
655 begin
656 Text := SDeleteChars(Clipboard.AsText, AllChars - FAllowedChars);
657 DoChange;
658 Result := true;
659 end else
660 if (Event.EventType = itKey) and
661 (Event.KeyString <> '') and
662 CharInSet(Event.KeyCharacter, AllowedChars) and
663 ((MaxLength = 0) or (Length(Text) < MaxLength)) and
664 { Do not add unprintable special characters, like Enter and Escape.
665 TCastleEdit doesn't process them,
666 but also users don't expect these keys are something added to the edit field.
667 Note: The range CtrlA..CtrlZ includes CharEnter. }
668 (not CharInSet(Event.KeyCharacter, [CtrlA..CtrlZ, CharEscape])) then
669 begin
670 Text := Text + Event.KeyString;
671 DoChange;
672 Result := true;
673 end;
674
675 { Ability to open keyboard when it is closed and edit is focused }
676 if FAutoOnScreenKeyboard and ApplicationProperties.TouchDevice and
677 (Event.IsMouseButton(TCastleMouseButton.buttonLeft)) then
678 begin
679 { If Edit is focused but ForceCaptureInput is not set to it that means
680 we need set focus again to show keyboard. }
681 if Container.ForceCaptureInput = nil then
682 begin
683 Focused := true;
684 end else
685 if Container.ForceCaptureInput <> Self then
686 begin
687 { Only when other edit shows keyboard (or none), change it focus to false
688 and focus/open keyboard for current edit }
689 Container.ForceCaptureInput.Focused := false;
690 Focused := true;
691 end;
692 end;
693 end;
694
695 function TCastleEdit.CapturesEventsAtPosition(const Position: TVector2): boolean;
696 begin
697 {$ifdef FPC}
698 {$warnings off} // using deprecated just to keep it working
699 if CaptureAllInput then
700 Result := true // always capture
701 else
702 {$warnings on}
703 {$endif}
704 Result := inherited CapturesEventsAtPosition(Position);
705 end;
706
707 procedure TCastleEdit.SetFocused(const Value: boolean);
708 begin
709 inherited;
710
711 { We take care about on screen keyboard only when AutoOnScreenKeyboard = true
712 and this is touch device. We don't want to change Container.ForceCaptureInput
713 in other cases.}
714 if FAutoOnScreenKeyboard and ApplicationProperties.TouchDevice then
715 begin
716 if Value then
717 ShowOnScreenKeyboard(Self, Text, PasswordChar <> #0)
718 else
719 HideOnScreenKeyboard(Self);
720 end;
721 end;
722
723 procedure TCastleEdit.SetPlaceholder(const Value: String);
724 begin
725 if FPlaceholder <> Value then
726 begin
727 FPlaceholder := Value;
728 VisibleChange([chRender]);
729 end;
730 end;
731
732 procedure TCastleEdit.SetPasswordChar(const Value: Char);
733 begin
734 if FPasswordChar <> Value then
735 begin
736 FPasswordChar := Value;
737 VisibleChange([chRender]);
738 end;
739 end;
740
741 function TCastleEdit.GetInternalText: String;
742 begin
743 Result := Text;
744 end;
745
746 procedure TCastleEdit.SetInternalText(const Value: String);
747 begin
748 Text := Value;
749 end;
750
751 procedure TCastleEdit.TranslateProperties(
752 const TranslatePropertyEvent: TTranslatePropertyEvent);
753 var
754 S: String;
755 begin
756 if TextTranslate and (Text <> '') then
757 begin
758 S := Text;
759 TranslatePropertyEvent(Self, 'Text', S);
760 Text := S;
761 end;
762
763 if PlaceholderTranslate and (Placeholder <> '') then
764 begin
765 S := Placeholder;
766 TranslatePropertyEvent(Self, 'Placeholder', S);
767 Placeholder := S;
768 end;
769 end;
770
771 procedure TCastleEdit.SetAlignment(const Value: THorizontalPosition);
772 begin
773 if FAlignment <> Value then
774 begin
775 FAlignment := Value;
776 VisibleChange([chRender]);
777 end;
778 end;
779
780 procedure TCastleEdit.SetEnabled(const Value: boolean);
781 begin
782 if FEnabled <> Value then
783 begin
784 FEnabled := Value;
785 VisibleChange([chRender]);
786 end;
787 end;
788
789 {$define read_implementation_methods}
790 {$I auto_generated_persistent_vectors/tcastleedit_persistent_vectors.inc}
791 {$undef read_implementation_methods}
792
793 { TCastleFloatEdit ---------------------------------------------------------- }
794
795 constructor TCastleFloatEdit.Create(AOwner: TComponent);
796 begin
797 inherited Create(AOwner);
798 TextTranslate := false;
799 FMin := 1;
800 FMax := 0;
801 Assert(not (Min <= Max)); // deliberately, so that limits do not work by default
802 AllowedChars := ['-', '+', '0'..'9'] + [{$ifdef FPC}DefaultFormatSettings.{$else}FormatSettings.{$endif}DecimalSeparator];
803 FDecimalDigits := 2;
804 Value := 0.0; // will also assign Text
805 end;
806
807 procedure TCastleFloatEdit.SetFocused(const AValue: Boolean);
808 begin
809 inherited SetFocused(AValue);
810 { Apply to Text limits (Min, Max) and fix invalid input (like Text empty). }
811 Value := Value;
812 end;
813
814 function TCastleFloatEdit.PropertySections(const PropertyName: String): TPropertySections;
815 begin
816 if (PropertyName = 'Text') then
817 Result := [] // override TCastleEdit that made it [psBasic], we instead expose Value
818 else if (PropertyName = 'Value') or
819 (PropertyName = 'Min') or
820 (PropertyName = 'Max') then
821 Result := [psBasic]
822 else
823 Result := inherited PropertySections(PropertyName);
824 end;
825
826 function TCastleFloatEdit.GetValue: Single;
827 begin
828 Result := StrToFloatDefDot(Text, 0);
829 if Min <= Max then
830 ClampVar(Result, Min, Max);
831 end;
832
833 procedure TCastleFloatEdit.SetValue(const AValue: Single);
834 begin
835 if DecimalDigits < 0 then
836 Text := FloatToStrDot(AValue)
837 else
838 // TODO Add FloatToStrFDot, use it
839 Text := FloatToStrF(AValue, ffFixed, 0 { Precision doesn't matter }, DecimalDigits);
840 end;
841
842 procedure TCastleFloatEdit.SetDecimalDigits(const AValue: Integer);
843 begin
844 if FDecimalDigits <> AValue then
845 begin
846 FDecimalDigits := AValue;
847 { Apply the DecimalDigits to current Text look }
848 Value := Value;
849 end;
850 end;
851
852 { TCastleIntegerEdit ---------------------------------------------------------- }
853
854 constructor TCastleIntegerEdit.Create(AOwner: TComponent);
855 begin
856 inherited Create(AOwner);
857 TextTranslate := false;
858 FMin := 1;
859 FMax := 0;
860 Assert(not (Min <= Max)); // deliberately, so that limits do not work by default
861 AllowedChars := ['-', '+', '0'..'9'];
862 Value := 0; // will also assign Text
863 end;
864
865 procedure TCastleIntegerEdit.SetFocused(const AValue: Boolean);
866 begin
867 inherited SetFocused(AValue);
868 { Apply to Text limits (Min, Max) and fix invalid input (like Text empty). }
869 Value := Value;
870 end;
871
872 function TCastleIntegerEdit.PropertySections(const PropertyName: String): TPropertySections;
873 begin
874 if (PropertyName = 'Text') then
875 Result := [] // override TCastleEdit that made it [psBasic], we instead expose Value
876 else if (PropertyName = 'Value') or
877 (PropertyName = 'Min') or
878 (PropertyName = 'Max') then
879 Result := [psBasic]
880 else
881 Result := inherited PropertySections(PropertyName);
882 end;
883
884 function TCastleIntegerEdit.GetValue: Int64;
885 begin
886 Result := StrToInt64Def(Text, 0);
887 if Min <= Max then
888 ClampVar(Result, Min, Max);
889 end;
890
891 procedure TCastleIntegerEdit.SetValue(const AValue: Int64);
892 begin
893 Text := IntToStr(AValue);
894 end;
895
896 function TCastleIntegerEdit.IsStoredValue: Boolean;
897 begin
898 Result := Value <> 0;
899 end;
900
901 function TCastleIntegerEdit.IsStoredMin: Boolean;
902 begin
903 Result := Min <> 1;
904 end;
905
906 function TCastleIntegerEdit.IsStoredMax: Boolean;
907 begin
908 Result := Max <> 0;
909 end;
910
911 {$endif read_implementation}