| 1 |
{ |
| 2 |
Copyright 2022-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 |
{ Form unit. } |
| 17 |
unit Unit1; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses |
| 22 |
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, |
| 23 |
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, |
| 24 |
Vcl.CastleControl, |
| 25 |
CastleGLVersion, CastleGLUtils, CastleControls; |
| 26 |
|
| 27 |
type |
| 28 |
TForm1 = class(TForm) |
| 29 |
Memo1: TMemo; |
| 30 |
Button3D: TButton; |
| 31 |
Button2D: TButton; |
| 32 |
ButtonUI: TButton; |
| 33 |
LabelFps: TLabel; |
| 34 |
Timer1: TTimer; |
| 35 |
CastleControl: TCastleControl; |
| 36 |
procedure FormCreate(Sender: TObject); |
| 37 |
procedure Button3DClick(Sender: TObject); |
| 38 |
procedure Button2DClick(Sender: TObject); |
| 39 |
procedure ButtonUIClick(Sender: TObject); |
| 40 |
procedure Timer1Timer(Sender: TObject); |
| 41 |
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); |
| 42 |
procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); |
| 43 |
private |
| 44 |
public |
| 45 |
{ Public declarations } |
| 46 |
end; |
| 47 |
|
| 48 |
var |
| 49 |
Form1: TForm1; |
| 50 |
|
| 51 |
implementation |
| 52 |
|
| 53 |
{$R *.dfm} |
| 54 |
|
| 55 |
uses CastleRenderOptions, CastleRectangles, CastleColors, CastleRenderContext, |
| 56 |
CastleApplicationProperties, CastleVectors, CastleUIControls, CastleKeysMouse, |
| 57 |
CastleCameras, CastleLog; |
| 58 |
|
| 59 |
{ TUiTest --------------------------------------------------------------------- } |
| 60 |
|
| 61 |
type |
| 62 |
{ Define a TCastleUserInterface descendant using code, |
| 63 |
to showcase various features from Pascal: |
| 64 |
|
| 65 |
- observe when GL context is available to do something (here: get OpenGL info) |
| 66 |
|
| 67 |
- do direct rendering by overriding TUiTest.Render |
| 68 |
(you can even use OpenGL(ES) directly there) |
| 69 |
|
| 70 |
- handle keys and mouse presses using CGE API. |
| 71 |
} |
| 72 |
TUiTest = class(TCastleUserInterface) |
| 73 |
constructor Create(Owner: TComponent); override; |
| 74 |
procedure Render; override; |
| 75 |
procedure GLContextOpen; override; |
| 76 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 77 |
end; |
| 78 |
|
| 79 |
constructor TUiTest.Create(Owner: TComponent); |
| 80 |
begin |
| 81 |
inherited; |
| 82 |
// keep in front, to not be obscured by designs we load using CastleControl.Container.DesignUrl |
| 83 |
KeepInFront := true; |
| 84 |
end; |
| 85 |
|
| 86 |
procedure TUiTest.GLContextOpen; |
| 87 |
begin |
| 88 |
inherited; |
| 89 |
Form1.Memo1.Lines.Add(GLInformationString); |
| 90 |
end; |
| 91 |
|
| 92 |
procedure TUiTest.Render; |
| 93 |
begin |
| 94 |
inherited; |
| 95 |
DrawRectangle(FloatRectangle(5, 5, 10, 10), Blue); |
| 96 |
FallbackFont.Print(30, 5, Yellow, FormatDateTime('yyyy-mm-dd, hh:nn:ss', Now)); |
| 97 |
end; |
| 98 |
|
| 99 |
function TUiTest.Press(const Event: TInputPressRelease): Boolean; |
| 100 |
var |
| 101 |
WalkNav: TCastleWalkNavigation; |
| 102 |
begin |
| 103 |
Result := inherited; |
| 104 |
|
| 105 |
{ See https://castle-engine.io/log where to find log output, |
| 106 |
it is also in Delphi Events window. } |
| 107 |
WritelnLog('Press received ' + Event.ToString); |
| 108 |
|
| 109 |
{ To test it, change design to remove TCastleExamineNavigation and add |
| 110 |
WalkNavigation1: TCastleWalkNavigation. } |
| 111 |
if Event.IsMouseButton(buttonRight) then |
| 112 |
begin |
| 113 |
WalkNav := Form1.CastleControl.Container.DesignedComponent( |
| 114 |
'WalkNavigation1', false) as TCastleWalkNavigation; |
| 115 |
if WalkNav <> nil then |
| 116 |
begin |
| 117 |
WalkNav.MouseLook := not WalkNav.MouseLook; |
| 118 |
Exit(true); |
| 119 |
end; |
| 120 |
end; |
| 121 |
end; |
| 122 |
|
| 123 |
{ TForm1 --------------------------------------------------------------------- } |
| 124 |
|
| 125 |
procedure TForm1.Button2DClick(Sender: TObject); |
| 126 |
begin |
| 127 |
CastleControl.Container.DesignUrl := 'castle-data:/test_2d.castle-user-interface'; |
| 128 |
end; |
| 129 |
|
| 130 |
procedure TForm1.Button3DClick(Sender: TObject); |
| 131 |
begin |
| 132 |
CastleControl.Container.DesignUrl := 'castle-data:/test_3d.castle-user-interface'; |
| 133 |
end; |
| 134 |
|
| 135 |
procedure TForm1.ButtonUIClick(Sender: TObject); |
| 136 |
begin |
| 137 |
CastleControl.Container.DesignUrl := 'castle-data:/test_ui.castle-user-interface'; |
| 138 |
end; |
| 139 |
|
| 140 |
procedure TForm1.FormCreate(Sender: TObject); |
| 141 |
var |
| 142 |
UiTest: TUiTest; |
| 143 |
begin |
| 144 |
// Call this to have UI scaling, same as in editor |
| 145 |
CastleControl.Container.LoadSettings('castle-data:/CastleSettings.xml'); |
| 146 |
|
| 147 |
InitializeLog; |
| 148 |
|
| 149 |
UiTest := TUiTest.Create(Self); |
| 150 |
UiTest.FullSize := true; |
| 151 |
CastleControl.Container.Controls.InsertFront(UiTest); |
| 152 |
end; |
| 153 |
|
| 154 |
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; |
| 155 |
Shift: TShiftState); |
| 156 |
begin |
| 157 |
{ If you want to handle arrow keys in CastleControl, |
| 158 |
you have to set KeyPreview=true on form, |
| 159 |
and redirect them here to the CastleControl. |
| 160 |
Otherwise arrow keys are used for navigation by Windows, |
| 161 |
and the CastleControl will never even see them. |
| 162 |
|
| 163 |
In this demo, you can test arrow keys and space by using |
| 164 |
them to rotate in 3D view. |
| 165 |
You can also see key presses received by CGE in Delphi Events window. |
| 166 |
} |
| 167 |
CastleControl.PreviewFormKeyDown(Key, Shift); |
| 168 |
end; |
| 169 |
|
| 170 |
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); |
| 171 |
begin |
| 172 |
CastleControl.PreviewFormKeyUp(Key, Shift); |
| 173 |
end; |
| 174 |
|
| 175 |
procedure TForm1.Timer1Timer(Sender: TObject); |
| 176 |
begin |
| 177 |
LabelFps.Caption := 'FPS: ' + CastleControl.Container.Fps.ToString; |
| 178 |
end; |
| 179 |
|
| 180 |
end. |