| 1 |
{ |
| 2 |
Copyright 2018-2024 Benedikt Magnus, 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, CastleUIControls, CastleControls, |
| 23 |
CastleKeysMouse, CastleNotifications, CastleClientServer; |
| 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 |
EditHostname: TCastleEdit; |
| 33 |
EditPort: TCastleIntegerEdit; |
| 34 |
ButtonCreateClient: TCastleButton; |
| 35 |
ButtonDestroyClient: TCastleButton; |
| 36 |
EditSend: TCastleEdit; |
| 37 |
ButtonSend: TCastleButton; |
| 38 |
LabelMessageContents: TCastleLabel; |
| 39 |
Notifications: TCastleNotifications; |
| 40 |
private |
| 41 |
FClient: TCastleTCPClient; |
| 42 |
procedure HandleConnected; |
| 43 |
procedure HandleDisconnected; |
| 44 |
procedure HandleMessageReceived(const AMessage: String); |
| 45 |
procedure ClickCreateClient(Sender: TObject); |
| 46 |
procedure ClickDestroyClient(Sender: TObject); |
| 47 |
procedure ClickSend(Sender: TObject); |
| 48 |
public |
| 49 |
constructor Create(AOwner: TComponent); override; |
| 50 |
procedure Start; override; |
| 51 |
procedure Stop; override; |
| 52 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 53 |
end; |
| 54 |
|
| 55 |
var |
| 56 |
ViewMain: TViewMain; |
| 57 |
|
| 58 |
implementation |
| 59 |
|
| 60 |
uses SysUtils, |
| 61 |
CastleUtils; |
| 62 |
|
| 63 |
{ TViewMain ----------------------------------------------------------------- } |
| 64 |
|
| 65 |
constructor TViewMain.Create(AOwner: TComponent); |
| 66 |
begin |
| 67 |
inherited; |
| 68 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 69 |
end; |
| 70 |
|
| 71 |
procedure TViewMain.Start; |
| 72 |
begin |
| 73 |
inherited; |
| 74 |
ButtonCreateClient.OnClick := {$ifdef FPC}@{$endif} ClickCreateClient; |
| 75 |
ButtonDestroyClient.OnClick := {$ifdef FPC}@{$endif} ClickDestroyClient; |
| 76 |
ButtonSend.OnClick := {$ifdef FPC}@{$endif} ClickSend; |
| 77 |
end; |
| 78 |
|
| 79 |
procedure TViewMain.Stop; |
| 80 |
begin |
| 81 |
FreeAndNil(FClient); |
| 82 |
inherited; |
| 83 |
end; |
| 84 |
|
| 85 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 86 |
begin |
| 87 |
inherited; |
| 88 |
{ This virtual method is executed every frame (many times per second). } |
| 89 |
Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code'); |
| 90 |
LabelFps.Caption := |
| 91 |
'FPS: ' + Container.Fps.ToString + NL + |
| 92 |
'Client Created: ' + BoolToStr(FClient <> nil, true) + NL + |
| 93 |
'Connected To Server: ' + BoolToStr((FClient <> nil) and FClient.IsConnected, true); |
| 94 |
|
| 95 |
ButtonCreateClient.Enabled := FClient = nil; |
| 96 |
ButtonDestroyClient.Enabled := FClient <> nil; |
| 97 |
ButtonSend.Enabled := FClient <> nil; |
| 98 |
end; |
| 99 |
|
| 100 |
procedure TViewMain.HandleConnected; |
| 101 |
begin |
| 102 |
Notifications.Show('Connected to server'); |
| 103 |
end; |
| 104 |
|
| 105 |
procedure TViewMain.HandleDisconnected; |
| 106 |
begin |
| 107 |
Notifications.Show('Disconnected from server'); |
| 108 |
end; |
| 109 |
|
| 110 |
procedure TViewMain.HandleMessageReceived(const AMessage: String); |
| 111 |
begin |
| 112 |
Notifications.Show('Received message: ' + AMessage); |
| 113 |
LabelMessageContents.Caption := AMessage; |
| 114 |
end; |
| 115 |
|
| 116 |
procedure TViewMain.ClickCreateClient(Sender: TObject); |
| 117 |
begin |
| 118 |
FClient := TCastleTCPClient.Create; |
| 119 |
FClient.Hostname := EditHostname.Text; |
| 120 |
FClient.Port := EditPort.Value; |
| 121 |
|
| 122 |
FClient.OnConnected := {$ifdef FPC}@{$endif} HandleConnected; |
| 123 |
FClient.OnDisconnected := {$ifdef FPC}@{$endif} HandleDisconnected; |
| 124 |
FClient.OnMessageReceived := {$ifdef FPC}@{$endif} HandleMessageReceived; |
| 125 |
|
| 126 |
FClient.Connect; |
| 127 |
end; |
| 128 |
|
| 129 |
procedure TViewMain.ClickDestroyClient(Sender: TObject); |
| 130 |
begin |
| 131 |
if FClient <> nil then |
| 132 |
begin |
| 133 |
FClient.Disconnect; |
| 134 |
FreeAndNil(FClient); |
| 135 |
end; |
| 136 |
end; |
| 137 |
|
| 138 |
procedure TViewMain.ClickSend(Sender: TObject); |
| 139 |
begin |
| 140 |
FClient.Send(EditSend.Text); |
| 141 |
end; |
| 142 |
|
| 143 |
end. |