Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
convert javascript object to jsonvalue for guava-converter
Browse files Browse the repository at this point in the history
  • Loading branch information
akjava committed Jan 9, 2017
1 parent 3f3385d commit 3ce265c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 16 deletions.
45 changes: 34 additions & 11 deletions src/com/akjava/gwt/three/client/java/file/JSONMorphTargetsFile.java
Expand Up @@ -4,6 +4,10 @@
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.JsArrayNumber;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;

/*
* extreamlly similar morphtarget animation clip
Expand All @@ -13,43 +17,62 @@
* somt time value need fix for fit the model.
*
*/
public class JSONMorphTargetsFile extends JSParameter{
protected JSONMorphTargetsFile(){}

public class JSONMorphTargetsFile extends JSONObject{
public JSONMorphTargetsFile(){}
/*
public final static JSONMorphTargetsFile create(){
JSONMorphTargetsFile file=JSONMorphTargetsFile.createObject().cast();
JSONMorphTargetsFile file=new JSONMorphTargetsFile();
JSParameter meta=JSParameter.createParameter();
meta.set("version", 1.0);
meta.set("type", "morphTargets");
file.set("metadata", meta);
JSONObject metaJson=new JSONObject(meta);
file.put("metadata", metaJson);
JSParameter data=JSParameter.createParameter();
file.set("data", data);
//empty
JsArray<JavaScriptObject> morphTargetDatas=JavaScriptObject.createArray().cast();
data.set("morphtargetDatas", morphTargetDatas);
file.put("data", new JSONArray(morphTargetDatas));
return file;
}*/

public JsArray<JSParameter> getMorphTargetData(){
JSONValue data=get("data");
JSONObject object=data.isObject();
JSONValue array=object.get("morphtargetDatas");
JSONArray jsonarray=array.isArray();
return jsonarray.getJavaScriptObject().cast();
}

public native final JsArray<JSParameter> getMorphTargetData ()/*-{
return this["data"]["morphtargetDatas"];
}-*/;
public String getString(String key){
JSONValue value=get(key);
JSONString string=value.isString();
return string.stringValue();
}

public JsArrayNumber getArrayNumber(String key){
JSONValue value=get(key);
JSONArray array=value.isArray();
return array.getJavaScriptObject().cast();
}


public final static JSParameter createMorphTargetData(String name,JsArrayNumber times,JsArrayNumber values){
/*public final static JSParameter createMorphTargetData(String name,JsArrayNumber times,JsArrayNumber values){
JSParameter data=JSParameter.createParameter();
data.set("name", name);
data.set("times",times);
data.set("values",values);
return data;
}
}*/

}
Expand Up @@ -12,12 +12,12 @@ public class JSONMorphTargetsFileConverter extends Converter<JSONMorphTargetsFil

@Override
protected JSONObject doForward(JSONMorphTargetsFile file) {
return new JSONObject(file);
return new JSONObject(file.getJavaScriptObject());
}

@Override
protected JSONMorphTargetsFile doBackward(JSONObject object) {
return object.getJavaScriptObject().cast();
return (JSONMorphTargetsFile)object;
}


Expand Down
Expand Up @@ -3,17 +3,25 @@
import java.util.List;

import com.akjava.gwt.lib.client.LogUtils;
import com.akjava.gwt.three.client.gwt.JSParameter;
import com.akjava.gwt.three.client.js.THREE;
import com.akjava.gwt.three.client.js.animation.AnimationClip;
import com.akjava.gwt.three.client.js.animation.KeyframeTrack;
import com.akjava.gwt.three.client.js.animation.tracks.NumberKeyframeTrack;
import com.google.common.base.Converter;
import com.google.common.collect.Lists;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.JsArrayNumber;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONString;

public class MorphTargetKeyFrameConverter extends Converter<List<MorphTargetKeyFrame>,JSONMorphTargetsFile>{

@Override
protected JSONMorphTargetsFile doForward(List<MorphTargetKeyFrame> list) {
JSONMorphTargetsFile file=new JSONMorphTargetsFile();
file.set("name", list.get(0).getKeyName());
file.put("name", new JSONString(list.get(0).getKeyName()));

JsArrayNumber times=JsArray.createArray().cast();
JsArrayNumber values=JsArray.createArray().cast();
Expand All @@ -22,8 +30,8 @@ protected JSONMorphTargetsFile doForward(List<MorphTargetKeyFrame> list) {
times.push(frame.getTime());
values.push(frame.getValue());
}
file.set("times", times);
file.set("values", values);
file.put("times",new JSONArray( times));
file.put("values",new JSONArray( values));

return file;
}
Expand Down Expand Up @@ -62,4 +70,42 @@ protected List<MorphTargetKeyFrame> doBackward(JSONMorphTargetsFile morphTargetF
}


public static AnimationClip converToAnimationClip(Iterable<List<MorphTargetKeyFrame>> framesList,String animationClipName,MorphtargetsModifier modifier,JSParameter morphTargetDictionary){

JsArray<KeyframeTrack> tracks=JavaScriptObject.createArray().cast();
for(List<MorphTargetKeyFrame> list:framesList){
String key=list.get(0).getKeyName();//TODO check size?

if(!morphTargetDictionary.exists(key)){
LogUtils.log("MorphTargetKeyFrameConverter-converToAnimationClip:call not exist key="+key+".skipped creating track");
continue;
}
int index=morphTargetDictionary.getInt(key);//possible null?
NumberKeyframeTrack track=morphTargetKeyFrameToTrack(key,index,list,modifier);
tracks.push(track);
}

AnimationClip clip=THREE.AnimationClip(animationClipName, -1, tracks);
return clip;
}

public static NumberKeyframeTrack morphTargetKeyFrameToTrack(String keyName,int index,List<MorphTargetKeyFrame> frames,MorphtargetsModifier modifier){
String trackName=".morphTargetInfluences["+index+"]";


JsArrayNumber times=JavaScriptObject.createArray().cast();
JsArrayNumber values=JavaScriptObject.createArray().cast();
for(MorphTargetKeyFrame frame:frames){
times.push(frame.getTime()/1000);//millisecond to second
values.push(toModifyValue(keyName,frame.getValue(),modifier));
}

NumberKeyframeTrack track=THREE.NumberKeyframeTrack(trackName, times, values);
return track;
}

//TODO BasicExpressionPanel to better location
private static double toModifyValue(String key,double value,MorphtargetsModifier modifier) {
return modifier.getModifiedValue(key, value);
}
}

0 comments on commit 3ce265c

Please sign in to comment.