Debian Codegraph @

src/deprecated_units/castleonscreenmenu.pas

raw
1 {
2 Copyright 2006-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 { On-screen menu displayed in OpenGL (TCastleOnScreenMenu). }
17 unit CastleOnScreenMenu
18 deprecated 'use TCastleVerticalGroup with buttons, sliders inside to desing on-screen menus';
19
20 {$I castleconf.inc}
21
22 interface
23
24 uses Classes, CastleVectors, CastleFonts, CastleControls,
25 CastleGLUtils, CastleUIControls, CastleKeysMouse, CastleColors,
26 CastleRectangles, CastleClassUtils, CastleSoundEngine;
27
28 type
29 TCastleOnScreenMenu = class;
30
31 TCastleMenuButton = class(TCastleButton)
32 public
33 constructor Create(AOwner: TComponent); override;
34 published
35 property CustomBackground default true;
36 property CustomTextColorUse default true;
37 property PaddingHorizontal {$ifdef FPC}default 0{$endif};
38 property PaddingVertical {$ifdef FPC}default 0{$endif};
39 end deprecated 'use TCastleOnScreenMenuItem';
40
41 { Button that looks nice as an "accessory" that can be toggled
42 (shows "yes" / "no" depending on @link(TCastleButton.Pressed)),
43 attached to the TCastleOnScreenMenu item. }
44 TCastleMenuToggle = class(TCastleMenuButton)
45 private
46 { Old TO DO (no longer valid, as this is deprecated):
47 it would be nice if Width of this component was appropriate
48 for the longest possible string ("Yes" in this case).
49 This way the menu could be larger, in preparation of it. }
50 FAutoToggle: boolean;
51 protected
52 procedure SetPressed(const Value: boolean); override;
53 public
54 constructor Create(AOwner: TComponent); override;
55 procedure DoClick; override;
56 published
57 property Toggle default true;
58 { Automatically negate @link(TCastleButton.Pressed Pressed) on each click.
59 This makes it easy to handle this component, often it means that you
60 don't need to handle it's @link(TCastleButton.OnClick OnClick) event,
61 and you only need to observe it's @link(TCastleButton.Pressed Pressed)
62 value. }
63 property AutoToggle: boolean read FAutoToggle write FAutoToggle default false;
64 end deprecated 'use TCastleOnScreenMenuItemToggle';
65
66 { Clickable menu item of @link(TCastleOnScreenMenu). }
67 TCastleOnScreenMenuItem = class(TCastleUserInterface)
68 strict private
69 const
70 Padding = 5;
71 var
72 FCaptionLabel, FRightCaptionLabel: TCastleLabel;
73 FCaption: String;
74 FOnClick: TNotifyEvent;
75 FRightCaption: String;
76 MenuAnimation: Single;
77 ClickStarted: boolean;
78 ClickStartedPosition: TVector2;
79 FCaptionTranslate: Boolean;
80 FEnabled: Boolean;
81 procedure SetCaption(const Value: String);
82 procedure SetRightCaption(const Value: String);
83 { Update Menu.CurrentItem to point to Self. }
84 procedure MakeCurrent;
85 { Containing TCastleOnScreenMenu. }
86 function Menu: TCastleOnScreenMenu;
87 procedure SetEnabled(const Value: Boolean);
88 private
89 function LeftColumnWidth: Single;
90 procedure PositionChildren(const MaxLeftColumnWidth: Single);
91 protected
92 procedure DoClick; virtual;
93 procedure BeforeSizing; override;
94 procedure TranslateProperties(const TranslatePropertyEvent: TTranslatePropertyEvent); override;
95 public
96 constructor Create(AOwner: TComponent); override;
97 procedure Render; override;
98 procedure Update(const SecondsPassed: Single; var HandleInput: boolean); override;
99 function Press(const Event: TInputPressRelease): boolean; override;
100 function Release(const Event: TInputPressRelease): boolean; override;
101 function Motion(const Event: TInputMotion): boolean; override;
102 published
103 property AutoSizeToChildren default true;
104
105 { Text displayed by this on-screen menu item. }
106 property Caption: String read FCaption write SetCaption;
107
108 { Should the @link(Caption) be localized (translated into other languages).
109 Determines if the property is enumerated by @link(TCastleComponent.TranslateProperties),
110 which affects the rest of localization routines. }
111 property CaptionTranslate: Boolean read FCaptionTranslate write FCaptionTranslate default true;
112
113 { Event fired when user chooses this menu item in any way. }
114 property OnClick: TNotifyEvent read FOnClick write FOnClick;
115
116 { Whether menu item is enabled. }
117 property Enabled: Boolean read FEnabled write SetEnabled default true;
118
119 { Additional text displayed on the right side. }
120 property RightCaption: String read FRightCaption write SetRightCaption;
121 end;
122
123 { Menu item of TCastleOnScreenMenuItem that can be toggled.
124 Shows "yes" or "no" depending on a Boolean @link(Checked). }
125 TCastleOnScreenMenuItemToggle = class(TCastleOnScreenMenuItem)
126 strict private
127 FChecked, FAutoToggle: Boolean;
128 procedure SetChecked(const Value: Boolean);
129 protected
130 procedure DoClick; override;
131 public
132 constructor Create(AOwner: TComponent); override;
133 published
134 { The Boolean value of this menu item. }
135 property Checked: Boolean read FChecked write SetChecked default false;
136
137 { Automatically negate @link(Checked) on each click.
138 This makes it easy to handle this component, often it means that you
139 don't need to handle it's @link(OnClick) event. }
140 property AutoToggle: boolean read FAutoToggle write FAutoToggle default false;
141 end;
142
143 { On-screen menu, with all menu items displayed on the screen,
144 one under another. Often used for game "main menu" screen.
145 Normal tools may prefer to use the normal menu bar
146 (@link(TCastleWindow.MainMenu), or normal Lazarus main menu)
147 instead of this.
148
149 Each clickable menu item should be a TCastleOnScreenMenuItem descendant,
150 and should be added to the MenuItems list.
151 The TCastleOnScreenMenuItem.Menu should be set to point back to the menu instance,
152 this way it can look at menu's properties like desired color and font.
153 You can also add other controls to MenuItems -- it's a simple
154 TCastleVerticalGroup, all the controls are just displayed one under
155 another. E.g. you can add TCastleLabel to have non-clickable text,
156 or you can add TCastleUserInterface with explicit TCastleUserInterface.Height
157 to add some vertical space. }
158 TCastleOnScreenMenu = class(TCastleUserInterfaceFont)
159 private
160 type
161 TMenuItems = class(TCastleVerticalGroup)
162 public
163 Menu: TCastleOnScreenMenu;
164 end;
165 var
166 FCaptureAllEvents: boolean;
167 FOnClick: TNotifyEvent;
168 FCurrentItem: Integer;
169 FKeyNextItem: TKey;
170 FKeyPreviousItem: TKey;
171 FKeySelectItem: TKey;
172 MenuAnimation: Single;
173 FCurrentItemBorderColor1: TCastleColor;
174 FCurrentItemBorderColor2: TCastleColor;
175 FCurrentItemColor: TCastleColor;
176 FNonCurrentItemColor: TCastleColor;
177 FNonFocusableItemColor: TCastleColor;
178 FRegularSpaceBetweenItems: Single;
179 FDrawBackgroundRectangle: boolean;
180 FDrawFocusedBorder: boolean;
181 FBackgroundOpacityFocused, FBackgroundOpacityNotFocused: Single;
182 FMenuItems: TCastleVerticalGroup;
183 FSoundClick: TCastleSound;
184 FSoundClickObserver: TFreeNotificationObserver;
185 FSoundCurrentItemChanged: TCastleSound;
186 FSoundCurrentItemChangedObserver: TFreeNotificationObserver;
187
188 function GetCurrentItem: Integer;
189 procedure SetCurrentItem(const Value: Integer);
190 procedure SetRegularSpaceBetweenItems(const Value: Single);
191 procedure SetSoundClick(const Value: TCastleSound);
192 procedure SoundClickFreeNotification(const Sender: TFreeNotificationObserver);
193 procedure SetSoundCurrentItemChanged(const Value: TCastleSound);
194 procedure SoundCurrentItemChangedFreeNotification(const Sender: TFreeNotificationObserver);
195 protected
196 procedure BeforeSizing; override;
197 public
198 const
199 DefaultMenuKeyNextItem = keyArrowDown;
200 DefaultMenuKeyPreviousItem = keyArrowUp;
201 DefaultMenuKeySelectItem = keyEnter;
202
203 DefaultCurrentItemBorderColor1: TCastleColor = (X: 1.0; Y: 1.0; Z: 1.0; W: 1.0) { White }; { }
204 DefaultCurrentItemBorderColor2: TCastleColor = (X: 0.5; Y: 0.5; Z: 0.5; W: 1.0) { Gray }; { }
205 DefaultCurrentItemColor : TCastleColor = (X: 1.0; Y: 1.0; Z: 0.0; W: 1.0) { Yellow }; { }
206 DefaultNonCurrentItemColor : TCastleColor = (X: 1.0; Y: 1.0; Z: 1.0; W: 1.0) { White }; { }
207 DefaultNonFocusableItemColor : TCastleColor = (X:0.75; Y: 0.75; Z: 0.75; W: 1.0) { Light Gray }; { }
208
209 DefaultRegularSpaceBetweenItems = 10;
210 DefaultBackgroundOpacityNotFocused = 0.4;
211 DefaultBackgroundOpacityFocused = 0.7;
212
213 constructor Create(AOwner: TComponent); override;
214 destructor Destroy; override;
215
216 procedure Add(const S: string); overload;
217 procedure Add(const S: string; const Accessory: TCastleUserInterface); overload;
218 procedure Add(const S: string; const ItemOnClick: TNotifyEvent); overload;
219 procedure Add(const NewItem: TCastleUserInterface); overload;
220
221 { Currently selected child index.
222 This is always some number between @code(0) and @code(MenuItems.ControlsCount - 1).
223 It can also be @code(-1) to indicate "no child is selected".
224 When MenuItems is empty (@code(MenuItems.ControlsCount = 0)) it is always @code(-1).
225
226 You can change it by code.
227 Assigning here an invalid value (e.g. larger then @code(MenuItems.ControlsCount - 1))
228 will be automatically fixed. Also when the children change
229 (and so ControlsCount changes), this is automatically fixed if necessary.
230
231 Changing this calls CurrentItemChanged automatically,
232 but not CurrentItemChangedByUser. }
233 property CurrentItem: Integer read GetCurrentItem write SetCurrentItem;
234
235 { Change CurrentItem to next or previous.
236 Usually you will just let this class call it internally
237 (from Motion, KeyDown etc.) and will not need to call it yourself.
238
239 @param(UserAction Determines should we play a sound when user changes
240 menu items.)
241
242 @groupBegin }
243 procedure NextItem;
244 procedure PreviousItem;
245 { @groupEnd }
246
247 function CapturesEventsAtPosition(const Position: TVector2): boolean; override;
248 procedure Render; override;
249
250 property KeyNextItem: TKey read FKeyNextItem write FKeyNextItem
251 default DefaultMenuKeyNextItem;
252 property KeyPreviousItem: TKey read FKeyPreviousItem write FKeyPreviousItem
253 default DefaultMenuKeyPreviousItem;
254 property KeySelectItem: TKey read FKeySelectItem write FKeySelectItem
255 default DefaultMenuKeySelectItem;
256
257 function Press(const Event: TInputPressRelease): boolean; override;
258 procedure Update(const SecondsPassed: Single;
259 var HandleInput: boolean); override;
260 function AllowSuspendForInput: boolean; override;
261
262 { Called when user will select CurrentItem, either with mouse
263 or with keyboard. }
264 procedure Click; virtual; deprecated 'use TCastleMenuButton and it''s OnClick event';
265
266 { @deprecated Deprecated name for Click. }
267 procedure CurrentItemSelected; virtual; deprecated 'use TCastleMenuButton and it''s OnClick event';
268
269 { Called when CurrentItem changed.
270 But *not* when CurrentItem changed because of ControlsCount changes.
271 If you want to only get notified when the CurrentItem changed because
272 of user actions, better override CurrentItemChangedByUser. }
273 procedure CurrentItemChanged; virtual;
274
275 { Called when CurrentItem changed by a user action (keyboard press,
276 mouse move or click or something like that). }
277 procedure CurrentItemChangedByUser; virtual;
278
279 { 1st color of the border to display around focused child.
280 Default value is DefaultCurrentItemBorderColor1 }
281 property CurrentItemBorderColor1: TCastleColor
282 read FCurrentItemBorderColor1 write FCurrentItemBorderColor1;
283 { 2nd color of the border to display around focused child.
284 Default value is DefaultCurrentItemBorderColor2 }
285 property CurrentItemBorderColor2: TCastleColor
286 read FCurrentItemBorderColor2 write FCurrentItemBorderColor2;
287 { Label color for the focused child.
288 Default value is DefaultCurrentItemColor }
289 property CurrentItemColor: TCastleColor
290 read FCurrentItemColor write FCurrentItemColor;
291 { Label color for the non-focused but focusable child.
292 Default value is DefaultNonCurrentItemColor }
293 property NonCurrentItemColor: TCastleColor
294 read FNonCurrentItemColor write FNonCurrentItemColor;
295 { Label color for the non-focusable child (or TCastleOnScreenMenuItem with TCastleOnScreenMenuItem.Enabled = @false).
296 Default value is DefaultNonFocusableItemColor }
297 property NonFocusableItemColor: TCastleColor
298 read FNonFocusableItemColor write FNonFocusableItemColor;
299
300 { Return the space needed before NextItemIndex.
301 This will be a space between NextItemIndex - 1 and NextItemIndex
302 (this method will not be called for NextItemIndex = 0).
303
304 Default implementation in this class simply returns
305 RegularSpaceBetweenItems always.
306
307 Note that this is used only at RecalculateSize call.
308 So when some variable affecting the implementation of this changes,
309 you should call RecalculateSize again. }
310 function SpaceBetweenItems(const NextItemIndex: Cardinal): Single; virtual;
311 deprecated 'ignored now; if you want to add extra space, add TCastleUserInterface with explicit Height to MenuItems';
312
313 {$ifdef FPC}
314 { Called when user will select CurrentItem.
315 @seealso Click }
316 property OnClick: TNotifyEvent read FOnClick write FOnClick;
317 deprecated 'use Add method to add a particular menu item with it''s own click callback; or just add TCastleMenuButton and handle it''s OnClick event';
318 {$endif}
319 published
320 property AutoSizeToChildren default true;
321
322 { Menu items, and things between menu items, should be added here. }
323 property MenuItems: TCastleVerticalGroup read FMenuItems;
324
325 { Opacity of the background rectangle (displayed when DrawBackgroundRectangle).
326 @groupBegin }
327 property BackgroundOpacityFocused: Single
328 read FBackgroundOpacityFocused
329 write FBackgroundOpacityFocused
330 {$ifdef FPC}default DefaultBackgroundOpacityFocused{$endif};
331 property BackgroundOpacityNotFocused: Single
332 read FBackgroundOpacityNotFocused
333 write FBackgroundOpacityNotFocused
334 {$ifdef FPC}default DefaultBackgroundOpacityNotFocused{$endif};
335 { @groupEnd }
336
337 property DrawBackgroundRectangle: boolean
338 read FDrawBackgroundRectangle write FDrawBackgroundRectangle
339 default true;
340
341 { Additional vertical space, in pixels, between menu items.
342
343 If you want more control over it (if you want to add more/less
344 space between some menu items), override SpaceBetweenItems method. }
345 property RegularSpaceBetweenItems: Single
346 read FRegularSpaceBetweenItems write SetRegularSpaceBetweenItems
347 {$ifdef FPC}default DefaultRegularSpaceBetweenItems{$endif};
348
349 { Draw a flashing border around the menu when we are focused. }
350 property DrawFocusedBorder: boolean read FDrawFocusedBorder write FDrawFocusedBorder
351 default true;
352
353 { Should menu intercept all key/mouse input, regardless if mouse position
354 is over our rectangle.
355 This affects key/mouse processing (menu processes input
356 before all controls underneath), but not drawing (controls underneath
357 are still visible as usual). }
358 property CaptureAllEvents: boolean
359 read FCaptureAllEvents write FCaptureAllEvents default false;
360
361 property SoundClick: TCastleSound
362 read FSoundClick write SetSoundClick;
363 property SoundCurrentItemChanged: TCastleSound
364 read FSoundCurrentItemChanged write SetSoundCurrentItemChanged;
365
366 {$define read_interface_class}
367 {$I auto_generated_persistent_vectors/tcastleonscreenmenu_persistent_vectors.inc}
368 {$undef read_interface_class}
369 end;
370
371 implementation
372
373 uses SysUtils, CastleUtils, CastleImages, CastleFilesUtils,
374 CastleStringUtils, CastleGLImages, CastleComponentSerialize;
375
376 { TCastleMenuButton ---------------------------------------------------------- }
377
378 constructor TCastleMenuButton.Create(AOwner: TComponent);
379 begin
380 inherited;
381 CustomBackground := true; // leave CustomBackgroundImage for transparent background
382 CustomTextColorUse := true;
383 CustomTextColor := LightGreen;
384 PaddingHorizontal := 0;
385 PaddingVertical := 0;
386 end;
387
388 { TCastleMenuToggle ---------------------------------------------------------- }
389
390 constructor TCastleMenuToggle.Create(AOwner: TComponent);
391 begin
392 inherited;
393 Toggle := true;
394 Caption := Iff(Pressed, 'Yes', 'No');
395 end;
396
397 procedure TCastleMenuToggle.SetPressed(const Value: boolean);
398 begin
399 inherited;
400 Caption := Iff(Pressed, 'Yes', 'No');
401 end;
402
403 procedure TCastleMenuToggle.DoClick;
404 begin
405 // change Boolean value before calling OnClick in inherited
406 if AutoToggle then
407 Pressed := not Pressed;
408 inherited;
409 end;
410
411 { TCastleOnScreenMenuItem ---------------------------------------------------- }
412
413 constructor TCastleOnScreenMenuItem.Create(AOwner: TComponent);
414 begin
415 inherited;
416 FEnabled := true;
417 FCaptionTranslate := true;
418
419 FCaptionLabel := TCastleLabel.Create(Self);
420 FCaptionLabel.SetTransient;
421 FCaptionLabel.Anchor(vpMiddle);
422 FCaptionLabel.Anchor(hpLeft, Padding);
423 //FCaptionLabel.PaddingVertical := Padding;
424 InsertFront(FCaptionLabel);
425
426 FRightCaptionLabel := TCastleLabel.Create(Self);
427 FRightCaptionLabel.SetTransient;
428 // the anchors for FRightCaptionLabel will be set in TCastleOnScreenMenuItem.PositionChildren
429 FRightCaptionLabel.Exists := false;
430 FRightCaptionLabel.Color := LightGreen;
431 InsertFront(FRightCaptionLabel);
432
433 AutoSizeToChildren := true;
434 AutoSizeToChildrenPaddingRight := Padding;
435 end;
436
437 function TCastleOnScreenMenuItem.LeftColumnWidth: Single;
438 begin
439 Result := FCaptionLabel.EffectiveWidth + Padding { padding on the left };
440 end;
441
442 procedure TCastleOnScreenMenuItem.PositionChildren(const MaxLeftColumnWidth: Single);
443 var
444 I: Integer;
445 begin
446 for I := 0 to ControlsCount - 1 do
447 if Controls[I] <> FCaptionLabel then
448 begin
449 Controls[I].Anchor(hpLeft, MaxLeftColumnWidth);
450 Controls[I].Anchor(vpMiddle);
451 end;
452 end;
453
454 procedure TCastleOnScreenMenuItem.SetCaption(const Value: String);
455 begin
456 if FCaption <> Value then
457 begin
458 FCaption := Value;
459 FCaptionLabel.Caption := Value;
460 VisibleChange([chRectangle]);
461 end;
462 end;
463
464 procedure TCastleOnScreenMenuItem.SetEnabled(const Value: Boolean);
465 begin
466 if FEnabled <> Value then
467 begin
468 FEnabled := Value;
469 VisibleChange([chRectangle]);
470 end;
471 end;
472
473 procedure TCastleOnScreenMenuItem.SetRightCaption(const Value: String);
474 begin
475 if FRightCaption <> Value then
476 begin
477 FRightCaption := Value;
478 FRightCaptionLabel.Caption := Value;
479 FRightCaptionLabel.Exists := Value <> '';
480 VisibleChange([chRectangle]);
481 end;
482 end;
483
484 procedure TCastleOnScreenMenuItem.BeforeSizing;
485 var
486 M: TCastleOnScreenMenu;
487 begin
488 inherited;
489
490 M := Menu;
491 if M <> nil then
492 begin
493 FCaptionLabel.AssignFont(M);
494 FRightCaptionLabel.AssignFont(M);
495 end;
496 end;
497
498 procedure TCastleOnScreenMenuItem.Render;
499 var
500 US: Single;
501
502 function Current: Boolean;
503 begin
504 Result :=
505 (Menu <> nil) and
506 Between(Menu.CurrentItem, 0, Menu.MenuItems.ControlsCount - 1) and
507 (Menu.MenuItems.Controls[Menu.CurrentItem] = Self);
508 end;
509
510 var
511 RR: TFloatRectangle;
512 ItemColor, CurrentItemBorderColor: TCastleColor;
513 begin
514 inherited;
515
516 US := UIScale;
517 RR := RenderRect;
518
519 if Menu <> nil then
520 begin
521 if not Enabled then
522 ItemColor := Menu.NonFocusableItemColor
523 else
524 if Current then
525 begin
526 { Calculate CurrentItemBorderColor }
527 if MenuAnimation <= 0.5 then
528 CurrentItemBorderColor := Lerp(
529 MapRange(MenuAnimation, 0, 0.5, 0, 1),
530 Menu.CurrentItemBorderColor1, Menu.CurrentItemBorderColor2)
531 else
532 CurrentItemBorderColor := Lerp(
533 MapRange(MenuAnimation, 0.5, 1, 0, 1),
534 Menu.CurrentItemBorderColor2, Menu.CurrentItemBorderColor1);
535 Theme.Draw(RR, tiActiveFrame, US, CurrentItemBorderColor);
536
537 ItemColor := Menu.CurrentItemColor;
538 end else
539 ItemColor := Menu.NonCurrentItemColor;
540 end else
541 begin
542 if Enabled then
543 ItemColor := TCastleOnScreenMenu.DefaultCurrentItemColor
544 else
545 ItemColor := TCastleOnScreenMenu.DefaultNonFocusableItemColor;
546 end;
547
548 FCaptionLabel.Color := ItemColor;
549 end;
550
551 function TCastleOnScreenMenuItem.Menu: TCastleOnScreenMenu;
552 begin
553 if Parent is TCastleOnScreenMenu.TMenuItems then
554 Result := TCastleOnScreenMenu.TMenuItems(Parent).Menu
555 else
556 Result := nil;
557 end;
558
559 procedure TCastleOnScreenMenuItem.MakeCurrent;
560 var
561 Index: Integer;
562 begin
563 if Menu <> nil then
564 begin
565 Index := Menu.MenuItems.IndexOfControl(Self);
566 if (Index <> -1) and
567 (Menu.CurrentItem <> Index) then
568 begin
569 Menu.CurrentItemChangedByUser;
570 Menu.CurrentItem := Index;
571 end;
572 end;
573 end;
574
575 function TCastleOnScreenMenuItem.Press(const Event: TInputPressRelease): boolean;
576 begin
577 Result := inherited;
578 if Result or (Event.EventType <> itMouseButton) then Exit;
579
580 Result := true;
581 MakeCurrent;
582 ClickStarted := true;
583 ClickStartedPosition := Event.Position;
584 end;
585
586 function TCastleOnScreenMenuItem.Release(const Event: TInputPressRelease): boolean;
587 begin
588 Result := inherited;
589 if Result or (Event.EventType <> itMouseButton) then Exit;
590
591 if ClickStarted then
592 begin
593 Result := true;
594 ClickStarted := false;
595 if CapturesEventsAtPosition(Event.Position) then
596 DoClick;
597 end;
598 end;
599
600 function TCastleOnScreenMenuItem.Motion(const Event: TInputMotion): boolean;
601 begin
602 Result := inherited;
603 if Result then Exit;
604
605 if Event.Pressed = [] then
606 begin
607 MakeCurrent;
608 Result := true;
609 end;
610 end;
611
612 procedure TCastleOnScreenMenuItem.DoClick;
613 begin
614 if not Enabled then
615 Exit;
616 {$warnings off}
617 if Menu <> nil then
618 Menu.Click; // keep deprecated Menu.Click working
619 {$warnings on}
620 if Menu.SoundClick <> nil then
621 SoundEngine.Play(Menu.SoundClick)
622 else
623 {$warnings off} // keep deprecated working
624 SoundEngine.Play(stMenuClick);
625 {$warnings on}
626 if Assigned(OnClick) then
627 OnClick(Self);
628 end;
629
630 procedure TCastleOnScreenMenuItem.Update(const SecondsPassed: Single;
631 var HandleInput: boolean);
632 begin
633 inherited;
634 MenuAnimation := MenuAnimation + (0.5 * SecondsPassed);
635 MenuAnimation := Frac(MenuAnimation);
636 VisibleChange([chRender]);
637 end;
638
639 procedure TCastleOnScreenMenuItem.TranslateProperties(
640 const TranslatePropertyEvent: TTranslatePropertyEvent);
641 var
642 S: String;
643 begin
644 if CaptionTranslate and (Caption <> '') then
645 begin
646 S := Caption;
647 TranslatePropertyEvent(Self, 'Caption', S);
648 Caption := S;
649 end;
650 end;
651
652 { TCastleOnScreenMenuItemToggle ---------------------------------------------------------- }
653
654 constructor TCastleOnScreenMenuItemToggle.Create(AOwner: TComponent);
655 begin
656 inherited;
657 RightCaption := Iff(Checked, 'Yes', 'No');
658 end;
659
660 procedure TCastleOnScreenMenuItemToggle.SetChecked(const Value: boolean);
661 begin
662 if FChecked <> Value then
663 begin
664 FChecked := Value;
665 RightCaption := Iff(Checked, 'Yes', 'No');
666 end;
667 end;
668
669 procedure TCastleOnScreenMenuItemToggle.DoClick;
670 begin
671 // change Boolean value before calling OnClick in inherited
672 if AutoToggle then
673 Checked := not Checked;
674 inherited;
675 end;
676
677 { TCastleOnScreenMenu -------------------------------------------------------------------- }
678
679 constructor TCastleOnScreenMenu.Create(AOwner: TComponent);
680 begin
681 inherited;
682
683 FMenuItems := TMenuItems.Create(Self);
684 TMenuItems(FMenuItems).Menu := Self;
685 FMenuItems.SetSubComponent(true);
686 FMenuItems.Name := 'MenuItems';
687 FMenuItems.Padding := 30;
688 InsertFront(FMenuItems);
689
690 FCurrentItem := 0;
691 BackgroundOpacityNotFocused := DefaultBackgroundOpacityNotFocused;
692 BackgroundOpacityFocused := DefaultBackgroundOpacityFocused;
693
694 KeyNextItem := DefaultMenuKeyNextItem;
695 KeyPreviousItem := DefaultMenuKeyPreviousItem;
696 KeySelectItem := DefaultMenuKeySelectItem;
697
698 FCurrentItemBorderColor1 := DefaultCurrentItemBorderColor1;
699 FCurrentItemBorderColor2 := DefaultCurrentItemBorderColor2;
700 FCurrentItemColor := DefaultCurrentItemColor;
701 FNonCurrentItemColor := DefaultNonCurrentItemColor;
702 FNonFocusableItemColor := DefaultNonFocusableItemColor;
703
704 FRegularSpaceBetweenItems := DefaultRegularSpaceBetweenItems;
705 FMenuItems.Spacing := RegularSpaceBetweenItems;
706
707 FDrawBackgroundRectangle := true;
708 FDrawFocusedBorder := true;
709 AutoSizeToChildren := true;
710
711 FSoundClickObserver := TFreeNotificationObserver.Create(Self);
712 FSoundClickObserver.OnFreeNotification := {$ifdef FPC}@{$endif}SoundClickFreeNotification;
713 FSoundCurrentItemChangedObserver := TFreeNotificationObserver.Create(Self);
714 FSoundCurrentItemChangedObserver.OnFreeNotification := {$ifdef FPC}@{$endif}SoundCurrentItemChangedFreeNotification;
715
716 {$define read_implementation_constructor}
717 {$I auto_generated_persistent_vectors/tcastleonscreenmenu_persistent_vectors.inc}
718 {$undef read_implementation_constructor}
719 end;
720
721 destructor TCastleOnScreenMenu.Destroy;
722 begin
723 {$define read_implementation_destructor}
724 {$I auto_generated_persistent_vectors/tcastleonscreenmenu_persistent_vectors.inc}
725 {$undef read_implementation_destructor}
726 inherited;
727 end;
728
729 function TCastleOnScreenMenu.GetCurrentItem: Integer;
730 begin
731 Result := FCurrentItem;
732
733 { Make sure that CurrentItem conditions are OK.
734 User can change the Controls list at any moment, so you cannot depend that
735 FCurrentItem is Ok here. }
736
737 if MenuItems.ControlsCount <> 0 then
738 begin
739 ClampVar(Result, 0, MenuItems.ControlsCount - 1);
740 end else
741 Result := -1;
742 end;
743
744 procedure TCastleOnScreenMenu.SetCurrentItem(const Value: Integer);
745 var
746 OldCurrentItem, NewCurrentItem: Integer;
747 begin
748 OldCurrentItem := CurrentItem;
749 FCurrentItem := Value;
750
751 NewCurrentItem := CurrentItem;
752 if OldCurrentItem <> NewCurrentItem then
753 CurrentItemChanged;
754 end;
755
756 procedure TCastleOnScreenMenu.NextItem;
757 var
758 OldCurrentItem, NewCurrentItem: Integer;
759 begin
760 if MenuItems.ControlsCount <> 0 then
761 begin
762 OldCurrentItem := CurrentItem;
763 repeat
764 if CurrentItem = MenuItems.ControlsCount - 1 then
765 CurrentItem := 0
766 else
767 CurrentItem := CurrentItem + 1;
768 { Loop until you find the next focusable control.
769 Also, stop when you reach the original control (to avoid an infinite loop). }
770 until
771 (MenuItems.Controls[CurrentItem] is TCastleOnScreenMenuItem) or
772 (CurrentItem = OldCurrentItem);
773
774 NewCurrentItem := CurrentItem;
775 if OldCurrentItem <> NewCurrentItem then
776 CurrentItemChangedByUser;
777 end;
778 end;
779
780 procedure TCastleOnScreenMenu.PreviousItem;
781 var
782 OldCurrentItem, NewCurrentItem: Integer;
783 begin
784 if MenuItems.ControlsCount <> 0 then
785 begin
786 OldCurrentItem := CurrentItem;
787 repeat
788 if CurrentItem = 0 then
789 CurrentItem := MenuItems.ControlsCount - 1
790 else
791 CurrentItem := CurrentItem - 1;
792 { Loop until you find the next focusable control.
793 Also, stop when you reach the original control (to avoid an infinite loop). }
794 until
795 (MenuItems.Controls[CurrentItem] is TCastleOnScreenMenuItem) or
796 (CurrentItem = OldCurrentItem);
797
798 NewCurrentItem := CurrentItem;
799 if OldCurrentItem <> NewCurrentItem then
800 CurrentItemChangedByUser;
801 end;
802 end;
803
804 function TCastleOnScreenMenu.SpaceBetweenItems(const NextItemIndex: Cardinal): Single;
805 begin
806 Result := RegularSpaceBetweenItems;
807 end;
808
809 procedure TCastleOnScreenMenu.BeforeSizing;
810 const
811 MarginBeforeAccessory = 20;
812 var
813 MaxLeftColumnWidth: Single;
814 I: Integer;
815 begin
816 inherited;
817
818 { recalculate MaxLeftColumnWidth and update children position based on it }
819 MaxLeftColumnWidth := 0;
820 for I := 0 to MenuItems.ControlsCount - 1 do
821 if MenuItems.Controls[I] is TCastleOnScreenMenuItem then
822 MaxVar(MaxLeftColumnWidth, TCastleOnScreenMenuItem(MenuItems.Controls[I]).LeftColumnWidth);
823 if MaxLeftColumnWidth <> 0 then
824 MaxLeftColumnWidth := MaxLeftColumnWidth + MarginBeforeAccessory;
825 for I := 0 to MenuItems.ControlsCount - 1 do
826 if MenuItems.Controls[I] is TCastleOnScreenMenuItem then
827 TCastleOnScreenMenuItem(MenuItems.Controls[I]).PositionChildren(MaxLeftColumnWidth);
828 end;
829
830 procedure TCastleOnScreenMenu.Render;
831 var
832 BgColor, CurrentItemBorderColor: TCastleColor;
833 SR: TFloatRectangle;
834 begin
835 inherited;
836
837 SR := RenderRect;
838
839 if DrawBackgroundRectangle then
840 begin
841 if Focused then
842 BgColor := Vector4(0, 0, 0, BackgroundOpacityFocused) else
843 BgColor := Vector4(0, 0, 0, BackgroundOpacityNotFocused);
844 DrawRectangle(SR, BgColor);
845 end;
846
847 { Calculate CurrentItemBorderColor }
848 if MenuAnimation <= 0.5 then
849 CurrentItemBorderColor := Lerp(
850 MapRange(MenuAnimation, 0, 0.5, 0, 1),
851 CurrentItemBorderColor1, CurrentItemBorderColor2)
852 else
853 CurrentItemBorderColor := Lerp(
854 MapRange(MenuAnimation, 0.5, 1, 0, 1),
855 CurrentItemBorderColor2, CurrentItemBorderColor1);
856
857 if Focused and DrawFocusedBorder then
858 Theme.Draw(SR, tiActiveFrame, UIScale, CurrentItemBorderColor);
859 end;
860
861 function TCastleOnScreenMenu.Press(const Event: TInputPressRelease): boolean;
862 begin
863 Result := inherited;
864 if Result then Exit;
865
866 if Event.IsKey(KeyPreviousItem) then
867 begin
868 PreviousItem;
869 Result := true;
870 end else
871 if Event.IsKey(KeyNextItem) then
872 begin
873 NextItem;
874 Result := true;
875 end else
876 if Event.IsKey(KeySelectItem) and
877 Between(CurrentItem, 0, MenuItems.ControlsCount - 1) and
878 (MenuItems.Controls[CurrentItem] is TCastleOnScreenMenuItem) then
879 begin
880 TCastleOnScreenMenuItem(MenuItems.Controls[CurrentItem]).DoClick;
881 Result := true;
882 end;
883 end;
884
885 procedure TCastleOnScreenMenu.Update(const SecondsPassed: Single;
886 var HandleInput: boolean);
887 begin
888 inherited;
889
890 { some keys pressed are already handled by Press }
891 if HandleInput and
892 (Container.Pressed[KeyPreviousItem] or
893 Container.Pressed[KeyNextItem] or
894 Container.Pressed[KeySelectItem]) then
895 HandleInput := false;
896
897 MenuAnimation := MenuAnimation + (0.5 * SecondsPassed);
898 MenuAnimation := Frac(MenuAnimation);
899 VisibleChange([chRender]);
900 end;
901
902 function TCastleOnScreenMenu.AllowSuspendForInput: boolean;
903 begin
904 Result := false;
905 end;
906
907 procedure TCastleOnScreenMenu.Click;
908 begin
909 // TODO: check this in Delphi
910 {$ifdef FPC}
911 {$warnings off}
912 if Assigned(OnClick) then OnClick(Self);
913 {$warnings on}
914 {$endif}
915 end;
916
917 procedure TCastleOnScreenMenu.CurrentItemChanged;
918 begin
919 VisibleChange([chRender]);
920 end;
921
922 procedure TCastleOnScreenMenu.CurrentItemChangedByUser;
923 begin
924 if SoundCurrentItemChanged <> nil then
925 SoundEngine.Play(SoundCurrentItemChanged)
926 else
927 {$warnings off} // keep deprecated working
928 SoundEngine.Play(stMenuCurrentItemChanged);
929 {$warnings on}
930 end;
931
932 procedure TCastleOnScreenMenu.CurrentItemSelected;
933 begin
934 {$warnings off}
935 Click;
936 {$warnings on}
937 end;
938
939 function TCastleOnScreenMenu.CapturesEventsAtPosition(const Position: TVector2): boolean;
940 begin
941 Result := CaptureAllEvents or (inherited CapturesEventsAtPosition(Position));
942 end;
943
944 procedure TCastleOnScreenMenu.SetRegularSpaceBetweenItems(const Value: Single);
945 begin
946 if FRegularSpaceBetweenItems <> Value then
947 begin
948 FRegularSpaceBetweenItems := Value;
949 FMenuItems.Spacing := RegularSpaceBetweenItems;
950 end;
951 end;
952
953 procedure TCastleOnScreenMenu.Add(const NewItem: TCastleUserInterface);
954 begin
955 MenuItems.InsertFront(NewItem);
956 end;
957
958 procedure TCastleOnScreenMenu.Add(const S: string; const Accessory: TCastleUserInterface);
959 var
960 Item: TCastleOnScreenMenuItem;
961 begin
962 if Accessory <> nil then
963 begin
964 Item := TCastleOnScreenMenuItem.Create(Self);
965 Item.Caption := S;
966 Item.InsertFront(Accessory);
967 Add(Item);
968 end else
969 begin
970 Add(S); // add only TCastleLabel
971 end;
972 end;
973
974 procedure TCastleOnScreenMenu.Add(const S: string);
975 var
976 ItemLabel: TCastleLabel;
977 begin
978 ItemLabel := TCastleLabel.Create(Self);
979 ItemLabel.Caption := S;
980 ItemLabel.Color := NonFocusableItemColor;
981 ItemLabel.Padding := 5; // like TCastleOnScreenMenuItem.Padding
982 Add(ItemLabel);
983 end;
984
985 procedure TCastleOnScreenMenu.Add(const S: string; const ItemOnClick: TNotifyEvent);
986 var
987 Item: TCastleOnScreenMenuItem;
988 begin
989 Item := TCastleOnScreenMenuItem.Create(Self);
990 Item.Caption := S;
991 Item.OnClick := ItemOnClick;
992 Add(Item);
993 end;
994
995 procedure TCastleOnScreenMenu.SetSoundClick(const Value: TCastleSound);
996 begin
997 if FSoundClick <> Value then
998 begin
999 FSoundClick := Value;
1000 FSoundClickObserver.Observed := Value;
1001 end;
1002 end;
1003
1004 procedure TCastleOnScreenMenu.SoundClickFreeNotification(const Sender: TFreeNotificationObserver);
1005 begin
1006 FSoundClick := nil;
1007 end;
1008
1009 procedure TCastleOnScreenMenu.SetSoundCurrentItemChanged(const Value: TCastleSound);
1010 begin
1011 if FSoundCurrentItemChanged <> Value then
1012 begin
1013 FSoundCurrentItemChanged := Value;
1014 FSoundCurrentItemChangedObserver.Observed := Value;
1015 end;
1016 end;
1017
1018 procedure TCastleOnScreenMenu.SoundCurrentItemChangedFreeNotification(const Sender: TFreeNotificationObserver);
1019 begin
1020 FSoundCurrentItemChanged := nil;
1021 end;
1022
1023 {$define read_implementation_methods}
1024 {$I auto_generated_persistent_vectors/tcastleonscreenmenu_persistent_vectors.inc}
1025 {$undef read_implementation_methods}
1026
1027 initialization
1028 RegisterSerializableComponent(TCastleOnScreenMenu, 'On-screen Menu');
1029 RegisterSerializableComponent(TCastleOnScreenMenuItem, 'On-screen Menu Item');
1030 end.