switch fixes

This commit is contained in:
Bane Stojkovic
2025-03-29 22:17:21 +01:00
parent e17b79a2c4
commit b4c1ed74c2
6 changed files with 212 additions and 69 deletions
@@ -31,15 +31,11 @@ import java.awt.geom.Area;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;
import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.common.IPlugInPort;
import org.diylc.common.ObjectCache;
import org.diylc.common.OrientationHV;
import org.diylc.awt.StringUtils;
import org.diylc.common.*;
import org.diylc.components.AbstractTransparentComponent;
import org.diylc.components.guitar.ToggleSwitchPositionPropertyValueSource;
import org.diylc.components.transform.MiniToggleSwitchTransformer;
@@ -59,8 +55,7 @@ import org.diylc.core.gerber.IGerberComponentSimple;
import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
import org.diylc.utils.Constants;
import static org.diylc.utils.SwitchUtils.getConnectedTerminals;
import org.diylc.utils.SwitchUtils;
@ComponentDescriptor(name = "Mini Toggle Switch", category = "Electro-Mechanical", author = "Branislav Stojkovic",
description = "Panel mounted mini toggle switch", zOrder = IDIYComponent.COMPONENT,
@@ -74,11 +69,14 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
private static Size CIRCLE_SIZE = new Size(0.09d, SizeUnit.in);
private static Size LUG_WIDTH = new Size(0.060d, SizeUnit.in);
private static Size LUG_THICKNESS = new Size(0.02d, SizeUnit.in);
private static Size MARKER_OFFSET = new Size(0.07d, SizeUnit.in);
private static Color BODY_COLOR = Color.decode("#3299CC");
private static Color BORDER_COLOR = BODY_COLOR.darker();
private static Color CIRCLE_COLOR = Color.decode("#287ba4");
private static Color TERMINAL_COLOR = LIGHT_METAL_COLOR;
private static Color MARKER_COLOR = LIGHT_METAL_COLOR;
protected Point2D[] controlPoints = new Point2D[] {new Point2D.Double(0, 0)};
transient protected Shape body;
@@ -92,7 +90,7 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
private Color terminalColor = TERMINAL_COLOR;
private Integer selectedPosition;
private Boolean highlightConnectedTerminals;
private Boolean showMarkers;
@Deprecated
protected String name;
@@ -126,17 +124,6 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
new Point2D.Double(firstPoint.getX() + spacing, firstPoint.getY() + spacing),
new Point2D.Double(firstPoint.getX() + spacing, firstPoint.getY() + 2 * spacing)};
break;
case _DP3T_mustang:
controlPoints =
new Point2D[] {firstPoint,
new Point2D.Double(firstPoint.getX(), firstPoint.getY() + spacing),
new Point2D.Double(firstPoint.getX(), firstPoint.getY() + 2 * spacing),
new Point2D.Double(firstPoint.getX(), firstPoint.getY() + 3 * spacing),
new Point2D.Double(firstPoint.getX() + spacing, firstPoint.getY()),
new Point2D.Double(firstPoint.getX() + spacing, firstPoint.getY() + spacing),
new Point2D.Double(firstPoint.getX() + spacing, firstPoint.getY() + 2 * spacing),
new Point2D.Double(firstPoint.getX() + spacing, firstPoint.getY() + 3 * spacing)};
break;
case _3PDT:
case _3PDT_off:
controlPoints =
@@ -328,8 +315,6 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
lugHeight = getClosestOdd((int) LUG_THICKNESS.convertToPixels());
}
List<Set<Integer>> connectedTerminals = getConnectedTerminals(this, controlPoints.length);
for (int i = 0; i < controlPoints.length; i++) {
Point2D p = controlPoints[i];
if (outlineMode) {
@@ -341,36 +326,44 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
g2d.setColor(theme.getOutlineColor());
g2d.drawRect((int)(p.getX() - lugWidth / 2d), (int)(p.getY() - lugHeight / 2d), lugWidth, lugHeight);
} else {
int finalI = i;
int groupIndex = IntStream.range(0, connectedTerminals.size())
.filter(j -> connectedTerminals.get(j).contains(finalI))
.findFirst().orElse(-1);
if (groupIndex < 0) {
g2d.setColor(getTerminalPadColor().darker());
} else {
g2d.setColor(ISwitch.POLE_COLORS[groupIndex].darker());
}
g2d.setColor(getTerminalPadColor().darker());
g2d.drawOval((int)(p.getX() - circleDiameter / 2d), (int)(p.getY() - circleDiameter / 2d), circleDiameter, circleDiameter);
if (groupIndex < 0) {
g2d.setColor(getTerminalPadColor());
} else {
g2d.setColor(ISwitch.POLE_COLORS[groupIndex]);
}
g2d.setColor(getTerminalPadColor());
g2d.fillOval((int)(p.getX() - circleDiameter / 2d), (int)(p.getY() - circleDiameter / 2d), circleDiameter, circleDiameter);
if (groupIndex < 0) {
g2d.setColor(getTerminalColor());
} else {
g2d.setColor(getTerminalColor().darker());
}
g2d.setColor(getTerminalColor());
// drawingObserver.startTrackingContinuityArea(true);
g2d.fillRect((int)(p.getX() - lugWidth / 2d), (int)(p.getY() - lugHeight / 2d), lugWidth, lugHeight);
// drawingObserver.stopTrackingContinuityArea();
}
}
if (getShowMarkers()) {
String[] markers =
SwitchUtils.getSwitchingMarkers(this, getControlPointCount(), false);
g2d.setColor(MARKER_COLOR);
double offset = MARKER_OFFSET.convertToPixels();
for (int i = 0; i < getControlPointCount(); i++) {
if (markers[i] == null)
continue;
Point2D p = getControlPoint(i);
Point2D labelPoint = new Point2D.Double(offset, 0);
AffineTransform tx = null;
if (getOrientation() == OrientationHV.HORIZONTAL) {
tx = AffineTransform.getRotateInstance(-Math.PI / 2, 0, 0);
}
if (tx != null) {
tx.transform(labelPoint, labelPoint);
}
StringUtils.drawCenteredText(g2d, markers[i], p.getX() + labelPoint.getX(), p.getY() + labelPoint.getY(),
HorizontalAlignment.CENTER, VerticalAlignment.CENTER);
}
}
}
public Shape getBody() {
@@ -398,11 +391,6 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
new RoundRectangle2D.Double(firstPoint.getX() - margin, firstPoint.getY() - margin, 2 * margin + spacing, 2
* margin + 2 * spacing, margin, margin);
break;
case _DP3T_mustang:
body =
new RoundRectangle2D.Double(firstPoint.getX() - margin, firstPoint.getY() - margin, 2 * margin + spacing, 2
* margin + 3 * spacing, margin, margin);
break;
case _3PDT:
case _3PDT_off:
body =
@@ -529,24 +517,25 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
public void setSelectedPosition(Integer selectedPosition) {
this.selectedPosition = selectedPosition;
this.body = null;
}
@EditableProperty(name = "Highlight Connected")
@EditableProperty(name = "Markers")
public Boolean getShowMarkers() {
if (highlightConnectedTerminals == null) {
highlightConnectedTerminals = false;
if (showMarkers == null) {
showMarkers = false;
}
return highlightConnectedTerminals;
return showMarkers;
}
public void setHighlightConnectedTerminals(Boolean highlightConnectedTerminals) {
this.highlightConnectedTerminals = highlightConnectedTerminals;
this.body = null;
public void setShowMarkers(Boolean showMarkers) {
this.showMarkers = showMarkers;
}
@Override
public boolean arePointsConnected(int index1, int index2, int position) {
if (index1 >= index2)
return false;
switch (switchType) {
case SPST:
return position == 0;
@@ -561,9 +550,11 @@ public class MiniToggleSwitch extends AbstractTransparentComponent<ToggleSwitchT
case _3PDT_off:
case _4PDT_off:
case _5PDT_off:
return position != 2 && (index2 - index1) < 3 && index1 % 3 == position && index2 % 3 == position + 1;
case _DP3T_mustang:
return (index2 - index1) < 3 && index1 % 3 == 0 && index2 % 3 == position + 1;
if (position == 1)
return false;
if (position == 2)
position = 1;
return (index2 - index1) < 3 && index1 % 3 == position && index2 % 3 == position + 1;
case DPDT_ononon_1:
switch (position) {
case 0:
@@ -30,7 +30,6 @@ public enum ToggleSwitchType {
DPDT_off("DPDT (On/Off/On)", 3),
DPDT_ononon_1("DPDT (On/On/On Type 1)", 3),
DPDT_ononon_2("DPDT (On/On/On Type 2)", 3),
_DP3T_mustang("DP3T", 3),
_3PDT("3PDT", 2),
_3PDT_off("3PDT (On/Off/On)", 3),
_4PDT("4PDT", 2),
@@ -151,8 +151,6 @@ public class LeverSwitch extends AbstractAngledComponent<LeverSwitch.LeverSwitch
SwitchUtils.getSwitchingMarkers(this, getControlPointCount(), false);
double theta = getAngle().getValueRad();
g2d.setColor(MARKER_COLOR);
double x = controlPoints[0].getX();
double y = controlPoints[0].getY();
double offset = MARKER_OFFSET.convertToPixels();
for (int i = 0; i < getControlPointCount(); i++) {
@@ -8,18 +8,168 @@ import org.diylc.components.electromechanical.ToggleSwitchType;
public class MiniToggleSwitchTests extends AbstractSwitchTests {
@Test
public void testMiniToggleSwitchDPDTononon1() {
public void testMiniToggleSwitchSPST() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType.DPDT_ononon_1);
String[] validCombinations = new String[] {"0,0,1", "0,3,4", "1,0,1", "1,4,5", "2,1,2", "2,4,5"};
toggleSwitch.setValue(ToggleSwitchType.SPST);
String[] validCombinations = new String[] {"0,0,1"};
testSwitch(toggleSwitch, validCombinations);
}
@Test
public void testMiniToggleSwitchDPDTononon2() {
public void testMiniToggleSwitchSPDT() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType.SPDT);
String[] validCombinations = new String[] {
"0,0,1",
"1,1,2"
};
testSwitch(toggleSwitch, validCombinations);
}
@Test
public void testMiniToggleSwitchSPDT_off() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType.SPDT_off);
String[] validCombinations = new String[] {
"0,0,1",
"2,1,2"
};
testSwitch(toggleSwitch, validCombinations);
}
@Test
public void testMiniToggleSwitchDPDT() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType.DPDT);
String[] validCombinations = new String[] {
"0,0,1", "0,3,4",
"1,1,2", "1,4,5"
};
testSwitch(toggleSwitch, validCombinations);
}
@Test
public void testMiniToggleSwitchDPDT_off() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType.DPDT_off);
String[] validCombinations = new String[] {
"0,0,1", "0,3,4",
"2,1,2", "2,4,5"
};
testSwitch(toggleSwitch, validCombinations);
}
@Test
public void testMiniToggleSwitchDPDT_ononon1() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType.DPDT_ononon_1);
String[] validCombinations = new String[] {
"0,0,1", "0,3,4",
"1,0,1", "1,4,5",
"2,1,2", "2,4,5"
};
testSwitch(toggleSwitch, validCombinations);
}
@Test
public void testMiniToggleSwitchDPDT_ononon2() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType.DPDT_ononon_2);
String[] validCombinations = new String[] {"0,0,1", "0,3,4", "1,1,2", "1,3,4", "2,1,2", "2,4,5"};
String[] validCombinations = new String[] {
"0,0,1", "0,3,4",
"1,1,2", "1,3,4",
"2,1,2", "2,4,5"
};
testSwitch(toggleSwitch, validCombinations);
}
@Test
public void testMiniToggleSwitch_3PDT() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType._3PDT);
String[] validCombinations = new String[] {
"0,0,1", "0,3,4", "0,6,7",
"1,1,2", "1,4,5", "1,7,8"
};
testSwitch(toggleSwitch, validCombinations);
}
@Test
public void testMiniToggleSwitch_3PDT_off() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType._3PDT_off);
String[] validCombinations = new String[] {
"0,0,1", "0,3,4", "0,6,7",
"2,1,2", "2,4,5", "2,7,8"
};
testSwitch(toggleSwitch, validCombinations);
}
@Test
public void testMiniToggleSwitch_4PDT() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType._4PDT);
String[] validCombinations = new String[] {
"0,0,1", "0,3,4", "0,6,7", "0,9,10",
"1,1,2", "1,4,5", "1,7,8", "1,10,11"
};
testSwitch(toggleSwitch, validCombinations);
}
@Test
public void testMiniToggleSwitch_4PDT_off() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType._4PDT_off);
String[] validCombinations = new String[] {
"0,0,1", "0,3,4", "0,6,7", "0,9,10",
"2,1,2", "2,4,5", "2,7,8", "2,10,11"
};
testSwitch(toggleSwitch, validCombinations);
}
@Test
public void testMiniToggleSwitch_4PDT_ononon1() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType._4PDT_ononon_1);
String[] validCombinations = new String[] {
"0,0,1", "0,3,4", "0,6,7", "0,9,10",
"1,0,1", "1,4,5", "1,6,7", "1,10,11",
"2,1,2", "2,4,5", "2,7,8", "2,10,11"
};
testSwitch(toggleSwitch, validCombinations);
}
@Test
public void testMiniToggleSwitch_4PDT_ononon2() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType._4PDT_ononon_2);
String[] validCombinations = new String[] {
"0,0,1", "0,3,4", "0,6,7", "0,9,10",
"1,1,2", "1,3,4", "1,7,8", "1,9,10",
"2,1,2", "2,4,5", "2,7,8", "2,10,11"
};
testSwitch(toggleSwitch, validCombinations);
}
@Test
public void testMiniToggleSwitch_5PDT() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType._5PDT);
String[] validCombinations = new String[] {
"0,0,1", "0,3,4", "0,6,7", "0,9,10", "0,12,13",
"1,1,2", "1,4,5", "1,7,8", "1,10,11", "1,13,14"
};
testSwitch(toggleSwitch, validCombinations);
}
@Test
public void testMiniToggleSwitch_5PDT_off() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType._5PDT_off);
String[] validCombinations = new String[] {
"0,0,1", "0,3,4", "0,6,7", "0,9,10", "0,12,13",
"2,1,2", "2,4,5", "2,7,8", "2,10,11", "2,13,14"
};
testSwitch(toggleSwitch, validCombinations);
}
}