Immutable Appearance & Decal objects. An AppearanceBuilder so they are not a pain to construct.

This commit is contained in:
Bill Kuker 2012-07-01 14:42:34 +00:00 committed by U-WINDOWS-C28163E\Administrator
parent 640cbc1179
commit 13289c423b
3 changed files with 348 additions and 0 deletions

View File

@ -0,0 +1,60 @@
package net.sf.openrocket.appearance;
import net.sf.openrocket.util.Color;
/**
* A component appearance.
* This class is immutable.
*
* @author Bill Kuker <bkuker@billkuker.com>
*/
public class Appearance {
public static final Appearance MISSING = new Appearance(new Color(0,0,0), new Color(128,128,128), new Color(255,255,255), 100, null);
private final Color ambient, diffuse, specular;
private final int shininess;
private final Decal texture;
Appearance(final Color ambient, final Color diffuse, final Color specular, final int shininess, final Decal texture){
this.ambient = ambient;
this.diffuse = diffuse;
this.specular = specular;
this.shininess = shininess;
this.texture = texture;
}
Appearance(final Color ambient, final Color diffuse, final Color specular, final int shininess){
this.ambient = ambient;
this.diffuse = diffuse;
this.specular = specular;
this.shininess = shininess;
this.texture = null;
}
public Color getAmbient() {
return ambient;
}
public Color getDiffuse() {
return diffuse;
}
public Color getSpecular() {
return specular;
}
public int getShininess() {
return shininess;
}
public Decal getTexture() {
return texture;
}
@Override
public String toString() {
return "Appearance [ambient=" + ambient + ", diffuse=" + diffuse + ", specular=" + specular + ", shininess="
+ shininess + ", texture=" + texture + "]";
}
}

View File

@ -0,0 +1,225 @@
package net.sf.openrocket.appearance;
import java.net.URL;
import net.sf.openrocket.appearance.Decal.EdgeMode;
import net.sf.openrocket.util.AbstractChangeSource;
import net.sf.openrocket.util.Color;
import net.sf.openrocket.util.Coordinate;
/**
* Use this class to build an immutable Appearance object in a friendly way. Set
* the various values one at a time with the setter methods and then call
* getAppearance(). Each call to getAppearance will return a new appearance.
*
* You can use this class repeatedly and resetToDefaults() in between, or create
* a new one every time.
*
* @author Bill Kuker <bkuker@billkuker.com>
*
*/
public class AppearanceBuilder extends AbstractChangeSource {
private Color ambient, diffuse, specular;
private int shininess;
private double offsetU, offsetV;
private double centerU, centerV;
private double scaleU, scaleV;
private double rotation;
private URL image;
private Decal.EdgeMode edgeMode;
public AppearanceBuilder() {
resetToDefaults();
}
public AppearanceBuilder(Appearance a) {
resetToDefaults();
if ( a != null ){
setAmbient(a.getAmbient());
setDiffuse(a.getDiffuse());
setSpecular(a.getSpecular());
setShininess(a.getShininess());
Decal d = a.getTexture();
if ( d != null ){
setOffset(d.getOffset().x, d.getOffset().y);
setCenter(d.getCenter().x, d.getCenter().y);
setScale(d.getScale().x, d.getScale().y);
setRotation(d.getRotation());
setEdgeMode(d.getEdgeMode());
setImage(d.getImageURL());
}
// TODO Critical the rest of this!
}
}
public void resetToDefaults() {
ambient = new Color(0, 0, 0);
diffuse = new Color(128, 128, 128);
specular = new Color(255, 255, 255);
shininess = 100;
offsetU = offsetV = 0;
centerU = centerV = 0;
scaleU = scaleV = 1;
rotation = 0;
image = null;
edgeMode = EdgeMode.REPEAT;
}
public Appearance getAppearance() {
Decal t = null;
if (image != null) {
t = new Decal( //
new Coordinate(offsetU, offsetV), //
new Coordinate(centerU, centerV), //
new Coordinate(scaleU, scaleV), //
rotation, //
image, //
edgeMode);
}
return new Appearance( ambient, diffuse, specular, shininess, t);
}
public Color getAmbient() {
return ambient;
}
public void setAmbient(Color ambient) {
this.ambient = ambient;
fireChangeEvent();
}
public Color getDiffuse() {
return diffuse;
}
public void setDiffuse(Color diffuse) {
this.diffuse = diffuse;
fireChangeEvent();
}
public Color getSpecular() {
return specular;
}
public void setSpecular(Color specular) {
this.specular = specular;
fireChangeEvent();
}
public int getShininess() {
return shininess;
}
public void setShininess(int shininess) {
this.shininess = shininess;
fireChangeEvent();
}
public double getOffsetU() {
return offsetU;
}
public void setOffsetU(double offsetU) {
this.offsetU = offsetU;
fireChangeEvent();
}
public double getOffsetV() {
return offsetV;
}
public void setOffsetV(double offsetV) {
this.offsetV = offsetV;
fireChangeEvent();
}
public void setOffset(double u, double v) {
setOffsetU(u);
setOffsetV(v);
}
public double getCenterU() {
return centerU;
}
public void setCenterU(double centerU) {
this.centerU = centerU;
fireChangeEvent();
}
public double getCenterV() {
return centerV;
}
public void setCenterV(double centerV) {
this.centerV = centerV;
fireChangeEvent();
}
public void setCenter(double u, double v) {
setCenterU(u);
setCenterV(v);
}
public double getScaleU() {
return scaleU;
}
public void setScaleU(double scaleU) {
this.scaleU = scaleU;
fireChangeEvent();
}
public double getScaleV() {
return scaleV;
}
public void setScaleV(double scaleV) {
this.scaleV = scaleV;
fireChangeEvent();
}
public void setScale(double u, double v) {
setScaleU(u);
setScaleV(v);
}
public double getRotation() {
return rotation;
}
public void setRotation(double rotation) {
this.rotation = rotation;
fireChangeEvent();
}
public URL getImage() {
return image;
}
public void setImage(URL image) {
this.image = image;
fireChangeEvent();
}
public Decal.EdgeMode getEdgeMode() {
return edgeMode;
}
public void setEdgeMode(Decal.EdgeMode edgeMode) {
this.edgeMode = edgeMode;
fireChangeEvent();
}
@Override
protected void fireChangeEvent() {
super.fireChangeEvent();
}
}

View File

@ -0,0 +1,63 @@
package net.sf.openrocket.appearance;
import java.net.URL;
import net.sf.openrocket.util.Coordinate;
/**
* A texture that can be applied by an Appearance. This class is immutable.
*
* @author Bill Kuker <bkuker@billkuker.com>
*/
public class Decal {
public static enum EdgeMode {
REPEAT, MIRROR, CLAMP;
}
private final Coordinate offset, center, scale;
private final double rotation;
private final URL image;
private final EdgeMode mode;
Decal(final Coordinate offset, final Coordinate center, final Coordinate scale, final double rotation,
final URL image, final EdgeMode mode) {
this.offset = offset;
this.center = center;
this.scale = scale;
this.rotation = rotation;
this.image = image;
this.mode = mode;
}
public Coordinate getOffset() {
return offset;
}
public Coordinate getCenter() {
return center;
}
public Coordinate getScale() {
return scale;
}
public double getRotation() {
return rotation;
}
public EdgeMode getEdgeMode() {
return mode;
}
public URL getImageURL() {
return image;
}
@Override
public String toString() {
return "Texture [offset=" + offset + ", center=" + center + ", scale=" + scale + ", rotation=" + rotation
+ ", image=" + image + "]";
}
}