| 1 |
{%MainUnit castlepropedits.pas} |
| 2 |
{ |
| 3 |
Copyright 2018-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 |
{ Vector property editors. } |
| 18 |
|
| 19 |
{ TCastleVector2PropertyEditor ------------------------------------------------- } |
| 20 |
|
| 21 |
type |
| 22 |
TCastleVector2PropertyEditor = class(TSubPropertiesEditor) |
| 23 |
strict protected |
| 24 |
procedure SetAllValues(const NewValue: TVector2); |
| 25 |
public |
| 26 |
function GetAttributes: TPropertyAttributes; override; |
| 27 |
function GetName: shortstring; override; |
| 28 |
function GetValue: String; override; |
| 29 |
procedure SetValue(const NewValue: String); override; |
| 30 |
{ Unsure if this is necessary to be overridden anymore. |
| 31 |
It is used by default ValueIsStreamed implementation, but we override that. } |
| 32 |
function GetDefaultValue: String; override; |
| 33 |
{ Determines if value is displayed bold or not. |
| 34 |
We override it to check using TCastleVectorXxPersistent.ValueIsStreamed. } |
| 35 |
function ValueIsStreamed: boolean; override; |
| 36 |
function AllEqual: Boolean; override; |
| 37 |
end; |
| 38 |
|
| 39 |
function TCastleVector2PropertyEditor.GetAttributes: TPropertyAttributes; |
| 40 |
begin |
| 41 |
Result := inherited GetAttributes + [paRevertable] |
| 42 |
// our SetValue allows to set value from string |
| 43 |
- [paReadOnly]; |
| 44 |
end; |
| 45 |
|
| 46 |
function TCastleVector2PropertyEditor.GetName: shortstring; |
| 47 |
begin |
| 48 |
Result := inherited GetName; |
| 49 |
{ Since all such properties are called XxxPersistent, and they correspond |
| 50 |
to properties called just Xxx (TCastleVector2PropertyEditor corresponds |
| 51 |
to TVector2) so it's more readable to just hide the "persistent" suffix. } |
| 52 |
Result := SuffixRemove('persistent', Result, true); |
| 53 |
end; |
| 54 |
|
| 55 |
function TCastleVector2PropertyEditor.GetValue: String; |
| 56 |
var |
| 57 |
VectorPersistent: TCastleVector2Persistent; |
| 58 |
begin |
| 59 |
VectorPersistent := (GetObjectValue as TCastleVector2Persistent); |
| 60 |
Result := VectorPersistent.Value.ToString; |
| 61 |
end; |
| 62 |
|
| 63 |
procedure TCastleVector2PropertyEditor.SetAllValues(const NewValue: TVector2); |
| 64 |
var |
| 65 |
I: Integer; |
| 66 |
VectorPersistent: TCastleVector2Persistent; |
| 67 |
begin |
| 68 |
for I := 0 to PropCount - 1 do |
| 69 |
begin |
| 70 |
VectorPersistent := GetObjectValueAt(I) as TCastleVector2Persistent; |
| 71 |
VectorPersistent.Value := NewValue; |
| 72 |
end; |
| 73 |
Modified; |
| 74 |
end; |
| 75 |
|
| 76 |
procedure TCastleVector2PropertyEditor.SetValue(const NewValue: String); |
| 77 |
begin |
| 78 |
SetAllValues(Vector2FromStr(NewValue)); |
| 79 |
end; |
| 80 |
|
| 81 |
function TCastleVector2PropertyEditor.GetDefaultValue: String; |
| 82 |
var |
| 83 |
VectorPersistent: TCastleVector2Persistent; |
| 84 |
begin |
| 85 |
VectorPersistent := (GetObjectValue as TCastleVector2Persistent); |
| 86 |
Result := VectorPersistent.InternalDefaultValue.ToString; |
| 87 |
end; |
| 88 |
|
| 89 |
function TCastleVector2PropertyEditor.ValueIsStreamed: boolean; |
| 90 |
var |
| 91 |
VectorPersistent: TCastleVector2Persistent; |
| 92 |
begin |
| 93 |
VectorPersistent := (GetObjectValue as TCastleVector2Persistent); |
| 94 |
Result := VectorPersistent.ValueIsStreamed; |
| 95 |
end; |
| 96 |
|
| 97 |
function TCastleVector2PropertyEditor.AllEqual: Boolean; |
| 98 |
var |
| 99 |
I: Integer; |
| 100 |
Value1, Value2: TVector2; |
| 101 |
begin |
| 102 |
if PropCount > 1 then |
| 103 |
begin |
| 104 |
Value1 := (GetObjectValueAt(0) as TCastleVector2Persistent).Value; |
| 105 |
for I := 1 to PropCount - 1 do |
| 106 |
begin |
| 107 |
Value2 := (GetObjectValueAt(I) as TCastleVector2Persistent).Value; |
| 108 |
if not TVector2.PerfectlyEquals(Value1, Value2) then |
| 109 |
Exit(false); |
| 110 |
end; |
| 111 |
end; |
| 112 |
|
| 113 |
Result := true; |
| 114 |
end; |
| 115 |
|
| 116 |
{ TCastleVector3PropertyEditor ------------------------------------------------- } |
| 117 |
|
| 118 |
type |
| 119 |
TCastleVector3PropertyEditor = class(TSubPropertiesEditor) |
| 120 |
strict protected |
| 121 |
procedure SetAllValues(const NewValue: TVector3); |
| 122 |
public |
| 123 |
function GetAttributes: TPropertyAttributes; override; |
| 124 |
function GetName: shortstring; override; |
| 125 |
function GetValue: String; override; |
| 126 |
procedure SetValue(const NewValue: String); override; |
| 127 |
function GetDefaultValue: String; override; |
| 128 |
function ValueIsStreamed: boolean; override; |
| 129 |
function AllEqual: Boolean; override; |
| 130 |
end; |
| 131 |
|
| 132 |
function TCastleVector3PropertyEditor.GetAttributes: TPropertyAttributes; |
| 133 |
begin |
| 134 |
Result := inherited GetAttributes + [paRevertable] |
| 135 |
// our SetValue allows to set value from string |
| 136 |
- [paReadOnly]; |
| 137 |
end; |
| 138 |
|
| 139 |
function TCastleVector3PropertyEditor.GetName: shortstring; |
| 140 |
begin |
| 141 |
Result := inherited GetName; |
| 142 |
{ Since all such properties are called XxxPersistent, and they correspond |
| 143 |
to properties called just Xxx (TCastleVector3PropertyEditor corresponds |
| 144 |
to TVector3) so it's more readable to just hide the "persistent" suffix. } |
| 145 |
Result := SuffixRemove('persistent', Result, true); |
| 146 |
end; |
| 147 |
|
| 148 |
function TCastleVector3PropertyEditor.GetValue: String; |
| 149 |
var |
| 150 |
VectorPersistent: TCastleVector3Persistent; |
| 151 |
begin |
| 152 |
VectorPersistent := (GetObjectValue as TCastleVector3Persistent); |
| 153 |
Result := VectorPersistent.Value.ToString; |
| 154 |
end; |
| 155 |
|
| 156 |
procedure TCastleVector3PropertyEditor.SetAllValues(const NewValue: TVector3); |
| 157 |
var |
| 158 |
I: Integer; |
| 159 |
VectorPersistent: TCastleVector3Persistent; |
| 160 |
begin |
| 161 |
for I := 0 to PropCount - 1 do |
| 162 |
begin |
| 163 |
VectorPersistent := GetObjectValueAt(I) as TCastleVector3Persistent; |
| 164 |
VectorPersistent.Value := NewValue; |
| 165 |
end; |
| 166 |
Modified; |
| 167 |
end; |
| 168 |
|
| 169 |
procedure TCastleVector3PropertyEditor.SetValue(const NewValue: String); |
| 170 |
begin |
| 171 |
SetAllValues(Vector3FromStr(NewValue)); |
| 172 |
end; |
| 173 |
|
| 174 |
function TCastleVector3PropertyEditor.GetDefaultValue: String; |
| 175 |
var |
| 176 |
VectorPersistent: TCastleVector3Persistent; |
| 177 |
begin |
| 178 |
VectorPersistent := (GetObjectValue as TCastleVector3Persistent); |
| 179 |
Result := VectorPersistent.InternalDefaultValue.ToString; |
| 180 |
end; |
| 181 |
|
| 182 |
function TCastleVector3PropertyEditor.ValueIsStreamed: boolean; |
| 183 |
var |
| 184 |
VectorPersistent: TCastleVector3Persistent; |
| 185 |
begin |
| 186 |
VectorPersistent := (GetObjectValue as TCastleVector3Persistent); |
| 187 |
Result := VectorPersistent.ValueIsStreamed; |
| 188 |
end; |
| 189 |
|
| 190 |
function TCastleVector3PropertyEditor.AllEqual: Boolean; |
| 191 |
var |
| 192 |
I: Integer; |
| 193 |
Value1, Value2: TVector3; |
| 194 |
begin |
| 195 |
if PropCount > 1 then |
| 196 |
begin |
| 197 |
Value1 := (GetObjectValueAt(0) as TCastleVector3Persistent).Value; |
| 198 |
for I := 1 to PropCount - 1 do |
| 199 |
begin |
| 200 |
Value2 := (GetObjectValueAt(I) as TCastleVector3Persistent).Value; |
| 201 |
if not TVector3.PerfectlyEquals(Value1, Value2) then |
| 202 |
Exit(false); |
| 203 |
end; |
| 204 |
end; |
| 205 |
|
| 206 |
Result := true; |
| 207 |
end; |
| 208 |
|
| 209 |
{ TScalePropertyEditor ------------------------------------------------- } |
| 210 |
|
| 211 |
type |
| 212 |
{ Editor for TVector3 properties, that accepts also a single float as input |
| 213 |
-- the float is interpreted as if it was repeated 3x. |
| 214 |
E.g. "2" works the same as "2 2 2". |
| 215 |
|
| 216 |
This is nice for 3D size / scale properties, |
| 217 |
like TCastleTransform.Scale, TCastleBox.Size. } |
| 218 |
TScalePropertyEditor = class(TCastleVector3PropertyEditor) |
| 219 |
procedure SetValue(const NewValue: String); override; |
| 220 |
end; |
| 221 |
|
| 222 |
procedure TScalePropertyEditor.SetValue(const NewValue: String); |
| 223 |
var |
| 224 |
F: Single; |
| 225 |
V: TVector3; |
| 226 |
begin |
| 227 |
try |
| 228 |
F := StrToFloatDot(NewValue); |
| 229 |
V := Vector3(F, F, F); |
| 230 |
except |
| 231 |
on EConvertError do |
| 232 |
V := Vector3FromStr(NewValue); |
| 233 |
end; |
| 234 |
SetAllValues(V); |
| 235 |
end; |
| 236 |
|
| 237 |
{ TSize2DPropertyEditor ------------------------------------------------- } |
| 238 |
|
| 239 |
type |
| 240 |
{ Editor for Size:TVector2 properties, like TCastlePlane.Size. } |
| 241 |
TSize2DPropertyEditor = class(TCastleVector2PropertyEditor) |
| 242 |
procedure SetValue(const NewValue: String); override; |
| 243 |
end; |
| 244 |
|
| 245 |
procedure TSize2DPropertyEditor.SetValue(const NewValue: String); |
| 246 |
var |
| 247 |
F: Single; |
| 248 |
V: TVector2; |
| 249 |
begin |
| 250 |
try |
| 251 |
{ accept just a single float as input, so e.g. "2" works the same as "2 2" } |
| 252 |
F := StrToFloat(NewValue); |
| 253 |
V := Vector2(F, F); |
| 254 |
except |
| 255 |
on EConvertError do |
| 256 |
V := Vector2FromStr(NewValue); |
| 257 |
end; |
| 258 |
SetAllValues(V); |
| 259 |
end; |
| 260 |
|
| 261 |
{ TCastleVector4PropertyEditor ------------------------------------------------- } |
| 262 |
|
| 263 |
type |
| 264 |
TCastleVector4PropertyEditor = class(TSubPropertiesEditor) |
| 265 |
strict private |
| 266 |
procedure SetAllValues(const NewValue: TVector4); |
| 267 |
protected |
| 268 |
function FormatValue(const Value: TVector4): String; virtual; |
| 269 |
public |
| 270 |
function GetAttributes: TPropertyAttributes; override; |
| 271 |
function GetName: shortstring; override; |
| 272 |
function GetValue: String; override; |
| 273 |
procedure SetValue(const NewValue: String); override; |
| 274 |
function GetDefaultValue: String; override; |
| 275 |
function ValueIsStreamed: boolean; override; |
| 276 |
function AllEqual: Boolean; override; |
| 277 |
end; |
| 278 |
|
| 279 |
function TCastleVector4PropertyEditor.GetAttributes: TPropertyAttributes; |
| 280 |
begin |
| 281 |
Result := inherited GetAttributes + [paRevertable] |
| 282 |
// our SetValue allows to set value from string |
| 283 |
- [paReadOnly]; |
| 284 |
end; |
| 285 |
|
| 286 |
function TCastleVector4PropertyEditor.GetName: shortstring; |
| 287 |
begin |
| 288 |
Result := inherited GetName; |
| 289 |
Result := SuffixRemove('persistent', Result, true); |
| 290 |
end; |
| 291 |
|
| 292 |
function TCastleVector4PropertyEditor.GetValue: String; |
| 293 |
var |
| 294 |
VectorPersistent: TCastleVector4Persistent; |
| 295 |
begin |
| 296 |
VectorPersistent := (GetObjectValue as TCastleVector4Persistent); |
| 297 |
Result := FormatValue(VectorPersistent.Value); |
| 298 |
end; |
| 299 |
|
| 300 |
procedure TCastleVector4PropertyEditor.SetAllValues(const NewValue: TVector4); |
| 301 |
var |
| 302 |
I: Integer; |
| 303 |
VectorPersistent: TCastleVector4Persistent; |
| 304 |
begin |
| 305 |
for I := 0 to PropCount - 1 do |
| 306 |
begin |
| 307 |
VectorPersistent := GetObjectValueAt(I) as TCastleVector4Persistent; |
| 308 |
VectorPersistent.Value := NewValue; |
| 309 |
end; |
| 310 |
Modified; |
| 311 |
end; |
| 312 |
|
| 313 |
procedure TCastleVector4PropertyEditor.SetValue(const NewValue: String); |
| 314 |
|
| 315 |
{ Convert srting to 4D vector, like Vector4FromStr. |
| 316 |
But also accept components using expressions. |
| 317 |
Allows to input e.g. "1 2 3 Deg(90)" or "1 1/2 1/3 1/4" } |
| 318 |
function Vector4FromStrExpression(const S: String): TVector4; |
| 319 |
var |
| 320 |
SPosition: Integer; |
| 321 |
begin |
| 322 |
SPosition := 1; |
| 323 |
Result.X := ParseConstantFloatExpression(NextToken(S, SPosition)); |
| 324 |
Result.Y := ParseConstantFloatExpression(NextToken(S, SPosition)); |
| 325 |
Result.Z := ParseConstantFloatExpression(NextToken(S, SPosition)); |
| 326 |
// interpret 4th component in degrees |
| 327 |
Result.W := ParseConstantFloatExpression(WrapDegIfNeeded(NextToken(S, SPosition))); |
| 328 |
if NextToken(S, SPosition) <> '' then |
| 329 |
raise EConvertError.Create('Expected end of data when reading vector from string'); |
| 330 |
end; |
| 331 |
|
| 332 |
begin |
| 333 |
SetAllValues(Vector4FromStrExpression(NewValue)); |
| 334 |
end; |
| 335 |
|
| 336 |
function TCastleVector4PropertyEditor.FormatValue(const Value: TVector4): String; |
| 337 |
begin |
| 338 |
Result := Value.ToString; |
| 339 |
end; |
| 340 |
|
| 341 |
function TCastleVector4PropertyEditor.GetDefaultValue: String; |
| 342 |
var |
| 343 |
VectorPersistent: TCastleVector4Persistent; |
| 344 |
begin |
| 345 |
VectorPersistent := (GetObjectValue as TCastleVector4Persistent); |
| 346 |
Result := FormatValue(VectorPersistent.InternalDefaultValue); |
| 347 |
end; |
| 348 |
|
| 349 |
function TCastleVector4PropertyEditor.ValueIsStreamed: boolean; |
| 350 |
var |
| 351 |
VectorPersistent: TCastleVector4Persistent; |
| 352 |
begin |
| 353 |
VectorPersistent := (GetObjectValue as TCastleVector4Persistent); |
| 354 |
Result := VectorPersistent.ValueIsStreamed; |
| 355 |
end; |
| 356 |
|
| 357 |
function TCastleVector4PropertyEditor.AllEqual: Boolean; |
| 358 |
var |
| 359 |
I: Integer; |
| 360 |
Value1, Value2: TVector4; |
| 361 |
begin |
| 362 |
if PropCount > 1 then |
| 363 |
begin |
| 364 |
Value1 := (GetObjectValueAt(0) as TCastleVector4Persistent).Value; |
| 365 |
for I := 1 to PropCount - 1 do |
| 366 |
begin |
| 367 |
Value2 := (GetObjectValueAt(I) as TCastleVector4Persistent).Value; |
| 368 |
if not TVector4.PerfectlyEquals(Value1, Value2) then |
| 369 |
Exit(false); |
| 370 |
end; |
| 371 |
end; |
| 372 |
|
| 373 |
Result := true; |
| 374 |
end; |
| 375 |
|
| 376 |
{ TCastleVectorRotationPropertyEditor ---------------------------------------- } |
| 377 |
|
| 378 |
type |
| 379 |
TCastleVectorRotationPropertyEditor = class(TCastleVector4PropertyEditor) |
| 380 |
protected |
| 381 |
function FormatValue(const Value: TVector4): String; override; |
| 382 |
end; |
| 383 |
|
| 384 |
function TCastleVectorRotationPropertyEditor.FormatValue(const Value: TVector4): String; |
| 385 |
begin |
| 386 |
{ The same format will also be accepted on input |
| 387 |
thanks to Vector4FromStrExpression. } |
| 388 |
(* |
| 389 |
Result := FormatDot('%f %f %f Deg(%f)', [ |
| 390 |
Value.X, |
| 391 |
Value.Y, |
| 392 |
Value.Z, |
| 393 |
RadToDeg(Value.W) |
| 394 |
]); |
| 395 |
*) |
| 396 |
Result := FormatDot('%s %s %s deg(%s)', [ |
| 397 |
FloatToStrDisplay(Value.X), |
| 398 |
FloatToStrDisplay(Value.Y), |
| 399 |
FloatToStrDisplay(Value.Z), |
| 400 |
FloatToStrDisplay(RadToDeg(Value.W)) |
| 401 |
]); |
| 402 |
end; |