Browse code

adapt code for gwt compiler

devnewton authored on 23/01/2014 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 39
  */
41 40
 public class TmxLoader {
42 41
 
43
-    public TmxMap load(String xml) {
42
+    public void parseTmx(TmxMap map, String xml) {
44 43
         try {
45 44
             XmlDoc doc = XmlParser.parseXml(xml);
46
-            TmxMap map = new TmxMap();
47 45
             parseMap(doc.root, map);
48 46
             parseTilesets(doc.root, map);
49
-            parseExternalTilesets(map);
50
-            decodeLayerData(map);
51
-            return map;
52
-        } catch (XmlParseException | IOException ex) {
47
+        } catch (XmlParseException ex) {
53 48
             throw new RuntimeException("Cannot load tmx", ex);
54 49
         }
55 50
     }
56 51
 
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
-            }
52
+    public void parseTsx(TmxMap map, TmxTileset tileset, String xml) {
53
+        try {
54
+            parseTileset(XmlParser.parseXml(xml).root, map, tileset);
55
+            tileset.afterUnmarshal();
56
+            tileset.setReady(true);
57
+        } catch (XmlParseException ex) {
58
+            throw new RuntimeException("Cannot load tmx", ex);
81 59
         }
82
-        return null;
83
-    }
84
-
85
-    protected String openExternalTileset(String source) {
86
-        throw new RuntimeException("Not implemented");
87 60
     }
88
-
89
-    private void decodeLayerData(TmxMap map) {
90
-        HashMap<TmxTileInstance, TmxTileInstance> tileInstancePool = new HashMap<>();
61
+    
62
+    public void decode(TmxMap map) {
63
+        HashMap<TmxTileInstance, TmxTileInstance> tileInstancePool = new HashMap<TmxTileInstance, TmxTileInstance>();
91 64
         for (TmxLayer layer : map.getLayers()) {
92 65
             int[][] data = new int[layer.getWidth()][layer.getHeight()];
93 66
             layer.getData().decodeTo(layer.getWidth(), layer.getHeight(), data);
... ...
@@ -120,25 +93,39 @@ public class TmxLoader {
120 93
                 }
121 94
             }
122 95
         }
96
+        map.setReady(true);
123 97
     }
124 98
 
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();
99
+    private void parseMap(XmlTag xmlMap, TmxMap map) throws NumberFormatException {
100
+        map.setWidth(getMandatoryIntAttribute(xmlMap, "width"));
101
+        map.setHeight(getMandatoryIntAttribute(xmlMap, "height"));
102
+        map.setTilewidth(getMandatoryIntAttribute(xmlMap, "tilewidth"));
103
+        map.setTileheight(getMandatoryIntAttribute(xmlMap, "tileheight"));
104
+        map.setOrientation(TmxMapOrientation.valueOf(xmlMap.attributes.get("orientation").toUpperCase()));
105
+        map.setProperties(parseProperties(findChild(xmlMap, "properties")));
106
+        List<TmxLayer> layers = new ArrayList<TmxLayer>();
107
+        for (XmlTag child : xmlMap.children) {
108
+            if ("layer".equals(child.name)) {
109
+                TmxLayer layer = new TmxLayer();
110
+                parseLayer(child, layer);
111
+                layer.afterUnmarshal();
112
+                layers.add(layer);
136 113
             }
137 114
         }
115
+        map.setLayers(layers);
116
+    }
117
+
118
+    private XmlTag findChild(XmlTag parent, String name) {
119
+        for (XmlTag child : parent.children) {
120
+            if (name.equals(child.name)) {
121
+                return child;
122
+            }
123
+        }
124
+        return null;
138 125
     }
139 126
 
140 127
     private List<TmxProperty> parseProperties(XmlTag xmlProperties) {
141
-        List<TmxProperty> properties = new ArrayList<>();
128
+        List<TmxProperty> properties = new ArrayList<TmxProperty>();
142 129
         if (null != xmlProperties) {
143 130
             for (XmlTag child : xmlProperties.children) {
144 131
                 if ("property".equals(child.name)) {
... ...
@@ -153,7 +140,7 @@ public class TmxLoader {
153 140
     }
154 141
 
155 142
     private void parseTilesets(XmlTag xmlMap, TmxMap map) throws XmlParseException {
156
-        List<TmxTileset> tilesets = new ArrayList<>();
143
+        List<TmxTileset> tilesets = new ArrayList<TmxTileset>();
157 144
         for (XmlTag child : xmlMap.children) {
158 145
             if ("tileset".equals(child.name)) {
159 146
                 TmxTileset tileset = new TmxTileset();
... ...
@@ -164,11 +151,11 @@ public class TmxLoader {
164 151
                 } else {
165 152
                     parseTileset(child, map, tileset);
166 153
                     tileset.afterUnmarshal();
154
+                    tileset.setReady(true);
167 155
                 }
168 156
                 tilesets.add(tileset);
169 157
             }
170 158
         }
171
-
172 159
         map.setTilesets(tilesets);
173 160
     }
174 161
 
... ...
@@ -180,7 +167,7 @@ public class TmxLoader {
180 167
         tileset.setTileheight(getIntAttribute(xmlTileset, "tileheight", map.getTileheight()));
181 168
         tileset.setProperties(parseProperties(findChild(xmlTileset, "properties")));
182 169
         tileset.setImage(parseImage(findChild(xmlTileset, "image")));
183
-        List<TmxTile> tiles = new ArrayList<>();
170
+        List<TmxTile> tiles = new ArrayList<TmxTile>();
184 171
         for (XmlTag child : xmlTileset.children) {
185 172
             if ("tile".equals(child.name)) {
186 173
                 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 109
     public String getProperty(String name, String defaultValue) {
109 110
         return TmxUtils.getProperty(properties, name, defaultValue);
110 111
     }
112
+
113
+    public boolean isReady() {
114
+        return ready;
115
+    }
116
+
117
+    public void setReady(boolean ready) {
118
+        this.ready = ready;
119
+    }
120
+    
121
+    public boolean canDecode() {
122
+        for(TmxTileset ts : tilesets) {
123
+            if(!ts.isReady()) {
124
+                return false;
125
+            }
126
+        }
127
+        return true;
128
+    }
111 129
 }
... ...
@@ -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 49
     @Override
52 50
     public int hashCode() {
53 51
         int hash = 7;
54
-        hash = 89 * hash + Objects.hashCode(this.tile);
55
-        hash = 89 * hash + Objects.hashCode(this.effect);
52
+        hash = 89 * hash + (this.tile != null ? this.tile.hashCode() : 0);
53
+        hash = 89 * hash + (this.effect != null ? this.effect.hashCode() : 0);
56 54
         return hash;
57 55
     }
58 56
 
... ...
@@ -65,10 +63,10 @@ public class TmxTileInstance {
65 63
             return false;
66 64
         }
67 65
         final TmxTileInstance other = (TmxTileInstance) obj;
68
-        if (!Objects.equals(this.tile, other.tile)) {
66
+        if (!TmxUtils.equals(this.tile, other.tile)) {
69 67
             return false;
70 68
         }
71
-        if (!Objects.equals(this.effect, other.effect)) {
69
+        if (!TmxUtils.equals(this.effect, other.effect)) {
72 70
             return false;
73 71
         }
74 72
         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
 }