show SMD labels

This commit is contained in:
bancika
2016-12-06 18:04:54 +01:00
parent ba9924cc99
commit d27b049110
4 changed files with 98 additions and 64 deletions
@@ -4,13 +4,16 @@ package org.diylc.components.smd;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.common.Display;
import org.diylc.common.IPlugInPort;
import org.diylc.common.ObjectCache;
import org.diylc.common.Orientation;
@@ -28,10 +31,10 @@ import org.diylc.utils.Constants;
public abstract class PassiveSMDComponent<T> extends AbstractTransparentComponent<T> {
private static final long serialVersionUID = 1L;
public static Color PIN_COLOR = Color.decode("#00B2EE");
public static Color PIN_BORDER_COLOR = PIN_COLOR.darker();
// public static Color LABEL_COLOR = Color.white;
public static Color LABEL_COLOR = Color.white;
public static int EDGE_RADIUS = 4;
public static Size PIN_SIZE = new Size(0.8d, SizeUnit.mm);
@@ -40,10 +43,10 @@ public abstract class PassiveSMDComponent<T> extends AbstractTransparentComponen
private SMDSize size = SMDSize._1206;
private Point[] controlPoints = new Point[] {new Point(0, 0), new Point(0, 0)};
// protected Display display = Display.NAME;
protected Display display = Display.NAME;
protected Color bodyColor;
protected Color borderColor;
// private Color labelColor = LABEL_COLOR;
private Color labelColor = LABEL_COLOR;
transient private Area[] body;
public PassiveSMDComponent() {
@@ -63,17 +66,17 @@ public abstract class PassiveSMDComponent<T> extends AbstractTransparentComponen
body = null;
}
// @EditableProperty
// public Display getDisplay() {
// if (display == null) {
// display = Display.VALUE;
// }
// return display;
// }
//
// public void setDisplay(Display display) {
// this.display = display;
// }
@EditableProperty
public Display getDisplay() {
if (display == null) {
display = Display.VALUE;
}
return display;
}
public void setDisplay(Display display) {
this.display = display;
}
@EditableProperty
public SMDSize getSize() {
@@ -198,7 +201,7 @@ public abstract class PassiveSMDComponent<T> extends AbstractTransparentComponen
contactArea.add(new Area(new Rectangle2D.Double(x, y + height - pinSize, width, pinSize)));
}
contactArea.intersect(mainArea);
mainArea.subtract(contactArea);
body[0] = mainArea;
body[1] = contactArea;
@@ -247,7 +250,7 @@ public abstract class PassiveSMDComponent<T> extends AbstractTransparentComponen
g2d.setColor(PIN_COLOR);
if (alpha < MAX_ALPHA) {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f * alpha / MAX_ALPHA));
}
}
g2d.fill(contactArea);
g2d.setComposite(oldComposite);
}
@@ -266,41 +269,72 @@ public abstract class PassiveSMDComponent<T> extends AbstractTransparentComponen
g2d.setColor(finalBorderColor);
g2d.draw(contactArea);
// // Draw label.
// g2d.setFont(LABEL_FONT);
// Color finalLabelColor;
// if (outlineMode) {
// Theme theme =
// (Theme) ConfigurationManager.getInstance().readObject(IPlugInPort.THEME_KEY,
// Constants.DEFAULT_THEME);
// finalLabelColor =
// componentState == ComponentState.SELECTED || componentState == ComponentState.DRAGGING ?
// LABEL_COLOR_SELECTED
// : theme.getOutlineColor();
// } else {
// finalLabelColor =
// componentState == ComponentState.SELECTED || componentState == ComponentState.DRAGGING ?
// LABEL_COLOR_SELECTED
// : getLabelColor();
// }
// g2d.setColor(finalLabelColor);
// FontMetrics fontMetrics = g2d.getFontMetrics(g2d.getFont());
// String label = "";
// label = (getDisplay() == Display.NAME) ? getName() : getValue();
// if (getDisplay() == Display.NONE) {
// label = "";
// }
// if (getDisplay() == Display.BOTH) {
// label = getName() + " " + (getValue() == null ? "" : getValue().toString());
// }
// Rectangle2D rect = fontMetrics.getStringBounds(label, g2d);
// int textHeight = (int) (rect.getHeight());
// int textWidth = (int) (rect.getWidth());
// // Center text horizontally and vertically
// Rectangle bounds = mainArea.getBounds();
// int x = bounds.x + (bounds.width - textWidth) / 2;
// int y = bounds.y + (bounds.height - textHeight) / 2 + fontMetrics.getAscent();
// g2d.drawString(label, x, y);
// Draw label.
g2d.setFont(LABEL_FONT);
Color finalLabelColor;
if (outlineMode) {
Theme theme =
(Theme) ConfigurationManager.getInstance().readObject(IPlugInPort.THEME_KEY, Constants.DEFAULT_THEME);
finalLabelColor =
componentState == ComponentState.SELECTED || componentState == ComponentState.DRAGGING ? LABEL_COLOR_SELECTED
: theme.getOutlineColor();
} else {
finalLabelColor =
componentState == ComponentState.SELECTED || componentState == ComponentState.DRAGGING ? LABEL_COLOR_SELECTED
: getLabelColor();
}
g2d.setColor(finalLabelColor);
FontMetrics fontMetrics = g2d.getFontMetrics(g2d.getFont());
String label = "";
label = (getDisplay() == Display.NAME) ? getName() : getValue().toString();
if (getDisplay() == Display.NONE) {
label = "";
}
if (getDisplay() == Display.BOTH) {
label = getName() + " " + (getValue() == null ? "" : getValue().toString());
}
Rectangle textTarget = mainArea.getBounds();
int length = textTarget.height > textTarget.width ? textTarget.height : textTarget.width;
Rectangle2D rect = fontMetrics.getStringBounds(label, g2d);
int textHeight = (int) (rect.getHeight());
int textWidth = (int) (rect.getWidth());
do {
System.out.println("trying font size: " + g2d.getFont().getSize2D());
g2d.setFont(g2d.getFont().deriveFont(g2d.getFont().getSize2D() - 1));
fontMetrics = g2d.getFontMetrics(g2d.getFont());
rect = fontMetrics.getStringBounds(label, g2d);
textHeight = (int) (rect.getHeight());
textWidth = (int) (rect.getWidth());
System.out.println("width: " + rect.getWidth());
} while (textWidth > length && g2d.getFont().getSize2D() > 2);
double centerX = textTarget.getX() + textTarget.getWidth() / 2;
double centerY = textTarget.getY() + textTarget.getHeight() / 2;
g2d.translate(centerX, centerY);
switch (orientation) {
case DEFAULT:
g2d.rotate(Math.PI / 2);
break;
case _90:
g2d.rotate(Math.PI);
break;
case _180:
g2d.rotate(Math.PI * 3 / 2);
break;
case _270:
break;
default:
throw new RuntimeException("Unexpected orientation: " + orientation);
}
// Center text horizontally and vertically
int x = -textWidth / 2;
int y = -textHeight / 2 + fontMetrics.getAscent() - 1;
g2d.drawString(label, x, y);
}
@Override
@@ -322,7 +356,7 @@ public abstract class PassiveSMDComponent<T> extends AbstractTransparentComponen
contactSize)));
contactArea.intersect(new Area(rect));
g2d.setColor(PIN_COLOR);
g2d.fill(contactArea);
g2d.fill(contactArea);
}
@EditableProperty(name = "Body")
@@ -343,17 +377,17 @@ public abstract class PassiveSMDComponent<T> extends AbstractTransparentComponen
this.borderColor = borderColor;
}
// @EditableProperty(name = "Label")
// public Color getLabelColor() {
// if (labelColor == null) {
// labelColor = LABEL_COLOR;
// }
// return labelColor;
// }
//
// public void setLabelColor(Color labelColor) {
// this.labelColor = labelColor;
// }
@EditableProperty(name = "Label")
public Color getLabelColor() {
if (labelColor == null) {
labelColor = LABEL_COLOR;
}
return labelColor;
}
public void setLabelColor(Color labelColor) {
this.labelColor = labelColor;
}
public static enum SMDSize {