Skip to content

Commit

Permalink
Object marquee tool semantics now uses undo. ChangeObjectEdit refacto…
Browse files Browse the repository at this point in the history
…red to use memento pattern
  • Loading branch information
Uwe Pachler committed Nov 24, 2008
1 parent b88e082 commit b22b7cd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 42 deletions.
Expand Up @@ -13,17 +13,20 @@
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import javax.swing.undo.UndoableEdit;
import tiled.core.MapLayer;
import tiled.core.MapObject;
import tiled.core.ObjectGroup;
import tiled.mapeditor.MapEditor;
import tiled.mapeditor.undo.ChangeObjectEdit;
import tiled.view.MapView;

/**
*
* @author upachler
*/
public class ObjectSelectionToolSemantic extends ToolSemantic{
private UndoableEdit undoableEdit = null;;

private enum Mode {
IDLE,
Expand Down Expand Up @@ -236,6 +239,7 @@ private void startMoveObject(int x, int y){
return;
mode = Mode.MOVE_OBJECT;
this.object = o;
undoableEdit = new ChangeObjectEdit(object);
MapView mapView = getEditor().getMapView();
selectedLayer = getEditor().getCurrentLayer();
selectionStart = mapView.screenToPixelCoords(selectedLayer, x, y);
Expand All @@ -262,6 +266,8 @@ private void updateMoveObject(int x, int y){
private void finishMoveObject(int x, int y){
if(mode != Mode.MOVE_OBJECT)
return;
getEditor().getUndoSupport().postEdit(undoableEdit);
undoableEdit = null;
mode = Mode.IDLE;
}

Expand All @@ -271,6 +277,7 @@ private void startResizeObject(int x, int y) {
return;
mode = Mode.RESIZE_OBJECT;
this.object = o;
undoableEdit = new ChangeObjectEdit(object);
MapView mapView = getEditor().getMapView();
selectedLayer = getEditor().getCurrentLayer();
selectionStart = mapView.screenToPixelCoords(selectedLayer, x, y);
Expand All @@ -284,6 +291,7 @@ private void finishResizeObject(int x, int y) {
if(mode != Mode.RESIZE_OBJECT)
return;
mode = Mode.IDLE;
getEditor().getUndoSupport().postEdit(undoableEdit);
}

private void updateResizeObject(int x, int y) {
Expand Down
81 changes: 39 additions & 42 deletions src/tiled/mapeditor/undo/ChangeObjectEdit.java
Expand Up @@ -12,6 +12,7 @@

package tiled.mapeditor.undo;

import java.awt.Rectangle;
import java.util.Properties;
import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
Expand All @@ -26,66 +27,62 @@
public class ChangeObjectEdit extends AbstractUndoableEdit
{
private final MapObject mapObject;
private State state;

private final String prevName;
private final String prevType;
private final String prevImageSource;
private final int prevWidth;
private final int prevHeight;
private final Properties prevProperties = new Properties();

private String newName;
private String newType;
private String newImageSource;
private int newWidth;
private int newHeight;
private final Properties newProperties = new Properties();

class State{
private String name;
private String type;
private String imageSource;
private Rectangle bounds;
private final Properties properties = new Properties();

public void retreive(MapObject o){
name = mapObject.getName();
type = mapObject.getType();
imageSource = mapObject.getImageSource();
bounds = new Rectangle(mapObject.getBounds());
properties.clear();
properties.putAll(mapObject.getProperties());
}

public void apply(MapObject mapObject){
mapObject.setName(name);
mapObject.setType(type);
mapObject.setImageSource(imageSource);
mapObject.setBounds(new Rectangle(bounds));
mapObject.getProperties().clear();
mapObject.getProperties().putAll(properties);
}
};

public ChangeObjectEdit(MapObject mapObject) {
this.mapObject = mapObject;

// Store the previous state so we can undo changes
prevName = mapObject.getName();
prevType = mapObject.getType();
prevImageSource = mapObject.getImageSource();
prevWidth = mapObject.getWidth();
prevHeight = mapObject.getHeight();
prevProperties.putAll(mapObject.getProperties());
state = new State();
state.retreive(mapObject);
}

public void undo() throws CannotUndoException {
super.undo();

// Store the current state so we can redo changes
newName = mapObject.getName();
newType = mapObject.getType();
newImageSource = mapObject.getImageSource();
newWidth = mapObject.getWidth();
newHeight = mapObject.getHeight();
newProperties.clear();
newProperties.putAll(mapObject.getProperties());

mapObject.setName(prevName);
mapObject.setType(prevType);
mapObject.setImageSource(prevImageSource);
mapObject.setWidth(prevWidth);
mapObject.setHeight(prevHeight);
mapObject.getProperties().clear();
mapObject.getProperties().putAll(prevProperties);
swap();
}

public void redo() throws CannotRedoException {
super.redo();

mapObject.setName(newName);
mapObject.setType(newType);
mapObject.setImageSource(newImageSource);
mapObject.setWidth(newWidth);
mapObject.setHeight(newHeight);
mapObject.getProperties().clear();
mapObject.getProperties().putAll(newProperties);
swap();
}

private void swap() {
State s = new State();
s.retreive(mapObject);
state.apply(mapObject);
state = s;
}

public String getPresentationName() {
return Resources.getString("action.object.change.name");
}
Expand Down

0 comments on commit b22b7cd

Please sign in to comment.