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.Shape;
import net.sf.arianne.marboard.client.entity.shape.StraightLine;
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 straight lines
*
* @author hendrik
*/
public class StraightLineDrawingTool implements DrawingTool {
private boolean firstClick = true;
private int startX;
private int startY;
private DrawingArea board;
/**
*creates a new StraightLineDrawingTool
*
* @param board DrawingArea
*/
public StraightLineDrawingTool(DrawingArea board) {
super();
this.board = board;
}
/**
* handles mouse events. On the first click the position is remembered
* and on the second click the line is drawn
*
* @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 line. 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_straight_line_action");
action.put("x", startX);
action.put("y", startY);
int directionX = event.getX();
int directionY = event.getY();
action.put("x2", directionX);
action.put("y2", directionY);
action.put("color", boardState.getColor().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 line when the mouse is moved
* after the first click. The preview line is drawn using xor so that
* simply drawing it again will delte 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("thickness", boardState.getThickness());
rpobject.put("x", startX);
rpobject.put("y", startY);
rpobject.put("x2", event.getX());
rpobject.put("y2", event.getY());
Shape line = new StraightLine(rpobject);
board.drawTemporaryShape(line);
}
}
}