Debian Codegraph @

examples/advanced_editor/advanced_loading_designs/code/gameviewmain.pas

raw
1 {
2 Copyright 2020-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 { Main view, where most of the application logic takes place. }
17 unit GameViewMain;
18
19 interface
20
21 uses Classes,
22 CastleComponentSerialize, CastleUIControls, CastleControls,
23 CastleKeysMouse, CastleNotifications, CastleViewport;
24
25 type
26 { Main view, where most of the application logic takes place. }
27 TViewMain = class(TCastleView)
28 published
29 { Components designed using CGE editor.
30 These fields will be automatically initialized at Start. }
31 LabelFps: TCastleLabel;
32 DesignedButton1: TCastleDesign;
33 DesignedButton1Button: TCastleButton;
34 DesignedButton2: TCastleDesign;
35 DesignedButton2Button: TCastleButton;
36 Notifications: TCastleNotifications;
37 ScrollViewTable: TCastleScrollView;
38 VerticalGroupTable: TCastleVerticalGroup;
39 ViewportForSoldiers: TCastleViewport;
40 private
41 procedure ClickDesignedButton1(Sender: TObject);
42 procedure ClickDesignedButton2(Sender: TObject);
43 public
44 constructor Create(AOwner: TComponent); override;
45 procedure Start; override;
46 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
47 end;
48
49 var
50 ViewMain: TViewMain;
51
52 implementation
53
54 uses SysUtils,
55 CastleVectors, CastleTransform;
56
57 { TViewMain ----------------------------------------------------------------- }
58
59 constructor TViewMain.Create(AOwner: TComponent);
60 begin
61 inherited;
62 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
63 end;
64
65 procedure TViewMain.Start;
66
67 procedure AddTableRow(const RowFactory: TCastleComponentFactory; const ProjectName, ProjectWebsite, ProjectInfo: String);
68 var
69 Row: TCastleUserInterface;
70 RowOwner: TComponent;
71 LabelProjectName: TCastleLabel;
72 LabelProjectWebsite: TCastleLabel;
73 EditProjectInfo: TCastleEdit;
74 begin
75 RowOwner := TComponent.Create(FreeAtStop);
76 Row := RowFactory.UserInterfaceLoad(RowOwner);
77 Row.FullSize := false;
78 Row.Width := 1000;
79 Row.Height := 50;
80
81 LabelProjectName := RowOwner.FindRequiredComponent('LabelProjectName') as TCastleLabel;
82 LabelProjectWebsite := RowOwner.FindRequiredComponent('LabelProjectWebsite') as TCastleLabel;
83 EditProjectInfo := RowOwner.FindRequiredComponent('EditProjectInfo') as TCastleEdit;
84
85 LabelProjectName.Caption := ProjectName;
86 LabelProjectWebsite.Caption := ProjectWebsite;
87 EditProjectInfo.Text := ProjectInfo;
88
89 VerticalGroupTable.InsertFront(Row);
90 end;
91
92 procedure AddSoldierWithCape(const Factory: TCastleComponentFactory; const Translation: TVector3);
93 var
94 SoldierInstance: TCastleTransform;
95 SoldierOwner: TComponent;
96 begin
97 SoldierOwner := TComponent.Create(FreeAtStop);
98 SoldierInstance := Factory.TransformLoad(SoldierOwner);
99 SoldierInstance.Translation := Translation;
100 ViewportForSoldiers.Items.Add(SoldierInstance);
101 end;
102
103 var
104 TableRowFactory: TCastleComponentFactory;
105 SoldierWithCapeFactory: TCastleComponentFactory;
106 begin
107 inherited;
108
109 { Find the components inside TCastleDesigns -- use TCastleDesign.DesignedComponent }
110 DesignedButton1Button := DesignedButton1.DesignedComponent('RootButton') as TCastleButton;
111 DesignedButton2Button := DesignedButton2.DesignedComponent('RootButton') as TCastleButton;
112
113 { Attach events }
114 DesignedButton1Button.OnClick := {$ifdef FPC}@{$endif} ClickDesignedButton1;
115 DesignedButton2Button.OnClick := {$ifdef FPC}@{$endif} ClickDesignedButton2;
116
117 { Load and instantiate table row UI many times }
118 TableRowFactory := TCastleComponentFactory.Create(nil);
119 try
120 TableRowFactory.Url := 'castle-data:/table_row.castle-user-interface';
121 AddTableRow(TableRowFactory, 'Castle Game Engine', 'https://castle-engine.io/', 'Game engine');
122 AddTableRow(TableRowFactory, 'Free Pascal Compiler', 'https://www.freepascal.org/', 'Pascal compiler');
123 AddTableRow(TableRowFactory, 'Lazarus', 'https://www.lazarus-ide.org/', 'IDE for FPC');
124 AddTableRow(TableRowFactory, 'Blender', 'https://www.blender.org/', '3D creation');
125 AddTableRow(TableRowFactory, 'OpenGL', 'https://www.khronos.org/opengl/', 'Rendering API');
126 AddTableRow(TableRowFactory, 'Debian', 'https://www.debian.org/', 'Debian GNU/Linux distro');
127 finally
128 FreeAndNil(TableRowFactory);
129 end;
130
131 { Load and instantiate soldier with cape many times }
132 SoldierWithCapeFactory := TCastleComponentFactory.Create(nil);
133 try
134 SoldierWithCapeFactory.Url := 'castle-data:/soldier_with_cape.castle-transform';
135 AddSoldierWithCape(SoldierWithCapeFactory, Vector3(0, 0, 0));
136 AddSoldierWithCape(SoldierWithCapeFactory, Vector3(1.5, 0, 0));
137 AddSoldierWithCape(SoldierWithCapeFactory, Vector3(3, 0, 0));
138 finally
139 FreeAndNil(SoldierWithCapeFactory);
140 end;
141 end;
142
143 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
144 begin
145 inherited;
146 { This virtual method is executed every frame (many times per second). }
147 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
148 end;
149
150 procedure TViewMain.ClickDesignedButton1(Sender: TObject);
151 begin
152 Notifications.Show('Clicked button 1');
153 end;
154
155 procedure TViewMain.ClickDesignedButton2(Sender: TObject);
156 begin
157 Notifications.Show('Clicked button 2');
158 end;
159
160 end.