improved switch and IC

This commit is contained in:
bancika
2011-01-10 23:32:47 +00:00
parent dcbd351589
commit c2e5b6d20c
4 changed files with 78 additions and 29 deletions
@@ -6,6 +6,7 @@ import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import org.diylc.components.AbstractTransparentComponent;
@@ -45,21 +46,16 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
private void updateControlPoints() {
Point firstPoint = controlPoints[0];
int margin = MARGIN.convertToPixels();
int spacing = SPACING.convertToPixels();
switch (switchType) {
case SPST:
controlPoints = new Point[] { firstPoint,
new Point(firstPoint.x, firstPoint.y + spacing) };
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin,
2 * margin, 2 * margin + spacing, margin, margin);
break;
case SPDT:
controlPoints = new Point[] { firstPoint,
new Point(firstPoint.x, firstPoint.y + spacing),
new Point(firstPoint.x, firstPoint.y + 2 * spacing) };
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin,
2 * margin, 2 * margin + 2 * spacing, margin, margin);
break;
case DPDT:
controlPoints = new Point[] { firstPoint,
@@ -68,8 +64,6 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
new Point(firstPoint.x + spacing, firstPoint.y),
new Point(firstPoint.x + spacing, firstPoint.y + spacing),
new Point(firstPoint.x + spacing, firstPoint.y + 2 * spacing) };
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, 2
* margin + spacing, 2 * margin + 2 * spacing, margin, margin);
break;
case _3PDT:
controlPoints = new Point[] { firstPoint,
@@ -81,8 +75,6 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
new Point(firstPoint.x + 2 * spacing, firstPoint.y),
new Point(firstPoint.x + 2 * spacing, firstPoint.y + spacing),
new Point(firstPoint.x + 2 * spacing, firstPoint.y + 2 * spacing) };
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, 2
* margin + 2 * spacing, 2 * margin + 2 * spacing, margin, margin);
break;
case _4PDT:
controlPoints = new Point[] { firstPoint,
@@ -97,8 +89,6 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
new Point(firstPoint.x + 3 * spacing, firstPoint.y),
new Point(firstPoint.x + 3 * spacing, firstPoint.y + spacing),
new Point(firstPoint.x + 3 * spacing, firstPoint.y + 2 * spacing) };
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, 2
* margin + 3 * spacing, 2 * margin + 2 * spacing, margin, margin);
break;
case _5PDT:
controlPoints = new Point[] { firstPoint,
@@ -116,8 +106,6 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
new Point(firstPoint.x + 4 * spacing, firstPoint.y),
new Point(firstPoint.x + 4 * spacing, firstPoint.y + spacing),
new Point(firstPoint.x + 4 * spacing, firstPoint.y + 2 * spacing) };
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, 2
* margin + 4 * spacing, 2 * margin + 2 * spacing, margin, margin);
break;
}
}
@@ -135,7 +123,8 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
@Override
public void setControlPoint(Point point, int index) {
controlPoints[index].setLocation(point);
updateControlPoints();
// Reset body shape.
body = null;
}
@EditableProperty
@@ -159,10 +148,14 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
public void setValue(ToggleSwitchType value) {
this.switchType = value;
updateControlPoints();
// Reset body shape.
body = null;
}
@Override
public void draw(Graphics2D g2d, ComponentState componentState, Project project) {
Shape body = getBody();
// Draw body if available.
if (body != null) {
if (componentState != ComponentState.DRAGGING) {
Composite oldComposite = g2d.getComposite();
@@ -178,18 +171,54 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
g2d.setColor(BORDER_COLOR);
g2d.draw(body);
}
if (componentState != ComponentState.DRAGGING) {
int circleDiameter = getClosestOdd(CIRCLE_SIZE.convertToPixels());
int lugWidth = getClosestOdd(LUG_WIDTH.convertToPixels());
int lugHeight = getClosestOdd(LUG_HEIGHT.convertToPixels());
for (Point p : controlPoints) {
// Draw lugs.
int circleDiameter = getClosestOdd(CIRCLE_SIZE.convertToPixels());
int lugWidth = getClosestOdd(LUG_WIDTH.convertToPixels());
int lugHeight = getClosestOdd(LUG_HEIGHT.convertToPixels());
for (Point p : controlPoints) {
if (componentState != ComponentState.DRAGGING) {
g2d.setColor(CIRCLE_COLOR);
g2d.fillOval(p.x - circleDiameter / 2, p.y - circleDiameter / 2,
circleDiameter, circleDiameter);
g2d.setColor(LUG_COLOR);
g2d.fillRect(p.x - lugWidth / 2, p.y - lugHeight / 2, lugWidth, lugHeight);
g2d.fillOval(p.x - circleDiameter / 2, p.y - circleDiameter / 2, circleDiameter,
circleDiameter);
}
g2d.setColor(LUG_COLOR);
g2d.fillRect(p.x - lugWidth / 2, p.y - lugHeight / 2, lugWidth, lugHeight);
}
}
public RoundRectangle2D getBody() {
if (body == null) {
Point firstPoint = controlPoints[0];
int margin = MARGIN.convertToPixels();
int spacing = SPACING.convertToPixels();
switch (switchType) {
case SPST:
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin,
2 * margin, 2 * margin + spacing, margin, margin);
break;
case SPDT:
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin,
2 * margin, 2 * margin + 2 * spacing, margin, margin);
break;
case DPDT:
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, 2
* margin + spacing, 2 * margin + 2 * spacing, margin, margin);
break;
case _3PDT:
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, 2
* margin + 2 * spacing, 2 * margin + 2 * spacing, margin, margin);
break;
case _4PDT:
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, 2
* margin + 3 * spacing, 2 * margin + 2 * spacing, margin, margin);
break;
case _5PDT:
body = new RoundRectangle2D.Double(firstPoint.x - margin, firstPoint.y - margin, 2
* margin + 4 * spacing, 2 * margin + 2 * spacing, margin, margin);
break;
}
}
return body;
}
@Override
@@ -34,6 +34,7 @@ public class DIL_IC extends AbstractTransparentComponent<String> {
public static Color PIN_BORDER_COLOR = PIN_COLOR.darker();
public static Color INDENT_COLOR = Color.lightGray;
public static Color LABEL_COLOR = Color.white;
public static Color LABEL_COLOR_SELECTED = Color.red;
public static int EDGE_RADIUS = 6;
public static Size PIN_SIZE = new Size(0.04d, SizeUnit.in);
public static Size INDENT_SIZE = new Size(0.15d, SizeUnit.in);
@@ -233,7 +234,16 @@ public class DIL_IC extends AbstractTransparentComponent<String> {
g2d.draw(getBody());
// Draw label.
g2d.setFont(Constants.LABEL_FONT);
g2d.setColor(componentState == ComponentState.DRAGGING ? BORDER_COLOR : LABEL_COLOR);
switch (componentState) {
case DRAGGING:
g2d.setColor(BORDER_COLOR);
break;
case SELECTED:
g2d.setColor(LABEL_COLOR_SELECTED);
break;
default:
g2d.setColor(LABEL_COLOR);
}
FontMetrics fontMetrics = g2d.getFontMetrics(g2d.getFont());
Rectangle2D rect = fontMetrics.getStringBounds(getName(), g2d);
int textHeight = (int) (rect.getHeight());
@@ -247,7 +257,17 @@ public class DIL_IC extends AbstractTransparentComponent<String> {
@Override
public void drawIcon(Graphics2D g2d, int width, int height) {
g2d.drawString("IC", 10, 10);
int radius = 6 * width / 32;
g2d.setColor(BODY_COLOR);
g2d.fillRoundRect(width / 6, 1, 4 * width / 6, height - 4, radius, radius);
g2d.setColor(BORDER_COLOR);
g2d.drawRoundRect(width / 6, 1, 4 * width / 6, height - 4, radius, radius);
int pinSize = 2 * width / 32;
g2d.setColor(PIN_COLOR);
for (int i = 0; i < 4; i++) {
g2d.fillRect(width / 6 - pinSize, (height / 5) * (i + 1) - 1, pinSize, pinSize);
g2d.fillRect(5 * width / 6 + 1, (height / 5) * (i + 1) - 1, pinSize, pinSize);
}
}
static enum PinCount {