Skip to content

Commit

Permalink
Editor fixes: bool values, node copies & auto-expand, menu rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
rzats committed Aug 2, 2016
1 parent 42d5210 commit 182fa88
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");

/**
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -413,7 +417,7 @@ private void updateListeners() {
}
}

private void moveSelected(int keyId) {
private boolean moveSelected(int keyId) {
if (state.getSelectedIndex() != null) {
Tree<T> selectedNode = model.get().getNode(state.getSelectedIndex());
Tree<T> parent = selectedNode.getParent();
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public JsonTree getSiblingWithKey(String key) {

@Override
public Tree<JsonTreeValue> copy() {
Tree<JsonTreeValue> copy = new JsonTree(this.value);
Tree<JsonTreeValue> copy = new JsonTree(this.value.copy());
copy.setExpanded(this.isExpanded());

for (Tree<JsonTreeValue> child : this.children) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,8 @@ public String toString() {
return key != null ? key : NULL_STRING;
}
}

public JsonTreeValue copy() {
return new JsonTreeValue(key, value, type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Tree<T> getNode(int index) {
* @return The node with the given value.
*/
public Tree<T> getNodeByValue(T value) {
Optional<Tree<T>> node = this.nodes.stream().filter(n -> n.getValue().equals(value)).findFirst();
Optional<Tree<T>> node = this.nodes.stream().filter(n -> n.getValue() == value).findFirst();
return node.isPresent() ? node.get() : null;
}

Expand Down

0 comments on commit 182fa88

Please sign in to comment.