Debian Codegraph @

src/castlescript/castlescriptxml.pas

raw
1 {
2 Copyright 2016-2023 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 { Helpers to read CastleScript expressions from XML config files
17 (TCastleConfigScriptHelper, TDOMElementScriptHelper). }
18 unit CastleScriptXML;
19
20 {$I castleconf.inc}
21
22 {$ifndef FPC}
23 {$message fatal 'This unit is not yet ported to Delphi'}
24 {$endif}
25
26 interface
27
28 uses Math, DOM,
29 CastleKeysMouse, CastleXMLConfig, CastleXmlUtils;
30
31 type
32 { Class helper to read CastleScript expressions from XML config files.
33 Use this unit, and then you can call powerful @link(GetFloatExpression)
34 instead of @link(TCastleConfig.GetFloat). }
35 TCastleConfigScriptHelper = class helper(TCastleConfigKeysMouseHelper) for TCastleConfig
36
37 { Read a float expression composed in CastleScript,
38 like @code("123.0") or @code("3.0 * 2.0") or @code("sin(2.0)").
39 @groupBegin }
40 function GetFloatExpression(const APath: string;
41 const ADefaultValue: Float): Float;
42 function GetFloatExpression(const APath: string;
43 const ADefaultValue: String): Float;
44 function GetFloatExpression(const APath: string): Float;
45 { @groupEnd }
46
47 { Read an integer expression composed in CastleScript,
48 like @code("123") or @code("3 * 2 + 5").
49 @groupBegin }
50 function GetIntExpression(const APath: string;
51 const ADefaultValue: Int64): Int64;
52 function GetIntExpression(const APath: string;
53 const ADefaultValue: String): Int64;
54 function GetIntExpression(const APath: string): Int64;
55 { @groupEnd }
56 end;
57
58 { Class helper to read CastleScript expressions from DOM (XML files).
59 Use this unit, and then you can call powerful methods
60 @link(AttributeFloatExpression), @link(AttributeFloatExpressionDef)
61 instead of the
62 @link(TDOMElementHelper.AttributeFloat), @link(TDOMElementHelper.AttributeFloatDef). }
63 TDOMElementScriptHelper = class helper(TDOMElementHelper) for TDOMElement
64
65 { Read from Element attribute value as a Float expression, and returns @true.
66 The expression can be anything in CastleScript
67 https://castle-engine.io/castle_script.php ,
68 for example something crazy like @code(2.0 * sin(5.0) + Pi).
69 You can use this method instead of @link(AttributeFloat) to easily allow
70 mathematical exressions in XML attributes.
71
72 If there is no such attribute returns @false and does not modify Value. }
73 function AttributeFloatExpression(const AttrName: string; var Value: Float): boolean;
74
75 { Retrieves from Element given attribute as a Float expression,
76 raises EDOMAttributeMissing if missing.
77
78 The expression can be anything in CastleScript
79 https://castle-engine.io/castle_script.php ,
80 for example something crazy like @code(2.0 * sin(5.0) + Pi).
81 You can use this method instead of @link(AttributeFloat) to easily allow
82 mathematical exressions in XML attributes.
83
84 @raises EDOMAttributeMissing }
85 function AttributeFloatExpression(const AttrName: string): Float;
86
87 { Retrieves from Element given attribute as a Float expression, or a default value.
88
89 The expression can be anything in CastleScript
90 https://castle-engine.io/castle_script.php ,
91 for example something crazy like @code(2.0 * sin(5.0) + Pi).
92 You can use this method instead of @link(AttributeFloatDef) to easily allow
93 mathematical exressions in XML attributes. }
94 function AttributeFloatExpressionDef(const AttrName: string; const DefaultValue: Float): Float;
95 end;
96
97 implementation
98
99 uses SysUtils,
100 CastleScript, CastleScriptParser, CastleUtils;
101
102 { TCastleConfigScriptHelper -------------------------------------------------- }
103
104 function TCastleConfigScriptHelper.GetFloatExpression(const APath: string;
105 const ADefaultValue: Float): Float;
106 begin
107 Result := GetFloatExpression(APath, FloatToStrDot(ADefaultValue));
108 end;
109
110 function TCastleConfigScriptHelper.GetFloatExpression(const APath: string;
111 const ADefaultValue: String): Float;
112 var
113 ResultString: string;
114 E: TCasScriptExpression;
115 begin
116 ResultString := GetValue(APath, ADefaultValue);
117 E := ParseFloatExpression(ResultString, []);
118 try
119 Result := E.AsFloat;
120 finally FreeAndNil(E) end;
121 end;
122
123 function TCastleConfigScriptHelper.GetFloatExpression(const APath: string): Float;
124 var
125 E: TCasScriptExpression;
126 begin
127 E := ParseFloatExpression(GetStringNonEmpty(APath), []);
128 try
129 Result := E.AsFloat;
130 finally FreeAndNil(E) end;
131 end;
132
133 function TCastleConfigScriptHelper.GetIntExpression(const APath: string;
134 const ADefaultValue: Int64): Int64;
135 begin
136 Result := GetIntExpression(APath, IntToStr(ADefaultValue));
137 end;
138
139 function TCastleConfigScriptHelper.GetIntExpression(const APath: string;
140 const ADefaultValue: String): Int64;
141 var
142 ResultString: string;
143 E: TCasScriptExpression;
144 begin
145 ResultString := GetValue(APath, ADefaultValue);
146 E := ParseIntExpression(ResultString, []);
147 try
148 Result := E.AsInt;
149 finally FreeAndNil(E) end;
150 end;
151
152 function TCastleConfigScriptHelper.GetIntExpression(const APath: string): Int64;
153 var
154 E: TCasScriptExpression;
155 begin
156 E := ParseIntExpression(GetStringNonEmpty(APath), []);
157 try
158 Result := E.AsInt;
159 finally FreeAndNil(E) end;
160 end;
161
162 { TDOMElementScriptHelper ---------------------------------------------------- }
163
164 function TDOMElementScriptHelper.AttributeFloatExpression(
165 const AttrName: string; var Value: Float): boolean;
166 var
167 ValueStr: string;
168 E: TCasScriptExpression;
169 begin
170 Result := AttributeString(AttrName, ValueStr);
171 if Result then
172 begin
173 E := ParseFloatExpression(ValueStr, []);
174 try
175 Value := E.AsFloat;
176 finally FreeAndNil(E) end;
177 end;
178 end;
179
180 function TDOMElementScriptHelper.AttributeFloatExpression(const AttrName: string): Float;
181 begin
182 if not AttributeFloatExpression(AttrName, Result) then
183 raise EDOMAttributeMissing.CreateFmt('Missing required (float) attribute "%s" on element "%s"', [AttrName, TagName]);
184 end;
185
186 function TDOMElementScriptHelper.AttributeFloatExpressionDef(const AttrName: string; const DefaultValue: Float): Float;
187 begin
188 if not AttributeFloatExpression(AttrName, Result) then
189 Result := DefaultValue;
190 end;
191
192 end.