[tiled] r725 - in branches/adam/src/tiled: core io io/xml mapeditor mapeditor/dialogs mapeditor/resources mapeditor/undo mapeditor/util mapeditor/widget

tiled-svn at biggeruniverse.com tiled-svn at biggeruniverse.com
Thu Feb 8 02:30:34 PST 2007


Author: aturk
Date: 2007-02-08 04:30:33 -0600 (Thu, 08 Feb 2007)
New Revision: 725

Added:
   branches/adam/src/tiled/mapeditor/undo/TileSetEdit.java
Modified:
   branches/adam/src/tiled/core/Map.java
   branches/adam/src/tiled/core/Sprite.java
   branches/adam/src/tiled/core/TileSet.java
   branches/adam/src/tiled/io/ImageHelper.java
   branches/adam/src/tiled/io/MapHelper.java
   branches/adam/src/tiled/io/xml/XMLMapTransformer.java
   branches/adam/src/tiled/io/xml/XMLMapWriter.java
   branches/adam/src/tiled/mapeditor/MapEditor.java
   branches/adam/src/tiled/mapeditor/dialogs/TileDialog.java
   branches/adam/src/tiled/mapeditor/dialogs/TilesetManager.java
   branches/adam/src/tiled/mapeditor/resources/gui.properties
   branches/adam/src/tiled/mapeditor/util/MapChangeListener.java
   branches/adam/src/tiled/mapeditor/widget/TabbedTilesetsPane.java
Log:
A rethink of tileset embedding. Tileset changes still lack the ability to be undone, 
this also means that any changes to the tileset do not cause the map to show changed. 
Tileset image is also available now to tiled-core users via TileSet#getTilesetImage.

+ Fixed tileset image embedding is now possible again via 'Pack'
+ Fixed tileset embedding now works correctly, if the set is an external TSX file
+ Fixed tileset saving in all permutations
+ Updated TMX map loading (internal changes)

Modified: branches/adam/src/tiled/core/Map.java
===================================================================
--- branches/adam/src/tiled/core/Map.java	2007-02-07 12:47:14 UTC (rev 724)
+++ branches/adam/src/tiled/core/Map.java	2007-02-08 10:30:33 UTC (rev 725)
@@ -126,6 +126,16 @@
         }
     }
 
+    protected void fireTilesetChanged(int index) {
+        Iterator iterator = mapChangeListeners.iterator();
+        MapChangedEvent event = null;
+
+        while (iterator.hasNext()) {
+            if (event == null) event = new MapChangedEvent(this);
+            ((MapChangeListener) iterator.next()).tilesetChanged(event, index);
+        }
+    }
+    
     /**
      * Causes a MapChangedEvent to be fired.
      */
@@ -133,6 +143,11 @@
         fireMapChanged();
     }
 
+    public void touchTileset(TileSet set) {
+        int index = tilesets.indexOf(set);
+        fireTilesetChanged(index);
+    }
+    
     public void addLayerSpecial(MapLayer layer) {
         layer.setMap(this);
         specialLayers.add(layer);

Modified: branches/adam/src/tiled/core/Sprite.java
===================================================================
--- branches/adam/src/tiled/core/Sprite.java	2007-02-07 12:47:14 UTC (rev 724)
+++ branches/adam/src/tiled/core/Sprite.java	2007-02-08 10:30:33 UTC (rev 725)
@@ -157,6 +157,10 @@
         createKey("", frames, KeyFrame.KEY_LOOP);
     }
 
+    public void setFrameSize(Rectangle r) {
+        frameSize.setBounds(r);
+    }
+    
     public void setFrameSize(int w, int h) {
         frameSize.width = w;
         frameSize.height = h;

Modified: branches/adam/src/tiled/core/TileSet.java
===================================================================
--- branches/adam/src/tiled/core/TileSet.java	2007-02-07 12:47:14 UTC (rev 724)
+++ branches/adam/src/tiled/core/TileSet.java	2007-02-08 10:30:33 UTC (rev 725)
@@ -47,7 +47,8 @@
     private Rectangle tileDimensions;
     private int tileSpacing;
     private int tilesPerRow;
-    private String externalSource;
+    private String imageExternalSource,
+                    externalSource;
     private File tilebmpFile;
     private String name;
     private Color transparentColor;
@@ -118,7 +119,7 @@
      * @param tilebmp     the image to be used, must not be null
      * @param cutter      the tile cutter, must not be null
      */
-    private void importTileBitmap(BufferedImage tilebmp, TileCutter cutter)
+    public void importTileBitmap(BufferedImage tilebmp, TileCutter cutter)
     {
         assert tilebmp != null;
         assert cutter != null;
@@ -215,15 +216,26 @@
     }
 
     /**
-     * Sets the URI path of the external source of this tile set. By setting
-     * this, the set is implied to be external in all other operations.
+     * Sets the URI path of the external source of the tileset's image. 
+     * By passing a valid URI, the set is implied to be external in all other 
+     * operations. If set to <code>null</code>, the tileset is implied to
+     * have an embedded tileset image
      *
      * @param source a URI of the tileset image file
      */
+    public void setImageSource(String source) {
+        imageExternalSource = source;
+    }
+
+    /**
+     * Sets the URI path of the external source of the tileset itself.
+     *  
+     * @param source a URI of the tileset file
+     */
     public void setSource(String source) {
         externalSource = source;
     }
-
+    
     /**
      * Sets the base directory for the tileset
      *
@@ -243,6 +255,7 @@
         if (name != null) {
             tilebmpFile = new File(name);
             tilebmpFileLastModified = tilebmpFile.lastModified();
+            setImageSource(name);
         }
         else {
             tilebmpFile = null;
@@ -453,6 +466,16 @@
     }
 
     /**
+     * Returns the source of this tileset's image.
+     *
+     * @return a filename if tileset is external or <code>null</code> if
+     *         tileset is internal.
+     */
+    public String getImageSource() {
+        return imageExternalSource;
+    }
+
+    /**
      * Returns the source of this tileset.
      *
      * @return a filename if tileset is external or <code>null</code> if
@@ -461,7 +484,7 @@
     public String getSource() {
         return externalSource;
     }
-
+    
     /**
      * Returns the base directory for the tileset
      *
@@ -489,6 +512,20 @@
     }
 
     /**
+     * Returns <code>true</code> if this uses a tileset
+     * image
+     * 
+     * @return
+     */
+    public boolean hasTilesetImage() {
+        return tileSetImage != null;
+    }
+    
+    public Image getTilesetImage() {
+        return tileSetImage;
+    }
+    
+    /**
      * Returns the first global id connected to this tileset.
      *
      * @return first global id

Modified: branches/adam/src/tiled/io/ImageHelper.java
===================================================================
--- branches/adam/src/tiled/io/ImageHelper.java	2007-02-07 12:47:14 UTC (rev 724)
+++ branches/adam/src/tiled/io/ImageHelper.java	2007-02-08 10:30:33 UTC (rev 725)
@@ -63,8 +63,8 @@
      * @throws IOException 
      * @see java.awt.Toolkit#createImage(byte[] imagedata)
      */
-    static public Image bytesToImage(byte[] imageData) throws IOException {
-        return ImageIO.read(new ByteArrayInputStream(imageData)).getScaledInstance(-1, -1, Image.SCALE_DEFAULT);
+    static public BufferedImage bytesToImage(byte[] imageData) throws IOException {
+        return ImageIO.read(new ByteArrayInputStream(imageData));
     }
 
     /**

Modified: branches/adam/src/tiled/io/MapHelper.java
===================================================================
--- branches/adam/src/tiled/io/MapHelper.java	2007-02-07 12:47:14 UTC (rev 724)
+++ branches/adam/src/tiled/io/MapHelper.java	2007-02-08 10:30:33 UTC (rev 725)
@@ -107,7 +107,7 @@
         	PluginLogger logger = new PluginLogger();
             mw.setLogger(logger);
             mw.writeTileset(set, filename);
-            set.setSource(filename);
+            set.setImageSource(filename);
             reportPluginMessages(logger);
         } else {
             throw new Exception("Unsupported tileset format");
@@ -210,7 +210,7 @@
             	PluginLogger logger = new PluginLogger();
                 mr.setLogger(logger);
                 ret = mr.readTileset(file);
-                ret.setSource(file);
+                ret.setImageSource(file);
                 reportPluginMessages(logger);
             } else {
                 throw new Exception("Unsupported tileset format");

Modified: branches/adam/src/tiled/io/xml/XMLMapTransformer.java
===================================================================
--- branches/adam/src/tiled/io/xml/XMLMapTransformer.java	2007-02-07 12:47:14 UTC (rev 724)
+++ branches/adam/src/tiled/io/xml/XMLMapTransformer.java	2007-02-08 10:30:33 UTC (rev 725)
@@ -14,6 +14,7 @@
 
 import java.awt.Color;
 import java.awt.Image;
+import java.awt.image.BufferedImage;
 import java.io.*;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
@@ -191,7 +192,7 @@
         return o;
     }
 
-    private Image unmarshalImage(Node t, String baseDir) throws IOException
+    private Image unmarshalImage(Node t, String baseDir, boolean scale) throws IOException
     {
         Image img = null;
 
@@ -222,13 +223,17 @@
                         byte[] imageData = Base64.decode(charArray);
                         img = ImageHelper.bytesToImage(imageData);
 
-                        // Deriving a scaled instance, even if it has the same
-                        // size, somehow makes drawing of the tiles a lot
-                        // faster on various systems (seen on Linux, Windows
-                        // and MacOS X).
-                        img = img.getScaledInstance(
-                                img.getWidth(null), img.getHeight(null),
-                                Image.SCALE_FAST);
+                        //In the case of tileset images, we need a BufferedImage 
+                        //to be able to cut it up quickly. (scale=false)
+                        if(scale) {
+                            // Deriving a scaled instance, even if it has the same
+                            // size, somehow makes drawing of the tiles a lot
+                            // faster on various systems (seen on Linux, Windows
+                            // and MacOS X).
+                            img = img.getScaledInstance(
+                                    img.getWidth(null), img.getHeight(null),
+                                    Image.SCALE_FAST);
+                        }
                     }
                     break;
                 }
@@ -273,7 +278,7 @@
             if (tsNode != null)
             {
                 set = unmarshalTileset(tsNode);
-                if (set.getSource() != null) {
+                if (set.getImageSource() != null) {
                     logger.warn("Recursive external Tilesets are not supported.");
                 }
                 set.setSource(filename);
@@ -328,8 +333,7 @@
 
             ext.setFirstGid(firstGid);
             return ext;
-        }
-        else {
+        } else {
             int tileWidth = getAttribute(t, "tilewidth", map != null ? map.getTileWidth() : 0);
             int tileHeight = getAttribute(t, "tileheight", map != null ? map.getTileHeight() : 0);
             int tileSpacing = getAttribute(t, "spacing", 0);
@@ -378,10 +382,13 @@
 
                         set.importTileBitmap(sourcePath, new BasicTileCutter(
                                 tileWidth, tileHeight, tileSpacing, 0));
+                    } else if(imgSource == null && id == null) {
+                        BufferedImage image = (BufferedImage)unmarshalImage(child, tilesetBaseDir, false);
+                        set.importTileBitmap(image, new BasicTileCutter(
+                                tileWidth, tileHeight, tileSpacing, 0));
                     } else {
-                        Image image = unmarshalImage(child, tilesetBaseDir);
-                        String idValue = getAttributeValue(child, "id");
-                        int imageId = Integer.parseInt(idValue);
+                        Image image = unmarshalImage(child, tilesetBaseDir, true);
+                        int imageId = Integer.parseInt(id);
                         set.addImage(image, imageId);
                     }
                 }
@@ -473,7 +480,7 @@
             Node child = children.item(i);
             if ("image".equalsIgnoreCase(child.getNodeName())) {
                 int id = getAttribute(child, "id", -1);
-                Image img = unmarshalImage(child, baseDir);
+                Image img = unmarshalImage(child, baseDir, true);
                 if (id < 0) {
                     id = set.addImage(img);
                 }

Modified: branches/adam/src/tiled/io/xml/XMLMapWriter.java
===================================================================
--- branches/adam/src/tiled/io/xml/XMLMapWriter.java	2007-02-07 12:47:14 UTC (rev 724)
+++ branches/adam/src/tiled/io/xml/XMLMapWriter.java	2007-02-08 10:30:33 UTC (rev 725)
@@ -163,8 +163,9 @@
 
     /**
      * Writes a reference to an external tileset into a XML document.  In the
-     * degenerate case where the tileset is not stored in an external file,
+     * embedded case where the tileset is not stored in an external file,
      * writes the contents of the tileset instead.
+     * 
      */
     private static void writeTilesetReference(TileSet set, XMLWriter w, String wp)
         throws IOException {
@@ -191,7 +192,7 @@
     private static void writeTileset(TileSet set, XMLWriter w, String wp)
         throws IOException {
 
-        String tilebmpFile = set.getTilebmpFile();
+        String setImageSource = set.getImageSource();
         String name = set.getName();
 
         w.startElement("tileset");
@@ -202,7 +203,7 @@
 
         w.writeAttribute("firstgid", set.getFirstGid());
 
-        if (tilebmpFile != null) {
+        if (set.getTilebmpFile() != null) {
             w.writeAttribute("tilewidth", set.getTileWidth());
             w.writeAttribute("tileheight", set.getTileHeight());
 
@@ -210,22 +211,27 @@
             if (tileSpacing != 0) {
                 w.writeAttribute("spacing", tileSpacing);
             }
-        }
+        }    
 
         if (set.getBaseDir() != null) {
             w.writeAttribute("basedir", set.getBaseDir());
         }
 
-        if (tilebmpFile != null) {
-            w.startElement("image");
-            w.writeAttribute("source", getRelativePath(wp, tilebmpFile));
+        if (setImageSource != null || set.hasTilesetImage()) {
+            if(setImageSource != null) {
+                w.startElement("image");
+                
+                w.writeAttribute("source", getRelativePath(wp, setImageSource));
 
-            Color trans = set.getTransparentColor();
-            if (trans != null) {
-                w.writeAttribute("trans", Integer.toHexString(
-                            trans.getRGB()).substring(2));
+                Color trans = set.getTransparentColor();
+                if (trans != null) {
+                    w.writeAttribute("trans", Integer.toHexString(
+                                trans.getRGB()).substring(2));
+                }
+                w.endElement();
+            } else {
+                writeImage(set.getTilesetImage(), null, set.getTransparentColor(), w);
             }
-            w.endElement();
 
             // Write tile properties when necessary.
             Iterator tileIterator = set.iterator();
@@ -443,6 +449,23 @@
         w.endElement();
     }
 
+    private static void writeImage(Image image, String id, Color transparentColor, XMLWriter w) throws IOException {
+        w.startElement("image");
+        w.writeAttribute("format", "png");
+        if(id != null)
+            w.writeAttribute("id", id);
+        if (transparentColor != null) {
+            w.writeAttribute("trans", Integer.toHexString(
+                    transparentColor.getRGB()).substring(2));
+        }
+        w.startElement("data");
+        w.writeAttribute("encoding", "base64");
+        w.writeCDATA(new String(Base64.encode(
+                        ImageHelper.imageToPNG(image))));
+        w.endElement();
+        w.endElement();
+    }
+    
     /**
      * Used to write tile elements for tilesets not based on a tileset image.
      *
@@ -468,14 +491,7 @@
         // Write encoded data
         if (tileImage != null) {
             if (embedImages && !tileSetImages) {
-                w.startElement("image");
-                w.writeAttribute("format", "png");
-                w.startElement("data");
-                w.writeAttribute("encoding", "base64");
-                w.writeCDATA(new String(Base64.encode(
-                                ImageHelper.imageToPNG(tileImage))));
-                w.endElement();
-                w.endElement();
+                writeImage(tileImage, null, tile.getTileSet().getTransparentColor(), w);
             } else if (embedImages && tileSetImages) {
                 w.startElement("image");
                 w.writeAttribute("id", tile.getImageId());

Modified: branches/adam/src/tiled/mapeditor/MapEditor.java
===================================================================
--- branches/adam/src/tiled/mapeditor/MapEditor.java	2007-02-07 12:47:14 UTC (rev 724)
+++ branches/adam/src/tiled/mapeditor/MapEditor.java	2007-02-08 10:30:33 UTC (rev 725)
@@ -1361,6 +1361,13 @@
         mapView.repaint();
     }
 
+    public void tilesetChanged(MapChangedEvent e, int index) {
+        if (tilePaletteDialog != null) {
+            tilePaletteDialog.setMap(currentMap);
+        }
+        mapView.repaint();
+    }
+    
     public void valueChanged(ListSelectionEvent e) {
         int selectedRow = layerTable.getSelectedRow();
 

Modified: branches/adam/src/tiled/mapeditor/dialogs/TileDialog.java
===================================================================
--- branches/adam/src/tiled/mapeditor/dialogs/TileDialog.java	2007-02-07 12:47:14 UTC (rev 724)
+++ branches/adam/src/tiled/mapeditor/dialogs/TileDialog.java	2007-02-08 10:30:33 UTC (rev 725)
@@ -52,6 +52,7 @@
     private JList imageList;
     private JTable tileProperties;
     private JButton okButton;
+    private JButton packButton;
     private JButton addImagesButton;
     private JButton deleteTileButton;
     private JButton changeImageButton;
@@ -65,6 +66,7 @@
 
     private static final String DIALOG_TITLE = Resources.getString("dialog.tile.title");
     private static final String OK_BUTTON = Resources.getString("general.button.ok");
+    private static final String PACK_BUTTON = Resources.getString("dialog.tile.button.pack");
     private static final String DELETE_BUTTON = Resources.getString("dialog.tile.button.deletetile");
     private static final String CI_BUTTON = Resources.getString("dialog.tile.button.changeimage");
     private static final String ADD_IMAGES_BUTTON = Resources.getString("dialog.tile.button.addimages");
@@ -202,10 +204,14 @@
         tabs.addTab(IMAGES_TAB, createImagePanel());
 
         okButton = new JButton(OK_BUTTON);
-
+        packButton = new JButton(PACK_BUTTON);
+        packButton.addActionListener(this);
+        
         JPanel buttons = new VerticalStaticJPanel();
         buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
         buttons.add(Box.createGlue());
+        buttons.add(packButton);
+        buttons.add(Box.createRigidArea(new Dimension(5, 0)));
         buttons.add(okButton);
 
         JPanel mainPanel = new JPanel();
@@ -281,8 +287,8 @@
 
         if (tileset != null) {
             // Find new tile images at the location of the tileset
-            if (tileset.getSource() != null) {
-                location = tileset.getSource();
+            if (tileset.getImageSource() != null) {
+                location = tileset.getImageSource();
             } else if (map != null) {
                 location = map.getFilename();
             }
@@ -373,6 +379,7 @@
         boolean tileSelected = currentTile != null;
         boolean atLeastOneSharedImage = tileset.getTotalImages() >= 1;
         boolean imageSelected = imageList.getSelectedValue() != null;
+        boolean externalImageSource = tileset.getImageSource() != null;
 
         deleteTileButton.setEnabled(!tilebmp && tileSelected);
         changeImageButton.setEnabled(atLeastOneSharedImage && !tilebmp
@@ -383,6 +390,7 @@
         tileProperties.setEnabled(tileSelected);
         addImagesButton.setEnabled(!tilebmp);
         createTileButton.setEnabled(!tilebmp && imageSelected);
+        packButton.setEnabled(externalImageSource);
     }
 
     /**
@@ -477,6 +485,9 @@
                         TILE_CREATED_TITLE,
                         JOptionPane.INFORMATION_MESSAGE);
             }
+        } else if (source == packButton) {
+            tileset.setImageSource(null);
+            updateEnabledState();
         }
 
         repaint();

Modified: branches/adam/src/tiled/mapeditor/dialogs/TilesetManager.java
===================================================================
--- branches/adam/src/tiled/mapeditor/dialogs/TilesetManager.java	2007-02-07 12:47:14 UTC (rev 724)
+++ branches/adam/src/tiled/mapeditor/dialogs/TilesetManager.java	2007-02-08 10:30:33 UTC (rev 725)
@@ -29,6 +29,7 @@
 import tiled.core.*;
 import tiled.io.MapHelper;
 import tiled.io.MapWriter;
+import tiled.mapeditor.MapEditor;
 import tiled.mapeditor.Resources;
 import tiled.mapeditor.plugin.PluginClassLoader;
 import tiled.mapeditor.util.TiledFileFilter;
@@ -195,6 +196,7 @@
             }
         } else if (command.equals(EMBED_BUTTON)) {
             set.setSource(null);
+            map.touchTileset(set);
             embedButton.setEnabled(false);
             saveButton.setEnabled(false);
         }

Modified: branches/adam/src/tiled/mapeditor/resources/gui.properties
===================================================================
--- branches/adam/src/tiled/mapeditor/resources/gui.properties	2007-02-07 12:47:14 UTC (rev 724)
+++ branches/adam/src/tiled/mapeditor/resources/gui.properties	2007-02-08 10:30:33 UTC (rev 725)
@@ -157,6 +157,7 @@
 dialog.tile.button.deletetile=Delete Tile
 dialog.tile.button.duptile=Duplicate Tile
 dialog.tile.button.newtile=Add Tile
+dialog.tile.button.pack=Pack
 dialog.tile.image.load.error=Error loading image
 dialog.tile.imgload.error.message=Error while loading image:
 dialog.tile.imgload.error.title=Error while loading image

Added: branches/adam/src/tiled/mapeditor/undo/TileSetEdit.java
===================================================================
--- branches/adam/src/tiled/mapeditor/undo/TileSetEdit.java	2007-02-07 12:47:14 UTC (rev 724)
+++ branches/adam/src/tiled/mapeditor/undo/TileSetEdit.java	2007-02-08 10:30:33 UTC (rev 725)
@@ -0,0 +1,26 @@
+/*
+ *  Tiled Map Editor, (c) 2004-2007
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ * 
+ *  Adam Turk <aturk at biggeruniverse.com>
+ *  Bjorn Lindeijer <b.lindeijer at xs4all.nl>
+ */
+
+package tiled.mapeditor.undo;
+
+import javax.swing.undo.AbstractUndoableEdit;
+
+import tiled.core.TileSet;
+
+/**
+ * @version $Id$
+ */
+public class TileSetEdit extends AbstractUndoableEdit {
+
+    private TileSet editedSet;
+    
+}

Modified: branches/adam/src/tiled/mapeditor/util/MapChangeListener.java
===================================================================
--- branches/adam/src/tiled/mapeditor/util/MapChangeListener.java	2007-02-07 12:47:14 UTC (rev 724)
+++ branches/adam/src/tiled/mapeditor/util/MapChangeListener.java	2007-02-08 10:30:33 UTC (rev 725)
@@ -29,4 +29,6 @@
     public void tilesetAdded(MapChangedEvent e, TileSet tileset);
 
     public void tilesetRemoved(MapChangedEvent e, int index);
+    
+    public void tilesetChanged(MapChangedEvent e, int index);
 }

Modified: branches/adam/src/tiled/mapeditor/widget/TabbedTilesetsPane.java
===================================================================
--- branches/adam/src/tiled/mapeditor/widget/TabbedTilesetsPane.java	2007-02-07 12:47:14 UTC (rev 724)
+++ branches/adam/src/tiled/mapeditor/widget/TabbedTilesetsPane.java	2007-02-08 10:30:33 UTC (rev 725)
@@ -144,14 +144,16 @@
         public void mapChanged(MapChangedEvent e) {
         }
 
-        public void tilesetAdded(MapChangedEvent e, TileSet tileset)
-        {
+        public void tilesetAdded(MapChangedEvent e, TileSet tileset) {
             addTabForTileset(tileset);
         }
 
-        public void tilesetRemoved(MapChangedEvent e, int index)
-        {
+        public void tilesetRemoved(MapChangedEvent e, int index) {
             removeTabAt(index);
         }
+        
+        public void tilesetChanged(MapChangedEvent e, int index) {
+            recreateTabs(map.getTilesets());
+        }
     }
 }




More information about the tiled-commit mailing list