Normand Briere
2019-10-01 e8908d5b90d44e43b9da885bc0202fb665a912d0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//package com.jmex.effects.particles;
 
import java.io.IOException;
 
import com.jme.renderer.ColorRGBA;
import com.jme.util.export.InputCapsule;
import com.jme.util.export.JMEExporter;
import com.jme.util.export.JMEImporter;
import com.jme.util.export.OutputCapsule;
import com.jme.util.export.Savable;
 
/**
 * <code>RampEntry</code> defines an entry for a ParticleAppearanceRamp.
 * 
 * @author Joshua Slack
 * @see ParticleAppearanceRamp
 */
public class RampEntry implements Savable {
 
    public static final float DEFAULT_OFFSET = 0.05f; // (5% of lifetime)
    public static final float DEFAULT_SIZE = -1; // special case -> negative = no size change at this entry
    public static final float DEFAULT_SPIN = Float.MAX_VALUE; // special case -> no spin change
    public static final float DEFAULT_MASS = Float.MAX_VALUE; // special case -> no mass change
    public static final ColorRGBA DEFAULT_COLOR = null; // special case -> no color change
 
    protected float offset = DEFAULT_OFFSET;
    protected ColorRGBA color = DEFAULT_COLOR; // no color change at this entry
    protected float size = DEFAULT_SIZE;
    protected float spin = DEFAULT_SPIN;
    protected float mass = DEFAULT_MASS;
 
    public RampEntry() {
    }
 
    /**
     * Construct new addition to color ramp
     * @param offset amount of time (as a percent of total lifetime) between the last appearance and this one.
     */
    public RampEntry(float offset) {
        setOffset(offset);
    }
 
    public ColorRGBA getColor() {
        return color;
    }
 
    public void setColor(ColorRGBA color) {
        this.color = color;
    }
    
    public boolean hasColorSet() {
        return color != DEFAULT_COLOR;
    }
 
    public float getSize() {
        return size;
    }
 
    public void setSize(float size) {
        this.size = size;
    }
    
    public boolean hasSizeSet() {
        return size != DEFAULT_SIZE;
    }
 
    public float getSpin() {
        return spin;
    }
 
    public void setSpin(float spin) {
        this.spin = spin;
    }
    
    public boolean hasSpinSet() {
        return spin != DEFAULT_SPIN;
    }
 
    public float getMass() {
        return mass;
    }
 
    public void setMass(float mass) {
        this.mass = mass;
    }
    
    public boolean hasMassSet() {
        return mass != DEFAULT_MASS;
    }
 
    public float getOffset() {
        return offset;
    }
 
    public void setOffset(float offset) {
        this.offset = offset;
    }
 
    public Class getClassTag() {
        return getClass();
    }
 
    public void read(JMEImporter im) throws IOException {
        InputCapsule capsule = im.getCapsule(this);
        offset = capsule.readFloat("offsetMS", DEFAULT_OFFSET);
        size = capsule.readFloat("size", DEFAULT_SIZE);
        spin = capsule.readFloat("spin", DEFAULT_SPIN);
        mass = capsule.readFloat("mass", DEFAULT_MASS);
        color = (ColorRGBA) capsule.readSavable("color", DEFAULT_COLOR);
    }
 
    public void write(JMEExporter ex) throws IOException {
        OutputCapsule capsule = ex.getCapsule(this);
        capsule.write(offset, "offsetMS", DEFAULT_OFFSET);
        capsule.write(size, "size", DEFAULT_SIZE);
        capsule.write(spin, "spin", DEFAULT_SPIN);
        capsule.write(mass, "mass", DEFAULT_MASS);
        capsule.write(color, "color", DEFAULT_COLOR);
    }
    
    private static String convColorToHex(ColorRGBA color) {
        if (color == null)
            return null;
        String sRed = Integer.toHexString((int)(color.r*255+.5f));
        if (sRed.length() == 1)
            sRed = "0" + sRed;
        String sGreen = Integer.toHexString((int)(color.g*255+.5f));
        if (sGreen.length() == 1)
            sGreen = "0" + sGreen;
        String sBlue = Integer.toHexString((int)(color.b*255+.5f));
        if (sBlue.length() == 1)
            sBlue = "0" + sBlue;
        return "#" + sRed + sGreen + sBlue;
    }
 
    @Override
    public String toString() {
        
        StringBuilder builder = new StringBuilder();
        if (offset > 0) {
            builder.append("prev+");
            builder.append((int)(offset*100));
            builder.append("% age...");
        }
        if (color != DEFAULT_COLOR) {
            builder.append("  color:");
            builder.append(convColorToHex(color).toUpperCase());
            builder.append(" a: ");
            builder.append((int)(color.a*100));
            builder.append("%");
        }
 
        if (size != DEFAULT_SIZE) {
            builder.append("  size: "+size);
        }
 
        if (mass != DEFAULT_MASS) {
            builder.append("  mass: "+spin);
        }
 
        if (spin != DEFAULT_SPIN) {
            builder.append("  spin: "+spin);
        }
        
        return builder.toString();
    }
}