Debian Codegraph @

examples/viewport_and_scenes/show_bounding_rect_in_2d/code/gameviewmain.pas

raw
1 {
2 Copyright 2018-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 CastleVectors, CastleComponentSerialize, X3DNodes,
23 CastleUIControls, CastleControls, CastleKeysMouse, CastleScene;
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 DragonScene: TCastleScene;
33 private
34 SceneBoundingRect: TCastleScene;
35 RectCoords: TCoordinateNode;
36 { Update contents of RectCoords. }
37 procedure UpdateRectangleCoordinates;
38 public
39 constructor Create(AOwner: TComponent); override;
40 procedure Start; override;
41 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
42 function Press(const Event: TInputPressRelease): Boolean; override;
43 end;
44
45 var
46 ViewMain: TViewMain;
47
48 implementation
49
50 uses SysUtils,
51 CastleColors, CastleRectangles;
52
53 { TViewMain ----------------------------------------------------------------- }
54
55 constructor TViewMain.Create(AOwner: TComponent);
56 begin
57 inherited;
58 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
59 end;
60
61 procedure TViewMain.Start;
62
63 { Build node showing a wireframe rectangle.
64
65 There are a lot of things you can customize here,
66 adjusting the look of the resulting rectangle.
67
68 Note: instead of TLineSetNode, one could also use TRectangle2DNode
69 to show a rectangle in 2D. You would need to set TRectangle2DNode.Size,
70 and place it within TTransformNode to position is correctly.
71 Below we show a little different, but also more flexible, approach
72 that uses TLineSetNode and TCoordinateNode. }
73 function CreateRectangleNode: TX3DRootNode;
74 var
75 Shape: TShapeNode;
76 LineSet: TLineSetNode;
77 LineProperties: TLinePropertiesNode;
78 Material: TMaterialNode;
79 Appearance: TAppearanceNode;
80 begin
81 RectCoords := TCoordinateNode.Create;
82 UpdateRectangleCoordinates;
83
84 LineSet := TLineSetNode.CreateWithShape(Shape);
85 LineSet.Coord := RectCoords;
86 LineSet.SetVertexCount([RectCoords.FdPoint.Count]);
87
88 Material := TMaterialNode.Create;
89 Material.EmissiveColor := YellowRGB;
90 //Material.Transparency := 0.8;
91
92 LineProperties := TLinePropertiesNode.Create;
93 LineProperties.LineWidthScaleFactor := 2;
94
95 Appearance := TAppearanceNode.Create;
96 Appearance.Material := Material;
97 Appearance.LineProperties := LineProperties;
98
99 Shape.Appearance := Appearance;
100
101 Result := TX3DRootNode.Create;
102 Result.AddChildren(Shape);
103 end;
104
105 begin
106 inherited;
107
108 SceneBoundingRect := TCastleScene.Create(FreeAtStop);
109 SceneBoundingRect.Load(CreateRectangleNode, true);
110
111 { Place SceneBoundingRect as a child of DragonScene,
112 this way transformation of DragonScene is automatically accounted for,
113 as in UpdateRectangleCoordinates we just use DragonScene.LocalBoundingBox,
114 not DragonScene.WorldBoundingBox.
115
116 OTOH it means we need to toggle SceneBoundingRect.Exists in
117 UpdateRectangleCoordinates, to make sure DragonScene.LocalBoundingBox
118 doesn't contain the visualization itself.
119
120 An alternative approach, placing SceneBoundingRect directly
121 in Viewport.Items, would also work, but then we'd have to look at
122 DragonScene.WorldBoundingBox. }
123 DragonScene.Add(SceneBoundingRect);
124 end;
125
126 procedure TViewMain.UpdateRectangleCoordinates;
127 var
128 BoundingRect: TFloatRectangle;
129 OldSceneBoundingRectExists: Boolean;
130 begin
131 OldSceneBoundingRectExists := SceneBoundingRect.Exists;
132 SceneBoundingRect.Exists := false;
133
134 BoundingRect := DragonScene.LocalBoundingBox.RectangleXY;
135
136 SceneBoundingRect.Exists := OldSceneBoundingRectExists;
137
138 RectCoords.SetPoint([
139 Vector3(BoundingRect.Left , BoundingRect.Bottom, 0),
140 Vector3(BoundingRect.Right, BoundingRect.Bottom, 0),
141 Vector3(BoundingRect.Right, BoundingRect.Top , 0),
142 Vector3(BoundingRect.Left , BoundingRect.Top , 0),
143 Vector3(BoundingRect.Left , BoundingRect.Bottom, 0)
144 ]);
145 end;
146
147 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
148 begin
149 inherited;
150 { This virtual method is executed every frame (many times per second). }
151 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
152
153 { Keep updating RectCoords, as bounding rectangle changes as the DragonScene animates }
154 UpdateRectangleCoordinates;
155 end;
156
157 function TViewMain.Press(const Event: TInputPressRelease): Boolean;
158 begin
159 Result := inherited;
160 if Result then Exit; // allow the ancestor to handle keys
161
162 if Event.IsKey(keyB) then
163 begin
164 SceneBoundingRect.Exists := not SceneBoundingRect.Exists;
165 Exit(true); // key was handled
166 end;
167 end;
168
169 end.