import objectdraw.*;
import java.awt.*;

/* $Id: DragAllRoads.java 1577 2011-03-28 02:10:20Z terescoj $ */

/**
 * Example DragAllRoads: draw "road segments" at mouse
 * press points, but drag any existing segment instead
 * if it happens to contain the mouse press point.
 *
 * @author Jim Teresco
 * Siena College, CSIS 120, Fall 2011
 *
 */

public class DragAllRoads extends WindowController {
    
    // the road segments in our drawing
    private static final int INITIAL_ARRAY_SIZE = 4;
    private RoadSegment[] segments;
    private int numSegments;
    
    // the road segment currently being dragged, if any
    private RoadSegment selectedSegment;
    
    // last mouse point and boolean flag to support dragging
    private boolean dragging;
    private Location lastMouse;
    
    /**
     * Set up: just initialize the array of segments
     * and set other instance variables.
     */
    public void begin() {
    
        segments = new RoadSegment[INITIAL_ARRAY_SIZE];
        numSegments = 0;
        selectedSegment = null;
        dragging = false;
        lastMouse = null;
    }
    
    /** Draw a road segment at the mouse press point
     *  unless the most recent road drawn was at that
     *  point, in which case we begin a drag operation.
     * 
     *  @param point the Location of the mouse press
     */
    public void onMousePress(Location point) {
    
        // see if we've pressed the mouse on an existing road segment
        for (int segmentNum = 0; segmentNum < numSegments; segmentNum++) {
            
            if (segments[segmentNum].contains(point)) {
                selectedSegment = segments[segmentNum];
                dragging = true;
                lastMouse = point;
            }
        }
        
        // if dragging is still false, we create a new segment
        if (!dragging) {
            
            // need to check first if there is still room in our array
            // and if not, build a new one with more room.
            if (numSegments == segments.length) {
                // create a new array with twice as many slots
                RoadSegment[] newArray = new RoadSegment[segments.length*2];
                // copy over the entries from the old array to the new
                for (int segmentNum = 0; segmentNum < segments.length; segmentNum++) {
                    newArray[segmentNum] = segments[segmentNum];
                }
                // replace the old array with the new, larger one
                segments = newArray;
            }
            // create the new segment and insert it into the first
            // empty slot (which we now are sure exists).
            segments[numSegments] = new RoadSegment(point, canvas);
            numSegments++;
            dragging = false;
        }
    }
    
    /** Perform dragging operation if we are dragging anything
     * 
     *  @param point the Location of the mouse press
     */
    public void onMouseDrag(Location point) {
     
        if (dragging) {
            selectedSegment.move(point.getX() - lastMouse.getX(),
                                 point.getY() - lastMouse.getY());
            lastMouse = point;
        }
    }
    
    /** Complete dragging operation if we are dragging anything
     * 
     *  @param point the Location of the mouse press
     */
    public void onMouseRelease(Location point) {
     
        if (dragging) {
            selectedSegment.move(point.getX() - lastMouse.getX(),
                                 point.getY() - lastMouse.getY());
            dragging = false;      
        }
    }  
 }
