Repository navigation
Expand file tree
/
Copy pathTile.java
More file actions
251 lines (212 loc) · 6.4 KB
/
Copy pathTile.java
File metadata and controls
251 lines (212 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
/**
* a Tile is simply a square of land or object on a map.
* Tiles have an image, a name, and a type. They also have a number.
*/
public class Tile
{
/* Allow global disable of colorization effects on tiles */
public static boolean effects_enabled = true;
BufferedImage effectImage = null;
float effect_rScale;
float effect_gScale;
float effect_bScale;
float effect_zoom;
float effect_hue;
float effect_sat;
private int imageWidth = 0;
private int imageHeight = 0;
int zoomWidth, zoomHeight;
String name = null;
int number = -1;
String type = null;
String path = null;
String info = null;
Image image = null;
public Tile(int number, String path, String name, String type)
{
this.type = type;
this.number = number;
this.name = name;
this.path = path;
//System.out.println("load image" + path);
this.image = new ImageIcon(path).getImage();
if(image == null) {
throw new RuntimeException("Could not load image" + path);
}
imageWidth = image.getWidth(null);
imageHeight = image.getHeight(null);
zoomWidth = imageWidth;
zoomHeight = imageHeight;
//System.out.println(imageWidth);
}
public Tile(int number, String path, String name, String type, String info)
{
this(number, path, name, type);
if(info.indexOf(',') >= 0) {
throw new RuntimeException("Info string cannot contain \",\" characters");
}
this.info = info;
}
/**
* Creates a shallow copy of the given tile.
* It shouldn't really be necessary to reproduce a tile, as
* the same tile may appear more than once in a map without
* causing any trouble.
**/
public Tile(Tile t)
{
System.err.println("WARNING: Creating shallow copy of tile");
this.number = t.number;
this.type = t.type;
this.name = t.name;
this.path = t.path;
this.image = t.image;
}
String getImageLocation() {
return path;
}
/**
* Compare tiles. if the number is the same,
* return true. else return false.
**/
public boolean equals(Tile t) {
if(t == null) return false;
if(this.number == t.number/* &&
this.name.equals(t.name) &&
this.type == t.type &&
this.image == t.image*/) {
return true;
} else {
return false;
}
}
static boolean areEqual(Tile t1, Tile t2) {
if(t1 == null && t2 == null) {
return true;
} else if (t1 != null) {
return t1.equals(t2);
} else {
return false;
}
}
/**
* creates a null tile. this tile does not render itself,
* and has type of 99999, and name "noname".
*/
public Tile()
{
image = null;
}
/**
* returns the Image that is rendered by this tile.
**/
public Image getImage()
{
return image;
}
/**
*returns the type of tile.
*/
public String getType()
{
return type;
}
/**
*returns the tile's number.
*/
public int getNumber()
{
return number;
}
/**
* returns the tile's name.
*/
public String getName()
{
return name;
}
public String getInfo() {
return info;
}
public String getPath() {
return path;
}
/**
* Copies a fresh effect image, which is a zoomed version
* from the original.
**/
void initEffectImage()
{
//System.out.println("Zoom is: " + effect_zoom);
effectImage = new BufferedImage((int)(imageWidth * effect_zoom), (int)(imageHeight * effect_zoom), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D)effectImage.getGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.drawImage(image, 0, 0, (int)(imageWidth * effect_zoom), (int)(imageHeight * effect_zoom), null, (ImageObserver)null);
//effectImage.getGraphics().drawImage(image, 0, 0, (ImageObserver)null);
}
/**
* Colourizes and zooms the image
**/
void adjustRGBHS(float rScale, float gScale, float bScale,
float hueOffset, float satScale, float zoom)
{
if(effects_enabled) {
if(zoom == effect_zoom &&
rScale == effect_rScale &&
gScale == effect_gScale &&
bScale == effect_bScale) {
return;
}
effect_zoom = zoom;
zoomWidth = (int)(imageWidth * zoom);
zoomHeight = (int)(imageHeight * zoom);
initEffectImage();
float[] hsbVals = new float[3];
Color rgbVals;
WritableRaster raster = effectImage.getRaster();
int pixelData[] = new int[(zoomWidth * zoomHeight * 4)];
pixelData = raster.getPixels(0, 0, zoomWidth, zoomHeight, pixelData);
for(int i = 0; i < pixelData.length / 4; i++) {
if(hueOffset != 0 || satScale != 1) {
hsbVals = Color.RGBtoHSB(pixelData[i * 4], pixelData[i * 4 + 1], pixelData[i * 4 + 2], hsbVals);
hsbVals[0] += hueOffset;
hsbVals[1] *= satScale;
if(hsbVals[1] > 1) {
hsbVals[1] = 1;
}
rgbVals = new Color(Color.HSBtoRGB(hsbVals[0], hsbVals[1], hsbVals[2]));
pixelData[i * 4 + 0] = rgbVals.getRed();
pixelData[i * 4 + 1] = rgbVals.getGreen();
pixelData[i * 4 + 2] = rgbVals.getBlue();
}
pixelData[i * 4] = (int)(pixelData[i * 4] * rScale);
if(pixelData[i * 4] > 255) {
pixelData[i * 4] = 255;
}
pixelData[i * 4 + 1] = (int)(pixelData[i * 4 + 1] * gScale);
if(pixelData[i * 4 + 1] > 255) {
pixelData[i * 4 + 1] = 255;
}
pixelData[i * 4 + 2] = (int)(pixelData[i * 4 + 2] * bScale);
if(pixelData[i * 4 + 2] > 255) {
pixelData[i * 4 + 2] = 255;
}
}
raster.setPixels(0, 0, zoomWidth, zoomHeight, pixelData);
}
}
/**
* renders the tile's image if it exists.
*/
public void render(Graphics g, int x, int y)
{
if(effects_enabled && effectImage != null) {
g.drawImage(effectImage, x-zoomWidth, y-zoomHeight, null);
} else if(image != null) {
g.drawImage(image, x-imageWidth, y-imageHeight, null);
}
}
}