Package syn3d.nodes.xith3d

Source Code of syn3d.nodes.xith3d.ShapeNodeXith3D$XithPropertiesPanel

/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2004, by :
*     Corporate:
*         EADS Corporate Research Center
*     Individual:
*         Nicolas Brodu
*
* $Id: ShapeNodeXith3D.java,v 1.9 2004/05/05 12:22:13 brodu Exp $
*
* Changes
* -------
* 19-Mar-2004 : Creation date (NB);
*
*/
package syn3d.nodes.xith3d;

import java.awt.Color;

import syn3d.base.ActiveNode;
import syn3d.nodes.ShapeNode;

import com.xith3d.scenegraph.Appearance;
import com.xith3d.scenegraph.ColoringAttributes;
import com.xith3d.scenegraph.Group;
import com.xith3d.scenegraph.Material;
import com.xith3d.scenegraph.PolygonAttributes;
import com.xith3d.scenegraph.Shape3D;

/**
* Base class for Xith3D shapes. Make cube and sphere from this, useful for plugins too
*/
public class ShapeNodeXith3D extends ShapeNode {

    protected Shape3D shape;
   
    public ShapeNodeXith3D(ActiveNode parent) {
        this(parent,null);
    }
   
    public ShapeNodeXith3D(ActiveNode parent, Shape3D shape3d) {
        super(parent);
        shape = shape3d;
        if (shape==null) shape = createShape();
        shape.setUserData(this);
        shape.setPickable(true);
        setAppearanceForHighlight(false);
        name = shape.getName();
        Object o = getParent().get3DObject();
        if (o instanceof Group) {
            ((Group)o).addChild(shape);
        }
        //mode = super.SHADING_MODE;
    }

    /**
     * Subclasses API when using the constructor without a shape specified
     * @return a newly created Shape3D.
     */
    protected Shape3D createShape() {
        return null;
    }

    public void remove() {
        if (shape!=null) {
            Object o = shape.getParent();
            if (o instanceof Group) {
                ((Group)o).removeChild(shape);
            }
        }
        super.remove();
    }
   
    public void setName(String name) {
        super.setName(name);
        if (shape!=null) shape.setName(name);
    }

    public Object get3DObject() {
        return shape;
    }

    protected void setAppearanceForHighlight(boolean on) {
        if (shape==null) return;
       
    Appearance a=new Appearance();
    ColoringAttributes ca = new ColoringAttributes();
    // don't use light
    if ((mode==WIREFRAME_MODE) || (mode==FILLED_MODE)) {
      int attribute = (mode==FILLED_MODE) ? PolygonAttributes.POLYGON_FILL : PolygonAttributes.POLYGON_LINE;
        a.setPolygonAttributes(new PolygonAttributes(attribute, PolygonAttributes.CULL_NONE, 0));
        Color selectedColor;
      if (on) selectedColor = highlightColor;
      else selectedColor = baseColor;
      float[] colors = selectedColor.getRGBColorComponents(null);
      ca.setColor(colors[0], colors[1], colors[2]);
      a.setColoringAttributes(ca);
    }
    // use light
    if ((mode==SHADING_MODE) || (mode==FLAT_MODE)) {
      if (mode==SHADING_MODE) ca.setShadeModel(ColoringAttributes.SHADE_GOURAUD);
      if (mode==FLAT_MODE) ca.setShadeModel(ColoringAttributes.SHADE_FLAT);
      a.setColoringAttributes(ca);
        a.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0));
        Material m = new Material();
        Color selectedColor;
      if (on) selectedColor = highlightColor;
      else selectedColor = baseColor;
       
      float[] colors = selectedColor.getRGBColorComponents(null);
      m.setAmbientColor(colors[0], colors[1], colors[2]);
     
      // Squash specular toward white => keep only hue
      float[] hsb = Color.RGBtoHSB(selectedColor.getRed(), selectedColor.getGreen(), selectedColor.getBlue(), null);
      Color.getHSBColor(hsb[0],hsb[1]/10,1.0f).getRGBColorComponents(colors);
      m.setSpecularColor(colors[0], colors[1], colors[2]);

      // diffuse is same hue and saturation but darker
      Color.getHSBColor(hsb[0],hsb[1],hsb[2]/2).getRGBColorComponents(colors);
      m.setDiffuseColor(colors[0], colors[1], colors[2]);
     
      // no emissive color
      m.setEmissiveColor(0f,0f,0f);
     
/*      m.setAmbientColor(1.0f,0.0f,0.0f);
          m.setSpecularColor(1.0f,0.9f,0.9f);
          m.setDiffuseColor(0.8f,0.0f,0.0f);
          m.setEmissiveColor(0f,0f,0f);*/
       
          m.setLightingEnable(true);
          a.setMaterial(m);
    }
        shape.setAppearance(a);
    }
   
    public void highlight(boolean on, Object parameter) {
        setAppearanceForHighlight(on);
        super.highlight(on,parameter);
    }
   
    protected PropertiesPanel createPanel(boolean showName) {
        return new XithPropertiesPanel(showName);
    }
   
    protected class XithPropertiesPanel extends PropertiesPanel {

      public XithPropertiesPanel(boolean showName) {
      super(showName);
    }

    public void apply() {
            super.apply();
            // change display to take mode into account
            ShapeNodeXith3D.this.notifyInternalChange();
        }
    }
   
  /* (non-Javadoc)
   * @see syn3d.base.ActiveNode#notifyInternalChange()
   */
  public void notifyInternalChange() {
        setAppearanceForHighlight(highlighted);
    super.notifyInternalChange();
  }
}
TOP

Related Classes of syn3d.nodes.xith3d.ShapeNodeXith3D$XithPropertiesPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.