Browse Source

Implement multi-file template wizard for code-based material definitions

Co-authored-by: neph1 <[email protected]>
copilot-swe-agent[bot] 4 weeks ago
parent
commit
8d5b127191

+ 0 - 2
jme3-materialeditor/src/com/jme3/gde/materialdefinition/package-info.java

@@ -31,8 +31,6 @@
  */
  */
 @TemplateRegistrations({
 @TemplateRegistrations({
     @TemplateRegistration(folder = "Material", content = "MatDef.j3md", displayName="Material Definition Template (Shader Nodes)"),
     @TemplateRegistration(folder = "Material", content = "MatDef.j3md", displayName="Material Definition Template (Shader Nodes)"),
-    @TemplateRegistration(folder = "Material", content = "CodeBasedMatDef.j3md", displayName="Material Definition Template (Code Based)", 
-                         position = 100, scriptEngine = "freemarker"),
     @TemplateRegistration(folder = "GLSL", content = "BasicShader.vert", displayName="Vertex Shader Template", 
     @TemplateRegistration(folder = "GLSL", content = "BasicShader.vert", displayName="Vertex Shader Template", 
                          position = 200, scriptEngine = "freemarker"),
                          position = 200, scriptEngine = "freemarker"),
     @TemplateRegistration(folder = "GLSL", content = "BasicShader.frag", displayName="Fragment Shader Template", 
     @TemplateRegistration(folder = "GLSL", content = "BasicShader.frag", displayName="Fragment Shader Template", 

+ 199 - 0
jme3-materialeditor/src/com/jme3/gde/materialdefinition/wizard/CodeBasedMatDefWizardIterator.java

@@ -0,0 +1,199 @@
+/*
+ * Copyright (c) 2009-2010 jMonkeyEngine
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
+ *   may be used to endorse or promote products derived from this software
+ *   without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.jme3.gde.materialdefinition.wizard;
+
+import java.awt.Component;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Set;
+import javax.swing.JComponent;
+import javax.swing.event.ChangeListener;
+import org.netbeans.api.project.Project;
+import org.netbeans.api.project.ProjectUtils;
+import org.netbeans.api.project.SourceGroup;
+import org.netbeans.api.project.Sources;
+import org.netbeans.api.templates.TemplateRegistration;
+import org.netbeans.spi.project.ui.templates.support.Templates;
+import org.openide.WizardDescriptor;
+import org.openide.filesystems.FileObject;
+import org.openide.loaders.DataFolder;
+import org.openide.loaders.DataObject;
+import org.openide.util.NbBundle.Messages;
+
+@TemplateRegistration(
+    folder = "Material",
+    content = "../CodeBasedMatDef.j3md",
+    displayName = "Material Definition Template (Code Based with Shaders)",
+    description = "Creates a material definition file with corresponding vertex and fragment shader files",
+    position = 100,
+    scriptEngine = "freemarker"
+)
+@Messages("CodeBasedMatDefWizardIterator_displayName=Code Based Material Definition with Shaders")
+public final class CodeBasedMatDefWizardIterator implements WizardDescriptor.InstantiatingIterator<WizardDescriptor> {
+
+    private int index;
+    private WizardDescriptor wizard;
+    private List<WizardDescriptor.Panel<WizardDescriptor>> panels;
+
+    @Override
+    public Set<FileObject> instantiate() throws IOException {
+        // Get the target name and directory
+        String targetName = Templates.getTargetName(wizard);
+        FileObject dir = Templates.getTargetFolder(wizard);
+        DataFolder df = DataFolder.findFolder(dir);
+        
+        // Prepare template variables
+        Map<String, Object> args = new HashMap<>();
+        args.put("name", targetName);
+        
+        // Get template files
+        FileObject j3mdTemplate = Templates.getTemplate(wizard);
+        FileObject templateDir = j3mdTemplate.getParent();
+        FileObject vertTemplate = templateDir.getFileObject("BasicShader.vert");
+        FileObject fragTemplate = templateDir.getFileObject("BasicShader.frag");
+        
+        // Create DataObjects for templates
+        DataObject j3mdTemplateData = DataObject.find(j3mdTemplate);
+        DataObject vertTemplateData = DataObject.find(vertTemplate);
+        DataObject fragTemplateData = DataObject.find(fragTemplate);
+        
+        // Create the files
+        DataObject j3mdFile = j3mdTemplateData.createFromTemplate(df, targetName, args);
+        vertTemplateData.createFromTemplate(df, targetName + ".vert", args);
+        fragTemplateData.createFromTemplate(df, targetName + ".frag", args);
+        
+        // Return the main j3md file as the primary created file
+        return Collections.singleton(j3mdFile.getPrimaryFile());
+    }
+
+    @Override
+    public void initialize(WizardDescriptor wizard) {
+        this.wizard = wizard;
+    }
+
+    @Override
+    public void uninitialize(WizardDescriptor wizard) {
+        panels = null;
+    }
+
+    @Override
+    public WizardDescriptor.Panel<WizardDescriptor> current() {
+        return getPanels().get(index);
+    }
+
+    @Override
+    public String name() {
+        return index + 1 + ". from " + getPanels().size();
+    }
+
+    @Override
+    public boolean hasNext() {
+        return index < getPanels().size() - 1;
+    }
+
+    @Override
+    public boolean hasPrevious() {
+        return index > 0;
+    }
+
+    @Override
+    public void nextPanel() {
+        if (!hasNext()) {
+            throw new NoSuchElementException();
+        }
+        index++;
+    }
+
+    @Override
+    public void previousPanel() {
+        if (!hasPrevious()) {
+            throw new NoSuchElementException();
+        }
+        index--;
+    }
+
+    @Override
+    public void addChangeListener(ChangeListener l) {
+    }
+
+    @Override
+    public void removeChangeListener(ChangeListener l) {
+    }
+
+    private List<WizardDescriptor.Panel<WizardDescriptor>> getPanels() {
+        if (panels == null) {
+            panels = new ArrayList<>();
+            // Use the standard target chooser
+            Project p = Templates.getProject(wizard);
+            SourceGroup[] groups = ProjectUtils.getSources(p).getSourceGroups(Sources.TYPE_GENERIC);
+            WizardDescriptor.Panel<WizardDescriptor> targetChooser = Templates.buildSimpleTargetChooser(p, groups).create();
+            panels.add(targetChooser);
+            
+            String[] steps = createSteps();
+            for (int i = 0; i < panels.size(); i++) {
+                Component c = panels.get(i).getComponent();
+                if (steps[i] == null) {
+                    // Default step name to component name of panel.
+                    steps[i] = c.getName();
+                }
+                if (c instanceof JComponent) { // assume Swing components
+                    JComponent jc = (JComponent) c;
+                    jc.putClientProperty(WizardDescriptor.PROP_CONTENT_SELECTED_INDEX, i);
+                    jc.putClientProperty(WizardDescriptor.PROP_CONTENT_DATA, steps);
+                    jc.putClientProperty(WizardDescriptor.PROP_AUTO_WIZARD_STYLE, true);
+                    jc.putClientProperty(WizardDescriptor.PROP_CONTENT_DISPLAYED, true);
+                    jc.putClientProperty(WizardDescriptor.PROP_CONTENT_NUMBERED, true);
+                }
+            }
+        }
+        return panels;
+    }
+
+    private String[] createSteps() {
+        String[] beforeSteps = (String[]) wizard.getProperty(WizardDescriptor.PROP_CONTENT_DATA);
+        assert beforeSteps != null : "This wizard may only be used embedded in the template wizard";
+        String[] res = new String[(beforeSteps.length - 1) + panels.size()];
+        for (int i = 0; i < res.length; i++) {
+            if (i < (beforeSteps.length - 1)) {
+                res[i] = beforeSteps[i];
+            } else {
+                res[i] = panels.get(i - beforeSteps.length + 1).getComponent().getName();
+            }
+        }
+        return res;
+    }
+}