Editable pin spacing for all radial components

Folded elytics
This commit is contained in:
bancika
2012-08-27 14:34:59 +00:00
parent b454c017a2
commit c08e52b994
3 changed files with 113 additions and 34 deletions
@@ -108,6 +108,37 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
// Normal mode with component body in the center and two lead parts. // Normal mode with component body in the center and two lead parts.
Rectangle shapeRect = shape.getBounds(); Rectangle shapeRect = shape.getBounds();
Double theta = Math.atan2(points[1].y - points[0].y, points[1].x - points[0].x); Double theta = Math.atan2(points[1].y - points[0].y, points[1].x - points[0].x);
// Go back to the original transformation to draw leads.
if (!outlineMode) {
AffineTransform textTransform = g2d.getTransform();
if (length.convertToPixels() > points[0].distance(points[1])) {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
}
int leadThickness = getClosestOdd(getLeadThickness());
double leadLength = (distance - calculatePinSpacing(shapeRect)) / 2 - leadThickness / 2;
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness));
Color leadColor = shouldShadeLeads() ? getLeadColor(componentState).darker()
: getLeadColor(componentState);
g2d.setColor(leadColor);
int endX = (int) (points[0].x + Math.cos(theta) * leadLength);
int endY = (int) Math.round(points[0].y + Math.sin(theta) * leadLength);
g2d.drawLine(points[0].x, points[0].y, endX, endY);
endX = (int) (points[1].x + Math.cos(theta - Math.PI) * leadLength);
endY = (int) Math.round(points[1].y + Math.sin(theta - Math.PI) * leadLength);
g2d.drawLine(points[1].x, points[1].y, endX, endY);
if (shouldShadeLeads()) {
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness - 2));
leadColor = getLeadColor(componentState);
g2d.setColor(leadColor);
g2d.drawLine(points[0].x, points[0].y, (int) (points[0].x + Math.cos(theta)
* leadLength), (int) (points[0].y + Math.sin(theta) * leadLength));
g2d.drawLine(points[1].x, points[1].y, (int) (points[1].x + Math.cos(theta
- Math.PI)
* leadLength), (int) (points[1].y + Math.sin(theta - Math.PI)
* leadLength));
}
g2d.setTransform(textTransform);
}
// Transform graphics to draw the body in the right place and at the // Transform graphics to draw the body in the right place and at the
// right angle. // right angle.
AffineTransform oldTransform = g2d.getTransform(); AffineTransform oldTransform = g2d.getTransform();
@@ -144,42 +175,43 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
g2d.setColor(finalBorderColor); g2d.setColor(finalBorderColor);
g2d.draw(shape); g2d.draw(shape);
// Go back to the original transformation to draw leads. // // Go back to the original transformation to draw leads.
if (!outlineMode) { // if (!outlineMode) {
AffineTransform textTransform = g2d.getTransform(); // AffineTransform textTransform = g2d.getTransform();
g2d.setTransform(oldTransform); // g2d.setTransform(oldTransform);
if (length.convertToPixels() > points[0].distance(points[1])) { // if (length.convertToPixels() > points[0].distance(points[1])) {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); // g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
} // }
int leadThickness = getClosestOdd(getLeadThickness()); // int leadThickness = getClosestOdd(getLeadThickness());
double leadLength = (distance - shapeRect.width) / 2 - leadThickness / 2; // double leadLength = (distance - calculatePinSpacing(shapeRect)) / 2 - leadThickness / 2;
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness)); // g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness));
Color leadColor = shouldShadeLeads() ? getLeadColor(componentState).darker() // Color leadColor = shouldShadeLeads() ? getLeadColor(componentState).darker()
: getLeadColor(componentState); // : getLeadColor(componentState);
g2d.setColor(leadColor); // g2d.setColor(leadColor);
int endX = (int) (points[0].x + Math.cos(theta) * leadLength); // int endX = (int) (points[0].x + Math.cos(theta) * leadLength);
int endY = (int) Math.round(points[0].y + Math.sin(theta) * leadLength); // int endY = (int) Math.round(points[0].y + Math.sin(theta) * leadLength);
g2d.drawLine(points[0].x, points[0].y, endX, endY); // g2d.drawLine(points[0].x, points[0].y, endX, endY);
endX = (int) (points[1].x + Math.cos(theta - Math.PI) * leadLength); // endX = (int) (points[1].x + Math.cos(theta - Math.PI) * leadLength);
endY = (int) Math.round(points[1].y + Math.sin(theta - Math.PI) * leadLength); // endY = (int) Math.round(points[1].y + Math.sin(theta - Math.PI) * leadLength);
g2d.drawLine(points[1].x, points[1].y, endX, endY); // g2d.drawLine(points[1].x, points[1].y, endX, endY);
if (shouldShadeLeads()) { // if (shouldShadeLeads()) {
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness - 2)); // g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(leadThickness - 2));
leadColor = getLeadColor(componentState); // leadColor = getLeadColor(componentState);
g2d.setColor(leadColor); // g2d.setColor(leadColor);
g2d.drawLine(points[0].x, points[0].y, (int) (points[0].x + Math.cos(theta) // g2d.drawLine(points[0].x, points[0].y, (int) (points[0].x + Math.cos(theta)
* leadLength), (int) (points[0].y + Math.sin(theta) * leadLength)); // * leadLength), (int) (points[0].y + Math.sin(theta) * leadLength));
g2d.drawLine(points[1].x, points[1].y, (int) (points[1].x + Math.cos(theta // g2d.drawLine(points[1].x, points[1].y, (int) (points[1].x + Math.cos(theta
- Math.PI) // - Math.PI)
* leadLength), (int) (points[1].y + Math.sin(theta - Math.PI) // * leadLength), (int) (points[1].y + Math.sin(theta - Math.PI)
* leadLength)); // * leadLength));
} // }
g2d.setComposite(oldComposite); // g2d.setComposite(oldComposite);
g2d.setTransform(textTransform); // g2d.setTransform(textTransform);
} // }
// Draw label. // Draw label.
g2d.setFont(LABEL_FONT); g2d.setFont(LABEL_FONT);
g2d.translate(shapeRect.x, shapeRect.y);
Color finalLabelColor; Color finalLabelColor;
if (outlineMode) { if (outlineMode) {
Theme theme = (Theme) ConfigurationManager.getInstance().readObject( Theme theme = (Theme) ConfigurationManager.getInstance().readObject(
@@ -277,6 +309,10 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
return componentState == ComponentState.SELECTED return componentState == ComponentState.SELECTED
|| componentState == ComponentState.DRAGGING ? SELECTION_COLOR : LEAD_COLOR; || componentState == ComponentState.DRAGGING ? SELECTION_COLOR : LEAD_COLOR;
} }
protected int calculatePinSpacing(Rectangle shapeRect) {
return shapeRect.width;
}
@Override @Override
public int getControlPointCount() { public int getControlPointCount() {
@@ -0,0 +1,43 @@
package org.diylc.components;
import java.awt.Rectangle;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
/**
* Base class for radial components. The same as {@link AbstractRadialComponent}
* but with added pin spacing.
*
* @author bancika
*
* @param <T>
*/
public abstract class AbstractRadialComponent<T> extends
AbstractLeadedComponent<T> {
private static final long serialVersionUID = 1L;
public static Size PIN_SPACING = new Size(0.1d, SizeUnit.in);
private Size pinSpacing = PIN_SPACING;
@Override
protected int calculatePinSpacing(Rectangle shapeRect) {
return getClosestOdd(getPinSpacing().convertToPixels());
}
@EditableProperty(name = "Pin spacing")
public Size getPinSpacing() {
if (pinSpacing == null) {
pinSpacing = PIN_SPACING;
}
return pinSpacing;
}
public void setPinSpacing(Size pinSpacing) {
this.pinSpacing = pinSpacing;
}
}
@@ -57,7 +57,7 @@ public class Presenter implements IPlugInPort {
private static final Logger LOG = Logger.getLogger(Presenter.class); private static final Logger LOG = Logger.getLogger(Presenter.class);
public static final VersionNumber CURRENT_VERSION = new VersionNumber(3, public static final VersionNumber CURRENT_VERSION = new VersionNumber(3,
13, 0); 14, 0);
public static final String DEFAULTS_KEY_PREFIX = "default."; public static final String DEFAULTS_KEY_PREFIX = "default.";
public static final List<IDIYComponent<?>> EMPTY_SELECTION = Collections public static final List<IDIYComponent<?>> EMPTY_SELECTION = Collections