Megaswitch highlighting

This commit is contained in:
Bane Stojkovic
2025-03-16 20:54:47 +01:00
parent f925093c80
commit 3fcc9091a7
6 changed files with 140 additions and 29 deletions
@@ -71,7 +71,7 @@ public interface ISwitch {
return null;
}
default Boolean getRenderConnectedTerminals() {
default Boolean getHighlightConnectedTerminals() {
return false;
}
}
@@ -31,7 +31,7 @@ public class SwitchUtils {
List<Set<Integer>> connectedGroups = new ArrayList<>();
// Only process if we have a selected position and should render connections
if (switchComponent.getSelectedPosition() == null || !Boolean.TRUE.equals(switchComponent.getRenderConnectedTerminals())) {
if (switchComponent.getSelectedPosition() == null || !Boolean.TRUE.equals(switchComponent.getHighlightConnectedTerminals())) {
return connectedGroups;
}
@@ -34,7 +34,7 @@ public class SwitchUtilsTest {
}
@Override
public Boolean getRenderConnectedTerminals() {
public Boolean getHighlightConnectedTerminals() {
return renderConnected;
}
@@ -83,7 +83,7 @@ public class LeverSwitch extends AbstractAngledComponent<LeverSwitch.LeverSwitch
@Deprecated
private Boolean highlightCommon;
private Integer selectedPosition;
private Boolean renderConnectedTerminals;
private Boolean highlightConnectedTerminals;
public LeverSwitch() {
super();
@@ -521,17 +521,17 @@ public class LeverSwitch extends AbstractAngledComponent<LeverSwitch.LeverSwitch
this.body = null;
}
@EditableProperty(name = "Visualize Connected")
@EditableProperty(name = "Highlight Connected")
@Override
public Boolean getRenderConnectedTerminals() {
if (renderConnectedTerminals == null) {
renderConnectedTerminals = false;
public Boolean getHighlightConnectedTerminals() {
if (highlightConnectedTerminals == null) {
highlightConnectedTerminals = false;
}
return renderConnectedTerminals;
return highlightConnectedTerminals;
}
public void setRenderConnectedTerminals(Boolean renderConnectedTerminals) {
this.renderConnectedTerminals = renderConnectedTerminals;
public void setHighlightConnectedTerminals(Boolean highlightConnectedTerminals) {
this.highlightConnectedTerminals = highlightConnectedTerminals;
this.body = null;
}
@@ -32,6 +32,9 @@ import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;
import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.awt.StringUtils;
@@ -50,6 +53,7 @@ import org.diylc.core.Project;
import org.diylc.core.Theme;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.DynamicEditableProperty;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.annotations.KeywordPolicy;
import org.diylc.core.images.IconLoader;
@@ -57,6 +61,8 @@ import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
import org.diylc.utils.Constants;
import static org.diylc.utils.SwitchUtils.getConnectedTerminals;
@ComponentDescriptor(name = "Schaller Megaswitch", category = "Guitar",
author = "Branislav Stojkovic", description = "Several variations of Schaller Megaswitch",
zOrder = IDIYComponent.COMPONENT, instanceNamePrefix = "SW",
@@ -90,6 +96,9 @@ public class SchallerMegaSwitch extends AbstractAngledComponent<String> implemen
private transient Double labelDx = null;
private transient Double labelDy = null;
private Integer selectedPosition;
private Boolean highlightConnectedTerminals;
public SchallerMegaSwitch() {
super();
updateControlPoints();
@@ -152,11 +161,12 @@ public class SchallerMegaSwitch extends AbstractAngledComponent<String> implemen
g2d.fill(body[2]);
g2d.setColor(LUG_COLOR.darker());
g2d.draw(body[2]);
// g2d.setColor(Color.black);
// for(int i = 0; i < getControlPointCount(); i++) {
// g2d.drawString(i + "", controlPoints[i].getX(), controlPoints[i].getY());
// }
for (int i = 3; i < body.length; i++) {
g2d.setColor(ISwitch.POLE_COLORS[i - 3]);
g2d.fill(body[i]);
g2d.setColor(ISwitch.POLE_COLORS[i - 3].darker());
g2d.draw(body[i]);
}
g2d.setColor(labelColor);
// g2d.setFont(project.getFont().deriveFont(LABEL_FONT_SIZE));
@@ -200,14 +210,15 @@ public class SchallerMegaSwitch extends AbstractAngledComponent<String> implemen
}
g2d.setFont(project.getFont().deriveFont((float) (project.getFont().getSize2D() * 1.25)));
StringUtils.drawCenteredText(g2d, "Schaller Megaswitch� " + type.toString(), x, y,
StringUtils.drawCenteredText(g2d, "Schaller Megaswitch " + type.toString(), x, y,
HorizontalAlignment.CENTER, VerticalAlignment.CENTER);
}
public Shape[] getBody() {
if (body == null) {
body = new Shape[4];
List<Set<Integer>> connectedTerminals = getConnectedTerminals(this, controlPoints.length);
body = new Shape[3 + connectedTerminals.size()];
int lastPointIdx =
type == MegaSwitchType.M ? controlPoints.length / 2 - 1 : controlPoints.length - 1;
@@ -251,6 +262,10 @@ public class SchallerMegaSwitch extends AbstractAngledComponent<String> implemen
double theta = getAngle().getValueRad();
for (int i = 0; i <= connectedTerminals.size(); i++) {
body[2 + i] = new Area();
}
Area terminalArea = new Area();
for (int i = 0; i < controlPoints.length; i++) {
Point2D point = controlPoints[i];
@@ -267,9 +282,13 @@ public class SchallerMegaSwitch extends AbstractAngledComponent<String> implemen
terminal.transform(rotation);
}
terminalArea.add(terminal);
int finalI = i;
int groupIndex = IntStream.range(0, connectedTerminals.size())
.filter(j -> connectedTerminals.get(j).contains(finalI))
.findFirst().orElse(-1);
((Area)body[3 + groupIndex]).add(terminal);
}
body[2] = terminalArea;
// Rotate if needed
if (theta != 0) {
@@ -457,12 +476,19 @@ public class SchallerMegaSwitch extends AbstractAngledComponent<String> implemen
}
public enum MegaSwitchType {
E("E"), E_PLUS("E+"), M("M"), P("P"), S("S"), T("T");
E("E", 5), E_PLUS("E+", 5), M("M", 5),
P("P", 5), S("S", 5), T("T", 3);
private String title;
private final int positionCount;
private MegaSwitchType(String title) {
private MegaSwitchType(String title, int positionCount) {
this.title = title;
this.positionCount = positionCount;
}
public int getPositionCount() {
return positionCount;
}
@Override
@@ -481,10 +507,7 @@ public class SchallerMegaSwitch extends AbstractAngledComponent<String> implemen
@Override
public int getPositionCount() {
if (type == MegaSwitchType.T) {
return 3;
}
return 5;
return type.getPositionCount();
}
@Override
@@ -542,6 +565,31 @@ public class SchallerMegaSwitch extends AbstractAngledComponent<String> implemen
return false;
}
@DynamicEditableProperty(source = SchallerMegaSwitchPositionPropertyValueSource.class)
@EditableProperty(name = "Selected Position")
@Override
public Integer getSelectedPosition() {
return selectedPosition;
}
public void setSelectedPosition(Integer selectedPosition) {
this.selectedPosition = selectedPosition;
this.body = null;
}
@EditableProperty(name = "Highlight Connected")
@Override
public Boolean getHighlightConnectedTerminals() {
if (highlightConnectedTerminals == null) {
highlightConnectedTerminals = false;
}
return highlightConnectedTerminals;
}
public void setHighlightConnectedTerminals(Boolean highlightConnectedTerminals) {
this.highlightConnectedTerminals = highlightConnectedTerminals;
this.body = null;
}
private static final List<List<int[]>> E_CONNECTIONS = Arrays.asList(
Arrays.asList(
@@ -0,0 +1,63 @@
package org.diylc.components.guitar;
import org.diylc.common.PropertyWrapper;
import org.diylc.core.IDIYComponent;
import org.diylc.core.IDynamicPropertySource;
import org.diylc.core.ISwitch;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class SchallerMegaSwitchPositionPropertyValueSource implements IDynamicPropertySource {
private List<PropertyWrapper> properties;
private IDIYComponent<?> component;
@Override
public void setProperties(List<PropertyWrapper> properties) {
this.properties = properties;
}
@Override
public void setComponent(IDIYComponent<?> component) {
this.component = component;
}
@Override
public List<Object> getAvailableValues() {
if (properties == null) {
return List.of();
}
Optional<PropertyWrapper> optionalPropertyWrapper = this.properties.stream().filter(p -> {
try {
return p.getGetter().getName().equals("getType");
} catch (NoSuchMethodException e) {
return false;
}
}).findFirst();
if (optionalPropertyWrapper.isEmpty()) {
return List.of();
}
SchallerMegaSwitch.MegaSwitchType
switchType = (SchallerMegaSwitch.MegaSwitchType) optionalPropertyWrapper.get().getValue();
int positionCount = switchType.getPositionCount();
List<Object> res = new ArrayList<>();
res.add(null);
for (int i = 0; i < positionCount; i++) {
res.add(i);
}
return res;
}
@Override
public String getDisplayValue(Object value) {
if (value == null) {
return "None";
}
if (component instanceof ISwitch sw) {
return sw.getPositionName((Integer)value);
}
return "#" + (((Integer)value) + 1);
}
}