From 6bc3b87329707c6f218222c39b237e3cae0efeb5 Mon Sep 17 00:00:00 2001 From: Brendan Robert Date: Sun, 8 Mar 2015 18:58:21 -0500 Subject: [PATCH 1/6] Changed category to a single value and added auto-sort in the tile and image selectors --- .../outlaweditor/data/PropertyHelper.java | 61 ----------------- .../outlaweditor/data/TileUtils.java | 2 +- .../outlaweditor/ui/EntitySelectorCell.java | 48 +++++++++----- .../ui/impl/ImageEditorTabControllerImpl.java | 49 ++++++++------ .../ui/impl/MapEditorTabControllerImpl.java | 2 +- .../ui/impl/TileEditorTabControllerImpl.java | 65 ++++++++++--------- .../src/main/resources/imageEditorTab.fxml | 2 +- .../jaxb/OutlawSchema/OutlawSchema.xsd | 4 +- .../src/main/resources/tileEditorTab.fxml | 2 +- 9 files changed, 104 insertions(+), 131 deletions(-) diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/data/PropertyHelper.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/data/PropertyHelper.java index 8a8498b8..6e7bbaa6 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/data/PropertyHelper.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/data/PropertyHelper.java @@ -4,23 +4,15 @@ */ package org.badvision.outlaweditor.data; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; import javafx.beans.property.Property; -import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.adapter.JavaBeanBooleanProperty; import javafx.beans.property.adapter.JavaBeanBooleanPropertyBuilder; import javafx.beans.property.adapter.JavaBeanIntegerProperty; import javafx.beans.property.adapter.JavaBeanIntegerPropertyBuilder; import javafx.beans.property.adapter.JavaBeanStringProperty; import javafx.beans.property.adapter.JavaBeanStringPropertyBuilder; -import org.badvision.outlaweditor.ui.ApplicationUIController; /** * @@ -36,59 +28,6 @@ public class PropertyHelper { return new JavaBeanBooleanPropertyBuilder().bean(t).name(fieldName).build(); } - public static Property categoryProp(final Object t, String fieldName) throws NoSuchMethodException { - final String camel = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); - SimpleStringProperty prop = new SimpleStringProperty() { - @Override - public String get() { - try { - Method getter = t.getClass().getMethod("get" + camel); - List list = (List) getter.invoke(t); - String out = ""; - for (String s : list) { - if (out.length() > 0) { - out += ","; - } - out += s; - } - return out; - } catch (NoSuchMethodException ex) { - Logger.getLogger(ApplicationUIController.class.getName()).log(Level.SEVERE, null, ex); - } catch (SecurityException ex) { - Logger.getLogger(ApplicationUIController.class.getName()).log(Level.SEVERE, null, ex); - } catch (IllegalAccessException ex) { - Logger.getLogger(ApplicationUIController.class.getName()).log(Level.SEVERE, null, ex); - } catch (IllegalArgumentException ex) { - Logger.getLogger(ApplicationUIController.class.getName()).log(Level.SEVERE, null, ex); - } catch (InvocationTargetException ex) { - Logger.getLogger(ApplicationUIController.class.getName()).log(Level.SEVERE, null, ex); - } - return null; - } - - @Override - public void set(String string) { - try { - Method getter = t.getClass().getMethod("get" + camel); - List list = (List) getter.invoke(t); - list.clear(); - Collections.addAll(list, string.split(",")); - } catch (NoSuchMethodException ex) { - Logger.getLogger(ApplicationUIController.class.getName()).log(Level.SEVERE, null, ex); - } catch (SecurityException ex) { - Logger.getLogger(ApplicationUIController.class.getName()).log(Level.SEVERE, null, ex); - } catch (IllegalAccessException ex) { - Logger.getLogger(ApplicationUIController.class.getName()).log(Level.SEVERE, null, ex); - } catch (IllegalArgumentException ex) { - Logger.getLogger(ApplicationUIController.class.getName()).log(Level.SEVERE, null, ex); - } catch (InvocationTargetException ex) { - Logger.getLogger(ApplicationUIController.class.getName()).log(Level.SEVERE, null, ex); - } - } - }; - return prop; - } - public static JavaBeanStringProperty stringProp(Object t, String fieldName) throws NoSuchMethodException { return new JavaBeanStringPropertyBuilder().bean(t).name(fieldName).build(); } diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/data/TileUtils.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/data/TileUtils.java index 799048b2..d2905db3 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/data/TileUtils.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/data/TileUtils.java @@ -45,7 +45,7 @@ public class TileUtils { public static Map getDisplay(Tile t) { if (display.get(getId(t)) == null) { - display.put(getId(t), new EnumMap(Platform.class)); + display.put(getId(t), new EnumMap<>(Platform.class)); } return display.get(getId(t)); } diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/EntitySelectorCell.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/EntitySelectorCell.java index 01814ea7..00efb1ab 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/EntitySelectorCell.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/EntitySelectorCell.java @@ -3,13 +3,13 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ - package org.badvision.outlaweditor.ui; import java.util.HashMap; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; +import javafx.beans.binding.Bindings; import javafx.scene.control.ListCell; import javafx.scene.control.TextField; import javafx.scene.control.cell.ComboBoxListCell; @@ -21,27 +21,33 @@ import org.badvision.outlaweditor.ui.impl.ApplicationUIControllerImpl; * @author blurry */ public abstract class EntitySelectorCell extends ComboBoxListCell { - static Map lastSelected = new HashMap<>(); - TextField nameField; - public EntitySelectorCell(TextField tileNameField) { + static Map lastSelected = new HashMap<>(); + TextField nameField, categoryField; + + public EntitySelectorCell(TextField tileNameField, TextField categoryNameField) { super.setPrefWidth(125); nameField = tileNameField; + categoryField = categoryNameField; } @Override public void updateSelected(boolean sel) { - if (sel) { - Object o = lastSelected.get(nameField); - if (o != null && !o.equals(getItem())) { - ((ListCell) o).updateSelected(false); - } - textProperty().unbind(); - textProperty().bind(nameField.textProperty()); - lastSelected.put(nameField, this); - } else { +// if (sel) { +// Object o = lastSelected.get(nameField); +// if (o != null && !o.equals(getItem())) { +// ((ListCell) o).updateSelected(false); +// } +// textProperty().unbind(); +// if (categoryField != null) { +// textProperty().bind(Bindings.concat(categoryField.textProperty(), "/", nameField.textProperty())); +// } else { +// textProperty().bind(Bindings.concat(nameField.textProperty())); +// } +// lastSelected.put(nameField, this); +// } else { updateItem(getItem(), false); - } +// } } @Override @@ -50,7 +56,17 @@ public abstract class EntitySelectorCell extends ComboBoxListCell { super.updateItem(item, empty); if (item != null && !(item instanceof String)) { try { - textProperty().bind(PropertyHelper.stringProp(item, "name")); + if (categoryField != null) { + textProperty().bind( + Bindings.concat( + PropertyHelper.stringProp(item, "category"), + "/", + PropertyHelper.stringProp(item, "name") + ) + ); + } else { + textProperty().bind(PropertyHelper.stringProp(item, "name")); + } } catch (NoSuchMethodException ex) { Logger.getLogger(ApplicationUIControllerImpl.class.getName()).log(Level.SEVERE, null, ex); } @@ -62,5 +78,5 @@ public abstract class EntitySelectorCell extends ComboBoxListCell { public void finishUpdate(T item) { } - + } diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/ImageEditorTabControllerImpl.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/ImageEditorTabControllerImpl.java index 92d2bbde..85cbf168 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/ImageEditorTabControllerImpl.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/ImageEditorTabControllerImpl.java @@ -1,18 +1,22 @@ package org.badvision.outlaweditor.ui.impl; +import java.util.List; import org.badvision.outlaweditor.ui.EntitySelectorCell; import java.util.logging.Level; import java.util.logging.Logger; +import javafx.beans.binding.Bindings; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.scene.control.ListView; import javafx.scene.control.cell.ComboBoxListCell; +import javafx.util.StringConverter; import org.badvision.outlaweditor.Application; import org.badvision.outlaweditor.Editor; import org.badvision.outlaweditor.ImageEditor; import static org.badvision.outlaweditor.Application.currentPlatform; import static org.badvision.outlaweditor.ui.UIAction.confirm; import static org.badvision.outlaweditor.data.PropertyHelper.bind; -import static org.badvision.outlaweditor.data.PropertyHelper.categoryProp; import static org.badvision.outlaweditor.data.PropertyHelper.stringProp; import org.badvision.outlaweditor.data.xml.Image; import org.badvision.outlaweditor.ui.ImageEditorTabController; @@ -26,31 +30,27 @@ public class ImageEditorTabControllerImpl extends ImageEditorTabController { public Image currentImage = null; public ImageEditor currentImageEditor = null; + ChangeListener rebuildListener = (ObservableValue value, Object oldValue, Object newValue) -> rebuildImageSelector(); /** * Initializes the controller class. */ public void initialize() { super.initalize(); - imageSelector.setButtonCell(new ComboBoxListCell() { - { - super.setPrefWidth(125); + imageSelector.setCellFactory((ListView param) -> new EntitySelectorCell(imageNameField, imageCategoryField) { + @Override + public void finishUpdate(Image item) { + } + }); + imageSelector.setConverter(new StringConverter() { + @Override + public String toString(Image object) { + return String.valueOf(object.getCategory()) + "/" + String.valueOf(object.getName()); } @Override - public void updateItem(Image item, boolean empty) { - textProperty().unbind(); - super.updateItem(item, empty); - if (item != null) { - textProperty().bind(imageNameField.textProperty()); - } else { - setText(null); - } - } - }); - imageSelector.setCellFactory((ListView param) -> new EntitySelectorCell(imageNameField) { - @Override - public void finishUpdate(Image item) { + public Image fromString(String string) { + return null; } }); } @@ -165,6 +165,8 @@ public class ImageEditorTabControllerImpl extends ImageEditorTabController { } private void setCurrentImage(Image i) { + imageNameField.textProperty().removeListener(rebuildListener); + imageCategoryField.textProperty().removeListener(rebuildListener); if (currentImage != null && currentImage.equals(i)) { return; } @@ -193,7 +195,7 @@ public class ImageEditorTabControllerImpl extends ImageEditorTabController { imageNameField.setDisable(false); imageWidthField.setDisable(false); bind(imageNameField.textProperty(), stringProp(i, "name")); - bind(imageCategoryField.textProperty(), categoryProp(i, "category")); + bind(imageCategoryField.textProperty(), stringProp(i, "category")); } catch (NoSuchMethodException ex) { Logger.getLogger(ApplicationUIControllerImpl.class.getName()).log(Level.SEVERE, null, ex); } @@ -207,6 +209,8 @@ public class ImageEditorTabControllerImpl extends ImageEditorTabController { currentImageEditor.buildPatternSelector(imagePatternMenu); imageEditorZoomGroup.setScaleX(1.0); imageEditorZoomGroup.setScaleY(1.0); + imageNameField.textProperty().addListener(rebuildListener); + imageCategoryField.textProperty().addListener(rebuildListener); } } @@ -218,7 +222,14 @@ public class ImageEditorTabControllerImpl extends ImageEditorTabController { public void rebuildImageSelector() { Image i = getCurrentImage(); imageSelector.getItems().clear(); - imageSelector.getItems().addAll(Application.gameData.getImage()); + List allImages = Application.gameData.getImage(); + allImages.sort((Image o1, Image o2) -> { + int c1 = String.valueOf(o1.getCategory()).compareTo(String.valueOf(o2.getCategory())); + if (c1 != 0) return c1; + return String.valueOf(o1.getName()).compareTo(String.valueOf(o2.getName())); + }); + + imageSelector.getItems().addAll(allImages); imageSelector.getSelectionModel().select(i); } diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/MapEditorTabControllerImpl.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/MapEditorTabControllerImpl.java index e08d71a2..ce1f9fa7 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/MapEditorTabControllerImpl.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/MapEditorTabControllerImpl.java @@ -267,7 +267,7 @@ public class MapEditorTabControllerImpl extends MapEditorTabController { } } }); - mapSelect.setCellFactory((ListView param) -> new EntitySelectorCell(mapNameField) { + mapSelect.setCellFactory((ListView param) -> new EntitySelectorCell(mapNameField, null) { @Override public void finishUpdate(Map item) { } diff --git a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/TileEditorTabControllerImpl.java b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/TileEditorTabControllerImpl.java index f8a95b30..3a1dfea9 100644 --- a/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/TileEditorTabControllerImpl.java +++ b/OutlawEditor/src/main/java/org/badvision/outlaweditor/ui/impl/TileEditorTabControllerImpl.java @@ -2,20 +2,20 @@ package org.badvision.outlaweditor.ui.impl; import org.badvision.outlaweditor.ui.EntitySelectorCell; import java.util.Arrays; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; -import javafx.scene.control.ListCell; import javafx.scene.control.ListView; -import javafx.scene.control.cell.ComboBoxListCell; import javafx.scene.image.ImageView; -import javafx.util.Callback; +import javafx.util.StringConverter; import org.badvision.outlaweditor.Application; import org.badvision.outlaweditor.TileEditor; import static org.badvision.outlaweditor.ui.UIAction.confirm; import static org.badvision.outlaweditor.data.PropertyHelper.bind; import static org.badvision.outlaweditor.data.PropertyHelper.boolProp; -import static org.badvision.outlaweditor.data.PropertyHelper.categoryProp; import static org.badvision.outlaweditor.data.PropertyHelper.stringProp; import org.badvision.outlaweditor.data.TileUtils; import org.badvision.outlaweditor.data.TilesetUtils; @@ -31,6 +31,8 @@ import org.badvision.outlaweditor.ui.TileEditorTabController; */ public class TileEditorTabControllerImpl extends TileEditorTabController { + ChangeListener rebuildListener = (ObservableValue value, Object oldValue, Object newValue) -> rebuildTileSelectors(); + @Override public void onCurrentTileSelected(ActionEvent event) { setCurrentTile(tileSelector.getSelectionModel().getSelectedItem()); @@ -48,7 +50,7 @@ public class TileEditorTabControllerImpl extends TileEditorTabController { t.setObstruction(getCurrentTile().isObstruction()); t.setSprite(getCurrentTile().isSprite()); t.setBlocker(getCurrentTile().isBlocker()); - t.getCategory().addAll(getCurrentTile().getCategory()); + t.setCategory(getCurrentTile().getCategory()); getCurrentTile().getDisplayData().stream().map((d) -> { PlatformData p = new PlatformData(); p.setHeight(d.getHeight()); @@ -90,7 +92,6 @@ public class TileEditorTabControllerImpl extends TileEditorTabController { @Override public void tileBitMode(ActionEvent event) { - ApplicationUIController mainController = ApplicationUIController.getController(); if (getCurrentTileEditor() != null) { getCurrentTileEditor().setDrawMode(TileEditor.DrawMode.Toggle); } @@ -98,7 +99,6 @@ public class TileEditorTabControllerImpl extends TileEditorTabController { @Override public void tileDraw1BitMode(ActionEvent event) { - ApplicationUIController mainController = ApplicationUIController.getController(); if (getCurrentTileEditor() != null) { getCurrentTileEditor().setDrawMode(TileEditor.DrawMode.Pencil1px); } @@ -106,7 +106,6 @@ public class TileEditorTabControllerImpl extends TileEditorTabController { @Override public void tileDraw3BitMode(ActionEvent event) { - ApplicationUIController mainController = ApplicationUIController.getController(); if (getCurrentTileEditor() != null) { getCurrentTileEditor().setDrawMode(TileEditor.DrawMode.Pencil3px); } @@ -114,7 +113,6 @@ public class TileEditorTabControllerImpl extends TileEditorTabController { @Override public void tileShift(ActionEvent event) { - ApplicationUIController mainController = ApplicationUIController.getController(); if (getCurrentTileEditor() != null) { getCurrentTileEditor().showShiftUI(); } @@ -138,31 +136,25 @@ public class TileEditorTabControllerImpl extends TileEditorTabController { assert tileBlockerField != null : "fx:id=\"tileBlockerField\" was not injected: check your FXML file 'tileEditorTab.fxml'."; assert tilePatternMenu != null : "fx:id=\"tilePatternMenu\" was not injected: check your FXML file 'tileEditorTab.fxml'."; assert tileSelector != null : "fx:id=\"tileSelector\" was not injected: check your FXML file 'tileEditorTab.fxml'."; - - tileSelector.setButtonCell(new ComboBoxListCell() { - { - super.setPrefWidth(125); - } - - @Override - public void updateItem(Tile item, boolean empty) { - textProperty().unbind(); - super.updateItem(item, empty); - if (item != null) { - textProperty().bind(tileNameField.textProperty()); - } else { - setText(null); - } - } - }); tileSelector.setCellFactory((ListView param) -> { - return new EntitySelectorCell(tileNameField) { + return new EntitySelectorCell(tileNameField, tileCategoryField) { @Override public void finishUpdate(Tile item) { setGraphic(new ImageView(TileUtils.getImage(item, Application.currentPlatform))); } }; }); + tileSelector.setConverter(new StringConverter() { + @Override + public String toString(Tile object) { + return String.valueOf(object.getCategory() + "/" + object.getName()); + } + + @Override + public Tile fromString(String string) { + return null; + } + }); } @Override @@ -176,6 +168,8 @@ public class TileEditorTabControllerImpl extends TileEditorTabController { @Override public void setCurrentTile(Tile t) { + tileNameField.textProperty().removeListener(rebuildListener); + tileCategoryField.textProperty().removeListener(rebuildListener); tileSelector.getSelectionModel().select(t); if (t != null && t.equals(getCurrentTile())) { return; @@ -188,6 +182,7 @@ public class TileEditorTabControllerImpl extends TileEditorTabController { bind(tileSpriteField.selectedProperty(), null); bind(tileBlockerField.selectedProperty(), null); bind(tileNameField.textProperty(), null); + tileIdField.setDisable(true); tileCategoryField.setDisable(true); tileObstructionField.setDisable(true); @@ -213,7 +208,7 @@ public class TileEditorTabControllerImpl extends TileEditorTabController { tileBlockerField.setDisable(false); tileNameField.setDisable(false); bind(tileIdField.textProperty(), stringProp(t, "id")); - bind(tileCategoryField.textProperty(), categoryProp(t, "category")); + bind(tileCategoryField.textProperty(), stringProp(t, "category")); bind(tileObstructionField.selectedProperty(), boolProp(t, "obstruction")); bind(tileSpriteField.selectedProperty(), boolProp(t, "sprite")); bind(tileBlockerField.selectedProperty(), boolProp(t, "blocker")); @@ -221,6 +216,8 @@ public class TileEditorTabControllerImpl extends TileEditorTabController { TileEditor editor = Application.currentPlatform.tileEditor.newInstance(); editor.setEntity(t); setCurrentTileEditor(editor); + tileNameField.textProperty().addListener(rebuildListener); + tileCategoryField.textProperty().addListener(rebuildListener); } catch (NoSuchMethodException ex) { Logger.getLogger(ApplicationUIController.class.getName()).log(Level.SEVERE, null, ex); } catch (InstantiationException | IllegalAccessException ex) { @@ -233,7 +230,15 @@ public class TileEditorTabControllerImpl extends TileEditorTabController { @Override public void rebuildTileSelectors() { tileSelector.getItems().clear(); - tileSelector.getItems().addAll(Application.gameData.getTile()); - tileSelector.getSelectionModel().select(getCurrentTile()); + List allTiles = Application.gameData.getTile(); + allTiles.sort((Tile o1, Tile o2) -> { + int c1 = String.valueOf(o1.getCategory()).compareTo(String.valueOf(o2.getCategory())); + if (c1 != 0) { + return c1; + } + return String.valueOf(o1.getName()).compareTo(String.valueOf(o2.getName())); + }); + tileSelector.getItems().addAll(allTiles); + tileSelector.getSelectionModel().select(allTiles.indexOf(getCurrentTile())); } } diff --git a/OutlawEditor/src/main/resources/imageEditorTab.fxml b/OutlawEditor/src/main/resources/imageEditorTab.fxml index cedd2a1c..47065b89 100644 --- a/OutlawEditor/src/main/resources/imageEditorTab.fxml +++ b/OutlawEditor/src/main/resources/imageEditorTab.fxml @@ -13,7 +13,7 @@