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
Binary file not shown.
@@ -4,13 +4,16 @@ package org.diylc.components.smd;
import java.awt.AlphaComposite; import java.awt.AlphaComposite;
import java.awt.Color; import java.awt.Color;
import java.awt.Composite; import java.awt.Composite;
import java.awt.FontMetrics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Point; import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Area; import java.awt.geom.Area;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D; import java.awt.geom.RoundRectangle2D;
import org.diylc.appframework.miscutils.ConfigurationManager; import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.common.Display;
import org.diylc.common.IPlugInPort; import org.diylc.common.IPlugInPort;
import org.diylc.common.ObjectCache; import org.diylc.common.ObjectCache;
import org.diylc.common.Orientation; import org.diylc.common.Orientation;
@@ -31,7 +34,7 @@ public abstract class PassiveSMDComponent<T> extends AbstractTransparentComponen
public static Color PIN_COLOR = Color.decode("#00B2EE"); public static Color PIN_COLOR = Color.decode("#00B2EE");
public static Color PIN_BORDER_COLOR = PIN_COLOR.darker(); 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 int EDGE_RADIUS = 4;
public static Size PIN_SIZE = new Size(0.8d, SizeUnit.mm); 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 SMDSize size = SMDSize._1206;
private Point[] controlPoints = new Point[] {new Point(0, 0), new Point(0, 0)}; 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 bodyColor;
protected Color borderColor; protected Color borderColor;
// private Color labelColor = LABEL_COLOR; private Color labelColor = LABEL_COLOR;
transient private Area[] body; transient private Area[] body;
public PassiveSMDComponent() { public PassiveSMDComponent() {
@@ -63,17 +66,17 @@ public abstract class PassiveSMDComponent<T> extends AbstractTransparentComponen
body = null; body = null;
} }
// @EditableProperty @EditableProperty
// public Display getDisplay() { public Display getDisplay() {
// if (display == null) { if (display == null) {
// display = Display.VALUE; display = Display.VALUE;
// } }
// return display; return display;
// } }
//
// public void setDisplay(Display display) { public void setDisplay(Display display) {
// this.display = display; this.display = display;
// } }
@EditableProperty @EditableProperty
public SMDSize getSize() { public SMDSize getSize() {
@@ -266,41 +269,72 @@ public abstract class PassiveSMDComponent<T> extends AbstractTransparentComponen
g2d.setColor(finalBorderColor); g2d.setColor(finalBorderColor);
g2d.draw(contactArea); g2d.draw(contactArea);
// // Draw label. // Draw label.
// g2d.setFont(LABEL_FONT); g2d.setFont(LABEL_FONT);
// Color finalLabelColor; Color finalLabelColor;
// if (outlineMode) { if (outlineMode) {
// Theme theme = Theme theme =
// (Theme) ConfigurationManager.getInstance().readObject(IPlugInPort.THEME_KEY, (Theme) ConfigurationManager.getInstance().readObject(IPlugInPort.THEME_KEY, Constants.DEFAULT_THEME);
// Constants.DEFAULT_THEME); finalLabelColor =
// finalLabelColor = componentState == ComponentState.SELECTED || componentState == ComponentState.DRAGGING ? LABEL_COLOR_SELECTED
// componentState == ComponentState.SELECTED || componentState == ComponentState.DRAGGING ? : theme.getOutlineColor();
// LABEL_COLOR_SELECTED } else {
// : theme.getOutlineColor(); finalLabelColor =
// } else { componentState == ComponentState.SELECTED || componentState == ComponentState.DRAGGING ? LABEL_COLOR_SELECTED
// finalLabelColor = : getLabelColor();
// componentState == ComponentState.SELECTED || componentState == ComponentState.DRAGGING ? }
// LABEL_COLOR_SELECTED g2d.setColor(finalLabelColor);
// : getLabelColor(); FontMetrics fontMetrics = g2d.getFontMetrics(g2d.getFont());
// } String label = "";
// g2d.setColor(finalLabelColor); label = (getDisplay() == Display.NAME) ? getName() : getValue().toString();
// FontMetrics fontMetrics = g2d.getFontMetrics(g2d.getFont()); if (getDisplay() == Display.NONE) {
// String label = ""; label = "";
// label = (getDisplay() == Display.NAME) ? getName() : getValue(); }
// if (getDisplay() == Display.NONE) { if (getDisplay() == Display.BOTH) {
// label = ""; label = getName() + " " + (getValue() == null ? "" : getValue().toString());
// } }
// 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()); Rectangle2D rect = fontMetrics.getStringBounds(label, g2d);
// int textWidth = (int) (rect.getWidth()); int textHeight = (int) (rect.getHeight());
// // Center text horizontally and vertically int textWidth = (int) (rect.getWidth());
// Rectangle bounds = mainArea.getBounds();
// int x = bounds.x + (bounds.width - textWidth) / 2; do {
// int y = bounds.y + (bounds.height - textHeight) / 2 + fontMetrics.getAscent(); System.out.println("trying font size: " + g2d.getFont().getSize2D());
// g2d.drawString(label, x, y); 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 @Override
@@ -343,17 +377,17 @@ public abstract class PassiveSMDComponent<T> extends AbstractTransparentComponen
this.borderColor = borderColor; this.borderColor = borderColor;
} }
// @EditableProperty(name = "Label") @EditableProperty(name = "Label")
// public Color getLabelColor() { public Color getLabelColor() {
// if (labelColor == null) { if (labelColor == null) {
// labelColor = LABEL_COLOR; labelColor = LABEL_COLOR;
// } }
// return labelColor; return labelColor;
// } }
//
// public void setLabelColor(Color labelColor) { public void setLabelColor(Color labelColor) {
// this.labelColor = labelColor; this.labelColor = labelColor;
// } }
public static enum SMDSize { public static enum SMDSize {
Binary file not shown.
Binary file not shown.