Debian Codegraph @

examples/component_gallery/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;
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 PageButton2: TCastleDesign;
33 private
34 const
35 PagesCount = 12;
36
37 { Suffixes of component names in the design,
38 to find ButtonXxx and PageXxx matching controls. }
39 PageNames: array [1..PagesCount] of String = (
40 'Intro',
41 'EmptyRectangle',
42 'ColorRectangle',
43 'Label',
44 'Image',
45 'Button',
46 'Button2',
47 'Checkbox',
48 'Edit',
49 'HorizontalVerticalGroup',
50 'ScrollView',
51 'Shape'
52 );
53
54 var
55 PageButtons: array [1..PagesCount] of TCastleButton;
56 Pages: array [1..PagesCount] of TCastleUserInterface;
57
58 procedure ClickPageButton(Sender: TObject);
59 procedure ClickToggle(Sender: TObject);
60 public
61 constructor Create(AOwner: TComponent); override;
62 procedure Start; override;
63 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
64 end;
65
66 var
67 ViewMain: TViewMain;
68
69 implementation
70
71 uses SysUtils,
72 CastleColors;
73
74 { TViewMain ----------------------------------------------------------------- }
75
76 constructor TViewMain.Create(AOwner: TComponent);
77 begin
78 inherited;
79 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
80 end;
81
82 procedure TViewMain.Start;
83 var
84 I: Integer;
85 ButtonToggle: TCastleButton;
86 begin
87 inherited;
88
89 { Find components, by name, that we need to access from code }
90 for I := 1 to PagesCount do
91 begin
92 PageButtons[I] := DesignedComponent('Button' + PageNames[I]) as TCastleButton;
93 PageButtons[I].Tag := I; // use button's tag to store page index
94 PageButtons[I].OnClick := {$ifdef FPC}@{$endif}ClickPageButton;
95 Pages[I] := DesignedComponent('Page' + PageNames[I]) as TCastleUserInterface;
96 end;
97
98 { Find components inside TCastleDesigns (use TCastleDesigns.DesignedComponent) }
99 ButtonToggle := PageButton2.DesignedComponent('ButtonToggle') as TCastleButton;
100
101 ButtonToggle.OnClick := {$ifdef FPC}@{$endif}ClickToggle;
102 end;
103
104 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
105 begin
106 inherited;
107 { This virtual method is executed every frame (many times per second). }
108 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
109 end;
110
111 procedure TViewMain.ClickPageButton(Sender: TObject);
112 var
113 I, CurrentPage: Integer;
114 PageButton: TCastleButton;
115 PageLabel: TCastleLabel;
116 begin
117 PageButton := Sender as TCastleButton;
118 CurrentPage := PageButton.Tag;
119 for I := 1 to PagesCount do
120 begin
121 PageButtons[I].Pressed := I = CurrentPage;
122 // adjust color of 1st label inside the button
123 if (PageButtons[I].ControlsCount >= 1) and
124 (PageButtons[I].Controls[0] is TCastleLabel) then
125 begin
126 PageLabel := PageButtons[I].Controls[0] as TCastleLabel;
127 if I = CurrentPage then
128 PageLabel.Color := White
129 else
130 PageLabel.Color := Black;
131 end;
132 Pages[I].Exists := I = CurrentPage;
133 end;
134 end;
135
136 procedure TViewMain.ClickToggle(Sender: TObject);
137 var
138 Button: TCastleButton;
139 begin
140 Button := Sender as TCastleButton;
141 Button.Pressed := not Button.Pressed;
142 end;
143
144 end.