From d80c216b8cbd3a7f232c0c0426f3e9a212a83108 Mon Sep 17 00:00:00 2001 From: serivesmejia Date: Sun, 20 Jun 2021 11:42:16 -0600 Subject: [PATCH 1/6] Plugged webcam detecti detection instead of manual index & exception tracker messages --- EOCV-Sim/build.gradle | 7 +- .../gui/dialog/source/CreateCameraSource.java | 84 +++++++------------ .../eocvsim/pipeline/PipelineManager.kt | 11 +-- .../pipeline/util/PipelineExceptionTracker.kt | 19 ++++- .../eocvsim/pipeline/util/PipelineSnapshot.kt | 3 - 5 files changed, 57 insertions(+), 67 deletions(-) diff --git a/EOCV-Sim/build.gradle b/EOCV-Sim/build.gradle index dc762e68..085973c8 100644 --- a/EOCV-Sim/build.gradle +++ b/EOCV-Sim/build.gradle @@ -1,3 +1,4 @@ +import java.nio.file.Paths import java.time.LocalDateTime import java.time.format.DateTimeFormatter @@ -34,7 +35,9 @@ apply from: '../test-logging.gradle' dependencies { implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8' + implementation 'org.openpnp:opencv:4.3.0-2' + implementation 'com.github.sarxos:webcam-capture:0.3.12' implementation 'com.google.code.gson:gson:2.8.7' implementation 'io.github.classgraph:classgraph:4.8.108' @@ -55,7 +58,7 @@ task(writeBuildClassJava) { String date = DateTimeFormatter.ofPattern("yyyy-M-d hh:mm:ss").format(LocalDateTime.now()) - File versionFile = java.nio.file.Paths.get( + File versionFile = Paths.get( projectDir.absolutePath, 'src', 'main', 'java', 'com', 'github', 'serivesmejia', 'eocvsim', 'Build.java' ).toFile() @@ -79,4 +82,4 @@ task(writeBuildClassJava) { "}" } -build.dependsOn writeBuildClassJava +build.dependsOn writeBuildClassJava \ No newline at end of file diff --git a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/dialog/source/CreateCameraSource.java b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/dialog/source/CreateCameraSource.java index 4626bd23..f72f3f12 100644 --- a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/dialog/source/CreateCameraSource.java +++ b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/dialog/source/CreateCameraSource.java @@ -23,9 +23,9 @@ package com.github.serivesmejia.eocvsim.gui.dialog.source; +import com.github.sarxos.webcam.Webcam; import com.github.serivesmejia.eocvsim.EOCVSim; import com.github.serivesmejia.eocvsim.gui.component.input.SizeFields; -import com.github.serivesmejia.eocvsim.gui.util.GuiUtil; import com.github.serivesmejia.eocvsim.input.source.CameraSource; import com.github.serivesmejia.eocvsim.util.CvUtil; import com.github.serivesmejia.eocvsim.util.Log; @@ -42,20 +42,17 @@ public class CreateCameraSource { public JDialog createCameraSource = null; - public JTextField cameraIdField = null; - public JButton createButton = null; - + public JComboBox camerasComboBox = null; public SizeFields sizeFieldsInput = null; - public JTextField nameTextField = null; + public JButton createButton = null; + public boolean wasCancelled = false; - private boolean validCameraIdNumber = true; private final EOCVSim eocvSim; private State state = State.INITIAL; - private int camId = 0; JLabel statusLabel = new JLabel(); @@ -71,25 +68,30 @@ public CreateCameraSource(JFrame parent, EOCVSim eocvSim) { } public void initCreateImageSource() { + java.util.List webcams = Webcam.getWebcams(); createCameraSource.setModal(true); createCameraSource.setTitle("Create camera source"); - createCameraSource.setSize(350, 230); + createCameraSource.setSize(350, 250); JPanel contentsPanel = new JPanel(new GridLayout(5, 1)); // Camera id part - JPanel idPanel = new JPanel(new FlowLayout()); - JLabel idLabel = new JLabel("Camera Index: "); - idLabel.setHorizontalAlignment(JLabel.CENTER); + JLabel idLabel = new JLabel("Camera: "); + idLabel.setHorizontalAlignment(JLabel.LEFT); - cameraIdField = new JTextField("0", 4); + camerasComboBox = new JComboBox<>(); + for(Webcam webcam : webcams) { + camerasComboBox.addItem(webcam.getName()); + } + + SwingUtilities.invokeLater(() -> camerasComboBox.setSelectedIndex(0)); idPanel.add(idLabel); - idPanel.add(cameraIdField); + idPanel.add(camerasComboBox); contentsPanel.add(idPanel); @@ -131,27 +133,25 @@ public void initCreateImageSource() { contentsPanel.add(buttonsPanel); //Add contents - contentsPanel.setBorder(BorderFactory.createEmptyBorder(15, 0, 0, 0)); createCameraSource.getContentPane().add(contentsPanel, BorderLayout.CENTER); // Additional stuff & events - - GuiUtil.jTextFieldOnlyNumbers(cameraIdField, -100, 0); - createButton.addActionListener(e -> { if(state == State.TEST_SUCCESSFUL) { - createSource(nameTextField.getText(), camId, sizeFieldsInput.getCurrentSize()); + createSource( + nameTextField.getText(), + camerasComboBox.getSelectedIndex(), + sizeFieldsInput.getCurrentSize() + ); close(); } else { - camId = Integer.parseInt(cameraIdField.getText()); - state = State.CLICKED_TEST; updateState(); eocvSim.onMainUpdate.doOnce(() -> { - if (testCamera(camId)) { + if (testCamera(camerasComboBox.getSelectedIndex())) { if (wasCancelled) return; SwingUtilities.invokeLater(() -> { @@ -168,32 +168,13 @@ public void initCreateImageSource() { } }); - cameraIdField.getDocument().addDocumentListener(new DocumentListener() { - public void changedUpdate(DocumentEvent e) { - changed(); - } - public void removeUpdate(DocumentEvent e) { - changed(); - } - public void insertUpdate(DocumentEvent e) { - changed(); + camerasComboBox.addActionListener((e) -> { + String sourceName = (String)camerasComboBox.getSelectedItem(); + if(!eocvSim.inputSourceManager.isNameOnUse(sourceName)) { + nameTextField.setText(sourceName); } - public void changed() { - try { - Integer.parseInt(cameraIdField.getText()); - - String sourceName = "Camera " + cameraIdField.getText(); - if(!eocvSim.inputSourceManager.isNameOnUse(sourceName)) { - nameTextField.setText(sourceName); - } - - validCameraIdNumber = true; - } catch (Exception ex) { - validCameraIdNumber = false; - } - updateCreateBtt(); - } + updateCreateBtt(); }); nameTextField.getDocument().addDocumentListener(new DocumentListener() { @@ -209,8 +190,6 @@ public void changed() { } }); - SwingUtilities.invokeLater(() -> cameraIdField.setText("0")); - cancelButton.addActionListener(e -> { wasCancelled = true; close(); @@ -228,7 +207,7 @@ public void close() { public boolean testCamera(int camIndex) { VideoCapture camera = new VideoCapture(); - camera.open(camIndex); + camera.open(camerasComboBox.getSelectedIndex()); boolean wasOpened = camera.isOpened(); @@ -264,21 +243,21 @@ private void updateState() { case CLICKED_TEST: statusLabel.setText("Trying to open camera, please wait..."); - cameraIdField.setEditable(false); + camerasComboBox.setEnabled(false); createButton.setEnabled(false); break; case TEST_SUCCESSFUL: - cameraIdField.setEditable(true); + camerasComboBox.setEnabled(true); createButton.setEnabled(true); statusLabel.setText("Camera was opened successfully."); createButton.setText("Create"); break; case TEST_FAILED: - cameraIdField.setEditable(true); + camerasComboBox.setEnabled(true); createButton.setEnabled(true); - statusLabel.setText("Failed to open camera, try with another index."); + statusLabel.setText("Failed to open camera, try another one."); createButton.setText("Test"); break; } @@ -293,7 +272,6 @@ public void createSource(String sourceName, int index, Size size) { public void updateCreateBtt() { createButton.setEnabled(!nameTextField.getText().trim().equals("") - && validCameraIdNumber && sizeFieldsInput.getValid() && !eocvSim.inputSourceManager.isNameOnUse(nameTextField.getText())); diff --git a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/PipelineManager.kt b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/PipelineManager.kt index 24fef625..de7aa51a 100644 --- a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/PipelineManager.kt +++ b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/PipelineManager.kt @@ -404,18 +404,15 @@ class PipelineManager(var eocvSim: EOCVSim) { Log.info(TAG, "Instantiated pipeline class " + pipelineClass.name) } catch (ex: NoSuchMethodException) { - eocvSim.visualizer.asyncPleaseWaitDialog("Error while instantiating requested pipeline", "Check console for details", - "Close", Dimension(300, 150), true, true) - - Log.error(TAG, "Error while instantiating requested pipeline (" + pipelineClass.simpleName + ")", ex) - Log.info(TAG, "Make sure your pipeline implements a public constructor with no parameters or with a Telemetry parameter") + pipelineExceptionTracker.addMessage("Error while instantiating requested pipeline (" + pipelineClass.simpleName + ")") + pipelineExceptionTracker.addMessage("Make sure your pipeline implements a public constructor with no parameters or with a Telemetry parameter") eocvSim.visualizer.pipelineSelectorPanel.selectedIndex = currentPipelineIndex Log.blank() } catch (ex: Exception) { - eocvSim.visualizer.asyncPleaseWaitDialog("Error while instantiating requested pipeline", "Falling back to previous one", - "Close", Dimension(300, 150), true, true) + pipelineExceptionTracker.addMessage("Error while instantiating requested pipeline (" + pipelineClass.simpleName + "), falling back to previous one") + updateExceptionTracker(ex) Log.error(TAG, "Error while instantiating requested pipeline (" + pipelineClass.simpleName + ")", ex) Log.blank() diff --git a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/util/PipelineExceptionTracker.kt b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/util/PipelineExceptionTracker.kt index 1dc2c381..58845ac7 100644 --- a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/util/PipelineExceptionTracker.kt +++ b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/util/PipelineExceptionTracker.kt @@ -41,6 +41,7 @@ class PipelineExceptionTracker(private val pipelineManager: PipelineManager) { private set val exceptionsThrown = mutableMapOf() + val messages = mutableMapOf() val onPipelineException = EventHandler("OnPipelineException") val onNewPipelineException = EventHandler("OnNewPipelineException") @@ -101,6 +102,13 @@ class PipelineExceptionTracker(private val pipelineManager: PipelineManager) { } } + for((message, millisAdded) in messages.entries.toTypedArray()) { + val timeElapsed = System.currentTimeMillis() - millisAdded + if(timeElapsed >= millisExceptionExpire) { + messages.remove(message) + } + } + onUpdate.run() } @@ -116,8 +124,6 @@ class PipelineExceptionTracker(private val pipelineManager: PipelineManager) { .append("**Pipeline $pipelineName is throwing ${exceptionsThrown.size} exception(s)**") .appendLine("\n") } else { - - messageBuilder.append("**Pipeline $pipelineName ") if(pipelineManager.paused) { @@ -144,11 +150,20 @@ class PipelineExceptionTracker(private val pipelineManager: PipelineManager) { .appendLine() } + for(message in messages) { + messageBuilder.appendLine(message) + } + return messageBuilder.toString().trim() } fun clear() = exceptionsThrown.clear() + fun addMessage(s: String) { + messages[s] = System.currentTimeMillis() + onNewPipelineException.run() + } + data class PipelineException(var count: Int, val stacktrace: String, var millisThrown: Long) diff --git a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/util/PipelineSnapshot.kt b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/util/PipelineSnapshot.kt index 79608c83..c37810fe 100644 --- a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/util/PipelineSnapshot.kt +++ b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/util/PipelineSnapshot.kt @@ -121,10 +121,7 @@ class PipelineSnapshot(holdingPipeline: OpenCvPipeline) { val (otherField, otherValue) = pipelineSnapshotB.getField(field.name) ?: continue if (field.type != otherField.type) continue - println("comparing $value (${field.type}) to $otherValue (${otherField.type})") - if(otherValue != value) { - println("field $field changed") changedList.add(field) } } From 840c226562640dc6c838622e178f921b87189c06 Mon Sep 17 00:00:00 2001 From: serivesmejia Date: Sun, 20 Jun 2021 12:04:44 -0600 Subject: [PATCH 2/6] Improve pipeline instantiation error messages --- .../serivesmejia/eocvsim/pipeline/PipelineManager.kt | 10 ++++++---- .../eocvsim/pipeline/util/PipelineExceptionTracker.kt | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/PipelineManager.kt b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/PipelineManager.kt index de7aa51a..98b59ac2 100644 --- a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/PipelineManager.kt +++ b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/PipelineManager.kt @@ -404,17 +404,19 @@ class PipelineManager(var eocvSim: EOCVSim) { Log.info(TAG, "Instantiated pipeline class " + pipelineClass.name) } catch (ex: NoSuchMethodException) { - pipelineExceptionTracker.addMessage("Error while instantiating requested pipeline (" + pipelineClass.simpleName + ")") - pipelineExceptionTracker.addMessage("Make sure your pipeline implements a public constructor with no parameters or with a Telemetry parameter") + pipelineExceptionTracker.addMessage("Error while instantiating requested pipeline, \"${pipelineClass.simpleName}\". Falling back to previous one.") + pipelineExceptionTracker.addMessage("Make sure your pipeline implements a public constructor with no parameters or a Telemetry parameter.") eocvSim.visualizer.pipelineSelectorPanel.selectedIndex = currentPipelineIndex + Log.error(TAG, "Error while instantiating requested pipeline, ${pipelineClass.simpleName} (usable constructor missing)", ex) Log.blank() + return } catch (ex: Exception) { - pipelineExceptionTracker.addMessage("Error while instantiating requested pipeline (" + pipelineClass.simpleName + "), falling back to previous one") + pipelineExceptionTracker.addMessage("Error while instantiating requested pipeline, \"${pipelineClass.simpleName}\". Falling back to previous one.") updateExceptionTracker(ex) - Log.error(TAG, "Error while instantiating requested pipeline (" + pipelineClass.simpleName + ")", ex) + Log.error(TAG, "Error while instantiating requested pipeline, ${pipelineClass.simpleName} (unknown issue)", ex) Log.blank() eocvSim.visualizer.pipelineSelectorPanel.selectedIndex = currentPipelineIndex diff --git a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/util/PipelineExceptionTracker.kt b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/util/PipelineExceptionTracker.kt index 58845ac7..c763c03f 100644 --- a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/util/PipelineExceptionTracker.kt +++ b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/util/PipelineExceptionTracker.kt @@ -150,7 +150,7 @@ class PipelineExceptionTracker(private val pipelineManager: PipelineManager) { .appendLine() } - for(message in messages) { + for((message, _) in messages) { messageBuilder.appendLine(message) } From ebe4cc9f2a16a5369fb386b65e6df3139afd3784 Mon Sep 17 00:00:00 2001 From: serivesmejia Date: Sun, 20 Jun 2021 14:27:04 -0600 Subject: [PATCH 3/6] Improve right panel by using BoxLayout instead --- .../serivesmejia/eocvsim/gui/Visualizer.java | 28 +++++----- .../eocvsim/gui/component/ResponsiveJLabel.kt | 55 +++++++++++++++++++ .../eocvsim/pipeline/PipelineManager.kt | 4 +- 3 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/component/ResponsiveJLabel.kt diff --git a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/Visualizer.java b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/Visualizer.java index bdc802b1..018bd12d 100644 --- a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/Visualizer.java +++ b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/Visualizer.java @@ -148,26 +148,25 @@ public void init(Theme theme) { imgScrollPane.getHorizontalScrollBar().setUnitIncrement(16); imgScrollPane.getVerticalScrollBar().setUnitIncrement(16); - rightContainer.setLayout(new BorderLayout()); - + rightContainer.setLayout(new BoxLayout(rightContainer, BoxLayout.Y_AXIS)); /* * PIPELINE SELECTOR */ pipelineSelectorPanel.setBorder(new EmptyBorder(0, 20, 0, 20)); - rightContainer.add(pipelineSelectorPanel, BorderLayout.NORTH); + rightContainer.add(pipelineSelectorPanel); /* * SOURCE SELECTOR */ sourceSelectorPanel.setBorder(new EmptyBorder(0, 20, 0, 20)); - rightContainer.add(sourceSelectorPanel, BorderLayout.CENTER); + rightContainer.add(sourceSelectorPanel); /* * TELEMETRY */ telemetryPanel.setBorder(new EmptyBorder(0, 20, 20, 20)); - rightContainer.add(telemetryPanel, BorderLayout.SOUTH); + rightContainer.add(telemetryPanel); /* * SPLIT @@ -269,24 +268,26 @@ public void mouseClicked(MouseEvent e) { frame.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent evt) { - double ratio = frame.getSize().getHeight() / 645; + double ratioH = frame.getSize().getHeight() / 645; - double fontSize = 15.5 * ratio; - int columns = (int) Math.round(5 * ratio); + double fontSize = 17 * ratioH; + int columns = (int) Math.round(6 * ratioH); Font font = pipelineSelectorPanel.getPipelineSelectorLabel().getFont().deriveFont((float)fontSize); - pipelineSelectorPanel.getPipelineSelector().setVisibleRowCount(columns); + //pipelineSelectorPanel.getPipelineSelector().setVisibleRowCount(columns); pipelineSelectorPanel.getPipelineSelectorLabel().setFont(font); pipelineSelectorPanel.revalAndRepaint(); - columns = (int) Math.round(5 * ratio); - - sourceSelectorPanel.getSourceSelector().setVisibleRowCount(columns); + columns = (int) Math.round(7.5 * ratioH); + + //sourceSelectorPanel.getSourceSelector().setVisibleRowCount(columns); sourceSelectorPanel.getSourceSelectorLabel().setFont(font); sourceSelectorPanel.revalAndRepaint(); - telemetryPanel.getTelemetryList().setVisibleRowCount(columns); + columns = (int) Math.round(6 * ratioH); + + //telemetryPanel.getTelemetryList().setVisibleRowCount(columns); telemetryPanel.getTelemetryLabel().setFont(font); telemetryPanel.revalAndRepaint(); @@ -296,6 +297,7 @@ public void componentResized(ComponentEvent evt) { }); //stop color-picking mode when changing pipeline + // TODO: find out why this breaks everything????? //eocvSim.pipelineManager.onPipelineChange.doPersistent(() -> colorPicker.stopPicking()); } diff --git a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/component/ResponsiveJLabel.kt b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/component/ResponsiveJLabel.kt new file mode 100644 index 00000000..a07e9961 --- /dev/null +++ b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/component/ResponsiveJLabel.kt @@ -0,0 +1,55 @@ +import java.awt.* +import java.awt.font.FontRenderContext +import java.awt.font.TextLayout +import java.awt.image.BufferedImage +import javax.swing.JFrame +import javax.swing.JLabel + + +class ResponsiveJLabel(text: String) : JLabel(text) { + + private val SIZE = 256 + private var image: BufferedImage? = null + + init { + image = createImage(super.getText()) + } + + override fun setText(text: String?) { + super.setText(text) + image = createImage(super.getText()) + repaint() + } + + override fun getPreferredSize(): Dimension? { + return Dimension(image!!.width / 2, image!!.height / 2) + } + + override fun paintComponent(g: Graphics) { + super.paintComponent(g) + g.drawImage(image, 0, 0, width, height, null) + } + + private fun createImage(label: String): BufferedImage? { + val font = Font(Font.SERIF, Font.PLAIN, SIZE) + val frc = FontRenderContext(null, true, true) + val layout = TextLayout(label, font, frc) + val r = layout.getPixelBounds(null, 0f, 0f) + println(r) + val bi = BufferedImage( + r.width + 1, r.height + 1, BufferedImage.TYPE_INT_RGB + ) + val g2d = bi.graphics as Graphics2D + g2d.setRenderingHint( + RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON + ) + g2d.color = background + g2d.fillRect(0, 0, bi.width, bi.height) + g2d.color = foreground + layout.draw(g2d, 0f, -r.y.toFloat()) + g2d.dispose() + return bi + } + +} \ No newline at end of file diff --git a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/PipelineManager.kt b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/PipelineManager.kt index 98b59ac2..7535a61d 100644 --- a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/PipelineManager.kt +++ b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/PipelineManager.kt @@ -394,10 +394,10 @@ class PipelineManager(var eocvSim: EOCVSim) { try { nextTelemetry = Telemetry() - try { //instantiate pipeline if it has a constructor with a telemetry parameter + try { //instantiate pipeline if it has a constructor of a telemetry parameter constructor = pipelineClass.getConstructor(Telemetry::class.java) nextPipeline = constructor.newInstance(nextTelemetry) as OpenCvPipeline - } catch (ex: NoSuchMethodException) { //instantiating with a constructor with no params + } catch (ex: NoSuchMethodException) { //instantiating with a constructor of no params constructor = pipelineClass.getConstructor() nextPipeline = constructor.newInstance() as OpenCvPipeline } From 96203f8fb9f2f1960d08d40a9c92e1eb077a9b3d Mon Sep 17 00:00:00 2001 From: serivesmejia Date: Sun, 20 Jun 2021 15:28:29 -0600 Subject: [PATCH 4/6] Fix int tunable field with color picker --- .../serivesmejia/eocvsim/gui/Visualizer.java | 13 +---- .../eocvsim/gui/component/ResponsiveJLabel.kt | 55 ------------------- .../component/tuner/TunableFieldPanel.java | 10 +++- 3 files changed, 11 insertions(+), 67 deletions(-) delete mode 100644 EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/component/ResponsiveJLabel.kt diff --git a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/Visualizer.java b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/Visualizer.java index 018bd12d..732b6f61 100644 --- a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/Visualizer.java +++ b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/Visualizer.java @@ -271,23 +271,14 @@ public void componentResized(ComponentEvent evt) { double ratioH = frame.getSize().getHeight() / 645; double fontSize = 17 * ratioH; - int columns = (int) Math.round(6 * ratioH); - Font font = pipelineSelectorPanel.getPipelineSelectorLabel().getFont().deriveFont((float)fontSize); - //pipelineSelectorPanel.getPipelineSelector().setVisibleRowCount(columns); pipelineSelectorPanel.getPipelineSelectorLabel().setFont(font); pipelineSelectorPanel.revalAndRepaint(); - columns = (int) Math.round(7.5 * ratioH); - - //sourceSelectorPanel.getSourceSelector().setVisibleRowCount(columns); sourceSelectorPanel.getSourceSelectorLabel().setFont(font); sourceSelectorPanel.revalAndRepaint(); - columns = (int) Math.round(6 * ratioH); - - //telemetryPanel.getTelemetryList().setVisibleRowCount(columns); telemetryPanel.getTelemetryLabel().setFont(font); telemetryPanel.revalAndRepaint(); @@ -296,9 +287,9 @@ public void componentResized(ComponentEvent evt) { } }); - //stop color-picking mode when changing pipeline + // stop color-picking mode when changing pipeline // TODO: find out why this breaks everything????? - //eocvSim.pipelineManager.onPipelineChange.doPersistent(() -> colorPicker.stopPicking()); + // eocvSim.pipelineManager.onPipelineChange.doPersistent(() -> colorPicker.stopPicking()); } public boolean hasFinishedInit() { return hasFinishedInitializing; } diff --git a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/component/ResponsiveJLabel.kt b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/component/ResponsiveJLabel.kt deleted file mode 100644 index a07e9961..00000000 --- a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/component/ResponsiveJLabel.kt +++ /dev/null @@ -1,55 +0,0 @@ -import java.awt.* -import java.awt.font.FontRenderContext -import java.awt.font.TextLayout -import java.awt.image.BufferedImage -import javax.swing.JFrame -import javax.swing.JLabel - - -class ResponsiveJLabel(text: String) : JLabel(text) { - - private val SIZE = 256 - private var image: BufferedImage? = null - - init { - image = createImage(super.getText()) - } - - override fun setText(text: String?) { - super.setText(text) - image = createImage(super.getText()) - repaint() - } - - override fun getPreferredSize(): Dimension? { - return Dimension(image!!.width / 2, image!!.height / 2) - } - - override fun paintComponent(g: Graphics) { - super.paintComponent(g) - g.drawImage(image, 0, 0, width, height, null) - } - - private fun createImage(label: String): BufferedImage? { - val font = Font(Font.SERIF, Font.PLAIN, SIZE) - val frc = FontRenderContext(null, true, true) - val layout = TextLayout(label, font, frc) - val r = layout.getPixelBounds(null, 0f, 0f) - println(r) - val bi = BufferedImage( - r.width + 1, r.height + 1, BufferedImage.TYPE_INT_RGB - ) - val g2d = bi.graphics as Graphics2D - g2d.setRenderingHint( - RenderingHints.KEY_ANTIALIASING, - RenderingHints.VALUE_ANTIALIAS_ON - ) - g2d.color = background - g2d.fillRect(0, 0, bi.width, bi.height) - g2d.color = foreground - layout.draw(g2d, 0f, -r.y.toFloat()) - g2d.dispose() - return bi - } - -} \ No newline at end of file diff --git a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/component/tuner/TunableFieldPanel.java b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/component/tuner/TunableFieldPanel.java index 1a254208..71efcf45 100644 --- a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/component/tuner/TunableFieldPanel.java +++ b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/component/tuner/TunableFieldPanel.java @@ -139,7 +139,15 @@ public void showFieldPanel() { public void setFieldValue(int index, Object value) { if(index >= fields.length) return; - fields[index].setText(value.toString()); + String text; + if(tunableField.getAllowMode() == TunableField.AllowMode.ONLY_NUMBERS) { + text = String.valueOf((int) Math.round(Double.parseDouble(value.toString()))); + } else { + text = value.toString(); + } + + fields[index].setText(text); + try { sliders[index].setScaledValue(Double.parseDouble(value.toString())); } catch(NumberFormatException ignored) {} From 0883b100b678c994d113612dcec49aca2aa52a94 Mon Sep 17 00:00:00 2001 From: serivesmejia Date: Sat, 26 Jun 2021 18:15:25 -0600 Subject: [PATCH 5/6] Fix eocv sim icon inverted on dark mode --- .../github/serivesmejia/eocvsim/gui/Icons.kt | 28 +++++++++++-------- .../eocvsim/gui/dialog/Configuration.java | 2 +- .../eocvsim/pipeline/PipelineManager.kt | 4 +-- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/Icons.kt b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/Icons.kt index 0d64707a..6f30dbd9 100644 --- a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/Icons.kt +++ b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/Icons.kt @@ -31,7 +31,7 @@ import javax.swing.ImageIcon object Icons { - private val bufferedImages = HashMap() + private val bufferedImages = HashMap() private val icons = HashMap() private val resizedIcons = HashMap() @@ -43,7 +43,7 @@ object Icons { private const val TAG = "Icons" init { - addFutureImage("ico_eocvsim", "/images/icon/ico_eocvsim.png") + addFutureImage("ico_eocvsim", "/images/icon/ico_eocvsim.png", false) addFutureImage("ico_img", "/images/icon/ico_img.png") addFutureImage("ico_cam", "/images/icon/ico_cam.png") @@ -64,7 +64,7 @@ object Icons { for(futureIcon in futureIcons.toTypedArray()) { if(futureIcon.name == name) { Log.info(TAG, "Loading future icon $name") - addImage(futureIcon.name, futureIcon.resourcePath) + addImage(futureIcon.name, futureIcon.resourcePath, futureIcon.allowInvert) futureIcons.remove(futureIcon) } @@ -97,15 +97,17 @@ object Icons { return icon!! } - fun addFutureImage(name: String, path: String) = futureIcons.add(FutureIcon(name, path)) + fun addFutureImage(name: String, path: String, allowInvert: Boolean = true) = futureIcons.add( + FutureIcon(name, path, allowInvert) + ) - fun addImage(name: String, path: String) { + fun addImage(name: String, path: String, allowInvert: Boolean = true) { val buffImg = GuiUtil.loadBufferedImage(path) - if(colorsInverted) { + if(colorsInverted && allowInvert) { GuiUtil.invertBufferedImageColors(buffImg) } - bufferedImages[name] = buffImg + bufferedImages[name] = Image(buffImg, allowInvert) icons[name] = ImageIcon(buffImg) } @@ -124,11 +126,15 @@ object Icons { } private fun invertAll() { - for((_, img) in bufferedImages) { - GuiUtil.invertBufferedImageColors(img) + for((_, image) in bufferedImages) { + if(image.allowInvert) { + GuiUtil.invertBufferedImageColors(image.img) + } } } - data class FutureIcon(val name: String, val resourcePath: String) + data class Image(val img: BufferedImage, val allowInvert: Boolean) + + data class FutureIcon(val name: String, val resourcePath: String, val allowInvert: Boolean) -} +} \ No newline at end of file diff --git a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/dialog/Configuration.java b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/dialog/Configuration.java index 867b0751..f8883d67 100644 --- a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/dialog/Configuration.java +++ b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/dialog/Configuration.java @@ -66,7 +66,7 @@ private void initConfiguration() { Config config = this.eocvSim.configManager.getConfig(); configuration.setModal(true); configuration.setTitle("Settings"); - configuration.setSize(350, 300); + configuration.setSize(350, 320); JPanel themePanel = new JPanel(new FlowLayout()); JLabel themeLabel = new JLabel("Theme: "); diff --git a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/PipelineManager.kt b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/PipelineManager.kt index 7535a61d..e0c3b237 100644 --- a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/PipelineManager.kt +++ b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/pipeline/PipelineManager.kt @@ -383,8 +383,8 @@ class PipelineManager(var eocvSim: EOCVSim) { captureSnapshot() - var nextPipeline: OpenCvPipeline? = null - var nextTelemetry: Telemetry? = null + var nextPipeline: OpenCvPipeline? + var nextTelemetry: Telemetry? val pipelineClass = pipelines[index].clazz Log.info(TAG, "Changing to pipeline " + pipelineClass.name) From 96cc7c299b665e139b8410d7632cc18d2b78998e Mon Sep 17 00:00:00 2001 From: serivesmejia Date: Sun, 27 Jun 2021 00:48:40 -0600 Subject: [PATCH 6/6] Fix camera creating dialog state handling --- .../eocvsim/gui/dialog/source/CreateCameraSource.java | 1 + 1 file changed, 1 insertion(+) diff --git a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/dialog/source/CreateCameraSource.java b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/dialog/source/CreateCameraSource.java index f72f3f12..e946b79d 100644 --- a/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/dialog/source/CreateCameraSource.java +++ b/EOCV-Sim/src/main/java/com/github/serivesmejia/eocvsim/gui/dialog/source/CreateCameraSource.java @@ -174,6 +174,7 @@ public void initCreateImageSource() { nameTextField.setText(sourceName); } + state = State.INITIAL; updateCreateBtt(); });