OSDN Git Service

- add support for parsing worldforge content to AssetPack projects
authornormen667 <normen667@75d07b2b-3a1a-0410-a2c5-0572b91ccdca>
Fri, 1 Jul 2011 22:07:45 +0000 (22:07 +0000)
committernormen667 <normen667@75d07b2b-3a1a-0410-a2c5-0572b91ccdca>
Fri, 1 Jul 2011 22:07:45 +0000 (22:07 +0000)
- add support for selectable material per asset in AssetPacks
- add option for cleaning up assets in AssetPack projects

git-svn-id: http://jmonkeyengine.googlecode.com/svn/trunk@7798 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

18 files changed:
sdk/jme3-assetpack-support/nbproject/genfiles.properties
sdk/jme3-assetpack-support/nbproject/project.xml
sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/AssetConfiguration.java [new file with mode: 0644]
sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/AssetPackLoader.java
sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/Bundle.properties
sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/VariationSelection.form [new file with mode: 0644]
sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/VariationSelection.java [new file with mode: 0644]
sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/actions/AddAssetAction.java
sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/actions/PreviewAssetAction.java
sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/project/AssetPackProjectLogicalView.java
sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/project/actions/CleanupProjectAction.java [new file with mode: 0644]
sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/project/actions/ImportWorldForgeAction.java [new file with mode: 0644]
sdk/jme3-core/src/com/jme3/gde/core/layer.xml
sdk/jme3-core/src/com/jme3/gde/core/sceneexplorer/nodes/JmeSpatialChildren.java
sdk/jme3-core/src/com/jme3/gde/core/util/DynamicLookup.java
sdk/jme3-ogretools/src/com/jme3/gde/ogretools/OgreBinaryMeshDataObject.java
sdk/jme3-ogretools/src/com/jme3/gde/ogretools/convert/AdvOgreXMLConvertAction.java
sdk/jme3-ogretools/src/com/jme3/gde/ogretools/convert/OgreXMLConvert.java

index 3e43796..21e3638 100644 (file)
@@ -3,6 +3,6 @@ build.xml.script.CRC32=c0969383
 build.xml.stylesheet.CRC32=a56c6a5b@1.42.2
 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
 # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
-nbproject/build-impl.xml.data.CRC32=71a82056
+nbproject/build-impl.xml.data.CRC32=0fd46426
 nbproject/build-impl.xml.script.CRC32=4d376df0
-nbproject/build-impl.xml.stylesheet.CRC32=238281d1@1.42.2
+nbproject/build-impl.xml.stylesheet.CRC32=238281d1@1.45.1
index fa88b8a..11b14ec 100644 (file)
                     </run-dependency>
                 </dependency>
                 <dependency>
+                    <code-name-base>org.netbeans.api.java.classpath</code-name-base>
+                    <build-prerequisite/>
+                    <compile-dependency/>
+                    <run-dependency>
+                        <release-version>1</release-version>
+                        <specification-version>1.30</specification-version>
+                    </run-dependency>
+                </dependency>
+                <dependency>
                     <code-name-base>org.netbeans.api.progress</code-name-base>
                     <build-prerequisite/>
                     <compile-dependency/>
diff --git a/sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/AssetConfiguration.java b/sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/AssetConfiguration.java
new file mode 100644 (file)
index 0000000..97ab33b
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package com.jme3.gde.assetpack;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+/**
+ * Configuration for a single asset item while loading
+ * @author normenhansen
+ */
+public class AssetConfiguration {
+
+    private Element assetElement;
+    private List<NodeList> variationAssets;
+
+    public AssetConfiguration(Element assetElement) {
+        this.assetElement = assetElement;
+    }
+
+    public Element getAssetElement() {
+        return assetElement;
+    }
+
+    public List<NodeList> getVariationAssets() {
+        return variationAssets;
+    }
+
+    public void addVariationAssets(Element variationElement) {
+        if (variationAssets == null) {
+            variationAssets = new ArrayList<NodeList>();
+        }
+        variationAssets.add(variationElement.getElementsByTagName("file"));
+    }
+
+    public void clearExtraAssets() {
+        variationAssets.clear();
+    }
+
+}
index 6e65a32..e2e9ee0 100644 (file)
@@ -4,10 +4,6 @@
  */
 package com.jme3.gde.assetpack;
 
-import com.jme3.animation.AnimControl;
-import com.jme3.animation.BoneAnimation;
-import com.jme3.animation.BoneTrack;
-import com.jme3.animation.SkeletonControl;
 import com.jme3.asset.ModelKey;
 import com.jme3.gde.assetpack.actions.AddAssetAction;
 import com.jme3.gde.assetpack.project.wizards.FileDescription;
@@ -23,13 +19,14 @@ import com.jme3.scene.plugins.ogre.matext.MaterialExtensionSet;
 import com.jme3.scene.plugins.ogre.matext.OgreMaterialKey;
 import java.io.File;
 import java.io.IOException;
-import java.util.Collection;
+import java.util.ArrayList;
 import java.util.Iterator;
-import java.util.LinkedList;
+import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import org.openide.filesystems.FileObject;
 import org.openide.filesystems.FileUtil;
+import org.openide.util.Exceptions;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
 
@@ -39,28 +36,68 @@ import org.w3c.dom.NodeList;
  */
 public class AssetPackLoader {
 
-    public static Spatial loadAssetPackModel(Element assetElement, ProjectAssetManager pm) {
+    public static Spatial loadAssetPackModel(ProjectAssetManager pm, AssetConfiguration config) {
+        Element assetElement = config.getAssetElement();
         NodeList fileNodeList = assetElement.getElementsByTagName("file");
         Element fileElement = XmlHelper.findChildElementWithAttribute(assetElement, "file", "main", "true");
         if (fileElement == null) {
             fileElement = XmlHelper.findChildElement(assetElement, "file");
         }
+        //find main files for this model
+        List<Element> files = new ArrayList<Element>();
+        while (fileElement != null) {
+            files.add(fileElement);
+            //TODO:doesnt work?
+            fileElement = XmlHelper.findNextElementWithAttribute(fileElement, "file", "main", "true");
+        }
+
+        //find material varations for this model
+        List<SelectionEntry> selList = new ArrayList<SelectionEntry>();
+        Element matset = XmlHelper.findChildElement(assetElement, "materialvariations");
+        if (matset != null) {
+            Element part = XmlHelper.findChildElement(matset, "mesh");
+            String partName = null;
+            while (part != null) {
+                partName = part.getAttribute("name");
+                ArrayList<Element> variations = new ArrayList<Element>();
+                selList.add(new SelectionEntry(partName, variations));
+                Element variation = XmlHelper.findChildElement(part, "variation");
+                while (variation != null) {
+                    variations.add(variation);
+                    variation = XmlHelper.findNextSiblingElement(variation);
+                }
+                part = XmlHelper.findNextSiblingElement(part);
+            }
+        }
+        //let user select variation
+        boolean selectable = false;
+        for (Iterator<SelectionEntry> it = selList.iterator(); it.hasNext();) {
+            SelectionEntry selectionEntry = it.next();
+            if (selectionEntry.names.size() > 1) {
+                selectable = true;
+            }
+        }
+        if (selectable) {
+            new VariationSelection(selList).setVisible(true);
+        }
+        for (Iterator<SelectionEntry> it = selList.iterator(); it.hasNext();) {
+            SelectionEntry selectionEntry = it.next();
+            config.addVariationAssets(selectionEntry.getSelected());
+        }
         Spatial model = null;
         Node node = null;
-        while (fileElement != null) {
-            Logger.getLogger(AssetPackLoader.class.getName()).log(Level.INFO, "Load main file {0}", fileElement.getAttribute("path"));
+        for (Element element : files) {
+            Logger.getLogger(AssetPackLoader.class.getName()).log(Level.INFO, "Load main file {0}", element.getAttribute("path"));
             if (model != null && node == null) {
                 node = new Node(assetElement.getAttribute("name"));
                 node.attachChild(model);
             }
-            model = AssetPackLoader.loadSingleMesh(fileElement, fileNodeList, pm);
+            model = AssetPackLoader.loadSingleMesh(element, fileNodeList, config.getVariationAssets(), pm);
             if (model != null && node != null) {
                 node.attachChild(model);
             } else {
                 Logger.getLogger(AssetPackLoader.class.getName()).log(Level.WARNING, "Error loading model");
             }
-            //TODO:doesnt work?
-            fileElement = XmlHelper.findNextElementWithAttribute(fileElement, "file", "main", "true");
         }
         if (node != null) {
             return node;
@@ -68,64 +105,7 @@ public class AssetPackLoader {
         return model;
     }
 
-    // TODO: merge animation controls for multi meshes
-    private static void moveControls(Spatial from, Node to) {
-        AnimControl control = to.getControl(AnimControl.class);
-        AnimControl control2 = from.getControl(AnimControl.class);
-        if (control == null) {
-            SkeletonControl fromSkeletonControl = from.getControl(SkeletonControl.class);
-            control = new AnimControl(control2.getSkeleton());
-            SkeletonControl toSkeletonControl = to.getControl(SkeletonControl.class);
-            if (toSkeletonControl == null) {
-                toSkeletonControl = new SkeletonControl(fromSkeletonControl.getTargets(), control.getSkeleton());
-            }
-            to.addControl(control);
-            to.addControl(toSkeletonControl);
-        }
-        Collection<String> names = control.getAnimationNames();
-        Collection<String> names2 = new LinkedList<String>(control2.getAnimationNames());
-        //add tracks from anims interface second that exist in first
-        for (Iterator<String> it = names.iterator(); it.hasNext();) {
-            String string = it.next();
-            names2.remove(string);
-            BoneAnimation anim = control.getAnim(string);
-            BoneTrack[] tracks = anim.getTracks();
-            BoneAnimation anim2 = control2.getAnim(string);
-            if (anim2 != null) {
-                BoneTrack[] tracks2 = anim2.getTracks();
-                BoneTrack[] newTracks = new BoneTrack[tracks.length + tracks2.length];
-                for (int i = 0; i < tracks.length; i++) {
-                    newTracks[i] = tracks[i];
-                }
-                for (int i = tracks.length; i < tracks2.length; i++) {
-                    newTracks[i] = tracks2[i - tracks.length];
-                }
-                anim.setTracks(newTracks);
-            }
-        }
-        //add tracks from anims in second to first
-        for (Iterator<String> it = names2.iterator(); it.hasNext();) {
-            String string = it.next();
-            BoneAnimation anim2 = control2.getAnim(string);
-            BoneTrack[] tracks2 = anim2.getTracks();
-            BoneAnimation anim = control.getAnim(string);
-            if (anim != null) {
-                BoneTrack[] tracks = anim.getTracks();
-                BoneTrack[] newTracks = new BoneTrack[tracks.length + tracks2.length];
-                for (int i = 0; i < tracks.length; i++) {
-                    newTracks[i] = tracks[i];
-                }
-                for (int i = tracks.length; i < tracks2.length; i++) {
-                    newTracks[i] = tracks2[i - tracks.length];
-                }
-                anim.setTracks(newTracks);
-            } else {
-                control.addAnim(anim2);
-            }
-        }
-    }
-
-    private static Spatial loadSingleMesh(Element fileElement, NodeList fileNodeList, ProjectAssetManager pm) {
+    private static Spatial loadSingleMesh(Element fileElement, NodeList fileNodeList, List<NodeList> variationNodeList, ProjectAssetManager pm) {
         ModelKey key = null;
         Material mat = null;
         Spatial model;
@@ -203,6 +183,37 @@ public class AssetPackLoader {
         } else if (hasExtension(name, "j3o")) {
             //should have all info inside
         }
+
+        if (variationNodeList != null) {
+            for (NodeList nodeList : variationNodeList) {
+                for (int i = 0; i < nodeList.getLength(); i++) {
+                    Element fileElem = (Element) nodeList.item(i);
+                    String type = fileElem.getAttribute("type");
+                    String path = fileElem.getAttribute("path");
+                    if ("material".equals(type)) {
+                        if (hasExtension(path, "j3m")) {
+                            mat = pm.loadMaterial(path);
+                        } else if (hasExtension(path, "material")) {
+                            if (matList == null) {
+                                Logger.getLogger(AssetPackLoader.class.getName()).log(Level.INFO, "Load Ogre Material");
+                                OgreMaterialKey matKey = new OgreMaterialKey(path);
+                                matKey.setMaterialExtensionSet(matExts);
+                                matList = pm.loadAsset(matKey);
+                                key = new OgreMeshKey(name, matList);
+                            } else {
+                                Logger.getLogger(AssetPackLoader.class.getName()).log(Level.INFO, "Add Ogre Material");
+                                OgreMaterialKey matKey = new OgreMaterialKey(path);
+                                matKey.setMaterialExtensionSet(matExts);
+                                MaterialList newMatList = pm.loadAsset(matKey);
+                                matList.putAll(newMatList);
+                            }
+                        }
+                    }
+                }
+
+            }
+        }
+
         if (key != null && mat != null) {
             Logger.getLogger(AddAssetAction.class.getName()).log(Level.WARNING, "j3m and ogre material defined for asset {0}.", name);
         }
@@ -233,12 +244,30 @@ public class AssetPackLoader {
         return false;
     }
 
+    public static FileDescription getFileDescription(String path) {
+        return getFileDescription(new File(path.replaceAll("/", File.separator)));
+    }
+
     public static FileDescription getFileDescription(File file) {
-        FileObject fileObject = FileUtil.toFileObject(file);
-        return getFileDescription(fileObject);
+        FileObject fileObject;
+        try {
+            fileObject = FileUtil.toFileObject(file.getCanonicalFile());
+            if (fileObject == null) {
+                Logger.getLogger(AssetPackLoader.class.getName()).log(Level.WARNING, "Cannot find asset {0}", file.getPath());
+                return null;
+            }
+            return getFileDescription(fileObject);
+        } catch (IOException ex) {
+            Logger.getLogger(AssetPackLoader.class.getName()).log(Level.WARNING, "Cannot find asset {0}", file.getPath());
+            Exceptions.printStackTrace(ex);
+        }
+        return null;
     }
 
     public static FileDescription getFileDescription(FileObject fileObject) {
+        if (fileObject == null) {
+            return null;
+        }
         FileDescription description = new FileDescription();
         description.setFile(fileObject);
         if ("material".equals(fileObject.getExt())) {
@@ -281,8 +310,8 @@ public class AssetPackLoader {
         return description;
     }
 
-    public static void addAllFiles(Element assetElement, ProjectAssetManager pm) {
-        //TODO: not good :/
+    public static void addAllFiles(ProjectAssetManager pm, AssetConfiguration config) {
+        Element assetElement = config.getAssetElement();
         NodeList list = assetElement.getElementsByTagName("file");
         ProjectAssetManager proman = null;
         try {
@@ -316,8 +345,8 @@ public class AssetPackLoader {
         return;
     }
 
-    public static void addModelFiles(Element assetElement, ProjectAssetManager pm) {
-        //TODO: not good :/
+    public static void addModelFiles(ProjectAssetManager pm, AssetConfiguration config) {
+        Element assetElement = config.getAssetElement();
         NodeList fileNodeList = assetElement.getElementsByTagName("file");
         ProjectAssetManager currentProjectAssetManager = null;
         try {
@@ -351,6 +380,68 @@ public class AssetPackLoader {
                 }
             }
         }
+        List<NodeList> varAssets = config.getVariationAssets();
+        if (varAssets != null) {
+            for (NodeList nodeList : varAssets) {
+                addVariationFiles(nodeList, pm);
+            }
+        }
+        return;
+    }
+
+    private static void addVariationFiles(NodeList fileNodeList, ProjectAssetManager pm) {
+        ProjectAssetManager currentProjectAssetManager = null;
+        try {
+            currentProjectAssetManager = SceneApplication.getApplication().getCurrentSceneRequest().getManager();
+            if (currentProjectAssetManager == null) {
+                Logger.getLogger(AssetPackLoader.class.getName()).log(Level.SEVERE, "Could not get project asset manager!");
+                return;
+            }
+        } catch (Exception e) {
+            Logger.getLogger(AssetPackLoader.class.getName()).log(Level.SEVERE, "Could not get project asset manager!");
+            return;
+        }
+        for (int i = 0; i < fileNodeList.getLength(); i++) {
+            Element fileElem = (Element) fileNodeList.item(i);
+            String type = fileElem.getAttribute("type");
+            try {
+                if ("texture".equals(type) || "sound".equals(type) || "materialdef".equals(type) || "shader".equals(type) || "other".equals(type)) {
+                    String src = pm.getAbsoluteAssetPath(fileElem.getAttribute("path"));
+                    if (src == null) {
+                        Logger.getLogger(AssetPackLoader.class.getName()).log(Level.SEVERE, "Could not find texture with manager!");
+                        return;
+                    }
+                    FileObject srcFile = FileUtil.toFileObject(new File(src));
+                    String destName = currentProjectAssetManager.getAssetFolderName() + "/" + fileElem.getAttribute("path");
+                    String destFolder = destName.replace("\\", "/");
+                    destFolder = destFolder.substring(0, destFolder.lastIndexOf("/"));
+                    FileObject folder = FileUtil.createFolder(new File(destFolder));
+                    srcFile.copy(folder, srcFile.getName(), srcFile.getExt());
+                }
+            } catch (IOException ex) {
+                Logger.getLogger(AssetPackLoader.class.getName()).log(Level.SEVERE, "Could not copy texture: {0}", ex.getMessage());
+            }
+        }
         return;
     }
+
+    public static class SelectionEntry {
+
+        String part;
+        List<Element> names;
+        int selected = 0;
+
+        public SelectionEntry(String part, List<Element> names) {
+            this.part = part;
+            this.names = names;
+        }
+
+        public Element getSelected() {
+            return names.get(selected);
+        }
+
+        public void setSelected(int selected) {
+            this.selected = selected;
+        }
+    }
 }
index 37e904b..efb841e 100644 (file)
@@ -4,3 +4,4 @@ OpenIDE-Module-Long-Description=\
 OpenIDE-Module-Name=AssetPack Support
 OpenIDE-Module-Short-Description=AssetPack Support
 Templates/Project/AssetPack/AssetPackProject.zip=Asset Pack
+VariationSelection.jButton1.text=OK
diff --git a/sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/VariationSelection.form b/sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/VariationSelection.form
new file mode 100644 (file)
index 0000000..639f21f
--- /dev/null
@@ -0,0 +1,66 @@
+<?xml version="1.1" encoding="UTF-8" ?>
+
+<Form version="1.5" maxVersion="1.7" type="org.netbeans.modules.form.forminfo.JDialogFormInfo">
+  <Properties>
+    <Property name="defaultCloseOperation" type="int" value="2"/>
+  </Properties>
+  <SyntheticProperties>
+    <SyntheticProperty name="formSizePolicy" type="int" value="1"/>
+  </SyntheticProperties>
+  <AuxValues>
+    <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="1"/>
+    <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
+    <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
+    <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
+    <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="true"/>
+    <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
+    <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
+    <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
+    <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
+  </AuxValues>
+
+  <Layout>
+    <DimensionLayout dim="0">
+      <Group type="103" groupAlignment="0" attributes="0">
+          <Group type="102" alignment="1" attributes="0">
+              <EmptySpace max="-2" attributes="0"/>
+              <Component id="jButton1" min="-2" max="-2" attributes="0"/>
+          </Group>
+          <Component id="jScrollPane1" alignment="0" pref="267" max="32767" attributes="0"/>
+      </Group>
+    </DimensionLayout>
+    <DimensionLayout dim="1">
+      <Group type="103" groupAlignment="0" attributes="0">
+          <Group type="102" alignment="1" attributes="0">
+              <Component id="jScrollPane1" pref="192" max="32767" attributes="0"/>
+              <EmptySpace max="-2" attributes="0"/>
+              <Component id="jButton1" min="-2" max="-2" attributes="0"/>
+          </Group>
+      </Group>
+    </DimensionLayout>
+  </Layout>
+  <SubComponents>
+    <Component class="javax.swing.JButton" name="jButton1">
+      <Properties>
+        <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
+          <ResourceString bundle="com/jme3/gde/assetpack/Bundle.properties" key="VariationSelection.jButton1.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
+        </Property>
+      </Properties>
+      <Events>
+        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButton1ActionPerformed"/>
+      </Events>
+    </Component>
+    <Container class="javax.swing.JScrollPane" name="jScrollPane1">
+
+      <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
+      <SubComponents>
+        <Container class="javax.swing.JPanel" name="jPanel1">
+
+          <Layout class="org.netbeans.modules.form.compat2.layouts.DesignBoxLayout">
+            <Property name="axis" type="int" value="3"/>
+          </Layout>
+        </Container>
+      </SubComponents>
+    </Container>
+  </SubComponents>
+</Form>
diff --git a/sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/VariationSelection.java b/sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/VariationSelection.java
new file mode 100644 (file)
index 0000000..939049a
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+/*
+ * VariationSelection.java
+ *
+ * Created on 01.07.2011, 17:15:52
+ */
+package com.jme3.gde.assetpack;
+
+import com.jme3.gde.assetpack.AssetPackLoader.SelectionEntry;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.Iterator;
+import java.util.List;
+import javax.swing.JComboBox;
+import javax.swing.JFrame;
+import org.w3c.dom.Element;
+
+/**
+ *
+ * @author normenhansen
+ */
+public class VariationSelection extends javax.swing.JDialog {
+
+    List<SelectionEntry> list;
+    private boolean canceled = false;
+
+    /** Creates new form VariationSelection */
+    public VariationSelection(List<SelectionEntry> list) {
+        super(new JFrame(), true);
+        this.list = list;
+        setLocationRelativeTo(null);
+        initComponents();
+        updateView();
+    }
+
+    private void updateView() {
+        jPanel1.removeAll();
+        for (Iterator<SelectionEntry> it = list.iterator(); it.hasNext();) {
+            final SelectionEntry selectionEntry = it.next();
+            final JComboBox box = new JComboBox();
+            for (Element element : selectionEntry.names) {
+                box.addItem(element.getAttribute("name"));
+            }
+            box.addActionListener(new ActionListener() {
+
+                public void actionPerformed(ActionEvent e) {
+                    selectionEntry.setSelected(box.getSelectedIndex());
+                }
+            });
+            jPanel1.add(box);
+        }
+        pack();
+    }
+
+    /** This method is called from within the constructor to
+     * initialize the form.
+     * WARNING: Do NOT modify this code. The content of this method is
+     * always regenerated by the Form Editor.
+     */
+    @SuppressWarnings("unchecked")
+    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
+    private void initComponents() {
+
+        jButton1 = new javax.swing.JButton();
+        jScrollPane1 = new javax.swing.JScrollPane();
+        jPanel1 = new javax.swing.JPanel();
+
+        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
+
+        jButton1.setText(org.openide.util.NbBundle.getMessage(VariationSelection.class, "VariationSelection.jButton1.text")); // NOI18N
+        jButton1.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                jButton1ActionPerformed(evt);
+            }
+        });
+
+        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.PAGE_AXIS));
+        jScrollPane1.setViewportView(jPanel1);
+
+        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
+        getContentPane().setLayout(layout);
+        layout.setHorizontalGroup(
+            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
+                .addContainerGap()
+                .addComponent(jButton1))
+            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
+        );
+        layout.setVerticalGroup(
+            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
+                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+                .addComponent(jButton1))
+        );
+
+        pack();
+    }// </editor-fold>//GEN-END:initComponents
+
+    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
+        setVisible(false);
+    }//GEN-LAST:event_jButton1ActionPerformed
+    // Variables declaration - do not modify//GEN-BEGIN:variables
+    private javax.swing.JButton jButton1;
+    private javax.swing.JPanel jPanel1;
+    private javax.swing.JScrollPane jScrollPane1;
+    // End of variables declaration//GEN-END:variables
+
+    public boolean isCanceled() {
+        return canceled;
+    }
+}
index 534c12d..dce4f4d 100644 (file)
@@ -4,24 +4,20 @@
  */
 package com.jme3.gde.assetpack.actions;
 
+import com.jme3.gde.assetpack.AssetConfiguration;
 import com.jme3.gde.assetpack.AssetPackLoader;
-import com.jme3.gde.assetpack.XmlHelper;
 import com.jme3.gde.core.assets.ProjectAssetManager;
-import com.jme3.gde.core.scene.SceneApplication;
 import com.jme3.scene.Spatial;
 import java.awt.event.ActionEvent;
 import java.beans.PropertyChangeListener;
-import java.io.IOException;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import javax.swing.Action;
 import org.openide.nodes.Node;
 import org.w3c.dom.Element;
 import com.jme3.gde.scenecomposer.SceneComposerTopComponent;
-import java.io.File;
-import org.openide.filesystems.FileObject;
-import org.openide.filesystems.FileUtil;
-import org.w3c.dom.NodeList;
+import java.util.ArrayList;
+import java.util.List;
 
 public final class AddAssetAction implements Action {
 
@@ -40,59 +36,18 @@ public final class AddAssetAction implements Action {
         Element assetElement = context.getLookup().lookup(Element.class);
         String type = assetElement.getAttribute("type");
         if ("model".equals(type) || "scene".equals(type)) {
-            addModelToScene(assetElement, pm);
-            AssetPackLoader.addModelFiles(assetElement, pm);
-        } else {
-            AssetPackLoader.addAllFiles(assetElement, pm);
-        }
-    }
-
-    private void addModelToScene(Element assetElement, ProjectAssetManager pm) {
-//        Element fileElement = XmlHelper.findChildElementWithAttribute(assetElement, "file", "main", "true");
-        Spatial model = AssetPackLoader.loadAssetPackModel(assetElement, pm);
-        if (model != null) {
-            SceneComposerTopComponent.findInstance().addModel(model);
-        } else {
-            Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Error loading model");
-            return;
-        }
-    }
-
-    private void copyModelData(NodeList fileNodeList, ProjectAssetManager pm) {
-        //TODO: not good :/
-        ProjectAssetManager currentProjectAssetManager = null;
-        try {
-            currentProjectAssetManager = SceneApplication.getApplication().getCurrentSceneRequest().getManager();
-            if (currentProjectAssetManager == null) {
-                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Could not get project asset manager!");
-                return;
-            }
-        } catch (Exception e) {
-            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Could not get project asset manager!");
-            return;
-        }
-        for (int i = 0; i < fileNodeList.getLength(); i++) {
-            Element fileElem = (Element) fileNodeList.item(i);
-            String type = fileElem.getAttribute("type");
-            if ("texture".equals(type) || "sound".equals(type) || "materialdef".equals(type) || "shader".equals(type) || "other".equals(type)) {
-                try {
-                    String src = pm.getAbsoluteAssetPath(fileElem.getAttribute("path"));
-                    if (src == null) {
-                        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Could not find texture with manager!");
-                        return;
-                    }
-                    FileObject srcFile = FileUtil.toFileObject(new File(src));
-                    String destName = currentProjectAssetManager.getAssetFolderName() + "/" + fileElem.getAttribute("path");
-                    String destFolder = destName.replace("\\", "/");
-                    destFolder = destFolder.substring(0, destFolder.lastIndexOf("/"));
-                    FileObject folder = FileUtil.createFolder(new File(destFolder));
-                    srcFile.copy(folder, srcFile.getName(), srcFile.getExt());
-                } catch (IOException ex) {
-                    Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Could not copy texture: {0}", ex.getMessage());
-                }
+            AssetConfiguration conf = new AssetConfiguration(assetElement);
+            Spatial model = AssetPackLoader.loadAssetPackModel(pm, conf);
+            if (model != null) {
+                SceneComposerTopComponent.findInstance().addModel(model);
+                AssetPackLoader.addModelFiles(pm, conf);
+            } else {
+                Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Error loading model");
             }
+        } else {
+            AssetConfiguration conf = new AssetConfiguration(assetElement);
+            AssetPackLoader.addAllFiles(pm, conf);
         }
-        return;
     }
 
     public Object getValue(String key) {
index 505c36c..ab48084 100644 (file)
@@ -4,6 +4,7 @@
  */
 package com.jme3.gde.assetpack.actions;
 
+import com.jme3.gde.assetpack.AssetConfiguration;
 import com.jme3.gde.assetpack.AssetPackLoader;
 import com.jme3.gde.core.assets.ProjectAssetManager;
 import com.jme3.gde.core.scene.SceneApplication;
@@ -36,7 +37,7 @@ public final class PreviewAssetAction implements Action {
         Element assetElement = context.getLookup().lookup(Element.class);
         com.jme3.scene.Node node = new com.jme3.scene.Node("PreviewRootNode");
         Spatial model = null;
-        model = AssetPackLoader.loadAssetPackModel(assetElement, pm);
+        model = AssetPackLoader.loadAssetPackModel(pm, new AssetConfiguration(assetElement));
         node.attachChild(model);
         JmeNode jmeNode = NodeUtility.createNode(node);
         SceneApplication app = SceneApplication.getApplication();
index 6984d82..d1031f5 100644 (file)
@@ -2,7 +2,9 @@ package com.jme3.gde.assetpack.project;
 
 import com.jme3.gde.assetpack.project.actions.PublishAssetPackAction;
 import com.jme3.gde.assetpack.browser.nodes.AssetPackBrowserFolder;
+import com.jme3.gde.assetpack.project.actions.CleanupProjectAction;
 import com.jme3.gde.assetpack.project.actions.ConvertOgreBinaryMeshesAction;
+import com.jme3.gde.assetpack.project.actions.ImportWorldForgeAction;
 import java.awt.Image;
 import java.util.LinkedList;
 import java.util.List;
@@ -57,14 +59,16 @@ class AssetPackProjectLogicalView implements LogicalViewProvider {
 
         @Override
         public Action[] getActions(boolean arg0) {
-            Action[] nodeActions = new Action[8];
+            Action[] nodeActions = new Action[9];
             nodeActions[0] = new PublishAssetPackAction(project);
             nodeActions[1] = new ConvertOgreBinaryMeshesAction(project);
-            nodeActions[2] = CommonProjectActions.copyProjectAction();
-            nodeActions[3] = CommonProjectActions.deleteProjectAction();
-            nodeActions[5] = CommonProjectActions.setAsMainProjectAction();
-            nodeActions[6] = CommonProjectActions.closeProjectAction();
-            nodeActions[7] = CommonProjectActions.customizeProjectAction();
+            nodeActions[2] = new ImportWorldForgeAction(project);
+            nodeActions[3] = new CleanupProjectAction(project);
+            nodeActions[4] = CommonProjectActions.copyProjectAction();
+            nodeActions[5] = CommonProjectActions.deleteProjectAction();
+            nodeActions[6] = CommonProjectActions.setAsMainProjectAction();
+            nodeActions[7] = CommonProjectActions.closeProjectAction();
+            nodeActions[8] = CommonProjectActions.customizeProjectAction();
             return nodeActions;
         }
 
diff --git a/sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/project/actions/CleanupProjectAction.java b/sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/project/actions/CleanupProjectAction.java
new file mode 100644 (file)
index 0000000..3733b9b
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package com.jme3.gde.assetpack.project.actions;
+
+import com.jme3.gde.assetpack.project.AssetPackProject;
+import java.awt.event.ActionEvent;
+import java.beans.PropertyChangeListener;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.swing.Action;
+import org.netbeans.api.progress.ProgressHandle;
+import org.netbeans.api.progress.ProgressHandleFactory;
+import org.openide.filesystems.FileObject;
+import org.openide.util.Exceptions;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+@SuppressWarnings("unchecked")
+public final class CleanupProjectAction implements Action {
+
+    private final AssetPackProject context;
+
+    public CleanupProjectAction(AssetPackProject context) {
+        this.context = context;
+    }
+
+    private void scanFiles(final FileObject folder, final ArrayList<String> paths) {
+        FileObject[] children = folder.getChildren();
+        for (FileObject fileObject : children) {
+            boolean stay = false;
+            if (fileObject.isFolder() && !fileObject.isVirtual()) {
+                scanFiles(fileObject, paths);
+                stay = true;
+            } else if (fileObject.isData()) {
+                if ("png".equalsIgnoreCase(fileObject.getExt())
+                        || "jpg".equalsIgnoreCase(fileObject.getExt())
+                        || "dds".equalsIgnoreCase(fileObject.getExt())
+                        || "bmp".equalsIgnoreCase(fileObject.getExt())) {
+                    for (String path : paths) {
+                        if (fileObject.getPath().endsWith(path)) {
+                            stay = true;
+                        }
+                    }
+                    if (!stay) {
+                        try {
+                            Logger.getLogger(CleanupProjectAction.class.getName()).log(Level.INFO, "Delete unused file {0}", fileObject);
+                            fileObject.delete();
+                        } catch (IOException ex) {
+                            Exceptions.printStackTrace(ex);
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    private ArrayList<String> scanProjectFiles(Element elem, ArrayList<String> elements) {
+        if (elem.getTagName().equals("file")) {
+//            Logger.getLogger(CleanupProjectAction.class.getName()).log(Level.INFO, "Found element {0}", elem.getAttribute("path"));
+            elements.add(elem.getAttribute("path"));
+        }
+        NodeList list = elem.getChildNodes();
+        for (int i = 0; i < list.getLength(); i++) {
+            if (list.item(i) instanceof Element) {
+                Element asset = (Element) list.item(i);
+                scanProjectFiles(asset, elements);
+            }
+        }
+        return elements;
+    }
+
+    public void actionPerformed(ActionEvent ev) {
+        final ArrayList<String> files = scanProjectFiles(context.getConfiguration().getDocumentElement(), new ArrayList<String>());
+        new Thread(new Runnable() {
+
+            public void run() {
+                ProgressHandle handle = ProgressHandleFactory.createHandle("Cleanup unused assets..");
+                handle.start();
+                scanFiles(context.getAssetsFolder(), files);
+                handle.finish();
+            }
+        }).start();
+
+    }
+
+    public Object getValue(String key) {
+        if (key.equals(NAME)) {
+            return "Clean unused images";
+        }
+        return null;
+    }
+
+    public void putValue(String key, Object value) {
+    }
+
+    public void setEnabled(boolean b) {
+    }
+
+    public boolean isEnabled() {
+        return true;
+    }
+
+    public void addPropertyChangeListener(PropertyChangeListener listener) {
+    }
+
+    public void removePropertyChangeListener(PropertyChangeListener listener) {
+    }
+}
diff --git a/sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/project/actions/ImportWorldForgeAction.java b/sdk/jme3-assetpack-support/src/com/jme3/gde/assetpack/project/actions/ImportWorldForgeAction.java
new file mode 100644 (file)
index 0000000..5b7f222
--- /dev/null
@@ -0,0 +1,578 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package com.jme3.gde.assetpack.project.actions;
+
+import com.jme3.asset.AssetEventListener;
+import com.jme3.asset.AssetKey;
+import com.jme3.asset.DesktopAssetManager;
+import com.jme3.gde.assetpack.AssetPackLoader;
+import com.jme3.gde.assetpack.XmlHelper;
+import com.jme3.gde.assetpack.project.AssetPackProject;
+import com.jme3.gde.assetpack.project.wizards.FileDescription;
+import com.jme3.gde.core.assets.ProjectAssetManager;
+import com.jme3.gde.ogretools.convert.OgreXMLConvert;
+import com.jme3.gde.ogretools.convert.OgreXMLConvertOptions;
+import com.jme3.material.MaterialList;
+import com.jme3.scene.plugins.ogre.OgreMeshKey;
+import com.jme3.scene.plugins.ogre.matext.MaterialExtension;
+import com.jme3.scene.plugins.ogre.matext.MaterialExtensionSet;
+import com.jme3.scene.plugins.ogre.matext.OgreMaterialKey;
+import java.awt.event.ActionEvent;
+import java.beans.PropertyChangeListener;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.swing.Action;
+import org.netbeans.api.progress.ProgressHandle;
+import org.netbeans.api.progress.ProgressHandleFactory;
+import org.openide.DialogDisplayer;
+import org.openide.NotifyDescriptor;
+import org.openide.NotifyDescriptor.Message;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.Exceptions;
+import org.openide.xml.XMLUtil;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.xml.sax.InputSource;
+
+public final class ImportWorldForgeAction implements Action {
+
+    private final AssetPackProject project;
+    private FileObject folder;
+    private ProjectAssetManager pm;
+    private DesktopAssetManager mgr;
+    private HashMap<String, ArrayList<String>> matRefs = new HashMap<String, ArrayList<String>>();
+    private List<String> modelNames = new ArrayList<String>();
+
+    public ImportWorldForgeAction(AssetPackProject context) {
+        this.project = context;
+        pm = project.getProjectAssetManager();
+        folder = pm.getAssetFolder();
+        mgr = new DesktopAssetManager(true);
+        mgr.registerLocator(folder.getPath(), "com.jme3.asset.plugins.FileLocator");
+    }
+
+    public void actionPerformed(ActionEvent ev) {
+        matRefs.clear();
+        modelNames.clear();
+        // TODO use context
+//        javax.swing.JFileChooser fr = new javax.swing.JFileChooser();
+//        javax.swing.filechooser.FileSystemView fw = fr.getFileSystemView();
+//        String projectDir = fw.getDefaultDirectory().getAbsolutePath();
+//        FileChooserBuilder builder = new FileChooserBuilder(projectDir);
+//        builder.setApproveText("Import");
+//        builder.setTitle("Please select WorldForge checkout folder");
+//        builder.setDirectoriesOnly(true);
+//        final File file = builder.showOpenDialog();
+//        if (file == null) {
+//            return;
+//        }
+        //FileUtil.toFileObject(file);
+        new Thread(new Runnable() {
+
+            public void run() {
+                ProgressHandle handle = ProgressHandleFactory.createHandle("Import WorldForge Models");
+                handle.start();
+                FileObject objects = folder.getFileObject("3d_objects");
+                if (objects == null) {
+                    showError("Cannot find worldforge content!\nPlease copy content of worldforge folder\ninto the assets folder of this project!");
+                    handle.finish();
+                    return;
+                }
+                List<String> binFiles = new ArrayList<String>();
+                convertOgreBinary(folder.getPath(), binFiles, handle);
+                deleteOgreBinary(binFiles);
+                handle.progress("Scanning folder");
+                scanFiles(folder);
+                //iterate through found models and add asset items
+                for (Iterator<String> it = modelNames.iterator(); it.hasNext();) {
+                    String modelName = it.next();
+                    handle.progress("Scanning " + modelName);
+                    //TODO: fill data
+                    Element elem = project.getConfiguration().createElement("asset");
+                    elem.setAttribute("name", getFileName(modelName));
+                    elem.setAttribute("type", "model");
+                    elem.setAttribute("format", "ogrexml");
+                    elem.setAttribute("categories", getCategory(modelName));
+                    elem.setAttribute("tags", getTags(modelName));
+                    Element description = project.getConfiguration().createElement("description");
+                    elem.appendChild(description);
+
+
+                    List<String> matNames = getModelMaterialNames(modelName);
+                    Element variationsElement = null;
+                    MaterialList keyMaterialList = new MaterialList();
+                    //assemble material variation assets and add variations
+                    for (String matName : matNames) {
+                        MaterialList materialList = null;
+                        Element partElement = null;
+                        ArrayList<String> matFiles = matRefs.get(matName);
+                        if (matFiles != null) {
+                            for (String matFile : matFiles) {
+                                Element variationElement = null;
+                                if (variationsElement == null) {
+                                    variationsElement = project.getConfiguration().createElement("materialvariations");
+                                    elem.appendChild(variationsElement);
+                                }
+                                if (partElement == null) {
+                                    partElement = project.getConfiguration().createElement("mesh");
+                                    partElement.setAttribute("name", matName);
+                                    variationsElement.appendChild(partElement);
+                                }
+                                List<AssetKey<?>> list = new ArrayList<AssetKey<?>>();
+                                materialList = getMaterialAssetList(matFile, list);
+                                for (Iterator<AssetKey<?>> it1 = list.iterator(); it1.hasNext();) {
+                                    AssetKey<? extends Object> assetKey = it1.next();
+                                    if (variationElement == null) {
+                                        variationElement = project.getConfiguration().createElement("variation");
+                                        variationElement.setAttribute("name", getFolderName(matFile));
+                                        partElement.appendChild(variationElement);
+                                    }
+                                    FileDescription desc = AssetPackLoader.getFileDescription(getAbsoluteAssetPath(assetKey.getName()));
+                                    if (desc != null) {
+                                        Element file = project.getConfiguration().createElement("file");
+                                        file.setAttribute("path", assetKey.getName());
+                                        file.setAttribute("type", desc.getType());
+                                        variationElement.appendChild(file);
+                                    }
+                                }
+                            }
+                        }
+                        if (materialList != null) {
+                            keyMaterialList.putAll(materialList);
+                        }
+                    }
+                    //assemble main assets and add to file
+                    if (keyMaterialList != null) {
+                        OgreMeshKey meshKey = new OgreMeshKey(modelName, keyMaterialList);
+                        List<AssetKey<?>> list = new ArrayList<AssetKey<?>>();
+                        if (getModelAssetList(meshKey, list)) {
+                            for (AssetKey<?> assetKey : list) {
+                                Element file = project.getConfiguration().createElement("file");
+                                if (assetKey.getName().endsWith(".mesh.xml")) {
+                                    file.setAttribute("main", "true");
+                                }
+                                file.setAttribute("path", assetKey.getName());
+                                FileDescription descr = AssetPackLoader.getFileDescription(getAbsoluteAssetPath(assetKey.getName()));
+                                file.setAttribute("type", descr.getType());
+                                elem.appendChild(file);
+                            }
+                            project.getProjectAssets().appendChild(elem);
+                            project.saveSettings();
+                            project.getAssetPackFolder().refresh();
+                        }
+                    }
+                }
+                handle.finish();
+            }
+        }).start();
+    }
+
+    private void scanFiles(FileObject folder) {
+        FileObject[] files = folder.getChildren();
+        for (FileObject fileObject : files) {
+            if (fileObject.isFolder() && !fileObject.isVirtual()) {
+                scanFiles(fileObject);
+            } else if (fileObject.getPath().endsWith(".mesh.xml")) {
+//                replaceMeshMatName(fileObject);
+                //TODO: workaround
+                if (!fileObject.getName().equals("campfire.mesh")) {
+                    modelNames.add(getRelativeAssetPath(fileObject.getPath()));
+                }
+            } else if ("material".equals(fileObject.getExt())) {
+//                replaceMaterialMatName(fileObject);
+                String name = getMaterialName(fileObject);
+                if (name != null) {
+                    addMaterialRef(name, getRelativeAssetPath(fileObject.getPath()));
+                }
+            }
+        }
+    }
+
+    private void replaceMaterialMatName(FileObject file) {
+        try {
+            List<String> lines = file.asLines();
+            boolean changed = false;
+            for (int i = 0; i < lines.size(); i++) {
+                String line = lines.get(i);
+                if (line.startsWith("material") && line.contains(":")) {
+                    int idx = line.indexOf(":");
+                    String matName = line.substring(9, idx).trim();
+                    String newName = removePastUnderScore(matName);
+                    if (!matName.equals(newName)) {
+                        Logger.getLogger(ImportWorldForgeAction.class.getName()).log(Level.INFO, "Change material name for {0}", file);
+                        lines.set(i, line.replace(matName, newName));
+                        changed = true;
+                    }
+                }
+            }
+            if (changed) {
+                OutputStreamWriter out = new OutputStreamWriter(file.getOutputStream());
+                for (String string : lines) {
+                    out.write(string + "\n");
+                }
+                out.close();
+            }
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+    }
+
+    private void replaceMeshMatName(FileObject file) {
+        InputStream stream = null;
+        try {
+            stream = file.getInputStream();
+            Document doc = XMLUtil.parse(new InputSource(stream), false, false, null, null);
+            stream.close();
+            Element elem = doc.getDocumentElement();
+            if (elem == null) {
+                throw new IllegalStateException("Cannot find root mesh element");
+            }
+            Element submeshes = XmlHelper.findChildElement(elem, "submeshes");
+            if (submeshes == null) {
+                throw new IllegalStateException("Cannot find submeshes element");
+            }
+            Element submesh = XmlHelper.findChildElement(submeshes, "submesh");
+            boolean changed = false;
+            while (submesh != null) {
+                String matName = submesh.getAttribute("material");
+                String newName = removePastUnderScore(matName);
+                if (!matName.equals(newName)) {
+                    Logger.getLogger(ImportWorldForgeAction.class.getName()).log(Level.INFO, "Change material name for {0}", file);
+                    submesh.setAttribute("material", newName);
+                    submesh = XmlHelper.findNextSiblingElement(submesh);
+                    changed = true;
+                }
+            }
+            if (changed) {
+                OutputStream out = file.getOutputStream();
+                XMLUtil.write(doc, out, "UTF-8");
+                out.close();
+            }
+
+        } catch (Exception ex) {
+            Exceptions.printStackTrace(ex);
+        } finally {
+        }
+    }
+
+    private String removePastUnderScore(String name) {
+        int idx = name.lastIndexOf("/");
+        if (idx == -1) {
+            return name;
+        }
+        int idx2 = name.indexOf("_", idx);
+        if (idx2 == -1) {
+            return name;
+        } else {
+            return name.substring(0, idx2);
+        }
+    }
+
+    private void addMaterialRef(String matName, String assetName) {
+        ArrayList<String> list = matRefs.get(matName);
+        if (list == null) {
+            list = new ArrayList<String>();
+            matRefs.put(matName, list);
+        }
+        list.add(assetName);
+    }
+
+    private void convertOgreBinary(String dir2scan, List<String> deleteFiles, ProgressHandle handle) {
+        try {
+            File zipDir = new File(dir2scan);
+            String[] dirList = zipDir.list();
+            for (int i = 0; i < dirList.length; i++) {
+                File f = new File(zipDir, dirList[i]);
+                if (f.isDirectory()) {
+                    String filePath = f.getPath();
+                    convertOgreBinary(filePath, deleteFiles, handle);
+                    continue;
+                }
+                FileObject fobj = FileUtil.toFileObject(f);
+                if (fobj.getExt().equalsIgnoreCase("mesh") || fobj.getExt().equalsIgnoreCase("skeleton")) {
+                    OgreXMLConvertOptions options = new OgreXMLConvertOptions(fobj.getPath());
+                    options.setBinaryFile(true);
+                    OgreXMLConvert conv = new OgreXMLConvert();
+                    conv.doConvert(options, handle);
+                    deleteFiles.add(fobj.getPath());
+                }
+            }
+        } catch (Exception e) {
+            Logger.getLogger(ConvertOgreBinaryMeshesAction.class.getName()).log(Level.SEVERE, "Error scanning directory", e);
+        } finally {
+//            handle.finish();
+        }
+    }
+
+    private void deleteOgreBinary(List<String> createdFiles) {
+        for (String string : createdFiles) {
+            new File(string).delete();
+        }
+    }
+
+    private OgreMaterialKey getOgreMaterialKey(String materialName) {
+        /**
+         * /base/normalmap/specular
+         * /base/normalmap
+         * /base/simple
+         */
+        MaterialExtensionSet matExts = new MaterialExtensionSet();
+        MaterialExtension baseLightExt = new MaterialExtension("/base/normalmap/specular",
+                "Common/MatDefs/Light/Lighting.j3md");
+        baseLightExt.setTextureMapping("DiffuseMap", "DiffuseMap");
+        baseLightExt.setTextureMapping("NormalHeightMap", "NormalMap");
+        baseLightExt.setTextureMapping("SpecularMap", "SpecularMap");
+        matExts.addMaterialExtension(baseLightExt);
+
+        MaterialExtension baseLightExt2 = new MaterialExtension("/base/normalmap",
+                "Common/MatDefs/Light/Lighting.j3md");
+        baseLightExt2.setTextureMapping("DiffuseMap", "DiffuseMap");
+        baseLightExt2.setTextureMapping("NormalHeightMap", "NormalMap");
+        baseLightExt2.setTextureMapping("SpecularMap", "SpecularMap");
+        matExts.addMaterialExtension(baseLightExt2);
+
+        MaterialExtension baseLightExt3 = new MaterialExtension("/base/simple",
+                "Common/MatDefs/Light/Lighting.j3md");
+        baseLightExt3.setTextureMapping("DiffuseMap", "DiffuseMap");
+        baseLightExt3.setTextureMapping("NormalHeightMap", "NormalMap");
+        baseLightExt3.setTextureMapping("SpecularMap", "SpecularMap");
+        matExts.addMaterialExtension(baseLightExt3);
+
+        OgreMaterialKey key = new OgreMaterialKey(materialName);
+        key.setMaterialExtensionSet(matExts);
+        return key;
+    }
+
+    private MaterialList getMaterialAssetList(String key, final List<AssetKey<?>> assetKeys) {
+        final AtomicBoolean good = new AtomicBoolean(true);
+        mgr.clearCache();
+        mgr.setAssetEventListener(new AssetEventListener() {
+
+            public void assetLoaded(AssetKey ak) {
+            }
+
+            public void assetRequested(AssetKey ak) {
+                if (!"j3md".equalsIgnoreCase(ak.getExtension())
+                        && !"glsllib".equalsIgnoreCase(ak.getExtension())
+                        && !"frag".equalsIgnoreCase(ak.getExtension())
+                        && !"vert".equalsIgnoreCase(ak.getExtension())
+                        && !"vert".equalsIgnoreCase(ak.getExtension())) {
+                    assetKeys.add(ak);
+                }
+                if (ak.getName().equals("Common/Materials/RedColor.j3m")) {
+                    good.set(false);
+                }
+            }
+        });
+        try {
+            return mgr.loadAsset(getOgreMaterialKey(key));
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    private boolean getModelAssetList(OgreMeshKey key, final List<AssetKey<?>> assetKeys) {
+        final AtomicBoolean good = new AtomicBoolean(true);
+        mgr.clearCache();
+        mgr.setAssetEventListener(new AssetEventListener() {
+
+            public void assetLoaded(AssetKey ak) {
+            }
+
+            public void assetRequested(AssetKey ak) {
+                if (ak instanceof OgreMaterialKey) {
+                    MaterialExtensionSet matExts = new MaterialExtensionSet();
+                    MaterialExtension baseLightExt = new MaterialExtension("/base/normalmap/specular",
+                            "Common/MatDefs/Light/Lighting.j3md");
+                    baseLightExt.setTextureMapping("DiffuseMap", "DiffuseMap");
+                    baseLightExt.setTextureMapping("NormalHeightMap", "NormalMap");
+                    baseLightExt.setTextureMapping("SpecularMap", "SpecularMap");
+                    matExts.addMaterialExtension(baseLightExt);
+
+                    MaterialExtension baseLightExt2 = new MaterialExtension("/base/normalmap",
+                            "Common/MatDefs/Light/Lighting.j3md");
+                    baseLightExt2.setTextureMapping("DiffuseMap", "DiffuseMap");
+                    baseLightExt2.setTextureMapping("NormalHeightMap", "NormalMap");
+                    baseLightExt2.setTextureMapping("SpecularMap", "SpecularMap");
+                    matExts.addMaterialExtension(baseLightExt2);
+
+                    MaterialExtension baseLightExt3 = new MaterialExtension("/base/simple",
+                            "Common/MatDefs/Light/Lighting.j3md");
+                    baseLightExt3.setTextureMapping("DiffuseMap", "DiffuseMap");
+                    baseLightExt3.setTextureMapping("NormalHeightMap", "NormalMap");
+                    baseLightExt3.setTextureMapping("SpecularMap", "SpecularMap");
+                    matExts.addMaterialExtension(baseLightExt3);
+
+                    ((OgreMaterialKey) ak).setMaterialExtensionSet(matExts);
+
+                }
+                if (!"j3md".equalsIgnoreCase(ak.getExtension())
+                        && !"glsllib".equalsIgnoreCase(ak.getExtension())
+                        && !"frag".equalsIgnoreCase(ak.getExtension())
+                        && !"vert".equalsIgnoreCase(ak.getExtension())
+                        && !"vert".equalsIgnoreCase(ak.getExtension())) {
+                    assetKeys.add(ak);
+                }
+                if (ak.getName().equals("Common/Materials/RedColor.j3m")) {
+                    good.set(false);
+                }
+            }
+        });
+        try {
+            mgr.loadModel(key);
+        } catch (Exception e) {
+            return false;
+        }
+        if (!good.get()) {
+            return false;
+        }
+        return true;
+    }
+
+    private List<String> getModelMaterialNames(String assetName) {
+        List<String> materialNames = new ArrayList<String>();
+        //TODO: check use of File
+        FileObject file = FileUtil.toFileObject(new File(getAbsoluteAssetPath(assetName).replaceAll("/", File.separator)));//Repository.getDefault().findResource(getAbsoluteAssetPath(assetName));
+        InputStream stream = null;
+        try {
+            stream = file.getInputStream();
+            Document doc = XMLUtil.parse(new InputSource(stream), false, false, null, null);
+            Element elem = doc.getDocumentElement();
+            if (elem == null) {
+                throw new IllegalStateException("Cannot find root mesh element");
+            }
+            Element submeshes = XmlHelper.findChildElement(elem, "submeshes");
+            if (submeshes == null) {
+                throw new IllegalStateException("Cannot find submeshes element");
+            }
+            Element submesh = XmlHelper.findChildElement(submeshes, "submesh");
+            while (submesh != null) {
+                String matName = submesh.getAttribute("material");
+                if (!materialNames.contains(matName)) {
+                    materialNames.add(matName);
+                }
+                submesh = XmlHelper.findNextSiblingElement(submesh);
+            }
+        } catch (Exception ex) {
+            Exceptions.printStackTrace(ex);
+        } finally {
+            if (stream != null) {
+                try {
+                    stream.close();
+                } catch (IOException ex) {
+                    Exceptions.printStackTrace(ex);
+                }
+            }
+        }
+        return materialNames;
+    }
+
+    private String getMaterialName(FileObject file) {
+        try {
+            System.out.println("MaterialScan " + file);
+            List<String> lines = file.asLines();
+            for (String line : lines) {
+                if (line.startsWith("material") && line.contains(":")) {
+                    int idx = line.indexOf(":");
+                    return line.substring(9, idx).trim();
+                }
+            }
+
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return null;
+    }
+
+    private String getRelativeAssetPath(String absolutePath) {
+        String prefix = folder.getPath();
+        int idx = absolutePath.indexOf(prefix);
+        if (idx == 0) {
+            return absolutePath.substring(prefix.length() + 1).replace("./", "");
+        }
+        return null;
+    }
+
+    private String getAbsoluteAssetPath(String relatvePath) {
+        String prefix = folder.getPath();
+        return (prefix + "/" + relatvePath).replace("./", "");
+    }
+
+    private String getTags(String modelName) {
+        String[] strings = modelName.split("/");
+        String ret = null;
+        for (String string : strings) {
+            if (!"models".equals(string) && !"3d_objects".equals(string) && !string.contains(".mesh.xml")) {
+                if (ret == null) {
+                    ret = string;
+                } else {
+                    ret = ret + ", " + string;
+                }
+            }
+        }
+        return ret;
+    }
+
+    private String getCategory(String modelName) {
+        if (!modelName.startsWith("3d_objects/")) {
+            return "";
+        }
+        int idx = modelName.indexOf("/", 11);
+        if (idx == -1) {
+            return "";
+        }
+        return modelName.substring(11, idx);
+    }
+
+    private String getFolderName(String name) {
+        int start = name.substring(0, name.lastIndexOf("/")).lastIndexOf("/") + 1;
+        return name.substring(start, name.lastIndexOf("/"));
+    }
+
+    private String getFileName(String name) {
+        return name.substring(name.lastIndexOf("/") + 1, name.indexOf("."));
+    }
+
+    private void showError(String e) {
+        Message msg = new NotifyDescriptor.Message(
+                e,
+                NotifyDescriptor.ERROR_MESSAGE);
+        DialogDisplayer.getDefault().notifyLater(msg);
+    }
+
+    public Object getValue(String key) {
+        if (key.equals(NAME)) {
+            return "Scan WorldForge..";
+        }
+        return null;
+    }
+
+    public void putValue(String key, Object value) {
+    }
+
+    public void setEnabled(boolean b) {
+    }
+
+    public boolean isEnabled() {
+        return true;
+    }
+
+    public void addPropertyChangeListener(PropertyChangeListener listener) {
+    }
+
+    public void removePropertyChangeListener(PropertyChangeListener listener) {
+    }
+}
index 30998ee..d6de129 100644 (file)
@@ -3,7 +3,7 @@
 <filesystem>
     <folder name="Actions">
         <folder name="CodelessProjects">
-            <file name="com-jme3-gde-core-codeless-wizard-CodelessProjectWizardAction.instance">
+            <file name="com-jme3-gde-core-codeless-CodelessProjectWizardAction.instance">
                 <attr name="delegate" methodvalue="org.openide.awt.Actions.inject"/>
                 <attr name="displayName" bundlevalue="com.jme3.gde.core.codeless.Bundle#CTL_Codeless_Project"/>
                 <attr name="iconBase" stringvalue="com/jme3/gde/core/assets/actions/jme-logo.png"/>
@@ -66,7 +66,7 @@
         <folder name="File">
             <folder name="Import">
                 <file name="CodelessProject.shadow">
-                    <attr name="originalFile" stringvalue="Actions/CodelessProjects/com-jme3-gde-core-codeless-wizard-CodelessProjectWizardAction.instance"/>
+                    <attr name="originalFile" stringvalue="Actions/CodelessProjects/com-jme3-gde-core-codeless-CodelessProjectWizardAction.instance"/>
                     <attr name="position" intvalue="0"/>
                 </file>
             </folder>
index d653f5c..f8fd307 100644 (file)
@@ -180,7 +180,7 @@ public class JmeSpatialChildren extends Children.Keys<Object> {
         } else if (key instanceof MeshGeometryPair) {
             MeshGeometryPair pair = (MeshGeometryPair) key;
             return new Node[]{new JmeMesh(pair.getGeometry(), pair.getMesh()).setReadOnly(readOnly)};
-        } else if (key instanceof Control) {
+        } else if (key instanceof Control && dataObject != null) {
             return new Node[]{new JmeGenericControl((Control) key, dataObject)};
         }
         return new Node[]{Node.EMPTY};
index b164e50..71cdea3 100644 (file)
@@ -43,6 +43,11 @@ public class DynamicLookup extends AbstractLookup{
     private static final long serialVersionUID = 1212314412L;
     private InstanceContent instanceContent;
 
+    public DynamicLookup() {
+        this.instanceContent = new InstanceContent();
+        instanceContent.add(this);
+    }
+
     public DynamicLookup(InstanceContent instanceContent) {
         super(instanceContent);
         this.instanceContent = instanceContent;
index 2bd451c..74b07e9 100644 (file)
@@ -28,7 +28,7 @@ public class OgreBinaryMeshDataObject extends SpatialAssetDataObject {
     @Override
     public Spatial loadAsset() {
         ProgressHandle handle = ProgressHandleFactory.createHandle("Converting OgreBinary");
-        handle.start(4);
+        handle.start();
         //mesh
         OgreXMLConvertOptions options = new OgreXMLConvertOptions(getPrimaryFile().getPath());
         options.setBinaryFile(true);
@@ -41,7 +41,7 @@ public class OgreBinaryMeshDataObject extends SpatialAssetDataObject {
             OgreXMLConvert conv2 = new OgreXMLConvert();
             conv2.doConvert(options2, handle);
         }
-        handle.progress(3);
+        handle.progress("Convert Model");
         ProjectAssetManager mgr = getLookup().lookup(ProjectAssetManager.class);
         if (mgr == null) {
             return null;
index 27106f4..41aa2a9 100644 (file)
@@ -51,7 +51,7 @@ public final class AdvOgreXMLConvertAction implements ActionListener {
 
             public void run() {
                 ProgressHandle progressHandle = ProgressHandleFactory.createHandle("Converting OgreXML");
-                progressHandle.start(4);
+                progressHandle.start();
 
                 OgreXMLConvert converter = new OgreXMLConvert();
                 if (!converter.doConvert(options, progressHandle)) {
@@ -82,7 +82,7 @@ public final class AdvOgreXMLConvertAction implements ActionListener {
 //                FileLock lock = null;
                 try {
 //                    lock = file.lock();
-                    progressHandle.progress("Creating j3o file", 3);
+                    progressHandle.progress("Creating j3o file");
                     String outputPath = file.getParent().getPath() + "/" + context.getPrimaryFile().getName() + ".j3o";
                     manager.clearCache();
                     Spatial model = manager.loadModel(manager.getRelativeAssetPath(file.getPath()));
index 6196270..dd2aed7 100644 (file)
@@ -37,7 +37,7 @@ public class OgreXMLConvert {
         String[] cmdOptions = getCommandString(options);
         Process proc = null;
         if (!options.isBinaryFile()) {
-            handle.progress("Optimizing Mesh / Creating LOD meshes", 1);
+            handle.progress("Optimizing Mesh / Creating LOD meshes");
             //convert to binary + modify
             try {
                 proc = Runtime.getRuntime().exec(cmdOptions);
@@ -61,7 +61,7 @@ public class OgreXMLConvert {
                 return false;
             }
         }
-        handle.progress("Converting Binary Mesh", 2);
+        handle.progress("Converting Binary Mesh");
         //convert back to xml
         cmdOptions = getBackCommandString(options);
         try {