1 /* 2 * HomeTexture.java 5 oct. 07 3 * 4 * Sweet Home 3D, Copyright (c) 2007 Emmanuel PUYBARET / eTeks <info@eteks.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 */ 20 package com.eteks.sweethome3d.model; 21 22 import java.io.IOException; 23 import java.io.ObjectInputStream; 24 import java.io.Serializable; 25 26 /** 27 * An image used as texture on home 3D objects. 28 * @author Emmanuel Puybaret 29 */ 30 public class HomeTexture implements TextureImage, Serializable { 31 private static final long serialVersionUID = 1L; 32 33 private final String catalogId; 34 private final String name; 35 private final String creator; 36 private final Content image; 37 private final float width; 38 private final float height; 39 private final float xOffset; 40 private final float yOffset; 41 private final float angle; 42 private float scale; 43 private final boolean leftToRightOriented; 44 45 /** 46 * Creates a home texture from an existing one. 47 * @param texture the texture from which data are copied 48 */ HomeTexture(TextureImage texture)49 public HomeTexture(TextureImage texture) { 50 this(texture, 0); 51 } 52 53 /** 54 * Creates a home texture from an existing one with customized angle and offset. 55 * @param texture the texture from which data are copied 56 * @param angle the rotation angle applied to the texture 57 * @since 4.4 58 */ HomeTexture(TextureImage texture, float angle)59 public HomeTexture(TextureImage texture, float angle) { 60 // Texture is left to right oriented when applied on objects seen from front 61 // added to homes with a version 3.4 and higher 62 this(texture, angle, true); 63 } 64 65 /** 66 * Creates a home texture from an existing one with customized angle and offset. 67 * @param texture the texture from which data are copied 68 * @param angle the rotation angle applied to the texture 69 * @param leftToRightOriented orientation used on the texture when applied on objects seen from front 70 * @since 5.3 71 */ HomeTexture(TextureImage texture, float angle, boolean leftToRightOriented)72 public HomeTexture(TextureImage texture, float angle, boolean leftToRightOriented) { 73 this(texture, angle, 1, leftToRightOriented); 74 } 75 76 /** 77 * Creates a home texture from an existing one with customized angle and offset. 78 * @param texture the texture from which data are copied 79 * @param angle the rotation angle applied to the texture 80 * @param scale the scale applied to the texture 81 * @param leftToRightOriented orientation used on the texture when applied on objects seen from front 82 * @since 5.5 83 */ HomeTexture(TextureImage texture, float angle, float scale, boolean leftToRightOriented)84 public HomeTexture(TextureImage texture, float angle, float scale, boolean leftToRightOriented) { 85 this(texture, 0, 0, angle, scale, leftToRightOriented); 86 } 87 88 /** 89 * Creates a home texture from an existing one with customized angle and offset. 90 * @param texture the texture from which data are copied 91 * @param xOffset the offset applied to the texture along X axis in percentage of its width 92 * @param yOffset the offset applied to the texture along Y axis in percentage of its height 93 * @param angle the rotation angle applied to the texture 94 * @param scale the scale applied to the texture 95 * @param leftToRightOriented orientation used on the texture when applied on objects seen from front 96 * @since 6.0 97 */ HomeTexture(TextureImage texture, float xOffset, float yOffset, float angle, float scale, boolean leftToRightOriented)98 public HomeTexture(TextureImage texture, float xOffset, float yOffset, float angle, float scale, boolean leftToRightOriented) { 99 this.name = texture.getName(); 100 this.creator = texture.getCreator(); 101 this.image = texture.getImage(); 102 this.width = texture.getWidth(); 103 this.height = texture.getHeight(); 104 this.xOffset = xOffset; 105 this.yOffset = yOffset; 106 this.angle = angle; 107 this.scale = scale; 108 this.leftToRightOriented = leftToRightOriented; 109 if (texture instanceof HomeTexture) { 110 this.catalogId = ((HomeTexture)texture).getCatalogId(); 111 } else if (texture instanceof CatalogTexture) { 112 this.catalogId = ((CatalogTexture)texture).getId(); 113 } else { 114 this.catalogId = null; 115 } 116 } 117 118 /** 119 * Initializes new fields 120 * and reads texture from <code>in</code> stream with default reading method. 121 */ readObject(ObjectInputStream in)122 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { 123 this.scale = 1f; 124 in.defaultReadObject(); 125 } 126 127 /** 128 * Returns the catalog ID of this texture or <code>null</code> if it doesn't exist. 129 * @since 4.4 130 */ getCatalogId()131 public String getCatalogId() { 132 return this.catalogId; 133 } 134 135 /** 136 * Returns the name of this texture. 137 */ getName()138 public String getName() { 139 return this.name; 140 } 141 142 /** 143 * Returns the creator of this texture. 144 * @since 5.5 145 */ getCreator()146 public String getCreator() { 147 return this.creator; 148 } 149 150 /** 151 * Returns the content of the image used for this texture. 152 */ getImage()153 public Content getImage() { 154 return this.image; 155 } 156 157 /** 158 * Returns the width of the image in centimeters. 159 */ getWidth()160 public float getWidth() { 161 return this.width; 162 } 163 164 /** 165 * Returns the height of the image in centimeters. 166 */ getHeight()167 public float getHeight() { 168 return this.height; 169 } 170 171 /** 172 * Returns the offset applied to the texture along X axis in percentage of its width. 173 * @since 6.0 174 */ getXOffset()175 public float getXOffset() { 176 return this.xOffset; 177 } 178 179 /** 180 * Returns the offset applied to the texture along Y axis in percentage of its height. 181 * @since 6.0 182 */ getYOffset()183 public float getYOffset() { 184 return this.yOffset; 185 } 186 187 /** 188 * Returns the angle of rotation in radians applied to this texture. 189 * @since 4.4 190 */ getAngle()191 public float getAngle() { 192 return this.angle; 193 } 194 195 /** 196 * Returns the scale applied to this texture. 197 * @since 5.5 198 */ getScale()199 public float getScale() { 200 return this.scale; 201 } 202 203 /** 204 * Returns <code>true</code> if the objects using this texture should take into account 205 * the orientation of the texture. 206 * @since 3.4 207 */ isLeftToRightOriented()208 public boolean isLeftToRightOriented() { 209 return this.leftToRightOriented; 210 } 211 212 /** 213 * Returns <code>true</code> if the object in parameter is equal to this texture. 214 */ 215 @Override equals(Object obj)216 public boolean equals(Object obj) { 217 if (obj == this) { 218 return true; 219 } else if (obj instanceof HomeTexture) { 220 HomeTexture texture = (HomeTexture)obj; 221 return (texture.name == this.name 222 || texture.name != null && texture.name.equals(this.name)) 223 && (texture.image == this.image 224 || texture.image != null && texture.image.equals(this.image)) 225 && texture.width == this.width 226 && texture.height == this.height 227 && texture.xOffset == this.xOffset 228 && texture.yOffset == this.yOffset 229 && texture.leftToRightOriented == this.leftToRightOriented 230 && texture.angle == this.angle 231 && texture.scale == this.scale; 232 } else { 233 return false; 234 } 235 } 236 237 /** 238 * Returns a hash code for this texture. 239 */ 240 @Override hashCode()241 public int hashCode() { 242 return (this.name != null ? this.name.hashCode() : 0) 243 + (this.image != null ? this.image.hashCode() : 0) 244 + Float.floatToIntBits(this.width) 245 + Float.floatToIntBits(this.height) 246 + Float.floatToIntBits(this.xOffset) 247 + Float.floatToIntBits(this.yOffset) 248 + Float.floatToIntBits(this.angle) 249 + Float.floatToIntBits(this.scale); 250 } 251 } 252