Debian Codegraph @

src/base/castlerectangles_persistent.inc

raw
1 {%MainUnit castlerectangles.pas}
2 {
3 Copyright 2018-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 { TFloatRectanglePersistent type wrapped as a TCastleComponent instance. }
18
19 {$ifdef read_interface}
20
21 type
22 TGetFloatRectangleEvent = function: TFloatRectangle of object;
23 TSetFloatRectangleEvent = procedure (const Value: TFloatRectangle) of object;
24
25 TFloatRectanglePersistent = class(TCastleComponent)
26 strict private
27 { During IsLoading, Set* only modifies ScheduledSetValue,
28 and then in Loaded we send whole vector by one InternalSetValue call.
29 This decreases the number of calls to the vector setters during deserialization.
30 Also it means that we send the vector as a whole, which is good if the setter
31 is doing some processing (e.g. normalizing the value). }
32 ScheduledSetValue: TFloatRectangle;
33 HasScheduledSetValue: Boolean;
34 function GetValue: TFloatRectangle;
35 function GetLeft: Single;
36 function GetBottom: Single;
37 function GetWidth: Single;
38 function GetHeight: Single;
39 procedure SetValue(const AValue: TFloatRectangle);
40 procedure SetLeft(const AValue: Single);
41 procedure SetBottom(const AValue: Single);
42 procedure SetWidth(const AValue: Single);
43 procedure SetHeight(const AValue: Single);
44 function LeftIsStored: Boolean;
45 function BottomIsStored: Boolean;
46 function WidthIsStored: Boolean;
47 function HeightIsStored: Boolean;
48 { Make sure HasScheduledSetValue is true, ScheduledSetValue is valid,
49 and return @ScheduledSetValue. }
50 function InitScheduledSetValue: PFloatRectangle;
51 protected
52 procedure Loaded; override;
53 public
54 InternalGetValue: TGetFloatRectangleEvent;
55 InternalSetValue: TSetFloatRectangleEvent;
56 InternalDefaultValue: TFloatRectangle;
57 property Value: TFloatRectangle read GetValue write SetValue;
58 function ValueIsStreamed: Boolean; override;
59 function PropertySections(const PropertyName: String): TPropertySections; override;
60 function ToString: String; override;
61 published
62 property Left: Single read GetLeft write SetLeft stored LeftIsStored nodefault;
63 property Bottom: Single read GetBottom write SetBottom stored BottomIsStored nodefault;
64 property Width: Single read GetWidth write SetWidth stored WidthIsStored nodefault;
65 property Height: Single read GetHeight write SetHeight stored HeightIsStored nodefault;
66 end;
67
68 {$endif read_interface}
69
70 {$ifdef read_implementation}
71
72 { TFloatRectanglePersistent ---------------------------------------------------- }
73
74 (*
75 Note that analogous TCastleVector2Persistent.Create constructor
76 1. is reintroduced, to have AOwner=nil by default
77 2. calls SetSubComponent(true) on itself.
78 Both 1 and 2 are somewhat dirty, and done only for backward compatibility.
79 For new TFloatRectanglePersistent we don't do this.
80
81 constructor TFloatRectanglePersistent.Create(const AOwner: TComponent = nil);
82 begin
83 inherited Create(AOwner);
84 { See TCastleVector2Persistent.Create comments about SetSubComponent call. }
85 SetSubComponent(true);
86 end;
87 *)
88
89 function TFloatRectanglePersistent.GetValue: TFloatRectangle;
90 begin
91 Result := InternalGetValue();
92 end;
93
94 procedure TFloatRectanglePersistent.SetValue(const AValue: TFloatRectangle);
95 begin
96 InternalSetValue(AValue);
97 end;
98
99 function TFloatRectanglePersistent.GetLeft: Single;
100 begin
101 Result := Value.Left;
102 end;
103
104 function TFloatRectanglePersistent.GetBottom: Single;
105 begin
106 Result := Value.Bottom;
107 end;
108
109 function TFloatRectanglePersistent.GetWidth: Single;
110 begin
111 Result := Value.Width;
112 end;
113
114 function TFloatRectanglePersistent.GetHeight: Single;
115 begin
116 Result := Value.Height;
117 end;
118
119 procedure TFloatRectanglePersistent.SetLeft(const AValue: Single);
120 var
121 V: TFloatRectangle;
122 begin
123 if IsLoading then
124 begin
125 InitScheduledSetValue^.Left := AValue;
126 end else
127 begin
128 V := Value;
129 V.Left := AValue;
130 Value := V;
131 end;
132 end;
133
134 procedure TFloatRectanglePersistent.SetBottom(const AValue: Single);
135 var
136 V: TFloatRectangle;
137 begin
138 if IsLoading then
139 begin
140 InitScheduledSetValue^.Bottom := AValue;
141 end else
142 begin
143 V := Value;
144 V.Bottom := AValue;
145 Value := V;
146 end;
147 end;
148
149 procedure TFloatRectanglePersistent.SetWidth(const AValue: Single);
150 var
151 V: TFloatRectangle;
152 begin
153 if IsLoading then
154 begin
155 InitScheduledSetValue^.Width := AValue;
156 end else
157 begin
158 V := Value;
159 V.Width := AValue;
160 Value := V;
161 end;
162 end;
163
164 procedure TFloatRectanglePersistent.SetHeight(const AValue: Single);
165 var
166 V: TFloatRectangle;
167 begin
168 if IsLoading then
169 begin
170 InitScheduledSetValue^.Height := AValue;
171 end else
172 begin
173 V := Value;
174 V.Height := AValue;
175 Value := V;
176 end;
177 end;
178
179 function TFloatRectanglePersistent.LeftIsStored: Boolean;
180 begin
181 Result := Value.Left <> InternalDefaultValue.Left;
182 end;
183
184 function TFloatRectanglePersistent.BottomIsStored: Boolean;
185 begin
186 Result := Value.Bottom <> InternalDefaultValue.Bottom;
187 end;
188
189 function TFloatRectanglePersistent.WidthIsStored: Boolean;
190 begin
191 Result := Value.Width <> InternalDefaultValue.Width;
192 end;
193
194 function TFloatRectanglePersistent.HeightIsStored: Boolean;
195 begin
196 Result := Value.Height <> InternalDefaultValue.Height;
197 end;
198
199 function TFloatRectanglePersistent.InitScheduledSetValue: PFloatRectangle;
200 begin
201 if not HasScheduledSetValue then
202 begin
203 HasScheduledSetValue := true;
204 ScheduledSetValue := Value;
205 end;
206 Result := @ScheduledSetValue;
207 end;
208
209 procedure TFloatRectanglePersistent.Loaded;
210 begin
211 inherited;
212 if HasScheduledSetValue then
213 begin
214 HasScheduledSetValue := false;
215 Value := ScheduledSetValue;
216 end;
217 end;
218
219 function TFloatRectanglePersistent.ValueIsStreamed: Boolean;
220 begin
221 Result := not Value.PerfectlyEquals(InternalDefaultValue);
222 end;
223
224 function TFloatRectanglePersistent.PropertySections(const PropertyName: String): TPropertySections;
225 begin
226 if (PropertyName = 'Left') or
227 (PropertyName = 'Bottom') or
228 (PropertyName = 'Width') or
229 (PropertyName = 'Height') then
230 Result := [psBasic, psLayout]
231 else
232 Result := inherited PropertySections(PropertyName);
233 end;
234
235 function TFloatRectanglePersistent.ToString: String;
236 begin
237 Result := Value.ToString;
238 end;
239
240 {$endif read_implementation}