From 182fa88fb2afea3102e286611f5e5c5c676bc57d Mon Sep 17 00:00:00 2001 From: Rostyslav Zatserkovnyi Date: Tue, 2 Aug 2016 21:33:16 +0300 Subject: [PATCH] Editor fixes: bool values, node copies & auto-expand, menu rendering --- .../nui/contextMenu/ContextMenuScreen.java | 12 +++++++- .../editor/NUIEditorContextMenuBuilder.java | 9 +++++- .../rendering/nui/editor/NUIEditorScreen.java | 2 +- .../rendering/nui/widgets/UITreeView.java | 30 ++++++++++++------- .../nui/widgets/treeView/JsonTree.java | 2 +- .../nui/widgets/treeView/JsonTreeValue.java | 4 +++ .../nui/widgets/treeView/TreeModel.java | 2 +- 7 files changed, 46 insertions(+), 15 deletions(-) diff --git a/engine/src/main/java/org/terasology/rendering/nui/contextMenu/ContextMenuScreen.java b/engine/src/main/java/org/terasology/rendering/nui/contextMenu/ContextMenuScreen.java index c7ec55a8dc8..2e4247d85b5 100644 --- a/engine/src/main/java/org/terasology/rendering/nui/contextMenu/ContextMenuScreen.java +++ b/engine/src/main/java/org/terasology/rendering/nui/contextMenu/ContextMenuScreen.java @@ -17,6 +17,8 @@ import com.google.common.base.Preconditions; import com.google.common.collect.Lists; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.terasology.assets.ResourceUrn; import org.terasology.math.geom.Rect2i; import org.terasology.math.geom.Vector2i; @@ -37,6 +39,7 @@ */ public class ContextMenuScreen extends CoreScreenLayer { + private Logger logger = LoggerFactory.getLogger(ContextMenuScreen.class); public static final ResourceUrn ASSET_URI = new ResourceUrn("engine:contextMenuScreen"); /** @@ -87,7 +90,6 @@ public void onClosed() { @Override public void onDraw(Canvas canvas) { canvas.addInteractionRegion(mainListener); - Vector2i currentPosition = null; int currentWidth = 0; for (ContextMenuLevel level : menuLevels) { @@ -100,6 +102,14 @@ public void onDraw(Canvas canvas) { UIWidget menuWidget = level.getMenuWidget(); Rect2i region = Rect2i.createFromMinAndSize(currentPosition, canvas.calculatePreferredSize(menuWidget)); + double percentageThreshold = 0.9; + if (region.maxY() > canvas.getRegion().height() * percentageThreshold) { + region = Rect2i.createFromMinAndMax(region.minX(), + region.minY() - (region.maxY() - canvas.getRegion().height()) - + (int) (canvas.getRegion().height() * (1 - percentageThreshold)), + region.maxX(), + canvas.getRegion().height()); + } currentWidth = canvas.calculatePreferredSize(menuWidget).getX() - 8; canvas.drawWidget(level.getMenuWidget(), region); } diff --git a/engine/src/main/java/org/terasology/rendering/nui/editor/NUIEditorContextMenuBuilder.java b/engine/src/main/java/org/terasology/rendering/nui/editor/NUIEditorContextMenuBuilder.java index b8c75e2c469..581b0348519 100644 --- a/engine/src/main/java/org/terasology/rendering/nui/editor/NUIEditorContextMenuBuilder.java +++ b/engine/src/main/java/org/terasology/rendering/nui/editor/NUIEditorContextMenuBuilder.java @@ -180,7 +180,9 @@ private void createChild(String name, JsonTree node, Field field, Class clazz) } } - node.addChild(childValue); + JsonTree child = new JsonTree(childValue); + child.setExpanded(true); + node.addChild(child); } private boolean isWidget(Field field) throws IllegalAccessException { @@ -230,6 +232,11 @@ private Object getFieldValue(Field field, Class clazz) throws IllegalAccessExcep } else { value = field.get(clazz.newInstance()); } + + if (value != null && value instanceof Boolean) { + value = !(Boolean) value; + } + return value != null ? value : field.getType().newInstance(); } diff --git a/engine/src/main/java/org/terasology/rendering/nui/editor/NUIEditorScreen.java b/engine/src/main/java/org/terasology/rendering/nui/editor/NUIEditorScreen.java index 6831c235364..abcc77c5bc9 100644 --- a/engine/src/main/java/org/terasology/rendering/nui/editor/NUIEditorScreen.java +++ b/engine/src/main/java/org/terasology/rendering/nui/editor/NUIEditorScreen.java @@ -170,7 +170,7 @@ public void set(String value) { } editor.subscribeTreeViewUpdate(() -> { - JsonTree rootNode = (JsonTree) editor.getModel().getNode(0).getRoot(); + JsonTree rootNode = (JsonTree) editor.getModel().getNode(0).getRoot().copy(); addToHistory(rootNode); setPreviewWidget(rootNode); updateConfig(); diff --git a/engine/src/main/java/org/terasology/rendering/nui/widgets/UITreeView.java b/engine/src/main/java/org/terasology/rendering/nui/widgets/UITreeView.java index 570ef6a3fae..9de994b8cdf 100644 --- a/engine/src/main/java/org/terasology/rendering/nui/widgets/UITreeView.java +++ b/engine/src/main/java/org/terasology/rendering/nui/widgets/UITreeView.java @@ -224,20 +224,24 @@ public boolean onKeyEvent(NUIKeyEvent event) { if (id == Keyboard.KeyId.UP || id == Keyboard.KeyId.DOWN) { // Up/Down: change a node's position within the parent node. - moveSelected(id); - return true; + return moveSelected(id); } else if (id == Keyboard.KeyId.DELETE) { // Delete: remove a node (and all its' children). - removeSelected(); - return true; + return removeSelected(); } else if (ctrlDown && id == Keyboard.KeyId.C) { // Ctrl+C: copy a selected node. - copy(model.get().getNode(state.getSelectedIndex())); - return true; + if (state.getSelectedIndex() != null) { + copy(model.get().getNode(state.getSelectedIndex())); + return true; + } + return false; } else if (ctrlDown && id == Keyboard.KeyId.V) { // Ctrl+V: paste the copied node as a child of the currently selected node. - paste(model.get().getNode(state.getSelectedIndex())); - return true; + if (state.getSelectedIndex() != null) { + paste(model.get().getNode(state.getSelectedIndex())); + return true; + } + return false; } else { return false; } @@ -413,7 +417,7 @@ private void updateListeners() { } } - private void moveSelected(int keyId) { + private boolean moveSelected(int keyId) { if (state.getSelectedIndex() != null) { Tree selectedNode = model.get().getNode(state.getSelectedIndex()); Tree parent = selectedNode.getParent(); @@ -443,16 +447,22 @@ private void moveSelected(int keyId) { fireUpdateListeners(); } } + + return true; } + + return false; } - private void removeSelected() { + private boolean removeSelected() { if (state.getSelectedIndex() != null) { model.get().removeNode(state.getSelectedIndex()); state.setSelectedIndex(null); fireUpdateListeners(); + return true; } + return false; } private boolean onNodeClick(int index, NUIMouseClickEvent event) { diff --git a/engine/src/main/java/org/terasology/rendering/nui/widgets/treeView/JsonTree.java b/engine/src/main/java/org/terasology/rendering/nui/widgets/treeView/JsonTree.java index 2d8edae9e1f..2f3937d816a 100644 --- a/engine/src/main/java/org/terasology/rendering/nui/widgets/treeView/JsonTree.java +++ b/engine/src/main/java/org/terasology/rendering/nui/widgets/treeView/JsonTree.java @@ -98,7 +98,7 @@ public JsonTree getSiblingWithKey(String key) { @Override public Tree copy() { - Tree copy = new JsonTree(this.value); + Tree copy = new JsonTree(this.value.copy()); copy.setExpanded(this.isExpanded()); for (Tree child : this.children) { diff --git a/engine/src/main/java/org/terasology/rendering/nui/widgets/treeView/JsonTreeValue.java b/engine/src/main/java/org/terasology/rendering/nui/widgets/treeView/JsonTreeValue.java index 08b96d2f16d..015e190e58f 100644 --- a/engine/src/main/java/org/terasology/rendering/nui/widgets/treeView/JsonTreeValue.java +++ b/engine/src/main/java/org/terasology/rendering/nui/widgets/treeView/JsonTreeValue.java @@ -124,4 +124,8 @@ public String toString() { return key != null ? key : NULL_STRING; } } + + public JsonTreeValue copy() { + return new JsonTreeValue(key, value, type); + } } diff --git a/engine/src/main/java/org/terasology/rendering/nui/widgets/treeView/TreeModel.java b/engine/src/main/java/org/terasology/rendering/nui/widgets/treeView/TreeModel.java index 6689e2fbe70..f09eef135d0 100644 --- a/engine/src/main/java/org/terasology/rendering/nui/widgets/treeView/TreeModel.java +++ b/engine/src/main/java/org/terasology/rendering/nui/widgets/treeView/TreeModel.java @@ -75,7 +75,7 @@ public Tree getNode(int index) { * @return The node with the given value. */ public Tree getNodeByValue(T value) { - Optional> node = this.nodes.stream().filter(n -> n.getValue().equals(value)).findFirst(); + Optional> node = this.nodes.stream().filter(n -> n.getValue() == value).findFirst(); return node.isPresent() ? node.get() : null; }