Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/particles/ParticleIO.java
1457 views
1
package org.newdawn.slick.particles;
2
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileOutputStream;
6
import java.io.IOException;
7
import java.io.InputStream;
8
import java.io.OutputStream;
9
import java.io.OutputStreamWriter;
10
import java.util.ArrayList;
11
12
import javax.xml.parsers.DocumentBuilder;
13
import javax.xml.parsers.DocumentBuilderFactory;
14
import javax.xml.transform.OutputKeys;
15
import javax.xml.transform.Result;
16
import javax.xml.transform.Transformer;
17
import javax.xml.transform.TransformerFactory;
18
import javax.xml.transform.dom.DOMSource;
19
import javax.xml.transform.stream.StreamResult;
20
21
import org.newdawn.slick.Color;
22
import org.newdawn.slick.geom.Vector2f;
23
import org.newdawn.slick.particles.ConfigurableEmitter.ColorRecord;
24
import org.newdawn.slick.particles.ConfigurableEmitter.LinearInterpolator;
25
import org.newdawn.slick.particles.ConfigurableEmitter.RandomValue;
26
import org.newdawn.slick.particles.ConfigurableEmitter.SimpleValue;
27
import org.newdawn.slick.util.Log;
28
import org.newdawn.slick.util.ResourceLoader;
29
import org.w3c.dom.Document;
30
import org.w3c.dom.Element;
31
import org.w3c.dom.NodeList;
32
33
/**
34
* Utility methods to (de)serialize ConfigureEmitters to and from XML
35
*
36
* @author kevin
37
*/
38
public class ParticleIO {
39
40
/**
41
* Load a set of configured emitters into a single system
42
*
43
* @param ref
44
* The reference to the XML file (file or classpath)
45
* @param mask
46
* @return A configured particle system
47
* @throws IOException
48
* Indicates a failure to find, read or parse the XML file
49
*/
50
public static ParticleSystem loadConfiguredSystem(String ref, Color mask)
51
throws IOException {
52
return loadConfiguredSystem(ResourceLoader.getResourceAsStream(ref),
53
null, null, mask);
54
}
55
56
/**
57
* Load a set of configured emitters into a single system
58
*
59
* @param ref
60
* The reference to the XML file (file or classpath)
61
* @return A configured particle system
62
* @throws IOException
63
* Indicates a failure to find, read or parse the XML file
64
*/
65
public static ParticleSystem loadConfiguredSystem(String ref)
66
throws IOException {
67
return loadConfiguredSystem(ResourceLoader.getResourceAsStream(ref),
68
null, null, null);
69
}
70
71
/**
72
* Load a set of configured emitters into a single system
73
*
74
* @param ref
75
* The XML file to read
76
* @return A configured particle system
77
* @throws IOException
78
* Indicates a failure to find, read or parse the XML file
79
*/
80
public static ParticleSystem loadConfiguredSystem(File ref)
81
throws IOException {
82
return loadConfiguredSystem(new FileInputStream(ref), null, null, null);
83
}
84
85
/**
86
* Load a set of configured emitters into a single system
87
*
88
* @param ref
89
* The stream to read the XML from
90
* @param mask The mask used to make the particle image transparent
91
* @return A configured particle system
92
* @throws IOException
93
* Indicates a failure to find, read or parse the XML file
94
*/
95
public static ParticleSystem loadConfiguredSystem(InputStream ref, Color mask)
96
throws IOException {
97
return loadConfiguredSystem(ref, null, null, mask);
98
}
99
100
/**
101
* Load a set of configured emitters into a single system
102
*
103
* @param ref
104
* The stream to read the XML from
105
* @return A configured particle system
106
* @throws IOException
107
* Indicates a failure to find, read or parse the XML file
108
*/
109
public static ParticleSystem loadConfiguredSystem(InputStream ref)
110
throws IOException {
111
return loadConfiguredSystem(ref, null, null, null);
112
}
113
114
/**
115
* Load a set of configured emitters into a single system
116
*
117
* @param ref
118
* The reference to the XML file (file or classpath)
119
* @return A configured particle system
120
* @param factory
121
* The factory used to create the emitter than will be poulated
122
* with loaded data.
123
* @throws IOException
124
* Indicates a failure to find, read or parse the XML file
125
*/
126
public static ParticleSystem loadConfiguredSystem(String ref,
127
ConfigurableEmitterFactory factory) throws IOException {
128
return loadConfiguredSystem(ResourceLoader.getResourceAsStream(ref),
129
factory, null, null);
130
}
131
132
/**
133
* Load a set of configured emitters into a single system
134
*
135
* @param ref
136
* The XML file to read
137
* @return A configured particle system
138
* @param factory
139
* The factory used to create the emitter than will be poulated
140
* with loaded data.
141
* @throws IOException
142
* Indicates a failure to find, read or parse the XML file
143
*/
144
public static ParticleSystem loadConfiguredSystem(File ref,
145
ConfigurableEmitterFactory factory) throws IOException {
146
return loadConfiguredSystem(new FileInputStream(ref), factory, null, null);
147
}
148
149
/**
150
* Load a set of configured emitters into a single system
151
*
152
* @param ref
153
* The stream to read the XML from
154
* @return A configured particle system
155
* @param factory
156
* The factory used to create the emitter than will be poulated
157
* with loaded data.
158
* @throws IOException
159
* Indicates a failure to find, read or parse the XML file
160
*/
161
public static ParticleSystem loadConfiguredSystem(InputStream ref,
162
ConfigurableEmitterFactory factory) throws IOException {
163
return loadConfiguredSystem(ref, factory, null, null);
164
}
165
166
/**
167
* Load a set of configured emitters into a single system
168
*
169
* @param ref
170
* The stream to read the XML from
171
* @param factory
172
* The factory used to create the emitter than will be poulated
173
* with loaded data.
174
* @param system The particle system that will be loaded into
175
* @param mask The mask used to make the image background transparent
176
* @return A configured particle system
177
* @throws IOException
178
* Indicates a failure to find, read or parse the XML file
179
*/
180
public static ParticleSystem loadConfiguredSystem(InputStream ref,
181
ConfigurableEmitterFactory factory, ParticleSystem system, Color mask) throws IOException {
182
if (factory == null) {
183
factory = new ConfigurableEmitterFactory() {
184
public ConfigurableEmitter createEmitter(String name) {
185
return new ConfigurableEmitter(name);
186
}
187
};
188
}
189
try {
190
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
191
.newDocumentBuilder();
192
Document document = builder.parse(ref);
193
194
Element element = document.getDocumentElement();
195
if (!element.getNodeName().equals("system")) {
196
throw new IOException("Not a particle system file");
197
}
198
199
if (system == null) {
200
system = new ParticleSystem("org/newdawn/slick/data/particle.tga",
201
2000, mask);
202
}
203
boolean additive = "true".equals(element.getAttribute("additive"));
204
if (additive) {
205
system.setBlendingMode(ParticleSystem.BLEND_ADDITIVE);
206
} else {
207
system.setBlendingMode(ParticleSystem.BLEND_COMBINE);
208
}
209
boolean points = "true".equals(element.getAttribute("points"));
210
system.setUsePoints(points);
211
212
NodeList list = element.getElementsByTagName("emitter");
213
for (int i = 0; i < list.getLength(); i++) {
214
Element em = (Element) list.item(i);
215
ConfigurableEmitter emitter = factory.createEmitter("new");
216
elementToEmitter(em, emitter);
217
218
system.addEmitter(emitter);
219
}
220
221
system.setRemoveCompletedEmitters(false);
222
return system;
223
} catch (IOException e) {
224
Log.error(e);
225
throw e;
226
} catch (Exception e) {
227
Log.error(e);
228
throw new IOException("Unable to load particle system config");
229
}
230
}
231
232
/**
233
* Save a particle system with only ConfigurableEmitters in to an XML file
234
*
235
* @param file
236
* The file to save to
237
* @param system
238
* The system to store
239
* @throws IOException
240
* Indicates a failure to save or encode the system XML.
241
*/
242
public static void saveConfiguredSystem(File file, ParticleSystem system)
243
throws IOException {
244
saveConfiguredSystem(new FileOutputStream(file), system);
245
}
246
247
/**
248
* Save a particle system with only ConfigurableEmitters in to an XML file
249
*
250
* @param out
251
* The location to which we'll save
252
* @param system
253
* The system to store
254
* @throws IOException
255
* Indicates a failure to save or encode the system XML.
256
*/
257
public static void saveConfiguredSystem(OutputStream out,
258
ParticleSystem system) throws IOException {
259
try {
260
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
261
.newDocumentBuilder();
262
Document document = builder.newDocument();
263
264
Element root = document.createElement("system");
265
root
266
.setAttribute(
267
"additive",
268
""
269
+ (system.getBlendingMode() == ParticleSystem.BLEND_ADDITIVE));
270
root.setAttribute("points", "" + (system.usePoints()));
271
272
document.appendChild(root);
273
for (int i = 0; i < system.getEmitterCount(); i++) {
274
ParticleEmitter current = system.getEmitter(i);
275
if (current instanceof ConfigurableEmitter) {
276
Element element = emitterToElement(document,
277
(ConfigurableEmitter) current);
278
root.appendChild(element);
279
} else {
280
throw new RuntimeException(
281
"Only ConfigurableEmitter instances can be stored");
282
}
283
}
284
285
Result result = new StreamResult(new OutputStreamWriter(out,
286
"utf-8"));
287
DOMSource source = new DOMSource(document);
288
289
TransformerFactory factory = TransformerFactory.newInstance();
290
Transformer xformer = factory.newTransformer();
291
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
292
293
xformer.transform(source, result);
294
} catch (Exception e) {
295
Log.error(e);
296
throw new IOException("Unable to save configured particle system");
297
}
298
}
299
300
/**
301
* Load a single emitter from an XML file
302
*
303
* @param ref
304
* The reference to the emitter XML file to load (classpath or
305
* file)
306
* @return The configured emitter
307
* @throws IOException
308
* Indicates a failure to find, read or parse the XML file
309
*/
310
public static ConfigurableEmitter loadEmitter(String ref)
311
throws IOException {
312
return loadEmitter(ResourceLoader.getResourceAsStream(ref), null);
313
}
314
315
/**
316
* Load a single emitter from an XML file
317
*
318
* @param ref
319
* The XML file to read
320
* @return The configured emitter
321
* @throws IOException
322
* Indicates a failure to find, read or parse the XML file
323
*/
324
public static ConfigurableEmitter loadEmitter(File ref) throws IOException {
325
return loadEmitter(new FileInputStream(ref), null);
326
327
}
328
329
/**
330
* Load a single emitter from an XML file
331
*
332
* @param ref
333
* The stream to read the XML from
334
* @return The configured emitter
335
* @throws IOException
336
* Indicates a failure to find, read or parse the XML file
337
*/
338
public static ConfigurableEmitter loadEmitter(InputStream ref)
339
throws IOException {
340
return loadEmitter(ref, null);
341
}
342
343
/**
344
* Load a single emitter from an XML file
345
*
346
* @param ref
347
* The reference to the emitter XML file to load (classpath or
348
* file)
349
* @return The configured emitter
350
* @param factory
351
* The factory used to create the emitter than will be poulated
352
* with loaded data.
353
* @throws IOException
354
* Indicates a failure to find, read or parse the XML file
355
*/
356
public static ConfigurableEmitter loadEmitter(String ref,
357
ConfigurableEmitterFactory factory) throws IOException {
358
return loadEmitter(ResourceLoader.getResourceAsStream(ref), factory);
359
}
360
361
/**
362
* Load a single emitter from an XML file
363
*
364
* @param ref
365
* The XML file to read
366
* @return The configured emitter
367
* @param factory
368
* The factory used to create the emitter than will be poulated
369
* with loaded data.
370
* @throws IOException
371
* Indicates a failure to find, read or parse the XML file
372
*/
373
public static ConfigurableEmitter loadEmitter(File ref,
374
ConfigurableEmitterFactory factory) throws IOException {
375
return loadEmitter(new FileInputStream(ref), factory);
376
377
}
378
379
/**
380
* Load a single emitter from an XML file
381
*
382
* @param ref
383
* The stream to read the XML from
384
* @param factory
385
* The factory used to create the emitter than will be poulated
386
* with loaded data.
387
* @return The configured emitter
388
* @throws IOException
389
* Indicates a failure to find, read or parse the XML file
390
*/
391
public static ConfigurableEmitter loadEmitter(InputStream ref,
392
ConfigurableEmitterFactory factory) throws IOException {
393
if (factory == null) {
394
factory = new ConfigurableEmitterFactory() {
395
public ConfigurableEmitter createEmitter(String name) {
396
return new ConfigurableEmitter(name);
397
}
398
};
399
}
400
try {
401
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
402
.newDocumentBuilder();
403
Document document = builder.parse(ref);
404
405
if (!document.getDocumentElement().getNodeName().equals("emitter")) {
406
throw new IOException("Not a particle emitter file");
407
}
408
409
ConfigurableEmitter emitter = factory.createEmitter("new");
410
elementToEmitter(document.getDocumentElement(), emitter);
411
412
return emitter;
413
} catch (IOException e) {
414
Log.error(e);
415
throw e;
416
} catch (Exception e) {
417
Log.error(e);
418
throw new IOException("Unable to load emitter");
419
}
420
}
421
422
/**
423
* Save a single emitter to the XML file
424
*
425
* @param file
426
* The file to save the emitter to
427
* @param emitter
428
* The emitter to store to the XML file
429
* @throws IOException
430
* Indicates a failure to write or encode the XML
431
*/
432
public static void saveEmitter(File file, ConfigurableEmitter emitter)
433
throws IOException {
434
saveEmitter(new FileOutputStream(file), emitter);
435
}
436
437
/**
438
* Save a single emitter to the XML file
439
*
440
* @param out
441
* The location to which we should save
442
* @param emitter
443
* The emitter to store to the XML file
444
* @throws IOException
445
* Indicates a failure to write or encode the XML
446
*/
447
public static void saveEmitter(OutputStream out, ConfigurableEmitter emitter)
448
throws IOException {
449
try {
450
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
451
.newDocumentBuilder();
452
Document document = builder.newDocument();
453
454
document.appendChild(emitterToElement(document, emitter));
455
Result result = new StreamResult(new OutputStreamWriter(out,
456
"utf-8"));
457
DOMSource source = new DOMSource(document);
458
459
TransformerFactory factory = TransformerFactory.newInstance();
460
Transformer xformer = factory.newTransformer();
461
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
462
463
xformer.transform(source, result);
464
} catch (Exception e) {
465
Log.error(e);
466
throw new IOException("Failed to save emitter");
467
}
468
}
469
470
/**
471
* Get the first child named as specified from the passed XML element
472
*
473
* @param element
474
* The element whose children are interogated
475
* @param name
476
* The name of the element to retrieve
477
* @return The requested element
478
*/
479
private static Element getFirstNamedElement(Element element, String name) {
480
NodeList list = element.getElementsByTagName(name);
481
if (list.getLength() == 0) {
482
return null;
483
}
484
485
return (Element) list.item(0);
486
}
487
488
/**
489
* Convert from an XML element to an configured emitter
490
*
491
* @param element
492
* The XML element to convert
493
* @param emitter
494
* The emitter that will be configured based on the XML
495
*/
496
private static void elementToEmitter(Element element,
497
ConfigurableEmitter emitter) {
498
emitter.name = element.getAttribute("name");
499
emitter.setImageName(element.getAttribute("imageName"));
500
501
String renderType = element.getAttribute("renderType");
502
emitter.usePoints = Particle.INHERIT_POINTS;
503
if (renderType.equals("quads")) {
504
emitter.usePoints = Particle.USE_QUADS;
505
}
506
if (renderType.equals("points")) {
507
emitter.usePoints = Particle.USE_POINTS;
508
}
509
510
String useOriented = element.getAttribute("useOriented");
511
if (useOriented != null)
512
emitter.useOriented = "true".equals(useOriented);
513
514
String useAdditive = element.getAttribute("useAdditive");
515
if (useAdditive != null)
516
emitter.useAdditive = "true".equals(useAdditive);
517
518
parseRangeElement(getFirstNamedElement(element, "spawnInterval"),
519
emitter.spawnInterval);
520
parseRangeElement(getFirstNamedElement(element, "spawnCount"),
521
emitter.spawnCount);
522
parseRangeElement(getFirstNamedElement(element, "initialLife"),
523
emitter.initialLife);
524
parseRangeElement(getFirstNamedElement(element, "initialSize"),
525
emitter.initialSize);
526
parseRangeElement(getFirstNamedElement(element, "xOffset"),
527
emitter.xOffset);
528
parseRangeElement(getFirstNamedElement(element, "yOffset"),
529
emitter.yOffset);
530
parseRangeElement(getFirstNamedElement(element, "initialDistance"),
531
emitter.initialDistance);
532
parseRangeElement(getFirstNamedElement(element, "speed"), emitter.speed);
533
parseRangeElement(getFirstNamedElement(element, "length"),
534
emitter.length);
535
parseRangeElement(getFirstNamedElement(element, "emitCount"),
536
emitter.emitCount);
537
538
parseValueElement(getFirstNamedElement(element, "spread"),
539
emitter.spread);
540
parseValueElement(getFirstNamedElement(element, "angularOffset"),
541
emitter.angularOffset);
542
parseValueElement(getFirstNamedElement(element, "growthFactor"),
543
emitter.growthFactor);
544
parseValueElement(getFirstNamedElement(element, "gravityFactor"),
545
emitter.gravityFactor);
546
parseValueElement(getFirstNamedElement(element, "windFactor"),
547
emitter.windFactor);
548
parseValueElement(getFirstNamedElement(element, "startAlpha"),
549
emitter.startAlpha);
550
parseValueElement(getFirstNamedElement(element, "endAlpha"),
551
emitter.endAlpha);
552
parseValueElement(getFirstNamedElement(element, "alpha"), emitter.alpha);
553
parseValueElement(getFirstNamedElement(element, "size"), emitter.size);
554
parseValueElement(getFirstNamedElement(element, "velocity"),
555
emitter.velocity);
556
parseValueElement(getFirstNamedElement(element, "scaleY"),
557
emitter.scaleY);
558
559
Element color = getFirstNamedElement(element, "color");
560
NodeList steps = color.getElementsByTagName("step");
561
emitter.colors.clear();
562
for (int i = 0; i < steps.getLength(); i++) {
563
Element step = (Element) steps.item(i);
564
float offset = Float.parseFloat(step.getAttribute("offset"));
565
float r = Float.parseFloat(step.getAttribute("r"));
566
float g = Float.parseFloat(step.getAttribute("g"));
567
float b = Float.parseFloat(step.getAttribute("b"));
568
569
emitter.addColorPoint(offset, new Color(r, g, b, 1));
570
}
571
572
// generate new random play length
573
emitter.replay();
574
}
575
576
/**
577
* Convert from an emitter to a XML element description
578
*
579
* @param document
580
* The document the element will be part of
581
* @param emitter
582
* The emitter to convert
583
* @return The XML element based on the configured emitter
584
*/
585
private static Element emitterToElement(Document document,
586
ConfigurableEmitter emitter) {
587
Element root = document.createElement("emitter");
588
root.setAttribute("name", emitter.name);
589
root.setAttribute("imageName", emitter.imageName == null ? ""
590
: emitter.imageName);
591
root
592
.setAttribute("useOriented", emitter.useOriented ? "true"
593
: "false");
594
root
595
.setAttribute("useAdditive", emitter.useAdditive ? "true"
596
: "false");
597
598
if (emitter.usePoints == Particle.INHERIT_POINTS) {
599
root.setAttribute("renderType", "inherit");
600
}
601
if (emitter.usePoints == Particle.USE_POINTS) {
602
root.setAttribute("renderType", "points");
603
}
604
if (emitter.usePoints == Particle.USE_QUADS) {
605
root.setAttribute("renderType", "quads");
606
}
607
608
root.appendChild(createRangeElement(document, "spawnInterval",
609
emitter.spawnInterval));
610
root.appendChild(createRangeElement(document, "spawnCount",
611
emitter.spawnCount));
612
root.appendChild(createRangeElement(document, "initialLife",
613
emitter.initialLife));
614
root.appendChild(createRangeElement(document, "initialSize",
615
emitter.initialSize));
616
root.appendChild(createRangeElement(document, "xOffset",
617
emitter.xOffset));
618
root.appendChild(createRangeElement(document, "yOffset",
619
emitter.yOffset));
620
root.appendChild(createRangeElement(document, "initialDistance",
621
emitter.initialDistance));
622
root.appendChild(createRangeElement(document, "speed", emitter.speed));
623
root
624
.appendChild(createRangeElement(document, "length",
625
emitter.length));
626
root.appendChild(createRangeElement(document, "emitCount",
627
emitter.emitCount));
628
629
root
630
.appendChild(createValueElement(document, "spread",
631
emitter.spread));
632
root.appendChild(createValueElement(document, "angularOffset",
633
emitter.angularOffset));
634
root.appendChild(createValueElement(document, "growthFactor",
635
emitter.growthFactor));
636
root.appendChild(createValueElement(document, "gravityFactor",
637
emitter.gravityFactor));
638
root.appendChild(createValueElement(document, "windFactor",
639
emitter.windFactor));
640
root.appendChild(createValueElement(document, "startAlpha",
641
emitter.startAlpha));
642
root.appendChild(createValueElement(document, "endAlpha",
643
emitter.endAlpha));
644
root.appendChild(createValueElement(document, "alpha", emitter.alpha));
645
root.appendChild(createValueElement(document, "size", emitter.size));
646
root.appendChild(createValueElement(document, "velocity",
647
emitter.velocity));
648
root
649
.appendChild(createValueElement(document, "scaleY",
650
emitter.scaleY));
651
652
Element color = document.createElement("color");
653
ArrayList list = emitter.colors;
654
for (int i = 0; i < list.size(); i++) {
655
ColorRecord record = (ColorRecord) list.get(i);
656
Element step = document.createElement("step");
657
step.setAttribute("offset", "" + record.pos);
658
step.setAttribute("r", "" + record.col.r);
659
step.setAttribute("g", "" + record.col.g);
660
step.setAttribute("b", "" + record.col.b);
661
662
color.appendChild(step);
663
}
664
665
root.appendChild(color);
666
667
return root;
668
}
669
670
/**
671
* Create an XML element based on a configured range
672
*
673
* @param document
674
* The document the element will be part of
675
* @param name
676
* The name to give the new element
677
* @param range
678
* The configured range
679
* @return A configured XML element on the range
680
*/
681
private static Element createRangeElement(Document document, String name,
682
ConfigurableEmitter.Range range) {
683
Element element = document.createElement(name);
684
element.setAttribute("min", "" + range.getMin());
685
element.setAttribute("max", "" + range.getMax());
686
element.setAttribute("enabled", "" + range.isEnabled());
687
688
return element;
689
}
690
691
/**
692
* Create an XML element based on a configured value
693
*
694
* @param document
695
* The document the element will be part of
696
* @param name
697
* The name to give the new element
698
* @param value
699
* The configured value
700
* @return A configure XML element based on the value
701
*/
702
private static Element createValueElement(Document document, String name,
703
ConfigurableEmitter.Value value) {
704
Element element = document.createElement(name);
705
706
// void: now writes the value type
707
if (value instanceof SimpleValue) {
708
element.setAttribute("type", "simple");
709
element.setAttribute("value", "" + value.getValue(0));
710
} else if (value instanceof RandomValue) {
711
element.setAttribute("type", "random");
712
element
713
.setAttribute("value", ""
714
+ ((RandomValue) value).getValue());
715
} else if (value instanceof LinearInterpolator) {
716
element.setAttribute("type", "linear");
717
element.setAttribute("min", ""
718
+ ((LinearInterpolator) value).getMin());
719
element.setAttribute("max", ""
720
+ ((LinearInterpolator) value).getMax());
721
element.setAttribute("active", ""
722
+ ((LinearInterpolator) value).isActive());
723
724
ArrayList curve = ((LinearInterpolator) value).getCurve();
725
for (int i = 0; i < curve.size(); i++) {
726
Vector2f point = (Vector2f) curve.get(i);
727
728
Element pointElement = document.createElement("point");
729
pointElement.setAttribute("x", "" + point.x);
730
pointElement.setAttribute("y", "" + point.y);
731
732
element.appendChild(pointElement);
733
}
734
} else {
735
Log.warn("unkown value type ignored: " + value.getClass());
736
}
737
738
return element;
739
}
740
741
/**
742
* Parse an XML element into a configured range
743
*
744
* @param element
745
* The XML element to parse
746
* @param range
747
* The range to configure based on the XML
748
*/
749
private static void parseRangeElement(Element element,
750
ConfigurableEmitter.Range range) {
751
if (element == null) {
752
return;
753
}
754
range.setMin(Float.parseFloat(element.getAttribute("min")));
755
range.setMax(Float.parseFloat(element.getAttribute("max")));
756
range.setEnabled("true".equals(element.getAttribute("enabled")));
757
}
758
759
/**
760
* Parse an XML element into a configured value
761
*
762
* @param element
763
* The XML element to parse
764
* @param value
765
* The value to configure based on the XML
766
*/
767
private static void parseValueElement(Element element,
768
ConfigurableEmitter.Value value) {
769
if (element == null) {
770
return;
771
}
772
773
String type = element.getAttribute("type");
774
String v = element.getAttribute("value");
775
776
if (type == null || type.length() == 0) {
777
// support for old style which did not write the type
778
if (value instanceof SimpleValue) {
779
((SimpleValue) value).setValue(Float.parseFloat(v));
780
} else if (value instanceof RandomValue) {
781
((RandomValue) value).setValue(Float.parseFloat(v));
782
} else {
783
Log.warn("problems reading element, skipping: " + element);
784
}
785
} else {
786
// type given: this is the new style
787
if (type.equals("simple")) {
788
((SimpleValue) value).setValue(Float.parseFloat(v));
789
} else if (type.equals("random")) {
790
((RandomValue) value).setValue(Float.parseFloat(v));
791
} else if (type.equals("linear")) {
792
String min = element.getAttribute("min");
793
String max = element.getAttribute("max");
794
String active = element.getAttribute("active");
795
796
NodeList points = element.getElementsByTagName("point");
797
798
ArrayList curve = new ArrayList();
799
for (int i = 0; i < points.getLength(); i++) {
800
Element point = (Element) points.item(i);
801
802
float x = Float.parseFloat(point.getAttribute("x"));
803
float y = Float.parseFloat(point.getAttribute("y"));
804
805
curve.add(new Vector2f(x, y));
806
}
807
808
((LinearInterpolator) value).setCurve(curve);
809
((LinearInterpolator) value).setMin(Integer.parseInt(min));
810
((LinearInterpolator) value).setMax(Integer.parseInt(max));
811
((LinearInterpolator) value).setActive("true".equals(active));
812
} else {
813
Log.warn("unkown type detected: " + type);
814
}
815
}
816
}
817
}
818
819