From 0351f9e60fcd41b9040b8c37247dfdb29ed001a8 Mon Sep 17 00:00:00 2001 From: Nehon Date: Sun, 8 Feb 2015 18:04:33 +0100 Subject: [PATCH] added a drag and drop to scroll feature to the node editor with the mouse wheel click. --- .../editor/BackdropPanel.java | 27 +++- .../materialdefinition/editor/Diagram.java | 131 +++++++++++------- .../editor/DraggablePanel.java | 34 +++-- 3 files changed, 123 insertions(+), 69 deletions(-) diff --git a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/BackdropPanel.java b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/BackdropPanel.java index 3f22ad328..5855d01f1 100644 --- a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/BackdropPanel.java +++ b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/editor/BackdropPanel.java @@ -8,12 +8,14 @@ package com.jme3.gde.materialdefinition.editor; import com.jme3.gde.materials.MaterialPreviewRenderer; import com.jme3.material.Material; import java.awt.Container; +import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; import javax.swing.JScrollPane; import javax.swing.JViewport; import javax.swing.SwingUtilities; @@ -25,7 +27,7 @@ import javax.swing.event.ChangeListener; * * @author Nehon */ -public class BackdropPanel extends javax.swing.JPanel implements MouseListener, ChangeListener { +public class BackdropPanel extends javax.swing.JPanel implements MouseListener, ChangeListener,MouseMotionListener { private final MaterialPreviewRenderer renderer; private Material mat; @@ -40,6 +42,7 @@ public class BackdropPanel extends javax.swing.JPanel implements MouseListener, setLayout(null); toolBar.setVisible(false); addMouseListener(this); + addMouseMotionListener(this); renderer = new MaterialPreviewRenderer(previewLabel); recalculateTimer.setRepeats(false); addComponentListener(new ComponentAdapter() { @@ -399,6 +402,10 @@ public class BackdropPanel extends javax.swing.JPanel implements MouseListener, @Override public void mousePressed(MouseEvent e) { + Container c = getParent(); + if (c != null) { + ((MouseListener) c).mousePressed(SwingUtilities.convertMouseEvent(this, e, c)); + } } @Override @@ -448,8 +455,8 @@ public class BackdropPanel extends javax.swing.JPanel implements MouseListener, int size = Math.min(width, height) - 25; setSize(size, size); setBounds(0, 0, size, size); - - setLocation(pane.getHorizontalScrollBar().getValue() + width / 2 - size / 2, pane.getVerticalScrollBar().getValue() + height / 2 - size / 2); + Point pos= viewPort.getViewPosition(); + setLocation(pos.x + width / 2 - size / 2, pos.y + height / 2 - size / 2); repaint(); if (recalculateTimer.isRunning()) { @@ -467,4 +474,18 @@ public class BackdropPanel extends javax.swing.JPanel implements MouseListener, } } + public void mouseDragged(MouseEvent e) { + Container c = getParent(); + if (c != null) { + ((MouseMotionListener) c).mouseDragged(SwingUtilities.convertMouseEvent(this, e, c)); + } + } + + public void mouseMoved(MouseEvent e) { + Container c = getParent(); + if (c != null) { + ((MouseMotionListener) c).mouseMoved(SwingUtilities.convertMouseEvent(this, e, c)); + } + } + } 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 5a634097c..a4161129f 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 @@ -19,10 +19,12 @@ import com.jme3.shader.UniformBinding; import com.jme3.shader.VarType; import java.awt.Color; import java.awt.Component; +import java.awt.Cursor; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Point; +import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentEvent; @@ -39,6 +41,7 @@ import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; +import javax.swing.JScrollPane; import javax.swing.JSeparator; import javax.swing.JViewport; import javax.swing.SwingUtilities; @@ -67,7 +70,7 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene addMouseListener(this); addMouseMotionListener(this); - createPopupMenu(); + createPopupMenu(); } @Override @@ -77,64 +80,79 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene @Override public void mousePressed(MouseEvent e) { - for (OutBusPanel outBusPanel : outBuses) { - Point p = SwingUtilities.convertPoint(this, e.getX(), e.getY(), outBusPanel); - if (outBusPanel.contains(p)) { - MouseEvent me = SwingUtilities.convertMouseEvent(this, e, outBusPanel); - outBusPanel.dispatchEvent(me); + if (e.getButton() == MouseEvent.BUTTON1) { + for (OutBusPanel outBusPanel : outBuses) { + Point p = SwingUtilities.convertPoint(this, e.getX(), e.getY(), outBusPanel); + if (outBusPanel.contains(p)) { + MouseEvent me = SwingUtilities.convertMouseEvent(this, e, outBusPanel); + outBusPanel.dispatchEvent(me); + if (me.isConsumed()) { + return; + } + } + } + + for (Connection connection : connections) { + MouseEvent me = SwingUtilities.convertMouseEvent(this, e, connection); + connection.select(me); if (me.isConsumed()) { return; } } - } - for (Connection connection : connections) { - MouseEvent me = SwingUtilities.convertMouseEvent(this, e, connection); - connection.select(me); - if (me.isConsumed()) { - return; - } + selectedItem = null; + repaint(); + } else if (e.getButton() == MouseEvent.BUTTON2) { + setCursor(hndCursor); + pp.setLocation(e.getPoint()); + ((JScrollPane)getParent().getParent()).setWheelScrollingEnabled(false); } - - selectedItem = null; - repaint(); } public void refreshPreviews(Material mat) { for (OutBusPanel outBusPanel : outBuses) { - outBusPanel.updatePreview(mat); + outBusPanel.updatePreview(mat); } - if(backDrop.isVisible()){ + if (backDrop.isVisible()) { backDrop.showMaterial(mat); } } - - public void displayBackdrop(){ - if(backDrop.getParent()== null){ + + public void displayBackdrop() { + if (backDrop.getParent() == null) { add(backDrop); - ((JViewport)getParent()).addChangeListener(backDrop); + ((JViewport) getParent()).addChangeListener(backDrop); } - + backDrop.setVisible(true); - backDrop.update(((JViewport)getParent())); + backDrop.update(((JViewport) getParent())); } - + Point clickLoc = new Point(0, 0); @Override public void mouseReleased(MouseEvent e) { - if (draggedFrom != null && draggedFrom.getNode() instanceof OutBusPanel) { - MouseEvent me = SwingUtilities.convertMouseEvent(this, e, draggedFrom.getNode()); - draggedFrom.getNode().dispatchEvent(me); - if (me.isConsumed()) { - return; - } - } - if (e.getButton() != MouseEvent.BUTTON3) { - dispatchToOutBuses(e); - } else { - contextMenu.show(this, e.getX(), e.getY()); - clickLoc.setLocation(e.getX(), e.getY()); + + switch (e.getButton()) { + case MouseEvent.BUTTON1: + if (draggedFrom != null && draggedFrom.getNode() instanceof OutBusPanel) { + MouseEvent me = SwingUtilities.convertMouseEvent(this, e, draggedFrom.getNode()); + draggedFrom.getNode().dispatchEvent(me); + if (me.isConsumed()) { + return; + } + } + + dispatchToOutBuses(e); + break; + case MouseEvent.BUTTON2: + setCursor(defCursor); + ((JScrollPane)getParent().getParent()).setWheelScrollingEnabled(true); + break; + case MouseEvent.BUTTON3: + contextMenu.show(this, e.getX(), e.getY()); + clickLoc.setLocation(e.getX(), e.getY()); + break; } } @@ -151,11 +169,11 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene } repaint(); } - - protected void showEdit(NodePanel node) { - parent.showShaderEditor(node.getName(),node.getType(),node.filePaths); + + protected void showEdit(NodePanel node) { + parent.showShaderEditor(node.getName(), node.getType(), node.filePaths); } - + public void notifyMappingCreation(Connection conn) { parent.makeMapping(conn); } @@ -284,14 +302,28 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene } } + private final Cursor defCursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR); + private final Cursor hndCursor = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR); + private final Point pp = new Point(); + @Override public void mouseDragged(MouseEvent e) { - if (draggedFrom == null) { - if (selectedItem instanceof OutBusPanel) { - OutBusPanel bus = (OutBusPanel) selectedItem; - MouseEvent me = SwingUtilities.convertMouseEvent(this, e, bus); - bus.dispatchEvent(me); + 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); + } } + } else if (SwingUtilities.isMiddleMouseButton(e)) { + JViewport vport = (JViewport) getParent(); + Point cp = e.getPoint(); + Point vp = vport.getViewPosition(); + vp.translate(pp.x - cp.x, pp.y - cp.y); + scrollRectToVisible(new Rectangle(vp, vport.getSize())); + //pp.setLocation(cp); + } } @@ -313,7 +345,6 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene start.connect(conn); end.connect(conn); - addConnection(conn); return conn; @@ -401,14 +432,13 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene return item; } - - public void clear(){ + public void clear() { removeAll(); outBuses.clear(); connections.clear(); nodes.clear(); } - + private void createPopupMenu() { contextMenu.setFont(new Font("Tahoma", 1, 10)); // NOI18N contextMenu.setOpaque(true); @@ -421,7 +451,6 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene contextMenu.setBorder(BorderFactory.createCompoundBorder(contextMenu.getBorder(), labelBorder)); - JMenuItem nodeItem = createMenuItem("Node", Icons.node); nodeItem.addActionListener(new ActionListener() { @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 5ba949c9a..e81d1d5b6 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 @@ -8,6 +8,7 @@ import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.JPanel; +import javax.swing.SwingUtilities; /** * @@ -18,8 +19,7 @@ public class DraggablePanel extends JPanel implements MouseListener, MouseMotion protected int svdx, svdy, svdex, svdey; private boolean vertical = false; protected Diagram diagram; - - + public DraggablePanel(boolean vertical) { this(); this.vertical = vertical; @@ -36,15 +36,17 @@ public class DraggablePanel extends JPanel implements MouseListener, MouseMotion @Override public void mousePressed(MouseEvent e) { - svdx = getLocation().x; - if (!vertical) { - svdex = e.getXOnScreen(); + if (e.getButton() != MouseEvent.BUTTON2) { + svdx = getLocation().x; + if (!vertical) { + svdex = e.getXOnScreen(); + } + svdy = getLocation().y; + svdey = e.getYOnScreen(); + e.consume(); } - svdy = getLocation().y; - svdey = e.getYOnScreen(); - e.consume(); } - + @Override public void mouseReleased(MouseEvent e) { } @@ -63,13 +65,15 @@ public class DraggablePanel extends JPanel implements MouseListener, MouseMotion @Override public void mouseDragged(MouseEvent e) { - int xoffset = 0; - if (!vertical) { - xoffset = e.getLocationOnScreen().x - svdex; + if (!SwingUtilities.isMiddleMouseButton(e)) { + int xoffset = 0; + if (!vertical) { + xoffset = e.getLocationOnScreen().x - svdex; + } + int yoffset = e.getLocationOnScreen().y - svdey; + setLocation(Math.max(0, svdx + xoffset), Math.max(0, svdy + yoffset)); + e.consume(); } - int yoffset = e.getLocationOnScreen().y - svdey; - setLocation(Math.max(0, svdx + xoffset), Math.max(0,svdy + yoffset)); - e.consume(); } public Diagram getDiagram() {