package com.xith.client.console;

/**
 * $Id: TestConsole.java,v 1.2 2000/11/11 19:54:12 wizofid Exp $
 *
 * $Log: TestConsole.java,v $
 * Revision 1.2  2000/11/11 19:54:12  wizofid
 * New chat box implementation
 *
 * Revision 1.1  2000/10/08 21:32:11  entropy
 * Initial checkin
 *
 * Revision 1.4  2000/09/23 05:19:42  dyazel
 * Continuing the integration with client/server code
 *
 * Revision 1.3  2000/09/21 05:28:52  dyazel
 * Updates on 9/21/00 12:30 am
 *
 * Revision 1.2  2000/09/17 19:56:15  dyazel
 * Updating ID and LOG tags
 *
 */

import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.*;
import java.awt.image.*;
import com.xith.client.universe.*;
import com.xith.client.engine.*;
import com.xith.client.global.*;
import com.xith.utility.logs.*;
import com.xith.java3d.geometry.shape.*;

public class TestConsole {

   double consoleWidth;
   double consoleHeight;
   double consoleZ = 1.2;
   double heightOverWidth;
   double scale;
   Dimension dim;

   public FlatPlane plane;

   private TransformGroup consoleTG;
   private BranchGroup consoleBG;

   public Transform3D planeOffset;
   public Transform3D worldTransform;

   public void setWorldTransform( Transform3D transform ) {

      worldTransform = new Transform3D(transform);
      worldTransform.mul(planeOffset);
      consoleTG.setTransform(worldTransform);

   }

   public double scaleX ( double x ) {
      return x;
//      return (x*consoleWidth/(double)dim.width);
   }

   public double scaleY( double y ) {
      return y;
//      return (y*consoleHeight/(double)dim.height);

   }

   public BranchGroup getRoot() {
      return consoleBG;
   }

   private void makePlatformGeometry() {

      consoleBG = new BranchGroup();
      consoleTG = new TransformGroup();
      consoleTG.setCapability(consoleTG.ALLOW_TRANSFORM_WRITE);
      consoleBG.addChild(consoleTG);
      consoleTG.setTransform(new Transform3D());

      double fov = (double)XithEngine.universe.getView().getFieldOfView();
      consoleWidth = ((double)java.lang.Math.tan (fov / 2.0) * consoleZ)*2.0 ;

//      Screen3D screen = XithGlobal.canvas.getScreen3D();
      dim = XithEngine.window.getSize();
      scale = consoleWidth / (double)dim.width;
      consoleHeight = dim.height * scale;
      heightOverWidth = (double)dim.height / (double)dim.width;

      Log.log.println(LogType.EXHAUSTIVE,"screen width = "+dim.width);
      Log.log.println(LogType.EXHAUSTIVE,"screen height = "+dim.height);
      Log.log.println(LogType.EXHAUSTIVE,"console width = "+consoleWidth);
      Log.log.println(LogType.EXHAUSTIVE,"console height = "+consoleHeight);
      Log.log.println(LogType.EXHAUSTIVE,"screen scale = "+scale);

      // build the plane offset

      planeOffset = new Transform3D();
      planeOffset.setTranslation(new Vector3d(-consoleWidth/2, -consoleHeight/2, (double)-consoleZ));
      planeOffset.setScale(scale);

      Log.log.println(LogType.DEBUG,"Console width is "+consoleWidth);

   }


   private Shape3D getFlatBox( double xScreen, double yScreen, double widthScreen, double heightScreen,
                               float texX, float texY ) {

      double width = scaleX(widthScreen);
      double height = scaleY(heightScreen);
      double lx = scaleX(xScreen);
      double ly = scaleY(yScreen);

      double hx = scaleX(xScreen+widthScreen);
      double hy = scaleY(yScreen+heightScreen);

      double[] verts = {
       hx,  ly,  0.0,
       hx,  hy,  0.0,
       lx,  hy,  0.0,
       lx,  ly,  0.0};

      float[] colors = {
       0.0f, 0.0f, 0.0f,
       0.0f, 0.0f, 0.0f,
       0.0f, 0.0f, 0.0f,
       0.0f, 0.0f, 0.0f};

      float[] tex = {
       texX, 0.0f,
       texX, texY,
       0.0f, texY,
       0.0f, 0.0f, };


       QuadArray planeGeom = new QuadArray(4, QuadArray.COORDINATES |
           QuadArray.COLOR_3 | QuadArray.TEXTURE_COORDINATE_2);

       planeGeom.setCoordinates(0, verts);
       planeGeom.setColors(0, colors);
      planeGeom.setTextureCoordinates(0,tex);

      Shape3D plane = new Shape3D();
       plane.setGeometry(planeGeom);
      return plane;


   }

   private void addTestGraphic() {

        Dimension windowSize=new Dimension(600,150);

        Dimension texSize=new Dimension(1024,256);

        int offsetY = texSize.height - windowSize.height;
        int charHeight = 15;
        int borderPad = 3;
        int indent = 10;

        // create a buffer with an alpha channel
        BufferedImage buffer=new BufferedImage(texSize.width,texSize.height,BufferedImage.TYPE_INT_ARGB);

        Graphics g=buffer.getGraphics();
        g.setFont(new Font("Dialog",Font.PLAIN,12));

        // set everything to 20 percent opaque
        g.setColor(new Color(0.0f,0.0f,0.0f,0.2f));
        g.fillRect(0,0,texSize.width,texSize.height);

        // draw a border
        g.setColor(Color.black);
        g.drawRect(0,offsetY+1,windowSize.width-1,windowSize.height-2);
        g.setColor(Color.yellow);

        g.setColor(Color.yellow);
        g.drawString("System Message: Welcome to the new world!",indent,offsetY+borderPad+charHeight*2);

        g.setColor(Color.white);
//        g.drawString("Dilvish: Where is everyone?",indent,offsetY+borderPad+charHeight*3);

        g.dispose();

        // now we are done creating the bitmap we need to make the texture
        Texture texture=new Texture2D(Texture2D.BASE_LEVEL,Texture2D.RGBA,texSize.width,texSize.height);
        texture.setImage(0,new ImageComponent2D(ImageComponent2D.FORMAT_RGBA,buffer));

        TransparencyAttributes ta = new TransparencyAttributes(TransparencyAttributes.BLENDED,1.0f);
        ta.setTransparencyMode(ta.BLENDED);

        RenderingAttributes ra  = new RenderingAttributes();
        ra.setIgnoreVertexColors(true);

        TextureAttributes texa = new TextureAttributes();
        texa.setTextureMode(texa.REPLACE);
        texa.setTextureBlendColor(new Color4f(0,0,0,1));

        Appearance app=new Appearance();
        app.setTexture(texture);
        app.setTransparencyAttributes(ta);
        app.setTextureAttributes(texa);
        app.setRenderingAttributes(ra);

        // calculate the texCoords

        float texX = (float) windowSize.width / (float) texSize.width;
        float texY = (float) windowSize.height / (float) texSize.height;

        Shape3D box = getFlatBox( 20,20, windowSize.width, windowSize.height, texX, texY);
        box.setAppearance(app);
        consoleTG.addChild(box);
   }

   public TestConsole() {

      makePlatformGeometry();
      addTestGraphic();

   }

}
