Skip to content

Commit 97e83fb

Browse files
authored
Add java types to VarType and type checks to MatParam (#1797)
1 parent 488d9fa commit 97e83fb

File tree

2 files changed

+98
-25
lines changed

2 files changed

+98
-25
lines changed

jme3-core/src/main/java/com/jme3/material/MatParam.java

+37
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.jme3.texture.Texture.WrapMode;
4040

4141
import java.io.IOException;
42+
import java.util.Arrays;
4243

4344
/**
4445
* Describes a material parameter. This is used for both defining a name and type
@@ -52,6 +53,7 @@ public class MatParam implements Savable, Cloneable {
5253
protected String name;
5354
protected String prefixedName;
5455
protected Object value;
56+
protected boolean typeCheck = true;
5557

5658
/**
5759
* Create a new material parameter. For internal use only.
@@ -73,6 +75,22 @@ public MatParam(VarType type, String name, Object value) {
7375
protected MatParam() {
7476
}
7577

78+
79+
public boolean isTypeCheckEnabled() {
80+
return typeCheck;
81+
}
82+
83+
84+
/**
85+
* Enable type check for this param.
86+
* When type check is enabled a RuntimeException is thrown if
87+
* an object of the wrong type is passed to setValue.
88+
* @param v (default = true)
89+
*/
90+
public void setTypeCheckEnabled(boolean v) {
91+
typeCheck = v;
92+
}
93+
7694
/**
7795
* Returns the material parameter type.
7896
*
@@ -130,6 +148,21 @@ public Object getValue() {
130148
* @param value the value of this material parameter.
131149
*/
132150
public void setValue(Object value) {
151+
if (isTypeCheckEnabled()) {
152+
if (value != null && this.type != null && this.type.getJavaType().length != 0) {
153+
boolean valid = false;
154+
for (Class<?> jtype : this.type.getJavaType()) {
155+
if (jtype.isAssignableFrom(value.getClass())) {
156+
valid = true;
157+
break;
158+
}
159+
}
160+
if (!valid) {
161+
throw new RuntimeException("Trying to assign a value of type " + value.getClass() + " to " + this.getName() + " of type " + type.name() + ". Valid types are "
162+
+ Arrays.deepToString(type.getJavaType()));
163+
}
164+
}
165+
}
133166
this.value = value;
134167
}
135168

@@ -323,6 +356,8 @@ public void write(JmeExporter ex) throws IOException {
323356
} else if (value.getClass().isArray() && value instanceof Savable[]) {
324357
oc.write((Savable[]) value, "value_savable_array", null);
325358
}
359+
360+
oc.write(typeCheck, "typeCheck", true);
326361
}
327362

328363
@Override
@@ -380,6 +415,8 @@ public void read(JmeImporter im) throws IOException {
380415
value = ic.readSavable("value_savable", null);
381416
break;
382417
}
418+
419+
typeCheck = ic.readBoolean("typeCheck", true);
383420
}
384421

385422
@Override

jme3-core/src/main/java/com/jme3/shader/VarType.java

+61-25
Original file line numberDiff line numberDiff line change
@@ -30,49 +30,85 @@
3030
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131
*/
3232
package com.jme3.shader;
33-
33+
import com.jme3.math.*;
34+
import com.jme3.texture.*;
35+
import com.jme3.shader.BufferObject;
3436
public enum VarType {
3537

36-
Float("float"),
37-
Vector2("vec2"),
38-
Vector3("vec3"),
39-
Vector4("vec4"),
38+
Float("float",float.class,Float.class),
39+
Vector2("vec2",Vector2f.class),
40+
Vector3("vec3",Vector3f.class),
41+
Vector4("vec4",Vector4f.class, ColorRGBA.class),
4042

41-
IntArray(true,false,"int"),
42-
FloatArray(true,false,"float"),
43-
Vector2Array(true,false,"vec2"),
44-
Vector3Array(true,false,"vec3"),
45-
Vector4Array(true,false,"vec4"),
43+
IntArray(true,false,"int",int[].class,Integer[].class),
44+
FloatArray(true,false,"float",float[].class,Float[].class),
45+
Vector2Array(true,false,"vec2",Vector2f[].class),
46+
Vector3Array(true,false,"vec3",Vector3f[].class),
47+
Vector4Array(true,false,"vec4",Vector4f[].class),
4648

47-
Boolean("bool"),
49+
Boolean("bool",Boolean.class,boolean.class),
4850

49-
Matrix3(true,false,"mat3"),
50-
Matrix4(true,false,"mat4"),
51+
Matrix3(true,false,"mat3",Matrix3f.class),
52+
Matrix4(true,false,"mat4",Matrix4f.class),
5153

52-
Matrix3Array(true,false,"mat3"),
53-
Matrix4Array(true,false,"mat4"),
54+
Matrix3Array(true,false,"mat3",Matrix3f[].class),
55+
Matrix4Array(true,false,"mat4",Matrix4f[].class),
5456

55-
TextureBuffer(false,true,"sampler1D|sampler1DShadow"),
56-
Texture2D(false,true,"sampler2D|sampler2DShadow"),
57-
Texture3D(false,true,"sampler3D"),
58-
TextureArray(false,true,"sampler2DArray|sampler2DArrayShadow"),
59-
TextureCubeMap(false,true,"samplerCube"),
60-
Int("int"),
61-
BufferObject(false, false, "custom");
57+
TextureBuffer(false,true,"sampler1D|sampler1DShadow"),
58+
Texture2D(false,true,"sampler2D|sampler2DShadow",Texture2D.class,Texture.class),
59+
Texture3D(false,true,"sampler3D",Texture3D.class,Texture.class),
60+
TextureArray(false,true,"sampler2DArray|sampler2DArrayShadow",TextureArray.class,Texture.class),
61+
TextureCubeMap(false,true,"samplerCube",TextureCubeMap.class,Texture.class),
62+
Int("int",int.class,Integer.class),
63+
BufferObject(false, false, "custom", BufferObject.class);
64+
6265

6366
private boolean usesMultiData = false;
6467
private boolean textureType = false;
6568
final private String glslType;
66-
69+
private Class<?> javaTypes[];
6770

68-
VarType(String glslType){
71+
VarType(String glslType,Class<?> ...javaTypes){
6972
this.glslType = glslType;
73+
if (javaTypes != null) {
74+
this.javaTypes = javaTypes;
75+
} else {
76+
this.javaTypes = new Class<?>[0];
77+
}
78+
7079
}
7180

72-
VarType(boolean multiData, boolean textureType,String glslType){
81+
82+
VarType(boolean multiData, boolean textureType, String glslType, Class<?>... javaTypes) {
7383
usesMultiData = multiData;
7484
this.textureType = textureType;
7585
this.glslType = glslType;
86+
if (javaTypes != null) {
87+
this.javaTypes = javaTypes;
88+
} else {
89+
this.javaTypes = new Class<?>[0];
90+
}
91+
}
92+
93+
/**
94+
* Check if the passed object is of a type mapped to this VarType
95+
* @param o Object to check
96+
* @return true if the object type is mapped to this VarType
97+
*/
98+
public boolean isOfType(Object o){
99+
for(Class<?> c : javaTypes){
100+
if(c.isAssignableFrom(o.getClass()))return true;
101+
}
102+
return false;
103+
}
104+
105+
106+
/**
107+
* Get the java types mapped to this VarType
108+
* @return an array of classes mapped to this VarType
109+
*/
110+
public Class<?>[] getJavaType(){
111+
return javaTypes;
76112
}
77113

78114
public boolean isTextureType() {

0 commit comments

Comments
 (0)