Source code: com/trapezium/chisel/reorganizers/BToHAnim.java
1 package com.trapezium.chisel.reorganizers;
2
3 import com.trapezium.chisel.*;
4
5 import com.trapezium.vrml.node.*;
6 //import com.trapezium.vrml.node.generated.*;
7 import com.trapezium.vrml.Scene;
8
9 public class BToHAnim extends Optimizer {
10 boolean patched = false;
11 public BToHAnim(){
12 super( "All", "Converting to H-Anim..." );
13 };
14 //public BToHAnim( String nodeName ) {
15 // super( nodeName, "Converting " + nodeName + " to H-Anim..." );
16 //}
17 public void attemptOptimization( Node n ) {
18 String nodeName = n.getNodeName();
19 if ( nodeName.equals("Script") ) {
20 DEFUSENode def = (DEFUSENode) n.getDEFparent();
21 if ( def == null ) {
22 // no DEF - skip this script
23 return;
24 }
25 if ( def.getDEFName().equals("Welder") ) {
26 // no modifications yet
27 } else if ( def.getDEFName().equals("Walker") ) {
28 System.out.println( "Changing Walker..." );
29 replaceRange( n.getFirstTokenOffset(), n.getLastTokenOffset(), "Walker" );
30 } else if ( def.getDEFName().equals("Patch") ) {
31 // already modified
32 }
33 } else if ( nodeName.indexOf("PROTO Avatar")>=0 ){
34 System.out.println( "attemptOptimization: "+n.getNodeName() );
35 String[] names = ((PROTO)n).getFieldNames();
36 for ( int i = 0; i < names.length; i++ ) {
37 if ( names[i].equals( "javaUrl" )) {
38 patched = true;
39 return;
40 }
41 }
42 patched = false;
43 replaceRange( n.getFirstTokenOffset(), n.getFirstTokenOffset()+2, "PROTO" );
44 }
45 }
46
47 public void optimize( TokenPrinter tp, Object param, int startTokenOffset, int endTokenOffset ) {
48 System.out.println( "optimize: "+param+" "+startTokenOffset+" "+endTokenOffset );
49 if ( ((String)param).equals( "Walker" ) ) {
50 tp.printLine(" Script {");
51 tp.printLine("directOutput TRUE");
52 tp.printLine("eventIn SFVec3f setPos IS set_position");
53 tp.printLine("eventIn SFRotation setRot IS rotation");
54 tp.printLine("eventIn SFBool isPilot IS isPilot");
55 tp.printLine("field MFInt32 mIsPilot [ 0 ]");
56 tp.printLine("eventIn SFBool isActive");
57 tp.printLine("eventIn SFFloat walk");
58 tp.printLine("eventIn SFBool watched");
59 tp.printLine("field MFInt32 mWatched [ 0 ]");
60 tp.printLine("field MFInt32 moving [ 0 ]");
61 tp.printLine("field MFInt32 newMove [ 0 ]");
62 tp.printLine("field MFInt32 notFirst [ 0 ]");
63 tp.printLine("eventIn SFVec3f curPos");
64 tp.printLine("field MFVec3f mCurPos [ 0 0 0 ]");
65 tp.printLine("field MFVec3f dir [ 0 0 0 ]");
66 tp.printLine("field MFVec3f keyValues[]");
67 tp.printLine("eventOut SFVec3f pos_changed");
68 tp.printLine("eventOut SFRotation rot_changed");
69 tp.printLine("field MFFloat angle [ 0 ]");
70 tp.printLine("eventOut SFTime startMove");
71 tp.printLine("eventOut SFTime stopWalk");
72 tp.printLine("eventOut MFVec3f values_changed");
73 tp.printLine("field MFFloat ooStep [ 1 ]");
74 tp.printLine("field MFFloat kWalk [ 0 ]");
75 tp.printLine("field MFFloat k2Walk [ 0 ]");
76 tp.printLine("field MFFloat vWalk [ 0 ]");
77 tp.printLine("field MFFloat fWalk [ 0 ]");
78 tp.printLine("eventOut SFFloat walkFrac");
79 tp.printLine("field SFFloat stepLength 1.4");
80 tp.printLine("eventIn SFBool idle");
81 tp.printLine("field SFNode welder USE Welder");
82 tp.printLine("field SFNode tsIdle USE TsIdle");
83 tp.printLine("url \"vrmlscript:");
84 tp.printLine("function watched( v, t ) {");
85 tp.printLine(" if ( v ) {");
86 tp.printLine(" mWatched[0] = 1;");
87 tp.printLine(" } else {");
88 tp.printLine(" mWatched[0] = 0;");
89 tp.printLine(" }");
90 tp.printLine("}");
91 tp.printLine("function isPilot( v, t ) {");
92 tp.printLine(" if ( v ) {");
93 tp.printLine(" mIsPilot[0] = 1;");
94 tp.printLine(" } else {");
95 tp.printLine(" mIsPilot[0] = 0;");
96 tp.printLine(" }");
97 tp.printLine("}");
98 tp.printLine("function curPos(v,t) {");
99 tp.printLine(" mCurPos[0] = v;");
100 tp.printLine("}");
101 tp.printLine("function move(t){");
102 tp.printLine(" keyValues[0]=mCurPos[0];");
103 tp.printLine(" values_changed = keyValues;");
104 tp.printLine(" vWalk[0]=0;");
105 tp.printLine(" k2Walk[0]=kWalk[0];");
106 tp.printLine(" startMove = t;");
107 tp.printLine("}");
108 tp.printLine("function isActive (v,t){");
109 tp.printLine(" if(!v){");
110 tp.printLine(" if(newMove[0]!=0){newMove[0]=0;move(t); }");
111 tp.printLine(" else stopw(t);");
112 tp.printLine(" }");
113 tp.printLine("}");
114 tp.printLine("function walk(v,t){");
115 tp.printLine(" if(mWatched[0]!=0){");
116 tp.printLine(" fWalk[0]+=(v-vWalk[0])*k2Walk[0];");
117 tp.printLine(" vWalk[0]=v;");
118 tp.printLine(" while(fWalk[0]>=1)fWalk[0]-=1;//mod!");
119 tp.printLine(" while(fWalk[0]<0)fWalk[0]+=1;//grr!");
120 tp.printLine(" walkFrac=fWalk[0];");
121 tp.printLine(" }");
122 tp.printLine("}");
123 tp.printLine("function setRot(v,t){");
124 tp.printLine(" rot_changed=v;");
125 tp.printLine(" if(v[3]!=angle[0]){");
126 tp.printLine(" angle[0]=v[3];");
127 tp.printLine(" dir[0].x=Math.sin(angle[0]);");
128 tp.printLine(" dir[0].z=Math.cos(angle[0]);");
129 tp.printLine(" }");
130 tp.printLine("}");
131 tp.printLine("function setPos(v,t){");
132 tp.printLine(" d=v.subtract(mCurPos[0]);");
133 tp.printLine(" if(d.length()==0){");
134 tp.printLine(" if(moving[0] != 0)stopw(t);");
135 tp.printLine(" return;");
136 tp.printLine(" }");
137 tp.printLine(" kWalk[0]=d.dot(dir[0])*ooStep[0];");
138 tp.printLine(" if(mIsPilot[0] != 0){");
139 tp.printLine(" if(moving[0] == 0)startw();");
140 tp.printLine(" if(kWalk[0]>0.2)kWalk[0]=0.2;");
141 tp.printLine(" fWalk[0]+=kWalk[0];");
142 tp.printLine(" while(fWalk[0]>=1)fWalk[0]-=1;//mod!");
143 tp.printLine(" while(fWalk[0]<0)fWalk[0]+=1;//grr!");
144 tp.printLine(" walkFrac=fWalk[0];");
145 tp.printLine(" pos_changed=v;");
146 tp.printLine(" return;");
147 tp.printLine(" }");
148 tp.printLine(" keyValues[1]=v;");
149 tp.printLine(" if (moving[0]!=0)newMove[0]=1;");
150 tp.printLine(" else if(notFirst[0] != 0){startw();move(t);}");
151 tp.printLine(" else{pos_changed=v;notFirst[0]=1;}");
152 tp.printLine("}");
153 tp.printLine("function startw(){");
154 tp.printLine(" moving[0]=1;");
155 tp.printLine(" tsIdle.enabled=false;");
156 tp.printLine(" welder.Gstart=-1;");
157 tp.printLine(" fWalk[0]=0;");
158 tp.printLine("}");
159 tp.printLine("function stopw(t){");
160 tp.printLine(" tsIdle.enabled=false;");
161 tp.printLine(" tsIdle.set_stopTime=t+0.3;");
162 tp.printLine(" tsIdle.set_startTime=t;");
163 tp.printLine(" tsIdle.enabled=true;");
164 tp.printLine(" moving[0]=0;");
165 tp.printLine("}");
166 tp.printLine("function idle(b,t){");
167 tp.printLine(" if(!b){");
168 tp.printLine(" if(moving[0]==0){");
169 tp.printLine(" tsIdle.set_stopTime=t+30+60*Math.random();");
170 tp.printLine(" tsIdle.set_startTime=t;");
171 tp.printLine(" welder.G10=t;");
172 tp.printLine(" }");
173 tp.printLine(" }");
174 tp.printLine("}");
175 tp.printLine("function initialize(){");
176 tp.printLine(" notFirst[0]=0;");
177 tp.printLine(" ooStep[0]=1/stepLength;");
178 tp.printLine(" dir[0].x=Math.sin(angle[0]);");
179 tp.printLine(" dir[0].z=Math.cos(angle[0]);");
180 tp.printLine("}");
181 tp.printLine("\"");
182 tp.printLine("}#Walker Script");
183 if ( ! patched ) {
184 tp.printLine("DEF ScriptGroup Group { children IS addScript }");
185 tp.printLine("DEF Patch Script {");
186 tp.printLine(" eventIn MFNode addScript");
187 tp.printLine(" field SFNode group USE ScriptGroup");
188 tp.printLine(" field SFNode script USE Patch");
189 tp.printLine(" field SFNode walker USE Walker");
190 tp.printLine(" eventIn SFRotation setRot");
191 tp.printLine(" eventOut SFRotation orientation_changed");
192 tp.printLine(" url \"javascript:");
193 tp.printLine(" function initialize() {");
194 tp.printLine(" Browser.addRoute( group, 'children_changed', script, 'addScript' );");
195 tp.printLine(" if ( Browser.getName().indexOf('blaxxun') >= 0 ) {");
196 tp.printLine(" addScript( group.children, 0 );");
197 tp.printLine(" }");
198 tp.printLine(" }");
199 tp.printLine(" function setRot(e, t) {");
200 tp.printLine(" orientation_changed = e.multiply( new SFRotation( 0, 1, 0, 3.14 ));");
201 tp.printLine(" }");
202 tp.printLine(" function addScript(e,t) {");
203 tp.printLine(" Browser.addRoute( e[0], 'position_changed', walker, 'setPos' );");
204 tp.printLine(" Browser.addRoute( e[0], 'orientation_changed', script, 'setRot' );");
205 tp.printLine(" Browser.addRoute( script, 'orientation_changed', walker, 'setRot' );");
206 tp.printLine(" }");
207 tp.printLine(" \"");
208 tp.printLine("}");
209 }
210 } else if ( ((String)param).equals( "PROTO" ) ) {
211 tp.printLine("PROTO Avatar [");
212 tp.printLine("exposedField MFString javaUrl [ \"UserScript.wrl\" ]");
213 tp.printLine("exposedField MFString jsUrl [ \"UserScript-js.wrl\" ]");
214 tp.printLine("exposedField MFNode addScript []");
215 }
216 }
217 }