浏览代码

Add java types to VarType and type checks to MatParam (#1797)

Riccardo Balbo 3 年之前
父节点
当前提交
97e83fbee3

+ 37 - 0
jme3-core/src/main/java/com/jme3/material/MatParam.java

@@ -39,6 +39,7 @@ import com.jme3.texture.Texture;
 import com.jme3.texture.Texture.WrapMode;
 
 import java.io.IOException;
+import java.util.Arrays;
 
 /**
  * Describes a material parameter. This is used for both defining a name and type
@@ -52,6 +53,7 @@ public class MatParam implements Savable, Cloneable {
     protected String name;
     protected String prefixedName;
     protected Object value;
+    protected boolean typeCheck = true;
 
     /**
      * Create a new material parameter. For internal use only.
@@ -73,6 +75,22 @@ public class MatParam implements Savable, Cloneable {
     protected MatParam() {
     }
 
+
+    public boolean isTypeCheckEnabled() {
+        return typeCheck;
+    }
+
+
+    /**
+     * Enable type check for this param.
+     * When type check is enabled a RuntimeException is thrown if 
+     * an object of the wrong type is passed to setValue.
+     * @param v (default = true)
+     */
+    public void setTypeCheckEnabled(boolean v) {
+        typeCheck = v;
+    }
+
     /**
      * Returns the material parameter type.
      *
@@ -130,6 +148,21 @@ public class MatParam implements Savable, Cloneable {
      * @param value the value of this material parameter.
      */
     public void setValue(Object value) {
+        if (isTypeCheckEnabled()) {
+            if (value != null && this.type != null && this.type.getJavaType().length != 0) {
+                boolean valid = false;
+                for (Class<?> jtype : this.type.getJavaType()) {
+                    if (jtype.isAssignableFrom(value.getClass())) {
+                        valid = true;
+                        break;
+                    }
+                }
+                if (!valid) {
+                    throw new RuntimeException("Trying to assign a value of type " + value.getClass() + " to " + this.getName() + " of type " + type.name() + ". Valid types are "
+                            + Arrays.deepToString(type.getJavaType()));
+                }
+            }
+        }
         this.value = value;
     }
 
@@ -323,6 +356,8 @@ When arrays can be inserted in J3M files
         } else if (value.getClass().isArray() && value instanceof Savable[]) {
             oc.write((Savable[]) value, "value_savable_array", null);
         }
+
+        oc.write(typeCheck, "typeCheck", true);
     }
 
     @Override
@@ -380,6 +415,8 @@ When arrays can be inserted in J3M files
                 value = ic.readSavable("value_savable", null);
                 break;
         }
+
+        typeCheck = ic.readBoolean("typeCheck", true);
     }
 
     @Override

+ 61 - 25
jme3-core/src/main/java/com/jme3/shader/VarType.java

@@ -30,49 +30,85 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 package com.jme3.shader;
-
+import com.jme3.math.*;
+import com.jme3.texture.*;
+import com.jme3.shader.BufferObject;
 public enum VarType {
 
-    Float("float"),
-    Vector2("vec2"),
-    Vector3("vec3"),
-    Vector4("vec4"),
+    Float("float",float.class,Float.class),
+    Vector2("vec2",Vector2f.class),
+    Vector3("vec3",Vector3f.class),
+    Vector4("vec4",Vector4f.class, ColorRGBA.class),
 
-    IntArray(true,false,"int"),
-    FloatArray(true,false,"float"),
-    Vector2Array(true,false,"vec2"),
-    Vector3Array(true,false,"vec3"),
-    Vector4Array(true,false,"vec4"),
+    IntArray(true,false,"int",int[].class,Integer[].class),
+    FloatArray(true,false,"float",float[].class,Float[].class),
+    Vector2Array(true,false,"vec2",Vector2f[].class),
+    Vector3Array(true,false,"vec3",Vector3f[].class),
+    Vector4Array(true,false,"vec4",Vector4f[].class),
 
-    Boolean("bool"),
+    Boolean("bool",Boolean.class,boolean.class),
 
-    Matrix3(true,false,"mat3"),
-    Matrix4(true,false,"mat4"),
+    Matrix3(true,false,"mat3",Matrix3f.class),
+    Matrix4(true,false,"mat4",Matrix4f.class),
 
-    Matrix3Array(true,false,"mat3"),
-    Matrix4Array(true,false,"mat4"),
+    Matrix3Array(true,false,"mat3",Matrix3f[].class),
+    Matrix4Array(true,false,"mat4",Matrix4f[].class),
     
-    TextureBuffer(false,true,"sampler1D|sampler1DShadow"),
-    Texture2D(false,true,"sampler2D|sampler2DShadow"),
-    Texture3D(false,true,"sampler3D"),
-    TextureArray(false,true,"sampler2DArray|sampler2DArrayShadow"),
-    TextureCubeMap(false,true,"samplerCube"),
-    Int("int"),
-    BufferObject(false, false, "custom");
+    TextureBuffer(false,true,"sampler1D|sampler1DShadow"), 
+    Texture2D(false,true,"sampler2D|sampler2DShadow",Texture2D.class,Texture.class),
+    Texture3D(false,true,"sampler3D",Texture3D.class,Texture.class),
+    TextureArray(false,true,"sampler2DArray|sampler2DArrayShadow",TextureArray.class,Texture.class),
+    TextureCubeMap(false,true,"samplerCube",TextureCubeMap.class,Texture.class),
+    Int("int",int.class,Integer.class),
+    BufferObject(false, false, "custom", BufferObject.class);
+
 
     private boolean usesMultiData = false;
     private boolean textureType = false;
     final private String glslType;
-
+    private Class<?> javaTypes[];
     
-    VarType(String glslType){
+    VarType(String glslType,Class<?> ...javaTypes){
         this.glslType = glslType;
+        if (javaTypes != null) {
+            this.javaTypes = javaTypes;
+        } else {
+            this.javaTypes = new Class<?>[0];
+        }
+        
     }
 
-    VarType(boolean multiData, boolean textureType,String glslType){
+
+    VarType(boolean multiData, boolean textureType, String glslType, Class<?>... javaTypes) {
         usesMultiData = multiData;
         this.textureType = textureType;
         this.glslType = glslType;
+        if (javaTypes != null) {
+            this.javaTypes = javaTypes;
+        } else {
+            this.javaTypes = new Class<?>[0];
+        }
+    }
+    
+    /**
+     * Check if the passed object is of a type mapped to this VarType
+     * @param o Object to check
+     * @return true if the object type is mapped to this VarType
+     */
+    public boolean isOfType(Object o){
+        for(Class<?> c : javaTypes){
+            if(c.isAssignableFrom(o.getClass()))return true;
+        }
+        return false;
+    }
+    
+
+    /**
+     * Get the java types mapped to this VarType
+     * @return an array of classes mapped to this VarType
+     */
+    public Class<?>[] getJavaType(){
+        return javaTypes;
     }
 
     public boolean isTextureType() {