94 lines
2.6 KiB
Java
94 lines
2.6 KiB
Java
package de.ph87.electro.sidebar;
|
|
|
|
import de.ph87.electro.circuit.part.Part;
|
|
import de.ph87.electro.circuit.part.Position;
|
|
import de.ph87.electro.circuit.part.parts.*;
|
|
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.*;
|
|
|
|
public class Sidebar extends JPanel {
|
|
|
|
@Setter
|
|
private Runnable repaintCallback = null;
|
|
|
|
public Sidebar(final Runnable newCircuit, final Runnable save) {
|
|
setPreferredSize(new Dimension(0, 200));
|
|
|
|
addButton("Neu", null, newCircuit, new Color(128, 202, 255));
|
|
|
|
addToggle("Details", () -> SHOW_WIRE_DETAILS, v -> {
|
|
SHOW_WIRE_DETAILS = v;
|
|
save.run();
|
|
});
|
|
addToggle("Namen", () -> SHOW_JUNCTION_NAMES, v -> {
|
|
SHOW_JUNCTION_NAMES = v;
|
|
save.run();
|
|
});
|
|
addToggle("Spannungen", () -> SHOW_JUNCTION_VOLTAGES, v -> {
|
|
SHOW_JUNCTION_VOLTAGES = v;
|
|
save.run();
|
|
});
|
|
|
|
addPart(new Battery(null, Position.ZERO));
|
|
addPart(new ConnectorCorner(null, Position.ZERO));
|
|
addPart(new ConnectorEdge(null, Position.ZERO));
|
|
addPart(new ConnectorMiddle(null, Position.ZERO));
|
|
addPart(new Light(null, Position.ZERO));
|
|
addPart(new Switch1x1(null, Position.ZERO));
|
|
addPart(new Switch1x2(null, Position.ZERO));
|
|
addPart(new SwitchCross(null, Position.ZERO));
|
|
addPart(new Poti(null, Position.ZERO));
|
|
}
|
|
|
|
@SuppressWarnings("UnusedReturnValue")
|
|
private Button addToggle(final String label, final Supplier<Boolean> get, final Consumer<Boolean> set) {
|
|
final Consumer<Button> refresh = button -> {
|
|
if (get.get()) {
|
|
button.setBackground(new Color(128, 255, 128));
|
|
} else {
|
|
button.setBackground(new Color(255, 128, 128));
|
|
}
|
|
};
|
|
final Runnable click = () -> {
|
|
set.accept(!get.get());
|
|
triggerRepaint();
|
|
};
|
|
return addButton(label, refresh, click, null);
|
|
}
|
|
|
|
private Button addButton(final String label, final Consumer<Button> refresh, final Runnable click, final Color background) {
|
|
final Button button = new Button(label);
|
|
button.setBackground(background);
|
|
button.setPreferredSize(new Dimension(RASTER / 2, RASTER / 2));
|
|
if (refresh != null) {
|
|
refresh.accept(button);
|
|
}
|
|
button.addActionListener(e -> {
|
|
click.run();
|
|
if (refresh != null) {
|
|
refresh.accept(button);
|
|
}
|
|
});
|
|
add(button);
|
|
return button;
|
|
}
|
|
|
|
private void triggerRepaint() {
|
|
if (repaintCallback != null) {
|
|
repaintCallback.run();
|
|
}
|
|
}
|
|
|
|
private void addPart(final Part part) {
|
|
final SidebarPart entry = new SidebarPart(part);
|
|
add(entry);
|
|
}
|
|
|
|
}
|