Added context menu to map scripts list, allowing you to copy, delete, and clear from map

This commit is contained in:
Brendan Robert 2017-09-23 23:18:41 -05:00
parent 35969f4127
commit e185009203
4 changed files with 128 additions and 58 deletions

View File

@ -33,7 +33,6 @@ import org.badvision.outlaweditor.ui.ApplicationUIController;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.launch.Framework;
import org.osgi.util.tracker.ServiceTracker;

View File

@ -44,6 +44,7 @@ import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javax.xml.bind.JAXBException;
import org.badvision.outlaweditor.api.ApplicationState;
import org.badvision.outlaweditor.data.TileMap;
import org.badvision.outlaweditor.data.TileUtils;
@ -213,6 +214,11 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
redraw();
}
public void clearScriptTriggers(Script s) {
getCurrentMap().clearScriptTriggersFromMap(s);
redraw();
}
public void togglePanZoom() {
anchorPane.getChildren().stream().filter((n) -> !(n == drawCanvas)).forEach((n) -> {
n.setVisible(!n.isVisible());
@ -485,6 +491,14 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
stage.show();
}
public void copyScript(Script s) {
java.util.Map<DataFormat, Object> clip = new HashMap<>();
clip.put(DataFormat.PLAIN_TEXT, "selection/map/" +
ApplicationState.getInstance().getGameData().getMap().indexOf(getEntity()) +
"/script/" + getEntity().getScripts().getScript().indexOf(s));
Clipboard.getSystemClipboard().setContent(clip);
}
@Override
public void copy() {
byte[] data = getCurrentPlatform().imageRenderer.renderPreview(currentMap, posX, posY, getCurrentPlatform().maxImageWidth, getCurrentPlatform().maxImageHeight);
@ -510,6 +524,20 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
if (Clipboard.getSystemClipboard().hasContent(DataFormat.PLAIN_TEXT)) {
String clipboardInfo = (String) Clipboard.getSystemClipboard().getContent(DataFormat.PLAIN_TEXT);
java.util.Map<String, Integer> selection = TransferHelper.getSelectionDetails(clipboardInfo);
if (selection.containsKey("script")) {
Map sourceMap = ApplicationState.getInstance().getGameData().getMap().get(selection.get("map"));
Script sourceScript = sourceMap.getScripts().getScript().get(selection.get("script"));
try {
Script cloneScript = TransferHelper.cloneObject(sourceScript, Script.class, "script");
if (sourceMap.equals(getEntity())) {
cloneScript.setName(cloneScript.getName() + " CLONE");
}
addScript(cloneScript);
ApplicationState.getInstance().getApplicationUI().redrawScripts();
} catch (JAXBException ex) {
Logger.getLogger(MapEditor.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
if (selection.containsKey("map")) {
trackState();
Map sourceMap = ApplicationState.getInstance().getGameData().getMap().get(selection.get("map"));
@ -531,6 +559,7 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
}
}
}
}
@Override
public void select() {

View File

@ -225,11 +225,15 @@ public class TileMap extends ArrayList<ArrayList<Tile>> implements Serializable
return tileId.equalsIgnoreCase(NULL_TILE_ID);
}
public void removeScriptFromMap(Script script) {
public void clearScriptTriggersFromMap(Script script) {
script.getLocationTrigger().clear();
locationScripts.values().stream().filter((scripts) -> !(scripts == null)).forEach((scripts) -> {
scripts.remove(script);
});
}
public void removeScriptFromMap(Script script) {
clearScriptTriggersFromMap(script);
backingMap.getScripts().getScript().remove(script);
}
}

View File

@ -10,14 +10,13 @@
package org.badvision.outlaweditor.ui.impl;
import java.util.HashMap;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.event.ActionEvent;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.Menu;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.*;
import javafx.scene.control.cell.ComboBoxListCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
@ -218,17 +217,7 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
@Override
public void onMapScriptDeletePressed(ActionEvent event) {
Script script = mapScriptsList.getSelectionModel().getSelectedItem();
if (script != null) {
UIAction.confirm(
"Are you sure you want to delete the script "
+ script.getName()
+ "? There is no undo for this!",
() -> {
getCurrentEditor().removeScript(script);
redrawMapScripts();
},
null);
}
deleteScript(script);
}
@Override
@ -378,9 +367,9 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
toolDragDrop.registerDragSupport(scriptEraseTool, ToolType.ERASER);
mapScriptsList.getSelectionModel().selectedItemProperty().addListener((val, oldValue, newValue) -> {
if (getCurrentEditor() != null) {
if (newValue == null &&
getCurrentEditor().getDrawMode() == MapEditor.DrawMode.ScriptPencil &&
getCurrentEditor().getSelectedScript() != null) {
if (newValue == null
&& getCurrentEditor().getDrawMode() == MapEditor.DrawMode.ScriptPencil
&& getCurrentEditor().getSelectedScript() != null) {
mapScriptsList.getSelectionModel().select(oldValue);
} else {
getCurrentEditor().setSelectedScript(newValue);
@ -446,6 +435,7 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
super.updateItem(item, empty);
if (empty || item == null) {
setText("");
setContextMenu(null);
} else {
ImageView visibleIcon = getVisibleIcon(item);
visibleIcon.setOnMouseClicked((e) -> {
@ -458,6 +448,7 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
setFont(Font.font(null, FontWeight.BOLD, 12.0));
scriptDragDrop.registerDragSupport(this, item);
visibleIcon.setMouseTransparent(false);
setContextMenu(generateContextMenu(item));
}
}
});
@ -471,6 +462,53 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
}
}
private ContextMenu generateContextMenu(Script script) {
ContextMenu menu = new ContextMenu(
createMenuItem("Copy", script, s -> copyScript(s)),
createMenuItem("Clear from map", script, s -> clearScriptTriggersFromMap(s)),
createMenuItem("Delete", script, s -> deleteScript(s))
);
return menu;
}
private <T> MenuItem createMenuItem(String title, T selection, Consumer<T> action) {
MenuItem item = new MenuItem(title);
item.setOnAction(e -> action.accept(selection));
return item;
}
private void clearScriptTriggersFromMap(Script s) {
if (s != null) {
UIAction.confirm(
"This will remove all tile assignments for "
+ s.getName()
+ ". There is no undo for this! Are you sure?",
() -> {
getCurrentEditor().clearScriptTriggers(s);
redrawMapScripts();
},
null);
}
}
private void copyScript(Script s) {
getCurrentEditor().copyScript(s);
}
private void deleteScript(Script s) {
if (s != null) {
UIAction.confirm(
"Are you sure you want to delete the script "
+ s.getName()
+ "? There is no undo for this!",
() -> {
getCurrentEditor().removeScript(s);
redrawMapScripts();
},
null);
}
}
public static final Image VISIBLE_IMAGE = new Image("images/visible.png");
public static final Image INVISIBLE_IMAGE = new Image("images/not_visible.png");