package selectingapp import javafx.geometry.Insets import javafx.geometry.Point2D import javafx.scene.Node import javafx.scene.Scene import javafx.scene.input.MouseEvent import javafx.scene.layout.AnchorPane import javafx.scene.layout.Pane import javafx.scene.paint.Color import javafx.scene.shape.Circle import tornadofx.* /** * @author carl */ class SelectingView : View("Selecting App") { var workArea : Pane by singleAssign() val circles = mutableListOf() val selectedCircles = mutableListOf() override val root = anchorpane { workArea = pane { addClass( SelectingStyles.workArea ) circle(100, 100, 50 ) { fill = Color.RED addClass( SelectingStyles.circleItem ) circles.add( this ) } circle(220, 100, 50 ) { fill = Color.BLUE addClass( SelectingStyles.circleItem ) circles.add( this ) } circle(100, 220, 50 ) { fill = Color.GREEN addClass( SelectingStyles.circleItem ) circles.add( this ) } circle(220, 220, 50 ) { fill = Color.YELLOW addClass( SelectingStyles.circleItem ) circles.add( this ) } anchorpaneConstraints { topAnchor = 0.0 bottomAnchor = 0.0 leftAnchor = 0.0 rightAnchor = 0.0 } addEventFilter(MouseEvent.MOUSE_PRESSED, ::press) } padding = Insets(10.0) } private fun press(evt : MouseEvent) { val sourceNode = evt.source as Node val parent = sourceNode.parent val topmost = sourceNode.findParentOfType(AnchorPane::class) println( "sourceNode=${sourceNode}") println( "parent=${parent}") println( "topmost=${topmost}") if( !evt.isControlDown && !evt.isShiftDown ) { circles .forEach { if( it.hasClass(SelectingStyles.selected) ) { it.removeClass(SelectingStyles.selected) } } selectedCircles.clear() } circles .filter { val mousePt : Point2D = it.sceneToLocal( evt.sceneX, evt.sceneY ) it.contains(mousePt) } .firstOrNull() .apply { if( this != null ) { this.addClass( SelectingStyles.selected ) selectedCircles.add( this ) } } } } class SelectingStyles : Stylesheet() { companion object { val workArea by cssclass() val selected by cssclass() val circleItem by cssclass() } init { workArea { backgroundColor += Color.LIGHTGRAY borderColor += box(Color.BLACK) borderWidth += box(1.px) } selected { stroke = Color.BLACK strokeWidth = 2.px } circleItem { hover { fill = Color.WHITE } } } } class SelectingApp : App(SelectingView::class, SelectingStyles::class) { override fun createPrimaryScene(view : UIComponent) = Scene(view.root, 667.0, 376.0) }