| 1 |
{%MainUnit castlepropedits.pas} |
| 2 |
{ |
| 3 |
Copyright 2020-2022 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 |
{ TCastleFloatPropertyEditor ------------------------------------------------- } |
| 18 |
|
| 19 |
type |
| 20 |
{ Improved property editor for floats. |
| 21 |
This is used e.g. for TCastleSpotLight.CutOffAngle editing. } |
| 22 |
TCastleFloatPropertyEditor = class(TFloatPropertyEditor) |
| 23 |
protected |
| 24 |
{ Used to display value. |
| 25 |
Virtual (unlike ancestor FormatValue) to can be overridden. } |
| 26 |
function FormatValueVirt(const AValue: Extended): String; virtual; |
| 27 |
public |
| 28 |
function GetValue: String; override; |
| 29 |
|
| 30 |
{ Determines whether the object inspector shows |
| 31 |
the current property state as default or not (bold). |
| 32 |
|
| 33 |
The default implementation is faulty: it compares "GetDefaultValue<>GetVisualValue" |
| 34 |
but GetDefaultValue always returns zero. |
| 35 |
|
| 36 |
TODO: Submit to fix in LCL. } |
| 37 |
function GetDefaultValue: String; override; |
| 38 |
|
| 39 |
{ In addition to ancestor features (interprets float, with dot or current locale DecimalSeparator) |
| 40 |
this also interprets floats from CastleScript expressions, |
| 41 |
so you can write e.g. "pi / 2". } |
| 42 |
procedure SetValue(const NewValue: String); override; |
| 43 |
end; |
| 44 |
|
| 45 |
{ Property editor for floats in rotation vectors, improves |
| 46 |
standard TCastleFloatPropertyEditor. } |
| 47 |
TCastleFloatRotationPropertyEditor = class(TCastleFloatPropertyEditor) |
| 48 |
protected |
| 49 |
function FormatValueVirt(const AValue: Extended): String; override; |
| 50 |
public |
| 51 |
function GetName: ShortString; override; |
| 52 |
procedure SetValue(const NewValue: String); override; |
| 53 |
end; |
| 54 |
|
| 55 |
{ TCastleFloatPropertyEditor ------------------------------------------------- } |
| 56 |
|
| 57 |
function TCastleFloatPropertyEditor.FormatValueVirt(const AValue: Extended): String; |
| 58 |
begin |
| 59 |
{ Using FloatToStrDisplay for consistency, so that e.g. expanding |
| 60 |
TCastleTransform.Rotation will show same precision for each row (X, Y...) |
| 61 |
as in 4D vector display. } |
| 62 |
Result := FloatToStrDisplay(AValue); |
| 63 |
//inherited FormatValue(AValue); |
| 64 |
end; |
| 65 |
|
| 66 |
function TCastleFloatPropertyEditor.GetValue: String; |
| 67 |
begin |
| 68 |
{ Like ancestor, but use virtual FormatValueVirt. } |
| 69 |
Result := FormatValueVirt(GetFloatValue); |
| 70 |
end; |
| 71 |
|
| 72 |
function TCastleFloatPropertyEditor.GetDefaultValue: String; |
| 73 |
var |
| 74 |
PropInfo: PPropInfo; |
| 75 |
DefFloatValue: Extended; |
| 76 |
begin |
| 77 |
if not HasDefaultValue then |
| 78 |
raise EPropertyError.Create('No property default available'); |
| 79 |
|
| 80 |
PropInfo := GetPropInfo; |
| 81 |
{ Hard-cast Default to Single, |
| 82 |
this is how default value for Single property is stored, |
| 83 |
consistent with PropertyHasDefaultValue in CastleInternalRttiUtils |
| 84 |
which is consistent with other FPC/Lazarus code. } |
| 85 |
DefFloatValue := PSingle(@PropInfo^.Default)^; |
| 86 |
Result := FormatValueVirt(DefFloatValue); |
| 87 |
end; |
| 88 |
|
| 89 |
procedure TCastleFloatPropertyEditor.SetValue(const NewValue: String); |
| 90 |
begin |
| 91 |
try |
| 92 |
inherited SetValue(NewValue); |
| 93 |
except |
| 94 |
// if ancestor could not interpret NewValue, try to interpret it as CastleScript expression |
| 95 |
on EConvertError do |
| 96 |
SetFloatValue(ParseConstantFloatExpression(NewValue)); |
| 97 |
end; |
| 98 |
end; |
| 99 |
|
| 100 |
{ TCastleFloatRotationPropertyEditor ----------------------------------------- } |
| 101 |
|
| 102 |
function TCastleFloatRotationPropertyEditor.GetName: ShortString; |
| 103 |
begin |
| 104 |
Result := inherited GetName; |
| 105 |
|
| 106 |
{ |
| 107 |
if (Result = 'W') and (GetInstProp^.Instance.ClassType = TCastleVector4RotationPersistent) then |
| 108 |
Result := 'Angle (W)'; |
| 109 |
} |
| 110 |
|
| 111 |
if Result = 'W' then |
| 112 |
Result := 'Angle (W)'; |
| 113 |
end; |
| 114 |
|
| 115 |
function TCastleFloatRotationPropertyEditor.FormatValueVirt(const AValue: Extended): String; |
| 116 |
begin |
| 117 |
{ Display value as Deg(...). |
| 118 |
CastleScript also accepts Deg(...) as input. } |
| 119 |
Result := inherited FormatValueVirt(RadToDeg(AValue)); |
| 120 |
Result := 'deg(' + Result + ')'; |
| 121 |
end; |
| 122 |
|
| 123 |
procedure TCastleFloatRotationPropertyEditor.SetValue(const NewValue: String); |
| 124 |
begin |
| 125 |
inherited SetValue(WrapDegIfNeeded(NewValue)); |
| 126 |
end; |
| 127 |
|
| 128 |
{ TCastleTagPropertyEditor ------------------------------------------------- } |
| 129 |
|
| 130 |
{$ifdef CPU64} |
| 131 |
|
| 132 |
type |
| 133 |
{ Improved property editor for TComponent.Tag (PtrInt, which means Int64 on x86_64). } |
| 134 |
TCastleTagPropertyEditor = class(TInt64PropertyEditor) |
| 135 |
public |
| 136 |
{ Determines whether the object inspector shows |
| 137 |
the current property state as default or not (bold). } |
| 138 |
function GetDefaultValue: String; override; |
| 139 |
end; |
| 140 |
|
| 141 |
{ We know that Tag has always default 0. } |
| 142 |
function TCastleTagPropertyEditor.GetDefaultValue: String; |
| 143 |
begin |
| 144 |
Result := IntToStr(0); |
| 145 |
end; |
| 146 |
|
| 147 |
{$endif} |