From 845b14c4f03cbc6e3df1d6323e0dc1a3da6e85dd Mon Sep 17 00:00:00 2001 From: "rem..om" Date: Thu, 28 Feb 2013 21:55:57 +0000 Subject: [PATCH] SDK mat def editor : proof checks on node naming git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10454 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../materialdefinition/editor/Diagram.java | 143 ++++++++++-------- .../fileStructure/TechniqueBlock.java | 1 - .../navigator/node/ShaderNodeNode.java | 33 +++- .../navigator/node/TechniqueNode.java | 4 + .../node/properties/DefaultProperty.java | 2 +- 5 files changed, 113 insertions(+), 70 deletions(-) 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 09e113489..c7a2954b5 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 @@ -49,7 +49,7 @@ import javax.swing.border.TitledBorder; * @author Nehon */ public class Diagram extends JPanel implements MouseListener, MouseMotionListener, ComponentListener { - + protected Dot draggedFrom; protected Dot draggedTo; protected Selectable selectedItem; @@ -59,21 +59,21 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene private MyMenu contextMenu = new MyMenu("Add"); private MatDefEditorlElement parent; private String currentTechniqueName; - + public Diagram() { - + addMouseListener(this); addMouseMotionListener(this); createPopupMenu(); } - + @Override public void mouseClicked(MouseEvent e) { } - + @Override public void mousePressed(MouseEvent e) { - + for (OutBusPanel outBusPanel : outBuses) { Point p = SwingUtilities.convertPoint(this, e.getX(), e.getY(), outBusPanel); if (outBusPanel.contains(p)) { @@ -84,7 +84,7 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene } } } - + for (Connection connection : connections) { MouseEvent me = SwingUtilities.convertMouseEvent(this, e, connection); connection.select(me); @@ -92,18 +92,18 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene return; } } - + selectedItem = null; repaint(); } - + public void refreshPreviews(Material mat) { for (OutBusPanel outBusPanel : outBuses) { outBusPanel.updatePreview(mat); } } Point clickLoc = new Point(0, 0); - + @Override public void mouseReleased(MouseEvent e) { if (draggedFrom != null && draggedFrom.getNode() instanceof OutBusPanel) { @@ -119,13 +119,13 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene contextMenu.show(this, e.getX(), e.getY()); clickLoc.setLocation(e.getX(), e.getY()); } - + } - + public MatDefEditorlElement getEditorParent() { return parent; } - + public void addConnection(Connection conn) { connections.add(conn); add(conn); @@ -134,11 +134,11 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene } repaint(); } - + public void notifyMappingCreation(Connection conn) { parent.makeMapping(conn); - } - + } + public void addNode(NodePanel node) { add(node); node.setTechName(currentTechniqueName); @@ -147,7 +147,7 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene setComponentZOrder(node, 0); node.addComponentListener(this); } - + public void addOutBus(OutBusPanel bus) { outBuses.add(bus); bus.setDiagram(this); @@ -157,15 +157,15 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene bus.componentResized(new ComponentEvent(this, ActionEvent.ACTION_PERFORMED)); bus.revalidate(); } - + @Override public void mouseEntered(MouseEvent e) { } - + @Override public void mouseExited(MouseEvent e) { } - + protected void removeSelectedConnection() { if (selectedItem instanceof Connection) { Connection selectedConnection = (Connection) selectedItem; @@ -174,11 +174,26 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene parent.notifyRemoveConnection(selectedConnection); } } - + + private String fixNodeName(String name) { + return fixNodeName(name, 0); + } + + private String fixNodeName(String name, int count) { + for (NodePanel nodePanel : nodes) { + if ((name + (count == 0 ? "" : count)).equals(nodePanel.getName())) { + return fixNodeName(name, count + 1); + } + } + return name + (count == 0 ? "" : count); + } + public void addNodesFromDefs(List defList, String path, Point clickPosition) { int i = 0; for (ShaderNodeDefinition def : defList) { ShaderNodeBlock sn = new ShaderNodeBlock(def, path); + sn.setName(fixNodeName(sn.getName())); + NodePanel np = new NodePanel(sn, def); addNode(np); np.setLocation(clickPosition.x + i * 150, clickPosition.y); @@ -189,7 +204,7 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene } repaint(); } - + public void addMatParam(String type, String name, Point point) { String fixedType = type; if (type.equals("Color")) { @@ -203,9 +218,9 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene repaint(); getEditorParent().notifyAddMapParam(type, name); } - + public void addWorldParam(UniformBinding binding, Point point) { - + ShaderNodeVariable param = new ShaderNodeVariable(binding.getGlslType(), binding.name()); NodePanel np = new NodePanel(param, NodePanel.NodeType.WorldParam); addNode(np); @@ -214,16 +229,16 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene repaint(); getEditorParent().notifyAddWorldParam(binding.name()); } - + public void addAttribute(String name, String type, Point point) { ShaderNodeVariable param = new ShaderNodeVariable(type, "Attr", name); NodePanel np = new NodePanel(param, NodePanel.NodeType.Attribute); addNode(np); np.setLocation(point.x, point.y); np.revalidate(); - repaint(); + 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); @@ -239,7 +254,7 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene remove(conn); } } - + remove(selectedNode); selectedItem = null; repaint(); @@ -247,7 +262,7 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene } } } - + @Override public void mouseDragged(MouseEvent e) { if (draggedFrom == null) { @@ -258,7 +273,7 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene } } } - + protected void draggingDot(MouseEvent e) { for (OutBusPanel outBusPanel : outBuses) { Point p = SwingUtilities.convertPoint(this, e.getX(), e.getY(), outBusPanel); @@ -271,18 +286,18 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene } } } - + public Connection connect(Dot start, Dot end) { Connection conn = new Connection(start, end); start.connect(conn); end.connect(conn); - - + + addConnection(conn); - + return conn; } - + public NodePanel getNodePanel(String key) { for (NodePanel nodePanel : nodes) { if (nodePanel.getKey().equals(key)) { @@ -291,7 +306,7 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene } return null; } - + public OutBusPanel getOutBusPanel(String key) { for (OutBusPanel out : outBuses) { if (out.getKey().equals(key)) { @@ -333,19 +348,19 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene * @return */ public Selectable select(String key) { - + for (NodePanel nodePanel : nodes) { if (nodePanel.getKey().equals(key)) { return doSelect(nodePanel); } } - + for (Connection connection : connections) { if (connection.getKey().equals(key)) { return doSelect(connection); } } - + for (OutBusPanel outBusPanel : outBuses) { if (outBusPanel.getKey().equals(key)) { return doSelect(outBusPanel); @@ -353,18 +368,18 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene } return doSelect(null); } - + @Override public void mouseMoved(MouseEvent e) { dispatchToOutBuses(e); } - + private JMenuItem createMenuItem(String text, Icon icon) { JMenuItem item = new JMenuItem(text, icon); item.setFont(new Font("Tahoma", 1, 10)); // NOI18N return item; } - + private void createPopupMenu() { contextMenu.setFont(new Font("Tahoma", 1, 10)); // NOI18N contextMenu.setOpaque(true); @@ -372,12 +387,12 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene TitledBorder labelBorder = BorderFactory.createTitledBorder( titleUnderline, contextMenu.getLabel(), TitledBorder.LEADING, TitledBorder.ABOVE_TOP, contextMenu.getFont(), Color.BLACK); - + contextMenu.setBorder(BorderFactory.createLineBorder(Color.BLACK)); contextMenu.setBorder(BorderFactory.createCompoundBorder(contextMenu.getBorder(), labelBorder)); - - + + JMenuItem nodeItem = createMenuItem("Node", Icons.node); nodeItem.addActionListener(new ActionListener() { @Override @@ -387,7 +402,7 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene d.setVisible(true); } }); - + contextMenu.add(nodeItem); contextMenu.add(createSeparator()); JMenuItem matParamItem = createMenuItem("Material Parameter", Icons.mat); @@ -400,7 +415,7 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene } }); contextMenu.add(matParamItem); - JMenuItem worldParamItem = createMenuItem("World Parameter", Icons.world); + JMenuItem worldParamItem = createMenuItem("World Parameter", Icons.world); worldParamItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -428,19 +443,19 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene public void actionPerformed(ActionEvent e) { OutBusPanel p2 = new OutBusPanel("color" + (outBuses.size() - 1), Shader.ShaderType.Fragment); p2.setBounds(0, 350 + 50 * (outBuses.size() - 1), p2.getWidth(), p2.getHeight()); - + addOutBus(p2); - + } }); } - + private JSeparator createSeparator() { JSeparator jsep = new JSeparator(JSeparator.HORIZONTAL); jsep.setBackground(Color.BLACK); return jsep; } - + private void dispatchToOutBuses(MouseEvent e) { for (OutBusPanel outBusPanel : outBuses) { Point p = SwingUtilities.convertPoint(this, e.getX(), e.getY(), outBusPanel); @@ -453,20 +468,20 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene } } } - + private void removeConnection(Connection selectedConnection) { connections.remove(selectedConnection); selectedConnection.end.disconnect(); selectedConnection.start.disconnect(); remove(selectedConnection); } - + private class MyMenu extends JPopupMenu { - + public MyMenu(String label) { super(label); } - + @Override protected void paintComponent(Graphics g) { //Color c1 = new Color(100, 100, 100, 255); @@ -475,11 +490,11 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene g.fillRect(0, 0, getWidth(), getHeight()); } } - + public void fixSize() { int maxWidth = minWidth; int maxHeight = minHeight; - + for (NodePanel nodePanel : nodes) { int w = nodePanel.getLocation().x + nodePanel.getWidth() + 150; if (w > maxWidth) { @@ -501,30 +516,30 @@ public class Diagram extends JPanel implements MouseListener, MouseMotionListene } int minWidth = 0; int minHeight = 0; - + public void componentResized(ComponentEvent e) { minWidth = e.getComponent().getWidth() - 2; minHeight = e.getComponent().getHeight() - 2; fixSize(); } - + public void componentMoved(ComponentEvent e) { } - + public void componentShown(ComponentEvent e) { } - + public void componentHidden(ComponentEvent e) { } - + public void setParent(MatDefEditorlElement parent) { this.parent = parent; } - + public void setCurrentTechniqueName(String currentTechniqueName) { this.currentTechniqueName = currentTechniqueName; } - + public String getCurrentTechniqueName() { return currentTechniqueName; } diff --git a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/fileStructure/TechniqueBlock.java b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/fileStructure/TechniqueBlock.java index 27a52c0ce..b364e6330 100644 --- a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/fileStructure/TechniqueBlock.java +++ b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/fileStructure/TechniqueBlock.java @@ -7,7 +7,6 @@ package com.jme3.gde.materialdefinition.fileStructure; import com.jme3.gde.materialdefinition.fileStructure.leaves.FragmentShaderFileBlock; import com.jme3.gde.materialdefinition.fileStructure.leaves.InputMappingBlock; import com.jme3.gde.materialdefinition.fileStructure.leaves.LightModeBlock; -import com.jme3.gde.materialdefinition.fileStructure.leaves.MatParamBlock; import com.jme3.gde.materialdefinition.fileStructure.leaves.ShaderFileBlock; import com.jme3.gde.materialdefinition.fileStructure.leaves.UnsupportedStatement; import com.jme3.gde.materialdefinition.fileStructure.leaves.VertexShaderFileBlock; diff --git a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/navigator/node/ShaderNodeNode.java b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/navigator/node/ShaderNodeNode.java index f7133bc8c..421a4022c 100644 --- a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/navigator/node/ShaderNodeNode.java +++ b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/navigator/node/ShaderNodeNode.java @@ -7,7 +7,9 @@ package com.jme3.gde.materialdefinition.navigator.node; import com.jme3.gde.core.assets.ProjectAssetManager; import com.jme3.gde.materialdefinition.MatDefDataObject; import com.jme3.gde.materialdefinition.editor.Selectable; +import com.jme3.gde.materialdefinition.fileStructure.MatDefBlock; import com.jme3.gde.materialdefinition.fileStructure.ShaderNodeBlock; +import com.jme3.gde.materialdefinition.fileStructure.TechniqueBlock; import com.jme3.gde.materialdefinition.fileStructure.leaves.MappingBlock; import com.jme3.gde.materialdefinition.icons.Icons; import com.jme3.gde.materialdefinition.navigator.node.properties.DefaultProperty; @@ -17,6 +19,7 @@ import com.jme3.shader.ShaderNodeDefinition; import java.awt.Image; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; import org.openide.nodes.Children; @@ -33,10 +36,9 @@ import org.openide.util.WeakListeners; public class ShaderNodeNode extends AbstractMatDefNode implements Selectable, PropertyChangeListener { ShaderNodeBlock shaderNode; - ShaderNodeDefinition def; + ShaderNodeDefinition def; String key = ""; - public ShaderNodeNode(final Lookup lookup, final ShaderNodeBlock shaderNode) { // super(Children.create(new ChildFactory() { // @Override @@ -55,7 +57,7 @@ public class ShaderNodeNode extends AbstractMatDefNode implements Selectable, Pr // return new MappingNode(lookup, key); // } // }, true), lookup); - super(new MappingNodeChildren(lookup, shaderNode), lookup); + super(new MappingNodeChildren(lookup, shaderNode), lookup); this.shaderNode = shaderNode; setName(shaderNode.getName()); key = makeKey(); @@ -78,7 +80,17 @@ public class ShaderNodeNode extends AbstractMatDefNode implements Selectable, Pr set.setDisplayName(shaderNode.getName() + " ShaderNode"); set.setShortDescription(def.getDocumentation()); try { - set.put(new DefaultProperty(shaderNode, String.class, "Name", "getName", "setName")); + set.put(new DefaultProperty(shaderNode, String.class, "Name", "getName", "setName") { + @Override + public void setValue(String val) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { + val = fixNodeName(val); + //glsl variable can't start with a number + if(val.matches("^\\d.*")){ + return; + } + super.setValue(val); + } + }); set.put(new DefaultProperty(shaderNode, String.class, "Condition", "getCondition", "setCondition")); } catch (NoSuchMethodException ex) { @@ -91,8 +103,21 @@ public class ShaderNodeNode extends AbstractMatDefNode implements Selectable, Pr sheet.put(set); return sheet; + } + private String fixNodeName(String name) { + TechniqueBlock tech = ((TechniqueNode) this.getParentNode()).getDef(); + List list = tech.getShaderNodes(); + return fixNodeName(list, name, 0); + } + private String fixNodeName(List nodes, String name, int count) { + for (ShaderNodeBlock nodePanel : nodes) { + if ((name + (count == 0 ? "" : count)).equals(nodePanel.getName())) { + return fixNodeName(nodes, name, count + 1); + } + } + return name + (count == 0 ? "" : count); } @Override diff --git a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/navigator/node/TechniqueNode.java b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/navigator/node/TechniqueNode.java index ebb5e199b..51ad77467 100644 --- a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/navigator/node/TechniqueNode.java +++ b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/navigator/node/TechniqueNode.java @@ -56,6 +56,10 @@ public class TechniqueNode extends AbstractMatDefNode implements Selectable, Pro } + protected TechniqueBlock getDef() { + return def; + } + @Override public Image getIcon(int type) { return Icons.tech.getImage(); diff --git a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/navigator/node/properties/DefaultProperty.java b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/navigator/node/properties/DefaultProperty.java index 487f3f80e..781abf540 100644 --- a/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/navigator/node/properties/DefaultProperty.java +++ b/sdk/jme3-materialeditor/src/com/jme3/gde/materialdefinition/navigator/node/properties/DefaultProperty.java @@ -28,5 +28,5 @@ public class DefaultProperty extends PropertySupport.Reflection { @Override public boolean canWrite() { return !readOnly; - } + } }