Browse code

adapt code for gwt compiler

devnewton authored on 23/01/2014 at 09:43:14
Showing 8 changed files
... ...
@@ -51,24 +51,21 @@ public class TmxData {
51 51
     }
52 52
 
53 53
     public void decodeTo(int width, int height, int[][] data) {
54
-        switch (encoding) {
55
-            case "csv":
56
-                decodeCsvTo(width, height, data);
57
-                break;
58
-            default:
59
-                throw new RuntimeException(
60
-                        "Unsupported tiled layer data encoding: " + encoding);
54
+        if ("csv".equals(encoding)) {
55
+            decodeCsvTo(width, height, data);
56
+        } else {
57
+            throw new RuntimeException(
58
+                    "Unsupported tiled layer data encoding: " + encoding);
61 59
         }
62 60
     }
63 61
 
64 62
     private void decodeCsvTo(int width, int height, int[][] gidArray) {
65
-        try (Scanner scanner = new Scanner(this.data.trim())) {
66
-            scanner.useDelimiter("[\\s]*,[\\s]*");
67
-            for (int y = 0; y < height; ++y) {
68
-                for (int x = 0; x < width; ++x) {
69
-                    String str = scanner.next();
70
-                    gidArray[x][y] = Integer.parseInt(str);
71
-                }
63
+        String[] values = this.data.replaceAll("[\\s]", "").split(",");
64
+        int index = 0;
65
+        for (int y = 0; y < height; ++y) {
66
+            for (int x = 0; x < width; ++x) {
67
+                String str = values[index++];
68
+                gidArray[x][y] = Integer.parseInt(str);
72 69
             }
73 70
         }
74 71
     }
... ...
@@ -36,7 +36,7 @@ public class TmxLayer {
36 36
     private int x, y;
37 37
     private int width;
38 38
     private int height;
39
-    private List<TmxProperty> properties = new ArrayList<>();
39
+    private List<TmxProperty> properties = new ArrayList<TmxProperty>();
40 40
     private TmxData data;
41 41
     private TmxTileInstance tiles[][];
42 42
 
... ...
@@ -27,7 +27,6 @@ import com.github.asilvestre.jpurexml.XmlDoc;
27 27
 import com.github.asilvestre.jpurexml.XmlParseException;
28 28
 import com.github.asilvestre.jpurexml.XmlParser;
29 29
 import com.github.asilvestre.jpurexml.XmlTag;
30
-import java.io.IOException;
31 30
 import java.util.ArrayList;
32 31
 import java.util.EnumSet;
33 32
 import java.util.HashMap;
... ...
@@ -40,54 +39,28 @@ import java.util.ListIterator;
40 40
  */
41 41
 public class TmxLoader {
42 42
 
43
-    public TmxMap load(String xml) {
43
+    public void parseTmx(TmxMap map, String xml) {
44 44
         try {
45 45
             XmlDoc doc = XmlParser.parseXml(xml);
46
-            TmxMap map = new TmxMap();
47 46
             parseMap(doc.root, map);
48 47
             parseTilesets(doc.root, map);
49
-            parseExternalTilesets(map);
50
-            decodeLayerData(map);
51
-            return map;
52
-        } catch (XmlParseException | IOException ex) {
48
+        } catch (XmlParseException ex) {
53 49
             throw new RuntimeException("Cannot load tmx", ex);
54 50
         }
55 51
     }
56 52
 
57
-    private void parseMap(XmlTag xmlMap, TmxMap map) throws NumberFormatException {
58
-        map.setWidth(getMandatoryIntAttribute(xmlMap, "width"));
59
-        map.setHeight(getMandatoryIntAttribute(xmlMap, "height"));
60
-        map.setTilewidth(getMandatoryIntAttribute(xmlMap, "tilewidth"));
61
-        map.setTileheight(getMandatoryIntAttribute(xmlMap, "tileheight"));
62
-        map.setOrientation(TmxMapOrientation.valueOf(xmlMap.attributes.get("orientation").toUpperCase()));
63
-        map.setProperties(parseProperties(findChild(xmlMap, "properties")));
64
-        List<TmxLayer> layers = new ArrayList<>();
65
-        for (XmlTag child : xmlMap.children) {
66
-            if ("layer".equals(child.name)) {
67
-                TmxLayer layer = new TmxLayer();
68
-                parseLayer(child, layer);
69
-                layer.afterUnmarshal();
70
-                layers.add(layer);
71
-            }
72
-        }
73
-        map.setLayers(layers);
74
-    }
75
-
76
-    XmlTag findChild(XmlTag parent, String name) {
77
-        for (XmlTag child : parent.children) {
78
-            if (name.equals(child.name)) {
79
-                return child;
80
-            }
53
+    public void parseTsx(TmxMap map, TmxTileset tileset, String xml) {
54
+        try {
55
+            parseTileset(XmlParser.parseXml(xml).root, map, tileset);
56
+            tileset.afterUnmarshal();
57
+            tileset.setReady(true);
58
+        } catch (XmlParseException ex) {
59
+            throw new RuntimeException("Cannot load tmx", ex);
81 60
         }
82
-        return null;
83
-    }
84
-
85
-    protected String openExternalTileset(String source) {
86
-        throw new RuntimeException("Not implemented");
87 61
     }
88
-
89
-    private void decodeLayerData(TmxMap map) {
90
-        HashMap<TmxTileInstance, TmxTileInstance> tileInstancePool = new HashMap<>();
62
+    
63
+    public void decode(TmxMap map) {
64
+        HashMap<TmxTileInstance, TmxTileInstance> tileInstancePool = new HashMap<TmxTileInstance, TmxTileInstance>();
91 65
         for (TmxLayer layer : map.getLayers()) {
92 66
             int[][] data = new int[layer.getWidth()][layer.getHeight()];
93 67
             layer.getData().decodeTo(layer.getWidth(), layer.getHeight(), data);
... ...
@@ -120,25 +93,39 @@ public class TmxLoader {
120 120
                 }
121 121
             }
122 122
         }
123
+        map.setReady(true);
123 124
     }
124 125
 
125
-    private void parseExternalTilesets(TmxMap map) throws IOException, XmlParseException {
126
-        for (TmxTileset tileset : map.getTilesets()) {
127
-            final String source = tileset.getSource();
128
-            if (null != source) {
129
-                parseTileset(XmlParser.parseXml(openExternalTileset(source)).root, map, tileset);
130
-                String tilesetDir = source.substring(0, source.lastIndexOf('/') + 1);
131
-                final TmxImage image = tileset.getImage();
132
-                if (null != image) {
133
-                    tileset.getImage().setSource(tilesetDir + image.getSource());
134
-                }
135
-                tileset.afterUnmarshal();
126
+    private void parseMap(XmlTag xmlMap, TmxMap map) throws NumberFormatException {
127
+        map.setWidth(getMandatoryIntAttribute(xmlMap, "width"));
128
+        map.setHeight(getMandatoryIntAttribute(xmlMap, "height"));
129
+        map.setTilewidth(getMandatoryIntAttribute(xmlMap, "tilewidth"));
130
+        map.setTileheight(getMandatoryIntAttribute(xmlMap, "tileheight"));
131
+        map.setOrientation(TmxMapOrientation.valueOf(xmlMap.attributes.get("orientation").toUpperCase()));
132
+        map.setProperties(parseProperties(findChild(xmlMap, "properties")));
133
+        List<TmxLayer> layers = new ArrayList<TmxLayer>();
134
+        for (XmlTag child : xmlMap.children) {
135
+            if ("layer".equals(child.name)) {
136
+                TmxLayer layer = new TmxLayer();
137
+                parseLayer(child, layer);
138
+                layer.afterUnmarshal();
139
+                layers.add(layer);
136 140
             }
137 141
         }
142
+        map.setLayers(layers);
143
+    }
144
+
145
+    private XmlTag findChild(XmlTag parent, String name) {
146
+        for (XmlTag child : parent.children) {
147
+            if (name.equals(child.name)) {
148
+                return child;
149
+            }
150
+        }
151
+        return null;
138 152
     }
139 153
 
140 154
     private List<TmxProperty> parseProperties(XmlTag xmlProperties) {
141
-        List<TmxProperty> properties = new ArrayList<>();
155
+        List<TmxProperty> properties = new ArrayList<TmxProperty>();
142 156
         if (null != xmlProperties) {
143 157
             for (XmlTag child : xmlProperties.children) {
144 158
                 if ("property".equals(child.name)) {
... ...
@@ -153,7 +140,7 @@ public class TmxLoader {
153 153
     }
154 154
 
155 155
     private void parseTilesets(XmlTag xmlMap, TmxMap map) throws XmlParseException {
156
-        List<TmxTileset> tilesets = new ArrayList<>();
156
+        List<TmxTileset> tilesets = new ArrayList<TmxTileset>();
157 157
         for (XmlTag child : xmlMap.children) {
158 158
             if ("tileset".equals(child.name)) {
159 159
                 TmxTileset tileset = new TmxTileset();
... ...
@@ -164,11 +151,11 @@ public class TmxLoader {
164 164
                 } else {
165 165
                     parseTileset(child, map, tileset);
166 166
                     tileset.afterUnmarshal();
167
+                    tileset.setReady(true);
167 168
                 }
168 169
                 tilesets.add(tileset);
169 170
             }
170 171
         }
171
-
172 172
         map.setTilesets(tilesets);
173 173
     }
174 174
 
... ...
@@ -180,7 +167,7 @@ public class TmxLoader {
180 180
         tileset.setTileheight(getIntAttribute(xmlTileset, "tileheight", map.getTileheight()));
181 181
         tileset.setProperties(parseProperties(findChild(xmlTileset, "properties")));
182 182
         tileset.setImage(parseImage(findChild(xmlTileset, "image")));
183
-        List<TmxTile> tiles = new ArrayList<>();
183
+        List<TmxTile> tiles = new ArrayList<TmxTile>();
184 184
         for (XmlTag child : xmlTileset.children) {
185 185
             if ("tile".equals(child.name)) {
186 186
                 tiles.add(parseTile(tileset, child));
... ...
@@ -32,14 +32,15 @@ import java.util.List;
32 32
  */
33 33
 public class TmxMap {
34 34
 
35
+    private boolean ready;
35 36
     private int width;
36 37
     private int height;
37 38
     private int tilewidth;
38 39
     private int tileheight;
39 40
     private TmxMapOrientation orientation;
40
-    private List<TmxTileset> tilesets = new ArrayList<>();
41
-    private List<TmxProperty> properties = new ArrayList<>();
42
-    private List<TmxLayer> layers = new ArrayList<>();
41
+    private List<TmxTileset> tilesets = new ArrayList<TmxTileset>();
42
+    private List<TmxProperty> properties = new ArrayList<TmxProperty>();
43
+    private List<TmxLayer> layers = new ArrayList<TmxLayer>();
43 44
 
44 45
     public TmxMapOrientation getOrientation() {
45 46
         return orientation;
... ...
@@ -108,4 +109,21 @@ public class TmxMap {
108 108
     public String getProperty(String name, String defaultValue) {
109 109
         return TmxUtils.getProperty(properties, name, defaultValue);
110 110
     }
111
+
112
+    public boolean isReady() {
113
+        return ready;
114
+    }
115
+
116
+    public void setReady(boolean ready) {
117
+        this.ready = ready;
118
+    }
119
+    
120
+    public boolean canDecode() {
121
+        for(TmxTileset ts : tilesets) {
122
+            if(!ts.isReady()) {
123
+                return false;
124
+            }
125
+        }
126
+        return true;
127
+    }
111 128
 }
... ...
@@ -33,7 +33,7 @@ import java.util.List;
33 33
 public class TmxTile {
34 34
 
35 35
     private int id;
36
-    private List<TmxProperty> properties = new ArrayList<>();
36
+    private List<TmxProperty> properties = new ArrayList<TmxProperty>();
37 37
     private TmxFrame frame;
38 38
 
39 39
     public int getId() {
... ...
@@ -24,8 +24,6 @@
24 24
 package im.bci.tmxloader;
25 25
 
26 26
 import java.util.EnumSet;
27
-import java.util.Objects;
28
-
29 27
 /**
30 28
  *
31 29
  * @author devnewton
... ...
@@ -51,8 +49,8 @@ public class TmxTileInstance {
51 51
     @Override
52 52
     public int hashCode() {
53 53
         int hash = 7;
54
-        hash = 89 * hash + Objects.hashCode(this.tile);
55
-        hash = 89 * hash + Objects.hashCode(this.effect);
54
+        hash = 89 * hash + (this.tile != null ? this.tile.hashCode() : 0);
55
+        hash = 89 * hash + (this.effect != null ? this.effect.hashCode() : 0);
56 56
         return hash;
57 57
     }
58 58
 
... ...
@@ -65,10 +63,10 @@ public class TmxTileInstance {
65 65
             return false;
66 66
         }
67 67
         final TmxTileInstance other = (TmxTileInstance) obj;
68
-        if (!Objects.equals(this.tile, other.tile)) {
68
+        if (!TmxUtils.equals(this.tile, other.tile)) {
69 69
             return false;
70 70
         }
71
-        if (!Objects.equals(this.effect, other.effect)) {
71
+        if (!TmxUtils.equals(this.effect, other.effect)) {
72 72
             return false;
73 73
         }
74 74
         return true;
... ...
@@ -38,12 +38,20 @@ public class TmxTileset {
38 38
     private int tilewidth;
39 39
     private int tileheight;
40 40
     private int spacing, margin;
41
-    private List<TmxProperty> properties = new ArrayList<>();
41
+    private List<TmxProperty> properties = new ArrayList<TmxProperty>();
42 42
     private TmxImage image;
43 43
     private int firstgid;
44
-    private final TreeMap<Integer/*id*/, TmxTile> tilesById = new TreeMap<>();
45
-    private List<TmxTile> tiles = new ArrayList<>();
44
+    private final TreeMap<Integer/*id*/, TmxTile> tilesById = new TreeMap<Integer/*id*/, TmxTile>();
45
+    private List<TmxTile> tiles = new ArrayList<TmxTile>();
46
+    private boolean ready;
47
+    
48
+    public boolean isReady() {
49
+        return ready;
50
+    }
46 51
 
52
+    public void setReady(boolean ready) {
53
+        this.ready = ready;
54
+    }
47 55
     public String getSource() {
48 56
         return source;
49 57
     }
... ...
@@ -42,4 +42,8 @@ public class TmxUtils {
42 42
         }
43 43
         return defaultValue;
44 44
     }
45
+    
46
+    public static boolean equals(Object a, Object b) {
47
+        return (a == b) || (a != null && a.equals(b));
48
+    }
45 49
 }