view java/src/uk/ac/qmul/eecs/ccmi/pdsupport/PdObject.java @ 1:e3935c01cde2 tip

moved license of PdPersistenceManager to the beginning of the file
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 19:52:03 +0100
parents 78b7fc5391a2
children
line wrap: on
line source
/*  
 accessPD - An accessible PD patches editor
  
 Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) 
 Copyright (C) 2014  Fiore Martin

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 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 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/   
package uk.ac.qmul.eecs.ccmi.pdsupport;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.InputStream;

import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties;
import uk.ac.qmul.eecs.ccmi.gui.Direction;
import uk.ac.qmul.eecs.ccmi.gui.Node;
import uk.ac.qmul.eecs.ccmi.simpletemplate.MultiLineString;
import uk.ac.qmul.eecs.ccmi.sound.SoundFactory;

public class PdObject extends Node implements  PdElement {
	private static final long serialVersionUID = 1L;
	private static final int DEFAULT_WIDTH = 100;
    private static final int DEFAULT_HEIGHT = 60;
	private static final Rectangle2D.Double minBounds = new Rectangle2D.Double(0,0,DEFAULT_WIDTH,DEFAULT_HEIGHT);
	
	
	private Rectangle2D.Double bounds;
	private static InputStream sound;
	private MultiLineString label;
	private int orderNumber;
	
	static{
		sound = PdObject.class.getResourceAsStream("audio/PdObject.mp3");
		SoundFactory.getInstance().loadSound(sound);
	}

	public PdObject(String type) {
		super(type, NodeProperties.NULL_PROPERTIES);
		bounds = new Rectangle2D.Double(0,0,DEFAULT_WIDTH,DEFAULT_HEIGHT);
		label = new MultiLineString();
		label.setText(getType());
	}

	public PdObject() {
		this("Object");
	}
	
	@Override
	protected void translateImplementation(Point2D p, double dx, double dy) {
        bounds.setFrame(bounds.getX() + dx,
                bounds.getY() + dy, 
                bounds.getWidth(), 
                bounds.getHeight());

	}

	@Override
	public boolean contains(Point2D aPoint) {
		return bounds.contains(aPoint);
	}

	@Override
	public Rectangle2D getBounds() {
		return bounds.getBounds2D();
	}

	@Override
	public Point2D getConnectionPoint(Direction d) {
		double slope = bounds.getHeight() / bounds.getWidth();
		double ex = d.getX();
		double ey = d.getY();
		double x = bounds.getCenterX();
		double y = bounds.getCenterY();

		if (ex != 0 && -slope <= ey / ex && ey / ex <= slope){  
			// intersects at left or right boundary
			if (ex > 0){
				x = bounds.getMaxX();
				y += (bounds.getWidth() / 2) * ey / ex;
			}else{
				x = bounds.getX();
				y -= (bounds.getWidth() / 2) * ey / ex;
			}
		}else if (ey != 0){  
			// intersects at top or bottom
			if (ey > 0){
				x += (bounds.getHeight() / 2) * ex / ey;
				y = bounds.getMaxY();
			}else{
				x -= (bounds.getHeight() / 2) * ex / ey;
				y = bounds.getY();
			}
		}

		return new Point2D.Double(x, y);

	}

	@Override
	public Shape getShape() {
		return getBounds();
	}

	@Override
	public InputStream getSound() {
		return sound;
	}
	
	@Override
	public void draw(Graphics2D g2d){
		Color oldColor = g2d.getColor();
		g2d.setColor(Color.WHITE);
		g2d.fill(getBounds());
		
		g2d.setColor(oldColor);
		
		label.draw(g2d, getBounds());
		g2d.draw(bounds);
	}
	
	@Override
	public void setName(String name, Object source){		
		label.setText(name);
		super.setName(name, source);
	}
	
	@Override
	public void setId(long id){
		super.setId(id);
		
		label.setText(getName());
		/* when they are given an id nodes change name into "new <type> node <id>" *
		 * where <type> is the actual type of the node and <id> is the given id    *
		 * therefore a reshape is necessary to display the new name                */
		Rectangle2D boundsBeforeReshape = getBounds();

		bounds = calculateBounds(label.getBounds());
		
		/* the reshape might change the bounds, so the shape is translated so that the top-left  *
		 * point is at the same position as before just to keep it more consistent               */
		Rectangle2D boundsAfterReshape = getBounds();
		translateImplementation(
			new Point2D.Double(),
			boundsBeforeReshape.getX() - boundsAfterReshape.getX(),
			boundsBeforeReshape.getY() - boundsAfterReshape.getY()
		);
		
		
	}
	
	protected Rectangle2D.Double getMinBounds(){
		return minBounds;
	}
	
	private Rectangle2D.Double calculateBounds(Rectangle2D bounds){
		return new Rectangle2D.Double(bounds.getX() , bounds.getY(), 
				Math.max(bounds.getWidth(), getMinBounds().getWidth()),
				Math.max(bounds.getHeight(), getMinBounds().getHeight())
				);
	}
	
	@Override
	public Object clone(){
		PdObject clone = (PdObject)super.clone();
		
		clone.bounds = new Rectangle2D.Double(0,0,DEFAULT_WIDTH,DEFAULT_HEIGHT);
		clone.label = new MultiLineString();
		clone.label.setText(getType());
		
		return clone;
	}
	
	public String toPdFile(){
		Rectangle2D bounds  = getBounds();
		return getChunckType() + " obj " + ((int)bounds.getX()) +
				' ' + ((int)bounds.getY()) + ' ' + getName().replaceAll("\\s+","");
	}

	@Override
	public String getChunckType() {
		return "#X";
	}

	public void setOrderNumber(int n){
		orderNumber = n;
	}
	
	@Override
	public int getOrderNumber(){
		return orderNumber;
	}
}