S1 improvement

This commit is contained in:
Bane Stojkovic
2026-10-12 09:51:35 +02:00
parent 6e4cd6bf8c
commit eba1b9e49b
4 changed files with 173 additions and 9 deletions
@@ -593,6 +593,10 @@
<changeType>NEW_FEATURE</changeType>
<description>Holding 'Shift' when starting to drag components or control points keeps the whole drag strictly horizontal or vertical, in the direction of the initial gesture. Holding 'Shift' while drawing a new wire or two-point component does the same</description>
</org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change>
<changeType>IMPROVEMENT</changeType>
<description>'S1 Switch' can now be rotated to match the orientation of the potentiometer it is mounted on, so the switching matrix stays correct relative to the pot lugs</description>
</org.diylc.appframework.update.Change>
</changes>
<url>https://github.com/bancika/diy-layout-creator/releases</url>
</org.diylc.appframework.update.Version>
@@ -27,6 +27,7 @@ import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
@@ -36,8 +37,10 @@ import org.diylc.awt.StringUtils;
import org.diylc.common.HorizontalAlignment;
import org.diylc.common.IPlugInPort;
import org.diylc.common.ObjectCache;
import org.diylc.common.Orientation;
import org.diylc.common.VerticalAlignment;
import org.diylc.components.AbstractTransparentComponent;
import org.diylc.components.transform.S1SwitchTransformer;
import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.IDrawingObserver;
@@ -53,7 +56,7 @@ import org.diylc.utils.Constants;
@ComponentDescriptor(name = "S1 Switch", category = "Guitar", author = "Branislav Stojkovic",
description = "Fender S1 4 pole pushbutton switch", zOrder = IDIYComponent.COMPONENT,
instanceNamePrefix = "SW", enableCache = true)
instanceNamePrefix = "SW", enableCache = true, transformer = S1SwitchTransformer.class)
public class S1Switch extends AbstractTransparentComponent<Void> implements ISwitch {
private static final long serialVersionUID = 1L;
@@ -77,6 +80,7 @@ public class S1Switch extends AbstractTransparentComponent<Void> implements ISwi
transient protected Shape body;
private boolean showMarkers = true;
private Orientation orientation = Orientation.DEFAULT;
public S1Switch() {
super();
@@ -108,6 +112,14 @@ public class S1Switch extends AbstractTransparentComponent<Void> implements ISwi
controlPoints[10] = new Point2D.Double(firstPoint.getX() - 2 * spacing, firstPoint.getY() - spacing + yOffset);
controlPoints[11] = new Point2D.Double(firstPoint.getX() - 2 * spacing, firstPoint.getY() + yOffset);
controlPoints[12] = new Point2D.Double(firstPoint.getX() - 2 * spacing, firstPoint.getY() + spacing + yOffset);
if (getOrientation() != Orientation.DEFAULT) {
AffineTransform rotation = AffineTransform.getRotateInstance(getOrientation().toRadians(),
firstPoint.getX(), firstPoint.getY());
for (int i = 1; i < controlPoints.length; i++) {
rotation.transform(controlPoints[i], controlPoints[i]);
}
}
}
@Override
@@ -182,6 +194,7 @@ public class S1Switch extends AbstractTransparentComponent<Void> implements ISwi
int padDiameter = getClosestOdd((int) PAD_DIAMETER.convertToPixels());
int holeDiameter = getClosestOdd((int) HOLE_DIAMETER.convertToPixels());
AffineTransform markerRotation = AffineTransform.getRotateInstance(getOrientation().toRadians());
for (int i = 1; i < controlPoints.length; i++) {
Point2D p = controlPoints[i];
@@ -213,31 +226,42 @@ public class S1Switch extends AbstractTransparentComponent<Void> implements ISwi
Stroke stroke = g2d.getStroke();
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(2f));
g2d.setColor(LINE_COLOR);
g2d.drawLine((int) Math.round(p.getX() - padDiameter / 3), (int) Math.round(p.getY() + dy * padDiameter * 4 / 5),
(int) Math.round(p.getX() + padDiameter / 3), (int) Math.round(p.getY() + dy * padDiameter * 4 / 5));
Point2D start = markerRotation.deltaTransform(
new Point2D.Double(-padDiameter / 3, dy * padDiameter * 4 / 5), null);
Point2D end = markerRotation.deltaTransform(
new Point2D.Double(padDiameter / 3, dy * padDiameter * 4 / 5), null);
g2d.drawLine((int) Math.round(p.getX() + start.getX()), (int) Math.round(p.getY() + start.getY()),
(int) Math.round(p.getX() + end.getX()), (int) Math.round(p.getY() + end.getY()));
g2d.setStroke(stroke);
}
double yOffset = Y_OFFSET.convertToPixels();
Point2D center = getBodyCenter();
g2d.setFont(g2d.getFont().deriveFont(1.5f * g2d.getFont().getSize()));
g2d.setColor(LABEL_COLOR);
StringUtils.drawCenteredText(g2d, getName(), controlPoints[0].getX(), controlPoints[0].getY() + yOffset,
StringUtils.drawCenteredText(g2d, getName(), center.getX(), center.getY(),
HorizontalAlignment.CENTER, VerticalAlignment.CENTER);
}
public Shape getBody() {
if (body == null) {
int diameter = getClosestOdd(DIAMETER.convertToPixels());
double yOffset = Y_OFFSET.convertToPixels();
body =
new Ellipse2D.Double(controlPoints[0].getX() - diameter / 2, controlPoints[0].getY() - diameter / 2 + yOffset,
diameter, diameter);
Point2D center = getBodyCenter();
body = new Ellipse2D.Double(center.getX() - diameter / 2, center.getY() - diameter / 2,
diameter, diameter);
}
return body;
}
private Point2D getBodyCenter() {
Point2D center = new Point2D.Double(controlPoints[0].getX(),
controlPoints[0].getY() + Y_OFFSET.convertToPixels());
AffineTransform.getRotateInstance(getOrientation().toRadians(), controlPoints[0].getX(),
controlPoints[0].getY()).transform(center, center);
return center;
}
@Override
public void drawIcon(Graphics2D g2d, int width, int height) {
int circleSize = 5 * width / 32;
@@ -291,6 +315,23 @@ public class S1Switch extends AbstractTransparentComponent<Void> implements ISwi
this.showMarkers = showMarkers;
}
// the switching matrix is not symmetric under rotation, so the switch has to follow the
// orientation of the potentiometer it is mounted on; the default matches the default
// potentiometer orientation, with the lugs pointing down
@EditableProperty
public Orientation getOrientation() {
if (orientation == null) {
orientation = Orientation.DEFAULT;
}
return orientation;
}
public void setOrientation(Orientation orientation) {
this.orientation = orientation;
updateControlPoints();
body = null;
}
@Override
public boolean arePointsConnected(int index1, int index2, int position) {
if ((index1 == 1 && index2 == 2 && position == 0)
@@ -0,0 +1,76 @@
/*
DIY Layout Creator (DIYLC).
Copyright (c) 2009-2025 held jointly by the individual authors.
This file is part of DIYLC.
DIYLC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DIYLC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.components.transform;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import org.diylc.common.IComponentTransformer;
import org.diylc.common.Orientation;
import org.diylc.components.guitar.S1Switch;
import org.diylc.core.IDIYComponent;
public class S1SwitchTransformer implements IComponentTransformer {
@Override
public boolean canRotate(IDIYComponent<?> component) {
return component.getClass().equals(S1Switch.class);
}
@Override
public boolean canMirror(IDIYComponent<?> component) {
return false;
}
@Override
public boolean mirroringChangesCircuit() {
return false;
}
@Override
public void rotate(IDIYComponent<?> component, Point2D center, int direction) {
AffineTransform rotate =
AffineTransform.getRotateInstance(Math.PI / 2 * direction, center.getX(), center.getY());
for (int index = 0; index < component.getControlPointCount(); index++) {
Point2D p = new Point2D.Double();
rotate.transform(component.getControlPoint(index), p);
component.setControlPoint(p, index);
}
S1Switch s1Switch = (S1Switch) component;
Orientation o = s1Switch.getOrientation();
int oValue = o.ordinal();
oValue += direction;
if (oValue < 0) {
oValue = Orientation.values().length - 1;
}
if (oValue >= Orientation.values().length) {
oValue = 0;
}
o = Orientation.values()[oValue];
s1Switch.setOrientation(o);
}
@Override
public void mirror(IDIYComponent<?> component, Point2D center, int direction) {
}
}
@@ -1,7 +1,11 @@
package org.diylc.netlist;
import static org.junit.Assert.assertTrue;
import java.awt.geom.Point2D;
import org.junit.Test;
import org.diylc.common.Orientation;
import org.diylc.components.guitar.S1Switch;
public class S1SwitchTests extends AbstractSwitchTests {
@@ -14,4 +18,43 @@ public class S1SwitchTests extends AbstractSwitchTests {
testSwitch(s1Switch, validCombinations);
}
// Fender S-1 matrix as seen from the back with the pot lugs pointing right: in the up position
// the top and bottom commons connect to the lug on their right and the side commons to the lug
// above them, pressed down connects the opposite lugs
@Test
public void testS1SwitchMatchesFenderMatrixWithLugsPointingRight() {
S1Switch s1Switch = new S1Switch();
s1Switch.setOrientation(Orientation._270);
int[] commons = new int[] {2, 5, 8, 11};
double centerX = 0;
double centerY = 0;
for (int common : commons) {
centerX += s1Switch.getControlPoint(common).getX() / commons.length;
centerY += s1Switch.getControlPoint(common).getY() / commons.length;
}
for (int position = 0; position < s1Switch.getPositionCount(); position++) {
int sign = "Up".equals(s1Switch.getPositionName(position)) ? 1 : -1;
for (int common : commons) {
Point2D c = s1Switch.getControlPoint(common);
boolean horizontalStage = Math.abs(c.getY() - centerY) > Math.abs(c.getX() - centerX);
int connected = 0;
for (int i = 1; i < s1Switch.getControlPointCount(); i++) {
if (i == common || !(s1Switch.arePointsConnected(Math.min(i, common), Math.max(i, common), position))) {
continue;
}
connected++;
Point2D p = s1Switch.getControlPoint(i);
if (horizontalStage) {
assertTrue("pin " + i + " for common " + common, sign * (p.getX() - c.getX()) > 0);
} else {
assertTrue("pin " + i + " for common " + common, sign * (c.getY() - p.getY()) > 0);
}
}
assertTrue(connected == 1);
}
}
}
}