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";
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
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";
// 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;
+ }
}
/** 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);
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 */
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);
}
public TextureAttribute(final long type) {
- this(type, (TextureDescriptor)null);
+ this(type, new TextureDescriptor());
}
public TextureAttribute(final long type, final Texture texture) {
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);
+ }
}