This commit is contained in:
Martin Haye 2014-04-20 09:20:44 -07:00
commit 175a2b12d7
3 changed files with 124 additions and 9 deletions

View File

@ -16,7 +16,11 @@ import javafx.scene.control.TextField;
import javafx.scene.control.cell.ComboBoxListCell;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.util.Callback;
import static org.badvision.outlaweditor.Application.currentPlatform;
import static org.badvision.outlaweditor.Application.gameData;
@ -833,6 +837,7 @@ public class ApplicationUIControllerImpl extends ApplicationUIController {
return null;
}
public static final DataFormat SCRIPT_DATA_FORMAT = new DataFormat("MythosScript");
public void redrawMapScripts() {
mapScriptsList.setOnEditStart(new EventHandler<ListView.EditEvent<Script>>() {
@Override
@ -840,10 +845,11 @@ public class ApplicationUIControllerImpl extends ApplicationUIController {
UIAction.editScript(event.getSource().getItems().get(event.getIndex()));
}
});
final DragDropHelper<Script> scriptDragDrop = new DragDropHelper<>(Script.class);
mapScriptsList.setCellFactory(new Callback<ListView<Script>, ListCell<Script>>() {
@Override
public ListCell<Script> call(ListView<Script> param) {
return new ListCell<Script>() {
final ListCell<Script> cell = new ListCell<Script>() {
@Override
protected void updateItem(Script item, boolean empty) {
super.updateItem(item, empty);
@ -851,9 +857,11 @@ public class ApplicationUIControllerImpl extends ApplicationUIController {
setText("");
} else {
setText(item.getName());
scriptDragDrop.registerDragSupport(this, item);
}
}
};
return cell;
}
});
if (currentMap == null) {

View File

@ -0,0 +1,91 @@
package org.badvision.outlaweditor;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
/**
* Simplify management of drag/drop operations
*
* @author blurry
* @param <T> Type of object being passed
*/
public class DragDropHelper<T> {
Class type;
DataFormat format;
static Random random = new Random();
static Map<String, Object> registry = new HashMap<>();
static Map<String, DataFormat> dataFormats = new HashMap<>();
public interface DropEventHandler<T> {
public void handle(T object, double x, double y);
}
private DragDropHelper() {
}
public DragDropHelper(Class<T> clazz) {
type = clazz;
if (!dataFormats.containsKey(clazz.getName())) {
dataFormats.put(clazz.getName(), new DataFormat(clazz.getName()));
}
format = dataFormats.get(clazz.getName());
}
public void registerDragSupport(final Node source, final T object) {
final String id = type.getName() + "_" + random.nextInt(999999999);
source.setOnDragDetected(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
registry.put(id, object);
Dragboard db = source.startDragAndDrop(TransferMode.LINK);
ClipboardContent content = new ClipboardContent();
content.put(format, id);
db.setContent(content);
event.consume();
}
});
source.setOnDragDone(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
registry.remove(id);
}
});
}
public void registerDropSupport(final Node target, final DropEventHandler<T> handler) {
target.setOnDragOver(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
Dragboard db = event.getDragboard();
if (db.getContentTypes().contains(format)) {
event.acceptTransferModes(TransferMode.LINK);
}
event.consume();
}
});
target.setOnDragDropped(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
Dragboard db = event.getDragboard();
if (db.getContentTypes().contains(format)) {
event.setDropCompleted(true);
String id = (String) db.getContent(format);
T object = (T) registry.get(id);
handler.handle(object, event.getX(), event.getY());
} else {
event.setDropCompleted(false);
}
event.consume();
}
});
}
}

View File

@ -20,17 +20,22 @@ import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.Clipboard;
import javafx.scene.input.DataFormat;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import static org.badvision.outlaweditor.Application.currentPlatform;
import org.badvision.outlaweditor.DragDropHelper.DropEventHandler;
import org.badvision.outlaweditor.data.TileMap;
import org.badvision.outlaweditor.data.TileUtils;
import org.badvision.outlaweditor.data.xml.Map;
import org.badvision.outlaweditor.data.xml.Script;
import org.badvision.outlaweditor.data.xml.Tile;
import static org.badvision.outlaweditor.Application.currentPlatform;
/**
*
@ -48,6 +53,7 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
TileMap currentMap;
double tileWidth = currentPlatform.tileRenderer.getWidth() * zoom;
double tileHeight = currentPlatform.tileRenderer.getHeight() * zoom;
public static DragDropHelper<Script> scriptDragDrop = new DragDropHelper<>(Script.class);
@Override
public void setEntity(Map t) {
@ -110,9 +116,19 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
drawCanvas.setOnMouseDragged(this);
drawCanvas.setOnMouseDragReleased(this);
drawCanvas.setOnMouseReleased(this);
scriptDragDrop.registerDropSupport(drawCanvas, new DropEventHandler<Script>() {
@Override
public void handle(Script script, double x, double y) {
assignScript(script, x, y);
}
});
anchorPane.getChildren().add(0, drawCanvas);
}
public void assignScript(Script script, double x, double y) {
System.out.println("Dropped " + script.getName() + " at " + x + "," + y);
}
public void togglePanZoom() {
for (Node n : anchorPane.getChildren()) {
if (n == drawCanvas) {
@ -270,20 +286,20 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
public void copy() {
byte[] data = currentPlatform.imageRenderer.renderPreview(currentMap, posX, posY, currentPlatform.maxImageWidth, currentPlatform.maxImageHeight);
WritableImage img = currentPlatform.imageRenderer.renderImage(null, data, currentPlatform.maxImageWidth, currentPlatform.maxImageHeight);
java.util.Map<DataFormat,Object> clip = new HashMap<>();
java.util.Map<DataFormat, Object> clip = new HashMap<>();
clip.put(DataFormat.IMAGE, img);
clip.put(DataFormat.PLAIN_TEXT, "selection/map/"+Application.gameData.getMap().indexOf(getEntity())+"/"+getSelectionInfo());
Clipboard.getSystemClipboard().setContent(clip);
clip.put(DataFormat.PLAIN_TEXT, "selection/map/" + Application.gameData.getMap().indexOf(getEntity()) + "/" + getSelectionInfo());
Clipboard.getSystemClipboard().setContent(clip);
}
@Override
public String getSelectedAllInfo() {
setSelectionArea(posX, posY, posX+19, posY+11);
setSelectionArea(posX, posY, posX + 19, posY + 11);
String result = getSelectionInfo();
setSelectionArea(0,0,0,0);
setSelectionArea(0, 0, 0, 0);
return result;
}
@Override
public void paste() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
@ -296,7 +312,7 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
@Override
public void selectNone() {
setSelectionArea(0,0,0,0);
setSelectionArea(0, 0, 0, 0);
}
public static enum DrawMode {