| 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 |
{ Define custom cursor as TCustomCursor class. } |
| 17 |
unit GameCustomCursor; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses SysUtils, Classes, |
| 22 |
CastleUIControls, CastleKeysMouse, CastleVectors, CastleControls; |
| 23 |
|
| 24 |
type |
| 25 |
TCustomCursorStyle = (csImage, csAnimated); |
| 26 |
|
| 27 |
{ User interface that shows custom cursor, and automatically adjusts its own |
| 28 |
position (anchors) to Container.MousePosition. |
| 29 |
|
| 30 |
Assumes that the parent UI is a container or some FullSize UI control |
| 31 |
(like some TCastleView). |
| 32 |
The TCustomCursor must fill the entire container, to capture motion |
| 33 |
events from the entire container, and for UpdateCursorPosition |
| 34 |
to work correctly. } |
| 35 |
TCustomCursor = class(TCastleUserInterface) |
| 36 |
strict private |
| 37 |
{ To avoid clashes in component names between image_cursor.castle-user-interface |
| 38 |
and animated_cursor.castle-user-interface, it's best if each of them has |
| 39 |
a separate owner component. } |
| 40 |
FImageCursorOwner: TComponent; |
| 41 |
FAnimatedCursorOwner: TComponent; |
| 42 |
FImageCursorUi: TCastleUserInterface; |
| 43 |
FAnimatedCursorUi: TCastleUserInterface; |
| 44 |
FCursorUi: TCastleUserInterface; |
| 45 |
FCursorDebugDot: TCastleRectangleControl; |
| 46 |
FStyle: TCustomCursorStyle; |
| 47 |
procedure SetStyle(const Value: TCustomCursorStyle); |
| 48 |
procedure UpdateStyle; |
| 49 |
procedure UpdateCursorPosition(const V: TVector2); |
| 50 |
public |
| 51 |
constructor Create(AOwner: TComponent); override; |
| 52 |
property Style: TCustomCursorStyle read FStyle write SetStyle default csAnimated; |
| 53 |
function Motion(const Event: TInputMotion): Boolean; override; |
| 54 |
procedure InternalSetContainer(const Value: TCastleContainer); override; |
| 55 |
procedure VisibleChange(const Changes: TCastleUserInterfaceChanges; |
| 56 |
const ChangeInitiatedByChildren: boolean = false); override; |
| 57 |
end; |
| 58 |
|
| 59 |
var |
| 60 |
{ Single instance of TCustomCursor. } |
| 61 |
CustomCursor: TCustomCursor; |
| 62 |
|
| 63 |
implementation |
| 64 |
|
| 65 |
uses CastleColors; |
| 66 |
|
| 67 |
constructor TCustomCursor.Create(AOwner: TComponent); |
| 68 |
begin |
| 69 |
inherited; |
| 70 |
KeepInFront := true; |
| 71 |
FullSize := true; |
| 72 |
|
| 73 |
FImageCursorOwner := TComponent.Create(Self); |
| 74 |
FImageCursorUi := UserInterfaceLoad('castle-data:/image_cursor.castle-user-interface', FImageCursorOwner); |
| 75 |
{ Shift, to make cursor pivot (where the cursor is visually pointing) perfect. |
| 76 |
Note that this assumes that RootGroup in image_cursor.castle-user-interface has FullSize=false. } |
| 77 |
FImageCursorUi.Anchor(hpLeft, -10); |
| 78 |
FImageCursorUi.Anchor(vpBottom, -100); |
| 79 |
|
| 80 |
FAnimatedCursorOwner := TComponent.Create(Self); |
| 81 |
FAnimatedCursorUi := UserInterfaceLoad('castle-data:/animated_cursor.castle-user-interface', FAnimatedCursorOwner); |
| 82 |
{ Shift, to make cursor pivot (where the cursor is visually pointing) perfect. |
| 83 |
Note that this assumes that RootGroup in animated_cursor.castle-user-interface has FullSize=false. } |
| 84 |
FAnimatedCursorUi.Anchor(vpBottom, -100); |
| 85 |
|
| 86 |
{ FCursorUi will contain, as child, FImageCursorUi or FAnimatedCursorUi. |
| 87 |
This makes it easy to implement UpdateCursorPosition that just moves FCursorUi. } |
| 88 |
FCursorUi := TCastleUserInterface.Create(Self); |
| 89 |
InsertFront(FCursorUi); |
| 90 |
|
| 91 |
{ FCursorDebugDot is a debug UI. |
| 92 |
It shows you where is the (0,0) point of the FCursorUi, |
| 93 |
which corresponds to the Container.MousePosition. |
| 94 |
Looking at it, you should adjust shift of FAnimatedCursorUi and FImageCursorUi, |
| 95 |
such that their "visual pivot" (where they point at) is at the red dot. |
| 96 |
For actual game, you should hide this. } |
| 97 |
FCursorDebugDot := TCastleRectangleControl.Create(Self); |
| 98 |
//FCursorDebugDot.Exists := false; // hide this for the actual game |
| 99 |
FCursorDebugDot.Color := Red; |
| 100 |
FCursorDebugDot.Width := 1; |
| 101 |
FCursorDebugDot.Height := 1; |
| 102 |
//FCursorUi.InsertFront(FCursorDebugDot); this will be done in each UpdateStyle |
| 103 |
|
| 104 |
// initialize default Style |
| 105 |
FStyle := csAnimated; |
| 106 |
UpdateStyle; |
| 107 |
end; |
| 108 |
|
| 109 |
procedure TCustomCursor.SetStyle(const Value: TCustomCursorStyle); |
| 110 |
begin |
| 111 |
if FStyle <> Value then |
| 112 |
begin |
| 113 |
FStyle := Value; |
| 114 |
UpdateStyle; |
| 115 |
end; |
| 116 |
end; |
| 117 |
|
| 118 |
procedure TCustomCursor.UpdateStyle; |
| 119 |
begin |
| 120 |
FCursorUi.ClearControls; |
| 121 |
FCursorUi.InsertFront(FCursorDebugDot); |
| 122 |
case FStyle of |
| 123 |
csImage: FCursorUi.InsertFront(FImageCursorUi); |
| 124 |
csAnimated: FCursorUi.InsertFront(FAnimatedCursorUi); |
| 125 |
end; |
| 126 |
end; |
| 127 |
|
| 128 |
procedure TCustomCursor.VisibleChange(const Changes: TCastleUserInterfaceChanges; |
| 129 |
const ChangeInitiatedByChildren: boolean); |
| 130 |
begin |
| 131 |
inherited; |
| 132 |
// show cursor at proper place when you toggle Exists to true |
| 133 |
if (chExists in Changes) and (Container <> nil) then |
| 134 |
begin |
| 135 |
UpdateCursorPosition(Container.MousePosition); |
| 136 |
end; |
| 137 |
end; |
| 138 |
|
| 139 |
procedure TCustomCursor.InternalSetContainer(const Value: TCastleContainer); |
| 140 |
begin |
| 141 |
inherited; |
| 142 |
if Value <> nil then |
| 143 |
begin |
| 144 |
// show cursor at proper place before even it is moved |
| 145 |
UpdateCursorPosition(Value.MousePosition); |
| 146 |
end; |
| 147 |
end; |
| 148 |
|
| 149 |
function TCustomCursor.Motion(const Event: TInputMotion): Boolean; |
| 150 |
begin |
| 151 |
Result := inherited; |
| 152 |
if Result then Exit; |
| 153 |
|
| 154 |
UpdateCursorPosition(Event.Position); |
| 155 |
end; |
| 156 |
|
| 157 |
procedure TCustomCursor.UpdateCursorPosition(const V: TVector2); |
| 158 |
begin |
| 159 |
FCursorUi.Anchor(hpLeft, V.X / UIScale); |
| 160 |
FCursorUi.Anchor(vpBottom, V.Y / UIScale); |
| 161 |
end; |
| 162 |
|
| 163 |
end. |