Skip to content

Commit

Permalink
Merge pull request #266 from simlu/develop
Browse files Browse the repository at this point in the history
Chinese, Collada Export Options, Misc
  • Loading branch information
simlu authored Oct 14, 2017
2 parents fe23c23 + 7469a29 commit 8e343df
Show file tree
Hide file tree
Showing 8 changed files with 884 additions and 39 deletions.
Binary file modified lib/render/jpct.jar
Binary file not shown.
763 changes: 763 additions & 0 deletions resource/bundle/LangBundle_zh_CN.properties

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ public void setUseVertexColoring(boolean useVertexColoring) {
this.useVertexColoring = useVertexColoring;
}

private boolean prefixObjectNamesWithFileName = true;
public void setPrefixObjectNamesWithFileName(boolean prefixObjectNamesWithFileName) {
this.prefixObjectNamesWithFileName = prefixObjectNamesWithFileName;
}

private float objectScale = 0.05f;
public void setObjectScale(float objectScale) {
this.objectScale = objectScale;
}

private boolean exportTexturedVoxels = false;
public void setExportTexturedVoxels(boolean exportTexturedVoxels) {
this.exportTexturedVoxels = exportTexturedVoxels;
Expand Down Expand Up @@ -132,7 +142,10 @@ public final boolean export(Data data, ErrorHandlerInterface errorHandler, File
forcePOT, separationMode, triangulateByColor, useVertexColoring, fixTJunctions, exportTexturedVoxels, useOverlappingUvs,
useSkewedUvs);
ColladaFileExporter colladaFileExporter = new ColladaFileExporter(
getProgressDialog(), getConsole(), exportDataManager, prefix, objectName, useYUP, exportOrthogonalVertexNormals, useVertexColoring);
getProgressDialog(), getConsole(), exportDataManager, prefix,
objectName, useYUP, exportOrthogonalVertexNormals, useVertexColoring,
prefixObjectNamesWithFileName, objectScale
);

setActivity("Writing Data File...", true);
// write the dae file
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/com/vitco/app/export/collada/ColladaFileExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,21 @@ public class ColladaFileExporter extends ProgressReporter {
// overwriting of textures that belong to different files)
private final String texturePrefix;

// whether to prefix all object names with the file name
private final boolean prefixObjectNamesWithFileName;

// scale factor (useful for Blender)
private static final float SCALE = 0.05f;
private final float objectScale;

// constructor
public ColladaFileExporter(ProgressDialog dialog, ConsoleInterface console, ExportDataManager exportDataManager,
String texturePrefix, String name, boolean useYUP, boolean exportOrthogonalVertexNormals, boolean useVertexColoring) {
String texturePrefix, String name, boolean useYUP, boolean exportOrthogonalVertexNormals, boolean useVertexColoring,
boolean prefixObjectNamesWithFileName, float objectScale) {
super(dialog, console);
this.exportDataManager = exportDataManager;
this.texturePrefix = texturePrefix;
this.prefixObjectNamesWithFileName = prefixObjectNamesWithFileName;
this.objectScale = objectScale;
// initialize the xml file
setActivity("Creating File Data...", true);
initXmlFile(useYUP, useVertexColoring);
Expand All @@ -65,35 +71,35 @@ public ColladaFileExporter(ProgressDialog dialog, ConsoleInterface console, Expo

// create the object in the scene
private void writeObjects(String name) {
String cleanName = name.replace(" ", "_").replaceAll("[^a-zA-Z0-9_\\-\\.]", "").toLowerCase();
String cleanName = name.replace(" ", "_").replaceAll("[^a-zA-Z0-9_\\-\\.]", "");
String[] layerNames = exportDataManager.getLayerNames();
HashSet<String> knownObjectIds = new HashSet<>();
float[][] offsets = exportDataManager.getOffsets();
for (int layerRef = 0; layerRef < layerNames.length; layerRef++) {
float[] offset = offsets[layerRef];
String layerName = layerNames[layerRef];
String cleanLayerName = layerName.replace(" ", "_").replaceAll("[^a-zA-Z0-9_\\-\\.]", "").toLowerCase();
String objectId = cleanLayerName;
String cleanLayerName = layerName.replace(" ", "_").replaceAll("[^a-zA-Z0-9_\\-\\.]", "");
String objectId = cleanName.toLowerCase() + "." + cleanLayerName.toLowerCase();
int count = 1;
while (knownObjectIds.contains(objectId)) {
objectId = cleanLayerName + "." + String.format("%03d", count);
objectId = cleanName.toLowerCase() + "." + cleanLayerName.toLowerCase() + "." + String.format("%03d", count);
count++;
}
knownObjectIds.add(objectId);
// create the object
xmlFile.resetTopNode("library_visual_scenes/visual_scene/node[-1]");
xmlFile.addAttributes("", new String[]{
"id=" + cleanName + "." + objectId,
"name=" + cleanName + "." + objectId,
"id=" + objectId,
"name=" + (this.prefixObjectNamesWithFileName ? cleanName + "." : "") + cleanLayerName,
"type=NODE"
});
xmlFile.addAttrAndTextContent("translate", new String[]{"sid=location"},
(-SCALE * offset[0] * 2) + " " + (-SCALE * offset[2] * 2) + " " + (-SCALE * offset[1] * 2));
(-objectScale * offset[0] * 2) + " " + (-objectScale * offset[2] * 2) + " " + (-objectScale * offset[1] * 2));
xmlFile.addAttrAndTextContent("rotate[-1]", new String[]{"sid=rotationZ"}, "0 0 1 0");
xmlFile.addAttrAndTextContent("rotate[-1]", new String[]{"sid=rotationY"}, "0 1 0 0");
xmlFile.addAttrAndTextContent("rotate[-1]", new String[]{"sid=rotationX"}, "1 0 0 0");
// scale the object down
xmlFile.addAttrAndTextContent("scale", new String[]{"sid=scale"}, SCALE + " " + SCALE + " " + SCALE);
// scale the object
xmlFile.addAttrAndTextContent("scale", new String[]{"sid=scale"}, objectScale + " " + objectScale + " " + objectScale);


// add the material to the object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ protected Object doInBackground() throws Exception {
// add help links
dialog.addLink(console, "Export to Blender", "https://github.com/simlu/voxelshop/wiki/Export-for-Blender");
dialog.addLink(console, "Export to Unity", "https://github.com/simlu/voxelshop/wiki/Export-for-Unity");
dialog.addLink(console, "Export to Stonehearth", "https://github.com/simlu/voxelshop/wiki/Export-for-Stonehearth");
dialog.addLink(console, "Export to Stonehearth", "https://discourse.stonehearth.net/t/5289");

// add file select
FieldSet location = new FieldSet("location", "Location");
Expand Down Expand Up @@ -455,11 +455,21 @@ protected Object doInBackground() throws Exception {

collada.addComponent(new SeparatorModule("Misc"));

// option: prefix object names with file name
CheckBoxModule prefixObjectNamesWithFileName = new CheckBoxModule(
"prefix_object_names_with_file_name", "Prefix Object Names with File Name", true);
collada.addComponent(prefixObjectNamesWithFileName);

// option: fix t junction problems
CheckBoxModule fixTJunctions = new CheckBoxModule("fix_tjunctions", "Fix all T-Junction problems", true);
fixTJunctions.setEnabledLookup("collada.type=poly2tri");
collada.addComponent(fixTJunctions);

// option: set output scale
TextInputModule objectScale = new TextInputModule(
"object_scale", "Object Scale", "0.05", true, "^([0-9]*\\.[0-9]+|[0-9]+)$");
collada.addComponent(objectScale);

collada.addComponent(new SeparatorModule("Object Separation"));
ComboBoxModule separationMode = new ComboBoxModule("separation_mode", new String[][]{
new String[]{"merged", "Merged"},
Expand Down Expand Up @@ -762,6 +772,10 @@ protected Object doInBackground() throws Exception {
colladaExportWrapper.setTriangulateByColor(dialog.is("collada.triangulate_by_color=true"));
// set use vertex coloring
colladaExportWrapper.setUseVertexColoring(dialog.is("collada.use_vertex_coloring=true"));
// set prefix object names with file name
colladaExportWrapper.setPrefixObjectNamesWithFileName(dialog.is("collada.prefix_object_names_with_file_name=true"));
// set object scale
colladaExportWrapper.setObjectScale(Float.parseFloat(dialog.getValue("collada.object_scale")));
// set export textured voxels
colladaExportWrapper.setExportTexturedVoxels(dialog.is("collada.export_textured_voxels=true"));
// set force power of two force textures
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/vitco/app/manager/lang/LangSelector.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.vitco.app.manager.lang;

import com.vitco.app.util.misc.UTF8Control;

import javax.annotation.PostConstruct;
import java.util.Locale;
import java.util.ResourceBundle;
Expand All @@ -22,7 +24,7 @@ public final class LangSelector implements LangSelectorInterface {
@Override
public void init() {
Locale locale = new Locale(language, country);
rb = ResourceBundle.getBundle(defaultFile, locale);
rb = ResourceBundle.getBundle(defaultFile, locale, new UTF8Control());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import com.vitco.app.util.components.dialog.BlankDialogModule;
import com.vitco.app.util.components.textfield.JCustomTextField;
import com.vitco.app.util.components.textfield.TextChangeListener;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;

/**
Expand All @@ -22,10 +19,21 @@ public class TextInputModule extends BlankDialogModule {
// the text field part
private final JCustomTextField textField = new JCustomTextField();

// constructor
private final String regex;
private final String text;

// constructor without regex
public TextInputModule(String identifier, String caption, String text, boolean requireText) {
this(identifier, caption, text, requireText, null);
}

// constructor
public TextInputModule(String identifier, String caption, String text, boolean requireText, String regex) {
super(identifier);
setLayout(new BorderLayout());
assert regex == null || text.matches(regex);
this.regex = regex;
this.text = text;
// create label
JLabel textLabel = new JLabel(caption);
// add spacing to the right
Expand All @@ -41,44 +49,36 @@ public TextInputModule(String identifier, String caption, String text, boolean r
setReady(!textField.getText().trim().equals(""));

// listen to edit events
textField.addTextChangeListener(new TextChangeListener() {
@Override
public void onChange() {
setReady(!textField.getText().trim().equals("") || !textField.isEnabled());
notifyContentChanged();
}
textField.addTextChangeListener(() -> {
setReady(!textField.getText().trim().equals("") || !textField.isEnabled());
notifyContentChanged();
});

// listen to enabled/disabled changes (they affect the "readyness")
textField.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("enabled".equals(evt.getPropertyName())) {
setReady(!textField.getText().trim().equals("") || !textField.isEnabled());
notifyContentChanged();
}
textField.addPropertyChangeListener(evt -> {
if ("enabled".equals(evt.getPropertyName())) {
setReady(!textField.getText().trim().equals("") || !textField.isEnabled());
notifyContentChanged();
}
});
} else {
// listen to change events (simple)
textField.addTextChangeListener(new TextChangeListener() {
@Override
public void onChange() {
notifyContentChanged();
}
});
textField.addTextChangeListener(this::notifyContentChanged);
}
}

// get the value of this object
@Override
protected String getValue(String identifier) {
return textField.getText();
if (this.regex != null && !textField.getText().trim().matches(this.regex)) {
textField.setText(this.text);
}
return textField.getText().trim();
}

@Override
protected ArrayList<String[]> getSerialization(String path) {
ArrayList<String[]> keyValuePair = new ArrayList<String[]>();
ArrayList<String[]> keyValuePair = new ArrayList<>();
keyValuePair.add(new String[] {path, getValue(null)});
return keyValuePair;
}
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/com/vitco/app/util/misc/UTF8Control.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.vitco.app.util.misc;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.ResourceBundle.Control;

// Reference: https://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle

public class UTF8Control extends Control {

public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException
{
// The below is a copy of the default implementation.
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, "properties");
ResourceBundle bundle = null;
InputStream stream = null;
if (reload) {
URL url = loader.getResource(resourceName);
if (url != null) {
URLConnection connection = url.openConnection();
if (connection != null) {
connection.setUseCaches(false);
stream = connection.getInputStream();
}
}
} else {
stream = loader.getResourceAsStream(resourceName);
}
if (stream != null) {
try (
InputStreamReader inputStreamReader = new InputStreamReader(stream, "UTF-8")
){
// Read properties files as user defined and if not provided then use "UTF-8" encoding.
bundle = new PropertyResourceBundle(inputStreamReader);
}
}
return bundle;
}
}

0 comments on commit 8e343df

Please sign in to comment.