From 3ec9b40ce75379edb28aa20cac5b50e585802a0e Mon Sep 17 00:00:00 2001 From: Divine Threepwood Date: Sat, 13 Jun 2026 02:54:03 +0200 Subject: [PATCH 1/3] implement map preview --- .../view/configuration/ConfigurationPanel.kt | 84 ++++++++++++++----- 1 file changed, 61 insertions(+), 23 deletions(-) diff --git a/src/main/kotlin/org/openbase/planetsudo/view/configuration/ConfigurationPanel.kt b/src/main/kotlin/org/openbase/planetsudo/view/configuration/ConfigurationPanel.kt index a50314bb..47e425ec 100644 --- a/src/main/kotlin/org/openbase/planetsudo/view/configuration/ConfigurationPanel.kt +++ b/src/main/kotlin/org/openbase/planetsudo/view/configuration/ConfigurationPanel.kt @@ -36,6 +36,10 @@ import java.util.* import javax.swing.* import javax.swing.border.BevelBorder import javax.swing.border.SoftBevelBorder +import javax.swing.event.ListSelectionEvent +import javax.swing.event.PopupMenuEvent +import javax.swing.event.PopupMenuListener +import javax.swing.plaf.basic.ComboPopup import kotlin.random.Random /** @@ -54,8 +58,9 @@ class ConfigurationPanel : JPanel() { levelChooserComboBox!!.addItem(levelName) } if (levelChooserComboBox!!.itemCount >= 0) { - val index = stateProperties.getProperty(PROPERTY_SELECTED_LEVEL, "0").toInt() - levelChooserComboBox!!.selectedIndex = if (index < levelChooserComboBox!!.itemCount) index else 0 + stateProperties.getProperty(PROPERTY_SELECTED_LEVEL, "0").toIntOrNull()?.let { index -> + levelChooserComboBox!!.selectedIndex = if (index < levelChooserComboBox!!.itemCount) index else 0 + } } levelChooserComboBox!!.isEnabled = true randomLevelButton!!.isEnabled = true @@ -106,11 +111,11 @@ class ConfigurationPanel : JPanel() { // restore selection if (teamAComboBox!!.itemCount > 0) { ( - selectTeam - ?: teamAComboBox!!.getItems() - .filterNotNull() - .find { it.name == stateProperties.getProperty(PROPERTY_SELECTED_TEAM_A, "") } - ) + selectTeam + ?: teamAComboBox!!.getItems() + .filterNotNull() + .find { it.name == stateProperties.getProperty(PROPERTY_SELECTED_TEAM_A, "") } + ) ?.also { teamAComboBox!!.selectedItem = it } ?: run { teamAComboBox!!.selectedIndex = 0 } } @@ -347,6 +352,33 @@ class ConfigurationPanel : JPanel() { levelChooserComboBox!!.setMaximumRowCount(20) levelChooserComboBox!!.setModel(DefaultComboBoxModel()) levelChooserComboBox!!.addActionListener { _ -> levelChooserComboBoxActionPerformed() } + levelChooserComboBox!!.addPopupMenuListener( + object : PopupMenuListener { + override fun popupMenuWillBecomeVisible(e: PopupMenuEvent?) { + val child: Any? = levelChooserComboBox!!.getAccessibleContext().getAccessibleChild(0) + + if (child is ComboPopup) { + child.list.addListSelectionListener { event: ListSelectionEvent? -> + if (!event!!.getValueIsAdjusting()) { + child.list.getSelectedValue()?.toString()?.let { levelName -> + setLevelPreview(levelName, levelIndex = child.list.selectedIndex) + } + } + } + } + } + + override fun popupMenuWillBecomeInvisible(e: PopupMenuEvent?) { + levelChooserComboBox?.selectedItem?.toString()?.let { levelName -> + levelChooserComboBox?.selectedIndex?.let { levelIndex -> + setLevelPreview(levelName = levelName, levelIndex = levelIndex) + } + } + } + + override fun popupMenuCanceled(e: PopupMenuEvent?) {} + }, + ) randomLevelButton!!.text = "Zufällig" randomLevelButton!!.addActionListener { _ -> randomLevelButtonActionPerformed() } @@ -641,22 +673,9 @@ class ConfigurationPanel : JPanel() { @Throws(Exception::class) override fun doInBackground(): Any? { synchronized(levelChooserComboBox!!) { - if (levelChooserComboBox!!.selectedItem != null) { - try { - if (levelChooserComboBox!!.isEnabled) { - stateProperties.setProperty( - PROPERTY_SELECTED_LEVEL, - levelChooserComboBox!!.selectedIndex.toString(), - ) - } - val level = getInstance()!! - .loadLevel(levelChooserComboBox!!.selectedItem!!.toString()) - gameManager.setLevel(level!!) - levelPreviewDisplayPanel!!.setLevel(level) - levelPreviewDisplayPanel!!.isOpaque = true - levelPreviewDisplayPanel!!.background = level.color - } catch (ex: CouldNotPerformException) { - logger.error("Could not update level preview!", ex) + levelChooserComboBox?.selectedItem?.toString()?.let { selectedItem -> + levelChooserComboBox?.selectedIndex?.let { selectedIndex -> + setLevelPreview(selectedItem, selectedIndex) } } } @@ -665,6 +684,25 @@ class ConfigurationPanel : JPanel() { }.execute() } // GEN-LAST:event_levelChooserComboBoxActionPerformed + private fun setLevelPreview(levelName: String, levelIndex: Int) { + try { + if (levelChooserComboBox!!.isEnabled) { + logger.error("set $levelIndex") + stateProperties.setProperty( + PROPERTY_SELECTED_LEVEL, + levelIndex.toString(), + ) + } + val level = getInstance()!!.loadLevel(levelName) + gameManager.setLevel(level!!) + levelPreviewDisplayPanel!!.setLevel(level) + levelPreviewDisplayPanel!!.isOpaque = true + levelPreviewDisplayPanel!!.background = level.color + } catch (ex: CouldNotPerformException) { + logger.error("Could not update level preview!", ex) + } + } + private fun randomLevelButtonActionPerformed() = levelChooserComboBox ?.takeIf { it.itemCount > 1 } From 6682c9d7e35d446ce74a77bd225c5e9af13f95e5 Mon Sep 17 00:00:00 2001 From: Divine Threepwood Date: Sat, 13 Jun 2026 02:54:25 +0200 Subject: [PATCH 2/3] fix formatting --- .../view/configuration/ConfigurationPanel.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/org/openbase/planetsudo/view/configuration/ConfigurationPanel.kt b/src/main/kotlin/org/openbase/planetsudo/view/configuration/ConfigurationPanel.kt index 47e425ec..182b0a08 100644 --- a/src/main/kotlin/org/openbase/planetsudo/view/configuration/ConfigurationPanel.kt +++ b/src/main/kotlin/org/openbase/planetsudo/view/configuration/ConfigurationPanel.kt @@ -111,11 +111,11 @@ class ConfigurationPanel : JPanel() { // restore selection if (teamAComboBox!!.itemCount > 0) { ( - selectTeam - ?: teamAComboBox!!.getItems() - .filterNotNull() - .find { it.name == stateProperties.getProperty(PROPERTY_SELECTED_TEAM_A, "") } - ) + selectTeam + ?: teamAComboBox!!.getItems() + .filterNotNull() + .find { it.name == stateProperties.getProperty(PROPERTY_SELECTED_TEAM_A, "") } + ) ?.also { teamAComboBox!!.selectedItem = it } ?: run { teamAComboBox!!.selectedIndex = 0 } } From f66c84b96547f9e2a389996766d3021ef9d97586 Mon Sep 17 00:00:00 2001 From: Marian Pohling Date: Tue, 16 Jun 2026 22:33:58 +0200 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: tommy33790 <118300318+tommy33790@users.noreply.github.com> --- .../planetsudo/view/configuration/ConfigurationPanel.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/org/openbase/planetsudo/view/configuration/ConfigurationPanel.kt b/src/main/kotlin/org/openbase/planetsudo/view/configuration/ConfigurationPanel.kt index 182b0a08..a3de66ec 100644 --- a/src/main/kotlin/org/openbase/planetsudo/view/configuration/ConfigurationPanel.kt +++ b/src/main/kotlin/org/openbase/planetsudo/view/configuration/ConfigurationPanel.kt @@ -359,7 +359,7 @@ class ConfigurationPanel : JPanel() { if (child is ComboPopup) { child.list.addListSelectionListener { event: ListSelectionEvent? -> - if (!event!!.getValueIsAdjusting()) { + if (!event!!.valueIsAdjusting) { child.list.getSelectedValue()?.toString()?.let { levelName -> setLevelPreview(levelName, levelIndex = child.list.selectedIndex) }