Skip to content

Commit 8d5b127

Browse files
Copilotneph1
andcommitted
Implement multi-file template wizard for code-based material definitions
Co-authored-by: neph1 <7988802+neph1@users.noreply.github.com>
1 parent 78d2ed7 commit 8d5b127

2 files changed

Lines changed: 199 additions & 2 deletions

File tree

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
*/
3232
@TemplateRegistrations({
3333
@TemplateRegistration(folder = "Material", content = "MatDef.j3md", displayName="Material Definition Template (Shader Nodes)"),
34-
@TemplateRegistration(folder = "Material", content = "CodeBasedMatDef.j3md", displayName="Material Definition Template (Code Based)",
35-
position = 100, scriptEngine = "freemarker"),
3634
@TemplateRegistration(folder = "GLSL", content = "BasicShader.vert", displayName="Vertex Shader Template",
3735
position = 200, scriptEngine = "freemarker"),
3836
@TemplateRegistration(folder = "GLSL", content = "BasicShader.frag", displayName="Fragment Shader Template",
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/*
2+
* Copyright (c) 2009-2010 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
package com.jme3.gde.materialdefinition.wizard;
33+
34+
import java.awt.Component;
35+
import java.io.IOException;
36+
import java.util.ArrayList;
37+
import java.util.Collections;
38+
import java.util.HashMap;
39+
import java.util.List;
40+
import java.util.Map;
41+
import java.util.NoSuchElementException;
42+
import java.util.Set;
43+
import javax.swing.JComponent;
44+
import javax.swing.event.ChangeListener;
45+
import org.netbeans.api.project.Project;
46+
import org.netbeans.api.project.ProjectUtils;
47+
import org.netbeans.api.project.SourceGroup;
48+
import org.netbeans.api.project.Sources;
49+
import org.netbeans.api.templates.TemplateRegistration;
50+
import org.netbeans.spi.project.ui.templates.support.Templates;
51+
import org.openide.WizardDescriptor;
52+
import org.openide.filesystems.FileObject;
53+
import org.openide.loaders.DataFolder;
54+
import org.openide.loaders.DataObject;
55+
import org.openide.util.NbBundle.Messages;
56+
57+
@TemplateRegistration(
58+
folder = "Material",
59+
content = "../CodeBasedMatDef.j3md",
60+
displayName = "Material Definition Template (Code Based with Shaders)",
61+
description = "Creates a material definition file with corresponding vertex and fragment shader files",
62+
position = 100,
63+
scriptEngine = "freemarker"
64+
)
65+
@Messages("CodeBasedMatDefWizardIterator_displayName=Code Based Material Definition with Shaders")
66+
public final class CodeBasedMatDefWizardIterator implements WizardDescriptor.InstantiatingIterator<WizardDescriptor> {
67+
68+
private int index;
69+
private WizardDescriptor wizard;
70+
private List<WizardDescriptor.Panel<WizardDescriptor>> panels;
71+
72+
@Override
73+
public Set<FileObject> instantiate() throws IOException {
74+
// Get the target name and directory
75+
String targetName = Templates.getTargetName(wizard);
76+
FileObject dir = Templates.getTargetFolder(wizard);
77+
DataFolder df = DataFolder.findFolder(dir);
78+
79+
// Prepare template variables
80+
Map<String, Object> args = new HashMap<>();
81+
args.put("name", targetName);
82+
83+
// Get template files
84+
FileObject j3mdTemplate = Templates.getTemplate(wizard);
85+
FileObject templateDir = j3mdTemplate.getParent();
86+
FileObject vertTemplate = templateDir.getFileObject("BasicShader.vert");
87+
FileObject fragTemplate = templateDir.getFileObject("BasicShader.frag");
88+
89+
// Create DataObjects for templates
90+
DataObject j3mdTemplateData = DataObject.find(j3mdTemplate);
91+
DataObject vertTemplateData = DataObject.find(vertTemplate);
92+
DataObject fragTemplateData = DataObject.find(fragTemplate);
93+
94+
// Create the files
95+
DataObject j3mdFile = j3mdTemplateData.createFromTemplate(df, targetName, args);
96+
vertTemplateData.createFromTemplate(df, targetName + ".vert", args);
97+
fragTemplateData.createFromTemplate(df, targetName + ".frag", args);
98+
99+
// Return the main j3md file as the primary created file
100+
return Collections.singleton(j3mdFile.getPrimaryFile());
101+
}
102+
103+
@Override
104+
public void initialize(WizardDescriptor wizard) {
105+
this.wizard = wizard;
106+
}
107+
108+
@Override
109+
public void uninitialize(WizardDescriptor wizard) {
110+
panels = null;
111+
}
112+
113+
@Override
114+
public WizardDescriptor.Panel<WizardDescriptor> current() {
115+
return getPanels().get(index);
116+
}
117+
118+
@Override
119+
public String name() {
120+
return index + 1 + ". from " + getPanels().size();
121+
}
122+
123+
@Override
124+
public boolean hasNext() {
125+
return index < getPanels().size() - 1;
126+
}
127+
128+
@Override
129+
public boolean hasPrevious() {
130+
return index > 0;
131+
}
132+
133+
@Override
134+
public void nextPanel() {
135+
if (!hasNext()) {
136+
throw new NoSuchElementException();
137+
}
138+
index++;
139+
}
140+
141+
@Override
142+
public void previousPanel() {
143+
if (!hasPrevious()) {
144+
throw new NoSuchElementException();
145+
}
146+
index--;
147+
}
148+
149+
@Override
150+
public void addChangeListener(ChangeListener l) {
151+
}
152+
153+
@Override
154+
public void removeChangeListener(ChangeListener l) {
155+
}
156+
157+
private List<WizardDescriptor.Panel<WizardDescriptor>> getPanels() {
158+
if (panels == null) {
159+
panels = new ArrayList<>();
160+
// Use the standard target chooser
161+
Project p = Templates.getProject(wizard);
162+
SourceGroup[] groups = ProjectUtils.getSources(p).getSourceGroups(Sources.TYPE_GENERIC);
163+
WizardDescriptor.Panel<WizardDescriptor> targetChooser = Templates.buildSimpleTargetChooser(p, groups).create();
164+
panels.add(targetChooser);
165+
166+
String[] steps = createSteps();
167+
for (int i = 0; i < panels.size(); i++) {
168+
Component c = panels.get(i).getComponent();
169+
if (steps[i] == null) {
170+
// Default step name to component name of panel.
171+
steps[i] = c.getName();
172+
}
173+
if (c instanceof JComponent) { // assume Swing components
174+
JComponent jc = (JComponent) c;
175+
jc.putClientProperty(WizardDescriptor.PROP_CONTENT_SELECTED_INDEX, i);
176+
jc.putClientProperty(WizardDescriptor.PROP_CONTENT_DATA, steps);
177+
jc.putClientProperty(WizardDescriptor.PROP_AUTO_WIZARD_STYLE, true);
178+
jc.putClientProperty(WizardDescriptor.PROP_CONTENT_DISPLAYED, true);
179+
jc.putClientProperty(WizardDescriptor.PROP_CONTENT_NUMBERED, true);
180+
}
181+
}
182+
}
183+
return panels;
184+
}
185+
186+
private String[] createSteps() {
187+
String[] beforeSteps = (String[]) wizard.getProperty(WizardDescriptor.PROP_CONTENT_DATA);
188+
assert beforeSteps != null : "This wizard may only be used embedded in the template wizard";
189+
String[] res = new String[(beforeSteps.length - 1) + panels.size()];
190+
for (int i = 0; i < res.length; i++) {
191+
if (i < (beforeSteps.length - 1)) {
192+
res[i] = beforeSteps[i];
193+
} else {
194+
res[i] = panels.get(i - beforeSteps.length + 1).getComponent().getName();
195+
}
196+
}
197+
return res;
198+
}
199+
}

0 commit comments

Comments
 (0)