<commit_before>package com.loloof64.chess_pgn_experiment

import com.loloof64.chess_core.pieces.*
import javafx.scene.control.*
import javafx.scene.image.ImageView
import javafx.scene.layout.HBox
import javafx.scene.layout.VBox
import javafx.util.Callback
import tornadofx.*

class PromotionDialog(whiteTurn: Boolean) : Dialog<PromotablePiece>() {

    companion object {
        private val picturesSize = 75.0
        private val wantedSize = 60.0
        private val picturesScales = wantedSize / picturesSize
    }

    private var selectedPieceValue: PromotablePiece = Queen(whiteTurn)

    init {
        title = "Choosing the promotion piece"
        headerText = "Choose your promotion piece"
        graphic = ImageView(if (whiteTurn) "chess_pl.png" else "chess_pd.png")

        dialogPane.buttonTypes.add(ButtonType.OK)

        val queenImageView = ImageView(if (whiteTurn) "chess_ql.png" else "chess_qd.png")
        val rookImageView = ImageView(if (whiteTurn) "chess_rl.png" else "chess_rd.png")
        val bishopImageView = ImageView(if (whiteTurn) "chess_bl.png" else "chess_bd.png")
        val knightImageView = ImageView(if (whiteTurn) "chess_nl.png" else "chess_nd.png")

        val selectionButtonsLine = HBox()
        val selectedPiecePreviewLine = HBox(Label("Selected piece"))

        arrayOf(queenImageView, rookImageView, bishopImageView, knightImageView).forEach {
            it.scaleX = picturesScales
            it.scaleY = picturesScales
        }

        var selectedPieceImageView = ImageView(if (whiteTurn) "chess_ql.png" else "chess_qd.png").apply {
            scaleX = picturesScales
            scaleY = picturesScales
        }

        val queenButton = Button("", queenImageView).apply {
            setOnAction {
                selectedPieceValue = Queen(whiteTurn)
                selectedPieceImageView = ImageView(if (whiteTurn) "chess_ql.png" else "chess_qd.png").apply {
                    scaleX = picturesScales
                    scaleY = picturesScales
                }
                selectedPiecePreviewLine.children[1] = selectedPieceImageView
            }
        }

        val rookButton = Button("", rookImageView).apply {
            setOnAction {
                selectedPieceValue = Rook(whiteTurn)
                selectedPieceImageView = ImageView(if (whiteTurn) "chess_rl.png" else "chess_rd.png").apply {
                    scaleX = picturesScales
                    scaleY = picturesScales
                }
                selectedPiecePreviewLine.children[1] = selectedPieceImageView
            }
        }

        val bishopButton = Button("", bishopImageView).apply {
            setOnAction {
                selectedPieceValue = Bishop(whiteTurn)
                selectedPieceImageView = ImageView(if (whiteTurn) "chess_bl.png" else "chess_bd.png").apply {
                    scaleX = picturesScales
                    scaleY = picturesScales
                }
                selectedPiecePreviewLine.children[1] = selectedPieceImageView
            }
        }

        val knightButton = Button("", knightImageView).apply {
            setOnAction {
                selectedPieceValue = Knight(whiteTurn)
                selectedPieceImageView = ImageView(if (whiteTurn) "chess_nl.png" else "chess_nd.png").apply {
                    scaleX = picturesScales
                    scaleY = picturesScales
                }
                selectedPiecePreviewLine.children[1] = selectedPieceImageView
            }
        }

        selectionButtonsLine.add(queenButton)
        selectionButtonsLine.add(rookButton)
        selectionButtonsLine.add(bishopButton)
        selectionButtonsLine.add(knightButton)

        selectedPiecePreviewLine.add(selectedPieceImageView)

        val customView = VBox(selectionButtonsLine, selectedPiecePreviewLine)

        dialogPane.content = customView

        resultConverter = Callback{ selectedPieceValue }
    }
}<commit_msg>Refactored the PromotionDialog class (Thanks to <NAME>, who helped me through the TornadoFX channel on Slack).<commit_after>package com.loloof64.chess_pgn_experiment

import com.loloof64.chess_core.pieces.*
import javafx.beans.property.ObjectProperty
import javafx.beans.property.SimpleObjectProperty
import javafx.scene.control.ButtonType
import javafx.scene.control.Dialog
import javafx.scene.image.Image
import javafx.scene.image.ImageView
import javafx.util.Callback
import tornadofx.*

class PromotionDialog(whiteTurn: Boolean) : Dialog<PromotablePiece>() {
    companion object {
        private val picturesSize = 75.0
        private val wantedSize = 60.0
        private val picturesScales = wantedSize / picturesSize
    }

    private val promotablePieces = mapOf(
            Queen(whiteTurn) to if (whiteTurn) "chess_ql.png" else "chess_qd.png",
            Rook(whiteTurn) to if (whiteTurn) "chess_rl.png" else "chess_rd.png",
            Bishop(whiteTurn) to if (whiteTurn) "chess_bl.png" else "chess_bd.png",
            Knight(whiteTurn) to if (whiteTurn) "chess_nl.png" else "chess_nd.png"
    )

    private val selectedPieceProperty: ObjectProperty<PromotablePiece> = SimpleObjectProperty(promotablePieces.keys.first())
    val selectedPiece: PromotablePiece get() = selectedPieceProperty.value

    init {
        title = "Choosing the promotion piece"
        headerText = "Choose your promotion piece"
        graphic = ImageView(if (whiteTurn) "chess_pl.png" else "chess_pd.png")
        dialogPane.buttonTypes.add(ButtonType.OK)

        dialogPane.content = vbox {
            hbox {
                promotablePieces.forEach { piece, image ->
                    button {
                        graphic = imageview(image, lazyload = false).scaled()
                        setOnAction { selectedPieceProperty.value = piece }
                    }
                }
            }
            hbox {
                label("Selected piece")
                imageview(promotablePieces[selectedPiece], lazyload = false) {
                    imageProperty().bind(objectBinding(selectedPieceProperty) { Image(promotablePieces[value]) })
                    scaled()
                }
            }
        }

        resultConverter = Callback { selectedPiece }
    }

    fun ImageView.scaled() = apply {
        scaleX = picturesScales
        scaleY = picturesScales
    }
}
{'fraction_non_alphanumeric': 0.0820754716981132, 'fraction_numerical': 0.0037735849056603774, 'mean_word_length': 2.3268828451882846, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5218194', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1728, 'n_words': 349}