Debian Codegraph @

src/scene/castleviewport_design_navigation.inc

raw
1 {%MainUnit castleviewport.pas}
2 {
3 Copyright 2022-2023 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Design-time navigation descendants. }
18
19 {$ifdef read_interface}
20
21 var
22 { @exclude }
23 InternalDesignMouseLook: Boolean;
24
25 {$endif read_interface}
26
27 {$ifdef read_implementation}
28
29 { TCastleWalkNavigationDesign ------------------------------------------------ }
30
31 type
32 { Descendant of TCastleWalkNavigation configured to work nicely for design-time
33 navigation in walk/fly mode. }
34 TCastleWalkNavigationDesign = class(TCastleWalkNavigation)
35 strict private
36 LabelMessageContainer: TCastleRectangleControl;
37 LabelMessage: TCastleLabel;
38 MessageStart: TTimerResult;
39 public
40 constructor Create(AOwner: TComponent); override;
41 function Press(const Event: TInputPressRelease): Boolean; override;
42 function Release(const Event: TInputPressRelease): Boolean; override;
43 procedure Update(const SecondsPassed: Single; var HandleInput: boolean); override;
44 published
45 property Input default [niNormal, ni3dMouse];
46 property Gravity default false;
47 property MoveSpeed {$ifdef FPC}default 10{$endif};
48 property MinAngleFromGravityUp {$ifdef FPC}default 0{$endif};
49 property CheckCollisions default false;
50
51 property MouseLook stored false;
52 property ZoomEnabled stored false;
53 { Also adjusted by MouseLook, and we also don't want to persist this. }
54 property Cursor stored false;
55 end;
56
57 constructor TCastleWalkNavigationDesign.Create(AOwner: TComponent);
58 begin
59 inherited;
60 Input := [niNormal, ni3dMouse]; // no niMouseDragging, no niGesture
61 Gravity := false;
62 MoveSpeed := 10; // faster default
63 MinAngleFromGravityUp := 0; // allow to rotate smoothly from Top (7) view
64 CheckCollisions := false;
65 ZoomEnabled := true;
66
67 Input_Jump.Assign(keyE);
68 Input_Crouch.Assign(keyQ);
69
70 LabelMessageContainer := TCastleRectangleControl.Create(Self);
71 LabelMessageContainer.Color := Vector4(0, 0, 0, 0.25);
72 LabelMessageContainer.AutoSizeToChildren := true;
73 LabelMessageContainer.Anchor(hpMiddle);
74 LabelMessageContainer.Anchor(vpMiddle);
75 LabelMessageContainer.SetTransient;
76 LabelMessageContainer.Exists := false;
77 InsertFront(LabelMessageContainer);
78
79 LabelMessage := TCastleLabel.Create(Self);
80 // LabelMessage.Outline := 1;
81 LabelMessage.Color := White;
82 LabelMessage.Anchor(hpMiddle);
83 LabelMessage.Anchor(vpMiddle);
84 LabelMessage.FontSize := 50;
85 LabelMessage.Padding := 20;
86 LabelMessageContainer.InsertFront(LabelMessage);
87
88 ForceFallbackLook(LabelMessageContainer);
89 end;
90
91 function TCastleWalkNavigationDesign.Press(const Event: TInputPressRelease): Boolean;
92 begin
93 { Display current speed if user presses key/mouse wheel to change speed
94 (even if speed doesn't actually change because of MoveSpeedMin/Max limit) }
95 if Input_MoveSpeedInc.IsEvent(Event) or
96 Input_MoveSpeedDec.IsEvent(Event) then
97 begin
98 MessageStart := Timer;
99 end;
100
101 Result := inherited;
102 if Result then Exit;
103
104 if Event.IsMouseButton(buttonRight) then
105 begin
106 MouseLook := true;
107 ZoomEnabled := false;
108 // use mouse wheel, as alterntive to +/-, to increase/decrease movement speed
109 Input_MoveSpeedInc.Assign(keyNumpadPlus , keyNone, '+', false, buttonLeft, mwUp);
110 Input_MoveSpeedDec.Assign(keyNumpadMinus, keyNone, '-', false, buttonLeft, mwDown);
111 InternalDesignMouseLook := true;
112 Exit(true);
113 end;
114
115 {
116 TODO:
117 - still wondering whether toggling this on G is nice for users
118 (maybe better menu item with shortcut like Ctrl+N in editor to change design-time navigation
119 would be better)
120 - doesn't seem to work ok, user never falls down.
121 maybe lack of OnInternalMoveAllowed is a problem?
122
123 if Event.IsKey(keyG) then
124 begin
125 Gravity := not Gravity;
126 Exit(true);
127 end;
128 }
129 end;
130
131 function TCastleWalkNavigationDesign.Release(const Event: TInputPressRelease): Boolean;
132 begin
133 Result := inherited;
134 if Result then Exit;
135
136 if Event.IsMouseButton(buttonRight) then
137 begin
138 MouseLook := false;
139 ZoomEnabled := true;
140 Input_MoveSpeedInc.MakeClear;
141 Input_MoveSpeedDec.MakeClear;
142 InternalDesignMouseLook := false;
143 Exit(true);
144 end;
145 end;
146
147 procedure TCastleWalkNavigationDesign.Update(const SecondsPassed: Single;
148 var HandleInput: boolean);
149 var
150 T: TFloatTime;
151 const
152 TimeToExist = 1.0;
153 TimeToFade = 0.75;
154
155 function AlphaAnimate(const MaxAlpha: Single): Single;
156 begin
157 if T < TimeToExist - TimeToFade then
158 Result := MaxAlpha
159 else
160 Result := MapRange(T, TimeToExist - TimeToFade, TimeToExist, MaxAlpha, 0.0);
161 end;
162
163 const
164 MessageAlpha = 1.0;
165 ContainerAlpha = 0.25;
166 begin
167 inherited;
168
169 { Display current speed if user presses key/mouse wheel to change speed
170 (even if speed doesn't actually change because of MoveSpeedMin/Max limit) }
171 if Input_MoveSpeedInc.IsPressed(Container) or
172 Input_MoveSpeedDec.IsPressed(Container) then
173 begin
174 MessageStart := Timer;
175 end;
176
177 if MessageStart.Initialized then
178 begin
179 T := MessageStart.ElapsedTime;
180 LabelMessageContainer.Exists := T < TimeToExist;
181 if LabelMessageContainer.Exists then
182 begin
183 LabelMessage.Caption := FormatDot('Speed %f', [MoveSpeed]);
184 LabelMessageContainer.Color :=
185 ColorOpacity(LabelMessageContainer.Color, AlphaAnimate(ContainerAlpha));
186 LabelMessage.Color :=
187 ColorOpacity(LabelMessage.Color, AlphaAnimate(MessageAlpha));
188 // LabelMessage.OutlineColor :=
189 // ColorOpacity(LabelMessage.OutlineColor, AlphaAnimate(MessageAlpha));
190 end;
191 end;
192 end;
193
194 { TCastleExamineNavigationDesign ------------------------------------------------ }
195
196 type
197 TCastleExamineNavigationDesign = class(TCastleExamineNavigation)
198 public
199 constructor Create(AOwner: TComponent); override;
200 published
201 property RotationAccelerate default false;
202 property CheckCollisions default false;
203 property Cursor stored false;
204 end;
205
206 constructor TCastleExamineNavigationDesign.Create(AOwner: TComponent);
207 begin
208 inherited;
209 RotationAccelerate := false;
210 CheckCollisions := false;
211
212 { rotate with right mouse button, no modifiers }
213 Input_Rotate.MouseButton := buttonRight;
214 Input_Rotate.MouseButtonCheckModifiers := [mkShift, mkCtrl];
215 Input_Rotate.MouseButtonModifiers := [];
216 Input_Rotate.MouseButton2Use := false;
217 { move with right mouse button, Shift }
218 Input_Move.MouseButton := buttonRight;
219 Input_Move.MouseButtonCheckModifiers := [mkShift, mkCtrl];
220 Input_Move.MouseButtonModifiers := [mkShift];
221 Input_Move.MouseButton2Use := false;
222 { no mouse dragging for zoom (but still you can do zoom with mouse wheel) }
223 Input_Zoom.MakeClear;
224 end;
225
226 { TCastle2DNavigationDesign ------------------------------------------------ }
227
228 type
229 TCastle2DNavigationDesign = class(TCastle2DNavigation)
230 public
231 constructor Create(AOwner: TComponent); override;
232 published
233 property MouseButtonMove default buttonRight;
234
235 property Cursor stored false;
236 end;
237
238 constructor TCastle2DNavigationDesign.Create(AOwner: TComponent);
239 begin
240 inherited;
241 { move with right mouse button, no modifiers }
242 Input_Move.MouseButton := buttonRight;
243 Input_Move.MouseButtonCheckModifiers := [mkShift, mkCtrl];
244 Input_Move.MouseButtonModifiers := [];
245 Input_Move.MouseButton2Use := false;
246 { no mouse dragging for zoom (but still you can do zoom with mouse wheel) }
247 Input_Zoom.MakeClear;
248 end;
249
250 {$endif read_implementation}