21
21
// USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
package phasereditor .assetpack .ui .preview ;
23
23
24
+ import java .awt .Graphics2D ;
25
+ import java .awt .image .BufferedImage ;
26
+ import java .io .IOException ;
24
27
import java .util .ArrayList ;
25
- import java .util .Arrays ;
26
28
import java .util .HashSet ;
27
29
import java .util .List ;
28
30
import java .util .Set ;
29
31
32
+ import javax .imageio .ImageIO ;
33
+
30
34
import org .eclipse .core .runtime .ListenerList ;
31
35
import org .eclipse .jface .action .Action ;
32
36
import org .eclipse .jface .action .IToolBarManager ;
51
55
import org .eclipse .swt .graphics .FontMetrics ;
52
56
import org .eclipse .swt .graphics .GC ;
53
57
import org .eclipse .swt .graphics .Image ;
54
- import org .eclipse .swt .graphics .ImageData ;
55
58
import org .eclipse .swt .graphics .Point ;
56
59
import org .eclipse .swt .graphics .Rectangle ;
57
60
import org .eclipse .swt .widgets .Composite ;
@@ -89,7 +92,7 @@ public class TilemapCanvas extends ZoomCanvas
89
92
private int _tileHeight ;
90
93
private Color [] _colors ;
91
94
protected ImageAssetModel _imageModel ;
92
- private Image _tileSetImage ;
95
+ private BufferedImage _tileSetImage2 ;
93
96
private Image _renderImage ;
94
97
private String _initialImageRef ;
95
98
private int _mouseX ;
@@ -117,7 +120,7 @@ public boolean add(Point e) {
117
120
addMouseMoveListener (this );
118
121
addMouseListener (this );
119
122
addKeyListener (this );
120
-
123
+
121
124
Point size = PhaserEditorUI .get_pref_Preview_Tilemap_size ();
122
125
_tileWidth = size .x ;
123
126
_tileHeight = size .y ;
@@ -195,36 +198,33 @@ public void setImageModel(ImageAssetModel imageModel) {
195
198
}
196
199
197
200
private void buildTilesetImage () {
198
- Image old = _tileSetImage ;
199
-
200
201
if (_imageModel == null ) {
201
- _tileSetImage = null ;
202
+ _tileSetImage2 = null ;
202
203
} else {
203
- _tileSetImage = new Image (getDisplay (), _imageModel .getUrlFile ().getLocation ().toFile ().getAbsolutePath ());
204
- }
205
-
206
- if (old != null ) {
207
- old .dispose ();
204
+ try {
205
+ _tileSetImage2 = ImageIO .read (_imageModel .getUrlFile ().getLocation ().toFile ());
206
+ } catch (IOException e ) {
207
+ e .printStackTrace ();
208
+ throw new RuntimeException (e );
209
+ }
208
210
}
209
211
}
210
212
211
213
void buildMapImage () {
212
- if (_model != null && _tileSetImage != null ) {
214
+ if (_model != null && _tileSetImage2 != null ) {
213
215
214
216
int [][] map = _model .getCsvData ();
215
217
216
218
if (map != null && map .length > 0 ) {
217
219
218
220
Point size = getImageSize ();
219
221
220
- ImageData srcData = _tileSetImage .getImageData ();
221
- ImageData data = new ImageData (size .x , size .y , srcData .depth , srcData .palette );
222
- data .setAlpha (0 , 0 , 0 );
223
- Arrays .fill (data .alphaData , (byte ) 0 );
222
+ int mapWidth = size .x ;
223
+ int mapHeight = size .y ;
224
224
225
- Image image = new Image ( getDisplay (), data );
225
+ BufferedImage mapImage = new BufferedImage ( mapWidth , mapHeight , BufferedImage . TYPE_INT_ARGB );
226
226
227
- GC gc = new GC ( image );
227
+ Graphics2D gc = mapImage . createGraphics ( );
228
228
229
229
for (int i = 0 ; i < map .length ; i ++) {
230
230
int [] row = map [i ];
@@ -242,26 +242,31 @@ void buildMapImage() {
242
242
int x = j * getTileWidth ();
243
243
int y = i * getTileHeight ();
244
244
245
- Rectangle b = _tileSetImage .getBounds ();
246
- int srcX = frame * _tileWidth % b .width ;
247
- int srcY = frame * _tileWidth / b .width * _tileHeight ;
245
+ int srcX = frame * _tileWidth % _tileSetImage2 .getWidth ();
246
+ int srcY = frame * _tileWidth / _tileSetImage2 .getWidth () * _tileHeight ;
248
247
249
248
try {
250
- gc .drawImage (_tileSetImage , srcX , srcY , _tileWidth , _tileHeight , x , y , w , h );
249
+ gc .drawImage (_tileSetImage2 , x , y , x + w , y + h , srcX , srcY , srcX + _tileWidth ,
250
+ srcY + _tileHeight , null );
251
251
} catch (IllegalArgumentException e ) {
252
- gc .drawText ("Invalid parameters. Please check the tiles size." , 10 , 10 );
252
+ gc .drawString ("Invalid parameters. Please check the tiles size." , 10 , 10 );
253
253
}
254
254
}
255
255
}
256
256
257
- Image old = _renderImage ;
258
- _renderImage = image ;
257
+ try {
258
+ Image old = _renderImage ;
259
259
260
- if (old != null ) {
261
- old .dispose ();
262
- }
260
+ _renderImage = PhaserEditorUI .image_Swing_To_SWT (mapImage );
261
+
262
+ if (old != null ) {
263
+ old .dispose ();
264
+ }
263
265
264
- _renderImage = image ;
266
+ } catch (IOException e ) {
267
+ e .printStackTrace ();
268
+ throw new RuntimeException (e );
269
+ }
265
270
266
271
return ;
267
272
}
@@ -324,7 +329,7 @@ public void paintControl(PaintEvent e) {
324
329
int x = (int ) (j * getTileWidth () * scale + offX );
325
330
int y = (int ) (i * getTileHeight () * scale + offY );
326
331
327
- if (_tileSetImage == null ) {
332
+ if (_tileSetImage2 == null ) {
328
333
// paint map with colors
329
334
Color c = _colors [frame % _colors .length ];
330
335
gc .setBackground (c );
0 commit comments