/* * Copyright (c) 2014 tabletoptool.com team. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl.html * * Contributors: * rptools.com team - initial implementation * tabletoptool.com team - further development */ package com.t3.client.ui.token; import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Composite; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.Shape; import java.awt.Stroke; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import com.t3.model.Token; import com.t3.xstreamversioned.version.SerializationVersion; /** * Paint a dot so that it doesn't overlay any other states being displayed in the same grid. * * @author Jay */ @SerializationVersion(0) public class FlowColorDotTokenOverlay extends XTokenOverlay { /** * Size of the grid used to place a token with this state. */ private int grid; /** * Flow used to define position of states */ private transient TokenOverlayFlow flow; /** * Default constructor needed for XML encoding/decoding */ public FlowColorDotTokenOverlay() { this(BooleanTokenOverlay.DEFAULT_STATE_NAME, Color.RED, -1); } /** * Create a new dot token overlay * * @param aName Name of the token overlay * @param aColor Color of the dot * @param aGrid Size of the overlay grid for this state. All states with the * same grid size share the same overlay. */ public FlowColorDotTokenOverlay(String aName, Color aColor, int aGrid) { super(aName, aColor, 0); grid = aGrid; } /** * @see com.t3.client.ui.token.BooleanTokenOverlay#clone() */ @Override public Object clone() { BooleanTokenOverlay overlay = new FlowColorDotTokenOverlay(getName(), getColor(), grid); overlay.setOrder(getOrder()); overlay.setGroup(getGroup()); overlay.setMouseover(isMouseover()); overlay.setOpacity(getOpacity()); overlay.setShowGM(isShowGM()); overlay.setShowOwner(isShowOwner()); overlay.setShowOthers(isShowOthers()); return overlay; } /** * Get the flow used to position the states. * * @return Flow used to position the states */ protected TokenOverlayFlow getFlow() { if (flow == null && grid > 0) flow = TokenOverlayFlow.getInstance(grid); return flow; } /** * @see com.t3.client.ui.token.BooleanTokenOverlay#paintOverlay(java.awt.Graphics2D, com.t3.model.Token, Rectangle) */ @Override public void paintOverlay(Graphics2D g, Token aToken, Rectangle bounds) { Color tempColor = g.getColor(); Stroke tempStroke = g.getStroke(); Composite tempComposite = g.getComposite(); try { g.setColor(getColor()); g.setStroke(getStroke()); if (getOpacity() != 100) g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)getOpacity()/100)); Shape s = getShape(bounds, aToken); g.fill(s); } finally { g.setColor(tempColor); g.setStroke(tempStroke); g.setComposite(tempComposite); } } /** * Return an ellipse. * * @param bounds Bounds of the token * @param token Token being rendered. * @return An ellipse that fits inside of the bounding box returned by the flow. */ protected Shape getShape(Rectangle bounds, Token token) { Rectangle2D r = getFlow().getStateBounds2D(bounds, token, getName()); return new Ellipse2D.Double(r.getX(), r.getY(), r.getWidth(), r.getHeight()); } /** @return Getter for grid */ public int getGrid() { return grid; } }