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

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

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

public class DragRoads extends WindowController {
    
    // the most recently-drawn road segment
    private RoadSegment lastSegment;
    
    // last mouse point and boolean flag to support dragging
    private boolean dragging = false;
    private Location lastMouse;
    
    /** 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) {
    
        if ((lastSegment != null) && lastSegment.contains(point)) {
            dragging = true;
            lastMouse = point;
        }
        else {
            lastSegment = new RoadSegment(point, canvas);
            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) {
            lastSegment.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) {
            lastSegment.move(point.getX() - lastMouse.getX(),
                             point.getY() - lastMouse.getY());
            dragging = false;      
        }
    }  
 }
