| 1 |
{%MainUnit castlevectors.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 |
{ CastleVectors types wrapped as a TCastleComponent instance. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
type |
| 22 |
TGetVector2Event = function: TVector2 of object; |
| 23 |
TSetVector2Event = procedure (const Value: TVector2) of object; |
| 24 |
TGetVector3Event = function: TVector3 of object; |
| 25 |
TSetVector3Event = procedure (const Value: TVector3) of object; |
| 26 |
TGetVector4Event = function: TVector4 of object; |
| 27 |
TSetVector4Event = procedure (const Value: TVector4) of object; |
| 28 |
|
| 29 |
{ TVector2 record represented as a TPersistent descendant, |
| 30 |
to be able to visually edit it (in Lazarus and Delphi visual designer, |
| 31 |
and Castle Game Engine visual designer) and to serialize it. |
| 32 |
|
| 33 |
Normal user code does not need to deal with this class or it's instances. |
| 34 |
Instead directly operate on TVector2 values, |
| 35 |
which is faster and more natural. |
| 36 |
TVector2 are simple records, so they are naturally copied on assignment, |
| 37 |
and various operators like additions work in a natural way too. |
| 38 |
} |
| 39 |
TCastleVector2Persistent = class(TCastleComponent) |
| 40 |
strict private |
| 41 |
{ During IsLoading, Set* only modifies ScheduledSetValue, |
| 42 |
and then in Loaded we send whole vector by one InternalSetValue call. |
| 43 |
This decreases the number of calls to the vector setters during deserialization. |
| 44 |
Also it means that we send the vector as a whole, which is good if the setter |
| 45 |
is doing some processing (e.g. normalizing the value). } |
| 46 |
ScheduledSetValue: TVector2; |
| 47 |
HasScheduledSetValue: Boolean; |
| 48 |
function GetValue: TVector2; |
| 49 |
function GetX: Single; |
| 50 |
function GetY: Single; |
| 51 |
procedure SetValue(const AValue: TVector2); |
| 52 |
procedure SetX(const AValue: Single); |
| 53 |
procedure SetY(const AValue: Single); |
| 54 |
function XIsStored: Boolean; |
| 55 |
function YIsStored: Boolean; |
| 56 |
{ Make sure HasScheduledSetValue is true, ScheduledSetValue is valid, |
| 57 |
and return @ScheduledSetValue. } |
| 58 |
function InitScheduledSetValue: PVector2; |
| 59 |
protected |
| 60 |
procedure Loaded; override; |
| 61 |
public |
| 62 |
InternalGetValue: TGetVector2Event; |
| 63 |
InternalSetValue: TSetVector2Event; |
| 64 |
{ Default value, just like Pascal "default" property attribute, |
| 65 |
is only used to decide which vector components to write to file. |
| 66 |
|
| 67 |
It is your responsibility to make sure that this is really the default |
| 68 |
(right after creation) state of the object, |
| 69 |
and the deserialization will just @italic(not modify) components not specified |
| 70 |
(it will not reset them to InternalDefaultValue value). } |
| 71 |
InternalDefaultValue: TVector2; |
| 72 |
constructor Create(const AOwner: TComponent = nil); reintroduce; |
| 73 |
property Value: TVector2 read GetValue write SetValue; |
| 74 |
function ValueIsStreamed: Boolean; override; |
| 75 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 76 |
function ToString: String; override; |
| 77 |
published |
| 78 |
property X: Single read GetX write SetX stored XIsStored nodefault; |
| 79 |
property Y: Single read GetY write SetY stored YIsStored nodefault; |
| 80 |
end; |
| 81 |
|
| 82 |
{ TVector3 record represented as a TPersistent descendant, |
| 83 |
to be able to visually edit it (in Lazarus and Delphi visual designer, |
| 84 |
and Castle Game Engine visual designer) and to serialize it. |
| 85 |
|
| 86 |
Normal user code does not need to deal with this class or it's instances. |
| 87 |
Instead directly operate on TVector3 values, |
| 88 |
which is faster and more natural. |
| 89 |
TVector3 are simple records, so they are naturally copied on assignment, |
| 90 |
and various operators like additions work in a natural way too. |
| 91 |
} |
| 92 |
TCastleVector3Persistent = class(TCastleComponent) |
| 93 |
strict private |
| 94 |
{ During IsLoading, Set* only modifies ScheduledSetValue, |
| 95 |
and then in Loaded we send whole vector by one InternalSetValue call. |
| 96 |
This decreases the number of calls to the vector setters during deserialization. |
| 97 |
Also it means that we send the vector as a whole, which is good if the setter |
| 98 |
is doing some processing (e.g. normalizing the value). } |
| 99 |
ScheduledSetValue: TVector3; |
| 100 |
HasScheduledSetValue: Boolean; |
| 101 |
function GetValue: TVector3; |
| 102 |
function GetX: Single; |
| 103 |
function GetY: Single; |
| 104 |
function GetZ: Single; |
| 105 |
procedure SetValue(const AValue: TVector3); |
| 106 |
procedure SetX(const AValue: Single); |
| 107 |
procedure SetY(const AValue: Single); |
| 108 |
procedure SetZ(const AValue: Single); |
| 109 |
function XIsStored: Boolean; |
| 110 |
function YIsStored: Boolean; |
| 111 |
function ZIsStored: Boolean; |
| 112 |
{ Make sure HasScheduledSetValue is true, ScheduledSetValue is valid, |
| 113 |
and return @ScheduledSetValue. } |
| 114 |
function InitScheduledSetValue: PVector3; |
| 115 |
protected |
| 116 |
procedure Loaded; override; |
| 117 |
public |
| 118 |
InternalGetValue: TGetVector3Event; |
| 119 |
InternalSetValue: TSetVector3Event; |
| 120 |
InternalDefaultValue: TVector3; |
| 121 |
constructor Create(const AOwner: TComponent = nil); reintroduce; |
| 122 |
property Value: TVector3 read GetValue write SetValue; |
| 123 |
function ValueIsStreamed: Boolean; override; |
| 124 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 125 |
function ToString: String; override; |
| 126 |
published |
| 127 |
property X: Single read GetX write SetX stored XIsStored nodefault; |
| 128 |
property Y: Single read GetY write SetY stored YIsStored nodefault; |
| 129 |
property Z: Single read GetZ write SetZ stored ZIsStored nodefault; |
| 130 |
end; |
| 131 |
|
| 132 |
{ TVector4 record represented as a TPersistent descendant, |
| 133 |
to be able to visually edit it (in Lazarus and Delphi visual designer, |
| 134 |
and Castle Game Engine visual designer) and to serialize it. |
| 135 |
|
| 136 |
Normal user code does not need to deal with this class or it's instances. |
| 137 |
Instead directly operate on TVector4 values, |
| 138 |
which is faster and more natural. |
| 139 |
TVector4 are simple records, so they are naturally copied on assignment, |
| 140 |
and various operators like additions work in a natural way too. |
| 141 |
} |
| 142 |
TCastleVector4Persistent = class(TCastleComponent) |
| 143 |
strict private |
| 144 |
{ During IsLoading, Set* only modifies ScheduledSetValue, |
| 145 |
and then in Loaded we send whole vector by one InternalSetValue call. |
| 146 |
This decreases the number of calls to the vector setters during deserialization. |
| 147 |
Also it means that we send the vector as a whole, which is good if the setter |
| 148 |
is doing some processing (e.g. normalizing the value). } |
| 149 |
ScheduledSetValue: TVector4; |
| 150 |
HasScheduledSetValue: Boolean; |
| 151 |
function GetValue: TVector4; |
| 152 |
function GetX: Single; |
| 153 |
function GetY: Single; |
| 154 |
function GetZ: Single; |
| 155 |
function GetW: Single; |
| 156 |
procedure SetValue(const AValue: TVector4); |
| 157 |
procedure SetX(const AValue: Single); |
| 158 |
procedure SetY(const AValue: Single); |
| 159 |
procedure SetZ(const AValue: Single); |
| 160 |
procedure SetW(const AValue: Single); |
| 161 |
function XIsStored: Boolean; |
| 162 |
function YIsStored: Boolean; |
| 163 |
function ZIsStored: Boolean; |
| 164 |
function WIsStored: Boolean; |
| 165 |
{ Make sure HasScheduledSetValue is true, ScheduledSetValue is valid, |
| 166 |
and return @ScheduledSetValue. } |
| 167 |
function InitScheduledSetValue: PVector4; |
| 168 |
protected |
| 169 |
procedure Loaded; override; |
| 170 |
public |
| 171 |
InternalGetValue: TGetVector4Event; |
| 172 |
InternalSetValue: TSetVector4Event; |
| 173 |
InternalDefaultValue: TVector4; |
| 174 |
constructor Create(const AOwner: TComponent = nil); reintroduce; |
| 175 |
property Value: TVector4 read GetValue write SetValue; |
| 176 |
function ValueIsStreamed: Boolean; override; |
| 177 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 178 |
function ToString: String; override; |
| 179 |
published |
| 180 |
property X: Single read GetX write SetX stored XIsStored nodefault; |
| 181 |
property Y: Single read GetY write SetY stored YIsStored nodefault; |
| 182 |
property Z: Single read GetZ write SetZ stored ZIsStored nodefault; |
| 183 |
property W: Single read GetW write SetW stored WIsStored nodefault; |
| 184 |
end; |
| 185 |
|
| 186 |
{ TCastleVector4Persistent descendant for castle editor |
| 187 |
to display angle component nicer |
| 188 |
(as 'Angle (W)' and 'Deg(90)' instead of as 'W' and in radians). } |
| 189 |
TCastleVector4RotationPersistent = class(TCastleVector4Persistent) |
| 190 |
|
| 191 |
end; |
| 192 |
|
| 193 |
{ Helper methods you can use from TCastleComponent.CustomSerialization |
| 194 |
to manage reading/writing of vectors. } |
| 195 |
TSerializationProcessVectorsHelper = class helper for TSerializationProcess |
| 196 |
{ Serialize and deserialize given vector Value. |
| 197 |
|
| 198 |
When deserializing, we always try to read it from file. |
| 199 |
If it is not present, the Value is not modified. |
| 200 |
If it is present in the file, but not all components, |
| 201 |
then the not present componens are not modified (they are @italic(not) |
| 202 |
reset to DefaultValue! The DefaultValue is only used at writing). |
| 203 |
|
| 204 |
When serializing, we write it to file only if IsStored. |
| 205 |
Generally IsStored=false should indicate "the Value is the same as when the object |
| 206 |
is created, thus there's no point in serializing it". |
| 207 |
Moreover, each vector component (Value.X, Value.Y...) |
| 208 |
is written only if it is different than corresponding DefaultValue |
| 209 |
vector component (DefaultValue.X, DefaultValue.Y...). |
| 210 |
|
| 211 |
@groupBegin } |
| 212 |
procedure ReadWriteVector(const Key: String; var Value: TVector2; |
| 213 |
const DefaultValue: TVector2; const IsStored: Boolean); overload; |
| 214 |
procedure ReadWriteVector(const Key: String; var Value: TVector3; |
| 215 |
const DefaultValue: TVector3; const IsStored: Boolean); overload; |
| 216 |
procedure ReadWriteVector(const Key: String; var Value: TVector4; |
| 217 |
const DefaultValue: TVector4; const IsStored: Boolean); overload; |
| 218 |
{ @groupEnd } |
| 219 |
end; |
| 220 |
|
| 221 |
{$endif read_interface} |
| 222 |
|
| 223 |
{$ifdef read_implementation} |
| 224 |
|
| 225 |
{ TCastleVector2Persistent ---------------------------------------------------- } |
| 226 |
|
| 227 |
constructor TCastleVector2Persistent.Create(const AOwner: TComponent = nil); |
| 228 |
begin |
| 229 |
inherited Create(AOwner); |
| 230 |
{ Usually the class should not call on itself SetSubComponent, |
| 231 |
as being subcomponent is determined by the caller/owner. |
| 232 |
But in this case |
| 233 |
1. we know that this class should *only* be useful to help with serialization, |
| 234 |
as subcomponent |
| 235 |
2. we don't want to break compatibility in how this class is used, |
| 236 |
and users may have used these classes in own projects following our |
| 237 |
earlier template without SetSubComponent call. } |
| 238 |
SetSubComponent(true); |
| 239 |
end; |
| 240 |
|
| 241 |
function TCastleVector2Persistent.GetValue: TVector2; |
| 242 |
begin |
| 243 |
Result := InternalGetValue(); |
| 244 |
end; |
| 245 |
|
| 246 |
procedure TCastleVector2Persistent.SetValue(const AValue: TVector2); |
| 247 |
begin |
| 248 |
InternalSetValue(AValue); |
| 249 |
end; |
| 250 |
|
| 251 |
function TCastleVector2Persistent.GetX: Single; |
| 252 |
begin |
| 253 |
Result := Value.X; |
| 254 |
end; |
| 255 |
|
| 256 |
function TCastleVector2Persistent.GetY: Single; |
| 257 |
begin |
| 258 |
Result := Value.Y; |
| 259 |
end; |
| 260 |
|
| 261 |
procedure TCastleVector2Persistent.SetX(const AValue: Single); |
| 262 |
var |
| 263 |
V: TVector2; |
| 264 |
begin |
| 265 |
if IsLoading then |
| 266 |
begin |
| 267 |
InitScheduledSetValue^.X := AValue; |
| 268 |
end else |
| 269 |
begin |
| 270 |
V := Value; |
| 271 |
V.X := AValue; |
| 272 |
Value := V; |
| 273 |
end; |
| 274 |
end; |
| 275 |
|
| 276 |
procedure TCastleVector2Persistent.SetY(const AValue: Single); |
| 277 |
var |
| 278 |
V: TVector2; |
| 279 |
begin |
| 280 |
if IsLoading then |
| 281 |
begin |
| 282 |
InitScheduledSetValue^.Y := AValue; |
| 283 |
end else |
| 284 |
begin |
| 285 |
V := Value; |
| 286 |
V.Y := AValue; |
| 287 |
Value := V; |
| 288 |
end; |
| 289 |
end; |
| 290 |
|
| 291 |
function TCastleVector2Persistent.XIsStored: Boolean; |
| 292 |
begin |
| 293 |
Result := Value.X <> InternalDefaultValue.X; |
| 294 |
end; |
| 295 |
|
| 296 |
function TCastleVector2Persistent.YIsStored: Boolean; |
| 297 |
begin |
| 298 |
Result := Value.Y <> InternalDefaultValue.Y; |
| 299 |
end; |
| 300 |
|
| 301 |
function TCastleVector2Persistent.ValueIsStreamed: Boolean; |
| 302 |
begin |
| 303 |
Result := not TVector2.PerfectlyEquals(Value, InternalDefaultValue); |
| 304 |
end; |
| 305 |
|
| 306 |
function TCastleVector2Persistent.InitScheduledSetValue: PVector2; |
| 307 |
begin |
| 308 |
if not HasScheduledSetValue then |
| 309 |
begin |
| 310 |
HasScheduledSetValue := true; |
| 311 |
ScheduledSetValue := Value; |
| 312 |
end; |
| 313 |
Result := @ScheduledSetValue; |
| 314 |
end; |
| 315 |
|
| 316 |
procedure TCastleVector2Persistent.Loaded; |
| 317 |
begin |
| 318 |
inherited; |
| 319 |
if HasScheduledSetValue then |
| 320 |
begin |
| 321 |
HasScheduledSetValue := false; |
| 322 |
Value := ScheduledSetValue; |
| 323 |
end; |
| 324 |
end; |
| 325 |
|
| 326 |
function TCastleVector2Persistent.PropertySections(const PropertyName: String): TPropertySections; |
| 327 |
begin |
| 328 |
if (PropertyName = 'X') or |
| 329 |
(PropertyName = 'Y') then |
| 330 |
Result := [psBasic, psLayout] |
| 331 |
else |
| 332 |
Result := inherited PropertySections(PropertyName); |
| 333 |
end; |
| 334 |
|
| 335 |
function TCastleVector2Persistent.ToString: String; |
| 336 |
begin |
| 337 |
Result := Value.ToString; |
| 338 |
end; |
| 339 |
|
| 340 |
{ TCastleVector3Persistent ---------------------------------------------------- } |
| 341 |
|
| 342 |
constructor TCastleVector3Persistent.Create(const AOwner: TComponent = nil); |
| 343 |
begin |
| 344 |
inherited Create(AOwner); |
| 345 |
{ See TCastleVector2Persistent.Create comments about SetSubComponent call. } |
| 346 |
SetSubComponent(true); |
| 347 |
end; |
| 348 |
|
| 349 |
function TCastleVector3Persistent.GetValue: TVector3; |
| 350 |
begin |
| 351 |
Result := InternalGetValue(); |
| 352 |
end; |
| 353 |
|
| 354 |
procedure TCastleVector3Persistent.SetValue(const AValue: TVector3); |
| 355 |
begin |
| 356 |
InternalSetValue(AValue); |
| 357 |
end; |
| 358 |
|
| 359 |
function TCastleVector3Persistent.GetX: Single; |
| 360 |
begin |
| 361 |
Result := Value.X; |
| 362 |
end; |
| 363 |
|
| 364 |
function TCastleVector3Persistent.GetY: Single; |
| 365 |
begin |
| 366 |
Result := Value.Y; |
| 367 |
end; |
| 368 |
|
| 369 |
function TCastleVector3Persistent.GetZ: Single; |
| 370 |
begin |
| 371 |
Result := Value.Z; |
| 372 |
end; |
| 373 |
|
| 374 |
procedure TCastleVector3Persistent.SetX(const AValue: Single); |
| 375 |
var |
| 376 |
V: TVector3; |
| 377 |
begin |
| 378 |
if IsLoading then |
| 379 |
begin |
| 380 |
InitScheduledSetValue^.X := AValue; |
| 381 |
end else |
| 382 |
begin |
| 383 |
V := Value; |
| 384 |
V.X := AValue; |
| 385 |
Value := V; |
| 386 |
end; |
| 387 |
end; |
| 388 |
|
| 389 |
procedure TCastleVector3Persistent.SetY(const AValue: Single); |
| 390 |
var |
| 391 |
V: TVector3; |
| 392 |
begin |
| 393 |
if IsLoading then |
| 394 |
begin |
| 395 |
InitScheduledSetValue^.Y := AValue; |
| 396 |
end else |
| 397 |
begin |
| 398 |
V := Value; |
| 399 |
V.Y := AValue; |
| 400 |
Value := V; |
| 401 |
end; |
| 402 |
end; |
| 403 |
|
| 404 |
procedure TCastleVector3Persistent.SetZ(const AValue: Single); |
| 405 |
var |
| 406 |
V: TVector3; |
| 407 |
begin |
| 408 |
if IsLoading then |
| 409 |
begin |
| 410 |
InitScheduledSetValue^.Z := AValue; |
| 411 |
end else |
| 412 |
begin |
| 413 |
V := Value; |
| 414 |
V.Z := AValue; |
| 415 |
Value := V; |
| 416 |
end; |
| 417 |
end; |
| 418 |
|
| 419 |
function TCastleVector3Persistent.XIsStored: Boolean; |
| 420 |
begin |
| 421 |
Result := Value.X <> InternalDefaultValue.X; |
| 422 |
end; |
| 423 |
|
| 424 |
function TCastleVector3Persistent.YIsStored: Boolean; |
| 425 |
begin |
| 426 |
Result := Value.Y <> InternalDefaultValue.Y; |
| 427 |
end; |
| 428 |
|
| 429 |
function TCastleVector3Persistent.ZIsStored: Boolean; |
| 430 |
begin |
| 431 |
Result := Value.Z <> InternalDefaultValue.Z; |
| 432 |
end; |
| 433 |
|
| 434 |
function TCastleVector3Persistent.ValueIsStreamed: Boolean; |
| 435 |
begin |
| 436 |
Result := not TVector3.PerfectlyEquals(Value, InternalDefaultValue); |
| 437 |
end; |
| 438 |
|
| 439 |
function TCastleVector3Persistent.InitScheduledSetValue: PVector3; |
| 440 |
begin |
| 441 |
if not HasScheduledSetValue then |
| 442 |
begin |
| 443 |
HasScheduledSetValue := true; |
| 444 |
ScheduledSetValue := Value; |
| 445 |
end; |
| 446 |
Result := @ScheduledSetValue; |
| 447 |
end; |
| 448 |
|
| 449 |
procedure TCastleVector3Persistent.Loaded; |
| 450 |
begin |
| 451 |
inherited; |
| 452 |
if HasScheduledSetValue then |
| 453 |
begin |
| 454 |
HasScheduledSetValue := false; |
| 455 |
Value := ScheduledSetValue; |
| 456 |
end; |
| 457 |
end; |
| 458 |
|
| 459 |
function TCastleVector3Persistent.PropertySections(const PropertyName: String): TPropertySections; |
| 460 |
begin |
| 461 |
if (PropertyName = 'X') or |
| 462 |
(PropertyName = 'Y') or |
| 463 |
(PropertyName = 'Z') then |
| 464 |
Result := [psBasic, psLayout] |
| 465 |
else |
| 466 |
Result := inherited PropertySections(PropertyName); |
| 467 |
end; |
| 468 |
|
| 469 |
function TCastleVector3Persistent.ToString: String; |
| 470 |
begin |
| 471 |
Result := Value.ToString; |
| 472 |
end; |
| 473 |
|
| 474 |
{ TCastleVector4Persistent ---------------------------------------------------- } |
| 475 |
|
| 476 |
constructor TCastleVector4Persistent.Create(const AOwner: TComponent = nil); |
| 477 |
begin |
| 478 |
inherited Create(AOwner); |
| 479 |
{ See TCastleVector2Persistent.Create comments about SetSubComponent call. } |
| 480 |
SetSubComponent(true); |
| 481 |
end; |
| 482 |
|
| 483 |
function TCastleVector4Persistent.GetValue: TVector4; |
| 484 |
begin |
| 485 |
Result := InternalGetValue(); |
| 486 |
end; |
| 487 |
|
| 488 |
procedure TCastleVector4Persistent.SetValue(const AValue: TVector4); |
| 489 |
begin |
| 490 |
InternalSetValue(AValue); |
| 491 |
end; |
| 492 |
|
| 493 |
function TCastleVector4Persistent.GetX: Single; |
| 494 |
begin |
| 495 |
Result := Value.X; |
| 496 |
end; |
| 497 |
|
| 498 |
function TCastleVector4Persistent.GetY: Single; |
| 499 |
begin |
| 500 |
Result := Value.Y; |
| 501 |
end; |
| 502 |
|
| 503 |
function TCastleVector4Persistent.GetZ: Single; |
| 504 |
begin |
| 505 |
Result := Value.Z; |
| 506 |
end; |
| 507 |
|
| 508 |
function TCastleVector4Persistent.GetW: Single; |
| 509 |
begin |
| 510 |
Result := Value.W; |
| 511 |
end; |
| 512 |
|
| 513 |
procedure TCastleVector4Persistent.SetX(const AValue: Single); |
| 514 |
var |
| 515 |
V: TVector4; |
| 516 |
begin |
| 517 |
if IsLoading then |
| 518 |
begin |
| 519 |
InitScheduledSetValue^.X := AValue; |
| 520 |
end else |
| 521 |
begin |
| 522 |
V := Value; |
| 523 |
V.X := AValue; |
| 524 |
Value := V; |
| 525 |
end; |
| 526 |
end; |
| 527 |
|
| 528 |
procedure TCastleVector4Persistent.SetY(const AValue: Single); |
| 529 |
var |
| 530 |
V: TVector4; |
| 531 |
begin |
| 532 |
if IsLoading then |
| 533 |
begin |
| 534 |
InitScheduledSetValue^.Y := AValue; |
| 535 |
end else |
| 536 |
begin |
| 537 |
V := Value; |
| 538 |
V.Y := AValue; |
| 539 |
Value := V; |
| 540 |
end; |
| 541 |
end; |
| 542 |
|
| 543 |
procedure TCastleVector4Persistent.SetZ(const AValue: Single); |
| 544 |
var |
| 545 |
V: TVector4; |
| 546 |
begin |
| 547 |
if IsLoading then |
| 548 |
begin |
| 549 |
InitScheduledSetValue^.Z := AValue; |
| 550 |
end else |
| 551 |
begin |
| 552 |
V := Value; |
| 553 |
V.Z := AValue; |
| 554 |
Value := V; |
| 555 |
end; |
| 556 |
end; |
| 557 |
|
| 558 |
procedure TCastleVector4Persistent.SetW(const AValue: Single); |
| 559 |
var |
| 560 |
V: TVector4; |
| 561 |
begin |
| 562 |
if IsLoading then |
| 563 |
begin |
| 564 |
InitScheduledSetValue^.W := AValue; |
| 565 |
end else |
| 566 |
begin |
| 567 |
V := Value; |
| 568 |
V.W := AValue; |
| 569 |
Value := V; |
| 570 |
end; |
| 571 |
end; |
| 572 |
|
| 573 |
function TCastleVector4Persistent.XIsStored: Boolean; |
| 574 |
begin |
| 575 |
Result := Value.X <> InternalDefaultValue.X; |
| 576 |
end; |
| 577 |
|
| 578 |
function TCastleVector4Persistent.YIsStored: Boolean; |
| 579 |
begin |
| 580 |
Result := Value.Y <> InternalDefaultValue.Y; |
| 581 |
end; |
| 582 |
|
| 583 |
function TCastleVector4Persistent.ZIsStored: Boolean; |
| 584 |
begin |
| 585 |
Result := Value.Z <> InternalDefaultValue.Z; |
| 586 |
end; |
| 587 |
|
| 588 |
function TCastleVector4Persistent.WIsStored: Boolean; |
| 589 |
begin |
| 590 |
Result := Value.W <> InternalDefaultValue.W; |
| 591 |
end; |
| 592 |
|
| 593 |
function TCastleVector4Persistent.ValueIsStreamed: Boolean; |
| 594 |
begin |
| 595 |
Result := not TVector4.PerfectlyEquals(Value, InternalDefaultValue); |
| 596 |
end; |
| 597 |
|
| 598 |
function TCastleVector4Persistent.InitScheduledSetValue: PVector4; |
| 599 |
begin |
| 600 |
if not HasScheduledSetValue then |
| 601 |
begin |
| 602 |
HasScheduledSetValue := true; |
| 603 |
ScheduledSetValue := Value; |
| 604 |
end; |
| 605 |
Result := @ScheduledSetValue; |
| 606 |
end; |
| 607 |
|
| 608 |
procedure TCastleVector4Persistent.Loaded; |
| 609 |
begin |
| 610 |
inherited; |
| 611 |
if HasScheduledSetValue then |
| 612 |
begin |
| 613 |
HasScheduledSetValue := false; |
| 614 |
Value := ScheduledSetValue; |
| 615 |
end; |
| 616 |
end; |
| 617 |
|
| 618 |
function TCastleVector4Persistent.PropertySections(const PropertyName: String): TPropertySections; |
| 619 |
begin |
| 620 |
if (PropertyName = 'X') or |
| 621 |
(PropertyName = 'Y') or |
| 622 |
(PropertyName = 'Z') or |
| 623 |
(PropertyName = 'W') then |
| 624 |
Result := [psBasic, psLayout] |
| 625 |
else |
| 626 |
Result := inherited PropertySections(PropertyName); |
| 627 |
end; |
| 628 |
|
| 629 |
function TCastleVector4Persistent.ToString: String; |
| 630 |
begin |
| 631 |
Result := Value.ToString; |
| 632 |
end; |
| 633 |
|
| 634 |
{ TCastleVector2PersistentSimple -------------------------------------------- } |
| 635 |
|
| 636 |
type |
| 637 |
{ Like TCastleVector2Persistent, but it just contains TVector2, |
| 638 |
you don't need to provide InternalGetValue, InternalSetValue callbacks. } |
| 639 |
TCastleVector2PersistentSimple = class(TCastleVector2Persistent) |
| 640 |
strict private |
| 641 |
function GetValueSimple: TVector2; |
| 642 |
procedure SetValueSimple(const AValue: TVector2); |
| 643 |
public |
| 644 |
SimpleValue: TVector2; |
| 645 |
constructor Create(const AOwner: TComponent = nil); |
| 646 |
end; |
| 647 |
|
| 648 |
constructor TCastleVector2PersistentSimple.Create(const AOwner: TComponent = nil); |
| 649 |
begin |
| 650 |
inherited Create(AOwner); |
| 651 |
InternalGetValue := {$ifdef FPC}@{$endif} GetValueSimple; |
| 652 |
InternalSetValue := {$ifdef FPC}@{$endif} SetValueSimple; |
| 653 |
end; |
| 654 |
|
| 655 |
function TCastleVector2PersistentSimple.GetValueSimple: TVector2; |
| 656 |
begin |
| 657 |
Result := SimpleValue; |
| 658 |
end; |
| 659 |
|
| 660 |
procedure TCastleVector2PersistentSimple.SetValueSimple(const AValue: TVector2); |
| 661 |
begin |
| 662 |
SimpleValue := AValue; |
| 663 |
end; |
| 664 |
|
| 665 |
{ TCastleVector3PersistentSimple -------------------------------------------- } |
| 666 |
|
| 667 |
type |
| 668 |
{ Like TCastleVector3Persistent, but it just contains TVector3, |
| 669 |
you don't need to provide InternalGetValue, InternalSetValue callbacks. } |
| 670 |
TCastleVector3PersistentSimple = class(TCastleVector3Persistent) |
| 671 |
strict private |
| 672 |
function GetValueSimple: TVector3; |
| 673 |
procedure SetValueSimple(const AValue: TVector3); |
| 674 |
public |
| 675 |
SimpleValue: TVector3; |
| 676 |
constructor Create(const AOwner: TComponent = nil); |
| 677 |
end; |
| 678 |
|
| 679 |
constructor TCastleVector3PersistentSimple.Create(const AOwner: TComponent = nil); |
| 680 |
begin |
| 681 |
inherited Create(AOwner); |
| 682 |
InternalGetValue := {$ifdef FPC}@{$endif} GetValueSimple; |
| 683 |
InternalSetValue := {$ifdef FPC}@{$endif} SetValueSimple; |
| 684 |
end; |
| 685 |
|
| 686 |
function TCastleVector3PersistentSimple.GetValueSimple: TVector3; |
| 687 |
begin |
| 688 |
Result := SimpleValue; |
| 689 |
end; |
| 690 |
|
| 691 |
procedure TCastleVector3PersistentSimple.SetValueSimple(const AValue: TVector3); |
| 692 |
begin |
| 693 |
SimpleValue := AValue; |
| 694 |
end; |
| 695 |
|
| 696 |
{ TCastleVector4PersistentSimple -------------------------------------------- } |
| 697 |
|
| 698 |
type |
| 699 |
{ Like TCastleVector4Persistent, but it just contains TVector4, |
| 700 |
you don't need to provide InternalGetValue, InternalSetValue callbacks. } |
| 701 |
TCastleVector4PersistentSimple = class(TCastleVector4Persistent) |
| 702 |
strict private |
| 703 |
function GetValueSimple: TVector4; |
| 704 |
procedure SetValueSimple(const AValue: TVector4); |
| 705 |
public |
| 706 |
SimpleValue: TVector4; |
| 707 |
constructor Create(const AOwner: TComponent = nil); |
| 708 |
end; |
| 709 |
|
| 710 |
constructor TCastleVector4PersistentSimple.Create(const AOwner: TComponent = nil); |
| 711 |
begin |
| 712 |
inherited Create(AOwner); |
| 713 |
InternalGetValue := {$ifdef FPC}@{$endif} GetValueSimple; |
| 714 |
InternalSetValue := {$ifdef FPC}@{$endif} SetValueSimple; |
| 715 |
end; |
| 716 |
|
| 717 |
function TCastleVector4PersistentSimple.GetValueSimple: TVector4; |
| 718 |
begin |
| 719 |
Result := SimpleValue; |
| 720 |
end; |
| 721 |
|
| 722 |
procedure TCastleVector4PersistentSimple.SetValueSimple(const AValue: TVector4); |
| 723 |
begin |
| 724 |
SimpleValue := AValue; |
| 725 |
end; |
| 726 |
|
| 727 |
{ TSerializationProcessVectorsHelper ----------------------------------------- } |
| 728 |
|
| 729 |
procedure TSerializationProcessVectorsHelper.ReadWriteVector( |
| 730 |
const Key: String; var Value: TVector2; const DefaultValue: TVector2; const IsStored: Boolean); |
| 731 |
var |
| 732 |
Persistent: TCastleVector2PersistentSimple; |
| 733 |
begin |
| 734 |
Persistent := TCastleVector2PersistentSimple.Create; |
| 735 |
try |
| 736 |
Persistent.SimpleValue := Value; |
| 737 |
Persistent.InternalDefaultValue := DefaultValue; |
| 738 |
ReadWriteSubComponent(Key, Persistent, IsStored); |
| 739 |
Value := Persistent.SimpleValue; // read back, in case ReadWriteSubComponent read it from file |
| 740 |
finally FreeAndNil(Persistent) end; |
| 741 |
end; |
| 742 |
|
| 743 |
procedure TSerializationProcessVectorsHelper.ReadWriteVector( |
| 744 |
const Key: String; var Value: TVector3; const DefaultValue: TVector3; const IsStored: Boolean); |
| 745 |
var |
| 746 |
Persistent: TCastleVector3PersistentSimple; |
| 747 |
begin |
| 748 |
Persistent := TCastleVector3PersistentSimple.Create; |
| 749 |
try |
| 750 |
Persistent.SimpleValue := Value; |
| 751 |
Persistent.InternalDefaultValue := DefaultValue; |
| 752 |
ReadWriteSubComponent(Key, Persistent, IsStored); |
| 753 |
Value := Persistent.SimpleValue; // read back, in case ReadWriteSubComponent read it from file |
| 754 |
finally FreeAndNil(Persistent) end; |
| 755 |
end; |
| 756 |
|
| 757 |
procedure TSerializationProcessVectorsHelper.ReadWriteVector( |
| 758 |
const Key: String; var Value: TVector4; const DefaultValue: TVector4; const IsStored: Boolean); |
| 759 |
var |
| 760 |
Persistent: TCastleVector4PersistentSimple; |
| 761 |
begin |
| 762 |
Persistent := TCastleVector4PersistentSimple.Create; |
| 763 |
try |
| 764 |
Persistent.SimpleValue := Value; |
| 765 |
Persistent.InternalDefaultValue := DefaultValue; |
| 766 |
ReadWriteSubComponent(Key, Persistent, IsStored); |
| 767 |
Value := Persistent.SimpleValue; // read back, in case ReadWriteSubComponent read it from file |
| 768 |
finally FreeAndNil(Persistent) end; |
| 769 |
end; |
| 770 |
|
| 771 |
{$endif read_implementation} |