Skip to content

Commit 123661c

Browse files
Tilemap preview: now works on windows.
1 parent 6603adc commit 123661c

File tree

2 files changed

+46
-31
lines changed

2 files changed

+46
-31
lines changed

source/phasereditor/phasereditor.assetpack.ui/src/phasereditor/assetpack/ui/preview/TilemapCanvas.java

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@
2121
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
package phasereditor.assetpack.ui.preview;
2323

24+
import java.awt.Graphics2D;
25+
import java.awt.image.BufferedImage;
26+
import java.io.IOException;
2427
import java.util.ArrayList;
25-
import java.util.Arrays;
2628
import java.util.HashSet;
2729
import java.util.List;
2830
import java.util.Set;
2931

32+
import javax.imageio.ImageIO;
33+
3034
import org.eclipse.core.runtime.ListenerList;
3135
import org.eclipse.jface.action.Action;
3236
import org.eclipse.jface.action.IToolBarManager;
@@ -51,7 +55,6 @@
5155
import org.eclipse.swt.graphics.FontMetrics;
5256
import org.eclipse.swt.graphics.GC;
5357
import org.eclipse.swt.graphics.Image;
54-
import org.eclipse.swt.graphics.ImageData;
5558
import org.eclipse.swt.graphics.Point;
5659
import org.eclipse.swt.graphics.Rectangle;
5760
import org.eclipse.swt.widgets.Composite;
@@ -89,7 +92,7 @@ public class TilemapCanvas extends ZoomCanvas
8992
private int _tileHeight;
9093
private Color[] _colors;
9194
protected ImageAssetModel _imageModel;
92-
private Image _tileSetImage;
95+
private BufferedImage _tileSetImage2;
9396
private Image _renderImage;
9497
private String _initialImageRef;
9598
private int _mouseX;
@@ -117,7 +120,7 @@ public boolean add(Point e) {
117120
addMouseMoveListener(this);
118121
addMouseListener(this);
119122
addKeyListener(this);
120-
123+
121124
Point size = PhaserEditorUI.get_pref_Preview_Tilemap_size();
122125
_tileWidth = size.x;
123126
_tileHeight = size.y;
@@ -195,36 +198,33 @@ public void setImageModel(ImageAssetModel imageModel) {
195198
}
196199

197200
private void buildTilesetImage() {
198-
Image old = _tileSetImage;
199-
200201
if (_imageModel == null) {
201-
_tileSetImage = null;
202+
_tileSetImage2 = null;
202203
} 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+
}
208210
}
209211
}
210212

211213
void buildMapImage() {
212-
if (_model != null && _tileSetImage != null) {
214+
if (_model != null && _tileSetImage2 != null) {
213215

214216
int[][] map = _model.getCsvData();
215217

216218
if (map != null && map.length > 0) {
217219

218220
Point size = getImageSize();
219221

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;
224224

225-
Image image = new Image(getDisplay(), data);
225+
BufferedImage mapImage = new BufferedImage(mapWidth, mapHeight, BufferedImage.TYPE_INT_ARGB);
226226

227-
GC gc = new GC(image);
227+
Graphics2D gc = mapImage.createGraphics();
228228

229229
for (int i = 0; i < map.length; i++) {
230230
int[] row = map[i];
@@ -242,26 +242,31 @@ void buildMapImage() {
242242
int x = j * getTileWidth();
243243
int y = i * getTileHeight();
244244

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;
248247

249248
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);
251251
} 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);
253253
}
254254
}
255255
}
256256

257-
Image old = _renderImage;
258-
_renderImage = image;
257+
try {
258+
Image old = _renderImage;
259259

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+
}
263265

264-
_renderImage = image;
266+
} catch (IOException e) {
267+
e.printStackTrace();
268+
throw new RuntimeException(e);
269+
}
265270

266271
return;
267272
}
@@ -324,7 +329,7 @@ public void paintControl(PaintEvent e) {
324329
int x = (int) (j * getTileWidth() * scale + offX);
325330
int y = (int) (i * getTileHeight() * scale + offY);
326331

327-
if (_tileSetImage == null) {
332+
if (_tileSetImage2 == null) {
328333
// paint map with colors
329334
Color c = _colors[frame % _colors.length];
330335
gc.setBackground(c);

source/phasereditor/phasereditor.ui/src/phasereditor/ui/PhaserEditorUI.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
package phasereditor.ui;
2323

24+
import java.awt.image.BufferedImage;
2425
import java.io.BufferedReader;
26+
import java.io.ByteArrayInputStream;
27+
import java.io.ByteArrayOutputStream;
2528
import java.io.File;
2629
import java.io.IOException;
2730
import java.io.InputStream;
@@ -836,6 +839,13 @@ public static Rectangle computeImageZoom(Rectangle srcBounds, Rectangle dstBound
836839

837840
return new Rectangle(dstX, dstY, dstW, dstH);
838841
}
842+
843+
public static Image image_Swing_To_SWT(BufferedImage img) throws IOException {
844+
ByteArrayOutputStream memory = new ByteArrayOutputStream();
845+
ImageIO.write(img, "png", memory);
846+
Image result = new Image(Display.getCurrent(), new ByteArrayInputStream(memory.toByteArray()));
847+
return result;
848+
}
839849

840850
public static String readString(InputStream input) throws IOException {
841851
StringBuilder sb = new StringBuilder();

0 commit comments

Comments
 (0)