| 1 |
{%MainUnit castlecontrols.pas} |
| 2 |
{ |
| 3 |
Copyright 2010-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 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
{ Abstract user interface with a scrollbar. |
| 20 |
In most cases you want to use @link(TCastleScrollView) in your application, |
| 21 |
which is a more comfortable descendant of this class. } |
| 22 |
TCastleScrollViewCustom = class(TCastleUserInterface) |
| 23 |
strict private |
| 24 |
FScrollbarFrame: TCastleImagePersistent; |
| 25 |
FScrollbarSlider: TCastleImagePersistent; |
| 26 |
FScrollbarVisible: Boolean; |
| 27 |
FScrollbarVerticalMargin: Single; |
| 28 |
{ Rects in screen coordinates (like RenderRect), |
| 29 |
after UI scaling and anchors. } |
| 30 |
ScrollbarFrameRect, ScrollbarSliderRect: TFloatRectangle; |
| 31 |
ScrollBarDragging: Boolean; |
| 32 |
FKeyScrollSpeed, FWheelScrollSpeed: Single; |
| 33 |
FScrollBarWidth: Single; |
| 34 |
FEnableDragging: Boolean; |
| 35 |
DragSinceLastUpdate, DragSpeed, TimeSinceDraggingStopped, TimeSinceDraggingStarted: Double; |
| 36 |
ScrollbarActive: Single; |
| 37 |
FTintScrollBarInactive: TCastleColor; |
| 38 |
{ Stop any movement due to inertial force. } |
| 39 |
procedure ResetInertialForce; |
| 40 |
procedure SetScrollbarVerticalMargin(const AValue: Single); |
| 41 |
protected |
| 42 |
FScrollbarLeft: Boolean; |
| 43 |
|
| 44 |
procedure DoScrollChange; virtual; |
| 45 |
|
| 46 |
procedure CustomScrollBarChanged(Sender: TObject); |
| 47 |
|
| 48 |
property ScrollbarVisible: Boolean read FScrollbarVisible; |
| 49 |
|
| 50 |
function GetScroll: Single; virtual; abstract; |
| 51 |
procedure SetScroll(Value: Single); virtual; abstract; |
| 52 |
function GetScrollStored: Boolean; virtual; abstract; |
| 53 |
|
| 54 |
{ How much @link(Scroll) is represented by the height of the scrollbar slider. |
| 55 |
Among other things, this determines how much @link(Scroll) changes |
| 56 |
in response to Page Up and Page Down keys. } |
| 57 |
function GetPageSize: Single; virtual; abstract; |
| 58 |
|
| 59 |
{ How much @link(Scroll) is represented by the height of the entire scrollbar |
| 60 |
(that is, the height of this @link(TCastleScrollViewCustom) instance). } |
| 61 |
function GetCompleteSize: Single; virtual; abstract; |
| 62 |
public |
| 63 |
const |
| 64 |
DefaultKeyScrollSpeed = 200.0; |
| 65 |
DefaultWheelScrollSpeed = 20.0; |
| 66 |
|
| 67 |
constructor Create(AOwner: TComponent); override; |
| 68 |
destructor Destroy; override; |
| 69 |
procedure RenderOverChildren; override; |
| 70 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 71 |
function Press(const Event: TInputPressRelease): boolean; override; |
| 72 |
function Release(const Event: TInputPressRelease): boolean; override; |
| 73 |
function PreviewPress(const Event: TInputPressRelease): boolean; override; |
| 74 |
function Motion(const Event: TInputMotion): boolean; override; |
| 75 |
procedure Update(const SecondsPassed: Single; |
| 76 |
var HandleInput: boolean); override; |
| 77 |
|
| 78 |
{ Color and alpha tint to use when scrollbar is not used. |
| 79 |
May have some alpha, which makes scrollbar "make itself more opaque", |
| 80 |
and thus noticeable, when you start dragging. |
| 81 |
By default it's opaque white, which means that no tint is shown. } |
| 82 |
property TintScrollBarInactive: TCastleColor read FTintScrollBarInactive write FTintScrollBarInactive; |
| 83 |
|
| 84 |
{ Scroll bar width, either from @link(ScrollBarWidth) or |
| 85 |
@link(TCastleTheme.ScrollBarWidth Theme.ScrollBarWidth). } |
| 86 |
function EffectiveScrollBarWidth: Single; |
| 87 |
|
| 88 |
{ Minimum sensible value for @link(Scroll), corresponds to scrollbar slider at the top. } |
| 89 |
function ScrollMin: Single; |
| 90 |
|
| 91 |
{ Maximum sensible value for @link(Scroll), corresponds to scrollbar slider at the bottom. } |
| 92 |
function ScrollMax: Single; |
| 93 |
published |
| 94 |
{ Speed of scrolling by arrow keys, in pixels (before UI scaling) per second. } |
| 95 |
property KeyScrollSpeed: Single read FKeyScrollSpeed write FKeyScrollSpeed {$ifdef FPC}default DefaultKeyScrollSpeed{$endif}; |
| 96 |
|
| 97 |
{ Speed of scrolling by mouse wheel, in pixels (before UI scaling) per event. } |
| 98 |
property WheelScrollSpeed: Single read FWheelScrollSpeed write FWheelScrollSpeed {$ifdef FPC}default DefaultWheelScrollSpeed{$endif}; |
| 99 |
|
| 100 |
{ Width of the scroll bar. |
| 101 |
Value of 0 means to use global default |
| 102 |
in @link(TCastleTheme.ScrollBarWidth Theme.ScrollBarWidth). } |
| 103 |
property ScrollBarWidth: Single read FScrollBarWidth write FScrollBarWidth {$ifdef FPC}default 0{$endif}; |
| 104 |
|
| 105 |
{ Position of the scroll bar. |
| 106 |
@definitionList( |
| 107 |
@itemLabel(@false) |
| 108 |
@item(Scroll bar is located on the right side of the ScrollView.) |
| 109 |
@itemLabel(@true) |
| 110 |
@item(Scroll bar is located on the left side of the ScrollView. |
| 111 |
Pay attention, that additional space is not reserved for the scroll bar automatically, |
| 112 |
you have to specify ScrollArea.Translation.X manually, |
| 113 |
e.g. @code(ScrollArea.Translation := Vector2(ScrollBarWidth + Gap, ScrollArea.Translation.Y)). |
| 114 |
) |
| 115 |
) } |
| 116 |
property ScrollBarLeft: Boolean read FScrollBarLeft write FScrollBarLeft default false; |
| 117 |
|
| 118 |
{ Enable scrolling by dragging @italic(anywhere) in the scroll area. |
| 119 |
This is usually suitable for mobile devices. |
| 120 |
Note that this doesn't affect the dragging directly by the scrollbar, |
| 121 |
which is always enabled. } |
| 122 |
property EnableDragging: Boolean read FEnableDragging write FEnableDragging default true; |
| 123 |
|
| 124 |
{ How much do we scroll. |
| 125 |
Setting it always clamps the value to a sensible range, |
| 126 |
between @link(ScrollMin) and @link(ScrollMax). |
| 127 |
|
| 128 |
In case of @link(TCastleScrollView), |
| 129 |
setting this moves the @link(TCastleScrollView.ScrollArea) vertical anchor, |
| 130 |
effectively moving the scrolled children. |
| 131 |
As such, it is expressed in unscaled pixels |
| 132 |
(just like @link(TCastleUserInterface.Translation), |
| 133 |
@link(TCastleUserInterface.Height), |
| 134 |
@link(TCastleUserInterface.EffectiveHeight)...). |
| 135 |
And it is always synchronized with @code(ScrollArea.Translation.Y). |
| 136 |
|
| 137 |
In case of @link(TCastleScrollViewManual), |
| 138 |
it can correspond to anything you want. |
| 139 |
You handle @link(TCastleScrollViewManual.OnChange) yourself, |
| 140 |
and interpret this value in whatever way you want. } |
| 141 |
property Scroll: Single read GetScroll write SetScroll stored GetScrollStored |
| 142 |
{$ifdef FPC}default 0{$endif}; |
| 143 |
|
| 144 |
{ Customize the image of scrollbar frame, drawn on the right side of this control. } |
| 145 |
property ScrollbarFrame: TCastleImagePersistent read FScrollbarFrame; |
| 146 |
|
| 147 |
{ Customize the image of scrollbar slider, drawn within ScrollbarFrame. } |
| 148 |
property ScrollbarSlider: TCastleImagePersistent read FScrollbarSlider; |
| 149 |
|
| 150 |
{ Vertical space between the scrollbar and borders of the scrollview. |
| 151 |
Can be used e.g. to draw up/down buttons by adding them to the @link(TCastleScrollViewCustom). |
| 152 |
|
| 153 |
TODO: We should add automatic PageUpDown buttons activated by property |
| 154 |
ButtonsUpDown: Boolean, with gfx customizable by ButtonUp, ButtonDown. |
| 155 |
Then the property ScrollbarVerticalMargin should be deprecated. } |
| 156 |
property ScrollbarVerticalMargin: Single |
| 157 |
read FScrollbarVerticalMargin write SetScrollbarVerticalMargin {$ifdef FPC}default 0{$endif}; |
| 158 |
|
| 159 |
{$define read_interface_class} |
| 160 |
{$I auto_generated_persistent_vectors/tcastlescrollviewcustom_persistent_vectors.inc} |
| 161 |
{$undef read_interface_class} |
| 162 |
end; |
| 163 |
|
| 164 |
{ Class that represents scrollable area inside @link(TCastleScrollView.ScrollArea). |
| 165 |
Never create instances of this class directly -- it should only be created by TCastleScrollView. } |
| 166 |
TCastleScrollArea = class(TCastleUserInterface) |
| 167 |
public |
| 168 |
constructor Create(AOwner: TComponent); override; |
| 169 |
published |
| 170 |
property AutoSizeToChildren default true; |
| 171 |
{ Do not store VerticalAnchorXxx properties, they should be only controlled |
| 172 |
using @link(TCastleScrollViewCustom.Scroll). |
| 173 |
Note: we cannot prevent storing (duplicated in this case) |
| 174 |
Translation.Y value. } |
| 175 |
property VerticalAnchorSelf stored false; |
| 176 |
property VerticalAnchorParent stored false; |
| 177 |
end; |
| 178 |
|
| 179 |
{ Container for a user interface children that can be scrolled vertically. |
| 180 |
Add the children (that should be scrolled) to the @link(ScrollArea). |
| 181 |
We automatically show a scrollbar, and handle various scrolling inputs |
| 182 |
to be functional on both desktops and mobile |
| 183 |
(we handle scrolling by keys, mouse wheel, dragging by scrollbar, |
| 184 |
dragging the whole area - see @link(EnableDragging)). } |
| 185 |
TCastleScrollView = class(TCastleScrollViewCustom) |
| 186 |
strict private |
| 187 |
FScrollArea: TCastleScrollArea; |
| 188 |
protected |
| 189 |
function GetPageSize: Single; override; |
| 190 |
function GetCompleteSize: Single; override; |
| 191 |
function GetScroll: Single; override; |
| 192 |
procedure SetScroll(Value: Single); override; |
| 193 |
function GetScrollStored: Boolean; override; |
| 194 |
public |
| 195 |
constructor Create(AOwner: TComponent); override; |
| 196 |
published |
| 197 |
{ Scroll view rendering makes sense only when ClipChildren is @true. } |
| 198 |
property ClipChildren default true; |
| 199 |
|
| 200 |
{ Children you add here will be scrolled. |
| 201 |
To have a useful scrolling area: |
| 202 |
|
| 203 |
@unorderedList( |
| 204 |
@itemSpacing compact |
| 205 |
@item(Add some children to ScrollArea,) |
| 206 |
@item(Set proper size of the ScrollArea (you can set an explicit size, |
| 207 |
or use @link(TCastleUserInterface.AutoSizeToChildren)),) |
| 208 |
@item(Leave the ScrollArea anchors as default: ScrollArea should be |
| 209 |
anchored to the top-left corner of the parent.) |
| 210 |
) } |
| 211 |
property ScrollArea: TCastleScrollArea read FScrollArea; |
| 212 |
end; |
| 213 |
|
| 214 |
{ Control with a scrollbar. |
| 215 |
This user interface control shows a vertical scrollbar on the right side, |
| 216 |
and allows to operate on it using keyboard, mouse, touch. |
| 217 |
|
| 218 |
The scrollbar automatically doesn't affect anything |
| 219 |
(hence the name of this class contains the word "manual"). |
| 220 |
You need to assign @link(OnChange) event to react to the scrollbar |
| 221 |
dragging to actually do something. |
| 222 |
Usually you also want to adjust @link(PageSize) and @link(CompleteSize). |
| 223 |
|
| 224 |
If you want to easily scroll a tall user interface contents, |
| 225 |
it is easier to use @link(TCastleScrollView) than this control. } |
| 226 |
TCastleScrollViewManual = class(TCastleScrollViewCustom) |
| 227 |
strict private |
| 228 |
FOnChange: TNotifyEvent; |
| 229 |
FPageSize, FCompleteSize: Single; |
| 230 |
FScroll: Single; |
| 231 |
procedure SetPageSize(const AValue: Single); |
| 232 |
procedure SetCompleteSize(const AValue: Single); |
| 233 |
protected |
| 234 |
function GetPageSize: Single; override; |
| 235 |
function GetCompleteSize: Single; override; |
| 236 |
procedure DoScrollChange; override; |
| 237 |
function GetScroll: Single; override; |
| 238 |
procedure SetScroll(Value: Single); override; |
| 239 |
function GetScrollStored: Boolean; override; |
| 240 |
public |
| 241 |
const |
| 242 |
DefaultPageSize = 1; |
| 243 |
DefaultCompleteSize = 10; |
| 244 |
|
| 245 |
constructor Create(AOwner: TComponent); override; |
| 246 |
published |
| 247 |
{ Event sent when @link(Scroll) value was changed, in any way. } |
| 248 |
property OnChange: TNotifyEvent read FOnChange write FOnChange; |
| 249 |
|
| 250 |
{ How much @link(Scroll) is represented by the height of the scrollbar slider. |
| 251 |
Among other things, this determines how much @link(Scroll) changes |
| 252 |
in response to Page Up and Page Down keys. } |
| 253 |
property PageSize: Single read GetPageSize write SetPageSize {$ifdef FPC}default DefaultPageSize{$endif}; |
| 254 |
|
| 255 |
{ How much @link(Scroll) is represented by the height of the entire scrollbar |
| 256 |
(that is, the height of this @link(TCastleScrollViewCustom) instance). } |
| 257 |
property CompleteSize: Single read GetCompleteSize write SetCompleteSize {$ifdef FPC}default DefaultCompleteSize{$endif}; |
| 258 |
end; |
| 259 |
|
| 260 |
{$endif read_interface} |
| 261 |
|
| 262 |
{$ifdef read_implementation} |
| 263 |
|
| 264 |
{ TCastleScrollViewCustom ---------------------------------------------------------- } |
| 265 |
|
| 266 |
constructor TCastleScrollViewCustom.Create(AOwner: TComponent); |
| 267 |
begin |
| 268 |
inherited; |
| 269 |
|
| 270 |
FKeyScrollSpeed := DefaultKeyScrollSpeed; |
| 271 |
FWheelScrollSpeed := DefaultWheelScrollSpeed; |
| 272 |
FTintScrollBarInactive := White; |
| 273 |
FEnableDragging := true; |
| 274 |
|
| 275 |
FScrollbarFrame := TCastleImagePersistent.Create; |
| 276 |
FScrollbarFrame.OnChange := {$Ifdef FPC}@{$endif} CustomScrollBarChanged; |
| 277 |
FScrollbarSlider := TCastleImagePersistent.Create; |
| 278 |
FScrollbarSlider.OnChange := {$Ifdef FPC}@{$endif} CustomScrollBarChanged; |
| 279 |
|
| 280 |
{$define read_implementation_constructor} |
| 281 |
{$I auto_generated_persistent_vectors/tcastlescrollviewcustom_persistent_vectors.inc} |
| 282 |
{$undef read_implementation_constructor} |
| 283 |
end; |
| 284 |
|
| 285 |
destructor TCastleScrollViewCustom.Destroy; |
| 286 |
begin |
| 287 |
FreeAndNil(FScrollbarFrame); |
| 288 |
FreeAndNil(FScrollbarSlider); |
| 289 |
|
| 290 |
{$define read_implementation_destructor} |
| 291 |
{$I auto_generated_persistent_vectors/tcastlescrollviewcustom_persistent_vectors.inc} |
| 292 |
{$undef read_implementation_destructor} |
| 293 |
inherited; |
| 294 |
end; |
| 295 |
|
| 296 |
procedure TCastleScrollViewCustom.RenderOverChildren; |
| 297 |
var |
| 298 |
Color: TCastleColor; |
| 299 |
FinalFrame: TCastleImagePersistent; |
| 300 |
FinalSlider: TCastleImagePersistent; |
| 301 |
begin |
| 302 |
if ScrollBarVisible then |
| 303 |
begin |
| 304 |
Color := Lerp(ScrollbarActive, TintScrollBarInactive, White); |
| 305 |
|
| 306 |
if FScrollbarFrame.Empty then |
| 307 |
FinalFrame := Theme.ImagesPersistent[tiScrollbarFrame] |
| 308 |
else |
| 309 |
FinalFrame := FScrollbarFrame; |
| 310 |
|
| 311 |
FinalFrame.DrawUiBegin(UIScale); |
| 312 |
FinalFrame.Color := FinalFrame.Color * Color; |
| 313 |
FinalFrame.Draw(ScrollbarFrameRect); |
| 314 |
FinalFrame.DrawUiEnd; |
| 315 |
|
| 316 |
if FScrollbarSlider.Empty then |
| 317 |
FinalSlider := Theme.ImagesPersistent[tiScrollbarSlider] |
| 318 |
else |
| 319 |
FinalSlider := FScrollbarSlider; |
| 320 |
|
| 321 |
FinalSlider.DrawUiBegin(UIScale); |
| 322 |
FinalSlider.Color := FinalSlider.Color * Color; |
| 323 |
FinalSlider.Draw(ScrollbarSliderRect); |
| 324 |
FinalSlider.DrawUiEnd; |
| 325 |
end; |
| 326 |
|
| 327 |
inherited; |
| 328 |
end; |
| 329 |
|
| 330 |
function TCastleScrollViewCustom.PropertySections(const PropertyName: String |
| 331 |
): TPropertySections; |
| 332 |
begin |
| 333 |
if ArrayContainsString(PropertyName, [ |
| 334 |
'ScrollBarWidth', 'ScrollbarFrame', 'ScrollbarSlider', 'EnableDragging' |
| 335 |
]) then |
| 336 |
Result := [psBasic] |
| 337 |
else |
| 338 |
if ArrayContainsString(PropertyName, ['ScrollbarVerticalMargin']) then |
| 339 |
Result := [psBasic, psLayout] |
| 340 |
else |
| 341 |
Result := inherited PropertySections(PropertyName); |
| 342 |
end; |
| 343 |
|
| 344 |
procedure TCastleScrollViewCustom.DoScrollChange; |
| 345 |
begin |
| 346 |
end; |
| 347 |
|
| 348 |
procedure TCastleScrollViewCustom.CustomScrollBarChanged(Sender: TObject); |
| 349 |
begin |
| 350 |
VisibleChange([chRender]); |
| 351 |
end; |
| 352 |
|
| 353 |
procedure TCastleScrollViewCustom.ResetInertialForce; |
| 354 |
begin |
| 355 |
DragSpeed := 0; |
| 356 |
TimeSinceDraggingStopped := 0; |
| 357 |
end; |
| 358 |
|
| 359 |
procedure TCastleScrollViewCustom.SetScrollbarVerticalMargin(const AValue: Single); |
| 360 |
begin |
| 361 |
if FScrollbarVerticalMargin <> AValue then |
| 362 |
begin |
| 363 |
FScrollbarVerticalMargin := AValue; |
| 364 |
VisibleChange([chRender]); |
| 365 |
end; |
| 366 |
end; |
| 367 |
|
| 368 |
procedure TCastleScrollViewCustom.Update(const SecondsPassed: Single; |
| 369 |
var HandleInput: boolean); |
| 370 |
|
| 371 |
{ Calculate ScrollBarVisible, ScrollbarFrame, ScrollbarFrame } |
| 372 |
procedure UpdateScrollBarVisibleAndRects; |
| 373 |
var |
| 374 |
SR: TFloatRectangle; |
| 375 |
begin |
| 376 |
SR := RenderRect; |
| 377 |
|
| 378 |
if FScrollbarLeft then |
| 379 |
ScrollbarFrameRect := SR.LeftPart(EffectiveScrollBarWidth * UIScale) |
| 380 |
else |
| 381 |
ScrollbarFrameRect := SR.RightPart(EffectiveScrollBarWidth * UIScale); |
| 382 |
|
| 383 |
ScrollbarFrameRect := ScrollbarFrameRect.Grow(0, - FScrollbarVerticalMargin * UIScale); |
| 384 |
|
| 385 |
ScrollbarSliderRect := ScrollbarFrameRect; |
| 386 |
if GetCompleteSize = 0 then |
| 387 |
FScrollBarVisible := false |
| 388 |
else |
| 389 |
begin |
| 390 |
ScrollbarSliderRect.Height := GetPageSize * ScrollbarFrameRect.Height / GetCompleteSize; |
| 391 |
ScrollbarSliderRect.Height := Max( |
| 392 |
Theme.MinimumScrollbarSliderHeight * UIScale, ScrollbarSliderRect.Height); |
| 393 |
// limit ScrollbarSliderRect.Height to not be taller then this control |
| 394 |
ScrollbarSliderRect.Height := Min( |
| 395 |
EffectiveHeightForChildren * UIScale, ScrollbarSliderRect.Height); |
| 396 |
|
| 397 |
FScrollBarVisible := ScrollbarFrameRect.Height > ScrollbarSliderRect.Height + SingleEpsilon; |
| 398 |
{ equivalent would be to set |
| 399 |
ScrollBarVisible := GetPageSize < GetCompleteSize; |
| 400 |
But this way makes it clear that MapRange below is valid, |
| 401 |
it will not divide by zero. } |
| 402 |
end; |
| 403 |
if FScrollBarVisible then |
| 404 |
begin |
| 405 |
{ above calculation of FScrollBarVisible must guarantee this. } |
| 406 |
Assert(ScrollbarFrameRect.Height > ScrollbarSliderRect.Height); |
| 407 |
ScrollbarSliderRect.Bottom := ScrollbarSliderRect.Bottom + |
| 408 |
(MapRange(Scroll, ScrollMin, ScrollMax, |
| 409 |
ScrollbarFrameRect.Height - ScrollbarSliderRect.Height, 0)); |
| 410 |
end else |
| 411 |
begin |
| 412 |
ScrollBarDragging := false; |
| 413 |
Scroll := ScrollMin; // make sure to shift to ScrollMin if scroll suddenly disappears |
| 414 |
ScrollbarActive := 0; |
| 415 |
ResetInertialForce; |
| 416 |
end; |
| 417 |
end; |
| 418 |
|
| 419 |
procedure HandleKeys; |
| 420 |
begin |
| 421 |
if ScrollBarVisible and HandleInput then |
| 422 |
begin |
| 423 |
if Container.Pressed[ keyArrowUp] or |
| 424 |
Container.Pressed[keyNumpadUp] then |
| 425 |
begin |
| 426 |
Scroll := Scroll - KeyScrollSpeed * SecondsPassed; |
| 427 |
ResetInertialForce; |
| 428 |
{ Do "HandleInput := false" only when some relevant key |
| 429 |
(for scroll view) is pressed, otherwise allow other controls to process |
| 430 |
the key. } |
| 431 |
HandleInput := false; |
| 432 |
end; |
| 433 |
if Container.Pressed[ keyArrowDown] or |
| 434 |
Container.Pressed[keyNumpadDown] then |
| 435 |
begin |
| 436 |
Scroll := Scroll + KeyScrollSpeed * SecondsPassed; |
| 437 |
ResetInertialForce; |
| 438 |
HandleInput := false; |
| 439 |
end; |
| 440 |
end; |
| 441 |
end; |
| 442 |
|
| 443 |
{ Make the illusion of "inertial force" when dragging, by gradually |
| 444 |
decelerating dragging speed once user stops dragging. |
| 445 |
Also updates TimeSinceDraggingStopped. } |
| 446 |
procedure DraggingInertialForce; |
| 447 |
const |
| 448 |
DragDecelerationDuration = 0.5; |
| 449 |
var |
| 450 |
CurrentDragSpeed: Single; |
| 451 |
begin |
| 452 |
if ScrollbarVisible then |
| 453 |
begin |
| 454 |
if buttonLeft in Container.MousePressed then |
| 455 |
begin |
| 456 |
{ note that we update DragSpeed even when DragSinceLastUpdate = 0, |
| 457 |
which means user keeps pressing but doesn't drag } |
| 458 |
if not IsZero(SecondsPassed) then |
| 459 |
DragSpeed := DragSinceLastUpdate / SecondsPassed else |
| 460 |
DragSpeed := 0; // whatever sensible value |
| 461 |
TimeSinceDraggingStopped := 0; |
| 462 |
end else |
| 463 |
begin |
| 464 |
TimeSinceDraggingStopped := TimeSinceDraggingStopped + SecondsPassed; |
| 465 |
if (DragSpeed <> 0) and |
| 466 |
(TimeSinceDraggingStopped < DragDecelerationDuration) then |
| 467 |
begin |
| 468 |
CurrentDragSpeed := MapRange( |
| 469 |
TimeSinceDraggingStopped, 0, DragDecelerationDuration, |
| 470 |
DragSpeed, 0); |
| 471 |
Scroll := Scroll + CurrentDragSpeed * SecondsPassed; |
| 472 |
{ stop inertial force if you reached the border of scroll } |
| 473 |
if CurrentDragSpeed > 0 then |
| 474 |
begin |
| 475 |
if Scroll = ScrollMax then TimeSinceDraggingStopped := DragDecelerationDuration; |
| 476 |
end else |
| 477 |
begin |
| 478 |
if Scroll = ScrollMin then TimeSinceDraggingStopped := DragDecelerationDuration; |
| 479 |
end; |
| 480 |
end; |
| 481 |
end; |
| 482 |
DragSinceLastUpdate := 0; |
| 483 |
end; |
| 484 |
end; |
| 485 |
|
| 486 |
{ Update ScrollbarActive, TimeSinceDraggingStarted } |
| 487 |
procedure UpdateScrollBarActive; |
| 488 |
const |
| 489 |
AppearTime = 0.5; |
| 490 |
DisappearTime = 0.5; |
| 491 |
var |
| 492 |
NewScrollbarActive: Single; |
| 493 |
begin |
| 494 |
{ update TimeSinceDragginStarted } |
| 495 |
if TimeSinceDraggingStopped = 0 then |
| 496 |
begin |
| 497 |
{ dragging now } |
| 498 |
TimeSinceDraggingStarted := TimeSinceDraggingStarted + SecondsPassed; |
| 499 |
if TimeSinceDraggingStarted > AppearTime then |
| 500 |
NewScrollbarActive := 1 else |
| 501 |
NewScrollbarActive := TimeSinceDraggingStarted / AppearTime; |
| 502 |
end else |
| 503 |
begin |
| 504 |
{ not dragging now } |
| 505 |
TimeSinceDraggingStarted := 0; |
| 506 |
if TimeSinceDraggingStopped > DisappearTime then |
| 507 |
NewScrollbarActive := 0 else |
| 508 |
NewScrollbarActive := 1 - TimeSinceDraggingStopped / DisappearTime; |
| 509 |
end; |
| 510 |
if ScrollbarActive <> NewScrollbarActive then |
| 511 |
begin |
| 512 |
ScrollbarActive := NewScrollbarActive; |
| 513 |
VisibleChange([chRender]); |
| 514 |
end; |
| 515 |
end; |
| 516 |
|
| 517 |
begin |
| 518 |
inherited; |
| 519 |
|
| 520 |
// clamp Scroll, in case the contents shrunk, see https://github.com/castle-engine/castle-engine/issues/465 |
| 521 |
if not Between(Scroll, ScrollMin, ScrollMax) then |
| 522 |
Scroll := Scroll; |
| 523 |
|
| 524 |
UpdateScrollBarVisibleAndRects; |
| 525 |
HandleKeys; |
| 526 |
DraggingInertialForce; |
| 527 |
UpdateScrollBarActive; |
| 528 |
end; |
| 529 |
|
| 530 |
function TCastleScrollViewCustom.PreviewPress(const Event: TInputPressRelease): boolean; |
| 531 |
begin |
| 532 |
Result := inherited; |
| 533 |
if Result then Exit; |
| 534 |
|
| 535 |
{ Process clicks in PreviewPress, |
| 536 |
since we draw ScrollbarFrameRect in RenderOverChildren. |
| 537 |
So clicking on ScrollbarFrameRect should be captured, before passing them |
| 538 |
to children (e.g. TCastleButton inside ScrollArea). } |
| 539 |
|
| 540 |
if ScrollBarVisible and |
| 541 |
Event.IsMouseButton(buttonLeft) and |
| 542 |
ScrollbarFrameRect.Contains(Event.Position) then |
| 543 |
begin |
| 544 |
if Event.Position[1] < ScrollbarSliderRect.Bottom then |
| 545 |
begin |
| 546 |
Scroll := Scroll + GetPageSize; |
| 547 |
ResetInertialForce; |
| 548 |
end else |
| 549 |
if Event.Position[1] >= ScrollbarSliderRect.Top then |
| 550 |
begin |
| 551 |
Scroll := Scroll - GetPageSize; |
| 552 |
ResetInertialForce; |
| 553 |
end else |
| 554 |
ScrollBarDragging := true; |
| 555 |
Exit(true); |
| 556 |
end; |
| 557 |
end; |
| 558 |
|
| 559 |
function TCastleScrollViewCustom.Press(const Event: TInputPressRelease): boolean; |
| 560 |
begin |
| 561 |
Result := inherited; |
| 562 |
if Result then Exit; |
| 563 |
|
| 564 |
{ if not ScrollBarVisible then there is no point in changing Scroll. |
| 565 |
|
| 566 |
This way we allow TStateDialog (that uses TDialog that uses TCastleScrollViewCustom) |
| 567 |
descendants like TStateDialogKey to handle keys and mouse wheel. |
| 568 |
And this is very good for MessageKey, |
| 569 |
when it's used e.g. to allow user to choose any TKey. |
| 570 |
Otherwise MessageKey would not be able to return |
| 571 |
keyPageDown, keyPageUp, etc. keys. } |
| 572 |
|
| 573 |
if ScrollBarVisible then |
| 574 |
case Event.EventType of |
| 575 |
itKey: |
| 576 |
case Event.Key of |
| 577 |
keyPageUp, keyNumpadPageUp: |
| 578 |
begin |
| 579 |
Scroll := Scroll - GetPageSize; |
| 580 |
ResetInertialForce; |
| 581 |
Result := true; |
| 582 |
end; |
| 583 |
keyPageDown, keyNumpadPageDown: |
| 584 |
begin |
| 585 |
Scroll := Scroll + GetPageSize; |
| 586 |
ResetInertialForce; |
| 587 |
Result := true; |
| 588 |
end; |
| 589 |
keyHome, keyNumpadHome: |
| 590 |
begin |
| 591 |
Scroll := ScrollMin; |
| 592 |
ResetInertialForce; |
| 593 |
Result := true; |
| 594 |
end; |
| 595 |
keyEnd, keyNumpadEnd: |
| 596 |
begin |
| 597 |
Scroll := ScrollMax; |
| 598 |
ResetInertialForce; |
| 599 |
Result := true; |
| 600 |
end; |
| 601 |
else ; |
| 602 |
end; |
| 603 |
itMouseWheel: |
| 604 |
if Event.MouseWheelVertical then |
| 605 |
begin |
| 606 |
Scroll := Scroll - Event.MouseWheelScroll * WheelScrollSpeed; |
| 607 |
ResetInertialForce; |
| 608 |
Result := true; |
| 609 |
end; |
| 610 |
else ; |
| 611 |
end; |
| 612 |
|
| 613 |
{ We do not process mouse button clicks (or touches) here |
| 614 |
(not on an entire TCastleScrollViewCustom area -- the code in PreviewPress |
| 615 |
handles clicks inside ScrollbarFrameRect only). |
| 616 |
We only process dragging (with buttonLeft) inside TCastleScrollViewCustom.Update |
| 617 |
and TCastleScrollViewCustom.Motion. |
| 618 |
|
| 619 |
But we still have to mark the mouse click as "handled" here. |
| 620 |
Otherwise some control underneath us (like TCastleView with InterceptInput = true, |
| 621 |
like the one used by TStateDialogOK) could handle |
| 622 |
mouse and capture it (the TCastleContainer automatically "captures" the control |
| 623 |
that returns "true" from Press event), and then we would not receive |
| 624 |
TCastleScrollViewCustom.Motion events anymore. |
| 625 |
|
| 626 |
(Testcase: show MessageOK or TStateDialogOK on mobile (Android, iOS) |
| 627 |
with a long text (to make scrolling relevant).) } |
| 628 |
|
| 629 |
if EnableDragging and Event.IsMouseButton(buttonLeft) and ScrollBarVisible then |
| 630 |
Result := true; |
| 631 |
end; |
| 632 |
|
| 633 |
function TCastleScrollViewCustom.Release(const Event: TInputPressRelease): boolean; |
| 634 |
begin |
| 635 |
Result := inherited; |
| 636 |
if Result then Exit; |
| 637 |
|
| 638 |
if Event.IsMouseButton(buttonLeft) then |
| 639 |
begin |
| 640 |
ScrollBarDragging := false; |
| 641 |
Result := true; |
| 642 |
end; |
| 643 |
end; |
| 644 |
|
| 645 |
function TCastleScrollViewCustom.Motion(const Event: TInputMotion): boolean; |
| 646 |
var |
| 647 |
Drag: Single; |
| 648 |
begin |
| 649 |
Result := inherited; |
| 650 |
if Result then Exit; |
| 651 |
|
| 652 |
if ScrollBarDragging then |
| 653 |
begin |
| 654 |
{ In scaled pixels, dragging by ScrollbarFrameRect.Height equals changing |
| 655 |
Scroll by GetCompleteSize. } |
| 656 |
Scroll := Scroll + GetCompleteSize * |
| 657 |
(Event.OldPosition[1] - Event.Position[1]) / ScrollbarFrameRect.Height; |
| 658 |
ResetInertialForce; |
| 659 |
Result := true; |
| 660 |
end else |
| 661 |
if EnableDragging and (buttonLeft in Event.Pressed) then |
| 662 |
begin |
| 663 |
Drag := ((Event.Position[1] - Event.OldPosition[1]) / UIScale); |
| 664 |
Scroll := Scroll + Drag; |
| 665 |
DragSinceLastUpdate := DragSinceLastUpdate + Drag; |
| 666 |
Result := true; |
| 667 |
end; |
| 668 |
end; |
| 669 |
|
| 670 |
function TCastleScrollViewCustom.ScrollMin: Single; |
| 671 |
begin |
| 672 |
Result := 0; |
| 673 |
end; |
| 674 |
|
| 675 |
function TCastleScrollViewCustom.ScrollMax: Single; |
| 676 |
begin |
| 677 |
Result := Max(0, GetCompleteSize - GetPageSize); |
| 678 |
end; |
| 679 |
|
| 680 |
function TCastleScrollViewCustom.EffectiveScrollBarWidth: Single; |
| 681 |
begin |
| 682 |
if ScrollBarWidth = 0 then |
| 683 |
Result := Theme.ScrollBarWidth |
| 684 |
else |
| 685 |
Result := ScrollBarWidth; |
| 686 |
end; |
| 687 |
|
| 688 |
{$define read_implementation_methods} |
| 689 |
{$I auto_generated_persistent_vectors/tcastlescrollviewcustom_persistent_vectors.inc} |
| 690 |
{$undef read_implementation_methods} |
| 691 |
|
| 692 |
{ TCastleScrollArea ---------------------------------------------------------- } |
| 693 |
|
| 694 |
constructor TCastleScrollArea.Create(AOwner: TComponent); |
| 695 |
begin |
| 696 |
inherited; |
| 697 |
|
| 698 |
if not ( |
| 699 |
(AOwner is TCastleScrollView) and |
| 700 |
(TCastleScrollView(AOwner).ScrollArea = nil) |
| 701 |
) then |
| 702 |
raise Exception.Create('TCastleScrollArea can only be created as a subcomponent of TCastleScrollView, do not create it yourself'); |
| 703 |
|
| 704 |
AutoSizeToChildren := true; |
| 705 |
Anchor(vpTop); |
| 706 |
end; |
| 707 |
|
| 708 |
{ TCastleScrollView ---------------------------------------------------------- } |
| 709 |
|
| 710 |
constructor TCastleScrollView.Create(AOwner: TComponent); |
| 711 |
begin |
| 712 |
inherited; |
| 713 |
|
| 714 |
FScrollArea := TCastleScrollArea.Create(Self); |
| 715 |
FScrollArea.SetSubComponent(true); |
| 716 |
FScrollArea.Name := 'ScrollArea'; |
| 717 |
InsertFront(FScrollArea); |
| 718 |
|
| 719 |
ClipChildren := true; |
| 720 |
end; |
| 721 |
|
| 722 |
function TCastleScrollView.GetPageSize: Single; |
| 723 |
begin |
| 724 |
Result := EffectiveHeightForChildren; |
| 725 |
end; |
| 726 |
|
| 727 |
function TCastleScrollView.GetCompleteSize: Single; |
| 728 |
begin |
| 729 |
Result := ScrollArea.EffectiveHeight; |
| 730 |
end; |
| 731 |
|
| 732 |
function TCastleScrollView.GetScroll: Single; |
| 733 |
begin |
| 734 |
Result := FScrollArea.Translation.Y; |
| 735 |
end; |
| 736 |
|
| 737 |
procedure TCastleScrollView.SetScroll(Value: Single); |
| 738 |
begin |
| 739 |
ClampVar(Value, ScrollMin, ScrollMax); |
| 740 |
FScrollArea.Anchor(vpTop, Value); // sets FScrollArea.Translation.Y |
| 741 |
end; |
| 742 |
|
| 743 |
function TCastleScrollView.GetScrollStored: Boolean; |
| 744 |
begin |
| 745 |
// no point in storing Scroll, it is always synchronized with ScrollArea.Translation.Y |
| 746 |
Result := false; |
| 747 |
end; |
| 748 |
|
| 749 |
{ TCastleScrollViewManual ---------------------------------------------------- } |
| 750 |
|
| 751 |
constructor TCastleScrollViewManual.Create(AOwner: TComponent); |
| 752 |
begin |
| 753 |
inherited Create(AOwner); |
| 754 |
FPageSize := DefaultPageSize; |
| 755 |
FCompleteSize := DefaultCompleteSize; |
| 756 |
end; |
| 757 |
|
| 758 |
function TCastleScrollViewManual.GetPageSize: Single; |
| 759 |
begin |
| 760 |
Result := FPageSize; |
| 761 |
end; |
| 762 |
|
| 763 |
procedure TCastleScrollViewManual.SetPageSize(const AValue: Single); |
| 764 |
begin |
| 765 |
if FPageSize = AValue then Exit; |
| 766 |
FPageSize := AValue; |
| 767 |
VisibleChange([chRender]); |
| 768 |
|
| 769 |
// since ScrollMax changed, make sure Scroll is within bounds |
| 770 |
Scroll := Scroll; |
| 771 |
end; |
| 772 |
|
| 773 |
function TCastleScrollViewManual.GetCompleteSize: Single; |
| 774 |
begin |
| 775 |
Result := FCompleteSize; |
| 776 |
end; |
| 777 |
|
| 778 |
procedure TCastleScrollViewManual.DoScrollChange; |
| 779 |
begin |
| 780 |
inherited; |
| 781 |
if Assigned(OnChange) then |
| 782 |
OnChange(Self); |
| 783 |
end; |
| 784 |
|
| 785 |
procedure TCastleScrollViewManual.SetCompleteSize(const AValue: Single); |
| 786 |
begin |
| 787 |
if FCompleteSize = AValue then Exit; |
| 788 |
FCompleteSize := AValue; |
| 789 |
VisibleChange([chRender]); |
| 790 |
|
| 791 |
// since ScrollMax changed, make sure Scroll is within bounds |
| 792 |
Scroll := Scroll; |
| 793 |
end; |
| 794 |
|
| 795 |
function TCastleScrollViewManual.GetScroll: Single; |
| 796 |
begin |
| 797 |
Result := FScroll; |
| 798 |
end; |
| 799 |
|
| 800 |
procedure TCastleScrollViewManual.SetScroll(Value: Single); |
| 801 |
begin |
| 802 |
ClampVar(Value, ScrollMin, ScrollMax); |
| 803 |
if FScroll <> Value then |
| 804 |
begin |
| 805 |
FScroll := Value; |
| 806 |
DoScrollChange; |
| 807 |
end; |
| 808 |
end; |
| 809 |
|
| 810 |
function TCastleScrollViewManual.GetScrollStored: Boolean; |
| 811 |
begin |
| 812 |
Result := true; |
| 813 |
end; |
| 814 |
|
| 815 |
{$endif read_implementation} |