View Javadoc
1   /*
2    * Copyright (C) 2012 Alberto Irurueta Carro (alberto@irurueta.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.irurueta.geometry.io;
17  
18  import java.io.File;
19  
20  /**
21   * Contains data related to a texture.
22   */
23  public class Texture {
24  
25      /**
26       * Path to file containing texture image in any of the supported formats.
27       */
28      private final String fileName;
29  
30      /**
31       * Reference to file actually containing the texture image.
32       */
33      private File file;
34  
35      /**
36       * With of texture image expressed in pixels.
37       */
38      private int width;
39  
40      /**
41       * Height of texture image expressed in pixels.
42       */
43      private int height;
44  
45      /**
46       * Indicates if texture is valid or not. A texture will be considered to be
47       * invalid when its image cannot be loaded because the file cannot be found,
48       * it is corrupted or its format is not supported.
49       */
50      private boolean valid;
51  
52      /**
53       * ID assigned to this texture. ID must be unique so that texture can be
54       * uniquely identified.
55       */
56      private final int id;
57  
58      /**
59       * Constructor.
60       *
61       * @param id id to be set for this texture.
62       */
63      public Texture(final int id) {
64          fileName = "";
65          this.id = id;
66          file = null;
67          width = height = -1;
68          valid = false;
69      }
70  
71      /**
72       * Constructor.
73       *
74       * @param fileName path to file containing texture image in any of the
75       *                 supported formats.
76       * @param id       id to be set for this texture.
77       */
78      public Texture(final String fileName, final int id) {
79          this.fileName = fileName;
80          this.id = id;
81          file = new File(fileName);
82          width = height = -1;
83          valid = false;
84      }
85  
86      /**
87       * Returns path to file containing texture image in any of the supported
88       * formats.
89       *
90       * @return path to file containing texture image in any of the supported
91       * formats.
92       */
93      public String getFileName() {
94          return fileName;
95      }
96  
97      /**
98       * Indicates if file name has been provided or not.
99       *
100      * @return true if file names has been provided, false otherwise.
101      */
102     public boolean isFileNameAvailable() {
103         return !fileName.isEmpty();
104     }
105 
106     /**
107      * Returns id assigned to this texture. ID must be unique so that texture
108      * can be uniquely identified.
109      *
110      * @return id assigned to this texture.
111      */
112     public int getId() {
113         return id;
114     }
115 
116     /**
117      * Returns reference to file actually containing the texture image.
118      *
119      * @return reference to file actually containing the texture image.
120      */
121     public File getFile() {
122         return file;
123     }
124 
125     /**
126      * Sets file containing the texture image.
127      *
128      * @param file file containing the texture image.
129      */
130     public void setFile(final File file) {
131         this.file = file;
132     }
133 
134     /**
135      * Indicates if file containing the texture image has already been provided.
136      *
137      * @return true if file containing the texture image is available, false
138      * otherwise.
139      */
140     public boolean isFileAvailable() {
141         return file != null;
142     }
143 
144     /**
145      * Returns texture image width expressed in pixels.
146      *
147      * @return texture image width expressed in pixels.
148      */
149     public int getWidth() {
150         return width;
151     }
152 
153     /**
154      * Sets texture image width expressed in pixels.
155      *
156      * @param width texture image width to be set and expressed in pixels.
157      */
158     public void setWidth(final int width) {
159         this.width = width;
160     }
161 
162     /**
163      * Indicates whether width has been provided (when its value is positive).
164      *
165      * @return true if width is available, false otherwise.
166      */
167     public boolean isWidthAvailable() {
168         return width >= 0;
169     }
170 
171     /**
172      * Returns texture image height expressed in pixels.
173      *
174      * @return texture image height expressed in pixels.
175      */
176     public int getHeight() {
177         return height;
178     }
179 
180     /**
181      * Sets texture image height expressed in pixels.
182      *
183      * @param height texture image height to be set and expressed in pixels.
184      */
185     public void setHeight(final int height) {
186         this.height = height;
187     }
188 
189     /**
190      * Indicates whether height has been provided (when its value is positive).
191      *
192      * @return true if height is available, false otherwise.
193      */
194     public boolean isHeightAvailable() {
195         return height >= 0;
196     }
197 
198     /**
199      * Indicates if texture is valid or not. A texture will be considered to be
200      * invalid when its image cannot be loaded because the file cannot be found,
201      * it is corrupted or its format is not supported.
202      *
203      * @return true if texture is valid, false otherwise.
204      */
205     public boolean isValid() {
206         return valid;
207     }
208 
209     /**
210      * Specifies whether a texture is valid or not. A texture will be considered
211      * to be invalid when its image cannot be loaded because the file cannot be
212      * found, it is corrupted or its format is not supported.
213      *
214      * @param valid whether texture is to be set as valid or not.
215      */
216     public void setValid(final boolean valid) {
217         this.valid = valid;
218     }
219 }