Debian Codegraph @

examples/user_interface/zombie_fighter_alternative_short_lived_views/code/gameviewaskdialog.pas

raw
1 {
2 Copyright 2016-2022 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 { Game view that asks a question using a dialog. }
17 unit GameViewAskDialog;
18
19 interface
20
21 uses Classes,
22 CastleControls, CastleUIControls;
23
24 type
25 TViewAskDialog = class(TCastleView)
26 strict private
27 type
28 { Example of designing a custom TCastleUserInterface descendant,
29 which acts as reusable UI control.
30
31 Note: In this example, this is quite pointless.
32 You could (and should) just put the dialog inside
33 gameviewaskdialog.castle-user-interface, and you don't need nested
34 class TZombieDialog! This is just a demo that you *could* design a reusable
35 UI component like this.
36
37 Other approach to have reusable UI is to use TCastleDesign to load one
38 design inside another. See examples/advanced_editor/ for demo. }
39 TZombieDialog = class(TCastleUserInterface)
40 strict private
41 ImageEnemy: TCastleImageControl;
42 ButtonRun, ButtonFight: TCastleButton;
43 procedure ClickRun(Sender: TObject);
44 procedure ClickFight(Sender: TObject);
45 public
46 View: TViewAskDialog; //< set after creation
47 constructor Create(AOwner: TComponent; const Male: boolean); reintroduce;
48 end;
49 var
50 Dialog: TZombieDialog;
51 { Whether to show male image. Set by constructor. }
52 Male: boolean;
53 public
54 constructor CreateUntilStopped(const AMale: Boolean);
55 procedure Start; override;
56 end;
57
58 implementation
59
60 uses CastleUtils, CastleVectors, CastleComponentSerialize;
61
62 { TViewAskDialog.TZombieDialog ---------------------------------------------- }
63
64 constructor TViewAskDialog.TZombieDialog.Create(AOwner: TComponent; const Male: boolean);
65 var
66 UiOwner: TComponent;
67 Ui: TCastleUserInterface;
68 begin
69 inherited Create(AOwner);
70
71 // UiOwner is useful to keep reference to all components loaded from the design
72 UiOwner := TComponent.Create(Self);
73
74 { Load designed user interface }
75 Ui := UserInterfaceLoad('castle-data:/ask_dialog.castle-user-interface', UiOwner);
76 InsertFront(Ui);
77
78 { Find components, by name, that we need to access from code }
79 ImageEnemy := UiOwner.FindRequiredComponent('ImageEnemy') as TCastleImageControl;
80 ButtonRun := UiOwner.FindRequiredComponent('ButtonRun') as TCastleButton;
81 ButtonFight := UiOwner.FindRequiredComponent('ButtonFight') as TCastleButton;
82
83 if Male then
84 ImageEnemy.URL := 'castle-data:/enemy_images/Male-Zombie-300px.png'
85 else
86 ImageEnemy.URL := 'castle-data:/enemy_images/Female-Zombie-300px.png';
87
88 ButtonRun.OnClick := {$ifdef FPC}@{$endif}ClickRun;
89 ButtonFight.OnClick := {$ifdef FPC}@{$endif}ClickFight;
90
91 { Set own size to be equal to designed dialog in ask_dialog.castle-user-interface,
92 that has explicit Width and Height set in editor. }
93 AutoSizeToChildren := true;
94 end;
95
96 procedure TViewAskDialog.TZombieDialog.ClickRun(Sender: TObject);
97 begin
98 { As this is just a demo, there's no actual "running",
99 we just return to TViewPlay. }
100 Container.PopView(View);
101 end;
102
103 procedure TViewAskDialog.TZombieDialog.ClickFight(Sender: TObject);
104 begin
105 { As this is just a demo, there's no actual "fighting",
106 we just return to TViewPlay. }
107 Container.PopView(View);
108 end;
109
110 { TViewAskDialog ------------------------------------------------------------ }
111
112 constructor TViewAskDialog.CreateUntilStopped(const AMale: Boolean);
113 begin
114 inherited CreateUntilStopped;
115 Male := AMale;
116 DesignUrl := 'castle-data:/gameviewaskdialog.castle-user-interface';
117 end;
118
119 procedure TViewAskDialog.Start;
120 begin
121 inherited;
122
123 { Do not allow clicks to pass to ViewPlay underneath.
124 We are transparent (show the ViewPlay underneath),
125 but we don't want to allow user to interact with it (e.g. by causing
126 another ViewAskDialog by clicking, or by pressing on
127 ViewPlay.ButtonBack). }
128 InterceptInput := true;
129
130 Dialog := TZombieDialog.Create(FreeAtStop, Male);
131 Dialog.View := Self;
132 Dialog.Anchor(hpMiddle);
133 Dialog.Anchor(vpMiddle);
134 InsertFront(Dialog);
135 end;
136
137 end.