[tiled] r750 - in trunk: . src/tiled/mapeditor src/tiled/mapeditor/resources src/tiled/mapeditor/undo

tiled-svn at biggeruniverse.com tiled-svn at biggeruniverse.com
Sun Jun 15 05:02:09 PDT 2008


Author: bjorn
Date: 2008-06-15 07:02:08 -0500 (Sun, 15 Jun 2008)
New Revision: 750

Added:
   trunk/src/tiled/mapeditor/undo/AddObjectEdit.java
   trunk/src/tiled/mapeditor/undo/MoveObjectEdit.java
   trunk/src/tiled/mapeditor/undo/RemoveObjectEdit.java
Modified:
   trunk/TODO
   trunk/src/tiled/mapeditor/MapEditor.java
   trunk/src/tiled/mapeditor/resources/gui.properties
   trunk/src/tiled/mapeditor/undo/MapLayerEdit.java
   trunk/src/tiled/mapeditor/undo/MapLayerStateEdit.java
   trunk/src/tiled/mapeditor/undo/MoveLayerEdit.java
Log:
Added undo/redo support for adding, removing and moving objects.

Modified: trunk/TODO
===================================================================
--- trunk/TODO	2008-06-10 20:32:32 UTC (rev 749)
+++ trunk/TODO	2008-06-15 12:02:08 UTC (rev 750)
@@ -2,12 +2,11 @@
 
 0.7.0
 * Replace tile button with a brush preview beneath the layer table
-* Fix undo/redo for object group related actions
+* Fix undo/redo for object dialog
 
 0.7.1
 * Check that tile palette panel doesn't keep listening after being replaced
-* Update translations for dialog.saveas.confirm.mismatch and add translations
-  for import/export
+* Update translations for dialog.saveas.confirm.mismatch
 * Make continuous layout and tiles per row behaviour configurable
 * Fix default tabbed tilesets panel height
 * Fix redraw issue on empty area in tile palette panel

Modified: trunk/src/tiled/mapeditor/MapEditor.java
===================================================================
--- trunk/src/tiled/mapeditor/MapEditor.java	2008-06-10 20:32:32 UTC (rev 749)
+++ trunk/src/tiled/mapeditor/MapEditor.java	2008-06-15 12:02:08 UTC (rev 750)
@@ -44,10 +44,7 @@
 import tiled.mapeditor.dialogs.*;
 import tiled.mapeditor.plugin.PluginClassLoader;
 import tiled.mapeditor.selection.SelectionLayer;
-import tiled.mapeditor.undo.MapLayerEdit;
-import tiled.mapeditor.undo.MapLayerStateEdit;
-import tiled.mapeditor.undo.MoveLayerEdit;
-import tiled.mapeditor.undo.UndoHandler;
+import tiled.mapeditor.undo.*;
 import tiled.mapeditor.util.*;
 import tiled.mapeditor.widget.*;
 import tiled.util.TiledConfiguration;
@@ -98,6 +95,7 @@
     private boolean bMouseIsDown, bMouseIsDragging;
     private SelectionLayer cursorHighlight;
     private Point mousePressLocation, mouseInitialPressLocation;
+    private Point mouseLastPixelLocation;
     private Point mouseInitialScreenLocation;
     private Point moveDist;
     private int mouseButton;
@@ -974,13 +972,15 @@
                     }
                     break;
                 case PS_MOVE:
-                    Point translation = new Point(
-                            tile.x - mousePressLocation.x,
-                            tile.y - mousePressLocation.y);
+                    if (layer instanceof TileLayer) {
+                        Point translation = new Point(
+                                tile.x - mousePressLocation.x,
+                                tile.y - mousePressLocation.y);
 
-                    layer.translate(translation.x, translation.y);
-                    moveDist.translate(translation.x, translation.y);
-                    mapView.repaint();
+                        layer.translate(translation.x, translation.y);
+                        moveDist.translate(translation.x, translation.y);
+                        mapView.repaint();
+                    }
                     break;
                 case PS_MARQUEE:
                     if (!(layer instanceof TileLayer)) {
@@ -1046,6 +1046,7 @@
                                 event.getX(), event.getY());
                         MapObject obj = group.getObjectAt(pos.x, pos.y);
                         if (obj != null) {
+                            undoSupport.postEdit(new RemoveObjectEdit(group, obj));
                             group.removeObject(obj);
                             // TODO: repaint only affected area
                             mapView.repaint();
@@ -1054,20 +1055,24 @@
                     break;
                 case PS_MOVEOBJ:
                     if (layer instanceof ObjectGroup) {
-                        ObjectGroup group = (ObjectGroup) layer;
                         Point pos = mapView.screenToPixelCoords(
                                 event.getX(), event.getY());
                         if (currentObject == null) {
+                            ObjectGroup group = (ObjectGroup) layer;
                             currentObject = group.getObjectAt(pos.x, pos.y);
                             if (currentObject == null) { // No object to move
                                 break;
                             }
-                            moveDist = new Point(
-                                    currentObject.getX() - pos.x,
-                                    currentObject.getY() - pos.y);
+                            mouseLastPixelLocation = pos;
+                            moveDist = new Point(0, 0);
+                            break;
                         }
-                        currentObject.setX(pos.x + moveDist.x);
-                        currentObject.setY(pos.y + moveDist.y);
+                        Point translation = new Point(
+                                pos.x - mouseLastPixelLocation.x,
+                                pos.y - mouseLastPixelLocation.y);
+                        currentObject.translate(translation.x, translation.y);
+                        moveDist.translate(translation.x, translation.y);
+                        mouseLastPixelLocation = pos;
                         mapView.repaint();
                     }
                     break;
@@ -1168,13 +1173,19 @@
             //    tileInstancePropertiesDialog.setSelection(marqueeSelection);
             //}
         } else if (currentPointerState == PS_MOVE) {
-            if (layer != null && moveDist.x != 0 || moveDist.x != 0) {
+            if (layer != null && (moveDist.x != 0 || moveDist.x != 0)) {
                 undoSupport.postEdit(new MoveLayerEdit(layer, moveDist));
             }
         } else if (currentPointerState == PS_PAINT) {
             if (layer instanceof TileLayer) {
                 currentBrush.endPaint();
             }
+        } else if (currentPointerState == PS_MOVEOBJ) {
+            if (layer instanceof ObjectGroup && currentObject != null &&
+                    (moveDist.x != 0 || moveDist.x != 0)) {
+                undoSupport.postEdit(
+                        new MoveObjectEdit(currentObject, moveDist));
+            }
         }
 
 
@@ -1219,7 +1230,9 @@
                         bounds.height * h);
                 /*Point pos = mapView.screenToPixelCoords(
                         event.getX(), event.getY());*/
-                ((ObjectGroup) layer).addObject(object);
+                ObjectGroup group = (ObjectGroup) layer;
+                undoSupport.postEdit(new AddObjectEdit(group, object));
+                group.addObject(object);
                 mapView.repaint();
             }
 

Modified: trunk/src/tiled/mapeditor/resources/gui.properties
===================================================================
--- trunk/src/tiled/mapeditor/resources/gui.properties	2008-06-10 20:32:32 UTC (rev 749)
+++ trunk/src/tiled/mapeditor/resources/gui.properties	2008-06-15 12:02:08 UTC (rev 750)
@@ -43,6 +43,9 @@
 action.map.save.tooltip=Save current map
 action.map.saveas.name=Save As...
 action.map.saveas.tooltip=Save current map as new file
+action.object.add.name=Add Object
+action.object.move.name=Move Object
+action.object.remove.name=Remove Object
 action.objectgroup.add.name=Add Object Group
 action.objectgroup.add.tooltip=Add a object group
 action.paste.name=Paste

Added: trunk/src/tiled/mapeditor/undo/AddObjectEdit.java
===================================================================
--- trunk/src/tiled/mapeditor/undo/AddObjectEdit.java	                        (rev 0)
+++ trunk/src/tiled/mapeditor/undo/AddObjectEdit.java	2008-06-15 12:02:08 UTC (rev 750)
@@ -0,0 +1,49 @@
+/*
+ *  Tiled Map Editor, (c) 2008
+ *
+ *  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 tiled.core.ObjectGroup;
+import tiled.core.MapObject;
+import tiled.mapeditor.Resources;
+
+import javax.swing.undo.AbstractUndoableEdit;
+import javax.swing.undo.CannotUndoException;
+import javax.swing.undo.CannotRedoException;
+
+/**
+ * Adds an object to an object group.
+ */
+public class AddObjectEdit extends AbstractUndoableEdit
+{
+    private final ObjectGroup objectGroup;
+    private final MapObject mapObject;
+
+    public AddObjectEdit(ObjectGroup objectGroup, MapObject mapObject) {
+        this.objectGroup = objectGroup;
+        this.mapObject = mapObject;
+    }
+
+    public void undo() throws CannotUndoException {
+        super.undo();
+        objectGroup.removeObject(mapObject);
+    }
+
+    public void redo() throws CannotRedoException {
+        super.redo();
+        objectGroup.addObject(mapObject);
+    }
+
+    public String getPresentationName() {
+        return Resources.getString("action.object.add.name");
+    }
+}

Modified: trunk/src/tiled/mapeditor/undo/MapLayerEdit.java
===================================================================
--- trunk/src/tiled/mapeditor/undo/MapLayerEdit.java	2008-06-10 20:32:32 UTC (rev 749)
+++ trunk/src/tiled/mapeditor/undo/MapLayerEdit.java	2008-06-15 12:02:08 UTC (rev 750)
@@ -24,7 +24,7 @@
  */
 public class MapLayerEdit extends AbstractUndoableEdit
 {
-    private MapLayer editedLayer;
+    private final MapLayer editedLayer;
     private MapLayer layerUndo, layerRedo;
     private String name;
     private boolean inProgress;

Modified: trunk/src/tiled/mapeditor/undo/MapLayerStateEdit.java
===================================================================
--- trunk/src/tiled/mapeditor/undo/MapLayerStateEdit.java	2008-06-10 20:32:32 UTC (rev 749)
+++ trunk/src/tiled/mapeditor/undo/MapLayerStateEdit.java	2008-06-15 12:02:08 UTC (rev 750)
@@ -20,10 +20,10 @@
 
 public class MapLayerStateEdit extends AbstractUndoableEdit
 {
-    private Map map;
-    private Vector layersBefore;
-    private Vector layersAfter;
-    private String name;
+    private final Map map;
+    private final Vector layersBefore;
+    private final Vector layersAfter;
+    private final String name;
 
     public MapLayerStateEdit(Map m, Vector before, Vector after, String name) {
         map = m;

Modified: trunk/src/tiled/mapeditor/undo/MoveLayerEdit.java
===================================================================
--- trunk/src/tiled/mapeditor/undo/MoveLayerEdit.java	2008-06-10 20:32:32 UTC (rev 749)
+++ trunk/src/tiled/mapeditor/undo/MoveLayerEdit.java	2008-06-15 12:02:08 UTC (rev 750)
@@ -16,12 +16,13 @@
 import javax.swing.undo.*;
 
 import tiled.core.*;
+import tiled.mapeditor.Resources;
 
 
 public class MoveLayerEdit extends AbstractUndoableEdit
 {
-    private MapLayer layer;
-    private Point moveDist;
+    private final MapLayer layer;
+    private final Point moveDist;
 
     public MoveLayerEdit(MapLayer layer, Point moveDist) {
         this.layer = layer;
@@ -39,6 +40,6 @@
     }
 
     public String getPresentationName() {
-        return "Move";
+        return Resources.getString("action.layer.move.name");
     }
 }

Added: trunk/src/tiled/mapeditor/undo/MoveObjectEdit.java
===================================================================
--- trunk/src/tiled/mapeditor/undo/MoveObjectEdit.java	                        (rev 0)
+++ trunk/src/tiled/mapeditor/undo/MoveObjectEdit.java	2008-06-15 12:02:08 UTC (rev 750)
@@ -0,0 +1,49 @@
+/*
+ *  Tiled Map Editor, (c) 2008
+ *
+ *  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 tiled.core.MapObject;
+import tiled.mapeditor.Resources;
+
+import javax.swing.undo.AbstractUndoableEdit;
+import javax.swing.undo.CannotUndoException;
+import javax.swing.undo.CannotRedoException;
+import java.awt.*;
+
+/**
+ * Moves an object.
+ */
+public class MoveObjectEdit extends AbstractUndoableEdit
+{
+    private final MapObject mapObject;
+    private final Point moveDist;
+
+    public MoveObjectEdit(MapObject mapObject, Point moveDist) {
+        this.mapObject = mapObject;
+        this.moveDist = moveDist;
+    }
+
+    public void undo() throws CannotUndoException {
+        super.undo();
+        mapObject.translate(-moveDist.x, -moveDist.y);
+    }
+
+    public void redo() throws CannotRedoException {
+        super.redo();
+        mapObject.translate(moveDist.x, moveDist.y);
+    }
+
+    public String getPresentationName() {
+        return Resources.getString("action.object.move.name");
+    }
+}

Added: trunk/src/tiled/mapeditor/undo/RemoveObjectEdit.java
===================================================================
--- trunk/src/tiled/mapeditor/undo/RemoveObjectEdit.java	                        (rev 0)
+++ trunk/src/tiled/mapeditor/undo/RemoveObjectEdit.java	2008-06-15 12:02:08 UTC (rev 750)
@@ -0,0 +1,49 @@
+/*
+ *  Tiled Map Editor, (c) 2008
+ *
+ *  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 tiled.core.ObjectGroup;
+import tiled.core.MapObject;
+import tiled.mapeditor.Resources;
+
+import javax.swing.undo.AbstractUndoableEdit;
+import javax.swing.undo.CannotUndoException;
+import javax.swing.undo.CannotRedoException;
+
+/**
+ * Removes an object from an object group.
+ */
+public class RemoveObjectEdit extends AbstractUndoableEdit
+{
+    private final ObjectGroup objectGroup;
+    private final MapObject mapObject;
+
+    public RemoveObjectEdit(ObjectGroup objectGroup, MapObject mapObject) {
+        this.objectGroup = objectGroup;
+        this.mapObject = mapObject;
+    }
+
+    public void undo() throws CannotUndoException {
+        super.undo();
+        objectGroup.addObject(mapObject);
+    }
+
+    public void redo() throws CannotRedoException {
+        super.redo();
+        objectGroup.removeObject(mapObject);
+    }
+
+    public String getPresentationName() {
+        return Resources.getString("action.object.remove.name");
+    }
+}




More information about the tiled-commit mailing list