/*
 * @(#)TweakableShape.java    1.0  24 August 2003
 *
 * Copyright 2004
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.gui;

import edu.neu.ccs.util.*;

import java.awt.*;
import java.awt.geom.*;
import java.beans.*;

/**
 * <P>Class <CODE>TweakableShape</CODE> implements a <CODE>BaseShape</CODE>
 * in which the vertex array and the tangent array may be changed
 * independently of one another.</P>
 *
 * <P>To define a shape in which the tangent array is computed from the
 * vertex array automatically, use the class <CODE>AutomaticShape</CODE>.</P>
 *
 * @see Path
 * @see AutomaticShape
 * @author  Richard Rasala
 * @version 2.3
 * @since   2.3
 */
public class TweakableShape extends BaseShape {

    /** Bound property name to set the vertex data. */
    public static final String SET_VERTEX_DATA          = "set.vertex.data";
    
    /** Bound property name to set the tangent data. */
    public static final String SET_TANGENT_DATA         = "set.tangent.data";
    
    /** Bound property name to set the vertex/tangent data. */
    public static final String SET_VERTEX_TANGENT_DATA  = "set.vertex.tangent.data";
    
    /** Bound property name to set one vertex. */
    public static final String SET_VERTEX               = "set.vertex";
    
    /** Bound property name to set one tangent. */
    public static final String SET_TANGENT              = "set.tangent";
    
    /** Bound property name to set one vertex/tangent pair. */
    public static final String SET_VERTEX_TANGENT       = "set.vertex.tangent";
    
    /** Bound property name to add one vertex/tangent pair. */
    public static final String ADD_VERTEX_TANGENT       = "add.vertex.tangent";
    
    /** Bound property name to remove one vertex/tangent pair. */
    public static final String REMOVE_VERTEX_TANGENT    = "remove.vertex.tangent";
    
    
    /**
     * <P>The default constructor with an empty shape.</P>
     *
     * <P>The default settings are as follows:</P>
     *
     * <UL>
     *   <LI>Vertex data:      <CODE>new float[0][2]</CODE></LI>
     *   <LI>Tangent data:     <CODE>new float[0][2]</CODE></LI>
     *   <LI>Path strategy:    <CODE>Path.POLYGON</CODE></LI>
     *   <LI>Closure mode:     <CODE>ClosureMode.CLOSED</CODE></LI>
     *   <LI>Winding rule:     <CODE>WindingRule.WIND_NON_ZERO</CODE>.</LI>
     * </UL>
     *
     * @see #TweakableShape(float[][])
     * @see #TweakableShape(float[][], float[][])
     * @see #TweakableShape(float[][], float[][], Path.Strategy)
     * @see #TweakableShape(float[][], float[][], Path.Strategy, ClosureMode)
     * @see #TweakableShape(float[][], float[][], Path.Strategy, ClosureMode, WindingRule)
     * @see #setVertexTangentData(float[][], float[][])
     */
    public TweakableShape() {
        this(null, null, null, null, null);
    }
    
    
    /**
     * <P>The constructor with the given vertex data.</P>
     *
     * <P>Precondition:</P>
     * <UL>
     *   <LI>For some integer N, the given vertex array is
     *          float[N][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     * 
     * @param vertex the vertex data
     * @see #TweakableShape()
     * @see #TweakableShape(float[][], float[][])
     * @see #TweakableShape(float[][], float[][], Path.Strategy)
     * @see #TweakableShape(float[][], float[][], Path.Strategy, ClosureMode)
     * @see #TweakableShape(float[][], float[][], Path.Strategy, ClosureMode, WindingRule)
     * @see #setVertexTangentData(float[][], float[][])
     */
    public TweakableShape(float[][] vertex) {
        this(vertex, null, null, null, null);
    }
    
    
    /**
     * <P>The constructor with the given vertex and tangent data.</P>
     *
     * <P>Precondition 1:</P>
     * <UL>
     *   <LI>For some integer N, the given vertex array is
     *          float[N][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     * 
     * <P>Precondition 2:</P>
     * <UL>
     *   <LI>The given tangent array is
     *          either <CODE>null</CODE> or
     *          float[N][2] with non-<CODE>null</CODE> entries
     *          for the same N as in the vertex array.</LI>
     * </UL>
     *
     * <P>If tangent is <CODE>null</CODE>
     * then an array of zero tangents is supplied.</P>
     *
     * @param vertex  the vertex  data
     * @param tangent the tangent data
     * @see #TweakableShape()
     * @see #TweakableShape(float[][])
     * @see #TweakableShape(float[][], float[][], Path.Strategy)
     * @see #TweakableShape(float[][], float[][], Path.Strategy, ClosureMode)
     * @see #TweakableShape(float[][], float[][], Path.Strategy, ClosureMode, WindingRule)
     * @see #setVertexTangentData(float[][], float[][])
     */
    public TweakableShape(float[][] vertex, float[][] tangent) {
        this(vertex, tangent, null, null, null);
    }
    
    
    /**
     * <P>The constructor with the given vertex and tangent data
     * and the given path strategy.</P>
     *
     * <P>Precondition 1:</P>
     * <UL>
     *   <LI>For some integer N, the given vertex array is
     *          float[N][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     * 
     * <P>Precondition 2:</P>
     * <UL>
     *   <LI>The given tangent array is
     *          either <CODE>null</CODE> or
     *          float[N][2] with non-<CODE>null</CODE> entries
     *          for the same N as in the vertex array.</LI>
     * </UL>
     *
     * <P>If tangent is <CODE>null</CODE>
     * then an array of zero tangents is supplied.</P>
     *
     * @param vertex       the vertex  data
     * @param tangent      the tangent data
     * @param pathstrategy the path strategy
     * @see #TweakableShape()
     * @see #TweakableShape(float[][])
     * @see #TweakableShape(float[][], float[][])
     * @see #TweakableShape(float[][], float[][], Path.Strategy, ClosureMode)
     * @see #TweakableShape(float[][], float[][], Path.Strategy, ClosureMode, WindingRule)
     * @see #setVertexTangentData(float[][], float[][])
     */
    public TweakableShape
        (float[][]     vertex,
         float[][]     tangent,
         Path.Strategy pathstrategy)
    {
        this(vertex, tangent, pathstrategy, null, null);
    }
    
    
    /**
     * <P>The constructor with the given vertex and tangent data,
     * the given path strategy,
     * and the given closure mode.</P>
     *
     * <P>Precondition 1:</P>
     * <UL>
     *   <LI>For some integer N, the given vertex array is
     *          float[N][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     * 
     * <P>Precondition 2:</P>
     * <UL>
     *   <LI>The given tangent array is
     *          either <CODE>null</CODE> or
     *          float[N][2] with non-<CODE>null</CODE> entries
     *          for the same N as in the vertex array.</LI>
     * </UL>
     *
     * <P>If tangent is <CODE>null</CODE>
     * then an array of zero tangents is supplied.</P>
     *
     * @param vertex       the vertex  data
     * @param tangent      the tangent data
     * @param pathstrategy the path strategy
     * @param closuremode  the closure mode
     * @see #TweakableShape()
     * @see #TweakableShape(float[][])
     * @see #TweakableShape(float[][], float[][])
     * @see #TweakableShape(float[][], float[][], Path.Strategy)
     * @see #TweakableShape(float[][], float[][], Path.Strategy, ClosureMode, WindingRule)
     * @see #setVertexTangentData(float[][], float[][])
     */
    public TweakableShape
        (float[][]     vertex,
         float[][]     tangent,
         Path.Strategy pathstrategy,
         ClosureMode   closuremode)
    {
        this(vertex, tangent, pathstrategy, closuremode, null);
    }
    
    
    /**
     * <P>The constructor with the given vertex and tangent data,
     * the given path strategy,
     * the given closure mode,
     * and the given winding rule.</P>
     *
     * <P>Precondition 1:</P>
     * <UL>
     *   <LI>For some integer N, the given vertex array is
     *          float[N][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     * 
     * <P>Precondition 2:</P>
     * <UL>
     *   <LI>The given tangent array is
     *          either <CODE>null</CODE> or
     *          float[N][2] with non-<CODE>null</CODE> entries
     *          for the same N as in the vertex array.</LI>
     * </UL>
     *
     * <P>If tangent is <CODE>null</CODE>
     * then an array of zero tangents is supplied.</P>
     *
     * @param vertex       the vertex  data
     * @param tangent      the tangent data
     * @param pathstrategy the path strategy
     * @param closuremode  the closure mode
     * @param windingrule  the winding rule
     * @see #TweakableShape()
     * @see #TweakableShape(float[][])
     * @see #TweakableShape(float[][], float[][])
     * @see #TweakableShape(float[][], float[][], Path.Strategy)
     * @see #TweakableShape(float[][], float[][], Path.Strategy, ClosureMode)
     * @see #setVertexTangentData(float[][], float[][])
     */
    public TweakableShape
        (float[][]     vertex,
         float[][]     tangent,
         Path.Strategy pathstrategy,
         ClosureMode   closuremode,
         WindingRule   windingrule)
    {
        setPathStrategy(pathstrategy);
        setClosureMode(closuremode);
        setWindingRule(windingrule);
        setVertexTangentData(vertex, tangent);
    }
    
    
    /**
     * <P>Sets the vertex data of the shape using a 2-dimensional array
     * of single precision numbers.</P>
     *
     * <P>Does not change the tangent data.</P>
     *
     * <P>Precondition:</P>
     * <UL>
     *   <LI>The given vertex array is
     *          float[N][2] with non-<CODE>null</CODE> entries
     *          for the same N as in the current tangent array.</LI>
     * </UL>
     *
     * <P>Does nothing if the preconditions fail.</P>
     *
     * <P>Fires property change: SET_VERTEX_DATA.</P>
     *
     * @param vertex the vertex data
     * @see #setTangentData(float[][])
     * @see #setVertexTangentData(float[][], float[][])
     */
    public final void setVertexData(float[][] vertex) {
        if (! (FloatArray.checkArray(vertex, 2) && (vertex.length == tangent.length)))
            return;
        
        if (FloatArray.equals(this.vertex, vertex))
            return;
        
        this.vertex = FloatArray.deepclone(vertex);
        
        makePath();
        
        firePropertyChange(SET_VERTEX_DATA, null, null);
    }
    
    
    /**
     * <P>Sets the tangent data of the shape using a 2-dimensional array
     * of single precision numbers.</P>
     *
     * <P>Does not change the vertex data.</P>
     *
     * <P>Precondition:</P>
     * <UL>
     *   <LI>The given tangent array is
     *          either <CODE>null</CODE> or
     *          float[N][2] with non-<CODE>null</CODE> entries
     *          for the same N as in the current vertex array.</LI>
     * </UL>
     *
     * <P>If tangent is <CODE>null</CODE>
     * then an array of zero tangents is supplied.</P>
     *
     * <P>Does nothing if the preconditions fail.</P>
     *
     * <P>Fires property change: SET_TANGENT_DATA.</P>
     *
     * @param tangent the tangent data
     * @see #setVertexData(float[][])
     * @see #setVertexTangentData(float[][], float[][])
     */
    public final void setTangentData(float[][] tangent) {
        if (tangent == null) {
            tangent = new float[vertex.length][2];
        }
        else {
            if (! (FloatArray.checkArray(tangent, 2) && (tangent.length == vertex.length)))
                return;
        }
        
        if (FloatArray.equals(this.tangent, tangent))
            return;
        
        this.tangent = FloatArray.deepclone(tangent);
        
        makePath();
        
        firePropertyChange(SET_TANGENT_DATA, null, null);
    }
    
    
    /**
     * <P>Sets the vertex and tangent data of the shape using
     * two 2-dimensional arrays of single precision numbers.</P>
     *
     * <P>Precondition 1:</P>
     * <UL>
     *   <LI>For some integer N, the given vertex array is
     *          float[N][2] with non-<CODE>null</CODE> entries.</LI>
     * </UL>
     * 
     * <P>Precondition 2:</P>
     * <UL>
     *   <LI>The given tangent array is
     *          either <CODE>null</CODE> or
     *          float[N][2] with non-<CODE>null</CODE> entries
     *          for the same N as in the vertex array.</LI>
     * </UL>
     *
     * <P>If tangent is <CODE>null</CODE>
     * then an array of zero tangents is supplied.</P>
     *
     * <P>Does nothing if the preconditions fail.</P>
     *
     * <P>Fires property change: SET_VERTEX_TANGENT_DATA.</P>
     *
     * @param vertex  the vertex data
     * @param tangent the tangent data
     * @see #setVertexData(float[][])
     * @see #setTangentData(float[][])
     */
    public final void setVertexTangentData(float[][] vertex, float[][] tangent) {
        if (! FloatArray.checkArray(vertex, 2))
            return;
        
        if (tangent == null) {
            tangent = new float[vertex.length][2];
        }
        else {
            if (! (FloatArray.checkArray(tangent, 2) && (tangent.length == vertex.length)))
                return;
        }
        
        if (FloatArray.equals(this.vertex, vertex) &&
                FloatArray.equals(this.tangent, tangent))
            return;
        
        this.vertex  = FloatArray.deepclone(vertex);
        this.tangent = FloatArray.deepclone(tangent);
        
        makePath();
        
        firePropertyChange(SET_VERTEX_TANGENT_DATA, null, null);
    }
    
    
    /**
     * <P>Sets the vertex at the given index to the given point coordinates.</P>
     *
     * <P>Does nothing if the index is not in [0, length()).</P>
     *
     * <P>Does not change the tangent data.</P>
     *
     * <P>Fires property change: SET_VERTEX.</P>
     *
     * @param index the vertex index
     * @param x the new x-coordinate for the vertex
     * @param y the new y-coordinate for the vertex
     * @see #setVertex(int, float[])
     * @see #setTangent(int, float, float)
     * @see #setTangent(int, float[])
     * @see #setVertexTangent(int, float, float, float, float)
     * @see #setVertexTangent(int, float[])
     */
    public final void setVertex(int index, float x, float y) {
        if ((index < 0) || (index >= vertex.length))
            return;
        
        if ((vertex[index][0] == x) && (vertex[index][1] == y))
            return;
        
        vertex[index][0] = x;
        vertex[index][1] = y;
        
        makePath();
        
        firePropertyChange(SET_VERTEX, null, null);
    }
    
    
    /**
     * <P>Sets the vertex at the given index to the given point.</P>
     *
     * <P>Does nothing if the index is not in [0, length()).</P>
     *
     * <P>Does nothing if point is not float[2].</P>
     *
     * <P>Does not change the tangent data.</P>
     *
     * <P>Fires property change: SET_VERTEX.</P>
     *
     * @param index the vertex index
     * @param point the new position for the vertex
     * @see #setVertex(int, float, float)
     * @see #setTangent(int, float, float)
     * @see #setTangent(int, float[])
     * @see #setVertexTangent(int, float, float, float, float)
     * @see #setVertexTangent(int, float[])
     */
    public final void setVertex(int index, float[] point) {
        if ((point == null) || (point.length != 2))
            return;
        
        setVertex(index, point[0], point[1]);
    }
    
    
    /**
     * <P>Sets the tangent at the given index to the given displacement
     * coordinates.</P>
     *
     * <P>Does nothing if the index is not in [0, length()).</P>
     *
     * <P>Does not change the vertex data.</P>
     *
     * <P>Fires property change: SET_TANGENT.</P>
     *
     * @param index the tangent index
     * @param dx the new x-coordinate for the displacement
     * @param dy the new y-coordinate for the displacement
     * @see #setVertex(int, float, float)
     * @see #setVertex(int, float[])
     * @see #setTangent(int, float[])
     * @see #setVertexTangent(int, float, float, float, float)
     * @see #setVertexTangent(int, float[])
     */
    public final void setTangent(int index, float dx, float dy) {
        if ((index < 0) || (index >= tangent.length))
            return;
        
        if ((tangent[index][0] == dx) && (tangent[index][1] == dy))
            return;
        
        tangent[index][0] = dx;
        tangent[index][1] = dy;
        
        makePath();
        
        firePropertyChange(SET_TANGENT, null, null);
    }
    
    
    /**
     * <P>Sets the tangent at the given index to the given displacement.</P>
     *
     * <P>Does nothing if the index is not in [0, length()).</P>
     *
     * <P>Does nothing if delta is not float[2].</P>
     *
     * <P>Does not change the vertex data.</P>
     *
     * <P>Fires property change: SET_TANGENT.</P>
     *
     * @param index the tangent index
     * @param delta the new displacement for the tangent
     * @see #setVertex(int, float, float)
     * @see #setVertex(int, float[])
     * @see #setTangent(int, float, float)
     * @see #setVertexTangent(int, float, float, float, float)
     * @see #setVertexTangent(int, float[])
     */
    public final void setTangent(int index, float[] delta) {
        if ((delta == null) || (delta.length != 2))
            return;
        
        setTangent(index, delta[0], delta[1]);
    }
    
    
    /**
     * <P>Sets the vertex and tangent at the given index to the given point
     * and displacement coordinates.</P>
     *
     * <P>Does nothing if the index is not in [0, length()).</P>
     *
     * <P>Fires property change: SET_VERTEX_TANGENT.</P>
     *
     * @param index the vertex/tangent index
     * @param x  the new x-coordinate for the vertex
     * @param y  the new y-coordinate for the vertex
     * @param dx the new x-coordinate for the displacement
     * @param dy the new y-coordinate for the displacement
     * @see #setVertex(int, float, float)
     * @see #setVertex(int, float[])
     * @see #setTangent(int, float, float)
     * @see #setTangent(int, float[])
     * @see #setVertexTangent(int, float[])
     */
    public final void setVertexTangent(int index, float x, float y, float dx, float dy) {
        if ((index < 0) || (index >= vertex.length))
            return;
        
        if ((vertex[index][0] == x) && (vertex[index][1] == y) &&
                (tangent[index][0] == dx) && (tangent[index][1] == dy))
            return;
        
        vertex[index][0] = x;
        vertex[index][1] = y;
        
        tangent[index][0] = dx;
        tangent[index][1] = dy;
        
        makePath();
        
        firePropertyChange(SET_VERTEX_TANGENT, null, null);
    }
    
    
    /**
     * <P>Sets the vertex and tangent at the given index to the given set of
     * point and displacement coordinates represented as float[4] with the
     * four array locations filled with x, y, dx, dy.</P>
     *
     * <P>Does nothing if the index is not in [0, length()).</P>
     *
     * <P>Does nothing if array is not float[4].</P>
     *
     * <P>Fires property change: SET_VERTEX_TANGENT.</P>
     *
     * @param index the vertex/tangent index
     * @param array the array for the vertex/tangent with x, y, dx, dy
     * @see #setVertex(int, float, float)
     * @see #setVertex(int, float[])
     * @see #setTangent(int, float, float)
     * @see #setTangent(int, float[])
     * @see #setVertexTangent(int, float, float, float, float)
     */
    public final void setVertexTangent(int index, float[] array) {
        if ((array == null) || (array.length != 4))
            return;
        
        setVertexTangent(index, array[0], array[1], array[2], array[3]);
    }
    
    
    /**
     * <P>Adds a new vertex and tangent at the given index with the
     * given point and displacement coordinates x, y, dx, dy.</P>
     *
     * <P>Does nothing if the index is not in [0, length()].</P>
     *
     * <P>If index is 0, the insertion is at the front.</P>
     *
     * <P>If index is length(), the insertion is at the end.</P>
     *
     * <P>Fires property change: ADD_VERTEX_TANGENT.</P>
     *
     * @param index the vertex/tangent index
     * @param x  the x-coordinate for the vertex
     * @param y  the y-coordinate for the vertex
     * @param dx the x-coordinate for the tangent
     * @param dy the y-coordinate for the tangent
     * @see #addVertexTangent(int, float[])
     * @see #removeVertexTangent(int)
     */
    public final void addVertexTangent
        (int index, float x, float y, float dx, float dy)
    {
        int N = vertex.length;
        int M = N + 1;
        
        if ((index < 0) || (index > N))
            return;
        
        float[][] oldvertex  = vertex;
        float[][] oldtangent = tangent;
        
        vertex  = new float[M][2];
        tangent = new float[M][2];
        
        vertex [index][0] = x;
        vertex [index][1] = y;
        
        tangent[index][0] = dx;
        tangent[index][1] = dy;
        
        for (int i = 0; i < M; i++) {
            if (i < index) {
                vertex [i][0] = oldvertex [i][0];
                vertex [i][1] = oldvertex [i][1];
                
                tangent[i][0] = oldtangent[i][0];
                tangent[i][1] = oldtangent[i][1];
            }
            else if (i > index) {
                int j = i - 1;
                vertex [i][0] = oldvertex [j][0];
                vertex [i][1] = oldvertex [j][1];
                
                tangent[i][0] = oldtangent[j][0];
                tangent[i][1] = oldtangent[j][1];
            }
        }
        
        makePath();
        
        firePropertyChange(ADD_VERTEX_TANGENT, null, null);
    }
    
    
    /**
     * <P>Adds a new vertex and tangent at the given index to the given set
     * of point and displacement coordinates represented as float[4] with the
     * four array locations filled with x, y, dx, dy.</P>
     *
     * <P>Does nothing if the index is not in [0, length()].</P>
     *
     * <P>If index is 0, the insertion is at the front.</P>
     *
     * <P>If index is length(), the insertion is at the end.</P>
     *
     * <P>Does nothing if array is not float[4].</P>
     *
     * <P>Fires property change: ADD_VERTEX_TANGENT.</P>
     *
     * @param index the vertex index
     * @param array the array for the vertex/tangent with x, y, dx, dy
     * @see #addVertexTangent(int, float, float, float, float)
     * @see #removeVertexTangent(int)
     */
    public final void addVertexTangent(int index, float[] array) {
        if ((array == null) || (array.length != 4))
            return;
        
        addVertexTangent(index, array[0], array[1], array[2], array[3]);
    }
    
    
    /**
     * <P>Removes the vertex/tangent at the given index.</P>
     *
     * <P>Does nothing if the index is not in [0, length()).</P>
     *
     * <P>Fires property change: REMOVE_VERTEX_TANGENT.</P>
     *
     * @param index the vertex index
     * @see #addVertexTangent(int, float, float, float, float)
     * @see #addVertexTangent(int, float[])
     */
    public final void removeVertexTangent(int index) {
        int N = vertex.length;
        int M = N - 1;
        
        if ((index < 0) || (index >= N))
                return;
        
        float[][] oldvertex  = vertex;
        float[][] oldtangent = tangent;
        
        vertex  = new float[M][2];
        tangent = new float[M][2];
        
        for (int i = 0; i < N; i++) {
            if (i < index) {
                vertex [i][0] = oldvertex [i][0];
                vertex [i][1] = oldvertex [i][1];
                
                tangent[i][0] = oldtangent[i][0];
                tangent[i][1] = oldtangent[i][1];
            }
            else if (i > index) {
                int j = i - 1;
                vertex [j][0] = oldvertex [i][0];
                vertex [j][1] = oldvertex [i][1];
                
                tangent[j][0] = oldtangent[i][0];
                tangent[j][1] = oldtangent[i][1];
            }
            
        }
        
        makePath();
        
        firePropertyChange(REMOVE_VERTEX_TANGENT, null, null);
    }
    
}
