From 5989711f7315abe4c3da0f1516a3eb3a81da6716 Mon Sep 17 00:00:00 2001 From: Nehon Date: Wed, 15 Jul 2015 23:55:33 +0200 Subject: [PATCH] Added multi selection to the ShaderNode editor. One can now select several items by left clicking with ctrl or shit held. One can now move all selected items at once. One can now delete all selected items at once. --- .../editor/ConnectionCurve.java | 10 +- .../editor/ConnectionStraight.java | 8 +- .../materialdefinition/editor/Diagram.java | 133 ++++++++++++------ .../editor/DraggablePanel.java | 20 ++- .../materialdefinition/editor/NodePanel.java | 29 +--- .../editor/OutBusPanel.java | 4 +- 6 files changed, 120 insertions(+), 84 deletions(-) diff --git a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/ConnectionCurve.java b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/ConnectionCurve.java index ec2f4aa98..7c225f745 100644 --- a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/ConnectionCurve.java +++ b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/ConnectionCurve.java @@ -123,7 +123,7 @@ public class ConnectionCurve extends JPanel implements ComponentListener, MouseI g2.setStroke(new BasicStroke(4)); Path2D.Double path1 = new Path2D.Double(); - if (getDiagram().selectedItem == this) { + if (getDiagram().getSelectedItems().contains(this)) { g.setColor(SELECTED_COLOR); } else { g.setColor(VERY_DARK_GREY); @@ -162,7 +162,7 @@ public class ConnectionCurve extends JPanel implements ComponentListener, MouseI ((Graphics2D) g).draw(path1); g2.setStroke(new BasicStroke(2)); - if (getDiagram().selectedItem == this) { + if (getDiagram().getSelectedItems().contains(this)) { g.setColor(Color.WHITE); } else { g.setColor(LIGHT_GREY); @@ -385,7 +385,7 @@ public class ConnectionCurve extends JPanel implements ComponentListener, MouseI } if (selected) { - getDiagram().select(this); + getDiagram().select(this, e.isShiftDown() || e.isControlDown()); e.consume(); } } @@ -407,9 +407,7 @@ public class ConnectionCurve extends JPanel implements ComponentListener, MouseI if (e.getKeyCode() == KeyEvent.VK_DELETE) { Diagram diag = getDiagram(); - if (diag.selectedItem == this) { - diag.removeSelectedConnection(); - } + diag.removeSelected(); } } diff --git a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/ConnectionStraight.java b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/ConnectionStraight.java index 3eba66a23..fc4873bd8 100644 --- a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/ConnectionStraight.java +++ b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/ConnectionStraight.java @@ -209,7 +209,7 @@ public class ConnectionStraight extends JPanel implements ComponentListener, Mou g.drawLine(p1.x, p1.y, p2.x, p2.y); - if (getDiagram().selectedItem == this) { + if (getDiagram().getSelectedItems().contains(this)) { g.setColor(Color.CYAN); } else { g.setColor(Color.GRAY); @@ -489,7 +489,7 @@ public class ConnectionStraight extends JPanel implements ComponentListener, Mou } if (selected) { - getDiagram().select(this); + getDiagram().select(this, e.isShiftDown() || e.isControlDown()); e.consume(); } } @@ -511,9 +511,7 @@ public class ConnectionStraight extends JPanel implements ComponentListener, Mou if (e.getKeyCode() == KeyEvent.VK_DELETE) { Diagram diag = getDiagram(); - if (diag.selectedItem == this) { - diag.removeSelectedConnection(); - } + diag.removeSelected(); } } diff --git a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/Diagram.java b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/Diagram.java index 9d344355e..88876f3e5 100644 --- a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/Diagram.java +++ b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/Diagram.java @@ -55,7 +55,7 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene protected Dot draggedFrom; protected Dot draggedTo; - protected Selectable selectedItem; + protected List selectedItems = new ArrayList(); protected List connections = new ArrayList(); protected List nodes = new ArrayList(); protected List outBuses = new ArrayList(); @@ -63,6 +63,9 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene private MatDefEditorlElement parent; private String currentTechniqueName; private final BackdropPanel backDrop = new BackdropPanel(); + private final Cursor defCursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR); + private final Cursor hndCursor = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR); + private final Point pp = new Point(); @SuppressWarnings("LeakingThisInConstructor") public Diagram() { @@ -99,7 +102,7 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene } } - selectedItem = null; + selectedItems.clear(); repaint(); } else if (e.getButton() == MouseEvent.BUTTON2) { setCursor(hndCursor); @@ -204,13 +207,10 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene public void mouseExited(MouseEvent e) { } - protected void removeSelectedConnection() { - if (selectedItem instanceof Connection) { - Connection selectedConnection = (Connection) selectedItem; - removeConnection(selectedConnection); - selectedItem = null; - parent.notifyRemoveConnection(selectedConnection); - } + protected void removeSelectedConnection(Selectable selectedItem) { + Connection selectedConnection = (Connection) selectedItem; + removeConnection(selectedConnection); + parent.notifyRemoveConnection(selectedConnection); } private String fixNodeName(String name) { @@ -276,44 +276,58 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene np.revalidate(); repaint(); } - - protected void removeSelectedNode() { - if (selectedItem instanceof NodePanel) { - int result = JOptionPane.showConfirmDialog(null, "Delete this node and all its mappings?", "Delete Shader Node", JOptionPane.OK_CANCEL_OPTION); - if (result == JOptionPane.OK_OPTION) { - NodePanel selectedNode = (NodePanel) selectedItem; - nodes.remove(selectedNode); - for (Iterator it = connections.iterator(); it.hasNext();) { - Connection conn = it.next(); - if (conn.start.getNode() == selectedNode || conn.end.getNode() == selectedNode) { - it.remove(); - conn.end.disconnect(); - conn.start.disconnect(); - remove(conn); - } + + protected void removeSelected(){ + + int result = JOptionPane.showConfirmDialog(null, "Delete all selected items, nodes and mappings?", "Delete Selected", JOptionPane.OK_CANCEL_OPTION); + + if (result == JOptionPane.OK_OPTION) { + for (Selectable selectedItem : selectedItems) { + if (selectedItem instanceof NodePanel) { + removeSelectedNode(selectedItem); } + if (selectedItem instanceof Connection) { + removeSelectedConnection(selectedItem); + } + } + selectedItems.clear(); + } + } - selectedNode.cleanup(); - remove(selectedNode); - selectedItem = null; - repaint(); - parent.notifyRemoveNode(selectedNode); + private void removeSelectedNode(Selectable selectedItem) { + + NodePanel selectedNode = (NodePanel) selectedItem; + nodes.remove(selectedNode); + for (Iterator it = connections.iterator(); it.hasNext();) { + Connection conn = it.next(); + if (conn.start.getNode() == selectedNode || conn.end.getNode() == selectedNode) { + it.remove(); + conn.end.disconnect(); + conn.start.disconnect(); + remove(conn); } } + + selectedNode.cleanup(); + remove(selectedNode); + repaint(); + parent.notifyRemoveNode(selectedNode); } - private final Cursor defCursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR); - private final Cursor hndCursor = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR); - private final Point pp = new Point(); + public List getSelectedItems() { + return selectedItems; + } @Override public void mouseDragged(MouseEvent e) { if (SwingUtilities.isLeftMouseButton(e)) { if (draggedFrom == null) { - if (selectedItem instanceof OutBusPanel) { - OutBusPanel bus = (OutBusPanel) selectedItem; - MouseEvent me = SwingUtilities.convertMouseEvent(this, e, bus); - bus.dispatchEvent(me); + for (Selectable selectedItem : selectedItems) { + if (selectedItem instanceof OutBusPanel) { + OutBusPanel bus = (OutBusPanel) selectedItem; + MouseEvent me = SwingUtilities.convertMouseEvent(this, e, bus); + bus.dispatchEvent(me); + } } } } else if (SwingUtilities.isMiddleMouseButton(e)) { @@ -373,22 +387,53 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene * * @param selectable */ - public void select(Selectable selectable) { - parent.selectionChanged(doSelect(selectable)); + public void select(Selectable selectable, boolean multi) { + parent.selectionChanged(doSelect(selectable, multi)); + } + + public void multiMove(DraggablePanel movedPanel ,int xOffset, int yOffset){ + + for (Selectable selectedItem : selectedItems) { + if(selectedItem != movedPanel){ + if(selectedItem instanceof DraggablePanel){ + ((DraggablePanel)selectedItem).movePanel(xOffset, yOffset); + } + } + } } + public void multiStartDrag(DraggablePanel movedPanel){ + for (Selectable selectedItem : selectedItems) { + if(selectedItem != movedPanel){ + if(selectedItem instanceof DraggablePanel){ + ((DraggablePanel)selectedItem).saveLocation(); + } + } + } + } + /** * do select the item and repaint the diagram * * @param selectable * @return */ - private Selectable doSelect(Selectable selectable) { - this.selectedItem = selectable; + private Selectable doSelect(Selectable selectable, boolean multi) { + + + if (!multi && !selectedItems.contains(selectable)) { + selectedItems.clear(); + } + + if (selectable != null) { + selectedItems.add(selectable); + } + if (selectable instanceof Component) { ((Component) selectable).requestFocusInWindow(); } repaint(); + return selectable; } @@ -403,23 +448,23 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene for (NodePanel nodePanel : nodes) { if (nodePanel.getKey().equals(key)) { - return doSelect(nodePanel); + return doSelect(nodePanel, false); } } for (Connection connection : connections) { if (connection.getKey().equals(key)) { - return doSelect(connection); + return doSelect(connection, false); } } for (OutBusPanel outBusPanel : outBuses) { if (outBusPanel.getKey().equals(key)) { - return doSelect(outBusPanel); + return doSelect(outBusPanel, false); } } - return doSelect(null); + return null; } @Override diff --git a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/DraggablePanel.java b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/DraggablePanel.java index e81d1d5b6..7941cb8d5 100644 --- a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/DraggablePanel.java +++ b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/DraggablePanel.java @@ -37,16 +37,22 @@ public class DraggablePanel extends JPanel implements MouseListener, MouseMotion @Override public void mousePressed(MouseEvent e) { if (e.getButton() != MouseEvent.BUTTON2) { - svdx = getLocation().x; + if (!vertical) { svdex = e.getXOnScreen(); } - svdy = getLocation().y; svdey = e.getYOnScreen(); + saveLocation(); + diagram.multiStartDrag(this); e.consume(); } } + protected void saveLocation() { + svdy = getLocation().y; + svdx = getLocation().x; + } + @Override public void mouseReleased(MouseEvent e) { } @@ -71,11 +77,19 @@ public class DraggablePanel extends JPanel implements MouseListener, MouseMotion xoffset = e.getLocationOnScreen().x - svdex; } int yoffset = e.getLocationOnScreen().y - svdey; - setLocation(Math.max(0, svdx + xoffset), Math.max(0, svdy + yoffset)); + movePanel(xoffset, yoffset); + diagram.multiMove(this, xoffset, yoffset); e.consume(); } } + protected void movePanel(int xoffset, int yoffset) { + if (vertical) { + xoffset = 0; + } + setLocation(Math.max(0, svdx + xoffset), Math.max(0, svdy + yoffset)); + } + public Diagram getDiagram() { return diagram; } diff --git a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/NodePanel.java b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/NodePanel.java index 49e04170d..540ce9742 100644 --- a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/NodePanel.java +++ b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/NodePanel.java @@ -56,23 +56,6 @@ public class NodePanel extends DraggablePanel implements Selectable, PropertyCha protected List filePaths = new ArrayList(); protected Shader.ShaderType shaderType; -// private List listeners = Collections.synchronizedList(new LinkedList()); -// -// public void addPropertyChangeListener(PropertyChangeListener pcl) { -// listeners.add(pcl); -// } -// -// public void removePropertyChangeListener(PropertyChangeListener pcl) { -// listeners.remove(pcl); -// } -// -// protected void fire(String propertyName, Object old, Object nue) { -// //Passing 0 below on purpose, so you only synchronize for one atomic call: -// PropertyChangeListener[] pcls = (PropertyChangeListener[]) listeners.toArray(new PropertyChangeListener[0]); -// for (int i = 0; i < pcls.length; i++) { -// pcls[i].propertyChange(new PropertyChangeEvent(this, propertyName, old, nue)); -// } -// } public enum NodeType { Vertex(new Color(220, 220, 70)),//yellow @@ -201,13 +184,13 @@ public class NodePanel extends DraggablePanel implements Selectable, PropertyCha protected void paintComponent(Graphics g1) { Graphics2D g = (Graphics2D) g1; Color boderColor = Color.BLACK; - if (diagram.selectedItem == this) { + if (getDiagram().getSelectedItems().contains(this)) { boderColor = Color.WHITE; } g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias! RenderingHints.VALUE_ANTIALIAS_ON); // Color[] colors = {new Color(0, 0, 0, 0.7f), new Color(0, 0, 0, 0.15f)}; - if (diagram.selectedItem == this) { + if (getDiagram().getSelectedItems().contains(this)) { Color[] colors = new Color[]{new Color(0.6f, 0.6f, 1.0f, 0.8f), new Color(0.6f, 0.6f, 1.0f, 0.5f)}; float[] factors = {0f, 1f}; g.setPaint(new RadialGradientPaint(getWidth() / 2, getHeight() / 2, getWidth() / 2, factors, colors)); @@ -260,8 +243,8 @@ public class NodePanel extends DraggablePanel implements Selectable, PropertyCha @Override public void mousePressed(MouseEvent e) { - super.mousePressed(e); - diagram.select(this); + super.mousePressed(e); + diagram.select(this, e.isShiftDown() || e.isControlDown()); showToolBar(); } @@ -442,9 +425,7 @@ public class NodePanel extends DraggablePanel implements Selectable, PropertyCha public void delete() { Diagram diag = getDiagram(); - if (diag.selectedItem == this) { - diag.removeSelectedNode(); - } + diag.removeSelected(); } public void keyReleased(KeyEvent e) { diff --git a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/OutBusPanel.java b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/OutBusPanel.java index 521776dce..6a489dea6 100644 --- a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/OutBusPanel.java +++ b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/OutBusPanel.java @@ -110,7 +110,7 @@ public class OutBusPanel extends DraggablePanel implements ComponentListener, Se Polygon p = new Polygon(xs, ys, 8); - if (diagram.selectedItem == this) { + if (getDiagram().getSelectedItems().contains(this)) { int[] xs2 = {0, width - 30, width - 30, width, width - 32, width - 32, 0, 0}; int[] ys2 = {10, 10, 0, getHeight() / 2 + 2, getHeight(), getHeight() - 8, getHeight() - 8, 10}; @@ -154,7 +154,7 @@ public class OutBusPanel extends DraggablePanel implements ComponentListener, Se return; } super.mousePressed(e); - diagram.select(this); + diagram.select(this, e.isShiftDown() || e.isControlDown()); } @Override