00001 package org.hfbk.vis.visnode;
00002
00003 import java.awt.event.MouseEvent;
00004 import java.util.List;
00005
00006 import org.lwjgl.util.vector.Vector3f;
00007
00045 public class Automator extends VisNode implements VisUI{
00046
00047 VisNode focus;
00048
00049 final int IDLE=1, ATTEND=2, FETCH=3, FETCHED=4;
00050 int state=IDLE;
00051
00052 float time=0;
00053
00054
00061 final float P_CHILD=.9f, P_PARENT=.4f;
00062
00066 final Class[] goodClasses={ VisText.class,
00067 VisKeyword.class,
00068 VisImage.class,
00069 VisImagefield.class,
00070 VisKeycloud.class
00071 };
00072
00073 Automator() {
00074 super(null, new Vector3f());
00075 radius=Float.POSITIVE_INFINITY;
00076 }
00077
00085 void renderSelf() {
00086 VisRoot root=getRoot();
00087
00088 time+=root.client.dt;
00089
00090 if (focus==null) focus=root;
00091
00092 switch (state){
00093 case IDLE:
00094 focus=travel(focus);
00095 root.client.setViewpoint(focus.traverse(root, new Vector3f()), .07f);
00096
00097 if(Math.random()<.8)
00098 root.client.hud.clear();
00099
00100 state(ATTEND);
00101
00102 break;
00103 case ATTEND:
00104 if (root.client.transViewpoint==null){
00105 if(focus instanceof VisKeyword){
00106 String keyword=((VisKeyword)focus).keyword;
00107 String source=getRandomSource((VisKeyword)focus);
00108 root.fetch(source, keyword, focus);
00109 state(FETCH);
00110 }else if (focus instanceof VisImagefield || focus instanceof VisImage){
00111
00112
00113 ((VisNodeMousable)focus).handleEvent(new VisNodeMousable.VisMouseEvent(MouseEvent.MOUSE_CLICKED,focus,null,null,0));
00114 state(IDLE);
00115 }else{
00116 state(IDLE);
00117 }
00118 }
00119 break;
00120 case FETCH:
00121 if (root.client.transViewpoint!=null)
00122 state(FETCHED);
00123 if(time>10){
00124 state(IDLE);
00125 }
00126 break;
00127 case FETCHED:
00128 if (root.client.transViewpoint==null) {
00129
00130 if (root.lastVisNode!=null){
00131 focus=root.lastVisNode;
00132 state(IDLE);
00133 }else
00134 state(IDLE);
00135 }
00136 break;
00137 }
00138 }
00139
00140
00141 void state(int newstate){
00142 time=0;
00143 state=newstate;
00144 }
00145
00146 VisNode comeFrom=null;
00155 VisNode travel(VisNode start){
00156
00157 VisNode root=getRoot();
00158 VisNode n=start, last=start;
00159
00160
00161
00162
00163 int i=0;
00164 while(n==root || n==start || n==comeFrom || !loveClass(n.getClass())){
00165
00166
00167
00168 if (i++>15)
00169 return start;
00170
00171 double r=Math.random();
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 if (r>1-P_PARENT && n.parent!=null)
00185 n=n.parent;
00186
00187 if (r<P_CHILD)
00188 n=getRandomChild(n);
00189 }
00190 comeFrom=last;
00191
00192 System.out.println(n);
00193
00194 return n;
00195 }
00196
00197 VisNode getRandomChild(VisNode n){
00198 double r=Math.random();
00199 if (n.children.size()>0)
00200 n=n.children.get((int)(r*n.children.size()));
00201 return n;
00202 }
00203
00204 String getRandomSource(VisKeyword kn){
00205 List<String> sources=kn.menuitems;
00206 return sources.get((int)(Math.random()*sources.size()));
00207 }
00208
00209 boolean loveClass(Class c){
00210 for (Class<?> goodClass: goodClasses)
00211 if (goodClass.isAssignableFrom(c))
00212 return true;
00213 return false;
00214
00215 }
00216 }