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

@ -1,10 +1,10 @@
/*
* Copyright (C) 2015 The 8-Bit Bunch. Licensed under the Apache License, Version 1.1
* Copyright (C) 2015 The 8-Bit Bunch. Licensed under the Apache License, Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at <http://www.apache.org/licenses/LICENSE-1.1>.
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific language
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.badvision.outlaweditor;
@ -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;
@ -95,7 +96,7 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
public DrawMode getDrawMode() {
return drawMode;
}
@Override
public void setDrawMode(DrawMode drawMode) {
this.drawMode = drawMode;
@ -181,15 +182,15 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
}
Script selectedScript = null;
public void setSelectedScript(Script script) {
selectedScript = script;
}
}
public Script getSelectedScript() {
return selectedScript;
}
private void drawScript(double x, double y, Script script) {
if (script != null) {
getCurrentMap().putLocationScript((int) x, (int) y, script);
@ -198,7 +199,7 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
}
redraw();
}
public void assignScript(Script script, double x, double y) {
int xx = (int) (x / tileWidth) + posX;
int yy = (int) (y / tileHeight) + posY;
@ -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,22 +524,37 @@ 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("map")) {
trackState();
if (selection.containsKey("script")) {
Map sourceMap = ApplicationState.getInstance().getGameData().getMap().get(selection.get("map"));
TileMap source = getCurrentMap();
if (!sourceMap.equals(getCurrentMap().getBackingMap())) {
source = new TileMap(sourceMap);
} else {
source.updateBackingMap();
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);
}
int height = selection.get("y2") - selection.get("y1");
int width = selection.get("x2") - selection.get("x1");
int x1 = selection.get("x1");
int y1 = selection.get("y1");
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
plot(x + lastX, y + lastY, source.get(x + x1, y + y1));
} else {
if (selection.containsKey("map")) {
trackState();
Map sourceMap = ApplicationState.getInstance().getGameData().getMap().get(selection.get("map"));
TileMap source = getCurrentMap();
if (!sourceMap.equals(getCurrentMap().getBackingMap())) {
source = new TileMap(sourceMap);
} else {
source.updateBackingMap();
}
int height = selection.get("y2") - selection.get("y1");
int width = selection.get("x2") - selection.get("x1");
int x1 = selection.get("x1");
int y1 = selection.get("y1");
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
plot(x + lastX, y + lastY, source.get(x + x1, y + y1));
}
}
}
}
@ -659,10 +688,10 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
selectRect.setWidth(maxX - minX);
selectRect.setHeight(maxY - minY);
setSelectionArea(
(int) (minX / tileWidth + posX),
(int) (minY / tileHeight + posY),
(int) (maxX / tileWidth + posX),
(int) (maxY / tileHeight + posY)
(int) (minX / tileWidth + posX),
(int) (minY / tileHeight + posY),
(int) (maxX / tileWidth + posX),
(int) (maxY / tileHeight + posY)
);
}

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

@ -1,23 +1,22 @@
/*
* Copyright (C) 2015 The 8-Bit Bunch. Licensed under the Apache License, Version 1.1
* Copyright (C) 2015 The 8-Bit Bunch. Licensed under the Apache License, Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at <http://www.apache.org/licenses/LICENSE-1.1>.
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific language
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
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;
@ -94,14 +93,14 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
getCurrentEditor().setDrawMode(MapEditor.DrawMode.ScriptPencil);
}
}
@Override
public void mapScriptErasor(ActionEvent event) {
if (getCurrentEditor() != null) {
getCurrentEditor().setDrawMode(MapEditor.DrawMode.ScriptEraser);
}
}
@Override
public void mapTogglePanZoom(ActionEvent event) {
if (getCurrentEditor() != null) {
@ -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
@ -322,7 +311,7 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
//bind(mapDisplay3dField.selectedProperty(), boolProp(m, "display3d"));
} catch (NoSuchMethodException ex) {
Logger.getLogger(ApplicationUIControllerImpl.class
.getName()).log(Level.SEVERE, null, ex);
.getName()).log(Level.SEVERE, null, ex);
}
MapEditor e = new MapEditor();
e.setEntity(m);
@ -334,8 +323,8 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
}
}
if (getCurrentEditor() != null) {
cursorInfo.textProperty().bind(getCurrentEditor().cursorInfoProperty());
} else {
cursorInfo.textProperty().bind(getCurrentEditor().cursorInfoProperty());
} else {
cursorInfo.textProperty().unbind();
cursorInfo.setText("");
}
@ -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");