| 1 |
{ |
| 2 |
Copyright 2019-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 user interface class. |
| 17 |
This implements the majority of this application functionality. } |
| 18 |
unit GameViewMain; |
| 19 |
|
| 20 |
interface |
| 21 |
|
| 22 |
uses CastleScene, CastleControls, |
| 23 |
CastleKeysMouse, CastleColors, CastleUIControls; |
| 24 |
|
| 25 |
type |
| 26 |
{ Main user interface class. |
| 27 |
This implements the majority of this application functionality. } |
| 28 |
TViewMain = class(TCastleView) |
| 29 |
private |
| 30 |
Buttons: TCastleVerticalGroup; |
| 31 |
ButtonDragSimple: TCastleButton; |
| 32 |
ButtonDragMouseLook: TCastleButton; |
| 33 |
Dragging, MouseLook: Boolean; |
| 34 |
DraggedRect: TCastleRectangleControl; |
| 35 |
Status: TCastleLabel; |
| 36 |
procedure ClickDragSimple(Sender: TObject); |
| 37 |
procedure ClickDragMouseLook(Sender: TObject); |
| 38 |
procedure RefreshButtonsPressed; |
| 39 |
public |
| 40 |
procedure Start; override; |
| 41 |
procedure Update(const SecondsPassed: Single; var HandleInput: boolean); override; |
| 42 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 43 |
function Release(const Event: TInputPressRelease): Boolean; override; |
| 44 |
function Motion(const Event: TInputMotion): Boolean; override; |
| 45 |
end; |
| 46 |
|
| 47 |
var |
| 48 |
ViewMain: TViewMain; |
| 49 |
|
| 50 |
implementation |
| 51 |
|
| 52 |
uses SysUtils, CastleVectors; |
| 53 |
|
| 54 |
{ TViewMain ----------------------------------------------------------------- } |
| 55 |
|
| 56 |
procedure TViewMain.Start; |
| 57 |
begin |
| 58 |
inherited; |
| 59 |
|
| 60 |
DraggedRect := TCastleRectangleControl.Create(FreeAtStop); |
| 61 |
DraggedRect.Color := Yellow; |
| 62 |
DraggedRect.Anchor(vpMiddle); |
| 63 |
DraggedRect.Anchor(hpMiddle); |
| 64 |
InsertFront(DraggedRect); |
| 65 |
|
| 66 |
Status := TCastleLabel.Create(FreeAtStop); |
| 67 |
Status.Color := Yellow; |
| 68 |
Status.Anchor(vpTop, -10); |
| 69 |
Status.Anchor(hpLeft, 10); |
| 70 |
InsertFront(Status); |
| 71 |
|
| 72 |
Buttons := TCastleVerticalGroup.Create(FreeAtStop); |
| 73 |
Buttons.Anchor(vpBottom); |
| 74 |
Buttons.Anchor(hpLeft); |
| 75 |
Buttons.Padding := 10; |
| 76 |
Buttons.Spacing := 10; |
| 77 |
InsertFront(Buttons); |
| 78 |
|
| 79 |
ButtonDragSimple := TCastleButton.Create(FreeAtStop); |
| 80 |
ButtonDragSimple.Caption := 'Drag by tracking Motion events'; |
| 81 |
ButtonDragSimple.OnClick := {$ifdef FPC}@{$endif}ClickDragSimple; |
| 82 |
ButtonDragSimple.Toggle := true; |
| 83 |
Buttons.InsertFront(ButtonDragSimple); |
| 84 |
|
| 85 |
ButtonDragMouseLook := TCastleButton.Create(FreeAtStop); |
| 86 |
ButtonDragMouseLook.Caption := 'Drag by MouseLook logic (hide mouse, pretend drag area is unbounded)'; |
| 87 |
ButtonDragMouseLook.OnClick := {$ifdef FPC}@{$endif}ClickDragMouseLook; |
| 88 |
ButtonDragMouseLook.Toggle := true; |
| 89 |
Buttons.InsertFront(ButtonDragMouseLook); |
| 90 |
|
| 91 |
MouseLook := false; |
| 92 |
RefreshButtonsPressed; |
| 93 |
end; |
| 94 |
|
| 95 |
procedure TViewMain.RefreshButtonsPressed; |
| 96 |
begin |
| 97 |
ButtonDragSimple.Pressed := not MouseLook; |
| 98 |
ButtonDragMouseLook.Pressed := MouseLook; |
| 99 |
end; |
| 100 |
|
| 101 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: boolean); |
| 102 |
begin |
| 103 |
inherited; |
| 104 |
if Dragging and MouseLook then |
| 105 |
Container.MouseLookUpdate; |
| 106 |
end; |
| 107 |
|
| 108 |
function TViewMain.Press(const Event: TInputPressRelease): Boolean; |
| 109 |
begin |
| 110 |
Result := inherited; |
| 111 |
if Result then Exit; |
| 112 |
|
| 113 |
if Event.IsMouseButton(buttonLeft) then |
| 114 |
begin |
| 115 |
Dragging := true; |
| 116 |
if MouseLook then |
| 117 |
begin |
| 118 |
Cursor := mcForceNone; |
| 119 |
Container.MouseLookPress; |
| 120 |
end; |
| 121 |
Exit(true); |
| 122 |
end; |
| 123 |
end; |
| 124 |
|
| 125 |
function TViewMain.Release(const Event: TInputPressRelease): Boolean; |
| 126 |
begin |
| 127 |
Result := inherited; |
| 128 |
if Result then Exit; |
| 129 |
|
| 130 |
if Event.IsMouseButton(buttonLeft) then |
| 131 |
begin |
| 132 |
Dragging := false; |
| 133 |
if MouseLook then |
| 134 |
Cursor := mcDefault; |
| 135 |
Exit(true); |
| 136 |
end; |
| 137 |
end; |
| 138 |
|
| 139 |
function TViewMain.Motion(const Event: TInputMotion): Boolean; |
| 140 |
var |
| 141 |
Delta: TVector2; |
| 142 |
begin |
| 143 |
Result := inherited; |
| 144 |
if Result then Exit; |
| 145 |
|
| 146 |
if Dragging then |
| 147 |
begin |
| 148 |
if MouseLook then |
| 149 |
Delta := Container.MouseLookDelta(Event, RenderRect) |
| 150 |
else |
| 151 |
Delta := Event.Position - Event.OldPosition; |
| 152 |
DraggedRect.Translation := DraggedRect.Translation + Delta / UIScale; |
| 153 |
Status.Caption := Format('Dragged rect position: %s', [DraggedRect.Translation.ToString]); |
| 154 |
Exit(true); |
| 155 |
end; |
| 156 |
end; |
| 157 |
|
| 158 |
procedure TViewMain.ClickDragSimple(Sender: TObject); |
| 159 |
begin |
| 160 |
MouseLook := false; |
| 161 |
RefreshButtonsPressed; |
| 162 |
end; |
| 163 |
|
| 164 |
procedure TViewMain.ClickDragMouseLook(Sender: TObject); |
| 165 |
begin |
| 166 |
MouseLook := true; |
| 167 |
RefreshButtonsPressed; |
| 168 |
end; |
| 169 |
|
| 170 |
end. |