Debian Codegraph @

examples/user_interface/zombie_fighter/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 view_ask_dialog.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 constructor Create(AOwner: TComponent; const Male: boolean); reintroduce;
47 end;
48 var
49 Dialog: TZombieDialog;
50 public
51 { Whether to show male image. Set before doing @link(Start). }
52 Male: boolean;
53 constructor Create(AOwner: TComponent); override;
54 procedure Start; override;
55 end;
56
57 var
58 ViewAskDialog: TViewAskDialog;
59
60 implementation
61
62 uses CastleUtils, CastleVectors, CastleComponentSerialize;
63
64 { TViewAskDialog.TZombieDialog ---------------------------------------------- }
65
66 constructor TViewAskDialog.TZombieDialog.Create(AOwner: TComponent; const Male: boolean);
67 var
68 UiOwner: TComponent;
69 Ui: TCastleUserInterface;
70 begin
71 inherited Create(AOwner);
72
73 // UiOwner is useful to keep reference to all components loaded from the design
74 UiOwner := TComponent.Create(Self);
75
76 { Load designed user interface }
77 Ui := UserInterfaceLoad('castle-data:/ask_dialog.castle-user-interface', UiOwner);
78 InsertFront(Ui);
79
80 { Find components, by name, that we need to access from code }
81 ImageEnemy := UiOwner.FindRequiredComponent('ImageEnemy') as TCastleImageControl;
82 ButtonRun := UiOwner.FindRequiredComponent('ButtonRun') as TCastleButton;
83 ButtonFight := UiOwner.FindRequiredComponent('ButtonFight') as TCastleButton;
84
85 if Male then
86 ImageEnemy.URL := 'castle-data:/enemy_images/Male-Zombie-300px.png'
87 else
88 ImageEnemy.URL := 'castle-data:/enemy_images/Female-Zombie-300px.png';
89
90 ButtonRun.OnClick := {$ifdef FPC}@{$endif}ClickRun;
91 ButtonFight.OnClick := {$ifdef FPC}@{$endif}ClickFight;
92
93 { Set own size to be equal to designed dialog in ask_dialog.castle-user-interface,
94 that has explicit Width and Height set in editor. }
95 AutoSizeToChildren := true;
96 end;
97
98 procedure TViewAskDialog.TZombieDialog.ClickRun(Sender: TObject);
99 begin
100 { As this is just a demo, there's no actual "running",
101 we just return to ViewPlay. }
102 Container.PopView(ViewAskDialog);
103 end;
104
105 procedure TViewAskDialog.TZombieDialog.ClickFight(Sender: TObject);
106 begin
107 { As this is just a demo, there's no actual "fighting",
108 we just return to ViewPlay. }
109 Container.PopView(ViewAskDialog);
110 end;
111
112 { TViewAskDialog ------------------------------------------------------------ }
113
114 constructor TViewAskDialog.Create(AOwner: TComponent);
115 begin
116 inherited;
117 DesignUrl := 'castle-data:/gameviewaskdialog.castle-user-interface';
118 end;
119
120 procedure TViewAskDialog.Start;
121 begin
122 inherited;
123
124 { Do not allow clicks to pass to ViewPlay underneath.
125 We are transparent (show the ViewPlay underneath),
126 but we don't want to allow user to interact with it (e.g. by causing
127 another ViewAskDialog by clicking, or by pressing on
128 ViewPlay.ButtonBack). }
129 InterceptInput := true;
130
131 Dialog := TZombieDialog.Create(FreeAtStop, Male);
132 Dialog.Anchor(hpMiddle);
133 Dialog.Anchor(vpMiddle);
134 InsertFront(Dialog);
135 end;
136
137 end.