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,14 +6,14 @@
<unit class="org.diylc.core.measures.ResistanceUnit">K</unit> <unit class="org.diylc.core.measures.ResistanceUnit">K</unit>
</org.diylc.core.measures.Resistance> </org.diylc.core.measures.Resistance>
</entry> </entry>
<entry>
<string>metric</string>
<boolean>false</boolean>
</entry>
<entry> <entry>
<string>lastPath</string> <string>lastPath</string>
<string>C:\Documents and Settings\Branislav Stojkovic\Desktop</string> <string>C:\Documents and Settings\Branislav Stojkovic\Desktop</string>
</entry> </entry>
<entry>
<string>metric</string>
<boolean>true</boolean>
</entry>
<entry> <entry>
<string>default.org.diylc.components.passive.AxialFilmCapacitor:Width</string> <string>default.org.diylc.components.passive.AxialFilmCapacitor:Width</string>
<org.diylc.core.measures.Size> <org.diylc.core.measures.Size>
@@ -6,6 +6,7 @@ import java.awt.Color;
import java.awt.Composite; import java.awt.Composite;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Point; import java.awt.Point;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D; import java.awt.geom.RoundRectangle2D;
import org.diylc.components.AbstractTransparentComponent; import org.diylc.components.AbstractTransparentComponent;
@@ -45,21 +46,16 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
private void updateControlPoints() { private void updateControlPoints() {
Point firstPoint = controlPoints[0]; Point firstPoint = controlPoints[0];
int margin = MARGIN.convertToPixels();
int spacing = SPACING.convertToPixels(); int spacing = SPACING.convertToPixels();
switch (switchType) { switch (switchType) {
case SPST: case SPST:
controlPoints = new Point[] { firstPoint, controlPoints = new Point[] { firstPoint,
new Point(firstPoint.x, firstPoint.y + spacing) }; 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; break;
case SPDT: case SPDT:
controlPoints = new Point[] { firstPoint, controlPoints = new Point[] { firstPoint,
new Point(firstPoint.x, firstPoint.y + spacing), new Point(firstPoint.x, firstPoint.y + spacing),
new Point(firstPoint.x, firstPoint.y + 2 * 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; break;
case DPDT: case DPDT:
controlPoints = new Point[] { firstPoint, 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),
new Point(firstPoint.x + spacing, firstPoint.y + spacing), new Point(firstPoint.x + spacing, firstPoint.y + spacing),
new Point(firstPoint.x + spacing, firstPoint.y + 2 * 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; break;
case _3PDT: case _3PDT:
controlPoints = new Point[] { firstPoint, 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),
new Point(firstPoint.x + 2 * spacing, firstPoint.y + spacing), new Point(firstPoint.x + 2 * spacing, firstPoint.y + spacing),
new Point(firstPoint.x + 2 * spacing, firstPoint.y + 2 * 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; break;
case _4PDT: case _4PDT:
controlPoints = new Point[] { firstPoint, 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),
new Point(firstPoint.x + 3 * spacing, firstPoint.y + spacing), new Point(firstPoint.x + 3 * spacing, firstPoint.y + spacing),
new Point(firstPoint.x + 3 * spacing, firstPoint.y + 2 * 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; break;
case _5PDT: case _5PDT:
controlPoints = new Point[] { firstPoint, 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),
new Point(firstPoint.x + 4 * spacing, firstPoint.y + spacing), new Point(firstPoint.x + 4 * spacing, firstPoint.y + spacing),
new Point(firstPoint.x + 4 * spacing, firstPoint.y + 2 * 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; break;
} }
} }
@@ -135,7 +123,8 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
@Override @Override
public void setControlPoint(Point point, int index) { public void setControlPoint(Point point, int index) {
controlPoints[index].setLocation(point); controlPoints[index].setLocation(point);
updateControlPoints(); // Reset body shape.
body = null;
} }
@EditableProperty @EditableProperty
@@ -159,10 +148,14 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
public void setValue(ToggleSwitchType value) { public void setValue(ToggleSwitchType value) {
this.switchType = value; this.switchType = value;
updateControlPoints(); updateControlPoints();
// Reset body shape.
body = null;
} }
@Override @Override
public void draw(Graphics2D g2d, ComponentState componentState, Project project) { public void draw(Graphics2D g2d, ComponentState componentState, Project project) {
Shape body = getBody();
// Draw body if available.
if (body != null) { if (body != null) {
if (componentState != ComponentState.DRAGGING) { if (componentState != ComponentState.DRAGGING) {
Composite oldComposite = g2d.getComposite(); Composite oldComposite = g2d.getComposite();
@@ -178,18 +171,54 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
g2d.setColor(BORDER_COLOR); g2d.setColor(BORDER_COLOR);
g2d.draw(body); g2d.draw(body);
} }
if (componentState != ComponentState.DRAGGING) { // Draw lugs.
int circleDiameter = getClosestOdd(CIRCLE_SIZE.convertToPixels()); int circleDiameter = getClosestOdd(CIRCLE_SIZE.convertToPixels());
int lugWidth = getClosestOdd(LUG_WIDTH.convertToPixels()); int lugWidth = getClosestOdd(LUG_WIDTH.convertToPixels());
int lugHeight = getClosestOdd(LUG_HEIGHT.convertToPixels()); int lugHeight = getClosestOdd(LUG_HEIGHT.convertToPixels());
for (Point p : controlPoints) { for (Point p : controlPoints) {
if (componentState != ComponentState.DRAGGING) {
g2d.setColor(CIRCLE_COLOR); g2d.setColor(CIRCLE_COLOR);
g2d.fillOval(p.x - circleDiameter / 2, p.y - circleDiameter / 2, g2d.fillOval(p.x - circleDiameter / 2, p.y - circleDiameter / 2, circleDiameter,
circleDiameter, circleDiameter); circleDiameter);
g2d.setColor(LUG_COLOR); }
g2d.fillRect(p.x - lugWidth / 2, p.y - lugHeight / 2, lugWidth, lugHeight); 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 @Override
@@ -34,6 +34,7 @@ public class DIL_IC extends AbstractTransparentComponent<String> {
public static Color PIN_BORDER_COLOR = PIN_COLOR.darker(); public static Color PIN_BORDER_COLOR = PIN_COLOR.darker();
public static Color INDENT_COLOR = Color.lightGray; public static Color INDENT_COLOR = Color.lightGray;
public static Color LABEL_COLOR = Color.white; public static Color LABEL_COLOR = Color.white;
public static Color LABEL_COLOR_SELECTED = Color.red;
public static int EDGE_RADIUS = 6; public static int EDGE_RADIUS = 6;
public static Size PIN_SIZE = new Size(0.04d, SizeUnit.in); public static Size PIN_SIZE = new Size(0.04d, SizeUnit.in);
public static Size INDENT_SIZE = new Size(0.15d, 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()); g2d.draw(getBody());
// Draw label. // Draw label.
g2d.setFont(Constants.LABEL_FONT); 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()); FontMetrics fontMetrics = g2d.getFontMetrics(g2d.getFont());
Rectangle2D rect = fontMetrics.getStringBounds(getName(), g2d); Rectangle2D rect = fontMetrics.getStringBounds(getName(), g2d);
int textHeight = (int) (rect.getHeight()); int textHeight = (int) (rect.getHeight());
@@ -247,7 +257,17 @@ public class DIL_IC extends AbstractTransparentComponent<String> {
@Override @Override
public void drawIcon(Graphics2D g2d, int width, int height) { 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 { static enum PinCount {