Skip to content

Instantly share code, notes, and snippets.

@StillManic
Created December 31, 2015 20:49
Show Gist options
  • Save StillManic/d959fd6100f81c66fc23 to your computer and use it in GitHub Desktop.
Save StillManic/d959fd6100f81c66fc23 to your computer and use it in GitHub Desktop.
public class OBJCustomData {
public enum DataKeys
{
AMBIENT_OCCLUSION("ambient", true),
GUI_3D("gui3d", true),
FLIP_DX11("flipDX11", false),
PROCESS_UVS("processUVs", new ProcessUVData()),
GROUP_CONFIGURATIONS("groupConfigs", new GroupConfigurations());
public final String key;
public final Object initialValue;
DataKeys(String key, Object initialValue)
{
this.key = key;
this.initialValue = initialValue;
}
}
protected static Map<String, DataKeys> keyLookUp = Maps.newHashMapWithExpectedSize(DataKeys.values().length);
protected EnumMap<DataKeys, Object> dataMap = Maps.newEnumMap(DataKeys.class);
protected float[] minUVBounds = new float[] {0.0f, 0.0f};
protected float[] maxUVBounds = new float[] {1.0f, 1.0f};
protected boolean hasProcessed = false;
protected boolean puvsAllFalse = true;
protected boolean useFullAtlas = false;
protected boolean hasGroupConfigurations = false;
public OBJCustomData(float[] minUVBounds, float[] maxUVBounds)
{
this.minUVBounds = minUVBounds != null ? minUVBounds : this.minUVBounds;
this.maxUVBounds = maxUVBounds != null ? maxUVBounds : this.maxUVBounds;
this.initDataMap();
}
public OBJCustomData(ImmutableMap<String, String> customData)
{
this.initDataMap();
this.process(customData);
}
public OBJCustomData duplicate()
{
OBJCustomData ret = new OBJCustomData(this.minUVBounds, this.maxUVBounds);
ret.dataMap = Maps.newEnumMap(this.dataMap);
ret.hasProcessed = this.hasProcessed;
ret.puvsAllFalse = this.puvsAllFalse;
ret.useFullAtlas = this.useFullAtlas;
ret.hasGroupConfigurations = this.hasGroupConfigurations;
return ret;
}
private void initDataMap()
{
for (DataKeys key : DataKeys.values())
{
keyLookUp.put(key.key, key);
this.dataMap.put(key, key.initialValue);
}
}
public void process(ImmutableMap<String, String> customData)
{
this.hasProcessed = true;
JsonParser parser = new JsonParser();
for (Map.Entry<String, String> e : customData.entrySet())
{
if (this.dataMap.containsKey(keyLookUp.get(e.getKey())))
{
JsonElement element = parser.parse(e.getValue());
if (element.isJsonPrimitive() && !e.getKey().equals(DataKeys.PROCESS_UVS.key) /*&& !e.getKey().equals(DataKeys.GROUP_CONFIGURATIONS.key)*/)
{
this.dataMap.replace(keyLookUp.get(e.getKey()), element.getAsBoolean());
}
else
{
JsonObject jsonObject = element.getAsJsonObject();
if (e.getKey().equals(DataKeys.PROCESS_UVS.key))
{
for (ProcessUVData.DataKeys procUVKey : ProcessUVData.DataKeys.values())
{
if (jsonObject.has(procUVKey.key))
{
if (jsonObject.get(procUVKey.key).isJsonPrimitive())
{
boolean value = jsonObject.get(procUVKey.key).getAsBoolean();
((ProcessUVData)this.dataMap.get(DataKeys.PROCESS_UVS)).setValueForDataKey(procUVKey, Pair.of(value, value));
}
else
{
JsonObject jsonUVs = jsonObject.getAsJsonObject(procUVKey.key);
boolean u = false;
boolean v = false;
if (jsonUVs.has("u")) u = jsonUVs.get("u").getAsBoolean();
if (jsonUVs.has("v")) v = jsonUVs.get("v").getAsBoolean();
((ProcessUVData)this.dataMap.get(DataKeys.PROCESS_UVS)).setValueForDataKey(procUVKey, Pair.of(u, v));
if (u || v) this.puvsAllFalse = false;
}
}
}
}
else if (e.getKey().equals(DataKeys.GROUP_CONFIGURATIONS.key))
{
GroupConfigurations configs = (GroupConfigurations) this.dataMap.get(DataKeys.GROUP_CONFIGURATIONS);
configs.process(element.getAsJsonObject());
this.dataMap.put(DataKeys.GROUP_CONFIGURATIONS, configs);
}
}
}
else if (e.getKey().equals("useFullAtlas"))
{
this.useFullAtlas = parser.parse(e.getValue()).getAsBoolean();
}
}
this.hasGroupConfigurations = ((GroupConfigurations) this.dataMap.get(DataKeys.GROUP_CONFIGURATIONS)).hasConfigs;
}
public Object getValueForDataKey(DataKeys key)
{
return this.dataMap.get(key);
}
public boolean hasUVsOutOfBounds()
{
return this.minUVBounds[0] < 0.0f || this.minUVBounds[1] < 0.0f || this.maxUVBounds[0] > 1.0f || this.maxUVBounds[1] > 1.0f;
}
static class ProcessUVData
{
public enum DataKeys
{
NORMALIZE,
UNITIZE,
//TODO: look into face splitting!!!
// WRAP,
CLAMP,
// TILE,
FLIP;
public final String key;
DataKeys()
{
this.key = this.name().toLowerCase(Locale.US);
}
}
protected EnumMap<DataKeys, Pair<Boolean, Boolean>> dataMap = Maps.newEnumMap(DataKeys.class);
public ProcessUVData()
{
initDataMap();
}
private void initDataMap()
{
for (DataKeys key : DataKeys.values())
{
this.dataMap.put(key, Pair.of(false, false));
}
}
public Pair<Boolean, Boolean> getValueForDataKey(DataKeys key)
{
return this.dataMap.get(key);
}
public void setValueForDataKey(DataKeys key, Pair<Boolean, Boolean> value)
{
this.dataMap.put(key, value);
}
public DataKeys getDataKey(String name)
{
return DataKeys.valueOf(name.toUpperCase(Locale.US));
}
public boolean allValuesFalse()
{
for (DataKeys key : DataKeys.values())
{
if (this.dataMap.get(key).getLeft() || this.dataMap.get(key).getRight())
{
return false;
}
}
return true;
}
}
public static class GroupConfigurations
{
public Map<String, GroupConfig> configMap = Maps.newHashMap();
public boolean hasConfigs = false;
public GroupConfigurations() {}
public void process(JsonObject jsonObject)
{
this.hasConfigs = true;
for (Map.Entry<String, JsonElement> e : jsonObject.entrySet())
{
String name = e.getKey();
GroupConfig config = new GroupConfig(name);
JsonObject configObject = e.getValue().getAsJsonObject();
JsonArray changeArray = configObject.getAsJsonArray(GroupConfig.DataKeys.CHANGE.key);
List<String> groupNames = Lists.newArrayListWithCapacity(changeArray.size());
for (int i = 0; i < changeArray.size(); i++)
{
groupNames.add(changeArray.get(i).getAsString());
}
config.dataMap.put(GroupConfig.DataKeys.CHANGE, groupNames);
if (configObject.has(GroupConfig.DataKeys.OPERATION.key))
{
JsonPrimitive value = configObject.getAsJsonPrimitive(GroupConfig.DataKeys.OPERATION.key);
if (value.isString())
{
config.dataMap.put(GroupConfig.DataKeys.OPERATION, OBJState.Operation.TOGGLE);
}
else if (value.isBoolean())
{
config.dataMap.put(GroupConfig.DataKeys.OPERATION, value.getAsBoolean() ? OBJState.Operation.SET_TRUE : OBJState.Operation.SET_FALSE);
}
}
if (configObject.has(GroupConfig.DataKeys.DEFAULT_OPERATION.key))
{
JsonPrimitive value = configObject.getAsJsonPrimitive(GroupConfig.DataKeys.DEFAULT_OPERATION.key);
if (value.isString())
{
config.dataMap.put(GroupConfig.DataKeys.DEFAULT_OPERATION, OBJState.Operation.TOGGLE);
}
else if (value.isBoolean())
{
config.dataMap.put(GroupConfig.DataKeys.DEFAULT_OPERATION, value.getAsBoolean() ? OBJState.Operation.SET_TRUE : OBJState.Operation.SET_FALSE);
}
}
if (configObject.has(GroupConfig.DataKeys.MODIFY_ALL_GROUPS.key))
{
config.dataMap.put(GroupConfig.DataKeys.MODIFY_ALL_GROUPS, configObject.get(GroupConfig.DataKeys.MODIFY_ALL_GROUPS.key).getAsBoolean());
}
this.configMap.put(name, config);
}
}
}
public static class GroupConfig
{
public enum DataKeys
{
CHANGE("change", Lists.<String>newArrayList()),
OPERATION("operation", OBJState.Operation.SET_TRUE),
DEFAULT_OPERATION("defOperation", OBJState.Operation.SET_FALSE),
MODIFY_ALL_GROUPS("modifyAll", false);
public final String key;
public final Object initialValue;
DataKeys(String key, Object initialValue)
{
this.key = key;
this.initialValue = initialValue;
}
}
protected static Map<String, DataKeys> keyLookUp = Maps.newHashMapWithExpectedSize(DataKeys.values().length);
protected EnumMap<DataKeys, Object> dataMap = Maps.newEnumMap(DataKeys.class);
public String name;
public GroupConfig(String name)
{
this.name = name;
initDataMap();
}
private void initDataMap()
{
for (DataKeys key : DataKeys.values())
{
keyLookUp.put(key.key, key);
this.dataMap.put(key, key.initialValue);
}
}
public Object getValueForDataKey(DataKeys key)
{
return this.dataMap.get(key);
}
public void setValueForDataKey(DataKeys key, Object value)
{
this.dataMap.put(key, value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment