Debian Codegraph @

src/base/castlecolors_persistent.inc

raw
1 {
2 Copyright 2018-2022 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { CastleColors types wrapped as a TPersistent instance. }
17
18 {$ifdef read_interface}
19
20 type
21 { TCastleColorRGB record represented as a TPersistent descendant,
22 to be able to visually edit it (in Lazarus and Delphi visual designer,
23 and Castle Game Engine visual designer) and to serialize it.
24
25 Normal user code does not need to deal with this class or it's instances.
26 Instead directly operate on TCastleColorRGB values,
27 which is faster and more natural.
28 TCastleColorRGB are simple records, so they are naturally copied on assignment,
29 and various operators like additions work in a natural way too.
30 }
31 TCastleColorRGBPersistent = class(TCastleComponent)
32 strict private
33 { During IsLoading, Set* only modifies ScheduledSetValue,
34 and then in Loaded we send whole vector by one InternalSetValue call.
35 This decreases the number of calls to the vector setters during deserialization.
36 Also it means that we send the vector as a whole, which is good if the setter
37 is doing some processing (e.g. normalizing the value). }
38 ScheduledSetValue: TCastleColorRGB;
39 HasScheduledSetValue: Boolean;
40 function GetValue: TCastleColorRGB;
41 function GetRed: Single;
42 function GetGreen: Single;
43 function GetBlue: Single;
44 procedure SetValue(const AValue: TCastleColorRGB);
45 procedure SetRed(const AValue: Single);
46 procedure SetGreen(const AValue: Single);
47 procedure SetBlue(const AValue: Single);
48 function RedIsStored: Boolean;
49 function GreenIsStored: Boolean;
50 function BlueIsStored: Boolean;
51 { Make sure HasScheduledSetValue is true, ScheduledSetValue is valid,
52 and return @ScheduledSetValue. }
53 function InitScheduledSetValue: PCastleColorRGB;
54 protected
55 procedure Loaded; override;
56 public
57 InternalGetValue: TGetVector3Event;
58 InternalSetValue: TSetVector3Event;
59 InternalDefaultValue: TCastleColorRGB;
60 constructor Create(const AOwner: TComponent = nil); reintroduce;
61 property Value: TCastleColorRGB read GetValue write SetValue;
62 function ValueIsStreamed: Boolean; override;
63 function PropertySections(const PropertyName: String): TPropertySections; override;
64 function ToString: String; override;
65 published
66 property Red: Single read GetRed write SetRed stored RedIsStored nodefault;
67 property Green: Single read GetGreen write SetGreen stored GreenIsStored nodefault;
68 property Blue: Single read GetBlue write SetBlue stored BlueIsStored nodefault;
69 end;
70
71 { TCastleColor record represented as a TPersistent descendant,
72 to be able to visually edit it (in Lazarus and Delphi visual designer,
73 and Castle Game Engine visual designer) and to serialize it.
74
75 Normal user code does not need to deal with this class or it's instances.
76 Instead directly operate on TCastleColor values,
77 which is faster and more natural.
78 TCastleColor are simple records, so they are naturally copied on assignment,
79 and various operators like additions work in a natural way too.
80 }
81 TCastleColorPersistent = class(TCastleComponent)
82 strict private
83 { During IsLoading, Set* only modifies ScheduledSetValue,
84 and then in Loaded we send whole vector by one InternalSetValue call.
85 This decreases the number of calls to the vector setters during deserialization.
86 Also it means that we send the vector as a whole, which is good if the setter
87 is doing some processing (e.g. normalizing the value). }
88 ScheduledSetValue: TCastleColor;
89 HasScheduledSetValue: Boolean;
90 function GetValue: TCastleColor;
91 function GetRed: Single;
92 function GetGreen: Single;
93 function GetBlue: Single;
94 function GetAlpha: Single;
95 procedure SetValue(const AValue: TCastleColor);
96 procedure SetRed(const AValue: Single);
97 procedure SetGreen(const AValue: Single);
98 procedure SetBlue(const AValue: Single);
99 procedure SetAlpha(const AValue: Single);
100 function RedIsStored: Boolean;
101 function GreenIsStored: Boolean;
102 function BlueIsStored: Boolean;
103 function AlphaIsStored: Boolean;
104 { Make sure HasScheduledSetValue is true, ScheduledSetValue is valid,
105 and return @ScheduledSetValue. }
106 function InitScheduledSetValue: PCastleColor;
107 protected
108 procedure Loaded; override;
109 public
110 InternalGetValue: TGetVector4Event;
111 InternalSetValue: TSetVector4Event;
112 InternalDefaultValue: TCastleColor;
113 constructor Create(const AOwner: TComponent = nil); reintroduce;
114 property Value: TCastleColor read GetValue write SetValue;
115 function ValueIsStreamed: Boolean; override;
116 function PropertySections(const PropertyName: String): TPropertySections; override;
117 function ToString: String; override;
118 published
119 property Red: Single read GetRed write SetRed stored RedIsStored nodefault;
120 property Green: Single read GetGreen write SetGreen stored GreenIsStored nodefault;
121 property Blue: Single read GetBlue write SetBlue stored BlueIsStored nodefault;
122 property Alpha: Single read GetAlpha write SetAlpha stored AlphaIsStored nodefault;
123 end;
124
125 { Helper methods you can use from TCastleComponent.CustomSerialization
126 to manage reading/writing of colors. }
127 TSerializationProcessColorsHelper = class helper(TSerializationProcessVectorsHelper) for TSerializationProcess
128 { Serialize and deserialize given color Value.
129
130 When deserializing, we always try to read it from file.
131 If it is not present, the Value is not modified.
132 If it is present in the file, but not all components,
133 then the not present componens are not modified (they are @italic(not)
134 reset to DefaultValue! The DefaultValue is only used at writing).
135
136 When serializing, we write it to file only if IsStored.
137 Generally IsStored=false should indicate "the Value is the same as when the object
138 is created, thus there's no point in serializing it".
139 Moreover, each color component (Value.X, Value.Y...)
140 is written only if it is different than corresponding DefaultValue
141 color component (DefaultValue.X, DefaultValue.Y...).
142
143 @groupBegin }
144 procedure ReadWriteColor(const Key: String; var Value: TCastleColor;
145 const DefaultValue: TCastleColor; const IsStored: Boolean); overload;
146 procedure ReadWriteColor(const Key: String; var Value: TCastleColorRGB;
147 const DefaultValue: TCastleColorRGB; const IsStored: Boolean); overload;
148 { @groupEnd }
149 end;
150
151 {$endif read_interface}
152
153 {$ifdef read_implementation}
154
155 { TCastleColorRGBPersistent ---------------------------------------------------- }
156
157 constructor TCastleColorRGBPersistent.Create(const AOwner: TComponent = nil);
158 begin
159 inherited Create(AOwner);
160 { See TCastleVector2Persistent.Create comments about SetSubComponent call. }
161 SetSubComponent(true);
162 end;
163
164 function TCastleColorRGBPersistent.GetValue: TCastleColorRGB;
165 begin
166 Result := InternalGetValue();
167 end;
168
169 procedure TCastleColorRGBPersistent.SetValue(const AValue: TCastleColorRGB);
170 begin
171 InternalSetValue(AValue);
172 end;
173
174 function TCastleColorRGBPersistent.GetRed: Single;
175 begin
176 Result := Value.X;
177 end;
178
179 function TCastleColorRGBPersistent.GetGreen: Single;
180 begin
181 Result := Value.Y;
182 end;
183
184 function TCastleColorRGBPersistent.GetBlue: Single;
185 begin
186 Result := Value.Z;
187 end;
188
189 procedure TCastleColorRGBPersistent.SetRed(const AValue: Single);
190 var
191 V: TCastleColorRGB;
192 begin
193 if IsLoading then
194 begin
195 InitScheduledSetValue^.X := AValue;
196 end else
197 begin
198 V := Value;
199 V.X := AValue;
200 Value := V;
201 end;
202 end;
203
204 procedure TCastleColorRGBPersistent.SetGreen(const AValue: Single);
205 var
206 V: TCastleColorRGB;
207 begin
208 if IsLoading then
209 begin
210 InitScheduledSetValue^.Y := AValue;
211 end else
212 begin
213 V := Value;
214 V.Y := AValue;
215 Value := V;
216 end;
217 end;
218
219 procedure TCastleColorRGBPersistent.SetBlue(const AValue: Single);
220 var
221 V: TCastleColorRGB;
222 begin
223 if IsLoading then
224 begin
225 InitScheduledSetValue^.Z := AValue;
226 end else
227 begin
228 V := Value;
229 V.Z := AValue;
230 Value := V;
231 end;
232 end;
233
234 function TCastleColorRGBPersistent.RedIsStored: Boolean;
235 begin
236 Result := Value.X <> InternalDefaultValue.X;
237 end;
238
239 function TCastleColorRGBPersistent.GreenIsStored: Boolean;
240 begin
241 Result := Value.Y <> InternalDefaultValue.Y;
242 end;
243
244 function TCastleColorRGBPersistent.BlueIsStored: Boolean;
245 begin
246 Result := Value.Z <> InternalDefaultValue.Z;
247 end;
248
249 function TCastleColorRGBPersistent.ValueIsStreamed: Boolean;
250 begin
251 Result := not TVector3.PerfectlyEquals(Value, InternalDefaultValue);
252 end;
253
254 function TCastleColorRGBPersistent.InitScheduledSetValue: PCastleColorRGB;
255 begin
256 if not HasScheduledSetValue then
257 begin
258 HasScheduledSetValue := true;
259 ScheduledSetValue := Value;
260 end;
261 Result := @ScheduledSetValue;
262 end;
263
264 procedure TCastleColorRGBPersistent.Loaded;
265 begin
266 inherited;
267 if HasScheduledSetValue then
268 begin
269 HasScheduledSetValue := false;
270 Value := ScheduledSetValue;
271 end;
272 end;
273
274 function TCastleColorRGBPersistent.PropertySections(const PropertyName: String): TPropertySections;
275 begin
276 if (PropertyName = 'Red') or
277 (PropertyName = 'Green') or
278 (PropertyName = 'Blue') then
279 Result := [psBasic, psLayout]
280 else
281 Result := inherited PropertySections(PropertyName);
282 end;
283
284 function TCastleColorRGBPersistent.ToString: String;
285 begin
286 Result := Value.ToString;
287 end;
288
289 { TCastleColorPersistent ---------------------------------------------------- }
290
291 constructor TCastleColorPersistent.Create(const AOwner: TComponent = nil);
292 begin
293 inherited Create(AOwner);
294 { See TCastleVector2Persistent.Create comments about SetSubComponent call. }
295 SetSubComponent(true);
296 end;
297
298 function TCastleColorPersistent.GetValue: TCastleColor;
299 begin
300 Result := InternalGetValue();
301 end;
302
303 procedure TCastleColorPersistent.SetValue(const AValue: TCastleColor);
304 begin
305 InternalSetValue(AValue);
306 end;
307
308 function TCastleColorPersistent.GetRed: Single;
309 begin
310 Result := Value.X;
311 end;
312
313 function TCastleColorPersistent.GetGreen: Single;
314 begin
315 Result := Value.Y;
316 end;
317
318 function TCastleColorPersistent.GetBlue: Single;
319 begin
320 Result := Value.Z;
321 end;
322
323 function TCastleColorPersistent.GetAlpha: Single;
324 begin
325 Result := Value.W;
326 end;
327
328 procedure TCastleColorPersistent.SetRed(const AValue: Single);
329 var
330 V: TCastleColor;
331 begin
332 if IsLoading then
333 begin
334 InitScheduledSetValue^.X := AValue;
335 end else
336 begin
337 V := Value;
338 V.X := AValue;
339 Value := V;
340 end;
341 end;
342
343 procedure TCastleColorPersistent.SetGreen(const AValue: Single);
344 var
345 V: TCastleColor;
346 begin
347 if IsLoading then
348 begin
349 InitScheduledSetValue^.Y := AValue;
350 end else
351 begin
352 V := Value;
353 V.Y := AValue;
354 Value := V;
355 end;
356 end;
357
358 procedure TCastleColorPersistent.SetBlue(const AValue: Single);
359 var
360 V: TCastleColor;
361 begin
362 if IsLoading then
363 begin
364 InitScheduledSetValue^.Z := AValue;
365 end else
366 begin
367 V := Value;
368 V.Z := AValue;
369 Value := V;
370 end;
371 end;
372
373 procedure TCastleColorPersistent.SetAlpha(const AValue: Single);
374 var
375 V: TCastleColor;
376 begin
377 if IsLoading then
378 begin
379 InitScheduledSetValue^.W := AValue;
380 end else
381 begin
382 V := Value;
383 V.W := AValue;
384 Value := V;
385 end;
386 end;
387
388 function TCastleColorPersistent.RedIsStored: Boolean;
389 begin
390 Result := Value.X <> InternalDefaultValue.X;
391 end;
392
393 function TCastleColorPersistent.GreenIsStored: Boolean;
394 begin
395 Result := Value.Y <> InternalDefaultValue.Y;
396 end;
397
398 function TCastleColorPersistent.BlueIsStored: Boolean;
399 begin
400 Result := Value.Z <> InternalDefaultValue.Z;
401 end;
402
403 function TCastleColorPersistent.AlphaIsStored: Boolean;
404 begin
405 Result := Value.W <> InternalDefaultValue.W;
406 end;
407
408 function TCastleColorPersistent.ValueIsStreamed: Boolean;
409 begin
410 Result := not TVector4.PerfectlyEquals(Value, InternalDefaultValue);
411 end;
412
413 function TCastleColorPersistent.InitScheduledSetValue: PCastleColor;
414 begin
415 if not HasScheduledSetValue then
416 begin
417 HasScheduledSetValue := true;
418 ScheduledSetValue := Value;
419 end;
420 Result := @ScheduledSetValue;
421 end;
422
423 procedure TCastleColorPersistent.Loaded;
424 begin
425 inherited;
426 if HasScheduledSetValue then
427 begin
428 HasScheduledSetValue := false;
429 Value := ScheduledSetValue;
430 end;
431 end;
432
433 function TCastleColorPersistent.PropertySections(const PropertyName: String): TPropertySections;
434 begin
435 if (PropertyName = 'Red') or
436 (PropertyName = 'Green') or
437 (PropertyName = 'Blue') or
438 (PropertyName = 'Alpha') then
439 Result := [psBasic, psLayout]
440 else
441 Result := inherited PropertySections(PropertyName);
442 end;
443
444 function TCastleColorPersistent.ToString: String;
445 begin
446 Result := Value.ToString;
447 end;
448
449 { TCastleColorPersistentSimple -------------------------------------------- }
450
451 type
452 { Like TCastleColorPersistent, but it just contains TCastleColor,
453 you don't need to provide InternalGetValue, InternalSetValue callbacks. }
454 TCastleColorPersistentSimple = class(TCastleColorPersistent)
455 strict private
456 function GetValueSimple: TCastleColor;
457 procedure SetValueSimple(const AValue: TCastleColor);
458 public
459 SimpleValue: TCastleColor;
460 constructor Create(const AOwner: TComponent = nil);
461 end;
462
463 constructor TCastleColorPersistentSimple.Create(const AOwner: TComponent = nil);
464 begin
465 inherited Create(AOwner);
466 InternalGetValue := {$ifdef FPC}@{$endif} GetValueSimple;
467 InternalSetValue := {$ifdef FPC}@{$endif} SetValueSimple;
468 end;
469
470 function TCastleColorPersistentSimple.GetValueSimple: TCastleColor;
471 begin
472 Result := SimpleValue;
473 end;
474
475 procedure TCastleColorPersistentSimple.SetValueSimple(const AValue: TCastleColor);
476 begin
477 SimpleValue := AValue;
478 end;
479
480 { TCastleColorRGBPersistentSimple -------------------------------------------- }
481
482 type
483 { Like TCastleColorRGBPersistent, but it just contains TCastleColorRGB,
484 you don't need to provide InternalGetValue, InternalSetValue callbacks. }
485 TCastleColorRGBPersistentSimple = class(TCastleColorRGBPersistent)
486 strict private
487 function GetValueSimple: TCastleColorRGB;
488 procedure SetValueSimple(const AValue: TCastleColorRGB);
489 public
490 SimpleValue: TCastleColorRGB;
491 constructor Create(const AOwner: TComponent = nil);
492 end;
493
494 constructor TCastleColorRGBPersistentSimple.Create(const AOwner: TComponent = nil);
495 begin
496 inherited Create(AOwner);
497 InternalGetValue := {$ifdef FPC}@{$endif} GetValueSimple;
498 InternalSetValue := {$ifdef FPC}@{$endif} SetValueSimple;
499 end;
500
501 function TCastleColorRGBPersistentSimple.GetValueSimple: TCastleColorRGB;
502 begin
503 Result := SimpleValue;
504 end;
505
506 procedure TCastleColorRGBPersistentSimple.SetValueSimple(const AValue: TCastleColorRGB);
507 begin
508 SimpleValue := AValue;
509 end;
510
511 { TSerializationProcessColorsHelper ----------------------------------------- }
512
513 procedure TSerializationProcessColorsHelper.ReadWriteColor(
514 const Key: String; var Value: TCastleColor; const DefaultValue: TCastleColor; const IsStored: Boolean);
515 var
516 Persistent: TCastleColorPersistentSimple;
517 begin
518 Persistent := TCastleColorPersistentSimple.Create;
519 try
520 Persistent.SimpleValue := Value;
521 Persistent.InternalDefaultValue := DefaultValue;
522 ReadWriteSubComponent(Key, Persistent, IsStored);
523 Value := Persistent.SimpleValue; // read back, in case ReadWriteSubComponent read it from file
524 finally FreeAndNil(Persistent) end;
525 end;
526
527 procedure TSerializationProcessColorsHelper.ReadWriteColor(
528 const Key: String; var Value: TCastleColorRGB; const DefaultValue: TCastleColorRGB; const IsStored: Boolean);
529 var
530 Persistent: TCastleColorRGBPersistentSimple;
531 begin
532 Persistent := TCastleColorRGBPersistentSimple.Create;
533 try
534 Persistent.SimpleValue := Value;
535 Persistent.InternalDefaultValue := DefaultValue;
536 ReadWriteSubComponent(Key, Persistent, IsStored);
537 Value := Persistent.SimpleValue; // read back, in case ReadWriteSubComponent read it from file
538 finally FreeAndNil(Persistent) end;
539 end;
540
541 {$endif read_implementation}