@@ -189,11 +189,14 @@ void clearRenderNode() {
189189
190190 @ Override
191191 void onEditorChange (String newContent ) {
192- this .add3dObjects (newContent );
192+ this .container .getChildren ().clear ();
193+ this .add3dObjects (newContent , true );
193194 }
194195
195196 @ Tool ("""
196- You can add 3D objects to the scene using the following DSL
197+
198+ You can add 3D objects to the scene using the following DSL commands (please use one command per line):
199+ clear
197200 color r g b # use this color for subsequent objects. Use RGB values 0-255
198201 box x y z width height depth
199202 sphere x y z radius
@@ -202,28 +205,43 @@ void onEditorChange(String newContent) {
202205 ambientLight r g b # Use RGB values 0-255 for the color
203206 """ )
204207 public void add3dObjects (@ P ("The dsl to add 3d objects" ) String dsl ) {
205- Platform .runLater (() -> container .getChildren ().clear ());
208+ add3dObjects (dsl , false );
209+ }
210+
211+ public void add3dObjects (String dsl , boolean userInput ) {
206212 var material = new PhongMaterial (Color .WHITE );
213+ var clear = false ;
207214 String [] lines = dsl .split ("\\ R" );
208215 for (String line : lines ) {
209216 var tokens = line .trim ().split ("\\ s+" );
210- if (tokens .length == 0 )
217+ if (tokens .length == 0 || line . startsWith ( "#" ) )
211218 continue ;
212219 var command = tokens [0 ];
213- double [] params = IntStream .range (1 , tokens .length )
214- .mapToObj (i -> tokens [i ])
215- .filter (v -> v != null && !v .isBlank ())
216- .mapToDouble (Double ::parseDouble )
217- .toArray ();
220+
221+ var params = new double [tokens .length - 1 ];
222+ for (int j = 1 ; j < tokens .length ; j ++) {
223+ var v = tokens [j ];
224+
225+ if ("#" .equals (v )) {
226+ break ;
227+ }
228+ if (v == null || v .isBlank () || !canParseToDouble (v )) {
229+ continue ;
230+ }
231+ params [j - 1 ] = Double .parseDouble (v );
232+ }
233+
218234 Node element = switch (command ) {
219235 case "color" -> {
236+ checkParams (command , params , 3 );
220237 var r = (int ) params [0 ];
221238 var g = (int ) params [1 ];
222239 var b = (int ) params [2 ];
223240 material = new PhongMaterial (Color .rgb (r , g , b ));
224241 yield null ;
225242 }
226243 case "box" -> {
244+ checkParams (command , params , 6 );
227245 Box box = new Box (params [3 ], params [4 ], params [5 ]);
228246 box .setMaterial (material );
229247 box .setTranslateX (params [0 ]);
@@ -232,6 +250,7 @@ public void add3dObjects(@P("The dsl to add 3d objects") String dsl) {
232250 yield box ;
233251 }
234252 case "sphere" -> {
253+ checkParams (command , params , 4 );
235254 Sphere sphere = new Sphere (params [3 ]);
236255 sphere .setMaterial (material );
237256 sphere .setTranslateX (params [0 ]);
@@ -240,6 +259,7 @@ public void add3dObjects(@P("The dsl to add 3d objects") String dsl) {
240259 yield sphere ;
241260 }
242261 case "cylinder" -> {
262+ checkParams (command , params , 5 );
243263 Cylinder cylinder = new Cylinder (params [3 ], params [4 ]);
244264 cylinder .setMaterial (material );
245265 cylinder .setTranslateX (params [0 ]);
@@ -248,26 +268,45 @@ public void add3dObjects(@P("The dsl to add 3d objects") String dsl) {
248268 yield cylinder ;
249269 }
250270 case "pointLight" -> {
271+ checkParams (command , params , 3 );
251272 var light = new PointLight (Color .WHITE );
252273 light .getTransforms ().addAll (
253274 new Translate (params [0 ], params [1 ], params [2 ]));
254275 yield light ;
255276 }
256277 case "ambientLight" -> {
278+ checkParams (command , params , 3 );
257279 var r = (int ) params [0 ];
258280 var g = (int ) params [1 ];
259281 var b = (int ) params [2 ];
260282 yield new AmbientLight (Color .rgb (r , g , b ));
261283
262284 }
285+ case "clear" -> {
286+ Platform .runLater (() -> container .getChildren ().clear ());
287+ clear = true ;
288+ yield null ;
289+ }
263290 default -> null ;
264291 };
265292
266293 if (element != null ) {
267294 Platform .runLater (() -> container .getChildren ().add (element ));
268295 }
269296 }
270- setEditorContent (dsl );
297+ if (!userInput ) {
298+ var newContent = clear ? dsl : getEditorContent () + "\n " + dsl ;
299+ setEditorContent (newContent );
300+ }
301+ }
302+
303+ private boolean canParseToDouble (String v ) {
304+ try {
305+ Double .parseDouble (v );
306+ return true ;
307+ } catch (NumberFormatException e ) {
308+ return false ;
309+ }
271310 }
272311
273312 void addStuff () {
@@ -291,4 +330,11 @@ void addStuff() {
291330 });
292331 }
293332
333+ private void checkParams (String command , double [] params , int expected ) {
334+ if (params .length < expected ) {
335+ throw new IllegalArgumentException (
336+ "Command " + command + " expects " + expected + " parameters, but got " + params .length );
337+ }
338+ }
339+
294340}
0 commit comments