sidebar details switches
This commit is contained in:
parent
3869293216
commit
6d34e37572
@ -6,7 +6,11 @@ import static java.lang.Math.round;
|
||||
|
||||
public class CONFIG {
|
||||
|
||||
public static boolean SHOW_DETAILS = true;
|
||||
public static boolean SHOW_WIRE_DETAILS = true;
|
||||
|
||||
public static boolean SHOW_JUNCTION_VOLTAGES = false;
|
||||
|
||||
public static boolean SHOW_JUNCTION_NAMES = false;
|
||||
|
||||
public static final double VOLTAGE_HIGH_MIN = 0.1;
|
||||
|
||||
|
||||
@ -29,6 +29,7 @@ public class Circuit {
|
||||
@Getter
|
||||
private final String created;
|
||||
|
||||
@Getter
|
||||
private final List<Part> parts = new ArrayList<>();
|
||||
|
||||
private final List<Wire> wires = new ArrayList<>();
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package de.ph87.electro.circuit;
|
||||
|
||||
import de.ph87.electro.circuit.part.Part;
|
||||
import de.ph87.electro.circuit.part.junction.Junction;
|
||||
import de.ph87.electro.circuit.part.parts.Battery;
|
||||
import de.ph87.electro.circuit.part.parts.Light;
|
||||
@ -7,6 +8,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
|
||||
import static de.ph87.electro.CONFIG.*;
|
||||
import static de.ph87.electro.circuit.part.Position.RST;
|
||||
@ -66,14 +68,36 @@ public class CircuitPanel extends JPanel {
|
||||
}
|
||||
|
||||
private void drawVoltages(final Graphics2D g) {
|
||||
circuit.streamParts().forEach(part -> {
|
||||
if (!SHOW_JUNCTION_VOLTAGES && !SHOW_JUNCTION_NAMES) {
|
||||
return;
|
||||
}
|
||||
|
||||
g.setColor(Color.BLACK);
|
||||
g.setFont(LABEL_FONT);
|
||||
final int third = g.getFont().getSize() / 3;
|
||||
|
||||
for (Part part : circuit.getParts()) {
|
||||
for (final Junction junction : part.getJunctions()) {
|
||||
final String string = "%.1fV".formatted(junction.getVoltage());
|
||||
g.setColor(Color.BLACK);
|
||||
g.setFont(LABEL_FONT);
|
||||
g.drawString(string, junction.getPosition().absolute.x, junction.getPosition().absolute.y);
|
||||
if (SHOW_JUNCTION_NAMES) {
|
||||
int offsetY = third;
|
||||
if (SHOW_JUNCTION_VOLTAGES) {
|
||||
offsetY -= third;
|
||||
}
|
||||
final String string = junction.getName();
|
||||
final Rectangle2D bounds = g.getFontMetrics().getStringBounds(string, g);
|
||||
g.drawString(string, junction.getPosition().absolute.x - (int) (bounds.getWidth() / 2), junction.getPosition().absolute.y + offsetY);
|
||||
}
|
||||
if (SHOW_JUNCTION_VOLTAGES) {
|
||||
int offsetY = third;
|
||||
if (SHOW_JUNCTION_NAMES) {
|
||||
offsetY += 2 * third;
|
||||
}
|
||||
final String string = "%.1fV".formatted(junction.getVoltage());
|
||||
final Rectangle2D bounds = g.getFontMetrics().getStringBounds(string, g);
|
||||
g.drawString(string, junction.getPosition().absolute.x - (int) (bounds.getWidth() / 2), junction.getPosition().absolute.y + offsetY);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -67,7 +67,6 @@ public class Junction {
|
||||
|
||||
public void render(final Render render) {
|
||||
render.circle(position, JUNCTION_RADIUS, Color.BLACK, JUNCTION_STROKE, color);
|
||||
render.textCenter(LABEL_FONT, name, position.inside.x, position.inside.y, Color.WHITE);
|
||||
}
|
||||
|
||||
public void updatePosition() {
|
||||
|
||||
@ -57,7 +57,7 @@ public class Wire {
|
||||
g.setStroke(WIRE_STROKE);
|
||||
g.drawLine(a.getPosition().absolute.x, a.getPosition().absolute.y, b.getPosition().absolute.x, b.getPosition().absolute.y);
|
||||
|
||||
if (SHOW_DETAILS) {
|
||||
if (SHOW_WIRE_DETAILS) {
|
||||
drawValues(g, "%.2f A".formatted(abs(current)), -0.5);
|
||||
drawValues(g, "%.2f V".formatted(a.getVoltage()), +0.5);
|
||||
}
|
||||
|
||||
@ -7,26 +7,20 @@ import lombok.Setter;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static de.ph87.electro.CONFIG.RASTER;
|
||||
import static de.ph87.electro.CONFIG.SHOW_DETAILS;
|
||||
import static de.ph87.electro.CONFIG.*;
|
||||
|
||||
public class Sidebar extends JPanel {
|
||||
|
||||
private final Button toggleDetails = new Button();
|
||||
|
||||
@Setter
|
||||
private Runnable repaintCallback = null;
|
||||
|
||||
public Sidebar() {
|
||||
toggleDetails.setPreferredSize(new Dimension(RASTER, RASTER));
|
||||
toggleDetailsSetLabel();
|
||||
toggleDetails.addActionListener(e -> {
|
||||
SHOW_DETAILS = !SHOW_DETAILS;
|
||||
toggleDetailsSetLabel();
|
||||
triggerRepaint();
|
||||
});
|
||||
add(toggleDetails);
|
||||
addToggle("Kabel Details", () -> SHOW_WIRE_DETAILS, v -> SHOW_WIRE_DETAILS = v);
|
||||
addToggle("Kontakt Namen", () -> SHOW_JUNCTION_NAMES, v -> SHOW_JUNCTION_NAMES = v);
|
||||
addToggle("Kontakt Spannungen", () -> SHOW_JUNCTION_VOLTAGES, v -> SHOW_JUNCTION_VOLTAGES = v);
|
||||
|
||||
add(new Battery(null, Position.ZERO));
|
||||
add(new ConnectorCorner(null, Position.ZERO));
|
||||
@ -40,14 +34,26 @@ public class Sidebar extends JPanel {
|
||||
setPreferredSize(new Dimension(0, 200));
|
||||
}
|
||||
|
||||
private void toggleDetailsSetLabel() {
|
||||
if (SHOW_DETAILS) {
|
||||
toggleDetails.setLabel("Details");
|
||||
toggleDetails.setBackground(new Color(128, 255, 128));
|
||||
} else {
|
||||
toggleDetails.setLabel("Details");
|
||||
toggleDetails.setBackground(new Color(255, 128, 128));
|
||||
}
|
||||
private void addToggle(final String label, final Supplier<Boolean> get, final Consumer<Boolean> set) {
|
||||
final Button button = new Button(label);
|
||||
button.setPreferredSize(new Dimension(RASTER, RASTER));
|
||||
|
||||
final Runnable toggle = () -> {
|
||||
if (get.get()) {
|
||||
button.setBackground(new Color(128, 255, 128));
|
||||
} else {
|
||||
button.setBackground(new Color(255, 128, 128));
|
||||
}
|
||||
};
|
||||
toggle.run();
|
||||
|
||||
button.addActionListener(e -> {
|
||||
set.accept(!get.get());
|
||||
toggle.run();
|
||||
triggerRepaint();
|
||||
});
|
||||
|
||||
add(button);
|
||||
}
|
||||
|
||||
private void triggerRepaint() {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user