VisClient/org/hfbk/vis/visnode/VisText.java

Go to the documentation of this file.
00001         package org.hfbk.vis.visnode;
00002 
00003 import java.awt.event.MouseEvent;
00004 
00005 import org.dronus.gl.GLBoxRenderer;
00006 import org.dronus.gl.GLFont;
00007 import org.dronus.gl.GLTextPanel;
00008 import org.dronus.gl.GLUtil;
00009 import org.dronus.graph.Node;
00010 import org.hfbk.vis.Prefs;
00011 import org.hfbk.vis.VisNodeLayouter;
00012 import org.lwjgl.opengl.GL11;
00013 import org.lwjgl.util.vector.Vector3f;
00014 
00015 
00031 public class VisText extends VisNodeDraggable{
00032         
00033         final int MINWIDTH=34;
00034         final float ASPECT=0.8f;
00035         String[] words;
00036         
00037         GLTextPanel tp;
00038         int dl;
00039         
00045         public VisText(Node n, Vector3f pos) {
00046                 super(n, pos);
00047                 String text=n.text;
00048                 String header=n.getSubNodeText("headline");
00049                 url=n.getSubNodeText("URL");
00050                 
00051                 if (header!=null)
00052                         text=header+"\n\n"+text;
00053 
00054                 float l=GLFont.getDefault().getLineLength(text);
00055                 l=Math.min(l, Prefs.current.maxTextLength);
00056 
00057                 //if MINWIDTH lead to a too high panel,  
00058                 //compute w by the area the text occupies.  
00059                 w=Math.max(MINWIDTH,(float)Math.sqrt(l)*2)*ASPECT;
00060                 h=l/w*2+4; //factor 2 because we stretch the text to make it nicer
00061                 radius=(float)Math.sqrt(w*w+h*h)/2;
00062         }
00063         
00069         public VisText(Node n, Vector3f pos, int width){
00070                 this(n, pos);
00071                 
00072                 float l=(h-4)*w/2;
00073                 
00074                 w=width;
00075                 h=l/w*2+4;
00076                 radius=(float)Math.sqrt(w*w+h*h)/2;
00077         }
00078         
00079         VisNode explodeButton;
00080         
00081         void showUI(){
00082                 if (explodeButton==null)
00083                         explodeButton=new SmallButton("explode/del", new Vector3f(1,.8f,0), new Vector3f(w/2-1.5f,h/2-1.5f,1)){
00084                         
00085                         void handleEvent(VisMouseEvent evt) {
00086                                 super.handleEvent(evt);
00087                                 if (evt.getID()==MouseEvent.MOUSE_CLICKED){
00088                         
00089                                         if (evt.getButton()==MouseEvent.BUTTON1){
00090                                                 
00091                                                 spillKeywords();
00092                                 
00093                                         }
00094                                         
00095                                         if (evt.getButton()==MouseEvent.BUTTON3){
00096                                         VisNode par=explodeButton.parent;
00097                                         par.parent.remove(par);
00098                                         }
00099                                 }                       
00100                         }       
00101                         
00102                         
00103                         public void handleClick() {
00104                                         //spillKeywords();
00105                                 }
00106                 };
00107                 
00108                 add(explodeButton);
00109         }
00110         
00111         void spillKeywords() {
00112         
00113                 if (this.children.size()>0)     this.children.clear();  
00114                 
00115                 Vector3f pe=new Vector3f();
00116 
00117                 String all=this.node.text.replace("\n", " ");
00118                 all = all.replaceAll("[^ A-Za-z]", "");
00119                 words=all.split(" +");
00120                 for (String word: words){
00121                         
00122                         if (word.length()>3)
00123                         {       pe.x=(float)Math.random()*10;
00124                         pe.y=(float)Math.random()*10;
00125                         pe.z=(float)Math.random()*10;
00126                         add(new VisKeyword(new Node(word), pe));                                        
00127                         }
00128                 }
00129                 layoutLocked=false;
00130         }
00131         
00138         public void renderSelf(){
00139         
00140                 //exclude UI from counting and layouting
00141                 for (VisNode n: children)
00142                         if (n instanceof VisUI)
00143                                 remove(n);
00144                 
00145                 //layout the attached keywords
00146                 VisNodeLayouter.layOut(this,8,2,8);
00147                 
00148                 if (tp==null){
00149                         tp=new GLTextPanel(node.text,w-1,h);
00150                         h=tp.h*2; //set real height                     
00151                         radius=(float)Math.sqrt(w*w+h*h)/2;
00152                 }
00153                 
00154                 Vector3f pos=new Vector3f(GLUtil.getPosition());
00155                 
00156                 GL11.glDisable(GL11.GL_TEXTURE_2D);
00157                 
00158                 //calculate fade away when viewer passes
00159                 float alpha=Math.max(Math.min(-pos.z/20,1),0);
00160                 
00161                 //push back the background in z buffer to allow overrendering by text
00162                 GL11.glPolygonOffset(1,1);
00163                 
00164                 //the white border
00165                 if (isHoovered)
00166                         GL11.glColor4f(1,0,0,alpha);
00167                 else
00168                         GL11.glColor4f(1,1,1,alpha);
00169                 GLUtil.renderFrame(-w/2,-h/2,w/2,h/2);          
00170                 
00171                 if (dl==0){
00172                         dl=GL11.glGenLists(1);
00173                         GL11.glNewList(dl,GL11.GL_COMPILE);
00174                         //the black background
00175                         GL11.glEnable(GL11.GL_CULL_FACE); //show only from front
00176                         GLUtil.renderQuad(-w/2,-h/2,w/2,h/2);
00177                         
00178                         GL11.glPolygonOffset(0,0);
00179                         
00180                         GL11.glDisable(GL11.GL_CULL_FACE); //show text from both sides  
00181                         GL11.glColor3f(1,1,1);
00182                         GL11.glTranslatef(-w/2,h/2-2,.2f);
00183                         GL11.glEnable(GL11.GL_TEXTURE_2D);              
00184                         GLFont.getDefault().render();
00185                 GL11.glScalef(1,2,1); //stretch them a little, looks nice.              
00186                         tp.render();
00187                         GL11.glEndList();
00188                 }
00189                 GL11.glColor4f(0,0,0,alpha);
00190                 GL11.glCallList(dl);
00191 
00192                 if (isHoovered){
00193                         showUI();
00194                 }               
00195         }
00196         
00197         
00198         abstract class SmallButton extends VisButton{
00199                 Vector3f color;
00200                 
00201                 SmallButton(String help, Vector3f color, Vector3f position) {
00202                         super(help, position);
00203                         this.color=color;
00204                         w=h=radius=3;
00205                         
00206                         parent=VisText.this;
00207                 }
00208 
00209                 void renderSelf() {
00210                         GL11.glDisable(GL11.GL_TEXTURE_2D);
00211                         //push back the surface in z buffer to allow overrendering by edges
00212                         GL11.glPolygonOffset(1,1);
00213                         if(isHoovered){
00214                                 renderHelp();
00215                                 GL11.glColor4f(1,1,1,.5f);
00216                                 GLBoxRenderer.renderBox(w,h,w);
00217                         //}else if (toggled)
00218                         //      GL11.glColor4f(color.x,color.y,color.z,1f);
00219                         }else{
00220                                 GLBoxRenderer.renderColoredBox(w,h,w,color.x,color.y,color.z);
00221                         }
00222                         //render edges
00223                         /*GL11.glPolygonOffset(0,0);
00224                         GL11.glColor4f(1,1,1,1);
00225                         GLBoxRenderer.renderFrame(w,h,w);*/
00226                 }
00227         }
00228 
00229 }
00230 

Generated on Tue Apr 7 17:57:22 2009 for visclient by  doxygen 1.5.1