package com._3dmathpuzzles.gwt.client;

import com.smartgwt.client.widgets.drawing.DrawPane;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;

public class ZoomInClickHandler implements ClickHandler {
  /** The zoom factor */
  public static final double ZOOM_FACTOR = 1.5;
  
  /** The draw pane we are managing */
  private DrawPane drawPane;
  
  /** Constructor */
  public ZoomInClickHandler(DrawPane drawPane) {
    this.drawPane = drawPane;
  }
  
  /** Process a click */
  @Override
  public void onClick(ClickEvent event) {
    double oldZoom = drawPane.getZoomLevelAsDouble();
    double newZoom = oldZoom * ZOOM_FACTOR;
    double oldLeft = drawPane.getScrollLeft();
    double oldTop = drawPane.getScrollTop();
    int newLeft = (int)(oldLeft * ZOOM_FACTOR + drawPane.getWidth()/ZOOM_FACTOR);
    int newTop = (int)(oldTop * ZOOM_FACTOR + drawPane.getHeight()/ZOOM_FACTOR);
    drawPane.setZoomLevel(newZoom);
    drawPane.scrollTo(newLeft,newTop);
  }
}