Undo support feature; partially working.

This commit is contained in:
Brendan Robert 2016-01-30 08:48:46 -06:00
parent d975768c62
commit 6854b8d744
9 changed files with 87 additions and 12 deletions

View File

@ -60,6 +60,17 @@
</schemaIncludes>
<episodeFile>${project.build.directory}/generated-sources/xjc/META-INF/jaxb-OutlawSchema.episode</episodeFile>
<generatePackage>org.badvision.outlaweditor.data.xml</generatePackage>
<args>
<arg>-Xcopyable</arg>
<arg>-Xmergeable</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.11.0</version>
</plugin>
</plugins>
</configuration>
<id>jaxb-generate-OutlawSchema</id>
</execution>
@ -69,7 +80,7 @@
</build>
<dependencies>
<!-- <dependency>
<!-- <dependency>
<groupId>com.oracle</groupId>
<artifactId>javafx</artifactId>
<version>2</version>
@ -90,5 +101,10 @@
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.11.0</version>
</dependency>
</dependencies>
</project>

View File

@ -10,10 +10,15 @@
package org.badvision.outlaweditor;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.layout.Pane;
import javax.xml.bind.JAXBException;
import org.badvision.outlaweditor.data.DataObserver;
import org.badvision.outlaweditor.data.DataProducer;
import org.badvision.outlaweditor.data.xml.Script;
import org.jvnet.jaxb2_commons.lang.CopyTo2;
/**
* Extremely generic editor abstraction -- useful for uniform edit features
@ -22,6 +27,8 @@ import org.badvision.outlaweditor.data.xml.Script;
* @author brobert
*/
public abstract class Editor<T, D> implements DataObserver<T> {
public static final int UNDO_HISTORY_LENGTH = 10;
LinkedList<T> undoStates = new LinkedList<>();
T editEntity;
@ -84,4 +91,23 @@ public abstract class Editor<T, D> implements DataObserver<T> {
}
abstract public void redraw();
protected void trackState() {
if (undoStates.size() >= UNDO_HISTORY_LENGTH) {
undoStates.removeLast();
}
try {
undoStates.push(TransferHelper.cloneObject(getEntity(), (Class<T>) getEntity().getClass(), getEntity().getClass().getName()));
} catch (JAXBException ex) {
Logger.getLogger(Editor.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void undo() {
if (!undoStates.isEmpty()) {
CopyTo2 undoState = (CopyTo2) undoStates.removeFirst();
undoState.copyTo(getEntity());
redraw();
}
}
}

View File

@ -7,7 +7,6 @@
* ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.badvision.outlaweditor;
import java.io.ByteArrayInputStream;
@ -19,6 +18,8 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
@ -30,6 +31,9 @@ import javafx.stage.FileChooser;
*/
public class FileUtils {
private FileUtils() {
}
public static enum Extension {
XML("XML files", "xml"),
@ -64,7 +68,9 @@ public class FileUtils {
}
if (create) {
File file = f.showSaveDialog(Application.getPrimaryStage());
if (file == null) return null;
if (file == null) {
return null;
}
if (!file.getName().contains(".")) {
return new File(file.getParentFile(), file.getName() + "." + supportedExtensions[0].extension);
} else {
@ -132,8 +138,8 @@ public class FileUtils {
}
public static byte[] decompress(byte[] bytesToDecompress) {
int numberOfBytesToDecompress = bytesToDecompress.length;
try {
int numberOfBytesToDecompress = bytesToDecompress.length;
Inflater inflater = new Inflater();
inflater.setInput(
bytesToDecompress,
@ -152,9 +158,9 @@ public class FileUtils {
numberOfBytesAfterDecompression);
inflater.end();
return returnValues;
} catch (DataFormatException dfe) {
dfe.printStackTrace(System.err);
} catch (DataFormatException ex) {
Logger.getLogger(FileUtils.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
return null;
}
}

View File

@ -580,6 +580,7 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
if (canSkip) {
return;
}
trackState();
plot(x,y,null);
redraw();
break;
@ -588,22 +589,26 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
if (canSkip) {
return;
}
trackState();
plot(x, y, getCurrentTile());
break;
case Pencil3px:
if (canSkip) {
return;
}
trackState();
drawBrush(x, y, 2, getCurrentTile());
break;
case Pencil5px:
if (canSkip) {
return;
}
trackState();
drawBrush(x, y, 5, getCurrentTile());
break;
case FilledRect:
if (t.getEventType().equals(MouseEvent.MOUSE_RELEASED)) {
trackState();
fillSelection(t.getX(), t.getY());
} else if (t.isPrimaryButtonDown()) {
updateSelection(t.getX(), t.getY());

View File

@ -33,10 +33,10 @@ import org.badvision.outlaweditor.Application;
import org.badvision.outlaweditor.FileUtils;
import org.badvision.outlaweditor.ImageEditor;
import org.badvision.outlaweditor.Platform;
import org.badvision.outlaweditor.ui.UIAction;
import org.badvision.outlaweditor.data.TileMap;
import org.badvision.outlaweditor.data.xml.Image;
import org.badvision.outlaweditor.data.xml.PlatformData;
import org.badvision.outlaweditor.ui.UIAction;
/**
*
@ -242,6 +242,7 @@ public class AppleImageEditor extends ImageEditor implements EventHandler<MouseE
if (canSkip) {
return false;
}
trackState();
if (alt) {
toggleHiBit(x, y);
} else {
@ -253,6 +254,7 @@ public class AppleImageEditor extends ImageEditor implements EventHandler<MouseE
if (canSkip) {
return false;
}
trackState();
plot(x, y, currentFillPattern, hiBitMatters); // [ref BigBlue1_30]
redrawScanline(y);
break;
@ -260,12 +262,14 @@ public class AppleImageEditor extends ImageEditor implements EventHandler<MouseE
if (canSkip) {
return false;
}
trackState();
drawBrush(x, y, 3, currentFillPattern, hiBitMatters);
break;
case Pencil5px:
if (canSkip) {
return false;
}
trackState();
drawBrush(x, y, 5, currentFillPattern, hiBitMatters);
break;
case Rectangle:

View File

@ -10,7 +10,6 @@
package org.badvision.outlaweditor.apple;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.control.Menu;
import javafx.scene.image.WritableImage;
@ -22,8 +21,8 @@ import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import org.badvision.outlaweditor.Platform;
import org.badvision.outlaweditor.TileEditor;
import org.badvision.outlaweditor.data.xml.Tile;
import org.badvision.outlaweditor.data.TileUtils;
import org.badvision.outlaweditor.data.xml.Tile;
/**
*
@ -99,6 +98,7 @@ public class AppleTileEditor extends TileEditor {
debounceTime = System.currentTimeMillis() + SAFE_WAIT_TIME;
lastActionX = x;
lastActionY = y;
trackState();
switch (drawMode) {
case Toggle:
if (alt) {

View File

@ -58,4 +58,7 @@ public abstract class ApplicationMenuController {
@FXML
abstract public void onHelpAbout(ActionEvent event);
@FXML
abstract public void performUndo(ActionEvent event);
}

View File

@ -15,6 +15,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import org.badvision.outlaweditor.Application;
import org.badvision.outlaweditor.Editor;
import org.badvision.outlaweditor.Platform;
import org.badvision.outlaweditor.apple.AppleTileRenderer;
import org.badvision.outlaweditor.ui.ApplicationMenuController;
@ -127,4 +128,12 @@ public class ApplicationMenuControllerImpl extends ApplicationMenuController {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void performUndo(ActionEvent event) {
Editor editor = ApplicationUIController.getController().getVisibleEditor();
if (editor != null) {
editor.undo();
}
}
}

View File

@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.input.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<MenuBar styleClass="menu" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.badvision.outlaweditor.ui.impl.ApplicationMenuControllerImpl">
<MenuBar styleClass="menu" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.badvision.outlaweditor.ui.impl.ApplicationMenuControllerImpl">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
@ -21,6 +22,11 @@
<MenuItem mnemonicParsing="false" onAction="#onEditSelect" text="Select" />
<MenuItem mnemonicParsing="false" onAction="#onEditCopy" text="Copy" />
<MenuItem mnemonicParsing="false" onAction="#onEditPaste" text="Paste" />
<MenuItem mnemonicParsing="false" onAction="#performUndo" text="Undo">
<accelerator>
<KeyCodeCombination alt="UP" code="Z" control="DOWN" meta="UP" shift="UP" shortcut="UP" />
</accelerator>
</MenuItem>
<Menu mnemonicParsing="false" text="Change Platform">
<items>
<MenuItem mnemonicParsing="false" onAction="#onChangePlatformAppleSolid" text="Apple (solid)" />
@ -38,4 +44,4 @@
</items>
</Menu>
</menus>
</MenuBar>
</MenuBar>