package net.sf.arianne.marboard.client.gui.drawingtool;
import java.awt.event.MouseEvent;
import marauroa.common.game.RPAction;
import marauroa.common.game.RPObject;
import net.sf.arianne.marboard.client.core.MarboardClientFramework;
import net.sf.arianne.marboard.client.entity.shape.Oval;
import net.sf.arianne.marboard.client.entity.shape.Shape;
import net.sf.arianne.marboard.client.gui.BoardState;
import net.sf.arianne.marboard.client.gui.DrawingTool;
import net.sf.arianne.marboard.client.gui.component.DrawingArea;
/**
* a drawing tool for ovals
*
* @author madmetzger
*/
public class OvalDrawingTool implements DrawingTool {
private boolean firstClick = true;
private int startX;
private int startY;
private DrawingArea board;
/**
*creates a new CircleDrawingTool
*
* @param board DrawingArea
*/
public OvalDrawingTool(DrawingArea board) {
super();
this.board = board;
}
/**
* handles mouse events. On the first click the position is remembered as one corner of a surrounding rectangle.
* and on the second click the oval is drawn within the rectangle created by both clicks as in the rectangle drawing
*
* @param boardState state of the board
* @param event MouseEvent
*/
public void handleMouseEvent(BoardState boardState, MouseEvent event) {
if (firstClick) {
startX = event.getX();
startY = event.getY();
firstClick = false;
} else {
handleSecondEvent(boardState, event);
}
}
/**
* tells the server to draw the oval. The selected color,
* and thickness are used.
*
* @param boardState state of the board
* @param event MouseEvent
*/
private void handleSecondEvent(BoardState boardState, MouseEvent event) {
RPAction action = new RPAction();
action.put("type", "create_oval_action");
action.put("x", startX);
action.put("y", startY);
action.put("x2", event.getX());
action.put("y2", event.getY());
action.put("color", boardState.getColor().getRGB());
action.put("fill_color", boardState.getFillColor().getRGB());
action.put("thickness", boardState.getThickness());
MarboardClientFramework.executeAction(action);
board.undrawTemporaryShape();
reset();
}
/**
* resets the tool
*/
public void reset() {
firstClick = true;
}
/**
* draws a preview version of the circe when the mouse is moved
* after the first click. The preview circle is drawn using xor so that
* simply drawing it again will delete it.
*
* @param boardState state of the board
* @param event MouseEvent
*/
public void handleMouseMovement(BoardState boardState, MouseEvent event) {
if(!firstClick) {
RPObject rpobject = new RPObject();
rpobject.put("color", boardState.getColor().getRGB());
rpobject.put("fill_color", boardState.getFillColor().getRGB());
rpobject.put("thickness", boardState.getThickness());
rpobject.put("x", Math.min(startX, event.getX()));
rpobject.put("y", Math.min(startY, event.getY()));
rpobject.put("x2", Math.max(startX, event.getX()));
rpobject.put("y2", Math.max(startY, event.getY()));
Shape line = new Oval(rpobject);
board.drawTemporaryShape(line);
}
}
}