SHOW_DETAILS

This commit is contained in:
Patrick Haßel 2024-05-22 11:56:09 +02:00
parent dfea68adaf
commit 4a335353b9
4 changed files with 44 additions and 5 deletions

View File

@ -6,6 +6,8 @@ import static java.lang.Math.round;
public class CONFIG {
public static boolean SHOW_DETAILS = false;
public static final double VOLTAGE_HIGH_MIN = 0.1;
public static final int RASTER = 200;

View File

@ -17,10 +17,12 @@ public class Window extends JFrame {
setExtendedState(MAXIMIZED_BOTH);
final Sidebar sidebar = new Sidebar();
final CircuitPanel circuitPanel = new CircuitPanel();
sidebar.setRepaintCallback(circuitPanel::repaint);
sidebar.setPreferredSize(new Dimension(calcWidth(3), 0));
sidebar.setMinimumSize(new Dimension(calcWidth(1), 0));
final CircuitPanel circuitPanel = new CircuitPanel();
circuitPanel.setPreferredSize(new Dimension(calcWidth(8), 0));
circuitPanel.setMinimumSize(new Dimension(calcWidth(2), 0));

View File

@ -10,8 +10,7 @@ import lombok.ToString;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import static de.ph87.electro.CONFIG.LABEL_FONT;
import static de.ph87.electro.CONFIG.WIRE_STROKE;
import static de.ph87.electro.CONFIG.*;
import static java.lang.Math.abs;
import static java.lang.Math.round;
@ -58,9 +57,11 @@ 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) {
drawValues(g, "%.2f A".formatted(abs(current)), -0.5);
drawValues(g, "%.2f V".formatted(a.getVoltage()), +0.5);
}
}
private void drawValues(final Graphics2D g, final String string, final double offset) {
g.setFont(LABEL_FONT);

View File

@ -2,13 +2,31 @@ package de.ph87.electro.sidebar;
import de.ph87.electro.circuit.part.Part;
import de.ph87.electro.circuit.part.parts.*;
import lombok.Setter;
import javax.swing.*;
import java.awt.*;
import static de.ph87.electro.CONFIG.RASTER;
import static de.ph87.electro.CONFIG.SHOW_DETAILS;
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);
add(new PartBattery());
add(new PartJunctionCorner());
add(new PartJunctionEdge());
@ -20,6 +38,22 @@ 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 triggerRepaint() {
if (repaintCallback != null) {
repaintCallback.run();
}
}
private void add(final Part part) {
final SidebarPart entry = new SidebarPart(part);
add(entry);