1 /*-
2  * #%L
3  * This file is part of libtiled-java.
4  * %%
5  * Copyright (C) 2004 - 2020 Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
6  * Copyright (C) 2004 - 2020 Adam Turk <aturk@biggeruniverse.com>
7  * Copyright (C) 2016 - 2020 Mike Thomas <mikepthomas@outlook.com>
8  * %%
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  *    this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  *    this list of conditions and the following disclaimer in the documentation
16  *    and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  * #L%
30  */
31 package org.mapeditor.core;
32 
33 import java.awt.image.BufferedImage;
34 import java.util.logging.Level;
35 import java.util.logging.Logger;
36 
37 import javax.xml.bind.annotation.XmlAccessType;
38 import javax.xml.bind.annotation.XmlAccessorType;
39 
40 /**
41  * The core class for our tiles.
42  *
43  * @version 1.4.2
44  */
45 @XmlAccessorType(XmlAccessType.NONE)
46 public class Tile extends TileData {
47 
48     private BufferedImage image;
49     private String source;
50     private TileSet tileset;
51 
52     /**
53      * Constructor for Tile.
54      */
Tile()55     public Tile() {
56         super();
57         this.id = -1;
58     }
59 
60     /**
61      * Constructor for Tile.
62      *
63      * @param set a {@link org.mapeditor.core.TileSet} object.
64      */
Tile(TileSet set)65     public Tile(TileSet set) {
66         this();
67         this.tileset = set;
68     }
69 
70     /**
71      * Copy constructor
72      *
73      * @param t tile to copy
74      */
Tile(Tile t)75     public Tile(Tile t) {
76         this.tileset = t.tileset;
77 
78         Properties tileProperties = t.properties;
79         if (tileProperties != null) {
80             try {
81                 properties = tileProperties.clone();
82             } catch (CloneNotSupportedException ex) {
83                 Logger.getLogger(Tile.class.getName()).log(Level.SEVERE, null, ex);
84             }
85         }
86     }
87 
88     /**
89      * {@inheritDoc}
90      *
91      * Sets the id of the tile as long as it is at least 0.
92      */
93     @Override
setId(Integer value)94     public void setId(Integer value) {
95         if (value >= 0) {
96             this.id = value;
97         }
98     }
99 
100     /**
101      * Sets the image of the tile.
102      *
103      * @param image the new image of the tile
104      */
setImage(BufferedImage image)105     public void setImage(BufferedImage image) {
106         this.image = image;
107     }
108 
109     /**
110      * Sets the parent tileset for a tile.
111      *
112      * @param set a {@link org.mapeditor.core.TileSet} object.
113      */
setTileSet(TileSet set)114     public void setTileSet(TileSet set) {
115         tileset = set;
116     }
117 
118     /**
119      * Returns the {@link org.mapeditor.core.TileSet} that this tile is part of.
120      *
121      * @return TileSet
122      */
getTileSet()123     public TileSet getTileSet() {
124         return tileset;
125     }
126 
127     /**
128      * getWidth.
129      *
130      * @return a int.
131      */
getWidth()132     public int getWidth() {
133         if (image != null) {
134             return image.getWidth();
135         }
136         return 0;
137     }
138 
139     /**
140      * getHeight.
141      *
142      * @return a int.
143      */
getHeight()144     public int getHeight() {
145         if (image != null) {
146             return image.getHeight();
147         }
148         return 0;
149     }
150 
151     /**
152      * Returns the tile image for this Tile.
153      *
154      * @return a {@link java.awt.image.BufferedImage} object.
155      */
getImage()156     public BufferedImage getImage() {
157         return image;
158     }
159 
160     /**
161      * Getter for the field <code>source</code>.
162      *
163      * @return a {@link java.lang.String} object.
164      */
getSource()165     public String getSource() {
166         return source;
167     }
168 
169     /**
170      * Sets the URI path of the external source of this tile set. By setting
171      * this, the set is implied to be external in all other operations.
172      *
173      * @param source a URI of the tileset image file
174      */
setSource(String source)175     public void setSource(String source) {
176         this.source = source;
177     }
178 
179     /** {@inheritDoc} */
180     @Override
getProperties()181     public Properties getProperties() {
182         if (properties == null) {
183             properties = new Properties();
184         }
185         return super.getProperties();
186     }
187 
188     /** {@inheritDoc} */
189     @Override
toString()190     public String toString() {
191         return "Tile " + id + " (" + getWidth() + "x" + getHeight() + ")";
192     }
193 }
194