OSDN Git Service

Allow overwriting material attributes.
authorXoppa <contact@xoppa.nl>
Sun, 7 Apr 2013 00:44:26 +0000 (02:44 +0200)
committerXoppa <contact@xoppa.nl>
Sun, 7 Apr 2013 00:44:26 +0000 (02:44 +0200)
gdx/src/com/badlogic/gdx/graphics/g3d/Light.java
gdx/src/com/badlogic/gdx/graphics/g3d/materials/BlendingAttribute.java
gdx/src/com/badlogic/gdx/graphics/g3d/materials/ColorAttribute.java
gdx/src/com/badlogic/gdx/graphics/g3d/materials/FloatAttribute.java
gdx/src/com/badlogic/gdx/graphics/g3d/materials/Material.java
gdx/src/com/badlogic/gdx/graphics/g3d/materials/TextureAttribute.java

index 97ac12b..1eb24f3 100644 (file)
@@ -100,6 +100,7 @@ public class Light {
                        direction.set(other.direction);
                        angle = other.angle;
                        attenuation.set(other.attenuation);
+                       exponent = other.exponent;
                }
        }
        
@@ -114,7 +115,7 @@ public class Light {
                        return false;
                if ((type == DIRECTIONAL || type == SPOT) && !direction.equals(other.direction))
                        return false;
-               if (type == SPOT && angle != other.angle)
+               if (type == SPOT && (angle != other.angle || exponent != other.exponent))
                        return false;
                return true;
        }
index d2c000d..6de288a 100644 (file)
@@ -1,6 +1,7 @@
 package com.badlogic.gdx.graphics.g3d.materials;
 
 import com.badlogic.gdx.graphics.g3d.materials.Material.Attribute;
+import com.badlogic.gdx.utils.GdxRuntimeException;
 
 public class BlendingAttribute extends Material.Attribute {
        public final static String Alias = "blended";
@@ -37,4 +38,13 @@ public class BlendingAttribute extends Material.Attribute {
                return ((BlendingAttribute)other).sourceFunction == sourceFunction && 
                        ((BlendingAttribute)other).destFunction == destFunction; 
        }
+
+       @Override
+       public void set (Attribute other) {
+               if (other == null || other.type != type || !(other instanceof BlendingAttribute))
+                       throw new GdxRuntimeException("Cannot set this attribute to an attribute of another type");
+               final BlendingAttribute o = (BlendingAttribute)other;
+               sourceFunction = o.sourceFunction;
+               destFunction = o.destFunction;
+       }
 }
\ No newline at end of file
index ea7c323..7281e82 100644 (file)
@@ -68,4 +68,12 @@ public class ColorAttribute extends Material.Attribute {
        protected boolean equals (Attribute other) {
                return ((ColorAttribute)other).color.equals(color);
        }
+       
+       @Override
+       public void set (Attribute other) {
+               if (other == null || other.type != type || !(other instanceof ColorAttribute))
+                       throw new GdxRuntimeException("Cannot set this attribute to an attribute of another type");
+               final ColorAttribute o = (ColorAttribute)other;
+               color.set(o.color);
+       }
 }
\ No newline at end of file
index 25c3ed4..3109fba 100644 (file)
@@ -1,6 +1,7 @@
 package com.badlogic.gdx.graphics.g3d.materials;
 
 import com.badlogic.gdx.graphics.g3d.materials.Material.Attribute;
+import com.badlogic.gdx.utils.GdxRuntimeException;
 
 public class FloatAttribute extends Material.Attribute {
        public static final String ShininessAlias = "shininess";
@@ -31,4 +32,12 @@ public class FloatAttribute extends Material.Attribute {
                // FIXME use epsilon?
                return ((FloatAttribute)other).value == value;
        }
+       
+       @Override
+       public void set (Attribute other) {
+               if (other == null || other.type != type || !(other instanceof FloatAttribute))
+                       throw new GdxRuntimeException("Cannot set this attribute to an attribute of another type");
+               final FloatAttribute o = (FloatAttribute)other;
+               value = o.value;
+       }
 }
index 2a98255..07032be 100644 (file)
@@ -11,7 +11,7 @@ public class Material implements Iterable<Material.Attribute>, Comparator<Materi
        /** Extend this class to implement a material attribute.
         *  Register the attribute type by statically calling the {@link #register(String)} method, 
         *  whose return value should be used to instantiate the attribute. 
-        *  A class can implement multiple types*/
+        *  A class can implement multiple types */
        public static abstract class Attribute {
                protected static long register(final String type) {
                        return Material.register(type);
@@ -37,6 +37,8 @@ public class Material implements Iterable<Material.Attribute>, Comparator<Materi
                public String toString () {
                        return Material.getAttributeAlias(type);
                }
+               /** Set this attribute to the values of the specified other attribute, which must be of the same type. */
+               public abstract void set(Attribute other);
        }
        
        /** The registered type aliases */
@@ -128,29 +130,41 @@ public class Material implements Iterable<Material.Attribute>, Comparator<Materi
        public final boolean has(final long type) {
                return type > 0 && (this.mask & type) == type;
        }
+       
+       /** @return the index of the attribute with the specified type or negative if not available. */
+       protected int indexOf(final long type) {
+               if (has(type))
+                       for (int i = 0; i < attributes.size; i++)
+                               if (attributes.get(i).type == type)
+                                       return i;
+               return -1;
+       }
 
-       /** Add one or more attributes to this material */
-       public final void add(final Attribute... attributes) {
-               for (int i = 0; i < attributes.length; i++) {
-                       final Attribute attr = attributes[i];
-                       if (!has(attr.type)) {
-                               enable(attr.type);
-                               this.attributes.add(attr);
-                               sorted = false;
-                       }
+       /** Add a attribute to this material.
+        * If the material already contains an attribute of the same type it is overwritten. */
+       public final void add(final Attribute attribute) {
+               final int idx = indexOf(attribute.type);
+               if (idx < 0) {
+                       enable(attribute.type);
+                       attributes.add(attribute);
+                       sorted = false;
+               } else {
+                       attributes.set(idx, attribute);
                }
        }
+       
+       /** Add an array of attributes to this material. 
+        * If the material already contains an attribute of the same type it is overwritten. */
+       public final void add(final Attribute... attributes) {
+               for (final Attribute attr : attributes)
+                       add(attr);
+       }
 
-       /** Add an array of attributes to this material */
+       /** Add an array of attributes to this material.
+        * If the material already contains an attribute of the same type it is overwritten. */
        public final void add(final Array<Attribute> attributes) {
-               for (int i = 0; i < attributes.size; i++) {
-                       final Attribute attr = attributes.get(i);
-                       if (!has(attr.type)) {
-                               enable(attr.type);
-                               this.attributes.add(attr);
-                               sorted = false;
-                       }
-               }
+               for (final Attribute attr : attributes)
+                       add(attr);
        }
        
        /** Removes the attribute from the material, i.e.: material.remove(BlendingAttribute.ID);
index 1e0c2ef..04825c0 100644 (file)
@@ -39,7 +39,7 @@ public class TextureAttribute extends Material.Attribute {
        }
        
        public TextureAttribute(final long type) {
-               this(type, (TextureDescriptor)null);
+               this(type, new TextureDescriptor());
        }
        
        public TextureAttribute(final long type, final Texture texture) {
@@ -59,4 +59,12 @@ public class TextureAttribute extends Material.Attribute {
        protected boolean equals (Attribute other) {
                return ((TextureAttribute)other).textureDescription.equals(textureDescription);
        }
+       
+       @Override
+       public void set (Attribute other) {
+               if (other == null || other.type != type || !(other instanceof TextureAttribute))
+                       throw new GdxRuntimeException("Cannot set this attribute to an attribute of another type");
+               final TextureAttribute o = (TextureAttribute)other;
+               textureDescription.set(o.textureDescription);
+       }
 }