| 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 |
{ An abstract slider user interface. You actually want to use one of its descendants: |
| 20 |
@link(TCastleFloatSlider) or @link(TCastleIntegerSlider). } |
| 21 |
TCastleAbstractSlider = class(TCastleUserInterfaceFont) |
| 22 |
strict private |
| 23 |
FDisplayValue: boolean; |
| 24 |
FOnChange: TNotifyEvent; |
| 25 |
FCaption: String; |
| 26 |
FBackground, FThumb: TCastleImagePersistent; |
| 27 |
FTextColor: TCastleColor; |
| 28 |
function GetThumbSize(const ARenderRect: TFloatRectangle): TVector2; |
| 29 |
procedure SetCaption(const Value: String); |
| 30 |
procedure SetTextColor(const Value: TCastleColor); |
| 31 |
procedure ImageChanged(Sender: TObject); |
| 32 |
private |
| 33 |
{ Draw a slider at given Position. If Position is outside 0..1, it is clamped |
| 34 |
to 0..1 (this way we do not show slider at some wild position if it's |
| 35 |
outside the expected range; but DrawSliderText will still show the true, |
| 36 |
unclamped, value). |
| 37 |
|
| 38 |
@param(ARenderRect Rectangle currently occupied by the whole slider, |
| 39 |
in final device pixels. Should be equal to RenderRect.) } |
| 40 |
procedure DrawSliderPosition(const ARenderRect: TFloatRectangle; const Position: Single); |
| 41 |
|
| 42 |
{ Returns a value of Position, always in 0..1 range, |
| 43 |
that would result in slider being drawn at XCoord screen position |
| 44 |
by DrawSliderPosition. |
| 45 |
|
| 46 |
@param(ARenderRect Rectangle currently occupied by the whole slider, |
| 47 |
in final device pixels. Should be equal to RenderRect.) } |
| 48 |
function XCoordToSliderPosition(const XCoord: Single; |
| 49 |
const ARenderRect: TFloatRectangle): Single; |
| 50 |
|
| 51 |
procedure DrawSliderText(const R: TFloatRectangle; Text: String); |
| 52 |
strict protected |
| 53 |
{ React to value change. The default implementation simply calls the OnChange |
| 54 |
event, if assigned. This is called when value is changed by user actions |
| 55 |
(key, mouse), not when it's explicitly assigned by code. } |
| 56 |
procedure DoChange; virtual; |
| 57 |
public |
| 58 |
const |
| 59 |
DefaultSliderWidth = 200; |
| 60 |
DefaultSliderHeight = 30; |
| 61 |
constructor Create(AOwner: TComponent); override; |
| 62 |
destructor Destroy; override; |
| 63 |
procedure Render; override; |
| 64 |
procedure Update(const SecondsPassed: Single; var HandleInput: boolean); override; |
| 65 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 66 |
|
| 67 |
{ Color of the label. By default opaque black. } |
| 68 |
property TextColor: TCastleColor read FTextColor write SetTextColor; |
| 69 |
published |
| 70 |
property Width {$ifdef FPC}default DefaultSliderWidth{$endif}; |
| 71 |
property Height {$ifdef FPC}default DefaultSliderHeight{$endif}; |
| 72 |
|
| 73 |
{ Display the current value as text on the slider, |
| 74 |
right next to the @link(Caption). |
| 75 |
|
| 76 |
The exact method to display is defined by method |
| 77 |
@link(TCastleFloatSlider.ValueToStr) or |
| 78 |
@link(TCastleIntegerSlider.ValueToStr) (depending on descendant), |
| 79 |
so you can further customize it. } |
| 80 |
property DisplayValue: boolean |
| 81 |
read FDisplayValue write FDisplayValue default true; |
| 82 |
|
| 83 |
{ Displayed on the slider. Right before value, if @link(DisplayValue). } |
| 84 |
property Caption: String read FCaption write SetCaption; |
| 85 |
|
| 86 |
{ Called when value (like @link(TCastleFloatSlider.Value) or @link(TCastleIntegerSlider.Value)) |
| 87 |
changed because of user interaction (but not when it was changed by code setting |
| 88 |
the property). } |
| 89 |
property OnChange: TNotifyEvent read FOnChange write FOnChange; |
| 90 |
|
| 91 |
{ Background image that fills the entire slider area. } |
| 92 |
property Background: TCastleImagePersistent read FBackground; |
| 93 |
|
| 94 |
{ Thumb image indicates the current value, |
| 95 |
like (like @link(TCastleFloatSlider.Value) or @link(TCastleIntegerSlider.Value)). } |
| 96 |
property Thumb: TCastleImagePersistent read FThumb; |
| 97 |
|
| 98 |
{$define read_interface_class} |
| 99 |
{$I auto_generated_persistent_vectors/tcastleabstractslider_persistent_vectors.inc} |
| 100 |
{$undef read_interface_class} |
| 101 |
end; |
| 102 |
|
| 103 |
{ Slider to change a float value within a given range. } |
| 104 |
TCastleFloatSlider = class(TCastleAbstractSlider) |
| 105 |
strict private |
| 106 |
FMin: Single; |
| 107 |
FMax: Single; |
| 108 |
FValue: Single; |
| 109 |
FMultipleOf: Single; |
| 110 |
FValuePointer: PSingle; //< non-nil only when initiazed with appropriate constructor |
| 111 |
procedure SetMin(const AMin: Single); |
| 112 |
procedure SetMax(const AMax: Single); |
| 113 |
procedure SetValue(const AValue: Single); |
| 114 |
strict protected |
| 115 |
procedure DoChange; override; |
| 116 |
public |
| 117 |
const |
| 118 |
DefaultMin = 0.0; |
| 119 |
DefaultMax = 1.0; |
| 120 |
constructor Create(AOwner: TComponent); overload; override; |
| 121 |
{ An easy way to construct float slider. } |
| 122 |
constructor Create(AOwner: TComponent; |
| 123 |
const ValuePointer: PSingle; const AMin, AMax: Single; |
| 124 |
const AOnChange: TNotifyEvent); reintroduce; overload; |
| 125 |
procedure Render; override; |
| 126 |
function Press(const Event: TInputPressRelease): boolean; override; |
| 127 |
function Motion(const Event: TInputMotion): boolean; override; |
| 128 |
function ValueToStr(const AValue: Single): String; virtual; |
| 129 |
{ Round to multiple of @link(MultipleOf), if non-zero, and clamp |
| 130 |
to @link(Min) and @link(Max) range. } |
| 131 |
function RoundAndClamp(const AValue: Single): Single; |
| 132 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 133 |
published |
| 134 |
property Min: Single read FMin write SetMin {$ifdef FPC}default DefaultMin{$endif}; |
| 135 |
property Max: Single read FMax write SetMax {$ifdef FPC}default DefaultMax{$endif}; |
| 136 |
|
| 137 |
{ Current value. Usually within @link(Min) and @link(Max) range, |
| 138 |
although the general code should be ready for handle any value here |
| 139 |
(to work even during changes to @link(Min) and @link(Max) properties). } |
| 140 |
property Value: Single read FValue write SetValue {$ifdef FPC}default DefaultMin{$endif}; |
| 141 |
|
| 142 |
{ If non-zero, we force the value selected by user to be a multiple |
| 143 |
of this value (clamped to @link(Min), @link(Max) range). |
| 144 |
For example, if you set this to 0.25, and slider is between 0..1, |
| 145 |
then when user clicks around 0.3 --- we will pick 0.25. It user clicks |
| 146 |
around 0.4 --- we will pick 0.5. |
| 147 |
|
| 148 |
This only affects values selected by user interactions (clicking, |
| 149 |
dragging). This does not process the values you set by code to @link(Value) |
| 150 |
property, though you can use RoundAndClamp method on your values |
| 151 |
yourself. } |
| 152 |
property MultipleOf: Single read FMultipleOf write FMultipleOf; |
| 153 |
end; |
| 154 |
|
| 155 |
{ Slider to change an integer value within a given range. } |
| 156 |
TCastleIntegerSlider = class(TCastleAbstractSlider) |
| 157 |
strict private |
| 158 |
FMin: Integer; |
| 159 |
FMax: Integer; |
| 160 |
FValue: Integer; |
| 161 |
FValuePointer: PInteger; //< non-nil only when initiazed with appropriate constructor |
| 162 |
function XCoordToValue( |
| 163 |
const XCoord: Single; const R: TFloatRectangle): Integer; |
| 164 |
procedure SetMin(const AMin: Integer); |
| 165 |
procedure SetMax(const AMax: Integer); |
| 166 |
procedure SetValue(const AValue: Integer); |
| 167 |
strict protected |
| 168 |
procedure DoChange; override; |
| 169 |
public |
| 170 |
const |
| 171 |
DefaultMin = 0; |
| 172 |
DefaultMax = 10; |
| 173 |
constructor Create(AOwner: TComponent); overload; override; |
| 174 |
{ An easy way to construct integer slider. } |
| 175 |
constructor Create(AOwner: TComponent; |
| 176 |
const ValuePointer: PInteger; const AMin, AMax: Integer; |
| 177 |
const AOnChange: TNotifyEvent); reintroduce; overload; |
| 178 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 179 |
procedure Render; override; |
| 180 |
function Press(const Event: TInputPressRelease): boolean; override; |
| 181 |
function Motion(const Event: TInputMotion): boolean; override; |
| 182 |
function ValueToStr(const AValue: Integer): String; virtual; |
| 183 |
published |
| 184 |
property Min: Integer read FMin write SetMin default DefaultMin; |
| 185 |
property Max: Integer read FMax write SetMax default DefaultMax; |
| 186 |
|
| 187 |
{ Current value. Usually within @link(Min) and @link(Max) range, |
| 188 |
although the general code should be ready for handle any value here |
| 189 |
(to work even during changes to @link(Min) and @link(Max) properties). } |
| 190 |
property Value: Integer read FValue write SetValue default DefaultMin; |
| 191 |
end; |
| 192 |
|
| 193 |
{$endif read_interface} |
| 194 |
|
| 195 |
{$ifdef read_implementation} |
| 196 |
|
| 197 |
{ TCastleAbstractSlider ------------------------------------------------------ } |
| 198 |
|
| 199 |
constructor TCastleAbstractSlider.Create(AOwner: TComponent); |
| 200 |
begin |
| 201 |
inherited; |
| 202 |
FDisplayValue := true; |
| 203 |
Width := DefaultSliderWidth; |
| 204 |
Height := DefaultSliderHeight; |
| 205 |
FTextColor := Black; |
| 206 |
|
| 207 |
FBackground := TCastleImagePersistent.Create; |
| 208 |
FBackground.OnChange := {$ifdef FPC}@{$endif} ImageChanged; |
| 209 |
FThumb := TCastleImagePersistent.Create; |
| 210 |
FThumb.OnChange := {$ifdef FPC}@{$endif} ImageChanged; |
| 211 |
|
| 212 |
{$define read_implementation_constructor} |
| 213 |
{$I auto_generated_persistent_vectors/tcastleabstractslider_persistent_vectors.inc} |
| 214 |
{$undef read_implementation_constructor} |
| 215 |
end; |
| 216 |
|
| 217 |
destructor TCastleAbstractSlider.Destroy; |
| 218 |
begin |
| 219 |
FreeAndNil(FBackground); |
| 220 |
FreeAndNil(FThumb); |
| 221 |
|
| 222 |
{$define read_implementation_destructor} |
| 223 |
{$I auto_generated_persistent_vectors/tcastleabstractslider_persistent_vectors.inc} |
| 224 |
{$undef read_implementation_destructor} |
| 225 |
inherited; |
| 226 |
end; |
| 227 |
|
| 228 |
procedure TCastleAbstractSlider.ImageChanged(Sender: TObject); |
| 229 |
begin |
| 230 |
VisibleChange([chRender]); |
| 231 |
end; |
| 232 |
|
| 233 |
procedure TCastleAbstractSlider.Render; |
| 234 |
var |
| 235 |
BackgroundFinal: TCastleImagePersistent; |
| 236 |
begin |
| 237 |
inherited; |
| 238 |
|
| 239 |
if FBackground.Empty then |
| 240 |
BackgroundFinal := Theme.ImagesPersistent[tiSliderBackground] |
| 241 |
else |
| 242 |
BackgroundFinal := FBackground; |
| 243 |
|
| 244 |
BackgroundFinal.DrawUiBegin(UIScale); |
| 245 |
BackgroundFinal.Draw(RenderRect); |
| 246 |
BackgroundFinal.DrawUiEnd; |
| 247 |
end; |
| 248 |
|
| 249 |
function TCastleAbstractSlider.GetThumbSize(const ARenderRect: TFloatRectangle): TVector2; |
| 250 |
begin |
| 251 |
if FThumb.Empty then |
| 252 |
begin |
| 253 |
{ The default thumb image adjusts to slides sizes. } |
| 254 |
Result.X := ARenderRect.Height; |
| 255 |
Result.Y := ARenderRect.Height; |
| 256 |
end else |
| 257 |
begin |
| 258 |
Result.X := UIScale * FThumb.Width; |
| 259 |
Result.Y := UIScale * FThumb.Height; |
| 260 |
end; |
| 261 |
end; |
| 262 |
|
| 263 |
procedure TCastleAbstractSlider.DrawSliderPosition(const ARenderRect: TFloatRectangle; |
| 264 |
const Position: Single); |
| 265 |
var |
| 266 |
ThumbSize: TVector2; |
| 267 |
ThumbFinal: TCastleImagePersistent; |
| 268 |
begin |
| 269 |
ThumbSize := GetThumbSize(ARenderRect); |
| 270 |
|
| 271 |
if FThumb.Empty then |
| 272 |
ThumbFinal := Theme.ImagesPersistent[tiSliderThumb] |
| 273 |
else |
| 274 |
ThumbFinal := FThumb; |
| 275 |
|
| 276 |
ThumbFinal.DrawUiBegin(UIScale); |
| 277 |
ThumbFinal.Draw(FloatRectangle( |
| 278 |
ARenderRect.Left |
| 279 |
+ Round(MapRangeClamped(Position, 0, 1, 0, ARenderRect.Width - ThumbSize.X)), |
| 280 |
ARenderRect.Bottom |
| 281 |
+ ARenderRect.Height / 2 |
| 282 |
- ThumbSize.Y / 2, |
| 283 |
ThumbSize.X, |
| 284 |
ThumbSize.Y)); |
| 285 |
ThumbFinal.DrawUiEnd; |
| 286 |
end; |
| 287 |
|
| 288 |
function TCastleAbstractSlider.XCoordToSliderPosition( |
| 289 |
const XCoord: Single; const ARenderRect: TFloatRectangle): Single; |
| 290 |
var |
| 291 |
ThumbSize: TVector2; |
| 292 |
begin |
| 293 |
ThumbSize := GetThumbSize(ARenderRect); |
| 294 |
Result := MapRangeClamped(XCoord, |
| 295 |
ARenderRect.Left + 0, |
| 296 |
ARenderRect.Left + ARenderRect.Width - ThumbSize.X, 0, 1); |
| 297 |
end; |
| 298 |
|
| 299 |
procedure TCastleAbstractSlider.DrawSliderText( |
| 300 |
const R: TFloatRectangle; Text: String); |
| 301 |
begin |
| 302 |
if (Caption <> '') and (Text <> '') then |
| 303 |
Text := Caption + ': ' + Text else |
| 304 |
Text := Caption + Text; |
| 305 |
Font.Print( |
| 306 |
R.Left + (R.Width - Font.TextWidth(Text)) / 2, |
| 307 |
R.Bottom + (R.Height - Font.Height) / 2, |
| 308 |
TextColor, Text); |
| 309 |
end; |
| 310 |
|
| 311 |
procedure TCastleAbstractSlider.DoChange; |
| 312 |
begin |
| 313 |
if Assigned(OnChange) then |
| 314 |
OnChange(Self); |
| 315 |
end; |
| 316 |
|
| 317 |
procedure TCastleAbstractSlider.SetCaption(const Value: String); |
| 318 |
begin |
| 319 |
if FCaption <> Value then |
| 320 |
begin |
| 321 |
FCaption := Value; |
| 322 |
VisibleChange([chRender]); |
| 323 |
end; |
| 324 |
end; |
| 325 |
|
| 326 |
procedure TCastleAbstractSlider.Update(const SecondsPassed: Single; |
| 327 |
var HandleInput: boolean); |
| 328 |
begin |
| 329 |
inherited; |
| 330 |
{ left / right arrow keys pressed are already handled by slider } |
| 331 |
if HandleInput and |
| 332 |
(Container.Pressed[keyArrowRight] or Container.Pressed[keyArrowLeft]) then |
| 333 |
HandleInput := false; |
| 334 |
end; |
| 335 |
|
| 336 |
function TCastleAbstractSlider.PropertySections(const PropertyName: String): TPropertySections; |
| 337 |
begin |
| 338 |
if ArrayContainsString(PropertyName, [ |
| 339 |
'Caption', 'DisplayValue', 'TextColorPersistent', 'Background', 'Thumb' |
| 340 |
]) then |
| 341 |
Result := [psBasic] |
| 342 |
else |
| 343 |
Result := inherited PropertySections(PropertyName); |
| 344 |
end; |
| 345 |
|
| 346 |
procedure TCastleAbstractSlider.SetTextColor(const Value: TCastleColor); |
| 347 |
begin |
| 348 |
if not TCastleColor.PerfectlyEquals(FTextColor, Value) then |
| 349 |
begin |
| 350 |
FTextColor := Value; |
| 351 |
VisibleChange([chRender]); |
| 352 |
end; |
| 353 |
end; |
| 354 |
|
| 355 |
{$define read_implementation_methods} |
| 356 |
{$I auto_generated_persistent_vectors/tcastleabstractslider_persistent_vectors.inc} |
| 357 |
{$undef read_implementation_methods} |
| 358 |
|
| 359 |
{ TCastleFloatSlider --------------------------------------------------------- } |
| 360 |
|
| 361 |
constructor TCastleFloatSlider.Create(AOwner: TComponent); |
| 362 |
begin |
| 363 |
inherited; |
| 364 |
FMin := DefaultMin; |
| 365 |
FMax := DefaultMax; |
| 366 |
FValue := FMin; |
| 367 |
end; |
| 368 |
|
| 369 |
constructor TCastleFloatSlider.Create(AOwner: TComponent; |
| 370 |
const ValuePointer: PSingle; const AMin, AMax: Single; |
| 371 |
const AOnChange: TNotifyEvent); |
| 372 |
begin |
| 373 |
Create(AOwner); |
| 374 |
FValuePointer := ValuePointer; |
| 375 |
if Assigned(FValuePointer) then |
| 376 |
Value := FValuePointer^; |
| 377 |
Min := AMin; |
| 378 |
Max := AMax; |
| 379 |
OnChange := AOnChange; |
| 380 |
end; |
| 381 |
|
| 382 |
procedure TCastleFloatSlider.DoChange; |
| 383 |
begin |
| 384 |
if Assigned(FValuePointer) then |
| 385 |
FValuePointer^ := Value; |
| 386 |
inherited; |
| 387 |
end; |
| 388 |
|
| 389 |
procedure TCastleFloatSlider.Render; |
| 390 |
var |
| 391 |
RR: TFloatRectangle; |
| 392 |
begin |
| 393 |
inherited; |
| 394 |
RR := RenderRect; |
| 395 |
DrawSliderPosition(RR, MapRange(Value, Min, Max, 0, 1)); |
| 396 |
if DisplayValue then |
| 397 |
DrawSliderText(RR, ValueToStr(Value)); |
| 398 |
end; |
| 399 |
|
| 400 |
function TCastleFloatSlider.Press(const Event: TInputPressRelease): boolean; |
| 401 |
|
| 402 |
function ValueChange: Single; |
| 403 |
begin |
| 404 |
Result := (Max - Min) / 100; |
| 405 |
end; |
| 406 |
|
| 407 |
begin |
| 408 |
Result := inherited; |
| 409 |
if Result then Exit; |
| 410 |
|
| 411 |
if Event.IsKey(keyArrowRight) then |
| 412 |
begin |
| 413 |
if MultipleOf <> 0 then |
| 414 |
Value := Math.Min(Max, Value + MultipleOf) else |
| 415 |
Value := Math.Min(Max, Value + ValueChange); |
| 416 |
DoChange; |
| 417 |
Result := true; |
| 418 |
end else |
| 419 |
if Event.IsKey(keyArrowLeft) then |
| 420 |
begin |
| 421 |
if MultipleOf <> 0 then |
| 422 |
Value := Math.Max(Min, Value - MultipleOf) else |
| 423 |
Value := Math.Max(Min, Value - ValueChange); |
| 424 |
DoChange; |
| 425 |
Result := true; |
| 426 |
end else |
| 427 |
if Event.IsMouseButton(buttonLeft) then |
| 428 |
begin |
| 429 |
Value := RoundAndClamp(MapRange( |
| 430 |
XCoordToSliderPosition(Event.Position[0], RenderRect), 0, 1, Min, Max)); |
| 431 |
DoChange; |
| 432 |
Result := true; |
| 433 |
end; |
| 434 |
end; |
| 435 |
|
| 436 |
function TCastleFloatSlider.Motion(const Event: TInputMotion): boolean; |
| 437 |
begin |
| 438 |
Result := inherited; |
| 439 |
if Result then Exit; |
| 440 |
|
| 441 |
if buttonLeft in Event.Pressed then |
| 442 |
begin |
| 443 |
Value := RoundAndClamp(MapRange( |
| 444 |
XCoordToSliderPosition(Event.Position[0], RenderRect), 0, 1, Min, Max)); |
| 445 |
DoChange; |
| 446 |
Result := true; |
| 447 |
end; |
| 448 |
end; |
| 449 |
|
| 450 |
function TCastleFloatSlider.ValueToStr(const AValue: Single): String; |
| 451 |
begin |
| 452 |
Result := Format('%f', [AValue]); |
| 453 |
end; |
| 454 |
|
| 455 |
procedure TCastleFloatSlider.SetMin(const AMin: Single); |
| 456 |
begin |
| 457 |
if FMin <> AMin then |
| 458 |
begin |
| 459 |
FMin := AMin; |
| 460 |
VisibleChange([chRender]); |
| 461 |
end; |
| 462 |
end; |
| 463 |
|
| 464 |
procedure TCastleFloatSlider.SetMax(const AMax: Single); |
| 465 |
begin |
| 466 |
if FMax <> AMax then |
| 467 |
begin |
| 468 |
FMax := AMax; |
| 469 |
VisibleChange([chRender]); |
| 470 |
end; |
| 471 |
end; |
| 472 |
|
| 473 |
procedure TCastleFloatSlider.SetValue(const AValue: Single); |
| 474 |
begin |
| 475 |
if FValue <> AValue then |
| 476 |
begin |
| 477 |
FValue := AValue; |
| 478 |
VisibleChange([chRender]); |
| 479 |
end; |
| 480 |
end; |
| 481 |
|
| 482 |
function TCastleFloatSlider.RoundAndClamp(const AValue: Single): Single; |
| 483 |
var |
| 484 |
DivResult: Int64; |
| 485 |
M, Remainder: Double; |
| 486 |
begin |
| 487 |
if MultipleOf <> 0 then |
| 488 |
begin |
| 489 |
{ we use FloatDivMod. |
| 490 |
We have to secure in case AValue or MultipleOf are < 0. |
| 491 |
For MultipleOf it's easy, just always use Abs(MultipleOf). } |
| 492 |
M := Abs(MultipleOf); |
| 493 |
if AValue >= 0 then |
| 494 |
begin |
| 495 |
FloatDivMod(AValue, M, DivResult, Remainder); |
| 496 |
if Remainder < M / 2 then |
| 497 |
Result := M * DivResult else |
| 498 |
Result := M * (DivResult + 1); |
| 499 |
end else |
| 500 |
begin |
| 501 |
FloatDivMod(-AValue, M, DivResult, Remainder); |
| 502 |
if Remainder < M / 2 then |
| 503 |
Result := M * DivResult else |
| 504 |
Result := M * (DivResult + 1); |
| 505 |
Result := -Result; |
| 506 |
end; |
| 507 |
end else |
| 508 |
Result := AValue; |
| 509 |
|
| 510 |
{ Clamp at the end. If Min, Max are not a multiple of MultipleOf - so be it. } |
| 511 |
Result := Clamped(Result, Min, Max); |
| 512 |
end; |
| 513 |
|
| 514 |
function TCastleFloatSlider.PropertySections(const PropertyName: String): TPropertySections; |
| 515 |
begin |
| 516 |
if ArrayContainsString(PropertyName, [ |
| 517 |
'Min', 'Max', 'Value', 'MultipleOf' |
| 518 |
]) then |
| 519 |
Result := [psBasic] |
| 520 |
else |
| 521 |
Result := inherited PropertySections(PropertyName); |
| 522 |
end; |
| 523 |
|
| 524 |
{ TCastleIntegerSlider ------------------------------------------------------- } |
| 525 |
|
| 526 |
constructor TCastleIntegerSlider.Create(AOwner: TComponent); |
| 527 |
begin |
| 528 |
inherited; |
| 529 |
FMin := DefaultMin; |
| 530 |
FMax := DefaultMax; |
| 531 |
FValue := FMin; |
| 532 |
end; |
| 533 |
|
| 534 |
constructor TCastleIntegerSlider.Create(AOwner: TComponent; |
| 535 |
const ValuePointer: PInteger; const AMin, AMax: Integer; |
| 536 |
const AOnChange: TNotifyEvent); |
| 537 |
begin |
| 538 |
Create(AOwner); |
| 539 |
FValuePointer := ValuePointer; |
| 540 |
if Assigned(FValuePointer) then |
| 541 |
Value := FValuePointer^; |
| 542 |
Min := AMin; |
| 543 |
Max := AMax; |
| 544 |
OnChange := AOnChange; |
| 545 |
end; |
| 546 |
|
| 547 |
procedure TCastleIntegerSlider.DoChange; |
| 548 |
begin |
| 549 |
if Assigned(FValuePointer) then |
| 550 |
FValuePointer^ := Value; |
| 551 |
inherited; |
| 552 |
end; |
| 553 |
|
| 554 |
procedure TCastleIntegerSlider.Render; |
| 555 |
var |
| 556 |
RR: TFloatRectangle; |
| 557 |
begin |
| 558 |
inherited; |
| 559 |
RR := RenderRect; |
| 560 |
DrawSliderPosition(RR, MapRange(Value, Min, Max, 0, 1)); |
| 561 |
if DisplayValue then |
| 562 |
DrawSliderText(RR, ValueToStr(Value)); |
| 563 |
end; |
| 564 |
|
| 565 |
function TCastleIntegerSlider.Press(const Event: TInputPressRelease): boolean; |
| 566 |
const |
| 567 |
ValueChange = 1; |
| 568 |
begin |
| 569 |
Result := inherited; |
| 570 |
if Result then Exit; |
| 571 |
|
| 572 |
if Event.IsKey(keyArrowRight) then |
| 573 |
begin |
| 574 |
Value := Math.Min(Max, Value + ValueChange); |
| 575 |
DoChange; |
| 576 |
Result := true; |
| 577 |
end else |
| 578 |
if Event.IsKey(keyArrowLeft) then |
| 579 |
begin |
| 580 |
Value := Math.Max(Min, Value - ValueChange); |
| 581 |
DoChange; |
| 582 |
Result := true; |
| 583 |
end else |
| 584 |
if Event.IsMouseButton(buttonLeft) then |
| 585 |
begin |
| 586 |
Value := XCoordToValue(Event.Position[0], RenderRect); |
| 587 |
DoChange; |
| 588 |
Result := true; |
| 589 |
end; |
| 590 |
end; |
| 591 |
|
| 592 |
function TCastleIntegerSlider.XCoordToValue( |
| 593 |
const XCoord: Single; const R: TFloatRectangle): Integer; |
| 594 |
begin |
| 595 |
{ We do additional Clamped over Round result to avoid any |
| 596 |
chance of floating-point errors due to lack of precision. } |
| 597 |
Result := Clamped(Round( |
| 598 |
MapRange(XCoordToSliderPosition(XCoord, R), 0, 1, |
| 599 |
Min, Max)), Min, Max); |
| 600 |
end; |
| 601 |
|
| 602 |
function TCastleIntegerSlider.Motion(const Event: TInputMotion): boolean; |
| 603 |
begin |
| 604 |
Result := inherited; |
| 605 |
if Result then Exit; |
| 606 |
|
| 607 |
if buttonLeft in Event.Pressed then |
| 608 |
begin |
| 609 |
Value := XCoordToValue(Event.Position[0], RenderRect); |
| 610 |
DoChange; |
| 611 |
Result := true; |
| 612 |
end; |
| 613 |
end; |
| 614 |
|
| 615 |
function TCastleIntegerSlider.ValueToStr(const AValue: Integer): String; |
| 616 |
begin |
| 617 |
Result := IntToStr(AValue); |
| 618 |
end; |
| 619 |
|
| 620 |
procedure TCastleIntegerSlider.SetMin(const AMin: Integer); |
| 621 |
begin |
| 622 |
if FMin <> AMin then |
| 623 |
begin |
| 624 |
FMin := AMin; |
| 625 |
VisibleChange([chRender]); |
| 626 |
end; |
| 627 |
end; |
| 628 |
|
| 629 |
procedure TCastleIntegerSlider.SetMax(const AMax: Integer); |
| 630 |
begin |
| 631 |
if FMax <> AMax then |
| 632 |
begin |
| 633 |
FMax := AMax; |
| 634 |
VisibleChange([chRender]); |
| 635 |
end; |
| 636 |
end; |
| 637 |
|
| 638 |
procedure TCastleIntegerSlider.SetValue(const AValue: Integer); |
| 639 |
begin |
| 640 |
if FValue <> AValue then |
| 641 |
begin |
| 642 |
FValue := AValue; |
| 643 |
VisibleChange([chRender]); |
| 644 |
end; |
| 645 |
end; |
| 646 |
|
| 647 |
function TCastleIntegerSlider.PropertySections(const PropertyName: String): TPropertySections; |
| 648 |
begin |
| 649 |
if ArrayContainsString(PropertyName, [ |
| 650 |
'Min', 'Max', 'Value' |
| 651 |
]) then |
| 652 |
Result := [psBasic] |
| 653 |
else |
| 654 |
Result := inherited PropertySections(PropertyName); |
| 655 |
end; |
| 656 |
|
| 657 |
{$endif read_implementation} |